xref: /linux-6.15/scripts/checkpatch.pl (revision 7da07c31)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0
3882ea1d6SJoe Perches#
4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]>
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftuse strict;
11cb77f0d6SKamil Rytarowskiuse warnings;
12c707a81dSJoe Perchesuse POSIX;
1336061e38SJoe Perchesuse File::Basename;
1436061e38SJoe Perchesuse Cwd 'abs_path';
1557230297SJoe Perchesuse Term::ANSIColor qw(:constants);
16cd261496SGeert Uytterhoevenuse Encode qw(decode encode);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $P = $0;
1936061e38SJoe Perchesmy $D = dirname(abs_path($P));
200a920b5bSAndy Whitcroft
21000d1cc1SJoe Perchesmy $V = '0.32';
220a920b5bSAndy Whitcroft
230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
240a920b5bSAndy Whitcroft
250a920b5bSAndy Whitcroftmy $quiet = 0;
260a920b5bSAndy Whitcroftmy $tree = 1;
270a920b5bSAndy Whitcroftmy $chk_signoff = 1;
280a920b5bSAndy Whitcroftmy $chk_patch = 1;
29773647a0SAndy Whitcroftmy $tst_only;
306c72ffaaSAndy Whitcroftmy $emacs = 0;
318905a67cSAndy Whitcroftmy $terse = 0;
3234d8815fSJoe Perchesmy $showfile = 0;
336c72ffaaSAndy Whitcroftmy $file = 0;
344a593c34SDu, Changbinmy $git = 0;
350dea9f1eSJoe Perchesmy %git_commits = ();
366c72ffaaSAndy Whitcroftmy $check = 0;
372ac73b4fSJoe Perchesmy $check_orig = 0;
388905a67cSAndy Whitcroftmy $summary = 1;
398905a67cSAndy Whitcroftmy $mailback = 0;
4013214adfSAndy Whitcroftmy $summary_file = 0;
41000d1cc1SJoe Perchesmy $show_types = 0;
423beb42ecSJoe Perchesmy $list_types = 0;
433705ce5bSJoe Perchesmy $fix = 0;
449624b8d6SJoe Perchesmy $fix_inplace = 0;
456c72ffaaSAndy Whitcroftmy $root;
460f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'};
470f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot);
48c2fdda0dSAndy Whitcroftmy %debug;
493445686aSJoe Perchesmy %camelcase = ();
5091bfe484SJoe Perchesmy %use_type = ();
5191bfe484SJoe Perchesmy @use = ();
5291bfe484SJoe Perchesmy %ignore_type = ();
53000d1cc1SJoe Perchesmy @ignore = ();
5477f5b10aSHannes Edermy $help = 0;
55000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
56bdc48fa1SJoe Perchesmy $max_line_length = 100;
57d62a201fSDave Hansenmy $ignore_perl_version = 0;
58d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5956193274SVadim Bendeburymy $min_conf_desc_length = 4;
6066b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
61ebfd7d62SJoe Perchesmy $codespell = 0;
62f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
63bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
64ced69da1SQuentin Monnetmy $typedefsfile;
65737c0767SJohn Brooksmy $color = "auto";
6698005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
67dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
68dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
69713a09deSAntonio Borneomy $tabsize = 8;
703e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_";
7177f5b10aSHannes Eder
7277f5b10aSHannes Edersub help {
7377f5b10aSHannes Eder	my ($exitcode) = @_;
7477f5b10aSHannes Eder
7577f5b10aSHannes Eder	print << "EOM";
7677f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7777f5b10aSHannes EderVersion: $V
7877f5b10aSHannes Eder
7977f5b10aSHannes EderOptions:
8077f5b10aSHannes Eder  -q, --quiet                quiet
8177f5b10aSHannes Eder  --no-tree                  run without a kernel tree
8277f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
8377f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
8477f5b10aSHannes Eder  --emacs                    emacs compile window format
8577f5b10aSHannes Eder  --terse                    one line per report
8634d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
874a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
884a593c34SDu, Changbin                             single git commit with:
894a593c34SDu, Changbin                               <rev>
904a593c34SDu, Changbin                               <rev>^
914a593c34SDu, Changbin                               <rev>~n
924a593c34SDu, Changbin                             multiple git commits with:
934a593c34SDu, Changbin                               <rev1>..<rev2>
944a593c34SDu, Changbin                               <rev1>...<rev2>
954a593c34SDu, Changbin                               <rev>-<count>
964a593c34SDu, Changbin                             git merges are ignored
9777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
993beb42ecSJoe Perches  --list-types               list the possible message types
10091bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
101000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
1023beb42ecSJoe Perches  --show-types               show the specific message type in the output
103bdc48fa1SJoe Perches  --max-line-length=n        set the maximum line length, (default $max_line_length)
104bdc48fa1SJoe Perches                             if exceeded, warn on patches
105bdc48fa1SJoe Perches                             requires --strict for use with --file
10656193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
107bdc48fa1SJoe Perches  --tab-size=n               set the number of spaces for tab (default $tabsize)
10877f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10977f5b10aSHannes Eder  --no-summary               suppress the per-file summary
11077f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
11177f5b10aSHannes Eder  --summary-file             include the filename in summary
11277f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
11377f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
11477f5b10aSHannes Eder                             is all off)
11577f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
11677f5b10aSHannes Eder                             literally
1173705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1183705ce5bSJoe Perches                             If correctable single-line errors exist, create
1193705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1203705ce5bSJoe Perches                             with potential errors corrected to the preferred
1213705ce5bSJoe Perches                             checkpatch style
1229624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1239624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1249624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
125d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
126d62a201fSDave Hansen                             runtime errors.
127ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
128f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
129ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
13075ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
131737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
132737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
1333e89ad85SJerome Forissier  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
1343e89ad85SJerome Forissier                             ${CONFIG_})
13577f5b10aSHannes Eder  -h, --help, --version      display this help and exit
13677f5b10aSHannes Eder
13777f5b10aSHannes EderWhen FILE is - read standard input.
13877f5b10aSHannes EderEOM
13977f5b10aSHannes Eder
14077f5b10aSHannes Eder	exit($exitcode);
14177f5b10aSHannes Eder}
14277f5b10aSHannes Eder
1433beb42ecSJoe Perchessub uniq {
1443beb42ecSJoe Perches	my %seen;
1453beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1463beb42ecSJoe Perches}
1473beb42ecSJoe Perches
1483beb42ecSJoe Perchessub list_types {
1493beb42ecSJoe Perches	my ($exitcode) = @_;
1503beb42ecSJoe Perches
1513beb42ecSJoe Perches	my $count = 0;
1523beb42ecSJoe Perches
1533beb42ecSJoe Perches	local $/ = undef;
1543beb42ecSJoe Perches
1553beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1563beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1573beb42ecSJoe Perches
1583beb42ecSJoe Perches	my $text = <$script>;
1593beb42ecSJoe Perches	close($script);
1603beb42ecSJoe Perches
1613beb42ecSJoe Perches	my @types = ();
1620547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1630547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1643beb42ecSJoe Perches		push (@types, $_);
1653beb42ecSJoe Perches	}
1663beb42ecSJoe Perches	@types = sort(uniq(@types));
1673beb42ecSJoe Perches	print("#\tMessage type\n\n");
1683beb42ecSJoe Perches	foreach my $type (@types) {
1693beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1703beb42ecSJoe Perches	}
1713beb42ecSJoe Perches
1723beb42ecSJoe Perches	exit($exitcode);
1733beb42ecSJoe Perches}
1743beb42ecSJoe Perches
175000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
176000d1cc1SJoe Perchesif (-f $conf) {
177000d1cc1SJoe Perches	my @conf_args;
178000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
179000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
180000d1cc1SJoe Perches
181000d1cc1SJoe Perches	while (<$conffile>) {
182000d1cc1SJoe Perches		my $line = $_;
183000d1cc1SJoe Perches
184000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
185000d1cc1SJoe Perches		$line =~ s/^\s*//g;
186000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
187000d1cc1SJoe Perches
188000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
189000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
190000d1cc1SJoe Perches
191000d1cc1SJoe Perches		my @words = split(" ", $line);
192000d1cc1SJoe Perches		foreach my $word (@words) {
193000d1cc1SJoe Perches			last if ($word =~ m/^#/);
194000d1cc1SJoe Perches			push (@conf_args, $word);
195000d1cc1SJoe Perches		}
196000d1cc1SJoe Perches	}
197000d1cc1SJoe Perches	close($conffile);
198000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
199000d1cc1SJoe Perches}
200000d1cc1SJoe Perches
201737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
202737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
203737c0767SJohn Brooksforeach (@ARGV) {
204737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
205737c0767SJohn Brooks		$_ = "--color=$color";
206737c0767SJohn Brooks	}
207737c0767SJohn Brooks}
208737c0767SJohn Brooks
2090a920b5bSAndy WhitcroftGetOptions(
2106c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2110a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2120a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2130a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2146c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2158905a67cSAndy Whitcroft	'terse!'	=> \$terse,
21634d8815fSJoe Perches	'showfile!'	=> \$showfile,
21777f5b10aSHannes Eder	'f|file!'	=> \$file,
2184a593c34SDu, Changbin	'g|git!'	=> \$git,
2196c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2206c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
221000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
22291bfe484SJoe Perches	'types=s'	=> \@use,
223000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2243beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2256cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
22656193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
227713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
2286c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2298905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2308905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
23113214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2323705ce5bSJoe Perches	'fix!'		=> \$fix,
2339624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
234d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
235c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
236773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
237ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
238ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
23975ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
240737c0767SJohn Brooks	'color=s'	=> \$color,
241737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
242737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
2433e89ad85SJerome Forissier	'kconfig-prefix=s'	=> \${CONFIG_},
24477f5b10aSHannes Eder	'h|help'	=> \$help,
24577f5b10aSHannes Eder	'version'	=> \$help
24677f5b10aSHannes Eder) or help(1);
24777f5b10aSHannes Eder
24877f5b10aSHannes Ederhelp(0) if ($help);
2490a920b5bSAndy Whitcroft
2503beb42ecSJoe Percheslist_types(0) if ($list_types);
2513beb42ecSJoe Perches
2529624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2532ac73b4fSJoe Perches$check_orig = $check;
2549624b8d6SJoe Perches
25532f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
25632f30ca9SJoe Perches
2570a920b5bSAndy Whitcroftmy $exit = 0;
2580a920b5bSAndy Whitcroft
2595b57980dSJoe Perchesmy $perl_version_ok = 1;
260d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2615b57980dSJoe Perches	$perl_version_ok = 0;
262d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2635b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
264d62a201fSDave Hansen}
265d62a201fSDave Hansen
26645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2670a920b5bSAndy Whitcroftif ($#ARGV < 0) {
26845107ff6SAllen Hubbe	push(@ARGV, '-');
2690a920b5bSAndy Whitcroft}
2700a920b5bSAndy Whitcroft
271737c0767SJohn Brooksif ($color =~ /^[01]$/) {
272737c0767SJohn Brooks	$color = !$color;
273737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
274737c0767SJohn Brooks	$color = 1;
275737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
276737c0767SJohn Brooks	$color = 0;
277737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
278737c0767SJohn Brooks	$color = (-t STDOUT);
279737c0767SJohn Brooks} else {
28032f30ca9SJoe Perches	die "$P: Invalid color mode: $color\n";
281737c0767SJohn Brooks}
282737c0767SJohn Brooks
283713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
28432f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
285713a09deSAntonio Borneo
28691bfe484SJoe Perchessub hash_save_array_words {
28791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
28891bfe484SJoe Perches
28991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
29091bfe484SJoe Perches	foreach my $word (@array) {
291000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
292000d1cc1SJoe Perches		$word =~ s/^\s*//g;
293000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
294000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
295000d1cc1SJoe Perches
296000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
297000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
298000d1cc1SJoe Perches
29991bfe484SJoe Perches		$hashRef->{$word}++;
300000d1cc1SJoe Perches	}
30191bfe484SJoe Perches}
30291bfe484SJoe Perches
30391bfe484SJoe Perchessub hash_show_words {
30491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
30591bfe484SJoe Perches
3063c816e49SJoe Perches	if (keys %$hashRef) {
307d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
30858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
30991bfe484SJoe Perches			print " $word";
31091bfe484SJoe Perches		}
311d8469f16SJoe Perches		print "\n";
31291bfe484SJoe Perches	}
31391bfe484SJoe Perches}
31491bfe484SJoe Perches
31591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
31691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
317000d1cc1SJoe Perches
318c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
319c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3207429c690SAndy Whitcroftmy $dbg_type = 0;
321a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
322c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
32321caa13cSAndy Whitcroft	## no critic
32421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
32521caa13cSAndy Whitcroft	die "$@" if ($@);
326c2fdda0dSAndy Whitcroft}
327c2fdda0dSAndy Whitcroft
328d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
329d2c0a235SAndy Whitcroft
3308905a67cSAndy Whitcroftif ($terse) {
3318905a67cSAndy Whitcroft	$emacs = 1;
3328905a67cSAndy Whitcroft	$quiet++;
3338905a67cSAndy Whitcroft}
3348905a67cSAndy Whitcroft
3356c72ffaaSAndy Whitcroftif ($tree) {
3366c72ffaaSAndy Whitcroft	if (defined $root) {
3376c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3386c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3396c72ffaaSAndy Whitcroft		}
3406c72ffaaSAndy Whitcroft	} else {
3416c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3426c72ffaaSAndy Whitcroft			$root = '.';
3436c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3446c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3456c72ffaaSAndy Whitcroft			$root = $1;
3466c72ffaaSAndy Whitcroft		}
3476c72ffaaSAndy Whitcroft	}
3486c72ffaaSAndy Whitcroft
3496c72ffaaSAndy Whitcroft	if (!defined $root) {
3500a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3510a920b5bSAndy Whitcroft		exit(2);
3520a920b5bSAndy Whitcroft	}
3536c72ffaaSAndy Whitcroft}
3546c72ffaaSAndy Whitcroft
3556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3566c72ffaaSAndy Whitcroft
3572ceb532bSAndy Whitcroftour $Ident	= qr{
3582ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3592ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3602ceb532bSAndy Whitcroft		}x;
3616c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3626c72ffaaSAndy Whitcroftour $Sparse	= qr{
3636c72ffaaSAndy Whitcroft			__user|
3646c72ffaaSAndy Whitcroft			__kernel|
3656c72ffaaSAndy Whitcroft			__force|
3666c72ffaaSAndy Whitcroft			__iomem|
3676c72ffaaSAndy Whitcroft			__must_check|
368417495edSAndy Whitcroft			__kprobes|
369165e72a6SSven Eckelmann			__ref|
37033aa4597SGeert Uytterhoeven			__refconst|
37133aa4597SGeert Uytterhoeven			__refdata|
372ad315455SBoqun Feng			__rcu|
373ad315455SBoqun Feng			__private
3746c72ffaaSAndy Whitcroft		}x;
375e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3808716de38SJoe Perches
38152131292SWolfram Sang# Notes to $Attribute:
38252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3836c72ffaaSAndy Whitcroftour $Attribute	= qr{
3846c72ffaaSAndy Whitcroft			const|
38503f1df7dSJoe Perches			__percpu|
38603f1df7dSJoe Perches			__nocast|
38703f1df7dSJoe Perches			__safe|
38846d832f5SMichael S. Tsirkin			__bitwise|
38903f1df7dSJoe Perches			__packed__|
39003f1df7dSJoe Perches			__packed2__|
39103f1df7dSJoe Perches			__naked|
39203f1df7dSJoe Perches			__maybe_unused|
39303f1df7dSJoe Perches			__always_unused|
39403f1df7dSJoe Perches			__noreturn|
39503f1df7dSJoe Perches			__used|
39603f1df7dSJoe Perches			__cold|
397e23ef1f3SJoe Perches			__pure|
39803f1df7dSJoe Perches			__noclone|
39903f1df7dSJoe Perches			__deprecated|
4006c72ffaaSAndy Whitcroft			__read_mostly|
401c5967e98SJoe Perches			__ro_after_init|
4026c72ffaaSAndy Whitcroft			__kprobes|
4038716de38SJoe Perches			$InitAttribute|
40424e1d81aSAndy Whitcroft			____cacheline_aligned|
40524e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
4065fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
4075fe3af11SAndy Whitcroft			__weak
4086c72ffaaSAndy Whitcroft		  }x;
409c45dcabdSAndy Whitcroftour $Modifier;
41091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
4116c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
4126c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
4136c72ffaaSAndy Whitcroft
41495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
41595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
41695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
41795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4182435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
419c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
420326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
421326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
422326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
42374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4242435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
425326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
426447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
42723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4286c72ffaaSAndy Whitcroftour $Operators	= qr{
4296c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4306c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
43123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4326c72ffaaSAndy Whitcroft		  }x;
4336c72ffaaSAndy Whitcroft
43491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
43591cb5195SJoe Perches
436ab7e23f3SJoe Perchesour $BasicType;
4378905a67cSAndy Whitcroftour $NonptrType;
4381813087dSJoe Perchesour $NonptrTypeMisordered;
4398716de38SJoe Perchesour $NonptrTypeWithAttr;
4408905a67cSAndy Whitcroftour $Type;
4411813087dSJoe Perchesour $TypeMisordered;
4428905a67cSAndy Whitcroftour $Declare;
4431813087dSJoe Perchesour $DeclareMisordered;
4448905a67cSAndy Whitcroft
44515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
44615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
447171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
448171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
449171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
450171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
451171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
452171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
453171ae1a4SAndy Whitcroft}x;
454171ae1a4SAndy Whitcroft
45515662b3eSJoe Perchesour $UTF8	= qr{
45615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
45715662b3eSJoe Perches	| $NON_ASCII_UTF8
45815662b3eSJoe Perches}x;
45915662b3eSJoe Perches
460e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
461021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
462021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
463021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
464021158b4SJoe Perches)};
465e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
466fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4678ed22cadSAndy Whitcroft	atomic_t
4688ed22cadSAndy Whitcroft)};
469e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
470e6176fa4SJoe Perches	$typeC99Typedefs\b|
471e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
472e6176fa4SJoe Perches	$typeKernelTypedefs\b
473e6176fa4SJoe Perches)};
4748ed22cadSAndy Whitcroft
4756d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4766d32f7a3SJoe Perches
477691e669bSJoe Perchesour $logFunctions = qr{(?x:
478758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4797d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
48087bd499aSJoe Perches	TP_printk|
4816e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
482b0531722SJoe Perches	panic|
48306668727SJoe Perches	MODULE_[A-Z_]+|
48406668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
485691e669bSJoe Perches)};
486691e669bSJoe Perches
487e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
488e29a70f1SJoe Perches	(?:(?:devm_)?
489e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
490e29a70f1SJoe Perches		kstrdup(?:_const)? |
491e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
492461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
493e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
494e29a70f1SJoe Perches	dma_alloc_coherent
495e29a70f1SJoe Perches)};
496e29a70f1SJoe Perches
49720112475SJoe Perchesour $signature_tags = qr{(?xi:
49820112475SJoe Perches	Signed-off-by:|
499d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
50020112475SJoe Perches	Acked-by:|
50120112475SJoe Perches	Tested-by:|
50220112475SJoe Perches	Reviewed-by:|
50320112475SJoe Perches	Reported-by:|
5048543ae12SMugunthan V N	Suggested-by:|
50520112475SJoe Perches	To:|
50620112475SJoe Perches	Cc:
50720112475SJoe Perches)};
50820112475SJoe Perches
509831242abSAditya Srivastavasub edit_distance_min {
510831242abSAditya Srivastava	my (@arr) = @_;
511831242abSAditya Srivastava	my $len = scalar @arr;
512831242abSAditya Srivastava	if ((scalar @arr) < 1) {
513831242abSAditya Srivastava		# if underflow, return
514831242abSAditya Srivastava		return;
515831242abSAditya Srivastava	}
516831242abSAditya Srivastava	my $min = $arr[0];
517831242abSAditya Srivastava	for my $i (0 .. ($len-1)) {
518831242abSAditya Srivastava		if ($arr[$i] < $min) {
519831242abSAditya Srivastava			$min = $arr[$i];
520831242abSAditya Srivastava		}
521831242abSAditya Srivastava	}
522831242abSAditya Srivastava	return $min;
523831242abSAditya Srivastava}
524831242abSAditya Srivastava
525831242abSAditya Srivastavasub get_edit_distance {
526831242abSAditya Srivastava	my ($str1, $str2) = @_;
527831242abSAditya Srivastava	$str1 = lc($str1);
528831242abSAditya Srivastava	$str2 = lc($str2);
529831242abSAditya Srivastava	$str1 =~ s/-//g;
530831242abSAditya Srivastava	$str2 =~ s/-//g;
531831242abSAditya Srivastava	my $len1 = length($str1);
532831242abSAditya Srivastava	my $len2 = length($str2);
533831242abSAditya Srivastava	# two dimensional array storing minimum edit distance
534831242abSAditya Srivastava	my @distance;
535831242abSAditya Srivastava	for my $i (0 .. $len1) {
536831242abSAditya Srivastava		for my $j (0 .. $len2) {
537831242abSAditya Srivastava			if ($i == 0) {
538831242abSAditya Srivastava				$distance[$i][$j] = $j;
539831242abSAditya Srivastava			} elsif ($j == 0) {
540831242abSAditya Srivastava				$distance[$i][$j] = $i;
541831242abSAditya Srivastava			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
542831242abSAditya Srivastava				$distance[$i][$j] = $distance[$i - 1][$j - 1];
543831242abSAditya Srivastava			} else {
544831242abSAditya Srivastava				my $dist1 = $distance[$i][$j - 1]; #insert distance
545831242abSAditya Srivastava				my $dist2 = $distance[$i - 1][$j]; # remove
546831242abSAditya Srivastava				my $dist3 = $distance[$i - 1][$j - 1]; #replace
547831242abSAditya Srivastava				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
548831242abSAditya Srivastava			}
549831242abSAditya Srivastava		}
550831242abSAditya Srivastava	}
551831242abSAditya Srivastava	return $distance[$len1][$len2];
552831242abSAditya Srivastava}
553831242abSAditya Srivastava
554831242abSAditya Srivastavasub find_standard_signature {
555831242abSAditya Srivastava	my ($sign_off) = @_;
556831242abSAditya Srivastava	my @standard_signature_tags = (
557831242abSAditya Srivastava		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
558831242abSAditya Srivastava		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
559831242abSAditya Srivastava	);
560831242abSAditya Srivastava	foreach my $signature (@standard_signature_tags) {
561831242abSAditya Srivastava		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
562831242abSAditya Srivastava	}
563831242abSAditya Srivastava
564831242abSAditya Srivastava	return "";
565831242abSAditya Srivastava}
566831242abSAditya Srivastava
5671813087dSJoe Perchesour @typeListMisordered = (
5681813087dSJoe Perches	qr{char\s+(?:un)?signed},
5691813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
5701813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
5711813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
5721813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
5731813087dSJoe Perches	qr{short\s+(?:un)?signed},
5741813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5751813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5761813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5771813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5781813087dSJoe Perches	qr{int\s+(?:un)?signed},
5791813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5801813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5811813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5821813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5831813087dSJoe Perches	qr{long\s+(?:un)?signed},
5841813087dSJoe Perches);
5851813087dSJoe Perches
5868905a67cSAndy Whitcroftour @typeList = (
5878905a67cSAndy Whitcroft	qr{void},
5880c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5890c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5900c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5910c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5920c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5930c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5940c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5950c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5960c773d9dSJoe Perches	qr{(?:un)?signed},
5978905a67cSAndy Whitcroft	qr{float},
5988905a67cSAndy Whitcroft	qr{double},
5998905a67cSAndy Whitcroft	qr{bool},
6008905a67cSAndy Whitcroft	qr{struct\s+$Ident},
6018905a67cSAndy Whitcroft	qr{union\s+$Ident},
6028905a67cSAndy Whitcroft	qr{enum\s+$Ident},
6038905a67cSAndy Whitcroft	qr{${Ident}_t},
6048905a67cSAndy Whitcroft	qr{${Ident}_handler},
6058905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
6061813087dSJoe Perches	@typeListMisordered,
6078905a67cSAndy Whitcroft);
608938224b5SJoe Perches
609938224b5SJoe Perchesour $C90_int_types = qr{(?x:
610938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
611938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
612938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
613938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
614938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
615938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
616938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
617938224b5SJoe Perches
618938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
619938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
620938224b5SJoe Perches	long\s+(?:un)?signed|
621938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
622938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
623938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
624938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
625938224b5SJoe Perches
626938224b5SJoe Perches	int\s+(?:un)?signed|
627938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
628938224b5SJoe Perches)};
629938224b5SJoe Perches
630485ff23eSAlex Dowadour @typeListFile = ();
6318716de38SJoe Perchesour @typeListWithAttr = (
6328716de38SJoe Perches	@typeList,
6338716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
6348716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
6358716de38SJoe Perches);
6368716de38SJoe Perches
637c45dcabdSAndy Whitcroftour @modifierList = (
638c45dcabdSAndy Whitcroft	qr{fastcall},
639c45dcabdSAndy Whitcroft);
640485ff23eSAlex Dowadour @modifierListFile = ();
6418905a67cSAndy Whitcroft
6422435880fSJoe Perchesour @mode_permission_funcs = (
6432435880fSJoe Perches	["module_param", 3],
6442435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
6452435880fSJoe Perches	["module_param_array_named", 5],
6462435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
6472435880fSJoe Perches	["proc_create(?:_data|)", 2],
648459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
649459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
650459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
651459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
652459cf0aeSJoe Perches	["__ATTR", 2],
6532435880fSJoe Perches);
6542435880fSJoe Perches
6551a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
6561a3dcf2eSJoe Perches
657515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
658515a235eSJoe Perchesour $mode_perms_search = "";
659515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
660515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
661515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
662515a235eSJoe Perches}
66300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
664515a235eSJoe Perches
6659189c7e7SJoe Perchesour %deprecated_apis = (
6669189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
6679189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
6689189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
6699189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
6709189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
6719189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
6729189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
6739189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
6749189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
6759189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6769189c7e7SJoe Perches);
6779189c7e7SJoe Perches
6789189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6799189c7e7SJoe Perchesour $deprecated_apis_search = "";
6809189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6819189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6829189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6839189c7e7SJoe Perches}
6849189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6859189c7e7SJoe Perches
686b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
687b392c64fSJoe Perches	S_IWUGO		|
688b392c64fSJoe Perches	S_IWOTH		|
689b392c64fSJoe Perches	S_IRWXUGO	|
690b392c64fSJoe Perches	S_IALLUGO	|
691b392c64fSJoe Perches	0[0-7][0-7][2367]
692b392c64fSJoe Perches}x;
693b392c64fSJoe Perches
694f90774e1SJoe Perchesour %mode_permission_string_types = (
695f90774e1SJoe Perches	"S_IRWXU" => 0700,
696f90774e1SJoe Perches	"S_IRUSR" => 0400,
697f90774e1SJoe Perches	"S_IWUSR" => 0200,
698f90774e1SJoe Perches	"S_IXUSR" => 0100,
699f90774e1SJoe Perches	"S_IRWXG" => 0070,
700f90774e1SJoe Perches	"S_IRGRP" => 0040,
701f90774e1SJoe Perches	"S_IWGRP" => 0020,
702f90774e1SJoe Perches	"S_IXGRP" => 0010,
703f90774e1SJoe Perches	"S_IRWXO" => 0007,
704f90774e1SJoe Perches	"S_IROTH" => 0004,
705f90774e1SJoe Perches	"S_IWOTH" => 0002,
706f90774e1SJoe Perches	"S_IXOTH" => 0001,
707f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
708f90774e1SJoe Perches	"S_IRUGO" => 0444,
709f90774e1SJoe Perches	"S_IWUGO" => 0222,
710f90774e1SJoe Perches	"S_IXUGO" => 0111,
711f90774e1SJoe Perches);
712f90774e1SJoe Perches
713f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
714f90774e1SJoe Perchesour $mode_perms_string_search = "";
715f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
716f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
717f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
718f90774e1SJoe Perches}
71900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
72000180468SJoe Perchesour $multi_mode_perms_string_search = qr{
72100180468SJoe Perches	${single_mode_perms_string_search}
72200180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
72300180468SJoe Perches}x;
72400180468SJoe Perches
72500180468SJoe Perchessub perms_to_octal {
72600180468SJoe Perches	my ($string) = @_;
72700180468SJoe Perches
72800180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
72900180468SJoe Perches
73000180468SJoe Perches	my $val = "";
73100180468SJoe Perches	my $oval = "";
73200180468SJoe Perches	my $to = 0;
73300180468SJoe Perches	my $curpos = 0;
73400180468SJoe Perches	my $lastpos = 0;
73500180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
73600180468SJoe Perches		$curpos = pos($string);
73700180468SJoe Perches		my $match = $2;
73800180468SJoe Perches		my $omatch = $1;
73900180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
74000180468SJoe Perches		$lastpos = $curpos;
74100180468SJoe Perches		$to |= $mode_permission_string_types{$match};
74200180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
74300180468SJoe Perches		$val .= $match;
74400180468SJoe Perches		$oval .= $omatch;
74500180468SJoe Perches	}
74600180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
74700180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
74800180468SJoe Perches	return sprintf("%04o", $to);
74900180468SJoe Perches}
750f90774e1SJoe Perches
7517840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
7527840a94cSWolfram Sang	irq|
753cdcee686SSergey Ryazanov	memory|
754cdcee686SSergey Ryazanov	time|
755cdcee686SSergey Ryazanov	reboot
7567840a94cSWolfram Sang)};
7577840a94cSWolfram Sang# memory.h: ARM has a custom one
7587840a94cSWolfram Sang
75966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
76066b47b4aSKees Cookmy $misspellings;
76166b47b4aSKees Cookmy %spelling_fix;
76236061e38SJoe Perches
76336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
76466b47b4aSKees Cook	while (<$spelling>) {
76566b47b4aSKees Cook		my $line = $_;
76666b47b4aSKees Cook
76766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
76866b47b4aSKees Cook		$line =~ s/^\s*//g;
76966b47b4aSKees Cook
77066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
77166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
77266b47b4aSKees Cook
77366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
77466b47b4aSKees Cook
77566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
77666b47b4aSKees Cook	}
77766b47b4aSKees Cook	close($spelling);
77836061e38SJoe Perches} else {
77936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
78036061e38SJoe Perches}
78166b47b4aSKees Cook
782ebfd7d62SJoe Perchesif ($codespell) {
783ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
784ebfd7d62SJoe Perches		while (<$spelling>) {
785ebfd7d62SJoe Perches			my $line = $_;
786ebfd7d62SJoe Perches
787ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
788ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
789ebfd7d62SJoe Perches
790ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
791ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
792ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
793ebfd7d62SJoe Perches
794ebfd7d62SJoe Perches			$line =~ s/,.*$//;
795ebfd7d62SJoe Perches
796ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
797ebfd7d62SJoe Perches
798ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
799ebfd7d62SJoe Perches		}
800ebfd7d62SJoe Perches		close($spelling);
801ebfd7d62SJoe Perches	} else {
802ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
803ebfd7d62SJoe Perches	}
804ebfd7d62SJoe Perches}
805ebfd7d62SJoe Perches
806ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
807ebfd7d62SJoe Perches
80875ad8c57SJerome Forissiersub read_words {
80975ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
81075ad8c57SJerome Forissier
81175ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
81275ad8c57SJerome Forissier		while (<$words>) {
813bf1fa1daSJoe Perches			my $line = $_;
814bf1fa1daSJoe Perches
815bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
816bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
817bf1fa1daSJoe Perches
818bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
819bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
820bf1fa1daSJoe Perches			if ($line =~ /\s/) {
82175ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
822bf1fa1daSJoe Perches				next;
823bf1fa1daSJoe Perches			}
824bf1fa1daSJoe Perches
825ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
82675ad8c57SJerome Forissier			$$wordsRef .= $line;
827bf1fa1daSJoe Perches		}
82875ad8c57SJerome Forissier		close($file);
82975ad8c57SJerome Forissier		return 1;
830bf1fa1daSJoe Perches	}
831bf1fa1daSJoe Perches
83275ad8c57SJerome Forissier	return 0;
83375ad8c57SJerome Forissier}
83475ad8c57SJerome Forissier
835ced69da1SQuentin Monnetmy $const_structs;
836ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
83775ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
83875ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
839ced69da1SQuentin Monnet}
84075ad8c57SJerome Forissier
841ced69da1SQuentin Monnetif (defined($typedefsfile)) {
842ced69da1SQuentin Monnet	my $typeOtherTypedefs;
84375ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
84475ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
845ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
84675ad8c57SJerome Forissier}
84775ad8c57SJerome Forissier
8488905a67cSAndy Whitcroftsub build_types {
849485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
850485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
8511813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
8528716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
853c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
854ab7e23f3SJoe Perches	$BasicType	= qr{
855ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
856ab7e23f3SJoe Perches				(?:${all}\b)
857ab7e23f3SJoe Perches		}x;
8588905a67cSAndy Whitcroft	$NonptrType	= qr{
859d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
860cf655043SAndy Whitcroft			(?:
8616b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
8628ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
863c45dcabdSAndy Whitcroft				(?:${all}\b)
864cf655043SAndy Whitcroft			)
865c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
8668905a67cSAndy Whitcroft		  }x;
8671813087dSJoe Perches	$NonptrTypeMisordered	= qr{
8681813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
8691813087dSJoe Perches			(?:
8701813087dSJoe Perches				(?:${Misordered}\b)
8711813087dSJoe Perches			)
8721813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
8731813087dSJoe Perches		  }x;
8748716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
8758716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
8768716de38SJoe Perches			(?:
8778716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8788716de38SJoe Perches				(?:$typeTypedefs\b)|
8798716de38SJoe Perches				(?:${allWithAttr}\b)
8808716de38SJoe Perches			)
8818716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8828716de38SJoe Perches		  }x;
8838905a67cSAndy Whitcroft	$Type	= qr{
884c45dcabdSAndy Whitcroft			$NonptrType
8857b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
886c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8878905a67cSAndy Whitcroft		  }x;
8881813087dSJoe Perches	$TypeMisordered	= qr{
8891813087dSJoe Perches			$NonptrTypeMisordered
8907b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
8911813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8921813087dSJoe Perches		  }x;
89391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8941813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8958905a67cSAndy Whitcroft}
8968905a67cSAndy Whitcroftbuild_types();
8976c72ffaaSAndy Whitcroft
8987d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
899d1fe9c09SJoe Perches
900d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
901d1fe9c09SJoe Perches# requires at least perl version v5.10.0
902d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
903d1fe9c09SJoe Perches
904d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
9052435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
906c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
9077d2367afSJoe Perches
908f8422308SJoe Perchesour $declaration_macros = qr{(?x:
9093e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
910fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
9113d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
912f8422308SJoe Perches)};
913f8422308SJoe Perches
9148d0325ccSAditya Srivastavaour %allow_repeated_words = (
9158d0325ccSAditya Srivastava	add => '',
9168d0325ccSAditya Srivastava	added => '',
9178d0325ccSAditya Srivastava	bad => '',
9188d0325ccSAditya Srivastava	be => '',
9198d0325ccSAditya Srivastava);
9208d0325ccSAditya Srivastava
9217d2367afSJoe Perchessub deparenthesize {
9227d2367afSJoe Perches	my ($string) = @_;
9237d2367afSJoe Perches	return "" if (!defined($string));
9245b9553abSJoe Perches
9255b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
9265b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
9275b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
9285b9553abSJoe Perches	}
9295b9553abSJoe Perches
9307d2367afSJoe Perches	$string =~ s@\s+@ @g;
9315b9553abSJoe Perches
9327d2367afSJoe Perches	return $string;
9337d2367afSJoe Perches}
9347d2367afSJoe Perches
9353445686aSJoe Perchessub seed_camelcase_file {
9363445686aSJoe Perches	my ($file) = @_;
9373445686aSJoe Perches
9383445686aSJoe Perches	return if (!(-f $file));
9393445686aSJoe Perches
9403445686aSJoe Perches	local $/;
9413445686aSJoe Perches
9423445686aSJoe Perches	open(my $include_file, '<', "$file")
9433445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
9443445686aSJoe Perches	my $text = <$include_file>;
9453445686aSJoe Perches	close($include_file);
9463445686aSJoe Perches
9473445686aSJoe Perches	my @lines = split('\n', $text);
9483445686aSJoe Perches
9493445686aSJoe Perches	foreach my $line (@lines) {
9503445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
9513445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
9523445686aSJoe Perches			$camelcase{$1} = 1;
95311ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
95411ea516aSJoe Perches			$camelcase{$1} = 1;
95511ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
9563445686aSJoe Perches			$camelcase{$1} = 1;
9573445686aSJoe Perches		}
9583445686aSJoe Perches	}
9593445686aSJoe Perches}
9603445686aSJoe Perches
961cd28b119SJoe Perchesour %maintained_status = ();
962cd28b119SJoe Perches
96385b0ee18SJoe Perchessub is_maintained_obsolete {
96485b0ee18SJoe Perches	my ($filename) = @_;
96585b0ee18SJoe Perches
966f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
96785b0ee18SJoe Perches
968cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
969cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
970cd28b119SJoe Perches	}
97185b0ee18SJoe Perches
972cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
97385b0ee18SJoe Perches}
97485b0ee18SJoe Perches
9753b6e8ac9SJoe Perchessub is_SPDX_License_valid {
9763b6e8ac9SJoe Perches	my ($license) = @_;
9773b6e8ac9SJoe Perches
9780f7f635bSJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
9793b6e8ac9SJoe Perches
98056294112SJoe Perches	my $root_path = abs_path($root);
98156294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
9823b6e8ac9SJoe Perches	return 0 if ($status ne "");
9833b6e8ac9SJoe Perches	return 1;
9843b6e8ac9SJoe Perches}
9853b6e8ac9SJoe Perches
9863445686aSJoe Perchesmy $camelcase_seeded = 0;
9873445686aSJoe Perchessub seed_camelcase_includes {
9883445686aSJoe Perches	return if ($camelcase_seeded);
9893445686aSJoe Perches
9903445686aSJoe Perches	my $files;
991c707a81dSJoe Perches	my $camelcase_cache = "";
992c707a81dSJoe Perches	my @include_files = ();
993c707a81dSJoe Perches
994c707a81dSJoe Perches	$camelcase_seeded = 1;
995351b2a1fSJoe Perches
9960f7f635bSJoe Perches	if (-e "$gitroot") {
997dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
998351b2a1fSJoe Perches		chomp $git_last_include_commit;
999c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1000c707a81dSJoe Perches	} else {
1001c707a81dSJoe Perches		my $last_mod_date = 0;
1002c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
1003c707a81dSJoe Perches		@include_files = split('\n', $files);
1004c707a81dSJoe Perches		foreach my $file (@include_files) {
1005c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
1006c707a81dSJoe Perches						   localtime((stat $file)[9]));
1007c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
1008c707a81dSJoe Perches		}
1009c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1010c707a81dSJoe Perches	}
1011c707a81dSJoe Perches
1012c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
1013c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
1014c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
1015351b2a1fSJoe Perches		while (<$camelcase_file>) {
1016351b2a1fSJoe Perches			chomp;
1017351b2a1fSJoe Perches			$camelcase{$_} = 1;
1018351b2a1fSJoe Perches		}
1019351b2a1fSJoe Perches		close($camelcase_file);
1020351b2a1fSJoe Perches
1021351b2a1fSJoe Perches		return;
1022351b2a1fSJoe Perches	}
1023c707a81dSJoe Perches
10240f7f635bSJoe Perches	if (-e "$gitroot") {
1025dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
1026c707a81dSJoe Perches		@include_files = split('\n', $files);
10273445686aSJoe Perches	}
1028c707a81dSJoe Perches
10293445686aSJoe Perches	foreach my $file (@include_files) {
10303445686aSJoe Perches		seed_camelcase_file($file);
10313445686aSJoe Perches	}
1032351b2a1fSJoe Perches
1033c707a81dSJoe Perches	if ($camelcase_cache ne "") {
1034351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
1035c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
1036c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
1037351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1038351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
1039351b2a1fSJoe Perches		}
1040351b2a1fSJoe Perches		close($camelcase_file);
1041351b2a1fSJoe Perches	}
10423445686aSJoe Perches}
10433445686aSJoe Perches
1044f5f61325SJoe Perchessub git_is_single_file {
1045f5f61325SJoe Perches	my ($filename) = @_;
1046f5f61325SJoe Perches
1047f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1048f5f61325SJoe Perches
1049f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1050f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
1051f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
1052f5f61325SJoe Perches}
1053f5f61325SJoe Perches
1054d311cd44SJoe Perchessub git_commit_info {
1055d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
1056d311cd44SJoe Perches
10570f7f635bSJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1058d311cd44SJoe Perches
1059dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1060d311cd44SJoe Perches	$output =~ s/^\s*//gm;
1061d311cd44SJoe Perches	my @lines = split("\n", $output);
1062d311cd44SJoe Perches
10630d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
10640d7835fcSJoe Perches
10655a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1066d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1067d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1068d311cd44SJoe Perches#
1069d311cd44SJoe Perches#		echo "checking commits $1..."
1070d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1071d311cd44SJoe Perches#		while read line ; do
1072d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1073d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1074d311cd44SJoe Perches#		done
1075d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
1076948b133aSHeinrich Schuchardt		$id = undef;
1077d311cd44SJoe Perches	} else {
1078d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1079d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1080d311cd44SJoe Perches	}
1081d311cd44SJoe Perches
1082d311cd44SJoe Perches	return ($id, $desc);
1083d311cd44SJoe Perches}
1084d311cd44SJoe Perches
10856c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
10860a920b5bSAndy Whitcroft
108700df344fSAndy Whitcroftmy @rawlines = ();
1088c2fdda0dSAndy Whitcroftmy @lines = ();
10893705ce5bSJoe Perchesmy @fixed = ();
1090d752fcc8SJoe Perchesmy @fixed_inserted = ();
1091d752fcc8SJoe Perchesmy @fixed_deleted = ();
1092194f66fcSJoe Perchesmy $fixlinenr = -1;
1093194f66fcSJoe Perches
10944a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10954a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10960f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
10974a593c34SDu, Changbin
10984a593c34SDu, Changbinif ($git) {
10994a593c34SDu, Changbin	my @commits = ();
11000dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
11014a593c34SDu, Changbin		my $git_range;
110228898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
110328898fd1SJoe Perches			$git_range = "-$2 $1";
11044a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
11054a593c34SDu, Changbin			$git_range = "$commit_expr";
11064a593c34SDu, Changbin		} else {
11070dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
11080dea9f1eSJoe Perches		}
1109dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
11100dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
111128898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
111228898fd1SJoe Perches			next if (!defined($1) || !defined($2));
11130dea9f1eSJoe Perches			my $sha1 = $1;
11140dea9f1eSJoe Perches			my $subject = $2;
11150dea9f1eSJoe Perches			unshift(@commits, $sha1);
11160dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
11174a593c34SDu, Changbin		}
11184a593c34SDu, Changbin	}
11194a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
11204a593c34SDu, Changbin	@ARGV = @commits;
11214a593c34SDu, Changbin}
11224a593c34SDu, Changbin
1123c2fdda0dSAndy Whitcroftmy $vname;
112498005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
11256c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
112621caa13cSAndy Whitcroft	my $FILE;
1127f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1128f5f61325SJoe Perches	my $oldfile = $file;
1129f5f61325SJoe Perches	$file = 1 if ($is_git_file);
11304a593c34SDu, Changbin	if ($git) {
11314a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
11324a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
11334a593c34SDu, Changbin	} elsif ($file) {
113421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
11356c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
113621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
113721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
11386c72ffaaSAndy Whitcroft	} else {
113921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
11406c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
11416c72ffaaSAndy Whitcroft	}
1142c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1143c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
11444a593c34SDu, Changbin	} elsif ($git) {
11450dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1146c2fdda0dSAndy Whitcroft	} else {
1147c2fdda0dSAndy Whitcroft		$vname = $filename;
1148c2fdda0dSAndy Whitcroft	}
114921caa13cSAndy Whitcroft	while (<$FILE>) {
11500a920b5bSAndy Whitcroft		chomp;
115100df344fSAndy Whitcroft		push(@rawlines, $_);
1152c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
11536c72ffaaSAndy Whitcroft	}
115421caa13cSAndy Whitcroft	close($FILE);
1155d8469f16SJoe Perches
1156d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1157d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1158d8469f16SJoe Perches		print "$vname\n";
1159d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1160d8469f16SJoe Perches	}
1161d8469f16SJoe Perches
1162c2fdda0dSAndy Whitcroft	if (!process($filename)) {
11630a920b5bSAndy Whitcroft		$exit = 1;
11640a920b5bSAndy Whitcroft	}
116500df344fSAndy Whitcroft	@rawlines = ();
116613214adfSAndy Whitcroft	@lines = ();
11673705ce5bSJoe Perches	@fixed = ();
1168d752fcc8SJoe Perches	@fixed_inserted = ();
1169d752fcc8SJoe Perches	@fixed_deleted = ();
1170194f66fcSJoe Perches	$fixlinenr = -1;
1171485ff23eSAlex Dowad	@modifierListFile = ();
1172485ff23eSAlex Dowad	@typeListFile = ();
1173485ff23eSAlex Dowad	build_types();
1174f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
11750a920b5bSAndy Whitcroft}
11760a920b5bSAndy Whitcroft
1177d8469f16SJoe Perchesif (!$quiet) {
11783c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
11793c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
11803c816e49SJoe Perches
11815b57980dSJoe Perches	if (!$perl_version_ok) {
1182d8469f16SJoe Perches		print << "EOM"
1183d8469f16SJoe Perches
1184d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
11855b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1186d8469f16SJoe PerchesEOM
1187d8469f16SJoe Perches	}
1188d8469f16SJoe Perches	if ($exit) {
1189d8469f16SJoe Perches		print << "EOM"
1190d8469f16SJoe Perches
1191d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1192d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1193d8469f16SJoe PerchesEOM
1194d8469f16SJoe Perches	}
1195d8469f16SJoe Perches}
1196d8469f16SJoe Perches
11970a920b5bSAndy Whitcroftexit($exit);
11980a920b5bSAndy Whitcroft
11990a920b5bSAndy Whitcroftsub top_of_kernel_tree {
12006c72ffaaSAndy Whitcroft	my ($root) = @_;
12016c72ffaaSAndy Whitcroft
12026c72ffaaSAndy Whitcroft	my @tree_check = (
12036c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
12046c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
12056c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
12066c72ffaaSAndy Whitcroft	);
12076c72ffaaSAndy Whitcroft
12086c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
12096c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
12100a920b5bSAndy Whitcroft			return 0;
12110a920b5bSAndy Whitcroft		}
12126c72ffaaSAndy Whitcroft	}
12136c72ffaaSAndy Whitcroft	return 1;
12146c72ffaaSAndy Whitcroft}
12150a920b5bSAndy Whitcroft
121620112475SJoe Perchessub parse_email {
121720112475SJoe Perches	my ($formatted_email) = @_;
121820112475SJoe Perches
121920112475SJoe Perches	my $name = "";
1220fccaebf0SDwaipayan Ray	my $quoted = "";
1221dfa05c28SJoe Perches	my $name_comment = "";
122220112475SJoe Perches	my $address = "";
122320112475SJoe Perches	my $comment = "";
122420112475SJoe Perches
122520112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
122620112475SJoe Perches		$name = $1;
122720112475SJoe Perches		$address = $2;
122820112475SJoe Perches		$comment = $3 if defined $3;
122920112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
123020112475SJoe Perches		$address = $1;
123120112475SJoe Perches		$comment = $2 if defined $2;
123220112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
123320112475SJoe Perches		$address = $1;
123420112475SJoe Perches		$comment = $2 if defined $2;
123585e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
123620112475SJoe Perches		$name = $formatted_email;
12373705ce5bSJoe Perches		$name = trim($name);
123820112475SJoe Perches		$name =~ s/^\"|\"$//g;
123920112475SJoe Perches		# If there's a name left after stripping spaces and
124020112475SJoe Perches		# leading quotes, and the address doesn't have both
124120112475SJoe Perches		# leading and trailing angle brackets, the address
124220112475SJoe Perches		# is invalid. ie:
124320112475SJoe Perches		#   "joe smith [email protected]" bad
124420112475SJoe Perches		#   "joe smith <[email protected]" bad
124520112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
124620112475SJoe Perches			$name = "";
124720112475SJoe Perches			$address = "";
124820112475SJoe Perches			$comment = "";
124920112475SJoe Perches		}
125020112475SJoe Perches	}
125120112475SJoe Perches
1252fccaebf0SDwaipayan Ray	# Extract comments from names excluding quoted parts
1253fccaebf0SDwaipayan Ray	# "John D. (Doe)" - Do not extract
1254fccaebf0SDwaipayan Ray	if ($name =~ s/\"(.+)\"//) {
1255fccaebf0SDwaipayan Ray		$quoted = $1;
1256dfa05c28SJoe Perches	}
1257fccaebf0SDwaipayan Ray	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1258fccaebf0SDwaipayan Ray		$name_comment .= trim($1);
1259fccaebf0SDwaipayan Ray	}
1260fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
1261fccaebf0SDwaipayan Ray	$name = trim("$quoted $name");
1262fccaebf0SDwaipayan Ray
12633705ce5bSJoe Perches	$address = trim($address);
126420112475SJoe Perches	$address =~ s/^\<|\>$//g;
1265fccaebf0SDwaipayan Ray	$comment = trim($comment);
126620112475SJoe Perches
126720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
126820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
126920112475SJoe Perches		$name = "\"$name\"";
127020112475SJoe Perches	}
127120112475SJoe Perches
1272dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
127320112475SJoe Perches}
127420112475SJoe Perches
127520112475SJoe Perchessub format_email {
127648ca2d8aSDwaipayan Ray	my ($name, $name_comment, $address, $comment) = @_;
127720112475SJoe Perches
127820112475SJoe Perches	my $formatted_email;
127920112475SJoe Perches
1280fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
12813705ce5bSJoe Perches	$address = trim($address);
1282fccaebf0SDwaipayan Ray	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
128320112475SJoe Perches
128420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
128520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
128620112475SJoe Perches		$name = "\"$name\"";
128720112475SJoe Perches	}
128820112475SJoe Perches
1289fccaebf0SDwaipayan Ray	$name_comment = trim($name_comment);
1290fccaebf0SDwaipayan Ray	$name_comment = " $name_comment" if ($name_comment ne "");
1291fccaebf0SDwaipayan Ray	$comment = trim($comment);
1292fccaebf0SDwaipayan Ray	$comment = " $comment" if ($comment ne "");
1293fccaebf0SDwaipayan Ray
129420112475SJoe Perches	if ("$name" eq "") {
129520112475SJoe Perches		$formatted_email = "$address";
129620112475SJoe Perches	} else {
129748ca2d8aSDwaipayan Ray		$formatted_email = "$name$name_comment <$address>";
129820112475SJoe Perches	}
129948ca2d8aSDwaipayan Ray	$formatted_email .= "$comment";
130020112475SJoe Perches	return $formatted_email;
130120112475SJoe Perches}
130220112475SJoe Perches
1303dfa05c28SJoe Perchessub reformat_email {
1304dfa05c28SJoe Perches	my ($email) = @_;
1305dfa05c28SJoe Perches
1306dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
130748ca2d8aSDwaipayan Ray	return format_email($email_name, $name_comment, $email_address, $comment);
1308dfa05c28SJoe Perches}
1309dfa05c28SJoe Perches
1310dfa05c28SJoe Perchessub same_email_addresses {
1311fccaebf0SDwaipayan Ray	my ($email1, $email2) = @_;
1312dfa05c28SJoe Perches
1313dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1314dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1315dfa05c28SJoe Perches
131648ca2d8aSDwaipayan Ray	return $email1_name eq $email2_name &&
131748ca2d8aSDwaipayan Ray	       $email1_address eq $email2_address &&
131848ca2d8aSDwaipayan Ray	       $name1_comment eq $name2_comment &&
131948ca2d8aSDwaipayan Ray	       $comment1 eq $comment2;
132048ca2d8aSDwaipayan Ray}
1321dfa05c28SJoe Perches
1322d311cd44SJoe Perchessub which {
1323d311cd44SJoe Perches	my ($bin) = @_;
1324d311cd44SJoe Perches
1325d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1326d311cd44SJoe Perches		if (-e "$path/$bin") {
1327d311cd44SJoe Perches			return "$path/$bin";
1328d311cd44SJoe Perches		}
1329d311cd44SJoe Perches	}
1330d311cd44SJoe Perches
1331d311cd44SJoe Perches	return "";
1332d311cd44SJoe Perches}
1333d311cd44SJoe Perches
1334000d1cc1SJoe Perchessub which_conf {
1335000d1cc1SJoe Perches	my ($conf) = @_;
1336000d1cc1SJoe Perches
1337000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1338000d1cc1SJoe Perches		if (-e "$path/$conf") {
1339000d1cc1SJoe Perches			return "$path/$conf";
1340000d1cc1SJoe Perches		}
1341000d1cc1SJoe Perches	}
1342000d1cc1SJoe Perches
1343000d1cc1SJoe Perches	return "";
1344000d1cc1SJoe Perches}
1345000d1cc1SJoe Perches
13460a920b5bSAndy Whitcroftsub expand_tabs {
13470a920b5bSAndy Whitcroft	my ($str) = @_;
13480a920b5bSAndy Whitcroft
13490a920b5bSAndy Whitcroft	my $res = '';
13500a920b5bSAndy Whitcroft	my $n = 0;
13510a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
13520a920b5bSAndy Whitcroft		if ($c eq "\t") {
13530a920b5bSAndy Whitcroft			$res .= ' ';
13540a920b5bSAndy Whitcroft			$n++;
1355713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
13560a920b5bSAndy Whitcroft				$res .= ' ';
13570a920b5bSAndy Whitcroft			}
13580a920b5bSAndy Whitcroft			next;
13590a920b5bSAndy Whitcroft		}
13600a920b5bSAndy Whitcroft		$res .= $c;
13610a920b5bSAndy Whitcroft		$n++;
13620a920b5bSAndy Whitcroft	}
13630a920b5bSAndy Whitcroft
13640a920b5bSAndy Whitcroft	return $res;
13650a920b5bSAndy Whitcroft}
13666c72ffaaSAndy Whitcroftsub copy_spacing {
1367773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
13686c72ffaaSAndy Whitcroft	return $res;
13696c72ffaaSAndy Whitcroft}
13700a920b5bSAndy Whitcroft
13714a0df2efSAndy Whitcroftsub line_stats {
13724a0df2efSAndy Whitcroft	my ($line) = @_;
13734a0df2efSAndy Whitcroft
13744a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
13754a0df2efSAndy Whitcroft	$line =~ s/^.//;
13764a0df2efSAndy Whitcroft	$line = expand_tabs($line);
13774a0df2efSAndy Whitcroft
13784a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
13794a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
13804a0df2efSAndy Whitcroft
13814a0df2efSAndy Whitcroft	return (length($line), length($white));
13824a0df2efSAndy Whitcroft}
13834a0df2efSAndy Whitcroft
1384773647a0SAndy Whitcroftmy $sanitise_quote = '';
1385773647a0SAndy Whitcroft
1386773647a0SAndy Whitcroftsub sanitise_line_reset {
1387773647a0SAndy Whitcroft	my ($in_comment) = @_;
1388773647a0SAndy Whitcroft
1389773647a0SAndy Whitcroft	if ($in_comment) {
1390773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1391773647a0SAndy Whitcroft	} else {
1392773647a0SAndy Whitcroft		$sanitise_quote = '';
1393773647a0SAndy Whitcroft	}
1394773647a0SAndy Whitcroft}
139500df344fSAndy Whitcroftsub sanitise_line {
139600df344fSAndy Whitcroft	my ($line) = @_;
139700df344fSAndy Whitcroft
139800df344fSAndy Whitcroft	my $res = '';
139900df344fSAndy Whitcroft	my $l = '';
140000df344fSAndy Whitcroft
1401c2fdda0dSAndy Whitcroft	my $qlen = 0;
1402773647a0SAndy Whitcroft	my $off = 0;
1403773647a0SAndy Whitcroft	my $c;
140400df344fSAndy Whitcroft
1405773647a0SAndy Whitcroft	# Always copy over the diff marker.
1406773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1407773647a0SAndy Whitcroft
1408773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1409773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1410773647a0SAndy Whitcroft
14118d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1412773647a0SAndy Whitcroft		# and end, all to $;.
1413773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1414773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1415773647a0SAndy Whitcroft
1416773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1417773647a0SAndy Whitcroft			$off++;
141800df344fSAndy Whitcroft			next;
1419773647a0SAndy Whitcroft		}
142081bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1421773647a0SAndy Whitcroft			$sanitise_quote = '';
1422773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1423773647a0SAndy Whitcroft			$off++;
1424773647a0SAndy Whitcroft			next;
1425773647a0SAndy Whitcroft		}
1426113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1427113f04a8SDaniel Walker			$sanitise_quote = '//';
1428113f04a8SDaniel Walker
1429113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1430113f04a8SDaniel Walker			$off++;
1431113f04a8SDaniel Walker			next;
1432113f04a8SDaniel Walker		}
1433773647a0SAndy Whitcroft
1434773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1435773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1436773647a0SAndy Whitcroft		    $c eq "\\") {
1437773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1438773647a0SAndy Whitcroft			$off++;
1439773647a0SAndy Whitcroft			next;
1440773647a0SAndy Whitcroft		}
1441773647a0SAndy Whitcroft		# Regular quotes.
1442773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1443773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1444773647a0SAndy Whitcroft				$sanitise_quote = $c;
1445773647a0SAndy Whitcroft
1446773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1447773647a0SAndy Whitcroft				next;
1448773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1449773647a0SAndy Whitcroft				$sanitise_quote = '';
145000df344fSAndy Whitcroft			}
145100df344fSAndy Whitcroft		}
1452773647a0SAndy Whitcroft
1453fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1454773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1455773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1456113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1457113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1458773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1459773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
146000df344fSAndy Whitcroft		} else {
1461773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
146200df344fSAndy Whitcroft		}
1463c2fdda0dSAndy Whitcroft	}
1464c2fdda0dSAndy Whitcroft
1465113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1466113f04a8SDaniel Walker		$sanitise_quote = '';
1467113f04a8SDaniel Walker	}
1468113f04a8SDaniel Walker
1469c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1470c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1471c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1472c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1473c2fdda0dSAndy Whitcroft
1474c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1475c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1476c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1477c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1478c2fdda0dSAndy Whitcroft	}
1479c2fdda0dSAndy Whitcroft
1480dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1481dadf680dSJoe Perches		my $match = $1;
1482dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1483dadf680dSJoe Perches	}
1484dadf680dSJoe Perches
148500df344fSAndy Whitcroft	return $res;
148600df344fSAndy Whitcroft}
148700df344fSAndy Whitcroft
1488a6962d72SJoe Perchessub get_quoted_string {
1489a6962d72SJoe Perches	my ($line, $rawline) = @_;
1490a6962d72SJoe Perches
1491478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
149233acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1493a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1494a6962d72SJoe Perches}
1495a6962d72SJoe Perches
14968905a67cSAndy Whitcroftsub ctx_statement_block {
14978905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
14988905a67cSAndy Whitcroft	my $line = $linenr - 1;
14998905a67cSAndy Whitcroft	my $blk = '';
15008905a67cSAndy Whitcroft	my $soff = $off;
15018905a67cSAndy Whitcroft	my $coff = $off - 1;
1502773647a0SAndy Whitcroft	my $coff_set = 0;
15038905a67cSAndy Whitcroft
150413214adfSAndy Whitcroft	my $loff = 0;
150513214adfSAndy Whitcroft
15068905a67cSAndy Whitcroft	my $type = '';
15078905a67cSAndy Whitcroft	my $level = 0;
1508a2750645SAndy Whitcroft	my @stack = ();
1509cf655043SAndy Whitcroft	my $p;
15108905a67cSAndy Whitcroft	my $c;
15118905a67cSAndy Whitcroft	my $len = 0;
151213214adfSAndy Whitcroft
151313214adfSAndy Whitcroft	my $remainder;
15148905a67cSAndy Whitcroft	while (1) {
1515a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1516a2750645SAndy Whitcroft
1517773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
15188905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
15198905a67cSAndy Whitcroft		# context.
15208905a67cSAndy Whitcroft		if ($off >= $len) {
15218905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1522dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1523c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
15248905a67cSAndy Whitcroft				$remain--;
152513214adfSAndy Whitcroft				$loff = $len;
1526c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
15278905a67cSAndy Whitcroft				$len = length($blk);
15288905a67cSAndy Whitcroft				$line++;
15298905a67cSAndy Whitcroft				last;
15308905a67cSAndy Whitcroft			}
15318905a67cSAndy Whitcroft			# Bail if there is no further context.
15328905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
153313214adfSAndy Whitcroft			if ($off >= $len) {
15348905a67cSAndy Whitcroft				last;
15358905a67cSAndy Whitcroft			}
1536f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1537f74bd194SAndy Whitcroft				$level++;
1538f74bd194SAndy Whitcroft				$type = '#';
1539f74bd194SAndy Whitcroft			}
15408905a67cSAndy Whitcroft		}
1541cf655043SAndy Whitcroft		$p = $c;
15428905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
154313214adfSAndy Whitcroft		$remainder = substr($blk, $off);
15448905a67cSAndy Whitcroft
1545773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
15464635f4fbSAndy Whitcroft
15474635f4fbSAndy Whitcroft		# Handle nested #if/#else.
15484635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
15494635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
15504635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
15514635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
15524635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
15534635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
15544635f4fbSAndy Whitcroft		}
15554635f4fbSAndy Whitcroft
15568905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
15578905a67cSAndy Whitcroft		# outermost level.
15588905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
15598905a67cSAndy Whitcroft			last;
15608905a67cSAndy Whitcroft		}
15618905a67cSAndy Whitcroft
156213214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1563773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1564773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1565773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1566773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1567773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1568773647a0SAndy Whitcroft			$coff_set = 1;
1569773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1570773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
157113214adfSAndy Whitcroft		}
157213214adfSAndy Whitcroft
15738905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
15748905a67cSAndy Whitcroft			$level++;
15758905a67cSAndy Whitcroft			$type = '(';
15768905a67cSAndy Whitcroft		}
15778905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
15788905a67cSAndy Whitcroft			$level--;
15798905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
15808905a67cSAndy Whitcroft
15818905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
15828905a67cSAndy Whitcroft				$coff = $off;
1583773647a0SAndy Whitcroft				$coff_set = 1;
1584773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
15858905a67cSAndy Whitcroft			}
15868905a67cSAndy Whitcroft		}
15878905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
15888905a67cSAndy Whitcroft			$level++;
15898905a67cSAndy Whitcroft			$type = '{';
15908905a67cSAndy Whitcroft		}
15918905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
15928905a67cSAndy Whitcroft			$level--;
15938905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
15948905a67cSAndy Whitcroft
15958905a67cSAndy Whitcroft			if ($level == 0) {
1596b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1597b998e001SPatrick Pannuto					$off++;
1598b998e001SPatrick Pannuto				}
15998905a67cSAndy Whitcroft				last;
16008905a67cSAndy Whitcroft			}
16018905a67cSAndy Whitcroft		}
1602f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1603f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1604f74bd194SAndy Whitcroft			$level--;
1605f74bd194SAndy Whitcroft			$type = '';
1606f74bd194SAndy Whitcroft			$off++;
1607f74bd194SAndy Whitcroft			last;
1608f74bd194SAndy Whitcroft		}
16098905a67cSAndy Whitcroft		$off++;
16108905a67cSAndy Whitcroft	}
1611a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
161213214adfSAndy Whitcroft	if ($off == $len) {
1613a3bb97a7SAndy Whitcroft		$loff = $len + 1;
161413214adfSAndy Whitcroft		$line++;
161513214adfSAndy Whitcroft		$remain--;
161613214adfSAndy Whitcroft	}
16178905a67cSAndy Whitcroft
16188905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
16198905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
16208905a67cSAndy Whitcroft
16218905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
16228905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
16238905a67cSAndy Whitcroft
1624773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
162513214adfSAndy Whitcroft
162613214adfSAndy Whitcroft	return ($statement, $condition,
162713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
162813214adfSAndy Whitcroft}
162913214adfSAndy Whitcroft
1630cf655043SAndy Whitcroftsub statement_lines {
1631cf655043SAndy Whitcroft	my ($stmt) = @_;
1632cf655043SAndy Whitcroft
1633cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1634cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1635cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1636cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1637cf655043SAndy Whitcroft
1638cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1639cf655043SAndy Whitcroft
1640cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1641cf655043SAndy Whitcroft}
1642cf655043SAndy Whitcroft
1643cf655043SAndy Whitcroftsub statement_rawlines {
1644cf655043SAndy Whitcroft	my ($stmt) = @_;
1645cf655043SAndy Whitcroft
1646cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1647cf655043SAndy Whitcroft
1648cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1649cf655043SAndy Whitcroft}
1650cf655043SAndy Whitcroft
1651cf655043SAndy Whitcroftsub statement_block_size {
1652cf655043SAndy Whitcroft	my ($stmt) = @_;
1653cf655043SAndy Whitcroft
1654cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1655cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1656cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1657cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1658cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1659cf655043SAndy Whitcroft
1660cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1661cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1662cf655043SAndy Whitcroft
1663cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1664cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1665cf655043SAndy Whitcroft
1666cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1667cf655043SAndy Whitcroft		return $stmt_lines;
1668cf655043SAndy Whitcroft	} else {
1669cf655043SAndy Whitcroft		return $stmt_statements;
1670cf655043SAndy Whitcroft	}
1671cf655043SAndy Whitcroft}
1672cf655043SAndy Whitcroft
167313214adfSAndy Whitcroftsub ctx_statement_full {
167413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
167513214adfSAndy Whitcroft	my ($statement, $condition, $level);
167613214adfSAndy Whitcroft
167713214adfSAndy Whitcroft	my (@chunks);
167813214adfSAndy Whitcroft
1679cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
168013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
168113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1682773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
168313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1684cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1685cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1686cf655043SAndy Whitcroft	}
1687cf655043SAndy Whitcroft
1688cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1689cf655043SAndy Whitcroft	# could continue the statement.
1690cf655043SAndy Whitcroft	for (;;) {
169113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
169213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1693cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1694773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1695cf655043SAndy Whitcroft		#print "C: push\n";
1696cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
169713214adfSAndy Whitcroft	}
169813214adfSAndy Whitcroft
169913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
17008905a67cSAndy Whitcroft}
17018905a67cSAndy Whitcroft
17024a0df2efSAndy Whitcroftsub ctx_block_get {
1703f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
17044a0df2efSAndy Whitcroft	my $line;
17054a0df2efSAndy Whitcroft	my $start = $linenr - 1;
17064a0df2efSAndy Whitcroft	my $blk = '';
17074a0df2efSAndy Whitcroft	my @o;
17084a0df2efSAndy Whitcroft	my @c;
17094a0df2efSAndy Whitcroft	my @res = ();
17104a0df2efSAndy Whitcroft
1711f0a594c1SAndy Whitcroft	my $level = 0;
17124635f4fbSAndy Whitcroft	my @stack = ($level);
171300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
171400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
171500df344fSAndy Whitcroft		$remain--;
171600df344fSAndy Whitcroft
171700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
17184635f4fbSAndy Whitcroft
17194635f4fbSAndy Whitcroft		# Handle nested #if/#else.
172001464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
17214635f4fbSAndy Whitcroft			push(@stack, $level);
172201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
17234635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
172401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
17254635f4fbSAndy Whitcroft			$level = pop(@stack);
17264635f4fbSAndy Whitcroft		}
17274635f4fbSAndy Whitcroft
172801464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1729f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1730f0a594c1SAndy Whitcroft			if ($off > 0) {
1731f0a594c1SAndy Whitcroft				$off--;
1732f0a594c1SAndy Whitcroft				next;
1733f0a594c1SAndy Whitcroft			}
17344a0df2efSAndy Whitcroft
1735f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1736f0a594c1SAndy Whitcroft				$level--;
1737f0a594c1SAndy Whitcroft				last if ($level == 0);
1738f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1739f0a594c1SAndy Whitcroft				$level++;
1740f0a594c1SAndy Whitcroft			}
1741f0a594c1SAndy Whitcroft		}
17424a0df2efSAndy Whitcroft
1743f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
174400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
17454a0df2efSAndy Whitcroft		}
17464a0df2efSAndy Whitcroft
1747f0a594c1SAndy Whitcroft		last if ($level == 0);
17484a0df2efSAndy Whitcroft	}
17494a0df2efSAndy Whitcroft
1750f0a594c1SAndy Whitcroft	return ($level, @res);
17514a0df2efSAndy Whitcroft}
17524a0df2efSAndy Whitcroftsub ctx_block_outer {
17534a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
17544a0df2efSAndy Whitcroft
1755f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1756f0a594c1SAndy Whitcroft	return @r;
17574a0df2efSAndy Whitcroft}
17584a0df2efSAndy Whitcroftsub ctx_block {
17594a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
17604a0df2efSAndy Whitcroft
1761f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1762f0a594c1SAndy Whitcroft	return @r;
1763653d4876SAndy Whitcroft}
1764653d4876SAndy Whitcroftsub ctx_statement {
1765f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1766f0a594c1SAndy Whitcroft
1767f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1768f0a594c1SAndy Whitcroft	return @r;
1769f0a594c1SAndy Whitcroft}
1770f0a594c1SAndy Whitcroftsub ctx_block_level {
1771653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1772653d4876SAndy Whitcroft
1773f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
17744a0df2efSAndy Whitcroft}
17759c0ca6f9SAndy Whitcroftsub ctx_statement_level {
17769c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
17779c0ca6f9SAndy Whitcroft
17789c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
17799c0ca6f9SAndy Whitcroft}
17804a0df2efSAndy Whitcroft
17814a0df2efSAndy Whitcroftsub ctx_locate_comment {
17824a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17834a0df2efSAndy Whitcroft
1784a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1785a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1786a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1787a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1788a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1789a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1790a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1791a55ee0ccSJoe Perches
17924a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1793a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
17944a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
17954a0df2efSAndy Whitcroft
17964a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
17974a0df2efSAndy Whitcroft	# comment.
17984a0df2efSAndy Whitcroft	my $in_comment = 0;
17994a0df2efSAndy Whitcroft	$current_comment = '';
18004a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
180100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
180200df344fSAndy Whitcroft		#warn "           $line\n";
18034a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
18044a0df2efSAndy Whitcroft			$in_comment = 1;
18054a0df2efSAndy Whitcroft		}
18064a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
18074a0df2efSAndy Whitcroft			$in_comment = 1;
18084a0df2efSAndy Whitcroft		}
18094a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
18104a0df2efSAndy Whitcroft			$current_comment = '';
18114a0df2efSAndy Whitcroft		}
18124a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
18134a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
18144a0df2efSAndy Whitcroft			$in_comment = 0;
18154a0df2efSAndy Whitcroft		}
18164a0df2efSAndy Whitcroft	}
18174a0df2efSAndy Whitcroft
18184a0df2efSAndy Whitcroft	chomp($current_comment);
18194a0df2efSAndy Whitcroft	return($current_comment);
18204a0df2efSAndy Whitcroft}
18214a0df2efSAndy Whitcroftsub ctx_has_comment {
18224a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
18234a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
18244a0df2efSAndy Whitcroft
182500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
18264a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
18274a0df2efSAndy Whitcroft
18284a0df2efSAndy Whitcroft	return ($cmt ne '');
18294a0df2efSAndy Whitcroft}
18304a0df2efSAndy Whitcroft
18314d001e4dSAndy Whitcroftsub raw_line {
18324d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
18334d001e4dSAndy Whitcroft
18344d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
18354d001e4dSAndy Whitcroft	$cnt++;
18364d001e4dSAndy Whitcroft
18374d001e4dSAndy Whitcroft	my $line;
18384d001e4dSAndy Whitcroft	while ($cnt) {
18394d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
18404d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
18414d001e4dSAndy Whitcroft		$cnt--;
18424d001e4dSAndy Whitcroft	}
18434d001e4dSAndy Whitcroft
18444d001e4dSAndy Whitcroft	return $line;
18454d001e4dSAndy Whitcroft}
18464d001e4dSAndy Whitcroft
18472a9f9d85STobin C. Hardingsub get_stat_real {
18482a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
18492a9f9d85STobin C. Harding
18502a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
18512a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
18522a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
18532a9f9d85STobin C. Harding	}
18542a9f9d85STobin C. Harding
18552a9f9d85STobin C. Harding	return $stat_real;
18562a9f9d85STobin C. Harding}
18572a9f9d85STobin C. Harding
1858e3d95a2aSTobin C. Hardingsub get_stat_here {
1859e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1860e3d95a2aSTobin C. Harding
1861e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1862e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1863e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1864e3d95a2aSTobin C. Harding	}
1865e3d95a2aSTobin C. Harding
1866e3d95a2aSTobin C. Harding	return $herectx;
1867e3d95a2aSTobin C. Harding}
1868e3d95a2aSTobin C. Harding
18690a920b5bSAndy Whitcroftsub cat_vet {
18700a920b5bSAndy Whitcroft	my ($vet) = @_;
18719c0ca6f9SAndy Whitcroft	my ($res, $coded);
18720a920b5bSAndy Whitcroft
18739c0ca6f9SAndy Whitcroft	$res = '';
18746c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
18756c72ffaaSAndy Whitcroft		$res .= $1;
18766c72ffaaSAndy Whitcroft		if ($2 ne '') {
18779c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
18786c72ffaaSAndy Whitcroft			$res .= $coded;
18796c72ffaaSAndy Whitcroft		}
18809c0ca6f9SAndy Whitcroft	}
18819c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
18820a920b5bSAndy Whitcroft
18839c0ca6f9SAndy Whitcroft	return $res;
18840a920b5bSAndy Whitcroft}
18850a920b5bSAndy Whitcroft
1886c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1887cf655043SAndy Whitcroftmy $av_pending;
1888c2fdda0dSAndy Whitcroftmy @av_paren_type;
18891f65f947SAndy Whitcroftmy $av_pend_colon;
1890c2fdda0dSAndy Whitcroft
1891c2fdda0dSAndy Whitcroftsub annotate_reset {
1892c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1893cf655043SAndy Whitcroft	$av_pending = '_';
1894cf655043SAndy Whitcroft	@av_paren_type = ('E');
18951f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1896c2fdda0dSAndy Whitcroft}
1897c2fdda0dSAndy Whitcroft
18986c72ffaaSAndy Whitcroftsub annotate_values {
18996c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
19006c72ffaaSAndy Whitcroft
19016c72ffaaSAndy Whitcroft	my $res;
19021f65f947SAndy Whitcroft	my $var = '_' x length($stream);
19036c72ffaaSAndy Whitcroft	my $cur = $stream;
19046c72ffaaSAndy Whitcroft
1905c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
19066c72ffaaSAndy Whitcroft
19076c72ffaaSAndy Whitcroft	while (length($cur)) {
1908773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1909cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1910171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
19116c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1912c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1913c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1914cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1915c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
19166c72ffaaSAndy Whitcroft			}
19176c72ffaaSAndy Whitcroft
1918c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
19199446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
19209446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1921addcdceaSAndy Whitcroft			$type = 'c';
19229446ef56SAndy Whitcroft
1923e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1924c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
19256c72ffaaSAndy Whitcroft			$type = 'T';
19266c72ffaaSAndy Whitcroft
1927389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1928389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1929389a2fe5SAndy Whitcroft			$type = 'T';
1930389a2fe5SAndy Whitcroft
1931c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1932171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1933c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1934171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1935171ae1a4SAndy Whitcroft			if ($2 ne '') {
1936cf655043SAndy Whitcroft				$av_pending = 'N';
1937171ae1a4SAndy Whitcroft			}
1938171ae1a4SAndy Whitcroft			$type = 'E';
1939171ae1a4SAndy Whitcroft
1940c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1941171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1942171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1943171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
19446c72ffaaSAndy Whitcroft
1945c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1946cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1947c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1948cf655043SAndy Whitcroft
1949cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1950cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1951171ae1a4SAndy Whitcroft			$type = 'E';
1952cf655043SAndy Whitcroft
1953c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1954cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1955cf655043SAndy Whitcroft			$av_preprocessor = 1;
1956cf655043SAndy Whitcroft
1957cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1958cf655043SAndy Whitcroft
1959171ae1a4SAndy Whitcroft			$type = 'E';
1960cf655043SAndy Whitcroft
1961c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1962cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1963cf655043SAndy Whitcroft
1964cf655043SAndy Whitcroft			$av_preprocessor = 1;
1965cf655043SAndy Whitcroft
1966cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1967cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1968cf655043SAndy Whitcroft			pop(@av_paren_type);
1969cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1970171ae1a4SAndy Whitcroft			$type = 'E';
19716c72ffaaSAndy Whitcroft
19726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1973c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
19746c72ffaaSAndy Whitcroft
1975171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1976171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1977171ae1a4SAndy Whitcroft			$av_pending = $type;
1978171ae1a4SAndy Whitcroft			$type = 'N';
1979171ae1a4SAndy Whitcroft
19806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1981c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
19826c72ffaaSAndy Whitcroft			if (defined $2) {
1983cf655043SAndy Whitcroft				$av_pending = 'V';
19846c72ffaaSAndy Whitcroft			}
19856c72ffaaSAndy Whitcroft			$type = 'N';
19866c72ffaaSAndy Whitcroft
198714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1988c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
198914b111c1SAndy Whitcroft			$av_pending = 'E';
19906c72ffaaSAndy Whitcroft			$type = 'N';
19916c72ffaaSAndy Whitcroft
19921f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
19931f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
19941f65f947SAndy Whitcroft			$av_pend_colon = 'C';
19951f65f947SAndy Whitcroft			$type = 'N';
19961f65f947SAndy Whitcroft
199714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1998c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
19996c72ffaaSAndy Whitcroft			$type = 'N';
20006c72ffaaSAndy Whitcroft
20016c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
2002c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
2003cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
2004cf655043SAndy Whitcroft			$av_pending = '_';
20056c72ffaaSAndy Whitcroft			$type = 'N';
20066c72ffaaSAndy Whitcroft
20076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
2008cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
2009cf655043SAndy Whitcroft			if ($new_type ne '_') {
2010cf655043SAndy Whitcroft				$type = $new_type;
2011c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
2012c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
20136c72ffaaSAndy Whitcroft			} else {
2014c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
20156c72ffaaSAndy Whitcroft			}
20166c72ffaaSAndy Whitcroft
2017c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
2018c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
2019c8cb2ca3SAndy Whitcroft			$type = 'V';
2020cf655043SAndy Whitcroft			$av_pending = 'V';
20216c72ffaaSAndy Whitcroft
20228e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
20238e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
20241f65f947SAndy Whitcroft				$av_pend_colon = 'B';
20258e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
20268e761b04SAndy Whitcroft				$av_pend_colon = 'L';
20271f65f947SAndy Whitcroft			}
20281f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
20291f65f947SAndy Whitcroft			$type = 'V';
20301f65f947SAndy Whitcroft
20316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
2032c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
20336c72ffaaSAndy Whitcroft			$type = 'V';
20346c72ffaaSAndy Whitcroft
20356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
2036c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
20376c72ffaaSAndy Whitcroft			$type = 'N';
20386c72ffaaSAndy Whitcroft
2039cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
2040c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
204113214adfSAndy Whitcroft			$type = 'E';
20421f65f947SAndy Whitcroft			$av_pend_colon = 'O';
204313214adfSAndy Whitcroft
20448e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
20458e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
20468e761b04SAndy Whitcroft			$type = 'C';
20478e761b04SAndy Whitcroft
20481f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
20491f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
20501f65f947SAndy Whitcroft			$type = 'N';
20511f65f947SAndy Whitcroft
20521f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
20531f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
20541f65f947SAndy Whitcroft
20551f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
20561f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
20571f65f947SAndy Whitcroft				$type = 'E';
20581f65f947SAndy Whitcroft			} else {
20591f65f947SAndy Whitcroft				$type = 'N';
20601f65f947SAndy Whitcroft			}
20611f65f947SAndy Whitcroft			$av_pend_colon = 'O';
20621f65f947SAndy Whitcroft
20638e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
206413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
20656c72ffaaSAndy Whitcroft			$type = 'N';
20666c72ffaaSAndy Whitcroft
20670d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
206874048ed8SAndy Whitcroft			my $variant;
206974048ed8SAndy Whitcroft
207074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
207174048ed8SAndy Whitcroft			if ($type eq 'V') {
207274048ed8SAndy Whitcroft				$variant = 'B';
207374048ed8SAndy Whitcroft			} else {
207474048ed8SAndy Whitcroft				$variant = 'U';
207574048ed8SAndy Whitcroft			}
207674048ed8SAndy Whitcroft
207774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
207874048ed8SAndy Whitcroft			$type = 'N';
207974048ed8SAndy Whitcroft
20806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2081c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
20826c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
20836c72ffaaSAndy Whitcroft				$type = 'N';
20846c72ffaaSAndy Whitcroft			}
20856c72ffaaSAndy Whitcroft
20866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2087c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
20886c72ffaaSAndy Whitcroft		}
20896c72ffaaSAndy Whitcroft		if (defined $1) {
20906c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
20916c72ffaaSAndy Whitcroft			$res .= $type x length($1);
20926c72ffaaSAndy Whitcroft		}
20936c72ffaaSAndy Whitcroft	}
20946c72ffaaSAndy Whitcroft
20951f65f947SAndy Whitcroft	return ($res, $var);
20966c72ffaaSAndy Whitcroft}
20976c72ffaaSAndy Whitcroft
20988905a67cSAndy Whitcroftsub possible {
209913214adfSAndy Whitcroft	my ($possible, $line) = @_;
21009a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
21010776e594SAndy Whitcroft		^(?:
21020776e594SAndy Whitcroft			$Modifier|
21030776e594SAndy Whitcroft			$Storage|
21040776e594SAndy Whitcroft			$Type|
21059a974fdbSAndy Whitcroft			DEFINE_\S+
21069a974fdbSAndy Whitcroft		)$|
21079a974fdbSAndy Whitcroft		^(?:
21080776e594SAndy Whitcroft			goto|
21090776e594SAndy Whitcroft			return|
21100776e594SAndy Whitcroft			case|
21110776e594SAndy Whitcroft			else|
21120776e594SAndy Whitcroft			asm|__asm__|
211389a88353SAndy Whitcroft			do|
211489a88353SAndy Whitcroft			\#|
211589a88353SAndy Whitcroft			\#\#|
21169a974fdbSAndy Whitcroft		)(?:\s|$)|
21170776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
21189a974fdbSAndy Whitcroft	    )}x;
21199a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
21209a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2121c45dcabdSAndy Whitcroft		# Check for modifiers.
2122c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2123c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2124c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2125c45dcabdSAndy Whitcroft
2126c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2127c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2128d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
21299a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2130d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2131485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2132d2506586SAndy Whitcroft				}
21339a974fdbSAndy Whitcroft			}
2134c45dcabdSAndy Whitcroft
2135c45dcabdSAndy Whitcroft		} else {
213613214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2137485ff23eSAlex Dowad			push(@typeListFile, $possible);
2138c45dcabdSAndy Whitcroft		}
21398905a67cSAndy Whitcroft		build_types();
21400776e594SAndy Whitcroft	} else {
21410776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
21428905a67cSAndy Whitcroft	}
21438905a67cSAndy Whitcroft}
21448905a67cSAndy Whitcroft
21456c72ffaaSAndy Whitcroftmy $prefix = '';
21466c72ffaaSAndy Whitcroft
2147000d1cc1SJoe Perchessub show_type {
2148cbec18afSJoe Perches	my ($type) = @_;
214991bfe484SJoe Perches
2150522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2151522b837cSAlexey Dobriyan
2152cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2153cbec18afSJoe Perches
2154cbec18afSJoe Perches	return !defined $ignore_type{$type};
2155000d1cc1SJoe Perches}
2156000d1cc1SJoe Perches
2157f0a594c1SAndy Whitcroftsub report {
2158cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2159cbec18afSJoe Perches
2160cbec18afSJoe Perches	if (!show_type($type) ||
2161cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2162773647a0SAndy Whitcroft		return 0;
2163773647a0SAndy Whitcroft	}
216457230297SJoe Perches	my $output = '';
2165737c0767SJohn Brooks	if ($color) {
216657230297SJoe Perches		if ($level eq 'ERROR') {
216757230297SJoe Perches			$output .= RED;
216857230297SJoe Perches		} elsif ($level eq 'WARNING') {
216957230297SJoe Perches			$output .= YELLOW;
2170000d1cc1SJoe Perches		} else {
217157230297SJoe Perches			$output .= GREEN;
2172000d1cc1SJoe Perches		}
217357230297SJoe Perches	}
217457230297SJoe Perches	$output .= $prefix . $level . ':';
217557230297SJoe Perches	if ($show_types) {
2176737c0767SJohn Brooks		$output .= BLUE if ($color);
217757230297SJoe Perches		$output .= "$type:";
217857230297SJoe Perches	}
2179737c0767SJohn Brooks	$output .= RESET if ($color);
218057230297SJoe Perches	$output .= ' ' . $msg . "\n";
218134d8815fSJoe Perches
218234d8815fSJoe Perches	if ($showfile) {
218334d8815fSJoe Perches		my @lines = split("\n", $output, -1);
218434d8815fSJoe Perches		splice(@lines, 1, 1);
218534d8815fSJoe Perches		$output = join("\n", @lines);
218634d8815fSJoe Perches	}
218757230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
21888905a67cSAndy Whitcroft
218957230297SJoe Perches	push(our @report, $output);
2190773647a0SAndy Whitcroft
2191773647a0SAndy Whitcroft	return 1;
2192f0a594c1SAndy Whitcroft}
2193cbec18afSJoe Perches
2194f0a594c1SAndy Whitcroftsub report_dump {
219513214adfSAndy Whitcroft	our @report;
2196f0a594c1SAndy Whitcroft}
2197000d1cc1SJoe Perches
2198d752fcc8SJoe Perchessub fixup_current_range {
2199d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2200d752fcc8SJoe Perches
2201d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2202d752fcc8SJoe Perches		my $o = $1;
2203d752fcc8SJoe Perches		my $l = $2;
2204d752fcc8SJoe Perches		my $no = $o + $offset;
2205d752fcc8SJoe Perches		my $nl = $l + $length;
2206d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2207d752fcc8SJoe Perches	}
2208d752fcc8SJoe Perches}
2209d752fcc8SJoe Perches
2210d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2211d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2212d752fcc8SJoe Perches
2213d752fcc8SJoe Perches	my $range_last_linenr = 0;
2214d752fcc8SJoe Perches	my $delta_offset = 0;
2215d752fcc8SJoe Perches
2216d752fcc8SJoe Perches	my $old_linenr = 0;
2217d752fcc8SJoe Perches	my $new_linenr = 0;
2218d752fcc8SJoe Perches
2219d752fcc8SJoe Perches	my $next_insert = 0;
2220d752fcc8SJoe Perches	my $next_delete = 0;
2221d752fcc8SJoe Perches
2222d752fcc8SJoe Perches	my @lines = ();
2223d752fcc8SJoe Perches
2224d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2225d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2226d752fcc8SJoe Perches
2227d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2228d752fcc8SJoe Perches		my $save_line = 1;
2229d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2230323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2231d752fcc8SJoe Perches			$delta_offset = 0;
2232d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2233d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2234d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2235d752fcc8SJoe Perches		}
2236d752fcc8SJoe Perches
2237d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2238d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2239d752fcc8SJoe Perches			$save_line = 0;
2240d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2241d752fcc8SJoe Perches		}
2242d752fcc8SJoe Perches
2243d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2244d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2245d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2246d752fcc8SJoe Perches			$new_linenr++;
2247d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2248d752fcc8SJoe Perches		}
2249d752fcc8SJoe Perches
2250d752fcc8SJoe Perches		if ($save_line) {
2251d752fcc8SJoe Perches			push(@lines, $line);
2252d752fcc8SJoe Perches			$new_linenr++;
2253d752fcc8SJoe Perches		}
2254d752fcc8SJoe Perches
2255d752fcc8SJoe Perches		$old_linenr++;
2256d752fcc8SJoe Perches	}
2257d752fcc8SJoe Perches
2258d752fcc8SJoe Perches	return @lines;
2259d752fcc8SJoe Perches}
2260d752fcc8SJoe Perches
2261f2d7e4d4SJoe Perchessub fix_insert_line {
2262f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2263f2d7e4d4SJoe Perches
2264f2d7e4d4SJoe Perches	my $inserted = {
2265f2d7e4d4SJoe Perches		LINENR => $linenr,
2266f2d7e4d4SJoe Perches		LINE => $line,
2267f2d7e4d4SJoe Perches	};
2268f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2269f2d7e4d4SJoe Perches}
2270f2d7e4d4SJoe Perches
2271f2d7e4d4SJoe Perchessub fix_delete_line {
2272f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2273f2d7e4d4SJoe Perches
2274f2d7e4d4SJoe Perches	my $deleted = {
2275f2d7e4d4SJoe Perches		LINENR => $linenr,
2276f2d7e4d4SJoe Perches		LINE => $line,
2277f2d7e4d4SJoe Perches	};
2278f2d7e4d4SJoe Perches
2279f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2280f2d7e4d4SJoe Perches}
2281f2d7e4d4SJoe Perches
2282de7d4f0eSAndy Whitcroftsub ERROR {
2283cbec18afSJoe Perches	my ($type, $msg) = @_;
2284cbec18afSJoe Perches
2285cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2286de7d4f0eSAndy Whitcroft		our $clean = 0;
22876c72ffaaSAndy Whitcroft		our $cnt_error++;
22883705ce5bSJoe Perches		return 1;
2289de7d4f0eSAndy Whitcroft	}
22903705ce5bSJoe Perches	return 0;
2291773647a0SAndy Whitcroft}
2292de7d4f0eSAndy Whitcroftsub WARN {
2293cbec18afSJoe Perches	my ($type, $msg) = @_;
2294cbec18afSJoe Perches
2295cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2296de7d4f0eSAndy Whitcroft		our $clean = 0;
22976c72ffaaSAndy Whitcroft		our $cnt_warn++;
22983705ce5bSJoe Perches		return 1;
2299de7d4f0eSAndy Whitcroft	}
23003705ce5bSJoe Perches	return 0;
2301773647a0SAndy Whitcroft}
2302de7d4f0eSAndy Whitcroftsub CHK {
2303cbec18afSJoe Perches	my ($type, $msg) = @_;
2304cbec18afSJoe Perches
2305cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2306de7d4f0eSAndy Whitcroft		our $clean = 0;
23076c72ffaaSAndy Whitcroft		our $cnt_chk++;
23083705ce5bSJoe Perches		return 1;
23096c72ffaaSAndy Whitcroft	}
23103705ce5bSJoe Perches	return 0;
2311de7d4f0eSAndy Whitcroft}
2312de7d4f0eSAndy Whitcroft
23136ecd9674SAndy Whitcroftsub check_absolute_file {
23146ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
23156ecd9674SAndy Whitcroft	my $file = $absolute;
23166ecd9674SAndy Whitcroft
23176ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
23186ecd9674SAndy Whitcroft
23196ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
23206ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
23216ecd9674SAndy Whitcroft		if (-f "$root/$file") {
23226ecd9674SAndy Whitcroft			##print "file<$file>\n";
23236ecd9674SAndy Whitcroft			last;
23246ecd9674SAndy Whitcroft		}
23256ecd9674SAndy Whitcroft	}
23266ecd9674SAndy Whitcroft	if (! -f _)  {
23276ecd9674SAndy Whitcroft		return 0;
23286ecd9674SAndy Whitcroft	}
23296ecd9674SAndy Whitcroft
23306ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
23316ecd9674SAndy Whitcroft	my $prefix = $absolute;
23326ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
23336ecd9674SAndy Whitcroft
23346ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
23356ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2336000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2337000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
23386ecd9674SAndy Whitcroft	}
23396ecd9674SAndy Whitcroft}
23406ecd9674SAndy Whitcroft
23413705ce5bSJoe Perchessub trim {
23423705ce5bSJoe Perches	my ($string) = @_;
23433705ce5bSJoe Perches
2344b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2345b34c648bSJoe Perches
2346b34c648bSJoe Perches	return $string;
2347b34c648bSJoe Perches}
2348b34c648bSJoe Perches
2349b34c648bSJoe Perchessub ltrim {
2350b34c648bSJoe Perches	my ($string) = @_;
2351b34c648bSJoe Perches
2352b34c648bSJoe Perches	$string =~ s/^\s+//;
2353b34c648bSJoe Perches
2354b34c648bSJoe Perches	return $string;
2355b34c648bSJoe Perches}
2356b34c648bSJoe Perches
2357b34c648bSJoe Perchessub rtrim {
2358b34c648bSJoe Perches	my ($string) = @_;
2359b34c648bSJoe Perches
2360b34c648bSJoe Perches	$string =~ s/\s+$//;
23613705ce5bSJoe Perches
23623705ce5bSJoe Perches	return $string;
23633705ce5bSJoe Perches}
23643705ce5bSJoe Perches
236552ea8506SJoe Perchessub string_find_replace {
236652ea8506SJoe Perches	my ($string, $find, $replace) = @_;
236752ea8506SJoe Perches
236852ea8506SJoe Perches	$string =~ s/$find/$replace/g;
236952ea8506SJoe Perches
237052ea8506SJoe Perches	return $string;
237152ea8506SJoe Perches}
237252ea8506SJoe Perches
23733705ce5bSJoe Perchessub tabify {
23743705ce5bSJoe Perches	my ($leading) = @_;
23753705ce5bSJoe Perches
2376713a09deSAntonio Borneo	my $source_indent = $tabsize;
23773705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
23783705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
23793705ce5bSJoe Perches
23803705ce5bSJoe Perches	#convert leading spaces to tabs
23813705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
23823705ce5bSJoe Perches	#Remove spaces before a tab
23833705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
23843705ce5bSJoe Perches
23853705ce5bSJoe Perches	return "$leading";
23863705ce5bSJoe Perches}
23873705ce5bSJoe Perches
2388d1fe9c09SJoe Perchessub pos_last_openparen {
2389d1fe9c09SJoe Perches	my ($line) = @_;
2390d1fe9c09SJoe Perches
2391d1fe9c09SJoe Perches	my $pos = 0;
2392d1fe9c09SJoe Perches
2393d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2394d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2395d1fe9c09SJoe Perches
2396d1fe9c09SJoe Perches	my $last_openparen = 0;
2397d1fe9c09SJoe Perches
2398d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2399d1fe9c09SJoe Perches		return -1;
2400d1fe9c09SJoe Perches	}
2401d1fe9c09SJoe Perches
2402d1fe9c09SJoe Perches	my $len = length($line);
2403d1fe9c09SJoe Perches
2404d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2405d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2406d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2407d1fe9c09SJoe Perches			$pos += length($1) - 1;
2408d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2409d1fe9c09SJoe Perches			$last_openparen = $pos;
2410d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2411d1fe9c09SJoe Perches			last;
2412d1fe9c09SJoe Perches		}
2413d1fe9c09SJoe Perches	}
2414d1fe9c09SJoe Perches
241591cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2416d1fe9c09SJoe Perches}
2417d1fe9c09SJoe Perches
2418f36d3eb8SJoe Perchessub get_raw_comment {
2419f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2420f36d3eb8SJoe Perches	my $comment = '';
2421f36d3eb8SJoe Perches
2422f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2423f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2424f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2425f36d3eb8SJoe Perches		}
2426f36d3eb8SJoe Perches	}
2427f36d3eb8SJoe Perches
2428f36d3eb8SJoe Perches	return $comment;
2429f36d3eb8SJoe Perches}
2430f36d3eb8SJoe Perches
24310a920b5bSAndy Whitcroftsub process {
24320a920b5bSAndy Whitcroft	my $filename = shift;
24330a920b5bSAndy Whitcroft
24340a920b5bSAndy Whitcroft	my $linenr=0;
24350a920b5bSAndy Whitcroft	my $prevline="";
2436c2fdda0dSAndy Whitcroft	my $prevrawline="";
24370a920b5bSAndy Whitcroft	my $stashline="";
2438c2fdda0dSAndy Whitcroft	my $stashrawline="";
24390a920b5bSAndy Whitcroft
24404a0df2efSAndy Whitcroft	my $length;
24410a920b5bSAndy Whitcroft	my $indent;
24420a920b5bSAndy Whitcroft	my $previndent=0;
24430a920b5bSAndy Whitcroft	my $stashindent=0;
24440a920b5bSAndy Whitcroft
2445de7d4f0eSAndy Whitcroft	our $clean = 1;
24460a920b5bSAndy Whitcroft	my $signoff = 0;
2447cd261496SGeert Uytterhoeven	my $author = '';
2448cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
244948ca2d8aSDwaipayan Ray	my $author_sob = '';
24500a920b5bSAndy Whitcroft	my $is_patch = 0;
2451133712a2SRob Herring	my $is_binding_patch = -1;
245229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
245315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
245444d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2455ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2456490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2457bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
24582a076f40SJoe Perches	my $commit_log_long_line = 0;
2459e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
246013f1937eSJoe Perches	my $reported_maintainer_file = 0;
2461fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2462fa64205dSPasi Savanainen
2463365dd4eaSJoe Perches	my $last_blank_line = 0;
24645e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2465365dd4eaSJoe Perches
246613214adfSAndy Whitcroft	our @report = ();
24676c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
24686c72ffaaSAndy Whitcroft	our $cnt_error = 0;
24696c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
24706c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
24716c72ffaaSAndy Whitcroft
24720a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
24730a920b5bSAndy Whitcroft	my $realfile = '';
24740a920b5bSAndy Whitcroft	my $realline = 0;
24750a920b5bSAndy Whitcroft	my $realcnt = 0;
24760a920b5bSAndy Whitcroft	my $here = '';
247777cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
24780a920b5bSAndy Whitcroft	my $in_comment = 0;
2479c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
24800a920b5bSAndy Whitcroft	my $first_line = 0;
24811e855726SWolfram Sang	my $p1_prefix = '';
24820a920b5bSAndy Whitcroft
248313214adfSAndy Whitcroft	my $prev_values = 'E';
248413214adfSAndy Whitcroft
248513214adfSAndy Whitcroft	# suppression flags
2486773647a0SAndy Whitcroft	my %suppress_ifbraces;
2487170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
24882b474a1aSAndy Whitcroft	my %suppress_export;
24893e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2490653d4876SAndy Whitcroft
24917e51f197SJoe Perches	my %signatures = ();
2492323c1260SJoe Perches
2493c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2494de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2495c2fdda0dSAndy Whitcroft	#
2496de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2497de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2498773647a0SAndy Whitcroft
2499d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2500d8b07710SJoe Perches
25019f3a8992SRob Herring	my $checklicenseline = 1;
25029f3a8992SRob Herring
2503773647a0SAndy Whitcroft	sanitise_line_reset();
2504c2fdda0dSAndy Whitcroft	my $line;
2505c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2506773647a0SAndy Whitcroft		$linenr++;
2507773647a0SAndy Whitcroft		$line = $rawline;
2508c2fdda0dSAndy Whitcroft
25093705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
25103705ce5bSJoe Perches
2511773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2512de7d4f0eSAndy Whitcroft			$setup_docs = 0;
25132581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2514de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2515de7d4f0eSAndy Whitcroft			}
2516773647a0SAndy Whitcroft			#next;
2517de7d4f0eSAndy Whitcroft		}
251874fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2519773647a0SAndy Whitcroft			$realline=$1-1;
2520773647a0SAndy Whitcroft			if (defined $2) {
2521773647a0SAndy Whitcroft				$realcnt=$3+1;
2522773647a0SAndy Whitcroft			} else {
2523773647a0SAndy Whitcroft				$realcnt=1+1;
2524773647a0SAndy Whitcroft			}
2525c45dcabdSAndy Whitcroft			$in_comment = 0;
2526773647a0SAndy Whitcroft
2527773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2528773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2529773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2530773647a0SAndy Whitcroft			# at context start.
2531773647a0SAndy Whitcroft			my $edge;
253201fa9147SAndy Whitcroft			my $cnt = $realcnt;
253301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
253401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
253501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
253601fa9147SAndy Whitcroft				$cnt--;
253701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2538721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2539fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2540fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2541fae17daeSAndy Whitcroft					($edge) = $1;
2542fae17daeSAndy Whitcroft					last;
2543fae17daeSAndy Whitcroft				}
2544773647a0SAndy Whitcroft			}
2545773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2546773647a0SAndy Whitcroft				$in_comment = 1;
2547773647a0SAndy Whitcroft			}
2548773647a0SAndy Whitcroft
2549773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2550773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2551773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2552773647a0SAndy Whitcroft			if (!defined $edge &&
255383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2554773647a0SAndy Whitcroft			{
2555773647a0SAndy Whitcroft				$in_comment = 1;
2556773647a0SAndy Whitcroft			}
2557773647a0SAndy Whitcroft
2558773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2559773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2560773647a0SAndy Whitcroft
2561171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2562773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2563171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2564773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2565773647a0SAndy Whitcroft		}
2566773647a0SAndy Whitcroft		push(@lines, $line);
2567773647a0SAndy Whitcroft
2568773647a0SAndy Whitcroft		if ($realcnt > 1) {
2569773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2570773647a0SAndy Whitcroft		} else {
2571773647a0SAndy Whitcroft			$realcnt = 0;
2572773647a0SAndy Whitcroft		}
2573773647a0SAndy Whitcroft
2574773647a0SAndy Whitcroft		#print "==>$rawline\n";
2575773647a0SAndy Whitcroft		#print "-->$line\n";
2576de7d4f0eSAndy Whitcroft
2577de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2578de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2579de7d4f0eSAndy Whitcroft		}
2580de7d4f0eSAndy Whitcroft	}
2581de7d4f0eSAndy Whitcroft
25826c72ffaaSAndy Whitcroft	$prefix = '';
25836c72ffaaSAndy Whitcroft
2584773647a0SAndy Whitcroft	$realcnt = 0;
2585773647a0SAndy Whitcroft	$linenr = 0;
2586194f66fcSJoe Perches	$fixlinenr = -1;
25870a920b5bSAndy Whitcroft	foreach my $line (@lines) {
25880a920b5bSAndy Whitcroft		$linenr++;
2589194f66fcSJoe Perches		$fixlinenr++;
25901b5539b1SJoe Perches		my $sline = $line;	#copy of $line
25911b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
25920a920b5bSAndy Whitcroft
2593c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2594f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
25956c72ffaaSAndy Whitcroft
259612c253abSJoe Perches# check if it's a mode change, rename or start of a patch
259712c253abSJoe Perches		if (!$in_commit_log &&
259812c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
259912c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
260012c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
260112c253abSJoe Perches			$is_patch = 1;
260212c253abSJoe Perches		}
260312c253abSJoe Perches
26040a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2605e518e9a5SJoe Perches		if (!$in_commit_log &&
260674fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
260774fd4f34SJoe Perches			my $context = $4;
26080a920b5bSAndy Whitcroft			$is_patch = 1;
26094a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
26100a920b5bSAndy Whitcroft			$realline=$1-1;
26110a920b5bSAndy Whitcroft			if (defined $2) {
26120a920b5bSAndy Whitcroft				$realcnt=$3+1;
26130a920b5bSAndy Whitcroft			} else {
26140a920b5bSAndy Whitcroft				$realcnt=1+1;
26150a920b5bSAndy Whitcroft			}
2616c2fdda0dSAndy Whitcroft			annotate_reset();
261713214adfSAndy Whitcroft			$prev_values = 'E';
261813214adfSAndy Whitcroft
2619773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2620170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
26212b474a1aSAndy Whitcroft			%suppress_export = ();
26223e469cdcSAndy Whitcroft			$suppress_statement = 0;
262374fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
262474fd4f34SJoe Perches				$context_function = $1;
262574fd4f34SJoe Perches			} else {
262674fd4f34SJoe Perches				undef $context_function;
262774fd4f34SJoe Perches			}
26280a920b5bSAndy Whitcroft			next;
26290a920b5bSAndy Whitcroft
26304a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
26314a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
26324a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2633773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
26340a920b5bSAndy Whitcroft			$realline++;
2635d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
26360a920b5bSAndy Whitcroft
26374a0df2efSAndy Whitcroft			# Measure the line length and indent.
2638c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
26390a920b5bSAndy Whitcroft
26400a920b5bSAndy Whitcroft			# Track the previous line.
26410a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
26420a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2643c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2644c2fdda0dSAndy Whitcroft
2645773647a0SAndy Whitcroft			#warn "line<$line>\n";
26466c72ffaaSAndy Whitcroft
2647d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2648d8aaf121SAndy Whitcroft			$realcnt--;
26490a920b5bSAndy Whitcroft		}
26500a920b5bSAndy Whitcroft
2651cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2652cc77cdcaSAndy Whitcroft
26536c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
26546c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2655773647a0SAndy Whitcroft
26562ac73b4fSJoe Perches		my $found_file = 0;
2657773647a0SAndy Whitcroft		# extract the filename as it passes
26583bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
26593bf9a009SRabin Vincent			$realfile = $1;
26602b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2661270c49a0SJoe Perches			$in_commit_log = 0;
26622ac73b4fSJoe Perches			$found_file = 1;
26633bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2664773647a0SAndy Whitcroft			$realfile = $1;
26652b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2666270c49a0SJoe Perches			$in_commit_log = 0;
26671e855726SWolfram Sang
26681e855726SWolfram Sang			$p1_prefix = $1;
2669e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2670e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2671000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2672000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
26731e855726SWolfram Sang			}
2674773647a0SAndy Whitcroft
2675c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2676000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2677000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2678773647a0SAndy Whitcroft			}
26792ac73b4fSJoe Perches			$found_file = 1;
26802ac73b4fSJoe Perches		}
26812ac73b4fSJoe Perches
268234d8815fSJoe Perches#make up the handle for any error we report on this line
268334d8815fSJoe Perches		if ($showfile) {
268434d8815fSJoe Perches			$prefix = "$realfile:$realline: "
268534d8815fSJoe Perches		} elsif ($emacs) {
26867d3a9f67SJoe Perches			if ($file) {
26877d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
26887d3a9f67SJoe Perches			} else {
268934d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
269034d8815fSJoe Perches			}
26917d3a9f67SJoe Perches		}
269234d8815fSJoe Perches
26932ac73b4fSJoe Perches		if ($found_file) {
269485b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
269585b0ee18SJoe Perches				WARN("OBSOLETE",
269685b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
269785b0ee18SJoe Perches			}
26987bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
26992ac73b4fSJoe Perches				$check = 1;
27002ac73b4fSJoe Perches			} else {
27012ac73b4fSJoe Perches				$check = $check_orig;
27022ac73b4fSJoe Perches			}
27039f3a8992SRob Herring			$checklicenseline = 1;
2704133712a2SRob Herring
2705133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2706133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2707133712a2SRob Herring
2708133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2709133712a2SRob Herring
2710133712a2SRob Herring				if (($last_binding_patch != -1) &&
2711133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2712133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2713858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2714133712a2SRob Herring				}
2715133712a2SRob Herring			}
2716133712a2SRob Herring
2717773647a0SAndy Whitcroft			next;
2718773647a0SAndy Whitcroft		}
2719773647a0SAndy Whitcroft
2720389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
27210a920b5bSAndy Whitcroft
2722c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2723c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2724c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
27250a920b5bSAndy Whitcroft
27266c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
27276c72ffaaSAndy Whitcroft
2728490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2729490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2730490b292cSJoe Perches		if ($in_commit_log) {
2731490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2732490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2733490b292cSJoe Perches			}
2734490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2735490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2736490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2737490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2738490b292cSJoe Perches		}
2739490b292cSJoe Perches
2740e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2741e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
274213e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
274313e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2744e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2745e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2746e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2747e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2748e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2749e518e9a5SJoe Perches		}
2750e518e9a5SJoe Perches
27513bf9a009SRabin Vincent# Check for incorrect file permissions
27523bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
27533bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
275404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
275504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2756000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2757000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
27583bf9a009SRabin Vincent			}
27593bf9a009SRabin Vincent		}
27603bf9a009SRabin Vincent
2761cd261496SGeert Uytterhoeven# Check the patch for a From:
2762cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2763cd261496SGeert Uytterhoeven			$author = $1;
2764e7f929f3SDwaipayan Ray			my $curline = $linenr;
2765e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2766e7f929f3SDwaipayan Ray				$author .= $1;
2767e7f929f3SDwaipayan Ray			}
2768cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2769cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2770dfa05c28SJoe Perches			$author = reformat_email($author);
2771cd261496SGeert Uytterhoeven		}
2772cd261496SGeert Uytterhoeven
277320112475SJoe Perches# Check the patch for a signoff:
2774dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
27754a0df2efSAndy Whitcroft			$signoff++;
277615662b3eSJoe Perches			$in_commit_log = 0;
277748ca2d8aSDwaipayan Ray			if ($author ne ''  && $authorsignoff != 1) {
2778fccaebf0SDwaipayan Ray				if (same_email_addresses($1, $author)) {
2779cd261496SGeert Uytterhoeven					$authorsignoff = 1;
278048ca2d8aSDwaipayan Ray				} else {
278148ca2d8aSDwaipayan Ray					my $ctx = $1;
278248ca2d8aSDwaipayan Ray					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
278348ca2d8aSDwaipayan Ray					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
278448ca2d8aSDwaipayan Ray
278548ca2d8aSDwaipayan Ray					if ($email_address eq $author_address && $email_name eq $author_name) {
278648ca2d8aSDwaipayan Ray						$author_sob = $ctx;
278748ca2d8aSDwaipayan Ray						$authorsignoff = 2;
278848ca2d8aSDwaipayan Ray					} elsif ($email_address eq $author_address) {
278948ca2d8aSDwaipayan Ray						$author_sob = $ctx;
279048ca2d8aSDwaipayan Ray						$authorsignoff = 3;
279148ca2d8aSDwaipayan Ray					} elsif ($email_name eq $author_name) {
279248ca2d8aSDwaipayan Ray						$author_sob = $ctx;
279348ca2d8aSDwaipayan Ray						$authorsignoff = 4;
279448ca2d8aSDwaipayan Ray
279548ca2d8aSDwaipayan Ray						my $address1 = $email_address;
279648ca2d8aSDwaipayan Ray						my $address2 = $author_address;
279748ca2d8aSDwaipayan Ray
279848ca2d8aSDwaipayan Ray						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
279948ca2d8aSDwaipayan Ray							$address1 = "$1$2";
280048ca2d8aSDwaipayan Ray						}
280148ca2d8aSDwaipayan Ray						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
280248ca2d8aSDwaipayan Ray							$address2 = "$1$2";
280348ca2d8aSDwaipayan Ray						}
280448ca2d8aSDwaipayan Ray						if ($address1 eq $address2) {
280548ca2d8aSDwaipayan Ray							$authorsignoff = 5;
280648ca2d8aSDwaipayan Ray						}
280748ca2d8aSDwaipayan Ray					}
2808cd261496SGeert Uytterhoeven				}
2809cd261496SGeert Uytterhoeven			}
28100a920b5bSAndy Whitcroft		}
281120112475SJoe Perches
281244d303ebSJoe Perches# Check for patch separator
281344d303ebSJoe Perches		if ($line =~ /^---$/) {
281444d303ebSJoe Perches			$has_patch_separator = 1;
281544d303ebSJoe Perches			$in_commit_log = 0;
281644d303ebSJoe Perches		}
281744d303ebSJoe Perches
2818e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2819e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2820e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2821e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2822e0d975b1SJoe Perches		}
2823e0d975b1SJoe Perches
282420112475SJoe Perches# Check signature styles
2825270c49a0SJoe Perches		if (!$in_header_lines &&
2826ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
282720112475SJoe Perches			my $space_before = $1;
282820112475SJoe Perches			my $sign_off = $2;
282920112475SJoe Perches			my $space_after = $3;
283020112475SJoe Perches			my $email = $4;
283120112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
283220112475SJoe Perches
2833ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2834831242abSAditya Srivastava				my $suggested_signature = find_standard_signature($sign_off);
2835831242abSAditya Srivastava				if ($suggested_signature eq "") {
2836ce0338dfSJoe Perches					WARN("BAD_SIGN_OFF",
2837ce0338dfSJoe Perches					     "Non-standard signature: $sign_off\n" . $herecurr);
2838831242abSAditya Srivastava				} else {
2839831242abSAditya Srivastava					if (WARN("BAD_SIGN_OFF",
2840831242abSAditya Srivastava						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2841831242abSAditya Srivastava					    $fix) {
2842831242abSAditya Srivastava						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2843831242abSAditya Srivastava					}
2844831242abSAditya Srivastava				}
2845ce0338dfSJoe Perches			}
284620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
28473705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28483705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
28493705ce5bSJoe Perches				    $fix) {
2850194f66fcSJoe Perches					$fixed[$fixlinenr] =
28513705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28523705ce5bSJoe Perches				}
285320112475SJoe Perches			}
285420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
28553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28563705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
28573705ce5bSJoe Perches				    $fix) {
2858194f66fcSJoe Perches					$fixed[$fixlinenr] =
28593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28603705ce5bSJoe Perches				}
28613705ce5bSJoe Perches
286220112475SJoe Perches			}
286320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
28643705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28653705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
28663705ce5bSJoe Perches				    $fix) {
2867194f66fcSJoe Perches					$fixed[$fixlinenr] =
28683705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28693705ce5bSJoe Perches				}
287020112475SJoe Perches			}
287120112475SJoe Perches
2872dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
287348ca2d8aSDwaipayan Ray			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
287420112475SJoe Perches			if ($suggested_email eq "") {
2875000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2876000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
287720112475SJoe Perches			} else {
287820112475SJoe Perches				my $dequoted = $suggested_email;
287920112475SJoe Perches				$dequoted =~ s/^"//;
288020112475SJoe Perches				$dequoted =~ s/" </ </;
288120112475SJoe Perches				# Don't force email to have quotes
288220112475SJoe Perches				# Allow just an angle bracketed address
2883fccaebf0SDwaipayan Ray				if (!same_email_addresses($email, $suggested_email)) {
2884fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2885fccaebf0SDwaipayan Ray						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2886fccaebf0SDwaipayan Ray					    $fix) {
2887fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2888fccaebf0SDwaipayan Ray					}
2889fccaebf0SDwaipayan Ray				}
2890fccaebf0SDwaipayan Ray
2891fccaebf0SDwaipayan Ray				# Address part shouldn't have comments
2892fccaebf0SDwaipayan Ray				my $stripped_address = $email_address;
2893fccaebf0SDwaipayan Ray				$stripped_address =~ s/\([^\(\)]*\)//g;
2894fccaebf0SDwaipayan Ray				if ($email_address ne $stripped_address) {
2895fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2896fccaebf0SDwaipayan Ray						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2897fccaebf0SDwaipayan Ray					    $fix) {
2898fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2899fccaebf0SDwaipayan Ray					}
2900fccaebf0SDwaipayan Ray				}
2901fccaebf0SDwaipayan Ray
2902fccaebf0SDwaipayan Ray				# Only one name comment should be allowed
2903fccaebf0SDwaipayan Ray				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2904fccaebf0SDwaipayan Ray				if ($comment_count > 1) {
2905000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2906fccaebf0SDwaipayan Ray					     "Use a single name comment in email: '$email'\n" . $herecurr);
2907fccaebf0SDwaipayan Ray				}
2908fccaebf0SDwaipayan Ray
2909fccaebf0SDwaipayan Ray
2910fccaebf0SDwaipayan Ray				# [email protected] or [email protected] shouldn't
2911e73d2715SDwaipayan Ray				# have an email name. In addition comments should strictly
2912fccaebf0SDwaipayan Ray				# begin with a #
2913fccaebf0SDwaipayan Ray				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2914fccaebf0SDwaipayan Ray					if (($comment ne "" && $comment !~ /^#.+/) ||
2915fccaebf0SDwaipayan Ray					    ($email_name ne "")) {
2916fccaebf0SDwaipayan Ray						my $cur_name = $email_name;
2917fccaebf0SDwaipayan Ray						my $new_comment = $comment;
2918fccaebf0SDwaipayan Ray						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2919fccaebf0SDwaipayan Ray
2920fccaebf0SDwaipayan Ray						# Remove brackets enclosing comment text
2921fccaebf0SDwaipayan Ray						# and # from start of comments to get comment text
2922fccaebf0SDwaipayan Ray						$new_comment =~ s/^\((.*)\)$/$1/;
2923fccaebf0SDwaipayan Ray						$new_comment =~ s/^\[(.*)\]$/$1/;
2924fccaebf0SDwaipayan Ray						$new_comment =~ s/^[\s\#]+|\s+$//g;
2925fccaebf0SDwaipayan Ray
2926fccaebf0SDwaipayan Ray						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2927fccaebf0SDwaipayan Ray						$new_comment = " # $new_comment" if ($new_comment ne "");
2928fccaebf0SDwaipayan Ray						my $new_email = "$email_address$new_comment";
2929fccaebf0SDwaipayan Ray
2930fccaebf0SDwaipayan Ray						if (WARN("BAD_STABLE_ADDRESS_STYLE",
2931fccaebf0SDwaipayan Ray							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2932fccaebf0SDwaipayan Ray						    $fix) {
2933fccaebf0SDwaipayan Ray							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2934fccaebf0SDwaipayan Ray						}
2935fccaebf0SDwaipayan Ray					}
2936fccaebf0SDwaipayan Ray				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2937fccaebf0SDwaipayan Ray					my $new_comment = $comment;
2938fccaebf0SDwaipayan Ray
2939fccaebf0SDwaipayan Ray					# Extract comment text from within brackets or
2940fccaebf0SDwaipayan Ray					# c89 style /*...*/ comments
2941fccaebf0SDwaipayan Ray					$new_comment =~ s/^\[(.*)\]$/$1/;
2942fccaebf0SDwaipayan Ray					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2943fccaebf0SDwaipayan Ray
2944fccaebf0SDwaipayan Ray					$new_comment = trim($new_comment);
2945fccaebf0SDwaipayan Ray					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2946fccaebf0SDwaipayan Ray					$new_comment = "($new_comment)" if ($new_comment ne "");
2947fccaebf0SDwaipayan Ray					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2948fccaebf0SDwaipayan Ray
2949fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2950fccaebf0SDwaipayan Ray						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2951fccaebf0SDwaipayan Ray					    $fix) {
2952fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2953fccaebf0SDwaipayan Ray					}
295420112475SJoe Perches				}
29550a920b5bSAndy Whitcroft			}
29567e51f197SJoe Perches
29577e51f197SJoe Perches# Check for duplicate signatures
29587e51f197SJoe Perches			my $sig_nospace = $line;
29597e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
29607e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
29617e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
29627e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
29637e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
29647e51f197SJoe Perches			} else {
29657e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
29667e51f197SJoe Perches			}
29676c5d24eeSSean Christopherson
29686c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
29696c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
29706c5d24eeSSean Christopherson				if ($email eq $author) {
29716c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29726c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
29736c5d24eeSSean Christopherson				}
29746c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
29756c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29766c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
29776c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
29786c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29796c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29806c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
29816c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29826c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29836c5d24eeSSean Christopherson				}
29846c5d24eeSSean Christopherson			}
29850a920b5bSAndy Whitcroft		}
29860a920b5bSAndy Whitcroft
2987a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2988a2fe16b9SJoe Perches		if ($in_header_lines &&
2989a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2990a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2991a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2992a2fe16b9SJoe Perches		}
2993a2fe16b9SJoe Perches
299444d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
299544d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
29967580c5b9SAditya Srivastava			if (ERROR("GERRIT_CHANGE_ID",
29977580c5b9SAditya Srivastava			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
29987580c5b9SAditya Srivastava			    $fix) {
29997580c5b9SAditya Srivastava                                fix_delete_line($fixlinenr, $rawline);
30007580c5b9SAditya Srivastava                        }
30017ebd05efSChristopher Covington		}
30027ebd05efSChristopher Covington
3003369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
3004369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3005369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3006369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3007369c8dd3SJoe Perches					# timestamp
3008634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3009634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3010634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3011634cffccSJoe Perches					# stack dump address styles
3012369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
3013369c8dd3SJoe Perches		}
3014369c8dd3SJoe Perches
30152a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
30162a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
3017bf4daf12SJoe Perches		    length($line) > 75 &&
3018bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3019bf4daf12SJoe Perches					# file delta changes
3020bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3021bf4daf12SJoe Perches					# filename then :
302227b379afSAditya Srivastava		      $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
302327b379afSAditya Srivastava					# A Fixes: or Link: line or signature tag line
3024bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
30252a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
30262a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
30272a076f40SJoe Perches			$commit_log_long_line = 1;
30282a076f40SJoe Perches		}
30292a076f40SJoe Perches
3030bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
3031bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
3032bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
3033bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
3034bf4daf12SJoe Perches		}
3035bf4daf12SJoe Perches
3036084a617aSDwaipayan Ray# Check for lines starting with a #
3037084a617aSDwaipayan Ray		if ($in_commit_log && $line =~ /^#/) {
3038084a617aSDwaipayan Ray			if (WARN("COMMIT_COMMENT_SYMBOL",
3039084a617aSDwaipayan Ray				 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3040084a617aSDwaipayan Ray			    $fix) {
3041084a617aSDwaipayan Ray				$fixed[$fixlinenr] =~ s/^/ /;
3042084a617aSDwaipayan Ray			}
3043084a617aSDwaipayan Ray		}
3044084a617aSDwaipayan Ray
30450d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
3046369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3047a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3048e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
3049fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3050aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3051369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3052bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3053fe043ea1SJoe Perches			my $init_char = "c";
3054fe043ea1SJoe Perches			my $orig_commit = "";
30550d7835fcSJoe Perches			my $short = 1;
30560d7835fcSJoe Perches			my $long = 0;
30570d7835fcSJoe Perches			my $case = 1;
30580d7835fcSJoe Perches			my $space = 1;
30590d7835fcSJoe Perches			my $hasdesc = 0;
306019c146a6SJoe Perches			my $hasparens = 0;
30610d7835fcSJoe Perches			my $id = '0123456789ab';
30620d7835fcSJoe Perches			my $orig_desc = "commit description";
30630d7835fcSJoe Perches			my $description = "";
30640d7835fcSJoe Perches
3065fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3066fe043ea1SJoe Perches				$init_char = $1;
3067fe043ea1SJoe Perches				$orig_commit = lc($2);
3068fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3069fe043ea1SJoe Perches				$orig_commit = lc($1);
3070fe043ea1SJoe Perches			}
3071fe043ea1SJoe Perches
30720d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
30730d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
30740d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
30750d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
30760d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
30770d7835fcSJoe Perches				$orig_desc = $1;
307819c146a6SJoe Perches				$hasparens = 1;
30790d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
30800d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
30810d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
30820d7835fcSJoe Perches				$orig_desc = $1;
308319c146a6SJoe Perches				$hasparens = 1;
3084b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3085b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
3086b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3087b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3088b671fde0SJoe Perches				$orig_desc = $1;
3089b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3090b671fde0SJoe Perches				$orig_desc .= " " . $1;
309119c146a6SJoe Perches				$hasparens = 1;
30920d7835fcSJoe Perches			}
30930d7835fcSJoe Perches
30940d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
30950d7835fcSJoe Perches							      $id, $orig_desc);
30960d7835fcSJoe Perches
3097948b133aSHeinrich Schuchardt			if (defined($id) &&
3098948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
3099d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
31000d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
31010d7835fcSJoe Perches			}
3102d311cd44SJoe Perches		}
3103d311cd44SJoe Perches
310413f1937eSJoe Perches# Check for added, moved or deleted files
310513f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
310613f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
310713f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
310813f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
310913f1937eSJoe Perches		      (defined($1) || defined($2))))) {
3110a82603a8SAndrew Jeffery			$is_patch = 1;
311113f1937eSJoe Perches			$reported_maintainer_file = 1;
311213f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
311313f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
311413f1937eSJoe Perches		}
311513f1937eSJoe Perches
3116e400edb1SRob Herring# Check for adding new DT bindings not in schema format
3117e400edb1SRob Herring		if (!$in_commit_log &&
3118e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3119e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3120e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
3121e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3122e400edb1SRob Herring		}
3123e400edb1SRob Herring
312400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
31258905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3126000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
3127000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
31286c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
3129de7d4f0eSAndy Whitcroft		}
3130de7d4f0eSAndy Whitcroft
3131de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3132de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3133171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
3134171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3135171ae1a4SAndy Whitcroft
3136171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
3137171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3138171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
3139171ae1a4SAndy Whitcroft
314034d99219SJoe Perches			CHK("INVALID_UTF8",
3141000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
314200df344fSAndy Whitcroft		}
31430a920b5bSAndy Whitcroft
314415662b3eSJoe Perches# Check if it's the start of a commit log
314515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
314615662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
3147eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
3148eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
314915662b3eSJoe Perches			$in_header_lines = 0;
315015662b3eSJoe Perches			$in_commit_log = 1;
3151ed43c4e5SAllen Hubbe			$has_commit_log = 1;
315215662b3eSJoe Perches		}
315315662b3eSJoe Perches
3154fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
3155fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
3156fa64205dSPasi Savanainen		if ($in_header_lines &&
3157fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3158fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
3159fa64205dSPasi Savanainen			$non_utf8_charset = 1;
3160fa64205dSPasi Savanainen		}
3161fa64205dSPasi Savanainen
3162fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
316315662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
3164fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
316515662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
316615662b3eSJoe Perches		}
316715662b3eSJoe Perches
3168d6430f71SJoe Perches# Check for absolute kernel paths in commit message
3169d6430f71SJoe Perches		if ($tree && $in_commit_log) {
3170d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
3171d6430f71SJoe Perches				my $file = $1;
3172d6430f71SJoe Perches
3173d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3174d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
3175d6430f71SJoe Perches					#
3176d6430f71SJoe Perches				} else {
3177d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
3178d6430f71SJoe Perches				}
3179d6430f71SJoe Perches			}
3180d6430f71SJoe Perches		}
3181d6430f71SJoe Perches
318266b47b4aSKees Cook# Check for various typo / spelling mistakes
318366d7a382SJoe Perches		if (defined($misspellings) &&
318466d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3185*7da07c31SDwaipayan Ray			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
318666b47b4aSKees Cook				my $typo = $1;
3187*7da07c31SDwaipayan Ray				my $blank = copy_spacing($rawline);
3188*7da07c31SDwaipayan Ray				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3189*7da07c31SDwaipayan Ray				my $hereptr = "$hereline$ptr\n";
319066b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
319166b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
319266b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
31930675a8fbSJean Delvare				my $msg_level = \&WARN;
31940675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
31950675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
3196*7da07c31SDwaipayan Ray						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
319766b47b4aSKees Cook				    $fix) {
319866b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
319966b47b4aSKees Cook				}
320066b47b4aSKees Cook			}
320166b47b4aSKees Cook		}
320266b47b4aSKees Cook
3203a8dd86bfSMatteo Croce# check for invalid commit id
3204a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3205a8dd86bfSMatteo Croce			my $id;
3206a8dd86bfSMatteo Croce			my $description;
3207a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
3208a8dd86bfSMatteo Croce			if (!defined($id)) {
3209a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
3210a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3211a8dd86bfSMatteo Croce			}
3212a8dd86bfSMatteo Croce		}
3213a8dd86bfSMatteo Croce
3214310cd06bSJoe Perches# check for repeated words separated by a single space
32158d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
32168d0325ccSAditya Srivastava		if (($rawline =~ /^\+/ || $in_commit_log) &&
32178d0325ccSAditya Srivastava		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
32181db81a68SDwaipayan Ray			pos($rawline) = 1 if (!$in_commit_log);
3219310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3220310cd06bSJoe Perches
3221310cd06bSJoe Perches				my $first = $1;
3222310cd06bSJoe Perches				my $second = $2;
32231db81a68SDwaipayan Ray				my $start_pos = $-[1];
32241db81a68SDwaipayan Ray				my $end_pos = $+[2];
3225310cd06bSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3226310cd06bSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3227310cd06bSJoe Perches					next;
3228310cd06bSJoe Perches				}
3229310cd06bSJoe Perches
32301db81a68SDwaipayan Ray				next if (lc($first) ne lc($second));
3231310cd06bSJoe Perches				next if ($first eq 'long');
3232310cd06bSJoe Perches
32331db81a68SDwaipayan Ray				# check for character before and after the word matches
32341db81a68SDwaipayan Ray				my $start_char = '';
32351db81a68SDwaipayan Ray				my $end_char = '';
32361db81a68SDwaipayan Ray				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
32371db81a68SDwaipayan Ray				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
32381db81a68SDwaipayan Ray
32391db81a68SDwaipayan Ray				next if ($start_char =~ /^\S$/);
32401db81a68SDwaipayan Ray				next if (index(" \t.,;?!", $end_char) == -1);
32411db81a68SDwaipayan Ray
32428d0325ccSAditya Srivastava                                # avoid repeating hex occurrences like 'ff ff fe 09 ...'
32438d0325ccSAditya Srivastava                                if ($first =~ /\b[0-9a-f]{2,}\b/i) {
32448d0325ccSAditya Srivastava                                        next if (!exists($allow_repeated_words{lc($first)}));
32458d0325ccSAditya Srivastava                                }
32468d0325ccSAditya Srivastava
3247310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3248310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3249310cd06bSJoe Perches				    $fix) {
3250310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3251310cd06bSJoe Perches				}
3252310cd06bSJoe Perches			}
3253310cd06bSJoe Perches
3254310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3255310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3256310cd06bSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3257310cd06bSJoe Perches				my $last_word = $1;
3258310cd06bSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3259310cd06bSJoe Perches					if (WARN("REPEATED_WORD",
3260310cd06bSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3261310cd06bSJoe Perches					    $fix) {
3262310cd06bSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3263310cd06bSJoe Perches					}
3264310cd06bSJoe Perches				}
3265310cd06bSJoe Perches			}
3266310cd06bSJoe Perches		}
3267310cd06bSJoe Perches
326830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
326930670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
327000df344fSAndy Whitcroft
32710a920b5bSAndy Whitcroft#trailing whitespace
32729c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
3273c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3274d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3275d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
3276d5e616fcSJoe Perches			    $fix) {
3277194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3278d5e616fcSJoe Perches			}
3279c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3280c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32813705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
32823705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
32833705ce5bSJoe Perches			    $fix) {
3284194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
32853705ce5bSJoe Perches			}
32863705ce5bSJoe Perches
3287d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32880a920b5bSAndy Whitcroft		}
32895368df20SAndy Whitcroft
32904783f894SJosh Triplett# Check for FSF mailing addresses.
3291109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
32921bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
32933e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
32943e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
32954783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32960675a8fbSJean Delvare			my $msg_level = \&ERROR;
32970675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
32980675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
32994783f894SJosh Triplett				      "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
33004783f894SJosh Triplett		}
33014783f894SJosh Triplett
33023354957aSAndi Kleen# check for Kconfig help text having a real description
33039fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
33049fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
33053354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3306678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3307678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3308678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3309678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
33103354957aSAndi Kleen			my $length = 0;
33119fe287d7SAndy Whitcroft			my $cnt = $realcnt;
33129fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
33139fe287d7SAndy Whitcroft			my $f;
3314a1385803SAndy Whitcroft			my $is_start = 0;
33159fe287d7SAndy Whitcroft			my $is_end = 0;
3316a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
33179fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
33189fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
33199fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
33209fe287d7SAndy Whitcroft
33219fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
33228d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3323a1385803SAndy Whitcroft
332486adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3325a1385803SAndy Whitcroft					$is_start = 1;
332622a4ac02SMasahiro Yamada				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
3327a1385803SAndy Whitcroft					$length = -1;
3328a1385803SAndy Whitcroft				}
3329a1385803SAndy Whitcroft
33309fe287d7SAndy Whitcroft				$f =~ s/^.//;
33313354957aSAndi Kleen				$f =~ s/#.*//;
33323354957aSAndi Kleen				$f =~ s/^\s+//;
33333354957aSAndi Kleen				next if ($f =~ /^$/);
3334678ae162SUlf Magnusson
3335678ae162SUlf Magnusson				# This only checks context lines in the patch
3336678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3337678ae162SUlf Magnusson				# positives, even though some of these are
3338678ae162SUlf Magnusson				# common words in help texts
3339678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3340678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
33419fe287d7SAndy Whitcroft					$is_end = 1;
33429fe287d7SAndy Whitcroft					last;
33439fe287d7SAndy Whitcroft				}
33443354957aSAndi Kleen				$length++;
33453354957aSAndi Kleen			}
334656193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3347000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
334856193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
334956193274SVadim Bendebury			}
3350a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
33513354957aSAndi Kleen		}
33523354957aSAndi Kleen
33537ccf41a8SJoe Perches# check MAINTAINERS entries
33547ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
33557ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
33567ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3357628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3358628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3359628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3360628f91a2SJoe Perches				    $fix) {
3361628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3362628f91a2SJoe Perches				}
3363628f91a2SJoe Perches			}
33647ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
33657ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
33667ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
33677ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
33687ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
33697ccf41a8SJoe Perches				my $cur = $1;
33707ccf41a8SJoe Perches				my $curval = $2;
33717ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
33727ccf41a8SJoe Perches				my $prev = $1;
33737ccf41a8SJoe Perches				my $prevval = $2;
33747ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
33757ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
33767ccf41a8SJoe Perches				if ($curindex < 0) {
33777ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
33787ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
33797ccf41a8SJoe Perches				} else {
33807ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
33817ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33827ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
33837ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
33847ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
33857ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
33867ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33877ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
33887ccf41a8SJoe Perches					}
33897ccf41a8SJoe Perches				}
33907ccf41a8SJoe Perches			}
33917ccf41a8SJoe Perches		}
3392628f91a2SJoe Perches
3393327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3394327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3395327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3396327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3397327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3398327953e9SChristoph Jaeger		}
3399327953e9SChristoph Jaeger
3400c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3401c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3402c68e5878SArnaud Lacombe			my $flag = $1;
3403c68e5878SArnaud Lacombe			my $replacement = {
3404c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3405c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3406c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3407c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3408c68e5878SArnaud Lacombe			};
3409c68e5878SArnaud Lacombe
3410c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3411c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3412c68e5878SArnaud Lacombe		}
3413c68e5878SArnaud Lacombe
3414bff5da43SRob Herring# check for DT compatible documentation
34157dd05b38SFlorian Vaussard		if (defined $root &&
34167dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
34177dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
34187dd05b38SFlorian Vaussard
3419bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3420bff5da43SRob Herring
3421cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3422852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3423cc93319bSFlorian Vaussard
3424bff5da43SRob Herring			foreach my $compat (@compats) {
3425bff5da43SRob Herring				my $compat2 = $compat;
3426185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3427185d566bSRob Herring				my $compat3 = $compat;
3428185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3429185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3430bff5da43SRob Herring				if ( $? >> 8 ) {
3431bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3432bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3433bff5da43SRob Herring				}
3434bff5da43SRob Herring
34354fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
34364fbf32a6SFlorian Vaussard				my $vendor = $1;
3437852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3438bff5da43SRob Herring				if ( $? >> 8 ) {
3439bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3440cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3441bff5da43SRob Herring				}
3442bff5da43SRob Herring			}
3443bff5da43SRob Herring		}
3444bff5da43SRob Herring
34459f3a8992SRob Herring# check for using SPDX license tag at beginning of files
34469f3a8992SRob Herring		if ($realline == $checklicenseline) {
34479f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
34489f3a8992SRob Herring				$checklicenseline = 2;
34499f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
34509f3a8992SRob Herring				my $comment = "";
34519f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
34529f3a8992SRob Herring					$comment = '/*';
34539f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
34549f3a8992SRob Herring					$comment = '//';
3455c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
34569f3a8992SRob Herring					$comment = '#';
34579f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
34589f3a8992SRob Herring					$comment = '..';
34599f3a8992SRob Herring				}
34609f3a8992SRob Herring
3461fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3462fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3463fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3464ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3465fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3466fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3467fdf13693SJoe Perches				}
3468fdf13693SJoe Perches
34699f3a8992SRob Herring				if ($comment !~ /^$/ &&
3470ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
34719f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
34729f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
34733b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
34743b6e8ac9SJoe Perches					my $spdx_license = $1;
34753b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
34763b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
34773b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
34783b6e8ac9SJoe Perches					}
347950c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
348050c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
348150c92900SLubomir Rintel						my $msg_level = \&WARN;
348250c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
348350c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
348450c92900SLubomir Rintel
348550c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
348650c92900SLubomir Rintel						    $fix) {
348750c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
348850c92900SLubomir Rintel						}
348950c92900SLubomir Rintel					}
34909f3a8992SRob Herring				}
34919f3a8992SRob Herring			}
34929f3a8992SRob Herring		}
34939f3a8992SRob Herring
3494a0154cdbSJoe Perches# check for embedded filenames
3495a0154cdbSJoe Perches		if ($rawline =~ /^\+.*\Q$realfile\E/) {
3496a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3497a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3498a0154cdbSJoe Perches		}
3499a0154cdbSJoe Perches
35005368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3501d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
35025368df20SAndy Whitcroft
3503a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3504a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3505a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3506a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3507a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3508a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3509a8da38a9SJoe Perches		}
3510a8da38a9SJoe Perches
351147e0c88bSJoe Perches# line length limit (with some exclusions)
351247e0c88bSJoe Perches#
351347e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
351447e0c88bSJoe Perches#	logging functions like pr_info that end in a string
351547e0c88bSJoe Perches#	lines with a single string
351647e0c88bSJoe Perches#	#defines that are a single string
35172e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
351847e0c88bSJoe Perches#
351947e0c88bSJoe Perches# There are 3 different line length message types:
3520ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
352147e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
352247e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
352347e0c88bSJoe Perches#
352447e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
352547e0c88bSJoe Perches#
352647e0c88bSJoe Perches
3527b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
352847e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
352947e0c88bSJoe Perches
353047e0c88bSJoe Perches			# Check the allowed long line types first
353147e0c88bSJoe Perches
353247e0c88bSJoe Perches			# logging functions that end in a string that starts
353347e0c88bSJoe Perches			# before $max_line_length
353447e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
353547e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
353647e0c88bSJoe Perches				$msg_type = "";
353747e0c88bSJoe Perches
353847e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
353947e0c88bSJoe Perches			# #defines with only strings
354047e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
354147e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
354247e0c88bSJoe Perches				$msg_type = "";
354347e0c88bSJoe Perches
3544cc147506SJoe Perches			# More special cases
3545cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3546cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3547d560a5f8SJoe Perches				$msg_type = "";
3548d560a5f8SJoe Perches
35492e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
35502e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
35512e4bbbc5SAndreas Brauchli				$msg_type = "";
35522e4bbbc5SAndreas Brauchli
355347e0c88bSJoe Perches			# Otherwise set the alternate message types
355447e0c88bSJoe Perches
355547e0c88bSJoe Perches			# a comment starts before $max_line_length
355647e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
355747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
355847e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
355947e0c88bSJoe Perches
356047e0c88bSJoe Perches			# a quoted string starts before $max_line_length
356147e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
356247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
356347e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
356447e0c88bSJoe Perches			}
356547e0c88bSJoe Perches
356647e0c88bSJoe Perches			if ($msg_type ne "" &&
356747e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3568bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3569bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3570bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3571bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
35720a920b5bSAndy Whitcroft			}
357347e0c88bSJoe Perches		}
35740a920b5bSAndy Whitcroft
35758905a67cSAndy Whitcroft# check for adding lines without a newline.
35768905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
357747ca69b8STom Rix			if (WARN("MISSING_EOF_NEWLINE",
357847ca69b8STom Rix			         "adding a line without newline at end of file\n" . $herecurr) &&
357947ca69b8STom Rix			    $fix) {
358047ca69b8STom Rix				fix_delete_line($fixlinenr+1, "No newline at end of file");
358147ca69b8STom Rix			}
35828905a67cSAndy Whitcroft		}
35838905a67cSAndy Whitcroft
3584b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3585de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
35860a920b5bSAndy Whitcroft
35870a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3588713a09deSAntonio Borneo# more than $tabsize must use tabs.
3589c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3590c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3591c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3592d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
35933705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
35943705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
35953705ce5bSJoe Perches			    $fix) {
3596194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
35973705ce5bSJoe Perches			}
35980a920b5bSAndy Whitcroft		}
35990a920b5bSAndy Whitcroft
360008e44365SAlberto Panizzo# check for space before tabs.
360108e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
360208e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
36033705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
36043705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
36053705ce5bSJoe Perches			    $fix) {
3606194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3607713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3608194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3609c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
36103705ce5bSJoe Perches			}
361108e44365SAlberto Panizzo		}
361208e44365SAlberto Panizzo
36136a487211SJoe Perches# check for assignments on the start of a line
36146a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3615da7355abSAditya Srivastava			my $operator = $1;
3616da7355abSAditya Srivastava			if (CHK("ASSIGNMENT_CONTINUATIONS",
3617da7355abSAditya Srivastava				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3618da7355abSAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
3619da7355abSAditya Srivastava				# add assignment operator to the previous line, remove from current line
3620da7355abSAditya Srivastava				$fixed[$fixlinenr - 1] .= " $operator";
3621da7355abSAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3622da7355abSAditya Srivastava			}
36236a487211SJoe Perches		}
36246a487211SJoe Perches
3625d1fe9c09SJoe Perches# check for && or || at the start of a line
3626d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
36278e08f076SAditya Srivastava			my $operator = $1;
36288e08f076SAditya Srivastava			if (CHK("LOGICAL_CONTINUATIONS",
36298e08f076SAditya Srivastava				"Logical continuations should be on the previous line\n" . $hereprev) &&
36308e08f076SAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
36318e08f076SAditya Srivastava				# insert logical operator at last non-comment, non-whitepsace char on previous line
36328e08f076SAditya Srivastava				$prevline =~ /[\s$;]*$/;
36338e08f076SAditya Srivastava				my $line_end = substr($prevrawline, $-[0]);
36348e08f076SAditya Srivastava				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
36358e08f076SAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
36368e08f076SAditya Srivastava			}
3637d1fe9c09SJoe Perches		}
3638d1fe9c09SJoe Perches
3639a91e8994SJoe Perches# check indentation starts on a tab stop
36405b57980dSJoe Perches		if ($perl_version_ok &&
3641bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3642a91e8994SJoe Perches			my $indent = length($1);
3643713a09deSAntonio Borneo			if ($indent % $tabsize) {
3644a91e8994SJoe Perches				if (WARN("TABSTOP",
3645a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3646a91e8994SJoe Perches				    $fix) {
3647713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3648a91e8994SJoe Perches				}
3649a91e8994SJoe Perches			}
3650a91e8994SJoe Perches		}
3651a91e8994SJoe Perches
3652d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
36535b57980dSJoe Perches		if ($perl_version_ok &&
3654fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3655d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3656d1fe9c09SJoe Perches			my $oldindent = $1;
3657d1fe9c09SJoe Perches			my $rest = $2;
3658d1fe9c09SJoe Perches
3659d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3660d1fe9c09SJoe Perches			if ($pos >= 0) {
3661b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3662b34a26f3SJoe Perches				my $newindent = $2;
3663d1fe9c09SJoe Perches
3664d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3665713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3666713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3667d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3668d1fe9c09SJoe Perches
3669d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3670d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
36713705ce5bSJoe Perches
36723705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
36733705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
36743705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3675194f66fcSJoe Perches						$fixed[$fixlinenr] =~
36763705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
36773705ce5bSJoe Perches					}
3678d1fe9c09SJoe Perches				}
3679d1fe9c09SJoe Perches			}
3680d1fe9c09SJoe Perches		}
3681d1fe9c09SJoe Perches
36826ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
36836ab3a970SJoe Perches# avoid checking a few false positives:
36846ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
36856ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
36866ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
36876ab3a970SJoe Perches#   multiline macros that define functions
36886ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
36896ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
36906ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
36913705ce5bSJoe Perches			if (CHK("SPACING",
3692f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
36933705ce5bSJoe Perches			    $fix) {
3694194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3695f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
36963705ce5bSJoe Perches			}
3697aad4f614SJoe Perches		}
3698aad4f614SJoe Perches
369986406b1cSJoe Perches# Block comment styles
370086406b1cSJoe Perches# Networking with an initial /*
370105880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3702fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
370385ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
3704c70735c2SŁukasz Stelmach		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
370505880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
370605880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
370705880600SJoe Perches		}
370805880600SJoe Perches
370986406b1cSJoe Perches# Block comments use * on subsequent lines
371086406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
371186406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3712a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
371361135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3714a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
371586406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
371686406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3717a605e32eSJoe Perches		}
3718a605e32eSJoe Perches
371986406b1cSJoe Perches# Block comments use */ on trailing lines
372086406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3721c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3722c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3723c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
372486406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
372586406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
372605880600SJoe Perches		}
372705880600SJoe Perches
372808eb9b80SJoe Perches# Block comment * alignment
372908eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3730af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3731af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3732af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
373308eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3734af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3735af207524SJoe Perches			my $oldindent;
373608eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3737af207524SJoe Perches			if (defined($1)) {
3738af207524SJoe Perches				$oldindent = expand_tabs($1);
3739af207524SJoe Perches			} else {
3740af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3741af207524SJoe Perches				$oldindent = expand_tabs($1);
3742af207524SJoe Perches			}
374308eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
374408eb9b80SJoe Perches			my $newindent = $1;
374508eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3746af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
374708eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
374808eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
374908eb9b80SJoe Perches			}
375008eb9b80SJoe Perches		}
375108eb9b80SJoe Perches
37527f619191SJoe Perches# check for missing blank lines after struct/union declarations
37537f619191SJoe Perches# with exceptions for various attributes and macros
37547f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
37557f619191SJoe Perches		    $line =~ /^\+/ &&
37567f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
37577f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
37587f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
37597f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
37607f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
37617f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
37627f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
37630bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
37647f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3765d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3766d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3767d752fcc8SJoe Perches			    $fix) {
3768f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3769d752fcc8SJoe Perches			}
37707f619191SJoe Perches		}
37717f619191SJoe Perches
3772365dd4eaSJoe Perches# check for multiple consecutive blank lines
3773365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3774365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3775365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3776d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3777d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3778d752fcc8SJoe Perches			    $fix) {
3779f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3780d752fcc8SJoe Perches			}
3781d752fcc8SJoe Perches
3782365dd4eaSJoe Perches			$last_blank_line = $linenr;
3783365dd4eaSJoe Perches		}
3784365dd4eaSJoe Perches
37853b617e3bSJoe Perches# check for missing blank lines after declarations
37863f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
37873f7bac03SJoe Perches			# actual declarations
37883f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
37895a4e1fd3SJoe Perches			# function pointer declarations
37905a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
37913f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
37923f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
37933f7bac03SJoe Perches			# known declaration macros
37943f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
37953f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
37963f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
37973f7bac03SJoe Perches			# other possible extensions of declaration lines
37983f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
37993f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
38003f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
38013f7bac03SJoe Perches			# looks like a declaration
38023f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
38035a4e1fd3SJoe Perches			# function pointer declarations
38045a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
38053f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
38063f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
38073f7bac03SJoe Perches			# known declaration macros
38083f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
38093f7bac03SJoe Perches			# start of struct or union or enum
3810328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
38113f7bac03SJoe Perches			# start or end of block or continuation of declaration
38123f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
38133f7bac03SJoe Perches			# bitfield continuation
38143f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
38153f7bac03SJoe Perches			# other possible extensions of declaration lines
38163f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
38173f7bac03SJoe Perches			# indentation of previous and current line are the same
38183f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3819d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3820d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3821d752fcc8SJoe Perches			    $fix) {
3822f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3823d752fcc8SJoe Perches			}
38243b617e3bSJoe Perches		}
38253b617e3bSJoe Perches
38265f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
38276b4c5bebSAndy Whitcroft# Exceptions:
38286b4c5bebSAndy Whitcroft#  1) within comments
38296b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
38306b4c5bebSAndy Whitcroft#  3) hanging labels
38313705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
38325f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
38333705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
38343705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
38353705ce5bSJoe Perches			    $fix) {
3836194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
38373705ce5bSJoe Perches			}
38385f7ddae6SRaffaele Recalcati		}
38395f7ddae6SRaffaele Recalcati
3840b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3841b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3842b9ea10d6SAndy Whitcroft
38435751a24eSJoe Perches# check for unusual line ending [ or (
38445751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
38455751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
38465751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
38475751a24eSJoe Perches		}
38485751a24eSJoe Perches
38494dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
38504dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
38514dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
38524dbed76fSJoe Perches			$context_function = $1;
38534dbed76fSJoe Perches		}
38544dbed76fSJoe Perches
38554dbed76fSJoe Perches# check if this appears to be the end of function declaration
38564dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
38574dbed76fSJoe Perches			undef $context_function;
38584dbed76fSJoe Perches		}
38594dbed76fSJoe Perches
3860032a4c0fSJoe Perches# check indentation of any line with a bare else
3861840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3862032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3863032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3864032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3865840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3866840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3867840080a0SJoe Perches			     defined $lines[$linenr] &&
3868840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3869032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3870032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3871032a4c0fSJoe Perches			}
3872032a4c0fSJoe Perches		}
3873032a4c0fSJoe Perches
3874c00df19aSJoe Perches# check indentation of a line with a break;
3875dc58bc55SJoe Perches# if the previous line is a goto, return or break
3876dc58bc55SJoe Perches# and is indented the same # of tabs
3877c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3878c00df19aSJoe Perches			my $tabs = $1;
3879dc58bc55SJoe Perches			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3880dc58bc55SJoe Perches				if (WARN("UNNECESSARY_BREAK",
3881dc58bc55SJoe Perches					 "break is not useful after a $1\n" . $hereprev) &&
3882dc58bc55SJoe Perches				    $fix) {
3883dc58bc55SJoe Perches					fix_delete_line($fixlinenr, $rawline);
3884dc58bc55SJoe Perches				}
3885c00df19aSJoe Perches			}
3886c00df19aSJoe Perches		}
3887c00df19aSJoe Perches
3888c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3889cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3890000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3891000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3892c2fdda0dSAndy Whitcroft		}
389322f2a2efSAndy Whitcroft
389456e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
389556e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
389656e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
389756e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
389856e77d70SJoe Perches		}
389956e77d70SJoe Perches
39009c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
39012b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
39022b474a1aSAndy Whitcroft		    $realline_next);
39033e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3904ca819864SJoe Perches		if ($linenr > $suppress_statement &&
39051b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3906170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3907f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3908171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3909171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3910171ae1a4SAndy Whitcroft
39113e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
39123e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
39133e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
39143e469cdcSAndy Whitcroft			# until we hit end of it.
39153e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
39163e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
39173e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
39183e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
39193e469cdcSAndy Whitcroft			}
3920f74bd194SAndy Whitcroft
39212b474a1aSAndy Whitcroft			# Find the real next line.
39222b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
39232b474a1aSAndy Whitcroft			if (defined $realline_next &&
39242b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
39252b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
39262b474a1aSAndy Whitcroft				$realline_next++;
39272b474a1aSAndy Whitcroft			}
39282b474a1aSAndy Whitcroft
3929171ae1a4SAndy Whitcroft			my $s = $stat;
3930171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3931cf655043SAndy Whitcroft
3932c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3933171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3934c2fdda0dSAndy Whitcroft
3935c2fdda0dSAndy Whitcroft			# Ignore functions being called
3936171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3937c2fdda0dSAndy Whitcroft
3938463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3939463f2864SAndy Whitcroft
3940c45dcabdSAndy Whitcroft			# declarations always start with types
3941d2506586SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
3942c45dcabdSAndy Whitcroft				my $type = $1;
3943c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3944c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3945c45dcabdSAndy Whitcroft
39466c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3947a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3948c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3949c2fdda0dSAndy Whitcroft			}
39508905a67cSAndy Whitcroft
39516c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
395265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3953c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
39549c0ca6f9SAndy Whitcroft			}
39558905a67cSAndy Whitcroft
39568905a67cSAndy Whitcroft			# Check for any sort of function declaration.
39578905a67cSAndy Whitcroft			# int foo(something bar, other baz);
39588905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3959171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
39608905a67cSAndy Whitcroft				my ($name_len) = length($1);
39618905a67cSAndy Whitcroft
3962cf655043SAndy Whitcroft				my $ctx = $s;
3963773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
39648905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3965cf655043SAndy Whitcroft
39668905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3967c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
39688905a67cSAndy Whitcroft
3969c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
39708905a67cSAndy Whitcroft					}
39718905a67cSAndy Whitcroft				}
39728905a67cSAndy Whitcroft			}
39738905a67cSAndy Whitcroft
39749c0ca6f9SAndy Whitcroft		}
39759c0ca6f9SAndy Whitcroft
397600df344fSAndy Whitcroft#
397700df344fSAndy Whitcroft# Checks which may be anchored in the context.
397800df344fSAndy Whitcroft#
397900df344fSAndy Whitcroft
398000df344fSAndy Whitcroft# Check for switch () and associated case and default
398100df344fSAndy Whitcroft# statements should be at the same indent.
398200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
398300df344fSAndy Whitcroft			my $err = '';
398400df344fSAndy Whitcroft			my $sep = '';
398500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
398600df344fSAndy Whitcroft			shift(@ctx);
398700df344fSAndy Whitcroft			for my $ctx (@ctx) {
398800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
398900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
399000df344fSAndy Whitcroft							$indent != $cindent) {
399100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
399200df344fSAndy Whitcroft					$sep = '';
399300df344fSAndy Whitcroft				} else {
399400df344fSAndy Whitcroft					$sep = "[...]\n";
399500df344fSAndy Whitcroft				}
399600df344fSAndy Whitcroft			}
399700df344fSAndy Whitcroft			if ($err ne '') {
3998000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3999000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
4000de7d4f0eSAndy Whitcroft			}
4001de7d4f0eSAndy Whitcroft		}
4002de7d4f0eSAndy Whitcroft
4003de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
4004de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
40050fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4006773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
4007773647a0SAndy Whitcroft
40089c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
40098eef05ddSJoe Perches
40108eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
40118eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
40128eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
40138eef05ddSJoe Perches			}
40148eef05ddSJoe Perches
4015de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
4016de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
4017de7d4f0eSAndy Whitcroft
4018548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
4019548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
4020de7d4f0eSAndy Whitcroft
4021548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4022548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
4023548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
4024548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4025548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4026773647a0SAndy Whitcroft				$ctx_ln++;
4027773647a0SAndy Whitcroft			}
4028548596d5SAndy Whitcroft
402953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
403053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4031773647a0SAndy Whitcroft
4032773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4033000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
4034000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
403501464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
403600df344fSAndy Whitcroft			}
4037773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4038773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
4039773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
4040773647a0SAndy Whitcroft			{
40419c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
40429c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
4043000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
4044000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
404501464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
40469c0ca6f9SAndy Whitcroft				}
40479c0ca6f9SAndy Whitcroft			}
404800df344fSAndy Whitcroft		}
404900df344fSAndy Whitcroft
40504d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
4051f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
40523e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
40533e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
40543e469cdcSAndy Whitcroft					if (!defined $stat);
40554d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
40564d001e4dSAndy Whitcroft
40574d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
40584d001e4dSAndy Whitcroft
40599f5af480SJoe Perches			# remove inline comments
40609f5af480SJoe Perches			$s =~ s/$;/ /g;
40619f5af480SJoe Perches			$c =~ s/$;/ /g;
40624d001e4dSAndy Whitcroft
40634d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
40646f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
40656f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
40664d001e4dSAndy Whitcroft
40679f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
40689f5af480SJoe Perches			# none on the first line, and are going to readd them
40699f5af480SJoe Perches			# where necessary.
40709f5af480SJoe Perches			$s =~ s/\n./\n/gs;
40719f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
40729f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
40739f5af480SJoe Perches			}
40749f5af480SJoe Perches
40754d001e4dSAndy Whitcroft			# We want to check the first line inside the block
40764d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
40774d001e4dSAndy Whitcroft			#  1) any blank line termination
40784d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
40794d001e4dSAndy Whitcroft			#  3) any do (...) {
40804d001e4dSAndy Whitcroft			my $continuation = 0;
40814d001e4dSAndy Whitcroft			my $check = 0;
40824d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
40834d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
40844d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
40854d001e4dSAndy Whitcroft				$continuation = 1;
40864d001e4dSAndy Whitcroft			}
40879bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
40884d001e4dSAndy Whitcroft				$check = 1;
40894d001e4dSAndy Whitcroft				$cond_lines++;
40904d001e4dSAndy Whitcroft			}
40914d001e4dSAndy Whitcroft
40924d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
40934d001e4dSAndy Whitcroft			# preprocessor statement.
40944d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
40954d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
40964d001e4dSAndy Whitcroft				$check = 0;
40974d001e4dSAndy Whitcroft			}
40984d001e4dSAndy Whitcroft
40999bd49efeSAndy Whitcroft			my $cond_ptr = -1;
4100740504c6SAndy Whitcroft			$continuation = 0;
41019bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
41029bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
41034d001e4dSAndy Whitcroft
4104f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
4105f16fa28fSAndy Whitcroft				# is not linear.
4106f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4107f16fa28fSAndy Whitcroft					$check = 0;
4108f16fa28fSAndy Whitcroft				}
4109f16fa28fSAndy Whitcroft
41109bd49efeSAndy Whitcroft				# Ignore:
41119bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
41129bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
41139bd49efeSAndy Whitcroft				#  3) labels.
4114740504c6SAndy Whitcroft				if ($continuation ||
4115740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
41169bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
41179bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
4118740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
411930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
41209bd49efeSAndy Whitcroft						$cond_lines++;
41219bd49efeSAndy Whitcroft					}
41224d001e4dSAndy Whitcroft				}
412330dad6ebSAndy Whitcroft			}
41244d001e4dSAndy Whitcroft
41254d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
41264d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
41274d001e4dSAndy Whitcroft
41284d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
41294d001e4dSAndy Whitcroft			# this is not this patch's fault.
41304d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
41314d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
41324d001e4dSAndy Whitcroft				$check = 0;
41334d001e4dSAndy Whitcroft			}
41344d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
41354d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
41364d001e4dSAndy Whitcroft			}
41374d001e4dSAndy Whitcroft
41389bd49efeSAndy Whitcroft			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
41394d001e4dSAndy Whitcroft
41409f5af480SJoe Perches			if ($check && $s ne '' &&
4141713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
41429f5af480SJoe Perches			     ($sindent < $indent) ||
4143f6950a73SJoe Perches			     ($sindent == $indent &&
4144f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4145713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
4146000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
4147000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
41484d001e4dSAndy Whitcroft			}
41494d001e4dSAndy Whitcroft		}
41504d001e4dSAndy Whitcroft
41516c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
41526c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
41531f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
41541f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
41556c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
4156c2fdda0dSAndy Whitcroft		if ($dbg_values) {
4157c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
4158cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
4159cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
41601f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
4161c2fdda0dSAndy Whitcroft		}
41626c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
41636c72ffaaSAndy Whitcroft
416400df344fSAndy Whitcroft#ignore lines not being added
41653705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
416600df344fSAndy Whitcroft
416799ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
416899ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
416999ca38c2SJoe Perches#	struct foo bar = *(&(bar));
417099ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
417199ca38c2SJoe Perches			my $var = $1;
417299ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
417399ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
417499ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
417599ca38c2SJoe Perches			}
417699ca38c2SJoe Perches		}
417799ca38c2SJoe Perches
417811ca40a0SJoe Perches# check for dereferences that span multiple lines
417911ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
418011ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
418111ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
418211ca40a0SJoe Perches			my $ref = $1;
418311ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
418411ca40a0SJoe Perches			$ref .= $1;
418511ca40a0SJoe Perches			$ref =~ s/\s//g;
418611ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
418711ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
418811ca40a0SJoe Perches		}
418911ca40a0SJoe Perches
4190a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
4191c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4192a1ce18e4SJoe Perches			my $type = $1;
4193a1ce18e4SJoe Perches			my $var = $2;
4194207a8e84SJoe Perches			$var = "" if (!defined $var);
4195207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4196a1ce18e4SJoe Perches				my $sign = $1;
4197a1ce18e4SJoe Perches				my $pointer = $2;
4198a1ce18e4SJoe Perches
4199a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
4200a1ce18e4SJoe Perches
4201a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
4202a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4203a1ce18e4SJoe Perches				    $fix) {
4204a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
4205207a8e84SJoe Perches					my $comp_pointer = $pointer;
4206207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
4207207a8e84SJoe Perches					$decl .= $comp_pointer;
4208207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
4209207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4210a1ce18e4SJoe Perches				}
4211a1ce18e4SJoe Perches			}
4212a1ce18e4SJoe Perches		}
4213a1ce18e4SJoe Perches
4214653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
42157429c690SAndy Whitcroft		if ($dbg_type) {
42167429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
4217000d1cc1SJoe Perches				ERROR("TEST_TYPE",
4218000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
42197429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4220000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
4221000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
42227429c690SAndy Whitcroft			}
4223653d4876SAndy Whitcroft			next;
4224653d4876SAndy Whitcroft		}
4225a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
4226a1ef277eSAndy Whitcroft		if ($dbg_attr) {
42279360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
4228000d1cc1SJoe Perches				ERROR("TEST_ATTR",
4229000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
42309360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4231000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
4232000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
4233a1ef277eSAndy Whitcroft			}
4234a1ef277eSAndy Whitcroft			next;
4235a1ef277eSAndy Whitcroft		}
4236653d4876SAndy Whitcroft
4237f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
423899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
423999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
4240d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4241d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4242f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4243f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4244f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4245d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4246d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4247f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4248d752fcc8SJoe Perches				$fixedline = $line;
42498d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4250f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4251d752fcc8SJoe Perches			}
4252f0a594c1SAndy Whitcroft		}
4253f0a594c1SAndy Whitcroft
425400df344fSAndy Whitcroft#
425500df344fSAndy Whitcroft# Checks which are anchored on the added line.
425600df344fSAndy Whitcroft#
425700df344fSAndy Whitcroft
4258653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4259c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4260653d4876SAndy Whitcroft			my $path = $1;
4261653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4262000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4263495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4264495e9d84SJoe Perches			}
4265495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4266495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4267495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4268653d4876SAndy Whitcroft			}
4269653d4876SAndy Whitcroft		}
4270653d4876SAndy Whitcroft
427100df344fSAndy Whitcroft# no C99 // comments
427200df344fSAndy Whitcroft		if ($line =~ m{//}) {
42733705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
42743705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
42753705ce5bSJoe Perches			    $fix) {
4276194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
42773705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
42783705ce5bSJoe Perches					my $comment = trim($1);
4279194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
42803705ce5bSJoe Perches				}
42813705ce5bSJoe Perches			}
428200df344fSAndy Whitcroft		}
428300df344fSAndy Whitcroft		# Remove C99 comments.
42840a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
42856c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
42860a920b5bSAndy Whitcroft
42872b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
42882b474a1aSAndy Whitcroft# the whole statement.
42892b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
42902b474a1aSAndy Whitcroft		if (defined $realline_next &&
42912b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
42922b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
42932b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
42942b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
42953cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
42963cbf62dfSAndy Whitcroft			# a prefix:
42973cbf62dfSAndy Whitcroft			#   XXX(foo);
42983cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4299653d4876SAndy Whitcroft			my $name = $1;
430087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
43013cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
43023cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
43033cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
43043cbf62dfSAndy Whitcroft
43053cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
43062b474a1aSAndy Whitcroft				\n.}\s*$|
430748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
430848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
430948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
43102b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
43112b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
431248012058SAndy Whitcroft			    )/x) {
43132b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
43142b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
43152b474a1aSAndy Whitcroft			} else {
43162b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
43170a920b5bSAndy Whitcroft			}
43180a920b5bSAndy Whitcroft		}
43192b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
43202b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
43212b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
43222b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
43232b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
43242b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
43252b474a1aSAndy Whitcroft		}
43262b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
43272b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4328000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4329000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
43302b474a1aSAndy Whitcroft		}
43310a920b5bSAndy Whitcroft
43325150bda4SJoe Eloff# check for global initialisers.
43336d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
4334d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
43356d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4336d5e616fcSJoe Perches			    $fix) {
43376d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4338d5e616fcSJoe Perches			}
4339f0a594c1SAndy Whitcroft		}
43400a920b5bSAndy Whitcroft# check for static initialisers.
43416d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4342d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
43436d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4344d5e616fcSJoe Perches				      $herecurr) &&
4345d5e616fcSJoe Perches			    $fix) {
43466d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4347d5e616fcSJoe Perches			}
43480a920b5bSAndy Whitcroft		}
43490a920b5bSAndy Whitcroft
43501813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
43511813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
43521813087dSJoe Perches			my $tmp = trim($1);
43531813087dSJoe Perches			WARN("MISORDERED_TYPE",
43541813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
43551813087dSJoe Perches		}
43561813087dSJoe Perches
4357809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4358809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4359809e082eSJoe Perches			my $type = trim($1);
4360809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4361809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4362809e082eSJoe Perches			my $new_type = $type;
4363809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4364809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4365809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4366809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4367809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4368809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4369809e082eSJoe Perches			$new_type = trim($new_type);
4370809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4371809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4372809e082eSJoe Perches			    $fix) {
4373809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4374809e082eSJoe Perches			}
4375809e082eSJoe Perches		}
4376809e082eSJoe Perches
4377cb710ecaSJoe Perches# check for static const char * arrays.
4378cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4379000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4380000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4381cb710ecaSJoe Perches				$herecurr);
4382cb710ecaSJoe Perches		}
4383cb710ecaSJoe Perches
438477b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
438577b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
438677b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
438777b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
438877b8c0a8SJoe Perches			    $fix) {
438977b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
439077b8c0a8SJoe Perches			}
439177b8c0a8SJoe Perches		}
439277b8c0a8SJoe Perches
4393cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4394cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4395000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4396000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4397cb710ecaSJoe Perches				$herecurr);
4398cb710ecaSJoe Perches		}
4399cb710ecaSJoe Perches
4400ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4401ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4402ab7e23f3SJoe Perches			my $found = $1;
4403ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4404ab7e23f3SJoe Perches				WARN("CONST_CONST",
4405ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4406ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4407ab7e23f3SJoe Perches				WARN("CONST_CONST",
4408ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4409ab7e23f3SJoe Perches			}
4410ab7e23f3SJoe Perches		}
4411ab7e23f3SJoe Perches
441273169765SJoe Perches# check for const static or static <non ptr type> const declarations
441373169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
441473169765SJoe Perches		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
441573169765SJoe Perches		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
441673169765SJoe Perches			if (WARN("STATIC_CONST",
441773169765SJoe Perches				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
441873169765SJoe Perches			    $fix) {
441973169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
442073169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
442173169765SJoe Perches			}
442273169765SJoe Perches		}
442373169765SJoe Perches
44249b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
44259b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
44269b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
44279b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
44289b0fa60dSJoe Perches				$herecurr);
44299b0fa60dSJoe Perches               }
44309b0fa60dSJoe Perches
4431b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4432b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4433b598b670SJoe Perches			my $array = $1;
4434b598b670SJoe Perches			if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4435b598b670SJoe Perches				my $array_div = $1;
4436b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4437b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4438b598b670SJoe Perches				    $fix) {
4439b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4440b598b670SJoe Perches				}
4441b598b670SJoe Perches			}
4442b598b670SJoe Perches		}
4443b598b670SJoe Perches
4444b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
444516b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4446b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4447b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4448b36190c5SJoe Perches			    $fix) {
4449194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4450b36190c5SJoe Perches			}
4451b36190c5SJoe Perches		}
4452b36190c5SJoe Perches
4453653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4454653d4876SAndy Whitcroft# make sense.
4455653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
44568054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4457c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
44588ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
445946d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4460000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4461000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
44620a920b5bSAndy Whitcroft		}
44630a920b5bSAndy Whitcroft
44640a920b5bSAndy Whitcroft# * goes on variable not on type
446565863862SAndy Whitcroft		# (char*[ const])
4466bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4467bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
44683705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4469d8aaf121SAndy Whitcroft
447065863862SAndy Whitcroft			# Should start with a space.
447165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
447265863862SAndy Whitcroft			# Should not end with a space.
447365863862SAndy Whitcroft			$to =~ s/\s+$//;
447465863862SAndy Whitcroft			# '*'s should not have spaces between.
4475f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
447665863862SAndy Whitcroft			}
4477d8aaf121SAndy Whitcroft
44783705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
447965863862SAndy Whitcroft			if ($from ne $to) {
44803705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
44813705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
44823705ce5bSJoe Perches				    $fix) {
44833705ce5bSJoe Perches					my $sub_from = $ident;
44843705ce5bSJoe Perches					my $sub_to = $ident;
44853705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4486194f66fcSJoe Perches					$fixed[$fixlinenr] =~
44873705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
44883705ce5bSJoe Perches				}
448965863862SAndy Whitcroft			}
4490bfcb2cc7SAndy Whitcroft		}
4491bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4492bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
44933705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4494d8aaf121SAndy Whitcroft
449565863862SAndy Whitcroft			# Should start with a space.
449665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
449765863862SAndy Whitcroft			# Should not end with a space.
449865863862SAndy Whitcroft			$to =~ s/\s+$//;
449965863862SAndy Whitcroft			# '*'s should not have spaces between.
4500f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
450165863862SAndy Whitcroft			}
450265863862SAndy Whitcroft			# Modifiers should have spaces.
450365863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
450465863862SAndy Whitcroft
45053705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4506667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
45073705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
45083705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
45093705ce5bSJoe Perches				    $fix) {
45103705ce5bSJoe Perches
45113705ce5bSJoe Perches					my $sub_from = $match;
45123705ce5bSJoe Perches					my $sub_to = $match;
45133705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4514194f66fcSJoe Perches					$fixed[$fixlinenr] =~
45153705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
45163705ce5bSJoe Perches				}
451765863862SAndy Whitcroft			}
45180a920b5bSAndy Whitcroft		}
45190a920b5bSAndy Whitcroft
45209d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
45219d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
45220675a8fbSJean Delvare			my $msg_level = \&WARN;
45230675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
45240675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
45259d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
45269d3e3c70SJoe Perches		}
45270a920b5bSAndy Whitcroft
45289d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
45298905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4530000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4531000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
45328905a67cSAndy Whitcroft		}
45338905a67cSAndy Whitcroft
453417441227SJoe Perches# check for uses of printk_ratelimit
453517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4536000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4537000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
453817441227SJoe Perches		}
453917441227SJoe Perches
4540eeef5733SJoe Perches# printk should use KERN_* levels
4541eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4542000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4543eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
454400df344fSAndy Whitcroft		}
45450a920b5bSAndy Whitcroft
4546243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4547243f3803SJoe Perches			my $orig = $1;
4548243f3803SJoe Perches			my $level = lc($orig);
4549243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
45508f26b837SJoe Perches			my $level2 = $level;
45518f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4552243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4553daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4554243f3803SJoe Perches		}
4555243f3803SJoe Perches
4556dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4557dc139313SJoe Perches			my $orig = $1;
4558dc139313SJoe Perches			my $level = lc($orig);
4559dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4560dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4561dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4562dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4563dc139313SJoe Perches		}
4564dc139313SJoe Perches
45658020b253SNicolas Boichat# trace_printk should not be used in production code.
45668020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
45678020b253SNicolas Boichat			WARN("TRACE_PRINTK",
45688020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
45698020b253SNicolas Boichat		}
45708020b253SNicolas Boichat
457191c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
457291c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
457391c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
457491c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
457591c9afafSAndy Lutomirski			WARN("ENOSYS",
457691c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
457791c9afafSAndy Lutomirski		}
457891c9afafSAndy Lutomirski
45796b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
45806b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
45816b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
45826b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
45836b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
45846b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
45856b9ea5ffSJakub Kicinski			    $fix) {
45866b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
45876b9ea5ffSJakub Kicinski			}
45886b9ea5ffSJakub Kicinski		}
45896b9ea5ffSJakub Kicinski
4590653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4591653d4876SAndy Whitcroft# or if closed on same line
45925b57980dSJoe Perches		if ($perl_version_ok &&
45932d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
45942d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
45952d453e3bSJoe Perches		    $sline !~ /}/) {
45968d182478SJoe Perches			if (ERROR("OPEN_BRACE",
45972d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
45988d182478SJoe Perches			    $fix) {
45998d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
46008d182478SJoe Perches				my $fixed_line = $rawline;
460103f49351SDwaipayan Ray				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
46028d182478SJoe Perches				my $line1 = $1;
46038d182478SJoe Perches				my $line2 = $2;
46048d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
46058d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
46068d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
46078d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
46088d182478SJoe Perches				}
46098d182478SJoe Perches			}
46100a920b5bSAndy Whitcroft		}
4611653d4876SAndy Whitcroft
46128905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
46138905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
46148905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
46158d182478SJoe Perches			if (ERROR("OPEN_BRACE",
46168d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
46178d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
46188d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
46198d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
46208d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
46218d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
46228d182478SJoe Perches				$fixedline = $rawline;
46238d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
46248d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
46258d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46268d182478SJoe Perches				}
46278d182478SJoe Perches			}
46288905a67cSAndy Whitcroft		}
46298905a67cSAndy Whitcroft
46300c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
46313705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
46323705ce5bSJoe Perches			if (WARN("SPACING",
46333705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
46343705ce5bSJoe Perches			    $fix) {
4635194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46363705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
46373705ce5bSJoe Perches			}
46380c73b4ebSAndy Whitcroft		}
46390c73b4ebSAndy Whitcroft
464031070b5dSJoe Perches# Function pointer declarations
464131070b5dSJoe Perches# check spacing between type, funcptr, and args
464231070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
464391f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
464431070b5dSJoe Perches			my $declare = $1;
464531070b5dSJoe Perches			my $pre_pointer_space = $2;
464631070b5dSJoe Perches			my $post_pointer_space = $3;
464731070b5dSJoe Perches			my $funcname = $4;
464831070b5dSJoe Perches			my $post_funcname_space = $5;
464931070b5dSJoe Perches			my $pre_args_space = $6;
465031070b5dSJoe Perches
465191f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
465291f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
465391f72e9cSJoe Perches# don't need a space so don't warn for those.
465491f72e9cSJoe Perches			my $post_declare_space = "";
465591f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
465691f72e9cSJoe Perches				$post_declare_space = $1;
465791f72e9cSJoe Perches				$declare = rtrim($declare);
465891f72e9cSJoe Perches			}
465991f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
466031070b5dSJoe Perches				WARN("SPACING",
466131070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
466291f72e9cSJoe Perches				$post_declare_space = " ";
466331070b5dSJoe Perches			}
466431070b5dSJoe Perches
466531070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
466691f72e9cSJoe Perches# This test is not currently implemented because these declarations are
466791f72e9cSJoe Perches# equivalent to
466891f72e9cSJoe Perches#	int  foo(int bar, ...)
466991f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
467091f72e9cSJoe Perches#
467191f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
467291f72e9cSJoe Perches#				WARN("SPACING",
467391f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
467491f72e9cSJoe Perches#			}
467531070b5dSJoe Perches
467631070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
467731070b5dSJoe Perches			if (defined $pre_pointer_space &&
467831070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
467931070b5dSJoe Perches				WARN("SPACING",
468031070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
468131070b5dSJoe Perches			}
468231070b5dSJoe Perches
468331070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
468431070b5dSJoe Perches			if (defined $post_pointer_space &&
468531070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
468631070b5dSJoe Perches				WARN("SPACING",
468731070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
468831070b5dSJoe Perches			}
468931070b5dSJoe Perches
469031070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
469131070b5dSJoe Perches			if (defined $post_funcname_space &&
469231070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
469331070b5dSJoe Perches				WARN("SPACING",
469431070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
469531070b5dSJoe Perches			}
469631070b5dSJoe Perches
469731070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
469831070b5dSJoe Perches			if (defined $pre_args_space &&
469931070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
470031070b5dSJoe Perches				WARN("SPACING",
470131070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
470231070b5dSJoe Perches			}
470331070b5dSJoe Perches
470431070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4705194f66fcSJoe Perches				$fixed[$fixlinenr] =~
470691f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
470731070b5dSJoe Perches			}
470831070b5dSJoe Perches		}
470931070b5dSJoe Perches
47108d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
47118d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4712fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4713fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
47148d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
47158d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
47168d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4717fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
471838dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
47193705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
47203705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
47213705ce5bSJoe Perches				    $fix) {
4722194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
47233705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
47243705ce5bSJoe Perches				}
47258d31cfceSAndy Whitcroft			}
47268d31cfceSAndy Whitcroft		}
47278d31cfceSAndy Whitcroft
4728f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
47296c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4730c2fdda0dSAndy Whitcroft			my $name = $1;
4731773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4732773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4733c2fdda0dSAndy Whitcroft
4734c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4735773647a0SAndy Whitcroft			if ($name =~ /^(?:
4736773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4737773647a0SAndy Whitcroft				volatile|__volatile__|
4738773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4739773647a0SAndy Whitcroft				asm|__asm__)$/x)
4740773647a0SAndy Whitcroft			{
4741c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4742c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4743c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4744c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4745773647a0SAndy Whitcroft
4746773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4747c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4748c2fdda0dSAndy Whitcroft
4749c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4750c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4751773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4752c2fdda0dSAndy Whitcroft
4753c2fdda0dSAndy Whitcroft			} else {
47543705ce5bSJoe Perches				if (WARN("SPACING",
47553705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
47563705ce5bSJoe Perches					     $fix) {
4757194f66fcSJoe Perches					$fixed[$fixlinenr] =~
47583705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
47593705ce5bSJoe Perches				}
4760f0a594c1SAndy Whitcroft			}
47616c72ffaaSAndy Whitcroft		}
47629a4cad4eSEric Nelson
4763653d4876SAndy Whitcroft# Check operator spacing.
47640a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
47653705ce5bSJoe Perches			my $fixed_line = "";
47663705ce5bSJoe Perches			my $line_fixed = 0;
47673705ce5bSJoe Perches
47689c0ca6f9SAndy Whitcroft			my $ops = qr{
47699c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
47709c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
47719c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
47721f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
477384731623SJoe Perches				\?:|\?|:
47749c0ca6f9SAndy Whitcroft			}x;
4775cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
47763705ce5bSJoe Perches
47773705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
47783705ce5bSJoe Perches##			foreach my $el (@elements) {
47793705ce5bSJoe Perches##				print("el: <$el>\n");
47803705ce5bSJoe Perches##			}
47813705ce5bSJoe Perches
47823705ce5bSJoe Perches			my @fix_elements = ();
478300df344fSAndy Whitcroft			my $off = 0;
47846c72ffaaSAndy Whitcroft
47853705ce5bSJoe Perches			foreach my $el (@elements) {
47863705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
47873705ce5bSJoe Perches				$off += length($el);
47883705ce5bSJoe Perches			}
47893705ce5bSJoe Perches
47903705ce5bSJoe Perches			$off = 0;
47913705ce5bSJoe Perches
47926c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4793b34c648bSJoe Perches			my $last_after = -1;
47946c72ffaaSAndy Whitcroft
47950a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
47963705ce5bSJoe Perches
47973705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
47983705ce5bSJoe Perches
47993705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
48003705ce5bSJoe Perches
48014a0df2efSAndy Whitcroft				$off += length($elements[$n]);
48024a0df2efSAndy Whitcroft
480325985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4804773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4805773647a0SAndy Whitcroft				my $cc = '';
4806773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4807773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4808773647a0SAndy Whitcroft				}
4809773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4810773647a0SAndy Whitcroft
48114a0df2efSAndy Whitcroft				my $a = '';
48124a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
48134a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4814cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
48154a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
48164a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4817773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
48184a0df2efSAndy Whitcroft
48190a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
48204a0df2efSAndy Whitcroft
48214a0df2efSAndy Whitcroft				my $c = '';
48220a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
48234a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
48244a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4825cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
48264a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
48274a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
48288b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
48294a0df2efSAndy Whitcroft				} else {
48304a0df2efSAndy Whitcroft					$c = 'E';
48310a920b5bSAndy Whitcroft				}
48320a920b5bSAndy Whitcroft
48334a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
48344a0df2efSAndy Whitcroft
48354a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
48364a0df2efSAndy Whitcroft
48376c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4838de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
48390a920b5bSAndy Whitcroft
484074048ed8SAndy Whitcroft				# Pull out the value of this operator.
48416c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
48420a920b5bSAndy Whitcroft
48431f65f947SAndy Whitcroft				# Get the full operator variant.
48441f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
48451f65f947SAndy Whitcroft
484613214adfSAndy Whitcroft				# Ignore operators passed as parameters.
484713214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4848d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
484913214adfSAndy Whitcroft
4850cf655043SAndy Whitcroft#				# Ignore comments
4851cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
485213214adfSAndy Whitcroft
4853d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
485413214adfSAndy Whitcroft				} elsif ($op eq ';') {
4855cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4856cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
48573705ce5bSJoe Perches						if (ERROR("SPACING",
48583705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4859b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
48603705ce5bSJoe Perches							$line_fixed = 1;
48613705ce5bSJoe Perches						}
4862d8aaf121SAndy Whitcroft					}
4863d8aaf121SAndy Whitcroft
4864d8aaf121SAndy Whitcroft				# // is a comment
4865d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
48660a920b5bSAndy Whitcroft
4867b00e4814SJoe Perches				#   :   when part of a bitfield
4868b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4869b00e4814SJoe Perches					# skip the bitfield test for now
4870b00e4814SJoe Perches
48711f65f947SAndy Whitcroft				# No spaces for:
48721f65f947SAndy Whitcroft				#   ->
4873b00e4814SJoe Perches				} elsif ($op eq '->') {
48744a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
48753705ce5bSJoe Perches						if (ERROR("SPACING",
48763705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4877b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
48783705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
48793705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
48803705ce5bSJoe Perches							}
4881b34c648bSJoe Perches							$line_fixed = 1;
48823705ce5bSJoe Perches						}
48830a920b5bSAndy Whitcroft					}
48840a920b5bSAndy Whitcroft
48852381097bSJoe Perches				# , must not have a space before and must have a space on the right.
48860a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
48872381097bSJoe Perches					my $rtrim_before = 0;
48882381097bSJoe Perches					my $space_after = 0;
48892381097bSJoe Perches					if ($ctx =~ /Wx./) {
48902381097bSJoe Perches						if (ERROR("SPACING",
48912381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
48922381097bSJoe Perches							$line_fixed = 1;
48932381097bSJoe Perches							$rtrim_before = 1;
48942381097bSJoe Perches						}
48952381097bSJoe Perches					}
4896cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
48973705ce5bSJoe Perches						if (ERROR("SPACING",
48983705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
48993705ce5bSJoe Perches							$line_fixed = 1;
4900b34c648bSJoe Perches							$last_after = $n;
49012381097bSJoe Perches							$space_after = 1;
49022381097bSJoe Perches						}
49032381097bSJoe Perches					}
49042381097bSJoe Perches					if ($rtrim_before || $space_after) {
49052381097bSJoe Perches						if ($rtrim_before) {
49062381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
49072381097bSJoe Perches						} else {
49082381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
49092381097bSJoe Perches						}
49102381097bSJoe Perches						if ($space_after) {
49112381097bSJoe Perches							$good .= " ";
49123705ce5bSJoe Perches						}
49130a920b5bSAndy Whitcroft					}
49140a920b5bSAndy Whitcroft
49159c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
491674048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
49179c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
49189c0ca6f9SAndy Whitcroft
49199c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
49209c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
49219c0ca6f9SAndy Whitcroft				# unary operator, or a cast
49229c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
492374048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
49240d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4925cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
49263705ce5bSJoe Perches						if (ERROR("SPACING",
49273705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4928b34c648bSJoe Perches							if ($n != $last_after + 2) {
4929b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
49303705ce5bSJoe Perches								$line_fixed = 1;
49313705ce5bSJoe Perches							}
49320a920b5bSAndy Whitcroft						}
4933b34c648bSJoe Perches					}
4934a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4935171ae1a4SAndy Whitcroft						# A unary '*' may be const
4936171ae1a4SAndy Whitcroft
4937171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
49383705ce5bSJoe Perches						if (ERROR("SPACING",
49393705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4940b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
49413705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
49423705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
49433705ce5bSJoe Perches							}
4944b34c648bSJoe Perches							$line_fixed = 1;
49453705ce5bSJoe Perches						}
49460a920b5bSAndy Whitcroft					}
49470a920b5bSAndy Whitcroft
49480a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
49490a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4950773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
49513705ce5bSJoe Perches						if (ERROR("SPACING",
49523705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4953b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
49543705ce5bSJoe Perches							$line_fixed = 1;
49553705ce5bSJoe Perches						}
49560a920b5bSAndy Whitcroft					}
4957773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4958773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
49593705ce5bSJoe Perches						if (ERROR("SPACING",
49603705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4961b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
49623705ce5bSJoe Perches							$line_fixed = 1;
49633705ce5bSJoe Perches						}
4964653d4876SAndy Whitcroft					}
4965773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
49663705ce5bSJoe Perches						if (ERROR("SPACING",
49673705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4968b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
49693705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
49703705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4971773647a0SAndy Whitcroft							}
4972b34c648bSJoe Perches							$line_fixed = 1;
49733705ce5bSJoe Perches						}
49743705ce5bSJoe Perches					}
49750a920b5bSAndy Whitcroft
49760a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
49779c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
49789c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
49799c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4980c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4981c2fdda0dSAndy Whitcroft					 $op eq '%')
49820a920b5bSAndy Whitcroft				{
4983d2e025f3SJoe Perches					if ($check) {
4984d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4985d2e025f3SJoe Perches							if (CHK("SPACING",
4986d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4987d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4988d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4989d2e025f3SJoe Perches								$line_fixed = 1;
4990d2e025f3SJoe Perches							}
4991d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4992d2e025f3SJoe Perches							if (CHK("SPACING",
4993d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4994d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4995d2e025f3SJoe Perches								$line_fixed = 1;
4996d2e025f3SJoe Perches							}
4997d2e025f3SJoe Perches						}
4998d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
49993705ce5bSJoe Perches						if (ERROR("SPACING",
50003705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
5001b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5002b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5003b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5004b34c648bSJoe Perches							}
50053705ce5bSJoe Perches							$line_fixed = 1;
50063705ce5bSJoe Perches						}
50070a920b5bSAndy Whitcroft					}
50080a920b5bSAndy Whitcroft
50091f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
50101f65f947SAndy Whitcroft				# terminating a case value or a label.
50111f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
50121f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
50133705ce5bSJoe Perches						if (ERROR("SPACING",
50143705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5015b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
50163705ce5bSJoe Perches							$line_fixed = 1;
50173705ce5bSJoe Perches						}
50181f65f947SAndy Whitcroft					}
50191f65f947SAndy Whitcroft
50200a920b5bSAndy Whitcroft				# All the others need spaces both sides.
5021cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
50221f65f947SAndy Whitcroft					my $ok = 0;
50231f65f947SAndy Whitcroft
502422f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
50251f65f947SAndy Whitcroft					if (($op eq '<' &&
50261f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
50271f65f947SAndy Whitcroft					    ($op eq '>' &&
50281f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
50291f65f947SAndy Whitcroft					{
50301f65f947SAndy Whitcroft						$ok = 1;
50311f65f947SAndy Whitcroft					}
50321f65f947SAndy Whitcroft
5033e0df7e1fSJoe Perches					# for asm volatile statements
5034e0df7e1fSJoe Perches					# ignore a colon with another
5035e0df7e1fSJoe Perches					# colon immediately before or after
5036e0df7e1fSJoe Perches					if (($op eq ':') &&
5037e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
5038e0df7e1fSJoe Perches						$ok = 1;
5039e0df7e1fSJoe Perches					}
5040e0df7e1fSJoe Perches
504184731623SJoe Perches					# messages are ERROR, but ?: are CHK
50421f65f947SAndy Whitcroft					if ($ok == 0) {
50430675a8fbSJean Delvare						my $msg_level = \&ERROR;
50440675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
504584731623SJoe Perches
50460675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
50473705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
5048b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5049b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5050b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5051b34c648bSJoe Perches							}
50523705ce5bSJoe Perches							$line_fixed = 1;
50533705ce5bSJoe Perches						}
50540a920b5bSAndy Whitcroft					}
505522f2a2efSAndy Whitcroft				}
50564a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
50573705ce5bSJoe Perches
50583705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
50593705ce5bSJoe Perches
50603705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
50610a920b5bSAndy Whitcroft			}
50623705ce5bSJoe Perches
50633705ce5bSJoe Perches			if (($#elements % 2) == 0) {
50643705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
50653705ce5bSJoe Perches			}
50663705ce5bSJoe Perches
5067194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5068194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
50693705ce5bSJoe Perches			}
50703705ce5bSJoe Perches
50713705ce5bSJoe Perches
50720a920b5bSAndy Whitcroft		}
50730a920b5bSAndy Whitcroft
5074786b6326SJoe Perches# check for whitespace before a non-naked semicolon
5075d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
5076786b6326SJoe Perches			if (WARN("SPACING",
5077786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
5078786b6326SJoe Perches			    $fix) {
5079194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
5080786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
5081786b6326SJoe Perches			}
5082786b6326SJoe Perches		}
5083786b6326SJoe Perches
5084f0a594c1SAndy Whitcroft# check for multiple assignments
5085f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5086000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
5087000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
5088f0a594c1SAndy Whitcroft		}
5089f0a594c1SAndy Whitcroft
509022f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
509122f2a2efSAndy Whitcroft## # continuation.
509222f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
509322f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
509422f2a2efSAndy Whitcroft##
509522f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
5096e73d2715SDwaipayan Ray## 			# falsely report the parameters of functions.
509722f2a2efSAndy Whitcroft## 			my $ln = $line;
509822f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
509922f2a2efSAndy Whitcroft## 			}
510022f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
5101000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
5102000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
510322f2a2efSAndy Whitcroft## 			}
510422f2a2efSAndy Whitcroft## 		}
5105f0a594c1SAndy Whitcroft
51060a920b5bSAndy Whitcroft#need space before brace following if, while, etc
51076b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
51086ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
51093705ce5bSJoe Perches			if (ERROR("SPACING",
51103705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
51113705ce5bSJoe Perches			    $fix) {
51126ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
51133705ce5bSJoe Perches			}
5114de7d4f0eSAndy Whitcroft		}
5115de7d4f0eSAndy Whitcroft
5116c4a62ef9SJoe Perches## # check for blank lines before declarations
5117c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5118c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
5119c4a62ef9SJoe Perches##			WARN("SPACING",
5120c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
5121c4a62ef9SJoe Perches##		}
5122c4a62ef9SJoe Perches##
5123c4a62ef9SJoe Perches
5124de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
5125de7d4f0eSAndy Whitcroft# on the line
512694fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5127d5e616fcSJoe Perches			if (ERROR("SPACING",
5128d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
5129d5e616fcSJoe Perches			    $fix) {
5130194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5131d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
5132d5e616fcSJoe Perches			}
51330a920b5bSAndy Whitcroft		}
51340a920b5bSAndy Whitcroft
513522f2a2efSAndy Whitcroft# check spacing on square brackets
513622f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
51373705ce5bSJoe Perches			if (ERROR("SPACING",
51383705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
51393705ce5bSJoe Perches			    $fix) {
5140194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51413705ce5bSJoe Perches				    s/\[\s+/\[/;
51423705ce5bSJoe Perches			}
514322f2a2efSAndy Whitcroft		}
514422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
51453705ce5bSJoe Perches			if (ERROR("SPACING",
51463705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
51473705ce5bSJoe Perches			    $fix) {
5148194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51493705ce5bSJoe Perches				    s/\s+\]/\]/;
51503705ce5bSJoe Perches			}
515122f2a2efSAndy Whitcroft		}
515222f2a2efSAndy Whitcroft
5153c45dcabdSAndy Whitcroft# check spacing on parentheses
51549c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
51559c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
51563705ce5bSJoe Perches			if (ERROR("SPACING",
51573705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
51583705ce5bSJoe Perches			    $fix) {
5159194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51603705ce5bSJoe Perches				    s/\(\s+/\(/;
51613705ce5bSJoe Perches			}
516222f2a2efSAndy Whitcroft		}
516313214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5164c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
5165c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
51663705ce5bSJoe Perches			if (ERROR("SPACING",
51673705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
51683705ce5bSJoe Perches			    $fix) {
5169194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51703705ce5bSJoe Perches				    s/\s+\)/\)/;
51713705ce5bSJoe Perches			}
517222f2a2efSAndy Whitcroft		}
517322f2a2efSAndy Whitcroft
5174e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
5175e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5176e2826fd0SJoe Perches
5177e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5178ea4acbb1SJoe Perches			my $var = $1;
5179ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5180ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
5181ea4acbb1SJoe Perches			    $fix) {
5182ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5183ea4acbb1SJoe Perches			}
5184ea4acbb1SJoe Perches		}
5185ea4acbb1SJoe Perches
5186ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
5187ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
5188ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
5189ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5190ea4acbb1SJoe Perches			my $var = $2;
5191ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5192ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5193ea4acbb1SJoe Perches			    $fix) {
5194ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
5195ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
5196ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5197ea4acbb1SJoe Perches			}
5198e2826fd0SJoe Perches		}
5199e2826fd0SJoe Perches
520063b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
5201a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
5202a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
52035b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
520463b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
520563b7c73eSJoe Perches			my $if_stat = $1;
520663b7c73eSJoe Perches			my $test = substr($2, 1, -1);
520763b7c73eSJoe Perches			my $herectx;
520863b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
520963b7c73eSJoe Perches				my $match = $1;
521063b7c73eSJoe Perches				# avoid parentheses around potential macro args
521163b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
521263b7c73eSJoe Perches				if (!defined($herectx)) {
521363b7c73eSJoe Perches					$herectx = $here . "\n";
521463b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
521563b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
521663b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
521763b7c73eSJoe Perches						$herectx .=  $rl . "\n";
521863b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
521963b7c73eSJoe Perches					}
522063b7c73eSJoe Perches				}
522163b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
522263b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
522363b7c73eSJoe Perches			}
522463b7c73eSJoe Perches		}
522563b7c73eSJoe Perches
52260a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
52274a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
52280a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
52293705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
52303705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
52313705ce5bSJoe Perches			    $fix) {
5232194f66fcSJoe Perches				$fixed[$fixlinenr] =~
52333705ce5bSJoe Perches				    s/^(.)\s+/$1/;
52343705ce5bSJoe Perches			}
52350a920b5bSAndy Whitcroft		}
52360a920b5bSAndy Whitcroft
523740873abaSJoe Perches# check if a statement with a comma should be two statements like:
523840873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
523940873abaSJoe Perches#	bar = baz();
524040873abaSJoe Perches		if (defined($stat) &&
524140873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
524240873abaSJoe Perches			my $cnt = statement_rawlines($stat);
524340873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
524440873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
524540873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
524640873abaSJoe Perches		}
524740873abaSJoe Perches
52485b9553abSJoe Perches# return is not a function
5249507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5250c45dcabdSAndy Whitcroft			my $spacing = $1;
52515b57980dSJoe Perches			if ($perl_version_ok &&
52525b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
52535b9553abSJoe Perches				my $value = $1;
52545b9553abSJoe Perches				$value = deparenthesize($value);
52555b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5256000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5257000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
52585b9553abSJoe Perches				}
5259c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5260000d1cc1SJoe Perches				ERROR("SPACING",
5261000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5262c45dcabdSAndy Whitcroft			}
5263c45dcabdSAndy Whitcroft		}
5264507e5141SJoe Perches
5265b43ae21bSJoe Perches# unnecessary return in a void function
5266b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5267b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5268b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5269b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5270b43ae21bSJoe Perches		    $linenr >= 3 &&
5271b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5272b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
52739819cf25SJoe Perches			WARN("RETURN_VOID",
5274b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
52759819cf25SJoe Perches               }
52769819cf25SJoe Perches
5277189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
52785b57980dSJoe Perches		if ($perl_version_ok &&
5279189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5280189248d8SJoe Perches			my $openparens = $1;
5281189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5282189248d8SJoe Perches			my $msg = "";
5283189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5284189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5285189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5286189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5287189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5288189248d8SJoe Perches			}
5289189248d8SJoe Perches		}
5290189248d8SJoe Perches
5291c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5292c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5293c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5294c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
52955b57980dSJoe Perches		if ($perl_version_ok &&
5296c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5297c5595fa2SJoe Perches			my $lead = $1;
5298c5595fa2SJoe Perches			my $const = $2;
5299c5595fa2SJoe Perches			my $comp = $3;
5300c5595fa2SJoe Perches			my $to = $4;
5301c5595fa2SJoe Perches			my $newcomp = $comp;
5302f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5303c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5304c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5305c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5306c5595fa2SJoe Perches			    $fix) {
5307c5595fa2SJoe Perches				if ($comp eq "<") {
5308c5595fa2SJoe Perches					$newcomp = ">";
5309c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5310c5595fa2SJoe Perches					$newcomp = ">=";
5311c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5312c5595fa2SJoe Perches					$newcomp = "<";
5313c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5314c5595fa2SJoe Perches					$newcomp = "<=";
5315c5595fa2SJoe Perches				}
5316c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5317c5595fa2SJoe Perches			}
5318c5595fa2SJoe Perches		}
5319c5595fa2SJoe Perches
5320f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5321f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
532253a3c448SAndy Whitcroft			my $name = $1;
532353a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
5324000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5325f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
532653a3c448SAndy Whitcroft			}
532753a3c448SAndy Whitcroft		}
5328c45dcabdSAndy Whitcroft
53290a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
53304a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
53313705ce5bSJoe Perches			if (ERROR("SPACING",
53323705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
53333705ce5bSJoe Perches			    $fix) {
5334194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53353705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
53363705ce5bSJoe Perches			}
53370a920b5bSAndy Whitcroft		}
53380a920b5bSAndy Whitcroft
5339f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5340f5fe35ddSAndy Whitcroft# statements after the conditional.
5341170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
53423e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
53433e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
53443e469cdcSAndy Whitcroft					if (!defined $stat);
5345170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5346170d3a22SAndy Whitcroft						$remain_next, $off_next);
5347170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5348170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5349170d3a22SAndy Whitcroft
5350170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5351170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5352170d3a22SAndy Whitcroft				# then count those as offsets.
5353170d3a22SAndy Whitcroft				my ($whitespace) =
5354170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5355170d3a22SAndy Whitcroft				my $offset =
5356170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5357170d3a22SAndy Whitcroft
5358170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5359170d3a22SAndy Whitcroft								$offset} = 1;
5360170d3a22SAndy Whitcroft			}
5361170d3a22SAndy Whitcroft		}
5362170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5363c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5364170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5365171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
53668905a67cSAndy Whitcroft
5367b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
536865b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
536965b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
537065b64b3bSJoe Perches				    $fix && $perl_version_ok) {
537165b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
537265b64b3bSJoe Perches						my $space = $1;
537365b64b3bSJoe Perches						my $not = $2;
537465b64b3bSJoe Perches						my $statement = $3;
537565b64b3bSJoe Perches						my $assigned = $4;
537665b64b3bSJoe Perches						my $test = $8;
537765b64b3bSJoe Perches						my $against = $9;
537865b64b3bSJoe Perches						my $brace = $15;
537965b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
538065b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
538165b64b3bSJoe Perches						my $newline = "${space}if (";
538265b64b3bSJoe Perches						$newline .= '!' if defined($not);
538365b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
538465b64b3bSJoe Perches						$newline .= "$assigned";
538565b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
538665b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
538765b64b3bSJoe Perches						$newline .= ')';
538865b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
538965b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
539065b64b3bSJoe Perches					}
539165b64b3bSJoe Perches				}
53928905a67cSAndy Whitcroft			}
53938905a67cSAndy Whitcroft
53948905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
53958905a67cSAndy Whitcroft			# conditional.
5396773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
53978905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
539813214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
539953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
540053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5401773647a0SAndy Whitcroft			{
5402bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5403bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5404bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
540542bdf74cSHidetoshi Seto				my $stat_real = '';
5406bb44ad39SAndy Whitcroft
540742bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
540842bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5409bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5410bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5411bb44ad39SAndy Whitcroft				}
5412bb44ad39SAndy Whitcroft
5413000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5414000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
54158905a67cSAndy Whitcroft			}
54168905a67cSAndy Whitcroft		}
54178905a67cSAndy Whitcroft
541813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
541913214adfSAndy Whitcroft		if ($line =~ /
542013214adfSAndy Whitcroft			(?:
542113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
542213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
542313214adfSAndy Whitcroft				(?:\&\&|\|\|)
542413214adfSAndy Whitcroft			|
542513214adfSAndy Whitcroft				(?:\&\&|\|\|)
542613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
542713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
542813214adfSAndy Whitcroft			)/x)
542913214adfSAndy Whitcroft		{
5430000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5431000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
543213214adfSAndy Whitcroft		}
543313214adfSAndy Whitcroft
54348905a67cSAndy Whitcroft# if and else should not have general statements after it
543513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
543613214adfSAndy Whitcroft			my $s = $1;
543713214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
543813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5439000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5440000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
54410a920b5bSAndy Whitcroft			}
544213214adfSAndy Whitcroft		}
544339667782SAndy Whitcroft# if should not continue a brace
544439667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5445000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5446048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
544739667782SAndy Whitcroft				$herecurr);
544839667782SAndy Whitcroft		}
5449a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5450a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5451a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
54523fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5453a1080bf8SAndy Whitcroft			\s*return\s+
5454a1080bf8SAndy Whitcroft		    )/xg)
5455a1080bf8SAndy Whitcroft		{
5456000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5457000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5458a1080bf8SAndy Whitcroft		}
54590a920b5bSAndy Whitcroft
54600a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
54610a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
54628b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
54630a920b5bSAndy Whitcroft		    $previndent == $indent) {
54648b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
54658b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
54668b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
54678b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
54688b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
54698b8856f4SJoe Perches				my $fixedline = $prevrawline;
54708b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
54718b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
54728b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54738b8856f4SJoe Perches				}
54748b8856f4SJoe Perches				$fixedline = $rawline;
54758b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
54768b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
54778b8856f4SJoe Perches			}
54780a920b5bSAndy Whitcroft		}
54790a920b5bSAndy Whitcroft
54808b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5481c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5482c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5483c2fdda0dSAndy Whitcroft
5484c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5485c2fdda0dSAndy Whitcroft			# conditional.
5486773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5487c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5488c2fdda0dSAndy Whitcroft
5489c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
54908b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
54918b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
54928b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
54938b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
54948b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
54958b8856f4SJoe Perches					my $fixedline = $prevrawline;
54968b8856f4SJoe Perches					my $trailing = $rawline;
54978b8856f4SJoe Perches					$trailing =~ s/^\+//;
54988b8856f4SJoe Perches					$trailing = trim($trailing);
54998b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
55008b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
55018b8856f4SJoe Perches				}
5502c2fdda0dSAndy Whitcroft			}
5503c2fdda0dSAndy Whitcroft		}
5504c2fdda0dSAndy Whitcroft
550595e2c602SJoe Perches#Specific variable tests
5506323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5507323c1260SJoe Perches			my $var = $1;
550895e2c602SJoe Perches
550995e2c602SJoe Perches#CamelCase
5510807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5511be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
55124104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values
55134104a206SŁukasz Stelmach			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
551422735ce8SJoe Perches#Ignore Page<foo> variants
5515807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5516d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5517d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5518d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5519f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5520f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
55217e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
55227e781f67SJoe Perches					my $word = $1;
55237e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5524d8b07710SJoe Perches					if ($check) {
5525d8b07710SJoe Perches						seed_camelcase_includes();
5526d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5527d8b07710SJoe Perches							seed_camelcase_file($realfile);
5528d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5529d8b07710SJoe Perches						}
5530d8b07710SJoe Perches					}
55317e781f67SJoe Perches					if (!defined $camelcase{$word}) {
55327e781f67SJoe Perches						$camelcase{$word} = 1;
5533be79794bSJoe Perches						CHK("CAMELCASE",
55347e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
55357e781f67SJoe Perches					}
5536323c1260SJoe Perches				}
5537323c1260SJoe Perches			}
55383445686aSJoe Perches		}
55390a920b5bSAndy Whitcroft
55400a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5541d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5542d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5543d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5544d5e616fcSJoe Perches			    $fix) {
5545194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5546d5e616fcSJoe Perches			}
55470a920b5bSAndy Whitcroft		}
55480a920b5bSAndy Whitcroft
55490e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
55500e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5551c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5552e09dec48SAndy Whitcroft			my $file = "$1.h";
5553e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5554e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5555e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
55567840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5557c45dcabdSAndy Whitcroft			{
55580e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
55590e212e0aSFabian Frederick				if ($asminclude > 0) {
5560e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5561000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5562000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5563e09dec48SAndy Whitcroft					} else {
5564000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5565000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5566e09dec48SAndy Whitcroft					}
55670a920b5bSAndy Whitcroft				}
55680a920b5bSAndy Whitcroft			}
55690e212e0aSFabian Frederick		}
55700a920b5bSAndy Whitcroft
5571653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5572653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5573cf655043SAndy Whitcroft# in a known good container
5574b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5575b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5576d8aaf121SAndy Whitcroft			my $ln = $linenr;
5577d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5578c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5579c45dcabdSAndy Whitcroft			my $ctx = '';
558008a2843eSJoe Perches			my $has_flow_statement = 0;
558108a2843eSJoe Perches			my $has_arg_concat = 0;
5582c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5583f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5584f74bd194SAndy Whitcroft			$ctx = $dstat;
5585c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5586a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5587c45dcabdSAndy Whitcroft
558808a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
558962e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
559008a2843eSJoe Perches
5591f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5592f59b64bfSJoe Perches			my $define_args = $1;
5593f59b64bfSJoe Perches			my $define_stmt = $dstat;
5594f59b64bfSJoe Perches			my @def_args = ();
5595f59b64bfSJoe Perches
5596f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5597f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5598f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
55998c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5600f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5601f59b64bfSJoe Perches			}
5602f59b64bfSJoe Perches
5603292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5604c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5605c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5606c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5607c45dcabdSAndy Whitcroft
5608c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
56092e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
56102e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
56112e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5612bf30d6edSAndy Whitcroft			{
5613c45dcabdSAndy Whitcroft			}
5614c45dcabdSAndy Whitcroft
5615342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
561633acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
561733acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5618e45bab8eSAndy Whitcroft			{
5619e45bab8eSAndy Whitcroft			}
5620e45bab8eSAndy Whitcroft
562142e15293SJoe Perches			# Make asm volatile uses seem like a generic function
562242e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
562342e15293SJoe Perches
5624c45dcabdSAndy Whitcroft			my $exceptions = qr{
5625c45dcabdSAndy Whitcroft				$Declare|
5626c45dcabdSAndy Whitcroft				module_param_named|
5627a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5628c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5629c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5630383099fdSAndy Whitcroft				__typeof__\(|
563122fd2d3eSStefani Seibold				union|
563222fd2d3eSStefani Seibold				struct|
5633ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
56346b10df42SVladimir Zapolskiy				^\"|\"$|
56356b10df42SVladimir Zapolskiy				^\[
5636c45dcabdSAndy Whitcroft			}x;
56375eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5638f59b64bfSJoe Perches
5639f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5640f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5641e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5642f59b64bfSJoe Perches
5643f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5644f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5645f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
56463cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5647356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5648f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5649f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5650e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
565172f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
56522e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5653f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5654f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5655f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
56564e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5657f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5658c45dcabdSAndy Whitcroft			{
5659e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5660e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5661e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5662e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5663f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5664f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5665f74bd194SAndy Whitcroft				} else {
5666000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5667388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5668d8aaf121SAndy Whitcroft				}
5669f59b64bfSJoe Perches
5670f59b64bfSJoe Perches			}
56715207649bSJoe Perches
56725207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
56735207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
56745207649bSJoe Perches			my $first = 1;
56755207649bSJoe Perches			$define_stmt = "";
56765207649bSJoe Perches			foreach my $l (@stmt_array) {
56775207649bSJoe Perches				$l =~ s/\\$//;
56785207649bSJoe Perches				if ($first) {
56795207649bSJoe Perches					$define_stmt = $l;
56805207649bSJoe Perches					$first = 0;
56815207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
56825207649bSJoe Perches					$define_stmt .= substr($l, 1);
56835207649bSJoe Perches				}
56845207649bSJoe Perches			}
56855207649bSJoe Perches			$define_stmt =~ s/$;//g;
56865207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
56875207649bSJoe Perches			$define_stmt = trim($define_stmt);
56885207649bSJoe Perches
5689f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5690f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5691f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
56929192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
56937fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
56946dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
56957fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
56967fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5697d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5698f59b64bfSJoe Perches				if ($use_cnt > 1) {
5699f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5700f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5701f59b64bfSJoe Perches				    }
57029192d41aSJoe Perches# check if any macro arguments may have other precedence issues
57037fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
57049192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
57059192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
57069192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
57079192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
57089192d41aSJoe Perches				}
57090a920b5bSAndy Whitcroft			}
57105023d347SJoe Perches
571108a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
571208a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
571308a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
571408a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5715e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
571608a2843eSJoe Perches
571708a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
571808a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
571908a2843eSJoe Perches			}
572008a2843eSJoe Perches
5721481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
57225023d347SJoe Perches
57235023d347SJoe Perches		} else {
57245023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5725481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5726481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
57275023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
57285023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
57295023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
57305023d347SJoe Perches			}
5731653d4876SAndy Whitcroft		}
57320a920b5bSAndy Whitcroft
5733b13edf7fSJoe Perches# do {} while (0) macro tests:
5734b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5735b13edf7fSJoe Perches# macro should not end with a semicolon
57365b57980dSJoe Perches		if ($perl_version_ok &&
5737b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5738b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5739b13edf7fSJoe Perches			my $ln = $linenr;
5740b13edf7fSJoe Perches			my $cnt = $realcnt;
5741b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5742b13edf7fSJoe Perches			my $ctx = '';
5743b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5744b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5745b13edf7fSJoe Perches			$ctx = $dstat;
5746b13edf7fSJoe Perches
5747b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
57481b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5749b13edf7fSJoe Perches
5750b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5751b13edf7fSJoe Perches				my $stmts = $2;
5752b13edf7fSJoe Perches				my $semis = $3;
5753b13edf7fSJoe Perches
5754b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5755b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5756e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5757b13edf7fSJoe Perches
5758ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5759ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5760b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5761b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5762b13edf7fSJoe Perches				}
5763b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5764b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5765b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5766b13edf7fSJoe Perches				}
5767f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5768f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5769f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5770e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5771f5ef95b1SJoe Perches
5772f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5773f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5774b13edf7fSJoe Perches			}
5775b13edf7fSJoe Perches		}
5776b13edf7fSJoe Perches
5777f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
577813214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
577913214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5780cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
578113214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5782cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5783cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5784aad4f614SJoe Perches				my @allowed = ();
5785aad4f614SJoe Perches				my $allow = 0;
578613214adfSAndy Whitcroft				my $seen = 0;
5787773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5788cf655043SAndy Whitcroft				my $ln = $linenr - 1;
578913214adfSAndy Whitcroft				for my $chunk (@chunks) {
579013214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
579113214adfSAndy Whitcroft
5792773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5793773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5794773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5795773647a0SAndy Whitcroft
5796aad4f614SJoe Perches					$allowed[$allow] = 0;
5797773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5798773647a0SAndy Whitcroft
5799773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5800773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5801773647a0SAndy Whitcroft
5802773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5803cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5804cf655043SAndy Whitcroft
5805773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
580613214adfSAndy Whitcroft
580713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
580813214adfSAndy Whitcroft
5809aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5810cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5811cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5812aad4f614SJoe Perches						$allowed[$allow] = 1;
581313214adfSAndy Whitcroft					}
581413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5815cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5816aad4f614SJoe Perches						$allowed[$allow] = 1;
581713214adfSAndy Whitcroft					}
5818cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5819cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5820aad4f614SJoe Perches						$allowed[$allow] = 1;
582113214adfSAndy Whitcroft					}
5822aad4f614SJoe Perches					$allow++;
582313214adfSAndy Whitcroft				}
5824aad4f614SJoe Perches				if ($seen) {
5825aad4f614SJoe Perches					my $sum_allowed = 0;
5826aad4f614SJoe Perches					foreach (@allowed) {
5827aad4f614SJoe Perches						$sum_allowed += $_;
5828aad4f614SJoe Perches					}
5829aad4f614SJoe Perches					if ($sum_allowed == 0) {
5830000d1cc1SJoe Perches						WARN("BRACES",
5831000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5832aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5833aad4f614SJoe Perches						 $seen != $allow) {
5834aad4f614SJoe Perches						CHK("BRACES",
5835aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5836aad4f614SJoe Perches					}
583713214adfSAndy Whitcroft				}
583813214adfSAndy Whitcroft			}
583913214adfSAndy Whitcroft		}
5840773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
584113214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5842cf655043SAndy Whitcroft			my $allowed = 0;
5843f0a594c1SAndy Whitcroft
5844cf655043SAndy Whitcroft			# Check the pre-context.
5845cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5846cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5847cf655043SAndy Whitcroft				$allowed = 1;
5848f0a594c1SAndy Whitcroft			}
5849773647a0SAndy Whitcroft
5850773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5851773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5852773647a0SAndy Whitcroft
5853cf655043SAndy Whitcroft			# Check the condition.
5854cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5855773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5856cf655043SAndy Whitcroft			if (defined $cond) {
5857773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5858cf655043SAndy Whitcroft			}
5859cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5860cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5861cf655043SAndy Whitcroft				$allowed = 1;
5862cf655043SAndy Whitcroft			}
5863cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5864cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5865cf655043SAndy Whitcroft				$allowed = 1;
5866cf655043SAndy Whitcroft			}
5867cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5868cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5869cf655043SAndy Whitcroft				$allowed = 1;
5870cf655043SAndy Whitcroft			}
5871cf655043SAndy Whitcroft			# Check the post-context.
5872cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5873cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5874cf655043SAndy Whitcroft				if (defined $cond) {
5875773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5876cf655043SAndy Whitcroft				}
5877cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5878cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5879cf655043SAndy Whitcroft					$allowed = 1;
5880cf655043SAndy Whitcroft				}
5881cf655043SAndy Whitcroft			}
5882cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5883f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5884e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5885cf655043SAndy Whitcroft
5886000d1cc1SJoe Perches				WARN("BRACES",
5887000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5888f0a594c1SAndy Whitcroft			}
5889f0a594c1SAndy Whitcroft		}
5890f0a594c1SAndy Whitcroft
5891e4c5babdSJoe Perches# check for single line unbalanced braces
589295330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
589395330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5894e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5895e4c5babdSJoe Perches		}
5896e4c5babdSJoe Perches
58970979ae66SJoe Perches# check for unnecessary blank lines around braces
589877b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5899f8e58219SJoe Perches			if (CHK("BRACES",
5900f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5901f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5902f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5903f8e58219SJoe Perches			}
59040979ae66SJoe Perches		}
590577b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5906f8e58219SJoe Perches			if (CHK("BRACES",
5907f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5908f8e58219SJoe Perches			    $fix) {
5909f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5910f8e58219SJoe Perches			}
59110979ae66SJoe Perches		}
59120979ae66SJoe Perches
59134a0df2efSAndy Whitcroft# no volatiles please
59146c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
59156c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5916000d1cc1SJoe Perches			WARN("VOLATILE",
59178c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
59184a0df2efSAndy Whitcroft		}
59194a0df2efSAndy Whitcroft
59205e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
59215e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
59225e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
59235e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
592433acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
59255e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
59265e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
59275e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
59285e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
59295e4f6ba5SJoe Perches				     $fix &&
59305e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
59315e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
59325e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
59335e4f6ba5SJoe Perches				my $comma_close = "";
59345e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
59355e4f6ba5SJoe Perches					$comma_close = $1;
59365e4f6ba5SJoe Perches				}
59375e4f6ba5SJoe Perches
59385e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
59395e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
59405e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
59415e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
59425e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
59435e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
59445e4f6ba5SJoe Perches				$fixedline = $rawline;
59455e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
59465e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
59475e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
59485e4f6ba5SJoe Perches				}
59495e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
59505e4f6ba5SJoe Perches			}
59515e4f6ba5SJoe Perches		}
59525e4f6ba5SJoe Perches
59535e4f6ba5SJoe Perches# check for missing a space in a string concatenation
59545e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
59555e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
59565e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
59575e4f6ba5SJoe Perches		}
59585e4f6ba5SJoe Perches
595977cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5960e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5961e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5962e4b7d309SJoe Perches# function declarations
596377cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
596477cb8546SJoe Perches		    defined($context_function) &&
5965e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5966e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
596777cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5968e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
596977cb8546SJoe Perches		}
597077cb8546SJoe Perches
59715e4f6ba5SJoe Perches# check for spaces before a quoted newline
59725e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
59735e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
59745e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
59755e4f6ba5SJoe Perches			    $fix) {
59765e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
59775e4f6ba5SJoe Perches			}
59785e4f6ba5SJoe Perches
59795e4f6ba5SJoe Perches		}
59805e4f6ba5SJoe Perches
5981f17dba4fSJoe Perches# concatenated string without spaces between elements
598279682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
598379682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
598479682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
598579682c0cSJoe Perches			    $fix) {
598679682c0cSJoe Perches				while ($line =~ /($String)/g) {
598779682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
598879682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
598979682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
599079682c0cSJoe Perches				}
599179682c0cSJoe Perches			}
5992f17dba4fSJoe Perches		}
5993f17dba4fSJoe Perches
599490ad30e5SJoe Perches# uncoalesced string fragments
599533acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
599679682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
599779682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
599879682c0cSJoe Perches			    $fix) {
599979682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
600079682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
600179682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
600279682c0cSJoe Perches				}
600379682c0cSJoe Perches			}
600490ad30e5SJoe Perches		}
600590ad30e5SJoe Perches
6006522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
6007522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
6008522b837cSAlexey Dobriyan		my $show_Z = 1;
60095e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6010522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
60115e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
6012522b837cSAlexey Dobriyan			# check for %L
6013522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
60145e4f6ba5SJoe Perches				WARN("PRINTF_L",
6015522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6016522b837cSAlexey Dobriyan				$show_L = 0;
60175e4f6ba5SJoe Perches			}
6018522b837cSAlexey Dobriyan			# check for %Z
6019522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6020522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
6021522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6022522b837cSAlexey Dobriyan				$show_Z = 0;
6023522b837cSAlexey Dobriyan			}
6024522b837cSAlexey Dobriyan			# check for 0x<decimal>
6025522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6026522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
60276e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
60286e300757SJoe Perches			}
60295e4f6ba5SJoe Perches		}
60305e4f6ba5SJoe Perches
60315e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
60323f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
60335e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
60345e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
60355e4f6ba5SJoe Perches		}
60365e4f6ba5SJoe Perches
603700df344fSAndy Whitcroft# warn about #if 0
6038c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
603960f89010SPrakruthi Deepak Heragu			WARN("IF_0",
604060f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
604160f89010SPrakruthi Deepak Heragu		}
604260f89010SPrakruthi Deepak Heragu
604360f89010SPrakruthi Deepak Heragu# warn about #if 1
604460f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
604560f89010SPrakruthi Deepak Heragu			WARN("IF_1",
604660f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
60474a0df2efSAndy Whitcroft		}
60484a0df2efSAndy Whitcroft
604903df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
605003df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6051100425deSJoe Perches			my $tested = quotemeta($1);
6052100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6053100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6054100425deSJoe Perches				my $func = $1;
6055100425deSJoe Perches				if (WARN('NEEDLESS_IF',
6056100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6057100425deSJoe Perches				    $fix) {
6058100425deSJoe Perches					my $do_fix = 1;
6059100425deSJoe Perches					my $leading_tabs = "";
6060100425deSJoe Perches					my $new_leading_tabs = "";
6061100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6062100425deSJoe Perches						$leading_tabs = $1;
6063100425deSJoe Perches					} else {
6064100425deSJoe Perches						$do_fix = 0;
6065100425deSJoe Perches					}
6066100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6067100425deSJoe Perches						$new_leading_tabs = $1;
6068100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6069100425deSJoe Perches							$do_fix = 0;
6070100425deSJoe Perches						}
6071100425deSJoe Perches					} else {
6072100425deSJoe Perches						$do_fix = 0;
6073100425deSJoe Perches					}
6074100425deSJoe Perches					if ($do_fix) {
6075100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
6076100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6077100425deSJoe Perches					}
6078100425deSJoe Perches				}
60794c432a8fSGreg Kroah-Hartman			}
60804c432a8fSGreg Kroah-Hartman		}
6081f0a594c1SAndy Whitcroft
6082ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
6083ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6084ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6085ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
6086ebfdc409SJoe Perches		    $linenr > 3) {
6087ebfdc409SJoe Perches			my $testval = $2;
6088ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
6089ebfdc409SJoe Perches
6090ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6091ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6092ebfdc409SJoe Perches
6093e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6094e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
6095ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
6096ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6097ebfdc409SJoe Perches			}
6098ebfdc409SJoe Perches		}
6099ebfdc409SJoe Perches
6100f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
6101dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6102f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6103f78d98f6SJoe Perches			my $level = $1;
6104f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
6105f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
6106f78d98f6SJoe Perches			    $fix) {
6107f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6108f78d98f6SJoe Perches			}
6109f78d98f6SJoe Perches		}
6110f78d98f6SJoe Perches
611145c55e92SJoe Perches# check for logging continuations
611245c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
611345c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
611445c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
611545c55e92SJoe Perches		}
611645c55e92SJoe Perches
611770eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
611870eb2275SDwaipayan Ray		if (defined $stat &&
611970eb2275SDwaipayan Ray		    $line =~ /\b$logFunctions\s*\(/ &&
612070eb2275SDwaipayan Ray		    index($stat, '"') >= 0) {
612170eb2275SDwaipayan Ray			my $lc = $stat =~ tr@\n@@;
612270eb2275SDwaipayan Ray			$lc = $lc + $linenr;
612370eb2275SDwaipayan Ray			my $stat_real = get_stat_real($linenr, $lc);
612470eb2275SDwaipayan Ray			pos($stat_real) = index($stat_real, '"');
612570eb2275SDwaipayan Ray			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
612670eb2275SDwaipayan Ray				my $pspec = $1;
612770eb2275SDwaipayan Ray				my $h = $2;
612870eb2275SDwaipayan Ray				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
612970eb2275SDwaipayan Ray				if (WARN("UNNECESSARY_MODIFIER",
613070eb2275SDwaipayan Ray					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
613170eb2275SDwaipayan Ray				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
613270eb2275SDwaipayan Ray					my $nspec = $pspec;
613370eb2275SDwaipayan Ray					$nspec =~ s/h//g;
613470eb2275SDwaipayan Ray					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
613570eb2275SDwaipayan Ray				}
613670eb2275SDwaipayan Ray			}
613770eb2275SDwaipayan Ray		}
613870eb2275SDwaipayan Ray
6139abb08a53SJoe Perches# check for mask then right shift without a parentheses
61405b57980dSJoe Perches		if ($perl_version_ok &&
6141abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6142abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6143abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
6144abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6145abb08a53SJoe Perches		}
6146abb08a53SJoe Perches
6147b75ac618SJoe Perches# check for pointer comparisons to NULL
61485b57980dSJoe Perches		if ($perl_version_ok) {
6149b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6150b75ac618SJoe Perches				my $val = $1;
6151b75ac618SJoe Perches				my $equal = "!";
6152b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
6153b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
6154b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6155b75ac618SJoe Perches					    $fix) {
6156b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6157b75ac618SJoe Perches				}
6158b75ac618SJoe Perches			}
6159b75ac618SJoe Perches		}
6160b75ac618SJoe Perches
61618716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
61628716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
61638716de38SJoe Perches			my $attr = $1;
61648716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
61658716de38SJoe Perches				my $ptr = $1;
61668716de38SJoe Perches				my $var = $2;
61678716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
61688716de38SJoe Perches				      ERROR("MISPLACED_INIT",
61698716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
61708716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
61718716de38SJoe Perches				      WARN("MISPLACED_INIT",
61728716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
61738716de38SJoe Perches				    $fix) {
6174194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
61758716de38SJoe Perches				}
61768716de38SJoe Perches			}
61778716de38SJoe Perches		}
61788716de38SJoe Perches
6179e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
6180e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6181e970b884SJoe Perches			my $attr = $1;
6182e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
6183e970b884SJoe Perches			my $attr_prefix = $1;
6184e970b884SJoe Perches			my $attr_type = $2;
6185e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6186e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6187e970b884SJoe Perches			    $fix) {
6188194f66fcSJoe Perches				$fixed[$fixlinenr] =~
6189e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
6190e970b884SJoe Perches			}
6191e970b884SJoe Perches		}
6192e970b884SJoe Perches
6193e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
6194e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6195e970b884SJoe Perches			my $attr = $1;
6196e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6197e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6198e970b884SJoe Perches			    $fix) {
6199194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
6200e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
6201e970b884SJoe Perches				$lead = rtrim($1);
6202e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
6203e970b884SJoe Perches				$lead = "${lead}const ";
6204194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6205e970b884SJoe Perches			}
6206e970b884SJoe Perches		}
6207e970b884SJoe Perches
6208c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
6209c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
6210c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6211c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
6212c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6213c17893c7SJoe Perches			    $fix) {
6214c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6215c17893c7SJoe Perches			}
6216c17893c7SJoe Perches		}
6217c17893c7SJoe Perches
6218fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
6219fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
6220fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6221fbdb8138SJoe Perches			my $constant_func = $1;
6222fbdb8138SJoe Perches			my $func = $constant_func;
6223fbdb8138SJoe Perches			$func =~ s/^__constant_//;
6224fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
6225fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
6226fbdb8138SJoe Perches			    $fix) {
6227194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6228fbdb8138SJoe Perches			}
6229fbdb8138SJoe Perches		}
6230fbdb8138SJoe Perches
62311a15a250SPatrick Pannuto# prefer usleep_range over udelay
623237581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
623343c1d77cSJoe Perches			my $delay = $1;
62341a15a250SPatrick Pannuto			# ignore udelay's < 10, however
623543c1d77cSJoe Perches			if (! ($delay < 10) ) {
6236000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
6237458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
623843c1d77cSJoe Perches			}
623943c1d77cSJoe Perches			if ($delay > 2000) {
624043c1d77cSJoe Perches				WARN("LONG_UDELAY",
624143c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
62421a15a250SPatrick Pannuto			}
62431a15a250SPatrick Pannuto		}
62441a15a250SPatrick Pannuto
624509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
624609ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
624709ef8725SPatrick Pannuto			if ($1 < 20) {
6248000d1cc1SJoe Perches				WARN("MSLEEP",
6249458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
625009ef8725SPatrick Pannuto			}
625109ef8725SPatrick Pannuto		}
625209ef8725SPatrick Pannuto
625336ec1939SJoe Perches# check for comparisons of jiffies
625436ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
625536ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
625636ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
625736ec1939SJoe Perches		}
625836ec1939SJoe Perches
62599d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
62609d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
62619d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
62629d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
62639d7a34a5SJoe Perches		}
62649d7a34a5SJoe Perches
626500df344fSAndy Whitcroft# warn about #ifdefs in C files
6266c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
626700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
626800df344fSAndy Whitcroft#			print "$herecurr";
626900df344fSAndy Whitcroft#			$clean = 0;
627000df344fSAndy Whitcroft#		}
627100df344fSAndy Whitcroft
627222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
6273c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
62743705ce5bSJoe Perches			if (ERROR("SPACING",
62753705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
62763705ce5bSJoe Perches			    $fix) {
6277194f66fcSJoe Perches				$fixed[$fixlinenr] =~
62783705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
62793705ce5bSJoe Perches			}
62803705ce5bSJoe Perches
628122f2a2efSAndy Whitcroft		}
628222f2a2efSAndy Whitcroft
62834a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6284171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6285171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
62864a0df2efSAndy Whitcroft			my $which = $1;
62874a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6288000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6289000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
62904a0df2efSAndy Whitcroft			}
62914a0df2efSAndy Whitcroft		}
62924a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6293402c2553SMichael S. Tsirkin
6294402c2553SMichael S. Tsirkin		my $barriers = qr{
6295402c2553SMichael S. Tsirkin			mb|
6296402c2553SMichael S. Tsirkin			rmb|
6297ad83ec6cSWill Deacon			wmb
6298402c2553SMichael S. Tsirkin		}x;
6299402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6300402c2553SMichael S. Tsirkin			mb__before_atomic|
6301402c2553SMichael S. Tsirkin			mb__after_atomic|
6302402c2553SMichael S. Tsirkin			store_release|
6303402c2553SMichael S. Tsirkin			load_acquire|
6304402c2553SMichael S. Tsirkin			store_mb|
6305402c2553SMichael S. Tsirkin			(?:$barriers)
6306402c2553SMichael S. Tsirkin		}x;
6307402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6308402c2553SMichael S. Tsirkin			(?:$barriers)|
630943e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
631043e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6311402c2553SMichael S. Tsirkin		}x;
6312402c2553SMichael S. Tsirkin
6313402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
63144a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6315c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6316000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
63174a0df2efSAndy Whitcroft			}
63184a0df2efSAndy Whitcroft		}
63193ad81779SPaul E. McKenney
6320f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6321f4073b0fSMichael S. Tsirkin
6322f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6323f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6324f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6325f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6326f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6327f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6328f4073b0fSMichael S. Tsirkin		}
6329f4073b0fSMichael S. Tsirkin
6330cb426e99SJoe Perches# check for waitqueue_active without a comment.
6331cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6332cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6333cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6334cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6335cb426e99SJoe Perches			}
6336cb426e99SJoe Perches		}
63373ad81779SPaul E. McKenney
63385099a722SMarco Elver# check for data_race without a comment.
63395099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
63405099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
63415099a722SMarco Elver				WARN("DATA_RACE",
63425099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
63435099a722SMarco Elver			}
63445099a722SMarco Elver		}
63455099a722SMarco Elver
63464a0df2efSAndy Whitcroft# check of hardware specific defines
6347c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6348000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6349000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
63500a920b5bSAndy Whitcroft		}
6351653d4876SAndy Whitcroft
6352596ed45bSJoe Perches# check that the storage class is not after a type
6353596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6354000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6355596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6356596ed45bSJoe Perches		}
6357596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6358596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6359596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6360596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6361596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6362596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6363596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6364d4977c78STobias Klauser		}
6365d4977c78STobias Klauser
6366de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6367de7d4f0eSAndy Whitcroft# storage class and type.
63689c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
63699c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6370000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6371000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6372de7d4f0eSAndy Whitcroft		}
6373de7d4f0eSAndy Whitcroft
63748905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
63752b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
63762b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6377d5e616fcSJoe Perches			if (WARN("INLINE",
6378d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6379d5e616fcSJoe Perches			    $fix) {
6380194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6381d5e616fcSJoe Perches
6382d5e616fcSJoe Perches			}
63838905a67cSAndy Whitcroft		}
63848905a67cSAndy Whitcroft
63857ebe1d17SDwaipayan Ray# Check for compiler attributes
63862b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
63877ebe1d17SDwaipayan Ray		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
63887ebe1d17SDwaipayan Ray			my $attr = $1;
63897ebe1d17SDwaipayan Ray			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
63907ebe1d17SDwaipayan Ray
63917ebe1d17SDwaipayan Ray			my %attr_list = (
63920830aab0SJoe Perches				"alias"				=> "__alias",
63937ebe1d17SDwaipayan Ray				"aligned"			=> "__aligned",
63947ebe1d17SDwaipayan Ray				"always_inline"			=> "__always_inline",
63957ebe1d17SDwaipayan Ray				"assume_aligned"		=> "__assume_aligned",
63967ebe1d17SDwaipayan Ray				"cold"				=> "__cold",
63977ebe1d17SDwaipayan Ray				"const"				=> "__attribute_const__",
63987ebe1d17SDwaipayan Ray				"copy"				=> "__copy",
63997ebe1d17SDwaipayan Ray				"designated_init"		=> "__designated_init",
64007ebe1d17SDwaipayan Ray				"externally_visible"		=> "__visible",
64017ebe1d17SDwaipayan Ray				"format"			=> "printf|scanf",
64027ebe1d17SDwaipayan Ray				"gnu_inline"			=> "__gnu_inline",
64037ebe1d17SDwaipayan Ray				"malloc"			=> "__malloc",
64047ebe1d17SDwaipayan Ray				"mode"				=> "__mode",
64057ebe1d17SDwaipayan Ray				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
64067ebe1d17SDwaipayan Ray				"noclone"			=> "__noclone",
64077ebe1d17SDwaipayan Ray				"noinline"			=> "noinline",
64087ebe1d17SDwaipayan Ray				"nonstring"			=> "__nonstring",
64097ebe1d17SDwaipayan Ray				"noreturn"			=> "__noreturn",
64107ebe1d17SDwaipayan Ray				"packed"			=> "__packed",
64117ebe1d17SDwaipayan Ray				"pure"				=> "__pure",
6412339f29d9SJoe Perches				"section"			=> "__section",
64130830aab0SJoe Perches				"used"				=> "__used",
64140830aab0SJoe Perches				"weak"				=> "__weak"
64157ebe1d17SDwaipayan Ray			);
64167ebe1d17SDwaipayan Ray
64177ebe1d17SDwaipayan Ray			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6418339f29d9SJoe Perches				my $orig_attr = $1;
64197ebe1d17SDwaipayan Ray				my $params = '';
64207ebe1d17SDwaipayan Ray				$params = $2 if defined($2);
6421339f29d9SJoe Perches				my $curr_attr = $orig_attr;
64227ebe1d17SDwaipayan Ray				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
64237ebe1d17SDwaipayan Ray				if (exists($attr_list{$curr_attr})) {
6424339f29d9SJoe Perches					my $new = $attr_list{$curr_attr};
64257ebe1d17SDwaipayan Ray					if ($curr_attr eq "format" && $params) {
64267ebe1d17SDwaipayan Ray						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6427339f29d9SJoe Perches						$new = "__$1\($2";
64287ebe1d17SDwaipayan Ray					} else {
6429339f29d9SJoe Perches						$new = "$new$params";
64307ebe1d17SDwaipayan Ray					}
64317ebe1d17SDwaipayan Ray					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6432339f29d9SJoe Perches						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
64337ebe1d17SDwaipayan Ray					    $fix) {
6434339f29d9SJoe Perches						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6435339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/$remove//;
6436339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6437339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6438339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
64397ebe1d17SDwaipayan Ray					}
644039b7e287SJoe Perches				}
6441462811d9SJoe Perches			}
6442462811d9SJoe Perches
64437ebe1d17SDwaipayan Ray			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
64447ebe1d17SDwaipayan Ray			if ($attr =~ /^_*unused/) {
64457ebe1d17SDwaipayan Ray				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
64467ebe1d17SDwaipayan Ray				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6447d5e616fcSJoe Perches			}
64486061d949SJoe Perches		}
64496061d949SJoe Perches
6450619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
64515b57980dSJoe Perches		if ($perl_version_ok &&
6452619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6453619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6454619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6455619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6456619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6457619a908aSJoe Perches		}
6458619a908aSJoe Perches
6459fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6460e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6461fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6462e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6463e6176fa4SJoe Perches			my $type = $1;
6464e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6465e6176fa4SJoe Perches				$type = $1;
6466e6176fa4SJoe Perches				my $kernel_type = 'u';
6467e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6468e6176fa4SJoe Perches				$type =~ /(\d+)/;
6469e6176fa4SJoe Perches				$kernel_type .= $1;
6470e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6471e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6472e6176fa4SJoe Perches				    $fix) {
6473e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6474e6176fa4SJoe Perches				}
6475e6176fa4SJoe Perches			}
6476e6176fa4SJoe Perches		}
6477e6176fa4SJoe Perches
6478938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6479938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6480938224b5SJoe Perches			my $cast = $1;
6481938224b5SJoe Perches			my $const = $2;
6482938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6483938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6484938224b5SJoe Perches			    $fix) {
6485938224b5SJoe Perches				my $suffix = "";
6486938224b5SJoe Perches				my $newconst = $const;
6487938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6488938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6489938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6490938224b5SJoe Perches					$suffix .= 'LL';
6491938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6492938224b5SJoe Perches					$suffix .= 'L';
6493938224b5SJoe Perches				}
6494938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6495938224b5SJoe Perches			}
6496938224b5SJoe Perches		}
6497938224b5SJoe Perches
64988f53a9b8SJoe Perches# check for sizeof(&)
64998f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6500000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6501000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
65028f53a9b8SJoe Perches		}
65038f53a9b8SJoe Perches
650466c80b60SJoe Perches# check for sizeof without parenthesis
650566c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6506d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6507d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6508d5e616fcSJoe Perches			    $fix) {
6509194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6510d5e616fcSJoe Perches			}
651166c80b60SJoe Perches		}
651266c80b60SJoe Perches
651388982feaSJoe Perches# check for struct spinlock declarations
651488982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
651588982feaSJoe Perches			WARN("USE_SPINLOCK_T",
651688982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
651788982feaSJoe Perches		}
651888982feaSJoe Perches
6519a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
652006668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6521a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6522caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6523caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6524d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6525d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6526d5e616fcSJoe Perches				    $fix) {
6527194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6528d5e616fcSJoe Perches				}
6529a6962d72SJoe Perches			}
6530a6962d72SJoe Perches		}
6531a6962d72SJoe Perches
65320b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
65335b57980dSJoe Perches		if ($perl_version_ok &&
65340b523769SJoe Perches		    defined $stat &&
65350b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
65360b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6537e3c6bc95STobin C. Harding			my $stat_real;
6538e3c6bc95STobin C. Harding
65390b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
65400b523769SJoe Perches			$lc = $lc + $linenr;
65410b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6542ffe07513SJoe Perches				my $specifier;
6543ffe07513SJoe Perches				my $extension;
65443bd32d6aSSakari Ailus				my $qualifier;
6545ffe07513SJoe Perches				my $bad_specifier = "";
65460b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
65470b523769SJoe Perches				$fmt =~ s/%%//g;
6548e3c6bc95STobin C. Harding
65493bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6550e3c6bc95STobin C. Harding					$specifier = $1;
6551e3c6bc95STobin C. Harding					$extension = $2;
65523bd32d6aSSakari Ailus					$qualifier = $3;
6553361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
65543bd32d6aSSakari Ailus					    ($extension eq "f" &&
65553bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6556e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
65570b523769SJoe Perches						last;
65580b523769SJoe Perches					}
6559e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6560e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6561e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
65620b523769SJoe Perches						}
6563e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6564e3c6bc95STobin C. Harding						     "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
6565e3c6bc95STobin C. Harding					}
6566e3c6bc95STobin C. Harding				}
6567e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
65682a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
65691df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
65701df7338aSSergey Senozhatsky					my $use = "";
6571e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
65721df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6573e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
65741df7338aSSergey Senozhatsky					}
65752a9f9d85STobin C. Harding
65760b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6577e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6578e3c6bc95STobin C. Harding				}
65790b523769SJoe Perches			}
65800b523769SJoe Perches		}
65810b523769SJoe Perches
6582554e165cSAndy Whitcroft# Check for misused memsets
65835b57980dSJoe Perches		if ($perl_version_ok &&
6584d1fe9c09SJoe Perches		    defined $stat &&
65859e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6586554e165cSAndy Whitcroft
6587d7c76ba7SJoe Perches			my $ms_addr = $2;
6588d1fe9c09SJoe Perches			my $ms_val = $7;
6589d1fe9c09SJoe Perches			my $ms_size = $12;
6590d7c76ba7SJoe Perches
6591554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6592554e165cSAndy Whitcroft				ERROR("MEMSET",
6593d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6594554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6595554e165cSAndy Whitcroft				WARN("MEMSET",
6596d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6597d7c76ba7SJoe Perches			}
6598d7c76ba7SJoe Perches		}
6599d7c76ba7SJoe Perches
660098a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
66015b57980dSJoe Perches#		if ($perl_version_ok &&
6602f333195dSJoe Perches#		    defined $stat &&
6603f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6604f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6605f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6606f333195dSJoe Perches#			    $fix) {
6607f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6608f333195dSJoe Perches#			}
6609f333195dSJoe Perches#		}
661098a9bba5SJoe Perches
6611b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
66125b57980dSJoe Perches#		if ($perl_version_ok &&
6613f333195dSJoe Perches#		    defined $stat &&
6614f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6615f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6616f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6617f333195dSJoe Perches#		}
6618b6117d17SMateusz Kulikowski
66198617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
66208617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
66215b57980dSJoe Perches#		if ($perl_version_ok &&
6622f333195dSJoe Perches#		    defined $stat &&
6623f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6624f333195dSJoe Perches#
6625f333195dSJoe Perches#			my $ms_val = $7;
6626f333195dSJoe Perches#
6627f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6628f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6629f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6630f333195dSJoe Perches#				    $fix) {
6631f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6632f333195dSJoe Perches#				}
6633f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6634f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6635f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6636f333195dSJoe Perches#				    $fix) {
6637f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6638f333195dSJoe Perches#				}
6639f333195dSJoe Perches#			}
6640f333195dSJoe Perches#		}
66418617cd09SMateusz Kulikowski
6642d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
66435b57980dSJoe Perches		if ($perl_version_ok &&
6644d1fe9c09SJoe Perches		    defined $stat &&
6645d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6646d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6647d7c76ba7SJoe Perches				my $call = $1;
6648d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6649d7c76ba7SJoe Perches				my $arg1 = $3;
6650d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6651d1fe9c09SJoe Perches				my $arg2 = $8;
6652d7c76ba7SJoe Perches				my $cast;
6653d7c76ba7SJoe Perches
6654d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6655d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6656d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6657d7c76ba7SJoe Perches					$cast = $cast1;
6658d7c76ba7SJoe Perches				} else {
6659d7c76ba7SJoe Perches					$cast = $cast2;
6660d7c76ba7SJoe Perches				}
6661d7c76ba7SJoe Perches				WARN("MINMAX",
6662d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6663554e165cSAndy Whitcroft			}
6664554e165cSAndy Whitcroft		}
6665554e165cSAndy Whitcroft
66664a273195SJoe Perches# check usleep_range arguments
66675b57980dSJoe Perches		if ($perl_version_ok &&
66684a273195SJoe Perches		    defined $stat &&
66694a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
66704a273195SJoe Perches			my $min = $1;
66714a273195SJoe Perches			my $max = $7;
66724a273195SJoe Perches			if ($min eq $max) {
66734a273195SJoe Perches				WARN("USLEEP_RANGE",
6674458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
66754a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
66764a273195SJoe Perches				 $min > $max) {
66774a273195SJoe Perches				WARN("USLEEP_RANGE",
6678458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
66794a273195SJoe Perches			}
66804a273195SJoe Perches		}
66814a273195SJoe Perches
6682823b794cSJoe Perches# check for naked sscanf
66835b57980dSJoe Perches		if ($perl_version_ok &&
6684823b794cSJoe Perches		    defined $stat &&
66856c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6686823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6687823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6688823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6689823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6690823b794cSJoe Perches			$lc = $lc + $linenr;
66912a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6692823b794cSJoe Perches			WARN("NAKED_SSCANF",
6693823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6694823b794cSJoe Perches		}
6695823b794cSJoe Perches
6696afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
66975b57980dSJoe Perches		if ($perl_version_ok &&
6698afc819abSJoe Perches		    defined $stat &&
6699afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6700afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6701afc819abSJoe Perches			$lc = $lc + $linenr;
67022a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6703afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6704afc819abSJoe Perches				my $format = $6;
6705afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6706afc819abSJoe Perches				if ($count == 1 &&
6707afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6708afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6709afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6710afc819abSJoe Perches				}
6711afc819abSJoe Perches			}
6712afc819abSJoe Perches		}
6713afc819abSJoe Perches
671470dc8a48SJoe Perches# check for new externs in .h files.
671570dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
671670dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6717d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
671870dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
671970dc8a48SJoe Perches			    $fix) {
6720194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
672170dc8a48SJoe Perches			}
672270dc8a48SJoe Perches		}
672370dc8a48SJoe Perches
6724de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6725171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6726c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6727171ae1a4SAndy Whitcroft		{
6728c45dcabdSAndy Whitcroft			my $function_name = $1;
6729c45dcabdSAndy Whitcroft			my $paren_space = $2;
6730171ae1a4SAndy Whitcroft
6731171ae1a4SAndy Whitcroft			my $s = $stat;
6732171ae1a4SAndy Whitcroft			if (defined $cond) {
6733171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6734171ae1a4SAndy Whitcroft			}
6735d8b44b58SKees Cook			if ($s =~ /^\s*;/)
6736c45dcabdSAndy Whitcroft			{
6737000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6738000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6739de7d4f0eSAndy Whitcroft			}
6740de7d4f0eSAndy Whitcroft
6741171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6742000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6743000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6744171ae1a4SAndy Whitcroft			}
67459c9ba34eSAndy Whitcroft
67469c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
67479c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
67489c9ba34eSAndy Whitcroft		{
6749000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6750000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6751171ae1a4SAndy Whitcroft		}
6752171ae1a4SAndy Whitcroft
6753a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6754a0ad7596SJoe Perches		if (defined $stat &&
6755d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6756d8b44b58SKees Cook		    $1 ne "void") {
6757d8b44b58SKees Cook			my $args = trim($1);
6758ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6759ca0d8929SJoe Perches				my $arg = trim($1);
6760d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6761ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6762ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6763ca0d8929SJoe Perches				}
6764ca0d8929SJoe Perches			}
6765ca0d8929SJoe Perches		}
6766ca0d8929SJoe Perches
6767a0ad7596SJoe Perches# check for function definitions
67685b57980dSJoe Perches		if ($perl_version_ok &&
6769a0ad7596SJoe Perches		    defined $stat &&
6770a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6771a0ad7596SJoe Perches			$context_function = $1;
6772a0ad7596SJoe Perches
6773a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6774a0ad7596SJoe Perches			my $ok = 0;
6775a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6776a0ad7596SJoe Perches			my $herectx = $here . "\n";
6777a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6778a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6779a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6780a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6781a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6782a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6783a0ad7596SJoe Perches			}
6784a0ad7596SJoe Perches			if (!$ok) {
6785a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6786a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6787a0ad7596SJoe Perches			}
6788a0ad7596SJoe Perches		}
6789a0ad7596SJoe Perches
6790de7d4f0eSAndy Whitcroft# checks for new __setup's
6791de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6792de7d4f0eSAndy Whitcroft			my $name = $1;
6793de7d4f0eSAndy Whitcroft
6794de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6795000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
67962581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6797de7d4f0eSAndy Whitcroft			}
6798653d4876SAndy Whitcroft		}
67999c0ca6f9SAndy Whitcroft
6800e29a70f1SJoe Perches# check for pointless casting of alloc functions
6801e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6802000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6803000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
68049c0ca6f9SAndy Whitcroft		}
680513214adfSAndy Whitcroft
6806a640d25cSJoe Perches# alloc style
6807a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
68085b57980dSJoe Perches		if ($perl_version_ok &&
6809e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6810a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6811a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6812a640d25cSJoe Perches		}
6813a640d25cSJoe Perches
681460a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
68155b57980dSJoe Perches		if ($perl_version_ok &&
68161b4a2ed4SJoe Perches		    defined $stat &&
68171b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
681860a55369SJoe Perches			my $oldfunc = $3;
681960a55369SJoe Perches			my $a1 = $4;
682060a55369SJoe Perches			my $a2 = $10;
682160a55369SJoe Perches			my $newfunc = "kmalloc_array";
682260a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
682360a55369SJoe Perches			my $r1 = $a1;
682460a55369SJoe Perches			my $r2 = $a2;
682560a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
682660a55369SJoe Perches				$r1 = $a2;
682760a55369SJoe Perches				$r2 = $a1;
682860a55369SJoe Perches			}
6829e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6830e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
68311b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6832e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6833e3d95a2aSTobin C. Harding
6834e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
68351b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
68361b4a2ed4SJoe Perches				    $cnt == 1 &&
6837e367455aSJoe Perches				    $fix) {
6838194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
683960a55369SJoe Perches				}
684060a55369SJoe Perches			}
684160a55369SJoe Perches		}
684260a55369SJoe Perches
6843972fdea2SJoe Perches# check for krealloc arg reuse
68445b57980dSJoe Perches		if ($perl_version_ok &&
68454cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
68464cab63ceSJoe Perches		    $1 eq $3) {
6847972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6848972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6849972fdea2SJoe Perches		}
6850972fdea2SJoe Perches
68515ce59ae0SJoe Perches# check for alloc argument mismatch
68525ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
68535ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
68545ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
68555ce59ae0SJoe Perches		}
68565ce59ae0SJoe Perches
6857caf2a54fSJoe Perches# check for multiple semicolons
6858caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6859d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6860d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6861d5e616fcSJoe Perches			    $fix) {
6862194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6863d5e616fcSJoe Perches			}
6864d1e2ad07SJoe Perches		}
6865d1e2ad07SJoe Perches
6866cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6867cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6868cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
68690ab90191SJoe Perches			my $ull = "";
68700ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
68710ab90191SJoe Perches			if (CHK("BIT_MACRO",
68720ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
68730ab90191SJoe Perches			    $fix) {
68740ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
68750ab90191SJoe Perches			}
68760ab90191SJoe Perches		}
68770ab90191SJoe Perches
687850161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
68793e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
688050161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
68813e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
688250161266SJoe Perches		}
688350161266SJoe Perches
68842d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
68853e89ad85SJerome Forissier		if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
68862d632745SJoe Perches			my $config = $1;
68872d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
68883e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
68892d632745SJoe Perches			    $fix) {
68902d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
68912d632745SJoe Perches			}
68922d632745SJoe Perches		}
68932d632745SJoe Perches
6894f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6895f36d3eb8SJoe Perches		my @fallthroughs = (
6896f36d3eb8SJoe Perches			'fallthrough',
6897f36d3eb8SJoe Perches			'@fallthrough@',
6898f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6899f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6900f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6901f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6902f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6903f36d3eb8SJoe Perches		    );
6904f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6905f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6906f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6907f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6908f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6909f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6910f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6911f36d3eb8SJoe Perches					last;
6912f36d3eb8SJoe Perches				}
6913f36d3eb8SJoe Perches			}
6914f36d3eb8SJoe Perches		}
6915f36d3eb8SJoe Perches
6916d1e2ad07SJoe Perches# check for switch/default statements without a break;
69175b57980dSJoe Perches		if ($perl_version_ok &&
6918d1e2ad07SJoe Perches		    defined $stat &&
6919d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6920d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6921e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6922e3d95a2aSTobin C. Harding
6923d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6924d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6925caf2a54fSJoe Perches		}
6926caf2a54fSJoe Perches
692713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6928d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6929d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6930d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6931d5e616fcSJoe Perches			    $fix) {
6932194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6933d5e616fcSJoe Perches			}
693413214adfSAndy Whitcroft		}
6935773647a0SAndy Whitcroft
693662ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
693762ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
693862ec818fSJoe Perches			ERROR("DATE_TIME",
693962ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
694062ec818fSJoe Perches		}
694162ec818fSJoe Perches
69422c92488aSJoe Perches# check for use of yield()
69432c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
69442c92488aSJoe Perches			WARN("YIELD",
69452c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
69462c92488aSJoe Perches		}
69472c92488aSJoe Perches
6948179f8f40SJoe Perches# check for comparisons against true and false
6949179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6950179f8f40SJoe Perches			my $lead = $1;
6951179f8f40SJoe Perches			my $arg = $2;
6952179f8f40SJoe Perches			my $test = $3;
6953179f8f40SJoe Perches			my $otype = $4;
6954179f8f40SJoe Perches			my $trail = $5;
6955179f8f40SJoe Perches			my $op = "!";
6956179f8f40SJoe Perches
6957179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6958179f8f40SJoe Perches
6959179f8f40SJoe Perches			my $type = lc($otype);
6960179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6961179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6962179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6963179f8f40SJoe Perches					$op = "";
6964179f8f40SJoe Perches				}
6965179f8f40SJoe Perches
6966179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6967179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6968179f8f40SJoe Perches
6969179f8f40SJoe Perches## maybe suggesting a correct construct would better
6970179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6971179f8f40SJoe Perches
6972179f8f40SJoe Perches			}
6973179f8f40SJoe Perches		}
6974179f8f40SJoe Perches
69754882720bSThomas Gleixner# check for semaphores initialized locked
69764882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6977000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6978000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6979773647a0SAndy Whitcroft		}
69806712d858SJoe Perches
698167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
698267d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6983000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
698467d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6985773647a0SAndy Whitcroft		}
69866712d858SJoe Perches
6987ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6988f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6989000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6990ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6991f3db6639SMichael Ellerman		}
69926712d858SJoe Perches
69933d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
69943d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
69953d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
69963d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
69973d709ab5SPaul E. McKenney		}
69983d709ab5SPaul E. McKenney
69999189c7e7SJoe Perches# check for deprecated apis
70009189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
70019189c7e7SJoe Perches			my $deprecated_api = $1;
70029189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
70039189c7e7SJoe Perches			WARN("DEPRECATED_API",
70049189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
70059189c7e7SJoe Perches		}
70069189c7e7SJoe Perches
70070f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
7008d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
7009ced69da1SQuentin Monnet		if (defined($const_structs) &&
7010ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
7011d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7012000d1cc1SJoe Perches			WARN("CONST_STRUCT",
7013d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
70142b6db5cbSAndy Whitcroft		}
7015773647a0SAndy Whitcroft
7016773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
7017773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
7018773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
7019c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7020c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7021171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7022171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7023171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
7024773647a0SAndy Whitcroft		{
7025000d1cc1SJoe Perches			WARN("NR_CPUS",
7026000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7027773647a0SAndy Whitcroft		}
70289c9ba34eSAndy Whitcroft
702952ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
703052ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
703152ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
703252ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
703352ea8506SJoe Perches		}
703452ea8506SJoe Perches
7035acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
70365b57980dSJoe Perches		if ($perl_version_ok &&
7037acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7038acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
7039acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7040acd9362cSJoe Perches		}
7041acd9362cSJoe Perches
7042de3f186fSDenis Efremov# nested likely/unlikely calls
7043de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7044de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
7045de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7046de3f186fSDenis Efremov		}
7047de3f186fSDenis Efremov
7048691d77b6SAndy Whitcroft# whine mightly about in_atomic
7049691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
7050691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
7051000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
7052000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
7053f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
7054000d1cc1SJoe Perches				WARN("IN_ATOMIC",
7055000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7056691d77b6SAndy Whitcroft			}
7057691d77b6SAndy Whitcroft		}
70581704f47bSPeter Zijlstra
70590f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
70600f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
70610f5225b0SPeter Zijlstra			ERROR("LOCKING",
70620f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
70630f5225b0SPeter Zijlstra		}
70640f5225b0SPeter Zijlstra
70651704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
70661704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
70671704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
70681704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
70691704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
70701704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
7071000d1cc1SJoe Perches				ERROR("LOCKDEP",
7072000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
70731704f47bSPeter Zijlstra			}
70741704f47bSPeter Zijlstra		}
707588f8831cSDave Jones
7076b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7077b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7078000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
7079000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
708088f8831cSDave Jones		}
70812435880fSJoe Perches
708200180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
708300180468SJoe Perches# and whether or not function naming is typical and if
708400180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
70855b57980dSJoe Perches		if ($perl_version_ok &&
708600180468SJoe Perches		    defined $stat &&
708700180468SJoe Perches		    $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
708800180468SJoe Perches			my $var = $1;
708900180468SJoe Perches			my $perms = $2;
709000180468SJoe Perches			my $show = $3;
709100180468SJoe Perches			my $store = $4;
709200180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
709300180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
709400180468SJoe Perches			    $store =~ /^${var}_store$/ &&
709500180468SJoe Perches			    $octal_perms eq "0644") {
709600180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
709700180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
709800180468SJoe Perches				    $fix) {
709900180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
710000180468SJoe Perches				}
710100180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
710200180468SJoe Perches				 $store =~ /^NULL$/ &&
710300180468SJoe Perches				 $octal_perms eq "0444") {
710400180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
710500180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
710600180468SJoe Perches				    $fix) {
710700180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
710800180468SJoe Perches				}
710900180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
711000180468SJoe Perches				 $store =~ /^${var}_store$/ &&
711100180468SJoe Perches				 $octal_perms eq "0200") {
711200180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
711300180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
711400180468SJoe Perches				    $fix) {
711500180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
711600180468SJoe Perches				}
711700180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
711800180468SJoe Perches				 $octal_perms eq "0444" ||
711900180468SJoe Perches				 $octal_perms eq "0200") {
712000180468SJoe Perches				my $newshow = "$show";
712100180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
712200180468SJoe Perches				my $newstore = $store;
712300180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
712400180468SJoe Perches				my $rename = "";
712500180468SJoe Perches				if ($show ne $newshow) {
712600180468SJoe Perches					$rename .= " '$show' to '$newshow'";
712700180468SJoe Perches				}
712800180468SJoe Perches				if ($store ne $newstore) {
712900180468SJoe Perches					$rename .= " '$store' to '$newstore'";
713000180468SJoe Perches				}
713100180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
713200180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
713300180468SJoe Perches			} else {
713400180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
713500180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
713600180468SJoe Perches			}
713700180468SJoe Perches		}
713800180468SJoe Perches
7139515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
7140515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
714173121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
714273121534SJoe Perches#   specific definition of not visible in sysfs.
714373121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
714473121534SJoe Perches#   use the default permissions
71455b57980dSJoe Perches		if ($perl_version_ok &&
7146459cf0aeSJoe Perches		    defined $stat &&
7147515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
71482435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
71492435880fSJoe Perches				my $func = $entry->[0];
71502435880fSJoe Perches				my $arg_pos = $entry->[1];
71512435880fSJoe Perches
7152459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
7153459cf0aeSJoe Perches				$lc = $lc + $linenr;
71542a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
7155459cf0aeSJoe Perches
71562435880fSJoe Perches				my $skip_args = "";
71572435880fSJoe Perches				if ($arg_pos > 1) {
71582435880fSJoe Perches					$arg_pos--;
71592435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
71602435880fSJoe Perches				}
7161f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7162459cf0aeSJoe Perches				if ($stat =~ /$test/) {
71632435880fSJoe Perches					my $val = $1;
71642435880fSJoe Perches					$val = $6 if ($skip_args ne "");
716573121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
716673121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
716773121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
71682435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
7169459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7170f90774e1SJoe Perches					}
7171f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7172c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
7173459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
71742435880fSJoe Perches					}
7175459cf0aeSJoe Perches				}
7176459cf0aeSJoe Perches			}
7177459cf0aeSJoe Perches		}
7178459cf0aeSJoe Perches
7179459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
7180bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
718100180468SJoe Perches			my $oval = $1;
718200180468SJoe Perches			my $octal = perms_to_octal($oval);
7183f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
7184459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7185f90774e1SJoe Perches			    $fix) {
718600180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
71872435880fSJoe Perches			}
718813214adfSAndy Whitcroft		}
71895a6d20ceSBjorn Andersson
71905a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
71915a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
71925a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
71935a6d20ceSBjorn Andersson			my $valid_licenses = qr{
71945a6d20ceSBjorn Andersson						GPL|
71955a6d20ceSBjorn Andersson						GPL\ v2|
71965a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
71975a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
71985a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
71995a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
72005a6d20ceSBjorn Andersson						Proprietary
72015a6d20ceSBjorn Andersson					}x;
72025a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
72035a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
72045a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
72055a6d20ceSBjorn Andersson			}
72065a6d20ceSBjorn Andersson		}
72076a8d76cbSMatteo Croce
72086a8d76cbSMatteo Croce# check for sysctl duplicate constants
72096a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
72106a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
72116a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
72126a8d76cbSMatteo Croce		}
7213515a235eSJoe Perches	}
721413214adfSAndy Whitcroft
721513214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
721613214adfSAndy Whitcroft	# so just keep quiet.
721713214adfSAndy Whitcroft	if ($#rawlines == -1) {
721813214adfSAndy Whitcroft		exit(0);
72190a920b5bSAndy Whitcroft	}
72200a920b5bSAndy Whitcroft
72218905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
72228905a67cSAndy Whitcroft	# things that appear to be patches.
72238905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
72248905a67cSAndy Whitcroft		exit(0);
72258905a67cSAndy Whitcroft	}
72268905a67cSAndy Whitcroft
7227e73d2715SDwaipayan Ray	# This is not a patch, and we are in 'no-patch' mode so
72288905a67cSAndy Whitcroft	# just keep quiet.
72298905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
72308905a67cSAndy Whitcroft		exit(0);
72318905a67cSAndy Whitcroft	}
72328905a67cSAndy Whitcroft
7233a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7234000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
7235000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
72360a920b5bSAndy Whitcroft	}
7237cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
7238cd261496SGeert Uytterhoeven		if ($signoff == 0) {
7239000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
7240000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
724148ca2d8aSDwaipayan Ray		} elsif ($authorsignoff != 1) {
724248ca2d8aSDwaipayan Ray			# authorsignoff values:
724348ca2d8aSDwaipayan Ray			# 0 -> missing sign off
724448ca2d8aSDwaipayan Ray			# 1 -> sign off identical
724548ca2d8aSDwaipayan Ray			# 2 -> names and addresses match, comments mismatch
724648ca2d8aSDwaipayan Ray			# 3 -> addresses match, names different
724748ca2d8aSDwaipayan Ray			# 4 -> names match, addresses different
724848ca2d8aSDwaipayan Ray			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
724948ca2d8aSDwaipayan Ray
725048ca2d8aSDwaipayan Ray			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
725148ca2d8aSDwaipayan Ray
725248ca2d8aSDwaipayan Ray			if ($authorsignoff == 0) {
725348ca2d8aSDwaipayan Ray				ERROR("NO_AUTHOR_SIGN_OFF",
7254cd261496SGeert Uytterhoeven				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
725548ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 2) {
725648ca2d8aSDwaipayan Ray				CHK("FROM_SIGN_OFF_MISMATCH",
725748ca2d8aSDwaipayan Ray				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
725848ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 3) {
725948ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
726048ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
726148ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 4) {
726248ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
726348ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
726448ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 5) {
726548ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
726648ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
726748ca2d8aSDwaipayan Ray			}
7268cd261496SGeert Uytterhoeven		}
72690a920b5bSAndy Whitcroft	}
72700a920b5bSAndy Whitcroft
7271f0a594c1SAndy Whitcroft	print report_dump();
727213214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
727313214adfSAndy Whitcroft		print "$filename " if ($summary_file);
72746c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
72756c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
72766c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
72776c72ffaaSAndy Whitcroft	}
72788905a67cSAndy Whitcroft
7279d2c0a235SAndy Whitcroft	if ($quiet == 0) {
7280ef212196SJoe Perches		# If there were any defects found and not already fixing them
7281ef212196SJoe Perches		if (!$clean and !$fix) {
7282ef212196SJoe Perches			print << "EOM"
7283ef212196SJoe Perches
7284ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
7285ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
7286ef212196SJoe PerchesEOM
7287ef212196SJoe Perches		}
7288d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
7289d2c0a235SAndy Whitcroft		# then suggest that.
7290d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
7291b0781216SMike Frysinger			$rpt_cleaners = 0;
7292d8469f16SJoe Perches			print << "EOM"
7293d8469f16SJoe Perches
7294d8469f16SJoe PerchesNOTE: Whitespace errors detected.
7295d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
7296d8469f16SJoe PerchesEOM
7297d2c0a235SAndy Whitcroft		}
7298d2c0a235SAndy Whitcroft	}
7299d2c0a235SAndy Whitcroft
7300d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
7301d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
7302d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
73039624b8d6SJoe Perches		my $newfile = $filename;
73049624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
73053705ce5bSJoe Perches		my $linecount = 0;
73063705ce5bSJoe Perches		my $f;
73073705ce5bSJoe Perches
7308d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7309d752fcc8SJoe Perches
73103705ce5bSJoe Perches		open($f, '>', $newfile)
73113705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
73123705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
73133705ce5bSJoe Perches			$linecount++;
73143705ce5bSJoe Perches			if ($file) {
73153705ce5bSJoe Perches				if ($linecount > 3) {
73163705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
73173705ce5bSJoe Perches					print $f $fixed_line . "\n";
73183705ce5bSJoe Perches				}
73193705ce5bSJoe Perches			} else {
73203705ce5bSJoe Perches				print $f $fixed_line . "\n";
73213705ce5bSJoe Perches			}
73223705ce5bSJoe Perches		}
73233705ce5bSJoe Perches		close($f);
73243705ce5bSJoe Perches
73253705ce5bSJoe Perches		if (!$quiet) {
73263705ce5bSJoe Perches			print << "EOM";
7327d8469f16SJoe Perches
73283705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
73293705ce5bSJoe Perches
73303705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
73313705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
73323705ce5bSJoe Perches
73333705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
73343705ce5bSJoe PerchesNo warranties, expressed or implied...
73353705ce5bSJoe PerchesEOM
73363705ce5bSJoe Perches		}
73373705ce5bSJoe Perches	}
73383705ce5bSJoe Perches
7339d8469f16SJoe Perches	if ($quiet == 0) {
7340d8469f16SJoe Perches		print "\n";
7341d8469f16SJoe Perches		if ($clean == 1) {
7342d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7343d8469f16SJoe Perches		} else {
7344d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
73450a920b5bSAndy Whitcroft		}
73460a920b5bSAndy Whitcroft	}
73470a920b5bSAndy Whitcroft	return $clean;
73480a920b5bSAndy Whitcroft}
7349