xref: /linux-6.15/scripts/checkpatch.pl (revision 8ea0114e)
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;
2652178ce0SDwaipayan Raymy $verbose = 0;
2752178ce0SDwaipayan Raymy %verbose_messages = ();
2852178ce0SDwaipayan Raymy %verbose_emitted = ();
290a920b5bSAndy Whitcroftmy $tree = 1;
300a920b5bSAndy Whitcroftmy $chk_signoff = 1;
310a920b5bSAndy Whitcroftmy $chk_patch = 1;
32773647a0SAndy Whitcroftmy $tst_only;
336c72ffaaSAndy Whitcroftmy $emacs = 0;
348905a67cSAndy Whitcroftmy $terse = 0;
3534d8815fSJoe Perchesmy $showfile = 0;
366c72ffaaSAndy Whitcroftmy $file = 0;
374a593c34SDu, Changbinmy $git = 0;
380dea9f1eSJoe Perchesmy %git_commits = ();
396c72ffaaSAndy Whitcroftmy $check = 0;
402ac73b4fSJoe Perchesmy $check_orig = 0;
418905a67cSAndy Whitcroftmy $summary = 1;
428905a67cSAndy Whitcroftmy $mailback = 0;
4313214adfSAndy Whitcroftmy $summary_file = 0;
44000d1cc1SJoe Perchesmy $show_types = 0;
453beb42ecSJoe Perchesmy $list_types = 0;
463705ce5bSJoe Perchesmy $fix = 0;
479624b8d6SJoe Perchesmy $fix_inplace = 0;
486c72ffaaSAndy Whitcroftmy $root;
490f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'};
500f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot);
51c2fdda0dSAndy Whitcroftmy %debug;
523445686aSJoe Perchesmy %camelcase = ();
5391bfe484SJoe Perchesmy %use_type = ();
5491bfe484SJoe Perchesmy @use = ();
5591bfe484SJoe Perchesmy %ignore_type = ();
56000d1cc1SJoe Perchesmy @ignore = ();
5777f5b10aSHannes Edermy $help = 0;
58000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
59bdc48fa1SJoe Perchesmy $max_line_length = 100;
60d62a201fSDave Hansenmy $ignore_perl_version = 0;
61d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
6256193274SVadim Bendeburymy $min_conf_desc_length = 4;
6366b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
64ebfd7d62SJoe Perchesmy $codespell = 0;
65f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
660ee3e7b8SPeter Ujfalusimy $user_codespellfile = "";
67bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
6852178ce0SDwaipayan Raymy $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
69ced69da1SQuentin Monnetmy $typedefsfile;
70737c0767SJohn Brooksmy $color = "auto";
7198005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
72dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
73dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
74713a09deSAntonio Borneomy $tabsize = 8;
753e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_";
7677f5b10aSHannes Eder
7777f5b10aSHannes Edersub help {
7877f5b10aSHannes Eder	my ($exitcode) = @_;
7977f5b10aSHannes Eder
8077f5b10aSHannes Eder	print << "EOM";
8177f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
8277f5b10aSHannes EderVersion: $V
8377f5b10aSHannes Eder
8477f5b10aSHannes EderOptions:
8577f5b10aSHannes Eder  -q, --quiet                quiet
8652178ce0SDwaipayan Ray  -v, --verbose              verbose mode
8777f5b10aSHannes Eder  --no-tree                  run without a kernel tree
8877f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
8977f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
9077f5b10aSHannes Eder  --emacs                    emacs compile window format
9177f5b10aSHannes Eder  --terse                    one line per report
9234d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
934a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
944a593c34SDu, Changbin                             single git commit with:
954a593c34SDu, Changbin                               <rev>
964a593c34SDu, Changbin                               <rev>^
974a593c34SDu, Changbin                               <rev>~n
984a593c34SDu, Changbin                             multiple git commits with:
994a593c34SDu, Changbin                               <rev1>..<rev2>
1004a593c34SDu, Changbin                               <rev1>...<rev2>
1014a593c34SDu, Changbin                               <rev>-<count>
1024a593c34SDu, Changbin                             git merges are ignored
10377f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
10477f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
1053beb42ecSJoe Perches  --list-types               list the possible message types
10691bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
107000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
1083beb42ecSJoe Perches  --show-types               show the specific message type in the output
109bdc48fa1SJoe Perches  --max-line-length=n        set the maximum line length, (default $max_line_length)
110bdc48fa1SJoe Perches                             if exceeded, warn on patches
111bdc48fa1SJoe Perches                             requires --strict for use with --file
11256193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
113bdc48fa1SJoe Perches  --tab-size=n               set the number of spaces for tab (default $tabsize)
11477f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
11577f5b10aSHannes Eder  --no-summary               suppress the per-file summary
11677f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
11777f5b10aSHannes Eder  --summary-file             include the filename in summary
11877f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
11977f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
12077f5b10aSHannes Eder                             is all off)
12177f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
12277f5b10aSHannes Eder                             literally
1233705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1243705ce5bSJoe Perches                             If correctable single-line errors exist, create
1253705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1263705ce5bSJoe Perches                             with potential errors corrected to the preferred
1273705ce5bSJoe Perches                             checkpatch style
1289624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1299624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1309624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
131d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
132d62a201fSDave Hansen                             runtime errors.
133ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
1340ee3e7b8SPeter Ujfalusi                             (default:$codespellfile)
135ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
13675ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
137737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
138737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
1393e89ad85SJerome Forissier  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
1403e89ad85SJerome Forissier                             ${CONFIG_})
14177f5b10aSHannes Eder  -h, --help, --version      display this help and exit
14277f5b10aSHannes Eder
14377f5b10aSHannes EderWhen FILE is - read standard input.
14477f5b10aSHannes EderEOM
14577f5b10aSHannes Eder
14677f5b10aSHannes Eder	exit($exitcode);
14777f5b10aSHannes Eder}
14877f5b10aSHannes Eder
1493beb42ecSJoe Perchessub uniq {
1503beb42ecSJoe Perches	my %seen;
1513beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1523beb42ecSJoe Perches}
1533beb42ecSJoe Perches
1543beb42ecSJoe Perchessub list_types {
1553beb42ecSJoe Perches	my ($exitcode) = @_;
1563beb42ecSJoe Perches
1573beb42ecSJoe Perches	my $count = 0;
1583beb42ecSJoe Perches
1593beb42ecSJoe Perches	local $/ = undef;
1603beb42ecSJoe Perches
1613beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1623beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1633beb42ecSJoe Perches
1643beb42ecSJoe Perches	my $text = <$script>;
1653beb42ecSJoe Perches	close($script);
1663beb42ecSJoe Perches
16752178ce0SDwaipayan Ray	my %types = ();
1680547fa58SJean Delvare	# Also catch when type or level is passed through a variable
16952178ce0SDwaipayan Ray	while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
17052178ce0SDwaipayan Ray		if (defined($1)) {
17152178ce0SDwaipayan Ray			if (exists($types{$2})) {
17252178ce0SDwaipayan Ray				$types{$2} .= ",$1" if ($types{$2} ne $1);
17352178ce0SDwaipayan Ray			} else {
17452178ce0SDwaipayan Ray				$types{$2} = $1;
1753beb42ecSJoe Perches			}
17652178ce0SDwaipayan Ray		} else {
17752178ce0SDwaipayan Ray			$types{$2} = "UNDETERMINED";
17852178ce0SDwaipayan Ray		}
17952178ce0SDwaipayan Ray	}
18052178ce0SDwaipayan Ray
1813beb42ecSJoe Perches	print("#\tMessage type\n\n");
18252178ce0SDwaipayan Ray	if ($color) {
18352178ce0SDwaipayan Ray		print(" ( Color coding: ");
18452178ce0SDwaipayan Ray		print(RED . "ERROR" . RESET);
18552178ce0SDwaipayan Ray		print(" | ");
18652178ce0SDwaipayan Ray		print(YELLOW . "WARNING" . RESET);
18752178ce0SDwaipayan Ray		print(" | ");
18852178ce0SDwaipayan Ray		print(GREEN . "CHECK" . RESET);
18952178ce0SDwaipayan Ray		print(" | ");
19052178ce0SDwaipayan Ray		print("Multiple levels / Undetermined");
19152178ce0SDwaipayan Ray		print(" )\n\n");
19252178ce0SDwaipayan Ray	}
19352178ce0SDwaipayan Ray
19452178ce0SDwaipayan Ray	foreach my $type (sort keys %types) {
19552178ce0SDwaipayan Ray		my $orig_type = $type;
19652178ce0SDwaipayan Ray		if ($color) {
19752178ce0SDwaipayan Ray			my $level = $types{$type};
19852178ce0SDwaipayan Ray			if ($level eq "ERROR") {
19952178ce0SDwaipayan Ray				$type = RED . $type . RESET;
20052178ce0SDwaipayan Ray			} elsif ($level eq "WARN") {
20152178ce0SDwaipayan Ray				$type = YELLOW . $type . RESET;
20252178ce0SDwaipayan Ray			} elsif ($level eq "CHK") {
20352178ce0SDwaipayan Ray				$type = GREEN . $type . RESET;
20452178ce0SDwaipayan Ray			}
20552178ce0SDwaipayan Ray		}
2063beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
20752178ce0SDwaipayan Ray		if ($verbose && exists($verbose_messages{$orig_type})) {
20852178ce0SDwaipayan Ray			my $message = $verbose_messages{$orig_type};
20952178ce0SDwaipayan Ray			$message =~ s/\n/\n\t/g;
21052178ce0SDwaipayan Ray			print("\t" . $message . "\n\n");
21152178ce0SDwaipayan Ray		}
2123beb42ecSJoe Perches	}
2133beb42ecSJoe Perches
2143beb42ecSJoe Perches	exit($exitcode);
2153beb42ecSJoe Perches}
2163beb42ecSJoe Perches
217000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
218000d1cc1SJoe Perchesif (-f $conf) {
219000d1cc1SJoe Perches	my @conf_args;
220000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
221000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
222000d1cc1SJoe Perches
223000d1cc1SJoe Perches	while (<$conffile>) {
224000d1cc1SJoe Perches		my $line = $_;
225000d1cc1SJoe Perches
226000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
227000d1cc1SJoe Perches		$line =~ s/^\s*//g;
228000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
229000d1cc1SJoe Perches
230000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
231000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
232000d1cc1SJoe Perches
233000d1cc1SJoe Perches		my @words = split(" ", $line);
234000d1cc1SJoe Perches		foreach my $word (@words) {
235000d1cc1SJoe Perches			last if ($word =~ m/^#/);
236000d1cc1SJoe Perches			push (@conf_args, $word);
237000d1cc1SJoe Perches		}
238000d1cc1SJoe Perches	}
239000d1cc1SJoe Perches	close($conffile);
240000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
241000d1cc1SJoe Perches}
242000d1cc1SJoe Perches
24352178ce0SDwaipayan Raysub load_docs {
24452178ce0SDwaipayan Ray	open(my $docs, '<', "$docsfile")
24552178ce0SDwaipayan Ray	    or warn "$P: Can't read the documentation file $docsfile $!\n";
24652178ce0SDwaipayan Ray
24752178ce0SDwaipayan Ray	my $type = '';
24852178ce0SDwaipayan Ray	my $desc = '';
24952178ce0SDwaipayan Ray	my $in_desc = 0;
25052178ce0SDwaipayan Ray
25152178ce0SDwaipayan Ray	while (<$docs>) {
25252178ce0SDwaipayan Ray		chomp;
25352178ce0SDwaipayan Ray		my $line = $_;
25452178ce0SDwaipayan Ray		$line =~ s/\s+$//;
25552178ce0SDwaipayan Ray
25652178ce0SDwaipayan Ray		if ($line =~ /^\s*\*\*(.+)\*\*$/) {
25752178ce0SDwaipayan Ray			if ($desc ne '') {
25852178ce0SDwaipayan Ray				$verbose_messages{$type} = trim($desc);
25952178ce0SDwaipayan Ray			}
26052178ce0SDwaipayan Ray			$type = $1;
26152178ce0SDwaipayan Ray			$desc = '';
26252178ce0SDwaipayan Ray			$in_desc = 1;
26352178ce0SDwaipayan Ray		} elsif ($in_desc) {
26452178ce0SDwaipayan Ray			if ($line =~ /^(?:\s{4,}|$)/) {
26552178ce0SDwaipayan Ray				$line =~ s/^\s{4}//;
26652178ce0SDwaipayan Ray				$desc .= $line;
26752178ce0SDwaipayan Ray				$desc .= "\n";
26852178ce0SDwaipayan Ray			} else {
26952178ce0SDwaipayan Ray				$verbose_messages{$type} = trim($desc);
27052178ce0SDwaipayan Ray				$type = '';
27152178ce0SDwaipayan Ray				$desc = '';
27252178ce0SDwaipayan Ray				$in_desc = 0;
27352178ce0SDwaipayan Ray			}
27452178ce0SDwaipayan Ray		}
27552178ce0SDwaipayan Ray	}
27652178ce0SDwaipayan Ray
27752178ce0SDwaipayan Ray	if ($desc ne '') {
27852178ce0SDwaipayan Ray		$verbose_messages{$type} = trim($desc);
27952178ce0SDwaipayan Ray	}
28052178ce0SDwaipayan Ray	close($docs);
28152178ce0SDwaipayan Ray}
28252178ce0SDwaipayan Ray
283737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
284737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
285737c0767SJohn Brooksforeach (@ARGV) {
286737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
287737c0767SJohn Brooks		$_ = "--color=$color";
288737c0767SJohn Brooks	}
289737c0767SJohn Brooks}
290737c0767SJohn Brooks
2910a920b5bSAndy WhitcroftGetOptions(
2926c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
29352178ce0SDwaipayan Ray	'v|verbose!'	=> \$verbose,
2940a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2950a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2960a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2976c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2988905a67cSAndy Whitcroft	'terse!'	=> \$terse,
29934d8815fSJoe Perches	'showfile!'	=> \$showfile,
30077f5b10aSHannes Eder	'f|file!'	=> \$file,
3014a593c34SDu, Changbin	'g|git!'	=> \$git,
3026c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
3036c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
304000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
30591bfe484SJoe Perches	'types=s'	=> \@use,
306000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
3073beb42ecSJoe Perches	'list-types!'	=> \$list_types,
3086cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
30956193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
310713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
3116c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
3128905a67cSAndy Whitcroft	'summary!'	=> \$summary,
3138905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
31413214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
3153705ce5bSJoe Perches	'fix!'		=> \$fix,
3169624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
317d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
318c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
319773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
320ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
3210ee3e7b8SPeter Ujfalusi	'codespellfile=s'	=> \$user_codespellfile,
32275ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
323737c0767SJohn Brooks	'color=s'	=> \$color,
324737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
325737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
3263e89ad85SJerome Forissier	'kconfig-prefix=s'	=> \${CONFIG_},
32777f5b10aSHannes Eder	'h|help'	=> \$help,
32877f5b10aSHannes Eder	'version'	=> \$help
3290ee3e7b8SPeter Ujfalusi) or $help = 2;
33077f5b10aSHannes Eder
3310ee3e7b8SPeter Ujfalusiif ($user_codespellfile) {
3320ee3e7b8SPeter Ujfalusi	# Use the user provided codespell file unconditionally
3330ee3e7b8SPeter Ujfalusi	$codespellfile = $user_codespellfile;
3340ee3e7b8SPeter Ujfalusi} elsif (!(-f $codespellfile)) {
3350ee3e7b8SPeter Ujfalusi	# If /usr/share/codespell/dictionary.txt is not present, try to find it
3360ee3e7b8SPeter Ujfalusi	# under codespell's install directory: <codespell_root>/data/dictionary.txt
337c882c6b1SSagar Patel	if (($codespell || $help) && which("python3") ne "") {
3380ee3e7b8SPeter Ujfalusi		my $python_codespell_dict = << "EOF";
3390ee3e7b8SPeter Ujfalusi
3400ee3e7b8SPeter Ujfalusiimport os.path as op
3410ee3e7b8SPeter Ujfalusiimport codespell_lib
3420ee3e7b8SPeter Ujfalusicodespell_dir = op.dirname(codespell_lib.__file__)
3430ee3e7b8SPeter Ujfalusicodespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
3440ee3e7b8SPeter Ujfalusiprint(codespell_file, end='')
3450ee3e7b8SPeter UjfalusiEOF
3460ee3e7b8SPeter Ujfalusi
347c882c6b1SSagar Patel		my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
3480ee3e7b8SPeter Ujfalusi		$codespellfile = $codespell_dict if (-f $codespell_dict);
3490ee3e7b8SPeter Ujfalusi	}
3500ee3e7b8SPeter Ujfalusi}
3510ee3e7b8SPeter Ujfalusi
3520ee3e7b8SPeter Ujfalusi# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
3530ee3e7b8SPeter Ujfalusi# $help is 2 if invalid option is passed - exitcode: 1
3540ee3e7b8SPeter Ujfalusihelp($help - 1) if ($help);
3550a920b5bSAndy Whitcroft
35652178ce0SDwaipayan Raydie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
35752178ce0SDwaipayan Raydie "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
35852178ce0SDwaipayan Ray
35952178ce0SDwaipayan Rayif ($color =~ /^[01]$/) {
36052178ce0SDwaipayan Ray	$color = !$color;
36152178ce0SDwaipayan Ray} elsif ($color =~ /^always$/i) {
36252178ce0SDwaipayan Ray	$color = 1;
36352178ce0SDwaipayan Ray} elsif ($color =~ /^never$/i) {
36452178ce0SDwaipayan Ray	$color = 0;
36552178ce0SDwaipayan Ray} elsif ($color =~ /^auto$/i) {
36652178ce0SDwaipayan Ray	$color = (-t STDOUT);
36752178ce0SDwaipayan Ray} else {
36852178ce0SDwaipayan Ray	die "$P: Invalid color mode: $color\n";
36952178ce0SDwaipayan Ray}
37052178ce0SDwaipayan Ray
37152178ce0SDwaipayan Rayload_docs() if ($verbose);
3723beb42ecSJoe Percheslist_types(0) if ($list_types);
3733beb42ecSJoe Perches
3749624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
3752ac73b4fSJoe Perches$check_orig = $check;
3769624b8d6SJoe Perches
3770a920b5bSAndy Whitcroftmy $exit = 0;
3780a920b5bSAndy Whitcroft
3795b57980dSJoe Perchesmy $perl_version_ok = 1;
380d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
3815b57980dSJoe Perches	$perl_version_ok = 0;
382d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
3835b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
384d62a201fSDave Hansen}
385d62a201fSDave Hansen
38645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
3870a920b5bSAndy Whitcroftif ($#ARGV < 0) {
38845107ff6SAllen Hubbe	push(@ARGV, '-');
3890a920b5bSAndy Whitcroft}
3900a920b5bSAndy Whitcroft
391713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
39232f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
393713a09deSAntonio Borneo
39491bfe484SJoe Perchessub hash_save_array_words {
39591bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
39691bfe484SJoe Perches
39791bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
39891bfe484SJoe Perches	foreach my $word (@array) {
399000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
400000d1cc1SJoe Perches		$word =~ s/^\s*//g;
401000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
402000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
403000d1cc1SJoe Perches
404000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
405000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
406000d1cc1SJoe Perches
40791bfe484SJoe Perches		$hashRef->{$word}++;
408000d1cc1SJoe Perches	}
40991bfe484SJoe Perches}
41091bfe484SJoe Perches
41191bfe484SJoe Perchessub hash_show_words {
41291bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
41391bfe484SJoe Perches
4143c816e49SJoe Perches	if (keys %$hashRef) {
415d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
41658cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
41791bfe484SJoe Perches			print " $word";
41891bfe484SJoe Perches		}
419d8469f16SJoe Perches		print "\n";
42091bfe484SJoe Perches	}
42191bfe484SJoe Perches}
42291bfe484SJoe Perches
42391bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
42491bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
425000d1cc1SJoe Perches
426c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
427c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
4287429c690SAndy Whitcroftmy $dbg_type = 0;
429a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
430c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
43121caa13cSAndy Whitcroft	## no critic
43221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
43321caa13cSAndy Whitcroft	die "$@" if ($@);
434c2fdda0dSAndy Whitcroft}
435c2fdda0dSAndy Whitcroft
436d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
437d2c0a235SAndy Whitcroft
4388905a67cSAndy Whitcroftif ($terse) {
4398905a67cSAndy Whitcroft	$emacs = 1;
4408905a67cSAndy Whitcroft	$quiet++;
4418905a67cSAndy Whitcroft}
4428905a67cSAndy Whitcroft
4436c72ffaaSAndy Whitcroftif ($tree) {
4446c72ffaaSAndy Whitcroft	if (defined $root) {
4456c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
4466c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
4476c72ffaaSAndy Whitcroft		}
4486c72ffaaSAndy Whitcroft	} else {
4496c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
4506c72ffaaSAndy Whitcroft			$root = '.';
4516c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
4526c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
4536c72ffaaSAndy Whitcroft			$root = $1;
4546c72ffaaSAndy Whitcroft		}
4556c72ffaaSAndy Whitcroft	}
4566c72ffaaSAndy Whitcroft
4576c72ffaaSAndy Whitcroft	if (!defined $root) {
4580a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
4590a920b5bSAndy Whitcroft		exit(2);
4600a920b5bSAndy Whitcroft	}
4616c72ffaaSAndy Whitcroft}
4626c72ffaaSAndy Whitcroft
4636c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
4646c72ffaaSAndy Whitcroft
4652ceb532bSAndy Whitcroftour $Ident	= qr{
4662ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
4672ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
4682ceb532bSAndy Whitcroft		}x;
4696c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
4706c72ffaaSAndy Whitcroftour $Sparse	= qr{
4716c72ffaaSAndy Whitcroft			__user|
4726c72ffaaSAndy Whitcroft			__kernel|
4736c72ffaaSAndy Whitcroft			__force|
4746c72ffaaSAndy Whitcroft			__iomem|
4756c72ffaaSAndy Whitcroft			__must_check|
476417495edSAndy Whitcroft			__kprobes|
477165e72a6SSven Eckelmann			__ref|
47833aa4597SGeert Uytterhoeven			__refconst|
47933aa4597SGeert Uytterhoeven			__refdata|
480ad315455SBoqun Feng			__rcu|
481ad315455SBoqun Feng			__private
4826c72ffaaSAndy Whitcroft		}x;
483e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
484e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
485e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
486e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
487e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
4888716de38SJoe Perches
48952131292SWolfram Sang# Notes to $Attribute:
49052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
4916c72ffaaSAndy Whitcroftour $Attribute	= qr{
4926c72ffaaSAndy Whitcroft			const|
493b5e8736aSJoe Perches			volatile|
49403f1df7dSJoe Perches			__percpu|
49503f1df7dSJoe Perches			__nocast|
49603f1df7dSJoe Perches			__safe|
49746d832f5SMichael S. Tsirkin			__bitwise|
49803f1df7dSJoe Perches			__packed__|
49903f1df7dSJoe Perches			__packed2__|
50003f1df7dSJoe Perches			__naked|
50103f1df7dSJoe Perches			__maybe_unused|
50203f1df7dSJoe Perches			__always_unused|
50303f1df7dSJoe Perches			__noreturn|
50403f1df7dSJoe Perches			__used|
50503f1df7dSJoe Perches			__cold|
506e23ef1f3SJoe Perches			__pure|
50703f1df7dSJoe Perches			__noclone|
50803f1df7dSJoe Perches			__deprecated|
5096c72ffaaSAndy Whitcroft			__read_mostly|
510c5967e98SJoe Perches			__ro_after_init|
5116c72ffaaSAndy Whitcroft			__kprobes|
5128716de38SJoe Perches			$InitAttribute|
51324e1d81aSAndy Whitcroft			____cacheline_aligned|
51424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
5155fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
51686cffecdSKees Cook			__weak|
51786cffecdSKees Cook			__alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
5186c72ffaaSAndy Whitcroft		  }x;
519c45dcabdSAndy Whitcroftour $Modifier;
52091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
5216c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
5226c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
5236c72ffaaSAndy Whitcroft
52495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
52595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
52695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
52795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
5282435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
529d2af5aa6SJoe Perchesour $String	= qr{(?:\b[Lu])?"[X\t]*"};
530326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
531326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
532326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
53374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
5342435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
535326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
536447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
53723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
5386c72ffaaSAndy Whitcroftour $Operators	= qr{
5396c72ffaaSAndy Whitcroft			<=|>=|==|!=|
5406c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
54123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
5426c72ffaaSAndy Whitcroft		  }x;
5436c72ffaaSAndy Whitcroft
54491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
54591cb5195SJoe Perches
546ab7e23f3SJoe Perchesour $BasicType;
5478905a67cSAndy Whitcroftour $NonptrType;
5481813087dSJoe Perchesour $NonptrTypeMisordered;
5498716de38SJoe Perchesour $NonptrTypeWithAttr;
5508905a67cSAndy Whitcroftour $Type;
5511813087dSJoe Perchesour $TypeMisordered;
5528905a67cSAndy Whitcroftour $Declare;
5531813087dSJoe Perchesour $DeclareMisordered;
5548905a67cSAndy Whitcroft
55515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
55615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
557171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
558171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
559171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
560171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
561171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
562171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
563171ae1a4SAndy Whitcroft}x;
564171ae1a4SAndy Whitcroft
56515662b3eSJoe Perchesour $UTF8	= qr{
56615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
56715662b3eSJoe Perches	| $NON_ASCII_UTF8
56815662b3eSJoe Perches}x;
56915662b3eSJoe Perches
570e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
571021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
572021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
573021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
574021158b4SJoe Perches)};
575e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
576fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
5778ed22cadSAndy Whitcroft	atomic_t
5788ed22cadSAndy Whitcroft)};
579*8ea0114eSMickaël Salaünour $typeStdioTypedefs = qr{(?x:
580*8ea0114eSMickaël Salaün	FILE
581*8ea0114eSMickaël Salaün)};
582e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
583e6176fa4SJoe Perches	$typeC99Typedefs\b|
584e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
585*8ea0114eSMickaël Salaün	$typeKernelTypedefs\b|
586*8ea0114eSMickaël Salaün	$typeStdioTypedefs\b
587e6176fa4SJoe Perches)};
5888ed22cadSAndy Whitcroft
5896d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
5906d32f7a3SJoe Perches
591691e669bSJoe Perchesour $logFunctions = qr{(?x:
592758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
5937d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
59487bd499aSJoe Perches	TP_printk|
5956e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
596b0531722SJoe Perches	panic|
59706668727SJoe Perches	MODULE_[A-Z_]+|
59806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
599691e669bSJoe Perches)};
600691e669bSJoe Perches
601e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
602e29a70f1SJoe Perches	(?:(?:devm_)?
60358f02267SJoe Perches		(?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
604e29a70f1SJoe Perches		kstrdup(?:_const)? |
605e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
606461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
607e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
608e29a70f1SJoe Perches	dma_alloc_coherent
609e29a70f1SJoe Perches)};
610e29a70f1SJoe Perches
61120112475SJoe Perchesour $signature_tags = qr{(?xi:
61220112475SJoe Perches	Signed-off-by:|
613d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
61420112475SJoe Perches	Acked-by:|
61520112475SJoe Perches	Tested-by:|
61620112475SJoe Perches	Reviewed-by:|
61720112475SJoe Perches	Reported-by:|
6188543ae12SMugunthan V N	Suggested-by:|
61920112475SJoe Perches	To:|
62020112475SJoe Perches	Cc:
62120112475SJoe Perches)};
62220112475SJoe Perches
623adb2da82SJoe Perchesour $tracing_logging_tags = qr{(?xi:
624adb2da82SJoe Perches	[=-]*> |
625adb2da82SJoe Perches	<[=-]* |
626adb2da82SJoe Perches	\[ |
627adb2da82SJoe Perches	\] |
628adb2da82SJoe Perches	start |
629adb2da82SJoe Perches	called |
630adb2da82SJoe Perches	entered |
631adb2da82SJoe Perches	entry |
632adb2da82SJoe Perches	enter |
633adb2da82SJoe Perches	in |
634adb2da82SJoe Perches	inside |
635adb2da82SJoe Perches	here |
636adb2da82SJoe Perches	begin |
637adb2da82SJoe Perches	exit |
638adb2da82SJoe Perches	end |
639adb2da82SJoe Perches	done |
640adb2da82SJoe Perches	leave |
641adb2da82SJoe Perches	completed |
642adb2da82SJoe Perches	out |
643adb2da82SJoe Perches	return |
644adb2da82SJoe Perches	[\.\!:\s]*
645adb2da82SJoe Perches)};
646adb2da82SJoe Perches
647831242abSAditya Srivastavasub edit_distance_min {
648831242abSAditya Srivastava	my (@arr) = @_;
649831242abSAditya Srivastava	my $len = scalar @arr;
650831242abSAditya Srivastava	if ((scalar @arr) < 1) {
651831242abSAditya Srivastava		# if underflow, return
652831242abSAditya Srivastava		return;
653831242abSAditya Srivastava	}
654831242abSAditya Srivastava	my $min = $arr[0];
655831242abSAditya Srivastava	for my $i (0 .. ($len-1)) {
656831242abSAditya Srivastava		if ($arr[$i] < $min) {
657831242abSAditya Srivastava			$min = $arr[$i];
658831242abSAditya Srivastava		}
659831242abSAditya Srivastava	}
660831242abSAditya Srivastava	return $min;
661831242abSAditya Srivastava}
662831242abSAditya Srivastava
663831242abSAditya Srivastavasub get_edit_distance {
664831242abSAditya Srivastava	my ($str1, $str2) = @_;
665831242abSAditya Srivastava	$str1 = lc($str1);
666831242abSAditya Srivastava	$str2 = lc($str2);
667831242abSAditya Srivastava	$str1 =~ s/-//g;
668831242abSAditya Srivastava	$str2 =~ s/-//g;
669831242abSAditya Srivastava	my $len1 = length($str1);
670831242abSAditya Srivastava	my $len2 = length($str2);
671831242abSAditya Srivastava	# two dimensional array storing minimum edit distance
672831242abSAditya Srivastava	my @distance;
673831242abSAditya Srivastava	for my $i (0 .. $len1) {
674831242abSAditya Srivastava		for my $j (0 .. $len2) {
675831242abSAditya Srivastava			if ($i == 0) {
676831242abSAditya Srivastava				$distance[$i][$j] = $j;
677831242abSAditya Srivastava			} elsif ($j == 0) {
678831242abSAditya Srivastava				$distance[$i][$j] = $i;
679831242abSAditya Srivastava			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
680831242abSAditya Srivastava				$distance[$i][$j] = $distance[$i - 1][$j - 1];
681831242abSAditya Srivastava			} else {
682831242abSAditya Srivastava				my $dist1 = $distance[$i][$j - 1]; #insert distance
683831242abSAditya Srivastava				my $dist2 = $distance[$i - 1][$j]; # remove
684831242abSAditya Srivastava				my $dist3 = $distance[$i - 1][$j - 1]; #replace
685831242abSAditya Srivastava				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
686831242abSAditya Srivastava			}
687831242abSAditya Srivastava		}
688831242abSAditya Srivastava	}
689831242abSAditya Srivastava	return $distance[$len1][$len2];
690831242abSAditya Srivastava}
691831242abSAditya Srivastava
692831242abSAditya Srivastavasub find_standard_signature {
693831242abSAditya Srivastava	my ($sign_off) = @_;
694831242abSAditya Srivastava	my @standard_signature_tags = (
695831242abSAditya Srivastava		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
696831242abSAditya Srivastava		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
697831242abSAditya Srivastava	);
698831242abSAditya Srivastava	foreach my $signature (@standard_signature_tags) {
699831242abSAditya Srivastava		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
700831242abSAditya Srivastava	}
701831242abSAditya Srivastava
702831242abSAditya Srivastava	return "";
703831242abSAditya Srivastava}
704831242abSAditya Srivastava
7051813087dSJoe Perchesour @typeListMisordered = (
7061813087dSJoe Perches	qr{char\s+(?:un)?signed},
7071813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
7081813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
7091813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
7101813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
7111813087dSJoe Perches	qr{short\s+(?:un)?signed},
7121813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
7131813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
7141813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
7151813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
7161813087dSJoe Perches	qr{int\s+(?:un)?signed},
7171813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
7181813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
7191813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
7201813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
7211813087dSJoe Perches	qr{long\s+(?:un)?signed},
7221813087dSJoe Perches);
7231813087dSJoe Perches
7248905a67cSAndy Whitcroftour @typeList = (
7258905a67cSAndy Whitcroft	qr{void},
7260c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
7270c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
7280c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
7290c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
7300c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
7310c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
7320c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
7330c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
7340c773d9dSJoe Perches	qr{(?:un)?signed},
7358905a67cSAndy Whitcroft	qr{float},
7368905a67cSAndy Whitcroft	qr{double},
7378905a67cSAndy Whitcroft	qr{bool},
7388905a67cSAndy Whitcroft	qr{struct\s+$Ident},
7398905a67cSAndy Whitcroft	qr{union\s+$Ident},
7408905a67cSAndy Whitcroft	qr{enum\s+$Ident},
7418905a67cSAndy Whitcroft	qr{${Ident}_t},
7428905a67cSAndy Whitcroft	qr{${Ident}_handler},
7438905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
7441813087dSJoe Perches	@typeListMisordered,
7458905a67cSAndy Whitcroft);
746938224b5SJoe Perches
747938224b5SJoe Perchesour $C90_int_types = qr{(?x:
748938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
749938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
750938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
751938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
752938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
753938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
754938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
755938224b5SJoe Perches
756938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
757938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
758938224b5SJoe Perches	long\s+(?:un)?signed|
759938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
760938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
761938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
762938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
763938224b5SJoe Perches
764938224b5SJoe Perches	int\s+(?:un)?signed|
765938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
766938224b5SJoe Perches)};
767938224b5SJoe Perches
768485ff23eSAlex Dowadour @typeListFile = ();
7698716de38SJoe Perchesour @typeListWithAttr = (
7708716de38SJoe Perches	@typeList,
7718716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
7728716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
7738716de38SJoe Perches);
7748716de38SJoe Perches
775c45dcabdSAndy Whitcroftour @modifierList = (
776c45dcabdSAndy Whitcroft	qr{fastcall},
777c45dcabdSAndy Whitcroft);
778485ff23eSAlex Dowadour @modifierListFile = ();
7798905a67cSAndy Whitcroft
7802435880fSJoe Perchesour @mode_permission_funcs = (
7812435880fSJoe Perches	["module_param", 3],
7822435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
7832435880fSJoe Perches	["module_param_array_named", 5],
7842435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
7852435880fSJoe Perches	["proc_create(?:_data|)", 2],
786459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
787459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
788459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
789459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
790459cf0aeSJoe Perches	["__ATTR", 2],
7912435880fSJoe Perches);
7922435880fSJoe Perches
7931a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
7941a3dcf2eSJoe Perches
795515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
796515a235eSJoe Perchesour $mode_perms_search = "";
797515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
798515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
799515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
800515a235eSJoe Perches}
80100180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
802515a235eSJoe Perches
8039189c7e7SJoe Perchesour %deprecated_apis = (
8049189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
8059189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
8069189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
8079189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
8089189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
8099189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
8109189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
8119189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
8129189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
8139189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
814defdaff1SIra Weiny	"kmap"					=> "kmap_local_page",
815defdaff1SIra Weiny	"kmap_atomic"				=> "kmap_local_page",
8169189c7e7SJoe Perches);
8179189c7e7SJoe Perches
8189189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
8199189c7e7SJoe Perchesour $deprecated_apis_search = "";
8209189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
8219189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
8229189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
8239189c7e7SJoe Perches}
8249189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
8259189c7e7SJoe Perches
826b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
827b392c64fSJoe Perches	S_IWUGO		|
828b392c64fSJoe Perches	S_IWOTH		|
829b392c64fSJoe Perches	S_IRWXUGO	|
830b392c64fSJoe Perches	S_IALLUGO	|
831b392c64fSJoe Perches	0[0-7][0-7][2367]
832b392c64fSJoe Perches}x;
833b392c64fSJoe Perches
834f90774e1SJoe Perchesour %mode_permission_string_types = (
835f90774e1SJoe Perches	"S_IRWXU" => 0700,
836f90774e1SJoe Perches	"S_IRUSR" => 0400,
837f90774e1SJoe Perches	"S_IWUSR" => 0200,
838f90774e1SJoe Perches	"S_IXUSR" => 0100,
839f90774e1SJoe Perches	"S_IRWXG" => 0070,
840f90774e1SJoe Perches	"S_IRGRP" => 0040,
841f90774e1SJoe Perches	"S_IWGRP" => 0020,
842f90774e1SJoe Perches	"S_IXGRP" => 0010,
843f90774e1SJoe Perches	"S_IRWXO" => 0007,
844f90774e1SJoe Perches	"S_IROTH" => 0004,
845f90774e1SJoe Perches	"S_IWOTH" => 0002,
846f90774e1SJoe Perches	"S_IXOTH" => 0001,
847f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
848f90774e1SJoe Perches	"S_IRUGO" => 0444,
849f90774e1SJoe Perches	"S_IWUGO" => 0222,
850f90774e1SJoe Perches	"S_IXUGO" => 0111,
851f90774e1SJoe Perches);
852f90774e1SJoe Perches
853f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
854f90774e1SJoe Perchesour $mode_perms_string_search = "";
855f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
856f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
857f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
858f90774e1SJoe Perches}
85900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
86000180468SJoe Perchesour $multi_mode_perms_string_search = qr{
86100180468SJoe Perches	${single_mode_perms_string_search}
86200180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
86300180468SJoe Perches}x;
86400180468SJoe Perches
86500180468SJoe Perchessub perms_to_octal {
86600180468SJoe Perches	my ($string) = @_;
86700180468SJoe Perches
86800180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
86900180468SJoe Perches
87000180468SJoe Perches	my $val = "";
87100180468SJoe Perches	my $oval = "";
87200180468SJoe Perches	my $to = 0;
87300180468SJoe Perches	my $curpos = 0;
87400180468SJoe Perches	my $lastpos = 0;
87500180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
87600180468SJoe Perches		$curpos = pos($string);
87700180468SJoe Perches		my $match = $2;
87800180468SJoe Perches		my $omatch = $1;
87900180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
88000180468SJoe Perches		$lastpos = $curpos;
88100180468SJoe Perches		$to |= $mode_permission_string_types{$match};
88200180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
88300180468SJoe Perches		$val .= $match;
88400180468SJoe Perches		$oval .= $omatch;
88500180468SJoe Perches	}
88600180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
88700180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
88800180468SJoe Perches	return sprintf("%04o", $to);
88900180468SJoe Perches}
890f90774e1SJoe Perches
8917840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
8927840a94cSWolfram Sang	irq|
893cdcee686SSergey Ryazanov	memory|
894cdcee686SSergey Ryazanov	time|
895cdcee686SSergey Ryazanov	reboot
8967840a94cSWolfram Sang)};
8977840a94cSWolfram Sang# memory.h: ARM has a custom one
8987840a94cSWolfram Sang
89966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
90066b47b4aSKees Cookmy $misspellings;
90166b47b4aSKees Cookmy %spelling_fix;
90236061e38SJoe Perches
90336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
90466b47b4aSKees Cook	while (<$spelling>) {
90566b47b4aSKees Cook		my $line = $_;
90666b47b4aSKees Cook
90766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
90866b47b4aSKees Cook		$line =~ s/^\s*//g;
90966b47b4aSKees Cook
91066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
91166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
91266b47b4aSKees Cook
91366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
91466b47b4aSKees Cook
91566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
91666b47b4aSKees Cook	}
91766b47b4aSKees Cook	close($spelling);
91836061e38SJoe Perches} else {
91936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
92036061e38SJoe Perches}
92166b47b4aSKees Cook
922ebfd7d62SJoe Perchesif ($codespell) {
923ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
924ebfd7d62SJoe Perches		while (<$spelling>) {
925ebfd7d62SJoe Perches			my $line = $_;
926ebfd7d62SJoe Perches
927ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
928ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
929ebfd7d62SJoe Perches
930ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
931ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
932ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
933ebfd7d62SJoe Perches
934ebfd7d62SJoe Perches			$line =~ s/,.*$//;
935ebfd7d62SJoe Perches
936ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
937ebfd7d62SJoe Perches
938ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
939ebfd7d62SJoe Perches		}
940ebfd7d62SJoe Perches		close($spelling);
941ebfd7d62SJoe Perches	} else {
942ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
943ebfd7d62SJoe Perches	}
944ebfd7d62SJoe Perches}
945ebfd7d62SJoe Perches
946ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
947ebfd7d62SJoe Perches
94875ad8c57SJerome Forissiersub read_words {
94975ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
95075ad8c57SJerome Forissier
95175ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
95275ad8c57SJerome Forissier		while (<$words>) {
953bf1fa1daSJoe Perches			my $line = $_;
954bf1fa1daSJoe Perches
955bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
956bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
957bf1fa1daSJoe Perches
958bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
959bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
960bf1fa1daSJoe Perches			if ($line =~ /\s/) {
96175ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
962bf1fa1daSJoe Perches				next;
963bf1fa1daSJoe Perches			}
964bf1fa1daSJoe Perches
965ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
96675ad8c57SJerome Forissier			$$wordsRef .= $line;
967bf1fa1daSJoe Perches		}
96875ad8c57SJerome Forissier		close($file);
96975ad8c57SJerome Forissier		return 1;
970bf1fa1daSJoe Perches	}
971bf1fa1daSJoe Perches
97275ad8c57SJerome Forissier	return 0;
97375ad8c57SJerome Forissier}
97475ad8c57SJerome Forissier
975ced69da1SQuentin Monnetmy $const_structs;
976ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
97775ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
97875ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
979ced69da1SQuentin Monnet}
98075ad8c57SJerome Forissier
981ced69da1SQuentin Monnetif (defined($typedefsfile)) {
982ced69da1SQuentin Monnet	my $typeOtherTypedefs;
98375ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
98475ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
985ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
98675ad8c57SJerome Forissier}
98775ad8c57SJerome Forissier
9888905a67cSAndy Whitcroftsub build_types {
989485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
990485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
9911813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
9928716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
993c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
994ab7e23f3SJoe Perches	$BasicType	= qr{
995ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
996ab7e23f3SJoe Perches				(?:${all}\b)
997ab7e23f3SJoe Perches		}x;
9988905a67cSAndy Whitcroft	$NonptrType	= qr{
999d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
1000cf655043SAndy Whitcroft			(?:
10016b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
10028ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
1003c45dcabdSAndy Whitcroft				(?:${all}\b)
1004cf655043SAndy Whitcroft			)
1005c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
10068905a67cSAndy Whitcroft		  }x;
10071813087dSJoe Perches	$NonptrTypeMisordered	= qr{
10081813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
10091813087dSJoe Perches			(?:
10101813087dSJoe Perches				(?:${Misordered}\b)
10111813087dSJoe Perches			)
10121813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
10131813087dSJoe Perches		  }x;
10148716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
10158716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
10168716de38SJoe Perches			(?:
10178716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
10188716de38SJoe Perches				(?:$typeTypedefs\b)|
10198716de38SJoe Perches				(?:${allWithAttr}\b)
10208716de38SJoe Perches			)
10218716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
10228716de38SJoe Perches		  }x;
10238905a67cSAndy Whitcroft	$Type	= qr{
1024c45dcabdSAndy Whitcroft			$NonptrType
10257b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
1026c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
10278905a67cSAndy Whitcroft		  }x;
10281813087dSJoe Perches	$TypeMisordered	= qr{
10291813087dSJoe Perches			$NonptrTypeMisordered
10307b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
10311813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
10321813087dSJoe Perches		  }x;
103391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
10341813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
10358905a67cSAndy Whitcroft}
10368905a67cSAndy Whitcroftbuild_types();
10376c72ffaaSAndy Whitcroft
10387d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
1039d1fe9c09SJoe Perches
1040d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1041d1fe9c09SJoe Perches# requires at least perl version v5.10.0
1042d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
1043d1fe9c09SJoe Perches
1044d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
10452435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
1046c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
10477d2367afSJoe Perches
1048f8422308SJoe Perchesour $declaration_macros = qr{(?x:
10493e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
1050fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
1051dcea7964SJoe Perches	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(|
1052dcea7964SJoe Perches	(?:$Storage\s+)?(?:XA_STATE|XA_STATE_ORDER)\s*\(
1053f8422308SJoe Perches)};
1054f8422308SJoe Perches
10558d0325ccSAditya Srivastavaour %allow_repeated_words = (
10568d0325ccSAditya Srivastava	add => '',
10578d0325ccSAditya Srivastava	added => '',
10588d0325ccSAditya Srivastava	bad => '',
10598d0325ccSAditya Srivastava	be => '',
10608d0325ccSAditya Srivastava);
10618d0325ccSAditya Srivastava
10627d2367afSJoe Perchessub deparenthesize {
10637d2367afSJoe Perches	my ($string) = @_;
10647d2367afSJoe Perches	return "" if (!defined($string));
10655b9553abSJoe Perches
10665b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
10675b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
10685b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
10695b9553abSJoe Perches	}
10705b9553abSJoe Perches
10717d2367afSJoe Perches	$string =~ s@\s+@ @g;
10725b9553abSJoe Perches
10737d2367afSJoe Perches	return $string;
10747d2367afSJoe Perches}
10757d2367afSJoe Perches
10763445686aSJoe Perchessub seed_camelcase_file {
10773445686aSJoe Perches	my ($file) = @_;
10783445686aSJoe Perches
10793445686aSJoe Perches	return if (!(-f $file));
10803445686aSJoe Perches
10813445686aSJoe Perches	local $/;
10823445686aSJoe Perches
10833445686aSJoe Perches	open(my $include_file, '<', "$file")
10843445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
10853445686aSJoe Perches	my $text = <$include_file>;
10863445686aSJoe Perches	close($include_file);
10873445686aSJoe Perches
10883445686aSJoe Perches	my @lines = split('\n', $text);
10893445686aSJoe Perches
10903445686aSJoe Perches	foreach my $line (@lines) {
10913445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
10923445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
10933445686aSJoe Perches			$camelcase{$1} = 1;
109411ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
109511ea516aSJoe Perches			$camelcase{$1} = 1;
109611ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
10973445686aSJoe Perches			$camelcase{$1} = 1;
10983445686aSJoe Perches		}
10993445686aSJoe Perches	}
11003445686aSJoe Perches}
11013445686aSJoe Perches
1102cd28b119SJoe Perchesour %maintained_status = ();
1103cd28b119SJoe Perches
110485b0ee18SJoe Perchessub is_maintained_obsolete {
110585b0ee18SJoe Perches	my ($filename) = @_;
110685b0ee18SJoe Perches
1107f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
110885b0ee18SJoe Perches
1109cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
1110cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1111cd28b119SJoe Perches	}
111285b0ee18SJoe Perches
1113cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
111485b0ee18SJoe Perches}
111585b0ee18SJoe Perches
11163b6e8ac9SJoe Perchessub is_SPDX_License_valid {
11173b6e8ac9SJoe Perches	my ($license) = @_;
11183b6e8ac9SJoe Perches
1119f9363b31SGuenter Roeck	return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
11203b6e8ac9SJoe Perches
112156294112SJoe Perches	my $root_path = abs_path($root);
1122f9363b31SGuenter Roeck	my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
11233b6e8ac9SJoe Perches	return 0 if ($status ne "");
11243b6e8ac9SJoe Perches	return 1;
11253b6e8ac9SJoe Perches}
11263b6e8ac9SJoe Perches
11273445686aSJoe Perchesmy $camelcase_seeded = 0;
11283445686aSJoe Perchessub seed_camelcase_includes {
11293445686aSJoe Perches	return if ($camelcase_seeded);
11303445686aSJoe Perches
11313445686aSJoe Perches	my $files;
1132c707a81dSJoe Perches	my $camelcase_cache = "";
1133c707a81dSJoe Perches	my @include_files = ();
1134c707a81dSJoe Perches
1135c707a81dSJoe Perches	$camelcase_seeded = 1;
1136351b2a1fSJoe Perches
11370f7f635bSJoe Perches	if (-e "$gitroot") {
1138dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
1139351b2a1fSJoe Perches		chomp $git_last_include_commit;
1140c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1141c707a81dSJoe Perches	} else {
1142c707a81dSJoe Perches		my $last_mod_date = 0;
1143c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
1144c707a81dSJoe Perches		@include_files = split('\n', $files);
1145c707a81dSJoe Perches		foreach my $file (@include_files) {
1146c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
1147c707a81dSJoe Perches						   localtime((stat $file)[9]));
1148c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
1149c707a81dSJoe Perches		}
1150c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1151c707a81dSJoe Perches	}
1152c707a81dSJoe Perches
1153c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
1154c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
1155c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
1156351b2a1fSJoe Perches		while (<$camelcase_file>) {
1157351b2a1fSJoe Perches			chomp;
1158351b2a1fSJoe Perches			$camelcase{$_} = 1;
1159351b2a1fSJoe Perches		}
1160351b2a1fSJoe Perches		close($camelcase_file);
1161351b2a1fSJoe Perches
1162351b2a1fSJoe Perches		return;
1163351b2a1fSJoe Perches	}
1164c707a81dSJoe Perches
11650f7f635bSJoe Perches	if (-e "$gitroot") {
1166dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
1167c707a81dSJoe Perches		@include_files = split('\n', $files);
11683445686aSJoe Perches	}
1169c707a81dSJoe Perches
11703445686aSJoe Perches	foreach my $file (@include_files) {
11713445686aSJoe Perches		seed_camelcase_file($file);
11723445686aSJoe Perches	}
1173351b2a1fSJoe Perches
1174c707a81dSJoe Perches	if ($camelcase_cache ne "") {
1175351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
1176c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
1177c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
1178351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1179351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
1180351b2a1fSJoe Perches		}
1181351b2a1fSJoe Perches		close($camelcase_file);
1182351b2a1fSJoe Perches	}
11833445686aSJoe Perches}
11843445686aSJoe Perches
1185f5f61325SJoe Perchessub git_is_single_file {
1186f5f61325SJoe Perches	my ($filename) = @_;
1187f5f61325SJoe Perches
1188f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1189f5f61325SJoe Perches
1190f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1191f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
1192f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
1193f5f61325SJoe Perches}
1194f5f61325SJoe Perches
1195d311cd44SJoe Perchessub git_commit_info {
1196d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
1197d311cd44SJoe Perches
11980f7f635bSJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1199d311cd44SJoe Perches
1200dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1201d311cd44SJoe Perches	$output =~ s/^\s*//gm;
1202d311cd44SJoe Perches	my @lines = split("\n", $output);
1203d311cd44SJoe Perches
12040d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
12050d7835fcSJoe Perches
12065a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1207d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1208d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1209d311cd44SJoe Perches#
1210d311cd44SJoe Perches#		echo "checking commits $1..."
1211d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1212d311cd44SJoe Perches#		while read line ; do
1213d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1214d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1215d311cd44SJoe Perches#		done
12164ce9f970SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
12174ce9f970SJoe Perches		 $lines[0] =~ /^fatal: bad object $commit/) {
1218948b133aSHeinrich Schuchardt		$id = undef;
1219d311cd44SJoe Perches	} else {
1220d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1221d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1222d311cd44SJoe Perches	}
1223d311cd44SJoe Perches
1224d311cd44SJoe Perches	return ($id, $desc);
1225d311cd44SJoe Perches}
1226d311cd44SJoe Perches
12276c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
12280a920b5bSAndy Whitcroft
122900df344fSAndy Whitcroftmy @rawlines = ();
1230c2fdda0dSAndy Whitcroftmy @lines = ();
12313705ce5bSJoe Perchesmy @fixed = ();
1232d752fcc8SJoe Perchesmy @fixed_inserted = ();
1233d752fcc8SJoe Perchesmy @fixed_deleted = ();
1234194f66fcSJoe Perchesmy $fixlinenr = -1;
1235194f66fcSJoe Perches
12364a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
12374a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
12380f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
12394a593c34SDu, Changbin
12404a593c34SDu, Changbinif ($git) {
12414a593c34SDu, Changbin	my @commits = ();
12420dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
12434a593c34SDu, Changbin		my $git_range;
124428898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
124528898fd1SJoe Perches			$git_range = "-$2 $1";
12464a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
12474a593c34SDu, Changbin			$git_range = "$commit_expr";
12484a593c34SDu, Changbin		} else {
12490dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
12500dea9f1eSJoe Perches		}
1251dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
12520dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
125328898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
125428898fd1SJoe Perches			next if (!defined($1) || !defined($2));
12550dea9f1eSJoe Perches			my $sha1 = $1;
12560dea9f1eSJoe Perches			my $subject = $2;
12570dea9f1eSJoe Perches			unshift(@commits, $sha1);
12580dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
12594a593c34SDu, Changbin		}
12604a593c34SDu, Changbin	}
12614a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
12624a593c34SDu, Changbin	@ARGV = @commits;
12634a593c34SDu, Changbin}
12644a593c34SDu, Changbin
1265c2fdda0dSAndy Whitcroftmy $vname;
126698005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
12676c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
126821caa13cSAndy Whitcroft	my $FILE;
1269f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1270f5f61325SJoe Perches	my $oldfile = $file;
1271f5f61325SJoe Perches	$file = 1 if ($is_git_file);
12724a593c34SDu, Changbin	if ($git) {
12734a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
12744a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
12754a593c34SDu, Changbin	} elsif ($file) {
127621caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
12776c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
127821caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
127921caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
12806c72ffaaSAndy Whitcroft	} else {
128121caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
12826c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
12836c72ffaaSAndy Whitcroft	}
1284c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1285c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
12864a593c34SDu, Changbin	} elsif ($git) {
12870dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1288c2fdda0dSAndy Whitcroft	} else {
1289c2fdda0dSAndy Whitcroft		$vname = $filename;
1290c2fdda0dSAndy Whitcroft	}
129121caa13cSAndy Whitcroft	while (<$FILE>) {
12920a920b5bSAndy Whitcroft		chomp;
129300df344fSAndy Whitcroft		push(@rawlines, $_);
1294c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
12956c72ffaaSAndy Whitcroft	}
129621caa13cSAndy Whitcroft	close($FILE);
1297d8469f16SJoe Perches
1298d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1299d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1300d8469f16SJoe Perches		print "$vname\n";
1301d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1302d8469f16SJoe Perches	}
1303d8469f16SJoe Perches
1304c2fdda0dSAndy Whitcroft	if (!process($filename)) {
13050a920b5bSAndy Whitcroft		$exit = 1;
13060a920b5bSAndy Whitcroft	}
130700df344fSAndy Whitcroft	@rawlines = ();
130813214adfSAndy Whitcroft	@lines = ();
13093705ce5bSJoe Perches	@fixed = ();
1310d752fcc8SJoe Perches	@fixed_inserted = ();
1311d752fcc8SJoe Perches	@fixed_deleted = ();
1312194f66fcSJoe Perches	$fixlinenr = -1;
1313485ff23eSAlex Dowad	@modifierListFile = ();
1314485ff23eSAlex Dowad	@typeListFile = ();
1315485ff23eSAlex Dowad	build_types();
1316f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
13170a920b5bSAndy Whitcroft}
13180a920b5bSAndy Whitcroft
1319d8469f16SJoe Perchesif (!$quiet) {
13203c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
13213c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
13223c816e49SJoe Perches
13235b57980dSJoe Perches	if (!$perl_version_ok) {
1324d8469f16SJoe Perches		print << "EOM"
1325d8469f16SJoe Perches
1326d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
13275b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1328d8469f16SJoe PerchesEOM
1329d8469f16SJoe Perches	}
1330d8469f16SJoe Perches	if ($exit) {
1331d8469f16SJoe Perches		print << "EOM"
1332d8469f16SJoe Perches
1333d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1334d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1335d8469f16SJoe PerchesEOM
1336d8469f16SJoe Perches	}
1337d8469f16SJoe Perches}
1338d8469f16SJoe Perches
13390a920b5bSAndy Whitcroftexit($exit);
13400a920b5bSAndy Whitcroft
13410a920b5bSAndy Whitcroftsub top_of_kernel_tree {
13426c72ffaaSAndy Whitcroft	my ($root) = @_;
13436c72ffaaSAndy Whitcroft
13446c72ffaaSAndy Whitcroft	my @tree_check = (
13456c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
13466c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
13476c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
13486c72ffaaSAndy Whitcroft	);
13496c72ffaaSAndy Whitcroft
13506c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
13516c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
13520a920b5bSAndy Whitcroft			return 0;
13530a920b5bSAndy Whitcroft		}
13546c72ffaaSAndy Whitcroft	}
13556c72ffaaSAndy Whitcroft	return 1;
13566c72ffaaSAndy Whitcroft}
13570a920b5bSAndy Whitcroft
135820112475SJoe Perchessub parse_email {
135920112475SJoe Perches	my ($formatted_email) = @_;
136020112475SJoe Perches
136120112475SJoe Perches	my $name = "";
1362fccaebf0SDwaipayan Ray	my $quoted = "";
1363dfa05c28SJoe Perches	my $name_comment = "";
136420112475SJoe Perches	my $address = "";
136520112475SJoe Perches	my $comment = "";
136620112475SJoe Perches
136720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
136820112475SJoe Perches		$name = $1;
136920112475SJoe Perches		$address = $2;
137020112475SJoe Perches		$comment = $3 if defined $3;
137120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
137220112475SJoe Perches		$address = $1;
137320112475SJoe Perches		$comment = $2 if defined $2;
137420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
137520112475SJoe Perches		$address = $1;
137620112475SJoe Perches		$comment = $2 if defined $2;
137785e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
137820112475SJoe Perches		$name = $formatted_email;
13793705ce5bSJoe Perches		$name = trim($name);
138020112475SJoe Perches		$name =~ s/^\"|\"$//g;
138120112475SJoe Perches		# If there's a name left after stripping spaces and
138220112475SJoe Perches		# leading quotes, and the address doesn't have both
138320112475SJoe Perches		# leading and trailing angle brackets, the address
138420112475SJoe Perches		# is invalid. ie:
138520112475SJoe Perches		#   "joe smith [email protected]" bad
138620112475SJoe Perches		#   "joe smith <[email protected]" bad
138720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
138820112475SJoe Perches			$name = "";
138920112475SJoe Perches			$address = "";
139020112475SJoe Perches			$comment = "";
139120112475SJoe Perches		}
139220112475SJoe Perches	}
139320112475SJoe Perches
1394fccaebf0SDwaipayan Ray	# Extract comments from names excluding quoted parts
1395fccaebf0SDwaipayan Ray	# "John D. (Doe)" - Do not extract
1396fccaebf0SDwaipayan Ray	if ($name =~ s/\"(.+)\"//) {
1397fccaebf0SDwaipayan Ray		$quoted = $1;
1398dfa05c28SJoe Perches	}
1399fccaebf0SDwaipayan Ray	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1400fccaebf0SDwaipayan Ray		$name_comment .= trim($1);
1401fccaebf0SDwaipayan Ray	}
1402fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
1403fccaebf0SDwaipayan Ray	$name = trim("$quoted $name");
1404fccaebf0SDwaipayan Ray
14053705ce5bSJoe Perches	$address = trim($address);
140620112475SJoe Perches	$address =~ s/^\<|\>$//g;
1407fccaebf0SDwaipayan Ray	$comment = trim($comment);
140820112475SJoe Perches
140920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
141020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
141120112475SJoe Perches		$name = "\"$name\"";
141220112475SJoe Perches	}
141320112475SJoe Perches
1414dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
141520112475SJoe Perches}
141620112475SJoe Perches
141720112475SJoe Perchessub format_email {
141848ca2d8aSDwaipayan Ray	my ($name, $name_comment, $address, $comment) = @_;
141920112475SJoe Perches
142020112475SJoe Perches	my $formatted_email;
142120112475SJoe Perches
1422fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
14233705ce5bSJoe Perches	$address = trim($address);
1424fccaebf0SDwaipayan Ray	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
142520112475SJoe Perches
142620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
142720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
142820112475SJoe Perches		$name = "\"$name\"";
142920112475SJoe Perches	}
143020112475SJoe Perches
1431fccaebf0SDwaipayan Ray	$name_comment = trim($name_comment);
1432fccaebf0SDwaipayan Ray	$name_comment = " $name_comment" if ($name_comment ne "");
1433fccaebf0SDwaipayan Ray	$comment = trim($comment);
1434fccaebf0SDwaipayan Ray	$comment = " $comment" if ($comment ne "");
1435fccaebf0SDwaipayan Ray
143620112475SJoe Perches	if ("$name" eq "") {
143720112475SJoe Perches		$formatted_email = "$address";
143820112475SJoe Perches	} else {
143948ca2d8aSDwaipayan Ray		$formatted_email = "$name$name_comment <$address>";
144020112475SJoe Perches	}
144148ca2d8aSDwaipayan Ray	$formatted_email .= "$comment";
144220112475SJoe Perches	return $formatted_email;
144320112475SJoe Perches}
144420112475SJoe Perches
1445dfa05c28SJoe Perchessub reformat_email {
1446dfa05c28SJoe Perches	my ($email) = @_;
1447dfa05c28SJoe Perches
1448dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
144948ca2d8aSDwaipayan Ray	return format_email($email_name, $name_comment, $email_address, $comment);
1450dfa05c28SJoe Perches}
1451dfa05c28SJoe Perches
1452dfa05c28SJoe Perchessub same_email_addresses {
1453fccaebf0SDwaipayan Ray	my ($email1, $email2) = @_;
1454dfa05c28SJoe Perches
1455dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1456dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1457dfa05c28SJoe Perches
145848ca2d8aSDwaipayan Ray	return $email1_name eq $email2_name &&
145948ca2d8aSDwaipayan Ray	       $email1_address eq $email2_address &&
146048ca2d8aSDwaipayan Ray	       $name1_comment eq $name2_comment &&
146148ca2d8aSDwaipayan Ray	       $comment1 eq $comment2;
146248ca2d8aSDwaipayan Ray}
1463dfa05c28SJoe Perches
1464d311cd44SJoe Perchessub which {
1465d311cd44SJoe Perches	my ($bin) = @_;
1466d311cd44SJoe Perches
1467d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1468d311cd44SJoe Perches		if (-e "$path/$bin") {
1469d311cd44SJoe Perches			return "$path/$bin";
1470d311cd44SJoe Perches		}
1471d311cd44SJoe Perches	}
1472d311cd44SJoe Perches
1473d311cd44SJoe Perches	return "";
1474d311cd44SJoe Perches}
1475d311cd44SJoe Perches
1476000d1cc1SJoe Perchessub which_conf {
1477000d1cc1SJoe Perches	my ($conf) = @_;
1478000d1cc1SJoe Perches
1479000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1480000d1cc1SJoe Perches		if (-e "$path/$conf") {
1481000d1cc1SJoe Perches			return "$path/$conf";
1482000d1cc1SJoe Perches		}
1483000d1cc1SJoe Perches	}
1484000d1cc1SJoe Perches
1485000d1cc1SJoe Perches	return "";
1486000d1cc1SJoe Perches}
1487000d1cc1SJoe Perches
14880a920b5bSAndy Whitcroftsub expand_tabs {
14890a920b5bSAndy Whitcroft	my ($str) = @_;
14900a920b5bSAndy Whitcroft
14910a920b5bSAndy Whitcroft	my $res = '';
14920a920b5bSAndy Whitcroft	my $n = 0;
14930a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
14940a920b5bSAndy Whitcroft		if ($c eq "\t") {
14950a920b5bSAndy Whitcroft			$res .= ' ';
14960a920b5bSAndy Whitcroft			$n++;
1497713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
14980a920b5bSAndy Whitcroft				$res .= ' ';
14990a920b5bSAndy Whitcroft			}
15000a920b5bSAndy Whitcroft			next;
15010a920b5bSAndy Whitcroft		}
15020a920b5bSAndy Whitcroft		$res .= $c;
15030a920b5bSAndy Whitcroft		$n++;
15040a920b5bSAndy Whitcroft	}
15050a920b5bSAndy Whitcroft
15060a920b5bSAndy Whitcroft	return $res;
15070a920b5bSAndy Whitcroft}
15086c72ffaaSAndy Whitcroftsub copy_spacing {
1509773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
15106c72ffaaSAndy Whitcroft	return $res;
15116c72ffaaSAndy Whitcroft}
15120a920b5bSAndy Whitcroft
15134a0df2efSAndy Whitcroftsub line_stats {
15144a0df2efSAndy Whitcroft	my ($line) = @_;
15154a0df2efSAndy Whitcroft
15164a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
15174a0df2efSAndy Whitcroft	$line =~ s/^.//;
15184a0df2efSAndy Whitcroft	$line = expand_tabs($line);
15194a0df2efSAndy Whitcroft
15204a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
15214a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
15224a0df2efSAndy Whitcroft
15234a0df2efSAndy Whitcroft	return (length($line), length($white));
15244a0df2efSAndy Whitcroft}
15254a0df2efSAndy Whitcroft
1526773647a0SAndy Whitcroftmy $sanitise_quote = '';
1527773647a0SAndy Whitcroft
1528773647a0SAndy Whitcroftsub sanitise_line_reset {
1529773647a0SAndy Whitcroft	my ($in_comment) = @_;
1530773647a0SAndy Whitcroft
1531773647a0SAndy Whitcroft	if ($in_comment) {
1532773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1533773647a0SAndy Whitcroft	} else {
1534773647a0SAndy Whitcroft		$sanitise_quote = '';
1535773647a0SAndy Whitcroft	}
1536773647a0SAndy Whitcroft}
153700df344fSAndy Whitcroftsub sanitise_line {
153800df344fSAndy Whitcroft	my ($line) = @_;
153900df344fSAndy Whitcroft
154000df344fSAndy Whitcroft	my $res = '';
154100df344fSAndy Whitcroft	my $l = '';
154200df344fSAndy Whitcroft
1543c2fdda0dSAndy Whitcroft	my $qlen = 0;
1544773647a0SAndy Whitcroft	my $off = 0;
1545773647a0SAndy Whitcroft	my $c;
154600df344fSAndy Whitcroft
1547773647a0SAndy Whitcroft	# Always copy over the diff marker.
1548773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1549773647a0SAndy Whitcroft
1550773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1551773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1552773647a0SAndy Whitcroft
15538d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1554773647a0SAndy Whitcroft		# and end, all to $;.
1555773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1556773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1557773647a0SAndy Whitcroft
1558773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1559773647a0SAndy Whitcroft			$off++;
156000df344fSAndy Whitcroft			next;
1561773647a0SAndy Whitcroft		}
156281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1563773647a0SAndy Whitcroft			$sanitise_quote = '';
1564773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1565773647a0SAndy Whitcroft			$off++;
1566773647a0SAndy Whitcroft			next;
1567773647a0SAndy Whitcroft		}
1568113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1569113f04a8SDaniel Walker			$sanitise_quote = '//';
1570113f04a8SDaniel Walker
1571113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1572113f04a8SDaniel Walker			$off++;
1573113f04a8SDaniel Walker			next;
1574113f04a8SDaniel Walker		}
1575773647a0SAndy Whitcroft
1576773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1577773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1578773647a0SAndy Whitcroft		    $c eq "\\") {
1579773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1580773647a0SAndy Whitcroft			$off++;
1581773647a0SAndy Whitcroft			next;
1582773647a0SAndy Whitcroft		}
1583773647a0SAndy Whitcroft		# Regular quotes.
1584773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1585773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1586773647a0SAndy Whitcroft				$sanitise_quote = $c;
1587773647a0SAndy Whitcroft
1588773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1589773647a0SAndy Whitcroft				next;
1590773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1591773647a0SAndy Whitcroft				$sanitise_quote = '';
159200df344fSAndy Whitcroft			}
159300df344fSAndy Whitcroft		}
1594773647a0SAndy Whitcroft
1595fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1596773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1597773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1598113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1599113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1600773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1601773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
160200df344fSAndy Whitcroft		} else {
1603773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
160400df344fSAndy Whitcroft		}
1605c2fdda0dSAndy Whitcroft	}
1606c2fdda0dSAndy Whitcroft
1607113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1608113f04a8SDaniel Walker		$sanitise_quote = '';
1609113f04a8SDaniel Walker	}
1610113f04a8SDaniel Walker
1611c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1612c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1613c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1614c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1615c2fdda0dSAndy Whitcroft
1616c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1617c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1618c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1619c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1620c2fdda0dSAndy Whitcroft	}
1621c2fdda0dSAndy Whitcroft
1622dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1623dadf680dSJoe Perches		my $match = $1;
1624dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1625dadf680dSJoe Perches	}
1626dadf680dSJoe Perches
162700df344fSAndy Whitcroft	return $res;
162800df344fSAndy Whitcroft}
162900df344fSAndy Whitcroft
1630a6962d72SJoe Perchessub get_quoted_string {
1631a6962d72SJoe Perches	my ($line, $rawline) = @_;
1632a6962d72SJoe Perches
1633478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
163433acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1635a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1636a6962d72SJoe Perches}
1637a6962d72SJoe Perches
16388905a67cSAndy Whitcroftsub ctx_statement_block {
16398905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16408905a67cSAndy Whitcroft	my $line = $linenr - 1;
16418905a67cSAndy Whitcroft	my $blk = '';
16428905a67cSAndy Whitcroft	my $soff = $off;
16438905a67cSAndy Whitcroft	my $coff = $off - 1;
1644773647a0SAndy Whitcroft	my $coff_set = 0;
16458905a67cSAndy Whitcroft
164613214adfSAndy Whitcroft	my $loff = 0;
164713214adfSAndy Whitcroft
16488905a67cSAndy Whitcroft	my $type = '';
16498905a67cSAndy Whitcroft	my $level = 0;
1650a2750645SAndy Whitcroft	my @stack = ();
1651cf655043SAndy Whitcroft	my $p;
16528905a67cSAndy Whitcroft	my $c;
16538905a67cSAndy Whitcroft	my $len = 0;
165413214adfSAndy Whitcroft
165513214adfSAndy Whitcroft	my $remainder;
16568905a67cSAndy Whitcroft	while (1) {
1657a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1658a2750645SAndy Whitcroft
1659773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
16608905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
16618905a67cSAndy Whitcroft		# context.
16628905a67cSAndy Whitcroft		if ($off >= $len) {
16638905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1664dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1665c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
16668905a67cSAndy Whitcroft				$remain--;
166713214adfSAndy Whitcroft				$loff = $len;
1668c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
16698905a67cSAndy Whitcroft				$len = length($blk);
16708905a67cSAndy Whitcroft				$line++;
16718905a67cSAndy Whitcroft				last;
16728905a67cSAndy Whitcroft			}
16738905a67cSAndy Whitcroft			# Bail if there is no further context.
16748905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
167513214adfSAndy Whitcroft			if ($off >= $len) {
16768905a67cSAndy Whitcroft				last;
16778905a67cSAndy Whitcroft			}
1678f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1679f74bd194SAndy Whitcroft				$level++;
1680f74bd194SAndy Whitcroft				$type = '#';
1681f74bd194SAndy Whitcroft			}
16828905a67cSAndy Whitcroft		}
1683cf655043SAndy Whitcroft		$p = $c;
16848905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
168513214adfSAndy Whitcroft		$remainder = substr($blk, $off);
16868905a67cSAndy Whitcroft
1687773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
16884635f4fbSAndy Whitcroft
16894635f4fbSAndy Whitcroft		# Handle nested #if/#else.
16904635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
16914635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
16924635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
16934635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
16944635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
16954635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
16964635f4fbSAndy Whitcroft		}
16974635f4fbSAndy Whitcroft
16988905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
16998905a67cSAndy Whitcroft		# outermost level.
17008905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
17018905a67cSAndy Whitcroft			last;
17028905a67cSAndy Whitcroft		}
17038905a67cSAndy Whitcroft
170413214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1705773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1706773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1707773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1708773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1709773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1710773647a0SAndy Whitcroft			$coff_set = 1;
1711773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1712773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
171313214adfSAndy Whitcroft		}
171413214adfSAndy Whitcroft
17158905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
17168905a67cSAndy Whitcroft			$level++;
17178905a67cSAndy Whitcroft			$type = '(';
17188905a67cSAndy Whitcroft		}
17198905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
17208905a67cSAndy Whitcroft			$level--;
17218905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
17228905a67cSAndy Whitcroft
17238905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
17248905a67cSAndy Whitcroft				$coff = $off;
1725773647a0SAndy Whitcroft				$coff_set = 1;
1726773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
17278905a67cSAndy Whitcroft			}
17288905a67cSAndy Whitcroft		}
17298905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
17308905a67cSAndy Whitcroft			$level++;
17318905a67cSAndy Whitcroft			$type = '{';
17328905a67cSAndy Whitcroft		}
17338905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
17348905a67cSAndy Whitcroft			$level--;
17358905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
17368905a67cSAndy Whitcroft
17378905a67cSAndy Whitcroft			if ($level == 0) {
1738b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1739b998e001SPatrick Pannuto					$off++;
1740b998e001SPatrick Pannuto				}
17418905a67cSAndy Whitcroft				last;
17428905a67cSAndy Whitcroft			}
17438905a67cSAndy Whitcroft		}
1744f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1745f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1746f74bd194SAndy Whitcroft			$level--;
1747f74bd194SAndy Whitcroft			$type = '';
1748f74bd194SAndy Whitcroft			$off++;
1749f74bd194SAndy Whitcroft			last;
1750f74bd194SAndy Whitcroft		}
17518905a67cSAndy Whitcroft		$off++;
17528905a67cSAndy Whitcroft	}
1753a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
175413214adfSAndy Whitcroft	if ($off == $len) {
1755a3bb97a7SAndy Whitcroft		$loff = $len + 1;
175613214adfSAndy Whitcroft		$line++;
175713214adfSAndy Whitcroft		$remain--;
175813214adfSAndy Whitcroft	}
17598905a67cSAndy Whitcroft
17608905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
17618905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
17628905a67cSAndy Whitcroft
17638905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
17648905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
17658905a67cSAndy Whitcroft
1766773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
176713214adfSAndy Whitcroft
176813214adfSAndy Whitcroft	return ($statement, $condition,
176913214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
177013214adfSAndy Whitcroft}
177113214adfSAndy Whitcroft
1772cf655043SAndy Whitcroftsub statement_lines {
1773cf655043SAndy Whitcroft	my ($stmt) = @_;
1774cf655043SAndy Whitcroft
1775cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1776cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1777cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1778cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1779cf655043SAndy Whitcroft
1780cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1781cf655043SAndy Whitcroft
1782cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1783cf655043SAndy Whitcroft}
1784cf655043SAndy Whitcroft
1785cf655043SAndy Whitcroftsub statement_rawlines {
1786cf655043SAndy Whitcroft	my ($stmt) = @_;
1787cf655043SAndy Whitcroft
1788cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1789cf655043SAndy Whitcroft
1790cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1791cf655043SAndy Whitcroft}
1792cf655043SAndy Whitcroft
1793cf655043SAndy Whitcroftsub statement_block_size {
1794cf655043SAndy Whitcroft	my ($stmt) = @_;
1795cf655043SAndy Whitcroft
1796cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1797cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1798cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1799cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1800cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1801cf655043SAndy Whitcroft
1802cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1803cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1804cf655043SAndy Whitcroft
1805cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1806cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1807cf655043SAndy Whitcroft
1808cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1809cf655043SAndy Whitcroft		return $stmt_lines;
1810cf655043SAndy Whitcroft	} else {
1811cf655043SAndy Whitcroft		return $stmt_statements;
1812cf655043SAndy Whitcroft	}
1813cf655043SAndy Whitcroft}
1814cf655043SAndy Whitcroft
181513214adfSAndy Whitcroftsub ctx_statement_full {
181613214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
181713214adfSAndy Whitcroft	my ($statement, $condition, $level);
181813214adfSAndy Whitcroft
181913214adfSAndy Whitcroft	my (@chunks);
182013214adfSAndy Whitcroft
1821cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
182213214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
182313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1824773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
182513214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1826cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1827cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1828cf655043SAndy Whitcroft	}
1829cf655043SAndy Whitcroft
1830cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1831cf655043SAndy Whitcroft	# could continue the statement.
1832cf655043SAndy Whitcroft	for (;;) {
183313214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
183413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1835cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1836773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1837cf655043SAndy Whitcroft		#print "C: push\n";
1838cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
183913214adfSAndy Whitcroft	}
184013214adfSAndy Whitcroft
184113214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
18428905a67cSAndy Whitcroft}
18438905a67cSAndy Whitcroft
18444a0df2efSAndy Whitcroftsub ctx_block_get {
1845f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
18464a0df2efSAndy Whitcroft	my $line;
18474a0df2efSAndy Whitcroft	my $start = $linenr - 1;
18484a0df2efSAndy Whitcroft	my $blk = '';
18494a0df2efSAndy Whitcroft	my @o;
18504a0df2efSAndy Whitcroft	my @c;
18514a0df2efSAndy Whitcroft	my @res = ();
18524a0df2efSAndy Whitcroft
1853f0a594c1SAndy Whitcroft	my $level = 0;
18544635f4fbSAndy Whitcroft	my @stack = ($level);
185500df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
185600df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
185700df344fSAndy Whitcroft		$remain--;
185800df344fSAndy Whitcroft
185900df344fSAndy Whitcroft		$blk .= $rawlines[$line];
18604635f4fbSAndy Whitcroft
18614635f4fbSAndy Whitcroft		# Handle nested #if/#else.
186201464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
18634635f4fbSAndy Whitcroft			push(@stack, $level);
186401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
18654635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
186601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
18674635f4fbSAndy Whitcroft			$level = pop(@stack);
18684635f4fbSAndy Whitcroft		}
18694635f4fbSAndy Whitcroft
187001464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1871f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1872f0a594c1SAndy Whitcroft			if ($off > 0) {
1873f0a594c1SAndy Whitcroft				$off--;
1874f0a594c1SAndy Whitcroft				next;
1875f0a594c1SAndy Whitcroft			}
18764a0df2efSAndy Whitcroft
1877f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1878f0a594c1SAndy Whitcroft				$level--;
1879f0a594c1SAndy Whitcroft				last if ($level == 0);
1880f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1881f0a594c1SAndy Whitcroft				$level++;
1882f0a594c1SAndy Whitcroft			}
1883f0a594c1SAndy Whitcroft		}
18844a0df2efSAndy Whitcroft
1885f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
188600df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
18874a0df2efSAndy Whitcroft		}
18884a0df2efSAndy Whitcroft
1889f0a594c1SAndy Whitcroft		last if ($level == 0);
18904a0df2efSAndy Whitcroft	}
18914a0df2efSAndy Whitcroft
1892f0a594c1SAndy Whitcroft	return ($level, @res);
18934a0df2efSAndy Whitcroft}
18944a0df2efSAndy Whitcroftsub ctx_block_outer {
18954a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
18964a0df2efSAndy Whitcroft
1897f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1898f0a594c1SAndy Whitcroft	return @r;
18994a0df2efSAndy Whitcroft}
19004a0df2efSAndy Whitcroftsub ctx_block {
19014a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
19024a0df2efSAndy Whitcroft
1903f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1904f0a594c1SAndy Whitcroft	return @r;
1905653d4876SAndy Whitcroft}
1906653d4876SAndy Whitcroftsub ctx_statement {
1907f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1908f0a594c1SAndy Whitcroft
1909f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1910f0a594c1SAndy Whitcroft	return @r;
1911f0a594c1SAndy Whitcroft}
1912f0a594c1SAndy Whitcroftsub ctx_block_level {
1913653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1914653d4876SAndy Whitcroft
1915f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
19164a0df2efSAndy Whitcroft}
19179c0ca6f9SAndy Whitcroftsub ctx_statement_level {
19189c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
19199c0ca6f9SAndy Whitcroft
19209c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
19219c0ca6f9SAndy Whitcroft}
19224a0df2efSAndy Whitcroft
19234a0df2efSAndy Whitcroftsub ctx_locate_comment {
19244a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
19254a0df2efSAndy Whitcroft
1926a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1927a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1928a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1929a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1930a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1931a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1932a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1933a55ee0ccSJoe Perches
19344a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1935a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
19364a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
19374a0df2efSAndy Whitcroft
19384a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
19394a0df2efSAndy Whitcroft	# comment.
19404a0df2efSAndy Whitcroft	my $in_comment = 0;
19414a0df2efSAndy Whitcroft	$current_comment = '';
19424a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
194300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
194400df344fSAndy Whitcroft		#warn "           $line\n";
19454a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
19464a0df2efSAndy Whitcroft			$in_comment = 1;
19474a0df2efSAndy Whitcroft		}
19484a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
19494a0df2efSAndy Whitcroft			$in_comment = 1;
19504a0df2efSAndy Whitcroft		}
19514a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
19524a0df2efSAndy Whitcroft			$current_comment = '';
19534a0df2efSAndy Whitcroft		}
19544a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
19554a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
19564a0df2efSAndy Whitcroft			$in_comment = 0;
19574a0df2efSAndy Whitcroft		}
19584a0df2efSAndy Whitcroft	}
19594a0df2efSAndy Whitcroft
19604a0df2efSAndy Whitcroft	chomp($current_comment);
19614a0df2efSAndy Whitcroft	return($current_comment);
19624a0df2efSAndy Whitcroft}
19634a0df2efSAndy Whitcroftsub ctx_has_comment {
19644a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
19654a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
19664a0df2efSAndy Whitcroft
196700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
19684a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
19694a0df2efSAndy Whitcroft
19704a0df2efSAndy Whitcroft	return ($cmt ne '');
19714a0df2efSAndy Whitcroft}
19724a0df2efSAndy Whitcroft
19734d001e4dSAndy Whitcroftsub raw_line {
19744d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
19754d001e4dSAndy Whitcroft
19764d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
19774d001e4dSAndy Whitcroft	$cnt++;
19784d001e4dSAndy Whitcroft
19794d001e4dSAndy Whitcroft	my $line;
19804d001e4dSAndy Whitcroft	while ($cnt) {
19814d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
19824d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
19834d001e4dSAndy Whitcroft		$cnt--;
19844d001e4dSAndy Whitcroft	}
19854d001e4dSAndy Whitcroft
19864d001e4dSAndy Whitcroft	return $line;
19874d001e4dSAndy Whitcroft}
19884d001e4dSAndy Whitcroft
19892a9f9d85STobin C. Hardingsub get_stat_real {
19902a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
19912a9f9d85STobin C. Harding
19922a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
19932a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
19942a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
19952a9f9d85STobin C. Harding	}
19962a9f9d85STobin C. Harding
19972a9f9d85STobin C. Harding	return $stat_real;
19982a9f9d85STobin C. Harding}
19992a9f9d85STobin C. Harding
2000e3d95a2aSTobin C. Hardingsub get_stat_here {
2001e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
2002e3d95a2aSTobin C. Harding
2003e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
2004e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
2005e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
2006e3d95a2aSTobin C. Harding	}
2007e3d95a2aSTobin C. Harding
2008e3d95a2aSTobin C. Harding	return $herectx;
2009e3d95a2aSTobin C. Harding}
2010e3d95a2aSTobin C. Harding
20110a920b5bSAndy Whitcroftsub cat_vet {
20120a920b5bSAndy Whitcroft	my ($vet) = @_;
20139c0ca6f9SAndy Whitcroft	my ($res, $coded);
20140a920b5bSAndy Whitcroft
20159c0ca6f9SAndy Whitcroft	$res = '';
20166c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
20176c72ffaaSAndy Whitcroft		$res .= $1;
20186c72ffaaSAndy Whitcroft		if ($2 ne '') {
20199c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
20206c72ffaaSAndy Whitcroft			$res .= $coded;
20216c72ffaaSAndy Whitcroft		}
20229c0ca6f9SAndy Whitcroft	}
20239c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
20240a920b5bSAndy Whitcroft
20259c0ca6f9SAndy Whitcroft	return $res;
20260a920b5bSAndy Whitcroft}
20270a920b5bSAndy Whitcroft
2028c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
2029cf655043SAndy Whitcroftmy $av_pending;
2030c2fdda0dSAndy Whitcroftmy @av_paren_type;
20311f65f947SAndy Whitcroftmy $av_pend_colon;
2032c2fdda0dSAndy Whitcroft
2033c2fdda0dSAndy Whitcroftsub annotate_reset {
2034c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
2035cf655043SAndy Whitcroft	$av_pending = '_';
2036cf655043SAndy Whitcroft	@av_paren_type = ('E');
20371f65f947SAndy Whitcroft	$av_pend_colon = 'O';
2038c2fdda0dSAndy Whitcroft}
2039c2fdda0dSAndy Whitcroft
20406c72ffaaSAndy Whitcroftsub annotate_values {
20416c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
20426c72ffaaSAndy Whitcroft
20436c72ffaaSAndy Whitcroft	my $res;
20441f65f947SAndy Whitcroft	my $var = '_' x length($stream);
20456c72ffaaSAndy Whitcroft	my $cur = $stream;
20466c72ffaaSAndy Whitcroft
2047c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
20486c72ffaaSAndy Whitcroft
20496c72ffaaSAndy Whitcroft	while (length($cur)) {
2050773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
2051cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
2052171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
20536c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
2054c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
2055c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
2056cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
2057c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
20586c72ffaaSAndy Whitcroft			}
20596c72ffaaSAndy Whitcroft
2060c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
20619446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
20629446ef56SAndy Whitcroft			push(@av_paren_type, $type);
2063addcdceaSAndy Whitcroft			$type = 'c';
20649446ef56SAndy Whitcroft
2065e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
2066c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
20676c72ffaaSAndy Whitcroft			$type = 'T';
20686c72ffaaSAndy Whitcroft
2069389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
2070389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
2071389a2fe5SAndy Whitcroft			$type = 'T';
2072389a2fe5SAndy Whitcroft
2073c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
2074171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
2075c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
2076171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
2077171ae1a4SAndy Whitcroft			if ($2 ne '') {
2078cf655043SAndy Whitcroft				$av_pending = 'N';
2079171ae1a4SAndy Whitcroft			}
2080171ae1a4SAndy Whitcroft			$type = 'E';
2081171ae1a4SAndy Whitcroft
2082c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
2083171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
2084171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
2085171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
20866c72ffaaSAndy Whitcroft
2087c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
2088cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
2089c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
2090cf655043SAndy Whitcroft
2091cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2092cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2093171ae1a4SAndy Whitcroft			$type = 'E';
2094cf655043SAndy Whitcroft
2095c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
2096cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2097cf655043SAndy Whitcroft			$av_preprocessor = 1;
2098cf655043SAndy Whitcroft
2099cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2100cf655043SAndy Whitcroft
2101171ae1a4SAndy Whitcroft			$type = 'E';
2102cf655043SAndy Whitcroft
2103c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
2104cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
2105cf655043SAndy Whitcroft
2106cf655043SAndy Whitcroft			$av_preprocessor = 1;
2107cf655043SAndy Whitcroft
2108cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
2109cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
2110cf655043SAndy Whitcroft			pop(@av_paren_type);
2111cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2112171ae1a4SAndy Whitcroft			$type = 'E';
21136c72ffaaSAndy Whitcroft
21146c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
2115c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
21166c72ffaaSAndy Whitcroft
2117171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2118171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
2119171ae1a4SAndy Whitcroft			$av_pending = $type;
2120171ae1a4SAndy Whitcroft			$type = 'N';
2121171ae1a4SAndy Whitcroft
21226c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
2123c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
21246c72ffaaSAndy Whitcroft			if (defined $2) {
2125cf655043SAndy Whitcroft				$av_pending = 'V';
21266c72ffaaSAndy Whitcroft			}
21276c72ffaaSAndy Whitcroft			$type = 'N';
21286c72ffaaSAndy Whitcroft
212914b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
2130c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
213114b111c1SAndy Whitcroft			$av_pending = 'E';
21326c72ffaaSAndy Whitcroft			$type = 'N';
21336c72ffaaSAndy Whitcroft
21341f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
21351f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
21361f65f947SAndy Whitcroft			$av_pend_colon = 'C';
21371f65f947SAndy Whitcroft			$type = 'N';
21381f65f947SAndy Whitcroft
213914b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
2140c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
21416c72ffaaSAndy Whitcroft			$type = 'N';
21426c72ffaaSAndy Whitcroft
21436c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
2144c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
2145cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
2146cf655043SAndy Whitcroft			$av_pending = '_';
21476c72ffaaSAndy Whitcroft			$type = 'N';
21486c72ffaaSAndy Whitcroft
21496c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
2150cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
2151cf655043SAndy Whitcroft			if ($new_type ne '_') {
2152cf655043SAndy Whitcroft				$type = $new_type;
2153c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
2154c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
21556c72ffaaSAndy Whitcroft			} else {
2156c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
21576c72ffaaSAndy Whitcroft			}
21586c72ffaaSAndy Whitcroft
2159c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
2160c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
2161c8cb2ca3SAndy Whitcroft			$type = 'V';
2162cf655043SAndy Whitcroft			$av_pending = 'V';
21636c72ffaaSAndy Whitcroft
21648e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
21658e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
21661f65f947SAndy Whitcroft				$av_pend_colon = 'B';
21678e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
21688e761b04SAndy Whitcroft				$av_pend_colon = 'L';
21691f65f947SAndy Whitcroft			}
21701f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
21711f65f947SAndy Whitcroft			$type = 'V';
21721f65f947SAndy Whitcroft
21736c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
2174c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
21756c72ffaaSAndy Whitcroft			$type = 'V';
21766c72ffaaSAndy Whitcroft
21776c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
2178c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
21796c72ffaaSAndy Whitcroft			$type = 'N';
21806c72ffaaSAndy Whitcroft
2181cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
2182c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
218313214adfSAndy Whitcroft			$type = 'E';
21841f65f947SAndy Whitcroft			$av_pend_colon = 'O';
218513214adfSAndy Whitcroft
21868e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
21878e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
21888e761b04SAndy Whitcroft			$type = 'C';
21898e761b04SAndy Whitcroft
21901f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
21911f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
21921f65f947SAndy Whitcroft			$type = 'N';
21931f65f947SAndy Whitcroft
21941f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
21951f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
21961f65f947SAndy Whitcroft
21971f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
21981f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
21991f65f947SAndy Whitcroft				$type = 'E';
22001f65f947SAndy Whitcroft			} else {
22011f65f947SAndy Whitcroft				$type = 'N';
22021f65f947SAndy Whitcroft			}
22031f65f947SAndy Whitcroft			$av_pend_colon = 'O';
22041f65f947SAndy Whitcroft
22058e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
220613214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
22076c72ffaaSAndy Whitcroft			$type = 'N';
22086c72ffaaSAndy Whitcroft
22090d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
221074048ed8SAndy Whitcroft			my $variant;
221174048ed8SAndy Whitcroft
221274048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
221374048ed8SAndy Whitcroft			if ($type eq 'V') {
221474048ed8SAndy Whitcroft				$variant = 'B';
221574048ed8SAndy Whitcroft			} else {
221674048ed8SAndy Whitcroft				$variant = 'U';
221774048ed8SAndy Whitcroft			}
221874048ed8SAndy Whitcroft
221974048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
222074048ed8SAndy Whitcroft			$type = 'N';
222174048ed8SAndy Whitcroft
22226c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2223c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
22246c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
22256c72ffaaSAndy Whitcroft				$type = 'N';
22266c72ffaaSAndy Whitcroft			}
22276c72ffaaSAndy Whitcroft
22286c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2229c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
22306c72ffaaSAndy Whitcroft		}
22316c72ffaaSAndy Whitcroft		if (defined $1) {
22326c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
22336c72ffaaSAndy Whitcroft			$res .= $type x length($1);
22346c72ffaaSAndy Whitcroft		}
22356c72ffaaSAndy Whitcroft	}
22366c72ffaaSAndy Whitcroft
22371f65f947SAndy Whitcroft	return ($res, $var);
22386c72ffaaSAndy Whitcroft}
22396c72ffaaSAndy Whitcroft
22408905a67cSAndy Whitcroftsub possible {
224113214adfSAndy Whitcroft	my ($possible, $line) = @_;
22429a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
22430776e594SAndy Whitcroft		^(?:
22440776e594SAndy Whitcroft			$Modifier|
22450776e594SAndy Whitcroft			$Storage|
22460776e594SAndy Whitcroft			$Type|
22479a974fdbSAndy Whitcroft			DEFINE_\S+
22489a974fdbSAndy Whitcroft		)$|
22499a974fdbSAndy Whitcroft		^(?:
22500776e594SAndy Whitcroft			goto|
22510776e594SAndy Whitcroft			return|
22520776e594SAndy Whitcroft			case|
22530776e594SAndy Whitcroft			else|
22540776e594SAndy Whitcroft			asm|__asm__|
225589a88353SAndy Whitcroft			do|
225689a88353SAndy Whitcroft			\#|
225789a88353SAndy Whitcroft			\#\#|
22589a974fdbSAndy Whitcroft		)(?:\s|$)|
22590776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
22609a974fdbSAndy Whitcroft	    )}x;
22619a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
22629a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2263c45dcabdSAndy Whitcroft		# Check for modifiers.
2264c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2265c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2266c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2267c45dcabdSAndy Whitcroft
2268c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2269c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2270d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
22719a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2272d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2273485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2274d2506586SAndy Whitcroft				}
22759a974fdbSAndy Whitcroft			}
2276c45dcabdSAndy Whitcroft
2277c45dcabdSAndy Whitcroft		} else {
227813214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2279485ff23eSAlex Dowad			push(@typeListFile, $possible);
2280c45dcabdSAndy Whitcroft		}
22818905a67cSAndy Whitcroft		build_types();
22820776e594SAndy Whitcroft	} else {
22830776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
22848905a67cSAndy Whitcroft	}
22858905a67cSAndy Whitcroft}
22868905a67cSAndy Whitcroft
22876c72ffaaSAndy Whitcroftmy $prefix = '';
22886c72ffaaSAndy Whitcroft
2289000d1cc1SJoe Perchessub show_type {
2290cbec18afSJoe Perches	my ($type) = @_;
229191bfe484SJoe Perches
2292522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2293522b837cSAlexey Dobriyan
2294cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2295cbec18afSJoe Perches
2296cbec18afSJoe Perches	return !defined $ignore_type{$type};
2297000d1cc1SJoe Perches}
2298000d1cc1SJoe Perches
2299f0a594c1SAndy Whitcroftsub report {
2300cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2301cbec18afSJoe Perches
2302cbec18afSJoe Perches	if (!show_type($type) ||
2303cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2304773647a0SAndy Whitcroft		return 0;
2305773647a0SAndy Whitcroft	}
230657230297SJoe Perches	my $output = '';
2307737c0767SJohn Brooks	if ($color) {
230857230297SJoe Perches		if ($level eq 'ERROR') {
230957230297SJoe Perches			$output .= RED;
231057230297SJoe Perches		} elsif ($level eq 'WARNING') {
231157230297SJoe Perches			$output .= YELLOW;
2312000d1cc1SJoe Perches		} else {
231357230297SJoe Perches			$output .= GREEN;
2314000d1cc1SJoe Perches		}
231557230297SJoe Perches	}
231657230297SJoe Perches	$output .= $prefix . $level . ':';
231757230297SJoe Perches	if ($show_types) {
2318737c0767SJohn Brooks		$output .= BLUE if ($color);
231957230297SJoe Perches		$output .= "$type:";
232057230297SJoe Perches	}
2321737c0767SJohn Brooks	$output .= RESET if ($color);
232257230297SJoe Perches	$output .= ' ' . $msg . "\n";
232334d8815fSJoe Perches
232434d8815fSJoe Perches	if ($showfile) {
232534d8815fSJoe Perches		my @lines = split("\n", $output, -1);
232634d8815fSJoe Perches		splice(@lines, 1, 1);
232734d8815fSJoe Perches		$output = join("\n", @lines);
232834d8815fSJoe Perches	}
232952178ce0SDwaipayan Ray
233052178ce0SDwaipayan Ray	if ($terse) {
233152178ce0SDwaipayan Ray		$output = (split('\n', $output))[0] . "\n";
233252178ce0SDwaipayan Ray	}
233352178ce0SDwaipayan Ray
233452178ce0SDwaipayan Ray	if ($verbose && exists($verbose_messages{$type}) &&
233552178ce0SDwaipayan Ray	    !exists($verbose_emitted{$type})) {
233652178ce0SDwaipayan Ray		$output .= $verbose_messages{$type} . "\n\n";
233752178ce0SDwaipayan Ray		$verbose_emitted{$type} = 1;
233852178ce0SDwaipayan Ray	}
23398905a67cSAndy Whitcroft
234057230297SJoe Perches	push(our @report, $output);
2341773647a0SAndy Whitcroft
2342773647a0SAndy Whitcroft	return 1;
2343f0a594c1SAndy Whitcroft}
2344cbec18afSJoe Perches
2345f0a594c1SAndy Whitcroftsub report_dump {
234613214adfSAndy Whitcroft	our @report;
2347f0a594c1SAndy Whitcroft}
2348000d1cc1SJoe Perches
2349d752fcc8SJoe Perchessub fixup_current_range {
2350d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2351d752fcc8SJoe Perches
2352d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2353d752fcc8SJoe Perches		my $o = $1;
2354d752fcc8SJoe Perches		my $l = $2;
2355d752fcc8SJoe Perches		my $no = $o + $offset;
2356d752fcc8SJoe Perches		my $nl = $l + $length;
2357d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2358d752fcc8SJoe Perches	}
2359d752fcc8SJoe Perches}
2360d752fcc8SJoe Perches
2361d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2362d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2363d752fcc8SJoe Perches
2364d752fcc8SJoe Perches	my $range_last_linenr = 0;
2365d752fcc8SJoe Perches	my $delta_offset = 0;
2366d752fcc8SJoe Perches
2367d752fcc8SJoe Perches	my $old_linenr = 0;
2368d752fcc8SJoe Perches	my $new_linenr = 0;
2369d752fcc8SJoe Perches
2370d752fcc8SJoe Perches	my $next_insert = 0;
2371d752fcc8SJoe Perches	my $next_delete = 0;
2372d752fcc8SJoe Perches
2373d752fcc8SJoe Perches	my @lines = ();
2374d752fcc8SJoe Perches
2375d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2376d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2377d752fcc8SJoe Perches
2378d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2379d752fcc8SJoe Perches		my $save_line = 1;
2380d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2381323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2382d752fcc8SJoe Perches			$delta_offset = 0;
2383d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2384d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2385d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2386d752fcc8SJoe Perches		}
2387d752fcc8SJoe Perches
2388d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2389d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2390d752fcc8SJoe Perches			$save_line = 0;
2391d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2392d752fcc8SJoe Perches		}
2393d752fcc8SJoe Perches
2394d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2395d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2396d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2397d752fcc8SJoe Perches			$new_linenr++;
2398d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2399d752fcc8SJoe Perches		}
2400d752fcc8SJoe Perches
2401d752fcc8SJoe Perches		if ($save_line) {
2402d752fcc8SJoe Perches			push(@lines, $line);
2403d752fcc8SJoe Perches			$new_linenr++;
2404d752fcc8SJoe Perches		}
2405d752fcc8SJoe Perches
2406d752fcc8SJoe Perches		$old_linenr++;
2407d752fcc8SJoe Perches	}
2408d752fcc8SJoe Perches
2409d752fcc8SJoe Perches	return @lines;
2410d752fcc8SJoe Perches}
2411d752fcc8SJoe Perches
2412f2d7e4d4SJoe Perchessub fix_insert_line {
2413f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2414f2d7e4d4SJoe Perches
2415f2d7e4d4SJoe Perches	my $inserted = {
2416f2d7e4d4SJoe Perches		LINENR => $linenr,
2417f2d7e4d4SJoe Perches		LINE => $line,
2418f2d7e4d4SJoe Perches	};
2419f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2420f2d7e4d4SJoe Perches}
2421f2d7e4d4SJoe Perches
2422f2d7e4d4SJoe Perchessub fix_delete_line {
2423f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2424f2d7e4d4SJoe Perches
2425f2d7e4d4SJoe Perches	my $deleted = {
2426f2d7e4d4SJoe Perches		LINENR => $linenr,
2427f2d7e4d4SJoe Perches		LINE => $line,
2428f2d7e4d4SJoe Perches	};
2429f2d7e4d4SJoe Perches
2430f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2431f2d7e4d4SJoe Perches}
2432f2d7e4d4SJoe Perches
2433de7d4f0eSAndy Whitcroftsub ERROR {
2434cbec18afSJoe Perches	my ($type, $msg) = @_;
2435cbec18afSJoe Perches
2436cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2437de7d4f0eSAndy Whitcroft		our $clean = 0;
24386c72ffaaSAndy Whitcroft		our $cnt_error++;
24393705ce5bSJoe Perches		return 1;
2440de7d4f0eSAndy Whitcroft	}
24413705ce5bSJoe Perches	return 0;
2442773647a0SAndy Whitcroft}
2443de7d4f0eSAndy Whitcroftsub WARN {
2444cbec18afSJoe Perches	my ($type, $msg) = @_;
2445cbec18afSJoe Perches
2446cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2447de7d4f0eSAndy Whitcroft		our $clean = 0;
24486c72ffaaSAndy Whitcroft		our $cnt_warn++;
24493705ce5bSJoe Perches		return 1;
2450de7d4f0eSAndy Whitcroft	}
24513705ce5bSJoe Perches	return 0;
2452773647a0SAndy Whitcroft}
2453de7d4f0eSAndy Whitcroftsub CHK {
2454cbec18afSJoe Perches	my ($type, $msg) = @_;
2455cbec18afSJoe Perches
2456cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2457de7d4f0eSAndy Whitcroft		our $clean = 0;
24586c72ffaaSAndy Whitcroft		our $cnt_chk++;
24593705ce5bSJoe Perches		return 1;
24606c72ffaaSAndy Whitcroft	}
24613705ce5bSJoe Perches	return 0;
2462de7d4f0eSAndy Whitcroft}
2463de7d4f0eSAndy Whitcroft
24646ecd9674SAndy Whitcroftsub check_absolute_file {
24656ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
24666ecd9674SAndy Whitcroft	my $file = $absolute;
24676ecd9674SAndy Whitcroft
24686ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
24696ecd9674SAndy Whitcroft
24706ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
24716ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
24726ecd9674SAndy Whitcroft		if (-f "$root/$file") {
24736ecd9674SAndy Whitcroft			##print "file<$file>\n";
24746ecd9674SAndy Whitcroft			last;
24756ecd9674SAndy Whitcroft		}
24766ecd9674SAndy Whitcroft	}
24776ecd9674SAndy Whitcroft	if (! -f _)  {
24786ecd9674SAndy Whitcroft		return 0;
24796ecd9674SAndy Whitcroft	}
24806ecd9674SAndy Whitcroft
24816ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
24826ecd9674SAndy Whitcroft	my $prefix = $absolute;
24836ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
24846ecd9674SAndy Whitcroft
24856ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
24866ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2487000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2488000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
24896ecd9674SAndy Whitcroft	}
24906ecd9674SAndy Whitcroft}
24916ecd9674SAndy Whitcroft
24923705ce5bSJoe Perchessub trim {
24933705ce5bSJoe Perches	my ($string) = @_;
24943705ce5bSJoe Perches
2495b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2496b34c648bSJoe Perches
2497b34c648bSJoe Perches	return $string;
2498b34c648bSJoe Perches}
2499b34c648bSJoe Perches
2500b34c648bSJoe Perchessub ltrim {
2501b34c648bSJoe Perches	my ($string) = @_;
2502b34c648bSJoe Perches
2503b34c648bSJoe Perches	$string =~ s/^\s+//;
2504b34c648bSJoe Perches
2505b34c648bSJoe Perches	return $string;
2506b34c648bSJoe Perches}
2507b34c648bSJoe Perches
2508b34c648bSJoe Perchessub rtrim {
2509b34c648bSJoe Perches	my ($string) = @_;
2510b34c648bSJoe Perches
2511b34c648bSJoe Perches	$string =~ s/\s+$//;
25123705ce5bSJoe Perches
25133705ce5bSJoe Perches	return $string;
25143705ce5bSJoe Perches}
25153705ce5bSJoe Perches
251652ea8506SJoe Perchessub string_find_replace {
251752ea8506SJoe Perches	my ($string, $find, $replace) = @_;
251852ea8506SJoe Perches
251952ea8506SJoe Perches	$string =~ s/$find/$replace/g;
252052ea8506SJoe Perches
252152ea8506SJoe Perches	return $string;
252252ea8506SJoe Perches}
252352ea8506SJoe Perches
25243705ce5bSJoe Perchessub tabify {
25253705ce5bSJoe Perches	my ($leading) = @_;
25263705ce5bSJoe Perches
2527713a09deSAntonio Borneo	my $source_indent = $tabsize;
25283705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
25293705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
25303705ce5bSJoe Perches
25313705ce5bSJoe Perches	#convert leading spaces to tabs
25323705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
25333705ce5bSJoe Perches	#Remove spaces before a tab
25343705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
25353705ce5bSJoe Perches
25363705ce5bSJoe Perches	return "$leading";
25373705ce5bSJoe Perches}
25383705ce5bSJoe Perches
2539d1fe9c09SJoe Perchessub pos_last_openparen {
2540d1fe9c09SJoe Perches	my ($line) = @_;
2541d1fe9c09SJoe Perches
2542d1fe9c09SJoe Perches	my $pos = 0;
2543d1fe9c09SJoe Perches
2544d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2545d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2546d1fe9c09SJoe Perches
2547d1fe9c09SJoe Perches	my $last_openparen = 0;
2548d1fe9c09SJoe Perches
2549d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2550d1fe9c09SJoe Perches		return -1;
2551d1fe9c09SJoe Perches	}
2552d1fe9c09SJoe Perches
2553d1fe9c09SJoe Perches	my $len = length($line);
2554d1fe9c09SJoe Perches
2555d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2556d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2557d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2558d1fe9c09SJoe Perches			$pos += length($1) - 1;
2559d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2560d1fe9c09SJoe Perches			$last_openparen = $pos;
2561d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2562d1fe9c09SJoe Perches			last;
2563d1fe9c09SJoe Perches		}
2564d1fe9c09SJoe Perches	}
2565d1fe9c09SJoe Perches
256691cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2567d1fe9c09SJoe Perches}
2568d1fe9c09SJoe Perches
2569f36d3eb8SJoe Perchessub get_raw_comment {
2570f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2571f36d3eb8SJoe Perches	my $comment = '';
2572f36d3eb8SJoe Perches
2573f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2574f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2575f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2576f36d3eb8SJoe Perches		}
2577f36d3eb8SJoe Perches	}
2578f36d3eb8SJoe Perches
2579f36d3eb8SJoe Perches	return $comment;
2580f36d3eb8SJoe Perches}
2581f36d3eb8SJoe Perches
25825b8f82e1SSong Liusub exclude_global_initialisers {
25835b8f82e1SSong Liu	my ($realfile) = @_;
25845b8f82e1SSong Liu
25855b8f82e1SSong Liu	# Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
25865b8f82e1SSong Liu	return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
25875b8f82e1SSong Liu		$realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
25885b8f82e1SSong Liu		$realfile =~ m@/bpf/.*\.bpf\.c$@;
25895b8f82e1SSong Liu}
25905b8f82e1SSong Liu
25910a920b5bSAndy Whitcroftsub process {
25920a920b5bSAndy Whitcroft	my $filename = shift;
25930a920b5bSAndy Whitcroft
25940a920b5bSAndy Whitcroft	my $linenr=0;
25950a920b5bSAndy Whitcroft	my $prevline="";
2596c2fdda0dSAndy Whitcroft	my $prevrawline="";
25970a920b5bSAndy Whitcroft	my $stashline="";
2598c2fdda0dSAndy Whitcroft	my $stashrawline="";
25990a920b5bSAndy Whitcroft
26004a0df2efSAndy Whitcroft	my $length;
26010a920b5bSAndy Whitcroft	my $indent;
26020a920b5bSAndy Whitcroft	my $previndent=0;
26030a920b5bSAndy Whitcroft	my $stashindent=0;
26040a920b5bSAndy Whitcroft
2605de7d4f0eSAndy Whitcroft	our $clean = 1;
26060a920b5bSAndy Whitcroft	my $signoff = 0;
2607cd261496SGeert Uytterhoeven	my $author = '';
2608cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
260948ca2d8aSDwaipayan Ray	my $author_sob = '';
26100a920b5bSAndy Whitcroft	my $is_patch = 0;
2611133712a2SRob Herring	my $is_binding_patch = -1;
261229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
261315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
261444d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2615ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2616490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2617bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
26182a076f40SJoe Perches	my $commit_log_long_line = 0;
2619e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
262013f1937eSJoe Perches	my $reported_maintainer_file = 0;
2621fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2622fa64205dSPasi Savanainen
26234ce9f970SJoe Perches	my $last_git_commit_id_linenr = -1;
26244ce9f970SJoe Perches
2625365dd4eaSJoe Perches	my $last_blank_line = 0;
26265e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2627365dd4eaSJoe Perches
262813214adfSAndy Whitcroft	our @report = ();
26296c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
26306c72ffaaSAndy Whitcroft	our $cnt_error = 0;
26316c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
26326c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
26336c72ffaaSAndy Whitcroft
26340a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
26350a920b5bSAndy Whitcroft	my $realfile = '';
26360a920b5bSAndy Whitcroft	my $realline = 0;
26370a920b5bSAndy Whitcroft	my $realcnt = 0;
26380a920b5bSAndy Whitcroft	my $here = '';
263977cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
26400a920b5bSAndy Whitcroft	my $in_comment = 0;
2641c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
26420a920b5bSAndy Whitcroft	my $first_line = 0;
26431e855726SWolfram Sang	my $p1_prefix = '';
26440a920b5bSAndy Whitcroft
264513214adfSAndy Whitcroft	my $prev_values = 'E';
264613214adfSAndy Whitcroft
264713214adfSAndy Whitcroft	# suppression flags
2648773647a0SAndy Whitcroft	my %suppress_ifbraces;
2649170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
26502b474a1aSAndy Whitcroft	my %suppress_export;
26513e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2652653d4876SAndy Whitcroft
26537e51f197SJoe Perches	my %signatures = ();
2654323c1260SJoe Perches
2655c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2656de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2657c2fdda0dSAndy Whitcroft	#
2658de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2659de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2660773647a0SAndy Whitcroft
2661d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2662d8b07710SJoe Perches
26639f3a8992SRob Herring	my $checklicenseline = 1;
26649f3a8992SRob Herring
2665773647a0SAndy Whitcroft	sanitise_line_reset();
2666c2fdda0dSAndy Whitcroft	my $line;
2667c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2668773647a0SAndy Whitcroft		$linenr++;
2669773647a0SAndy Whitcroft		$line = $rawline;
2670c2fdda0dSAndy Whitcroft
26713705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
26723705ce5bSJoe Perches
2673773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2674de7d4f0eSAndy Whitcroft			$setup_docs = 0;
26752581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2676de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2677de7d4f0eSAndy Whitcroft			}
2678773647a0SAndy Whitcroft			#next;
2679de7d4f0eSAndy Whitcroft		}
268074fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2681773647a0SAndy Whitcroft			$realline=$1-1;
2682773647a0SAndy Whitcroft			if (defined $2) {
2683773647a0SAndy Whitcroft				$realcnt=$3+1;
2684773647a0SAndy Whitcroft			} else {
2685773647a0SAndy Whitcroft				$realcnt=1+1;
2686773647a0SAndy Whitcroft			}
2687c45dcabdSAndy Whitcroft			$in_comment = 0;
2688773647a0SAndy Whitcroft
2689773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2690773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2691773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2692773647a0SAndy Whitcroft			# at context start.
2693773647a0SAndy Whitcroft			my $edge;
269401fa9147SAndy Whitcroft			my $cnt = $realcnt;
269501fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
269601fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
269701fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
269801fa9147SAndy Whitcroft				$cnt--;
269901fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2700721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2701fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2702fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2703fae17daeSAndy Whitcroft					($edge) = $1;
2704fae17daeSAndy Whitcroft					last;
2705fae17daeSAndy Whitcroft				}
2706773647a0SAndy Whitcroft			}
2707773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2708773647a0SAndy Whitcroft				$in_comment = 1;
2709773647a0SAndy Whitcroft			}
2710773647a0SAndy Whitcroft
2711773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2712773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2713773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2714773647a0SAndy Whitcroft			if (!defined $edge &&
271583242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2716773647a0SAndy Whitcroft			{
2717773647a0SAndy Whitcroft				$in_comment = 1;
2718773647a0SAndy Whitcroft			}
2719773647a0SAndy Whitcroft
2720773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2721773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2722773647a0SAndy Whitcroft
2723171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2724773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2725171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2726773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2727773647a0SAndy Whitcroft		}
2728773647a0SAndy Whitcroft		push(@lines, $line);
2729773647a0SAndy Whitcroft
2730773647a0SAndy Whitcroft		if ($realcnt > 1) {
2731773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2732773647a0SAndy Whitcroft		} else {
2733773647a0SAndy Whitcroft			$realcnt = 0;
2734773647a0SAndy Whitcroft		}
2735773647a0SAndy Whitcroft
2736773647a0SAndy Whitcroft		#print "==>$rawline\n";
2737773647a0SAndy Whitcroft		#print "-->$line\n";
2738de7d4f0eSAndy Whitcroft
2739de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2740de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2741de7d4f0eSAndy Whitcroft		}
2742de7d4f0eSAndy Whitcroft	}
2743de7d4f0eSAndy Whitcroft
27446c72ffaaSAndy Whitcroft	$prefix = '';
27456c72ffaaSAndy Whitcroft
2746773647a0SAndy Whitcroft	$realcnt = 0;
2747773647a0SAndy Whitcroft	$linenr = 0;
2748194f66fcSJoe Perches	$fixlinenr = -1;
27490a920b5bSAndy Whitcroft	foreach my $line (@lines) {
27500a920b5bSAndy Whitcroft		$linenr++;
2751194f66fcSJoe Perches		$fixlinenr++;
27521b5539b1SJoe Perches		my $sline = $line;	#copy of $line
27531b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
27540a920b5bSAndy Whitcroft
2755c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2756f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
27576c72ffaaSAndy Whitcroft
275812c253abSJoe Perches# check if it's a mode change, rename or start of a patch
275912c253abSJoe Perches		if (!$in_commit_log &&
276012c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
276112c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
276212c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
276312c253abSJoe Perches			$is_patch = 1;
276412c253abSJoe Perches		}
276512c253abSJoe Perches
27660a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2767e518e9a5SJoe Perches		if (!$in_commit_log &&
276874fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
276974fd4f34SJoe Perches			my $context = $4;
27700a920b5bSAndy Whitcroft			$is_patch = 1;
27714a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
27720a920b5bSAndy Whitcroft			$realline=$1-1;
27730a920b5bSAndy Whitcroft			if (defined $2) {
27740a920b5bSAndy Whitcroft				$realcnt=$3+1;
27750a920b5bSAndy Whitcroft			} else {
27760a920b5bSAndy Whitcroft				$realcnt=1+1;
27770a920b5bSAndy Whitcroft			}
2778c2fdda0dSAndy Whitcroft			annotate_reset();
277913214adfSAndy Whitcroft			$prev_values = 'E';
278013214adfSAndy Whitcroft
2781773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2782170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
27832b474a1aSAndy Whitcroft			%suppress_export = ();
27843e469cdcSAndy Whitcroft			$suppress_statement = 0;
278574fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
278674fd4f34SJoe Perches				$context_function = $1;
278774fd4f34SJoe Perches			} else {
278874fd4f34SJoe Perches				undef $context_function;
278974fd4f34SJoe Perches			}
27900a920b5bSAndy Whitcroft			next;
27910a920b5bSAndy Whitcroft
27924a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
27934a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
27944a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2795773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
27960a920b5bSAndy Whitcroft			$realline++;
2797d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
27980a920b5bSAndy Whitcroft
27994a0df2efSAndy Whitcroft			# Measure the line length and indent.
2800c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
28010a920b5bSAndy Whitcroft
28020a920b5bSAndy Whitcroft			# Track the previous line.
28030a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
28040a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2805c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2806c2fdda0dSAndy Whitcroft
2807773647a0SAndy Whitcroft			#warn "line<$line>\n";
28086c72ffaaSAndy Whitcroft
2809d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2810d8aaf121SAndy Whitcroft			$realcnt--;
28110a920b5bSAndy Whitcroft		}
28120a920b5bSAndy Whitcroft
2813cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2814cc77cdcaSAndy Whitcroft
28156c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
28166c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2817773647a0SAndy Whitcroft
28182ac73b4fSJoe Perches		my $found_file = 0;
2819773647a0SAndy Whitcroft		# extract the filename as it passes
28203bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
28213bf9a009SRabin Vincent			$realfile = $1;
28222b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2823270c49a0SJoe Perches			$in_commit_log = 0;
28242ac73b4fSJoe Perches			$found_file = 1;
28253bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2826773647a0SAndy Whitcroft			$realfile = $1;
28272b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2828270c49a0SJoe Perches			$in_commit_log = 0;
28291e855726SWolfram Sang
28301e855726SWolfram Sang			$p1_prefix = $1;
2831e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2832e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2833000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2834000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
28351e855726SWolfram Sang			}
2836773647a0SAndy Whitcroft
2837c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2838000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2839000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2840773647a0SAndy Whitcroft			}
28412ac73b4fSJoe Perches			$found_file = 1;
28422ac73b4fSJoe Perches		}
28432ac73b4fSJoe Perches
284434d8815fSJoe Perches#make up the handle for any error we report on this line
284534d8815fSJoe Perches		if ($showfile) {
284634d8815fSJoe Perches			$prefix = "$realfile:$realline: "
284734d8815fSJoe Perches		} elsif ($emacs) {
28487d3a9f67SJoe Perches			if ($file) {
28497d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
28507d3a9f67SJoe Perches			} else {
285134d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
285234d8815fSJoe Perches			}
28537d3a9f67SJoe Perches		}
285434d8815fSJoe Perches
28552ac73b4fSJoe Perches		if ($found_file) {
285685b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
285785b0ee18SJoe Perches				WARN("OBSOLETE",
285885b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
285985b0ee18SJoe Perches			}
28607bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
28612ac73b4fSJoe Perches				$check = 1;
28622ac73b4fSJoe Perches			} else {
28632ac73b4fSJoe Perches				$check = $check_orig;
28642ac73b4fSJoe Perches			}
28659f3a8992SRob Herring			$checklicenseline = 1;
2866133712a2SRob Herring
2867133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2868133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2869133712a2SRob Herring
2870133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2871133712a2SRob Herring
2872133712a2SRob Herring				if (($last_binding_patch != -1) &&
2873133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2874133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2875858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2876133712a2SRob Herring				}
2877133712a2SRob Herring			}
2878133712a2SRob Herring
2879773647a0SAndy Whitcroft			next;
2880773647a0SAndy Whitcroft		}
2881773647a0SAndy Whitcroft
2882389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
28830a920b5bSAndy Whitcroft
2884c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2885c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2886c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
28870a920b5bSAndy Whitcroft
28886c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
28896c72ffaaSAndy Whitcroft
2890490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2891490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2892490b292cSJoe Perches		if ($in_commit_log) {
2893490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2894490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2895490b292cSJoe Perches			}
2896490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2897490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2898490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2899490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2900490b292cSJoe Perches		}
2901490b292cSJoe Perches
2902e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2903e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
290413e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
290513e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2906e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2907e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2908e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2909e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2910e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2911e518e9a5SJoe Perches		}
2912e518e9a5SJoe Perches
29133bf9a009SRabin Vincent# Check for incorrect file permissions
29143bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
29153bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
291604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
291704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2918000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2919000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
29203bf9a009SRabin Vincent			}
29213bf9a009SRabin Vincent		}
29223bf9a009SRabin Vincent
2923cd261496SGeert Uytterhoeven# Check the patch for a From:
2924cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2925cd261496SGeert Uytterhoeven			$author = $1;
2926e7f929f3SDwaipayan Ray			my $curline = $linenr;
2927e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2928e7f929f3SDwaipayan Ray				$author .= $1;
2929e7f929f3SDwaipayan Ray			}
2930cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2931cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2932dfa05c28SJoe Perches			$author = reformat_email($author);
2933cd261496SGeert Uytterhoeven		}
2934cd261496SGeert Uytterhoeven
293520112475SJoe Perches# Check the patch for a signoff:
2936dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
29374a0df2efSAndy Whitcroft			$signoff++;
293815662b3eSJoe Perches			$in_commit_log = 0;
293948ca2d8aSDwaipayan Ray			if ($author ne ''  && $authorsignoff != 1) {
2940fccaebf0SDwaipayan Ray				if (same_email_addresses($1, $author)) {
2941cd261496SGeert Uytterhoeven					$authorsignoff = 1;
294248ca2d8aSDwaipayan Ray				} else {
294348ca2d8aSDwaipayan Ray					my $ctx = $1;
294448ca2d8aSDwaipayan Ray					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
294548ca2d8aSDwaipayan Ray					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
294648ca2d8aSDwaipayan Ray
2947046fc741SMimi Zohar					if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
294848ca2d8aSDwaipayan Ray						$author_sob = $ctx;
294948ca2d8aSDwaipayan Ray						$authorsignoff = 2;
2950046fc741SMimi Zohar					} elsif (lc $email_address eq lc $author_address) {
295148ca2d8aSDwaipayan Ray						$author_sob = $ctx;
295248ca2d8aSDwaipayan Ray						$authorsignoff = 3;
295348ca2d8aSDwaipayan Ray					} elsif ($email_name eq $author_name) {
295448ca2d8aSDwaipayan Ray						$author_sob = $ctx;
295548ca2d8aSDwaipayan Ray						$authorsignoff = 4;
295648ca2d8aSDwaipayan Ray
295748ca2d8aSDwaipayan Ray						my $address1 = $email_address;
295848ca2d8aSDwaipayan Ray						my $address2 = $author_address;
295948ca2d8aSDwaipayan Ray
296048ca2d8aSDwaipayan Ray						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
296148ca2d8aSDwaipayan Ray							$address1 = "$1$2";
296248ca2d8aSDwaipayan Ray						}
296348ca2d8aSDwaipayan Ray						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
296448ca2d8aSDwaipayan Ray							$address2 = "$1$2";
296548ca2d8aSDwaipayan Ray						}
296648ca2d8aSDwaipayan Ray						if ($address1 eq $address2) {
296748ca2d8aSDwaipayan Ray							$authorsignoff = 5;
296848ca2d8aSDwaipayan Ray						}
296948ca2d8aSDwaipayan Ray					}
2970cd261496SGeert Uytterhoeven				}
2971cd261496SGeert Uytterhoeven			}
29720a920b5bSAndy Whitcroft		}
297320112475SJoe Perches
297444d303ebSJoe Perches# Check for patch separator
297544d303ebSJoe Perches		if ($line =~ /^---$/) {
297644d303ebSJoe Perches			$has_patch_separator = 1;
297744d303ebSJoe Perches			$in_commit_log = 0;
297844d303ebSJoe Perches		}
297944d303ebSJoe Perches
2980e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2981e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2982e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2983e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2984e0d975b1SJoe Perches		}
2985e0d975b1SJoe Perches
298620112475SJoe Perches# Check signature styles
2987270c49a0SJoe Perches		if (!$in_header_lines &&
2988ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
298920112475SJoe Perches			my $space_before = $1;
299020112475SJoe Perches			my $sign_off = $2;
299120112475SJoe Perches			my $space_after = $3;
299220112475SJoe Perches			my $email = $4;
299320112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
299420112475SJoe Perches
2995ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2996831242abSAditya Srivastava				my $suggested_signature = find_standard_signature($sign_off);
2997831242abSAditya Srivastava				if ($suggested_signature eq "") {
2998ce0338dfSJoe Perches					WARN("BAD_SIGN_OFF",
2999ce0338dfSJoe Perches					     "Non-standard signature: $sign_off\n" . $herecurr);
3000831242abSAditya Srivastava				} else {
3001831242abSAditya Srivastava					if (WARN("BAD_SIGN_OFF",
3002831242abSAditya Srivastava						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
3003831242abSAditya Srivastava					    $fix) {
3004831242abSAditya Srivastava						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
3005831242abSAditya Srivastava					}
3006831242abSAditya Srivastava				}
3007ce0338dfSJoe Perches			}
300820112475SJoe Perches			if (defined $space_before && $space_before ne "") {
30093705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30103705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
30113705ce5bSJoe Perches				    $fix) {
3012194f66fcSJoe Perches					$fixed[$fixlinenr] =
30133705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30143705ce5bSJoe Perches				}
301520112475SJoe Perches			}
301620112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
30173705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30183705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
30193705ce5bSJoe Perches				    $fix) {
3020194f66fcSJoe Perches					$fixed[$fixlinenr] =
30213705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30223705ce5bSJoe Perches				}
30233705ce5bSJoe Perches
302420112475SJoe Perches			}
302520112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
30263705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30273705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
30283705ce5bSJoe Perches				    $fix) {
3029194f66fcSJoe Perches					$fixed[$fixlinenr] =
30303705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30313705ce5bSJoe Perches				}
303220112475SJoe Perches			}
303320112475SJoe Perches
3034dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
303548ca2d8aSDwaipayan Ray			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
303620112475SJoe Perches			if ($suggested_email eq "") {
3037000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
3038000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
303920112475SJoe Perches			} else {
304020112475SJoe Perches				my $dequoted = $suggested_email;
304120112475SJoe Perches				$dequoted =~ s/^"//;
304220112475SJoe Perches				$dequoted =~ s/" </ </;
304320112475SJoe Perches				# Don't force email to have quotes
304420112475SJoe Perches				# Allow just an angle bracketed address
3045fccaebf0SDwaipayan Ray				if (!same_email_addresses($email, $suggested_email)) {
3046fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3047fccaebf0SDwaipayan Ray						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3048fccaebf0SDwaipayan Ray					    $fix) {
3049fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3050fccaebf0SDwaipayan Ray					}
3051fccaebf0SDwaipayan Ray				}
3052fccaebf0SDwaipayan Ray
3053fccaebf0SDwaipayan Ray				# Address part shouldn't have comments
3054fccaebf0SDwaipayan Ray				my $stripped_address = $email_address;
3055fccaebf0SDwaipayan Ray				$stripped_address =~ s/\([^\(\)]*\)//g;
3056fccaebf0SDwaipayan Ray				if ($email_address ne $stripped_address) {
3057fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3058fccaebf0SDwaipayan Ray						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3059fccaebf0SDwaipayan Ray					    $fix) {
3060fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3061fccaebf0SDwaipayan Ray					}
3062fccaebf0SDwaipayan Ray				}
3063fccaebf0SDwaipayan Ray
3064fccaebf0SDwaipayan Ray				# Only one name comment should be allowed
3065fccaebf0SDwaipayan Ray				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3066fccaebf0SDwaipayan Ray				if ($comment_count > 1) {
3067000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
3068fccaebf0SDwaipayan Ray					     "Use a single name comment in email: '$email'\n" . $herecurr);
3069fccaebf0SDwaipayan Ray				}
3070fccaebf0SDwaipayan Ray
3071fccaebf0SDwaipayan Ray
3072fccaebf0SDwaipayan Ray				# [email protected] or [email protected] shouldn't
3073e73d2715SDwaipayan Ray				# have an email name. In addition comments should strictly
3074fccaebf0SDwaipayan Ray				# begin with a #
3075fccaebf0SDwaipayan Ray				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3076fccaebf0SDwaipayan Ray					if (($comment ne "" && $comment !~ /^#.+/) ||
3077fccaebf0SDwaipayan Ray					    ($email_name ne "")) {
3078fccaebf0SDwaipayan Ray						my $cur_name = $email_name;
3079fccaebf0SDwaipayan Ray						my $new_comment = $comment;
3080fccaebf0SDwaipayan Ray						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3081fccaebf0SDwaipayan Ray
3082fccaebf0SDwaipayan Ray						# Remove brackets enclosing comment text
3083fccaebf0SDwaipayan Ray						# and # from start of comments to get comment text
3084fccaebf0SDwaipayan Ray						$new_comment =~ s/^\((.*)\)$/$1/;
3085fccaebf0SDwaipayan Ray						$new_comment =~ s/^\[(.*)\]$/$1/;
3086fccaebf0SDwaipayan Ray						$new_comment =~ s/^[\s\#]+|\s+$//g;
3087fccaebf0SDwaipayan Ray
3088fccaebf0SDwaipayan Ray						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3089fccaebf0SDwaipayan Ray						$new_comment = " # $new_comment" if ($new_comment ne "");
3090fccaebf0SDwaipayan Ray						my $new_email = "$email_address$new_comment";
3091fccaebf0SDwaipayan Ray
3092fccaebf0SDwaipayan Ray						if (WARN("BAD_STABLE_ADDRESS_STYLE",
3093fccaebf0SDwaipayan Ray							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3094fccaebf0SDwaipayan Ray						    $fix) {
3095fccaebf0SDwaipayan Ray							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3096fccaebf0SDwaipayan Ray						}
3097fccaebf0SDwaipayan Ray					}
3098fccaebf0SDwaipayan Ray				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3099fccaebf0SDwaipayan Ray					my $new_comment = $comment;
3100fccaebf0SDwaipayan Ray
3101fccaebf0SDwaipayan Ray					# Extract comment text from within brackets or
3102fccaebf0SDwaipayan Ray					# c89 style /*...*/ comments
3103fccaebf0SDwaipayan Ray					$new_comment =~ s/^\[(.*)\]$/$1/;
3104fccaebf0SDwaipayan Ray					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3105fccaebf0SDwaipayan Ray
3106fccaebf0SDwaipayan Ray					$new_comment = trim($new_comment);
3107fccaebf0SDwaipayan Ray					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3108fccaebf0SDwaipayan Ray					$new_comment = "($new_comment)" if ($new_comment ne "");
3109fccaebf0SDwaipayan Ray					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3110fccaebf0SDwaipayan Ray
3111fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3112fccaebf0SDwaipayan Ray						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3113fccaebf0SDwaipayan Ray					    $fix) {
3114fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3115fccaebf0SDwaipayan Ray					}
311620112475SJoe Perches				}
31170a920b5bSAndy Whitcroft			}
31187e51f197SJoe Perches
31197e51f197SJoe Perches# Check for duplicate signatures
31207e51f197SJoe Perches			my $sig_nospace = $line;
31217e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
31227e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
31237e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
31247e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
31257e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
31267e51f197SJoe Perches			} else {
31277e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
31287e51f197SJoe Perches			}
31296c5d24eeSSean Christopherson
31306c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
31316c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
31326c5d24eeSSean Christopherson				if ($email eq $author) {
31336c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31346c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
31356c5d24eeSSean Christopherson				}
31366c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
31376c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31386c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
31396c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
31406c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31416c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
31426c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
31436c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31446c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
31456c5d24eeSSean Christopherson				}
31466c5d24eeSSean Christopherson			}
31470a920b5bSAndy Whitcroft		}
31480a920b5bSAndy Whitcroft
3149a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
3150a2fe16b9SJoe Perches		if ($in_header_lines &&
3151a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3152a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
3153a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3154a2fe16b9SJoe Perches		}
3155a2fe16b9SJoe Perches
315644d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
315744d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
31587580c5b9SAditya Srivastava			if (ERROR("GERRIT_CHANGE_ID",
31597580c5b9SAditya Srivastava			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
31607580c5b9SAditya Srivastava			    $fix) {
31617580c5b9SAditya Srivastava				fix_delete_line($fixlinenr, $rawline);
31627580c5b9SAditya Srivastava			}
31637ebd05efSChristopher Covington		}
31647ebd05efSChristopher Covington
3165369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
3166369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3167369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3168369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3169369c8dd3SJoe Perches					# timestamp
3170634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3171634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3172634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3173634cffccSJoe Perches					# stack dump address styles
3174369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
3175369c8dd3SJoe Perches		}
3176369c8dd3SJoe Perches
31772a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
31782a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
3179bf4daf12SJoe Perches		    length($line) > 75 &&
3180bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3181bf4daf12SJoe Perches					# file delta changes
318236f8b348SJerome Forissier		      $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\.\-\+]+:/ ||
3183bf4daf12SJoe Perches					# filename then :
318427b379afSAditya Srivastava		      $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
318527b379afSAditya Srivastava					# A Fixes: or Link: line or signature tag line
3186bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
31872a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
31882a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
31892a076f40SJoe Perches			$commit_log_long_line = 1;
31902a076f40SJoe Perches		}
31912a076f40SJoe Perches
3192bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
3193bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
3194bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
3195bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
3196bf4daf12SJoe Perches		}
3197bf4daf12SJoe Perches
3198084a617aSDwaipayan Ray# Check for lines starting with a #
3199084a617aSDwaipayan Ray		if ($in_commit_log && $line =~ /^#/) {
3200084a617aSDwaipayan Ray			if (WARN("COMMIT_COMMENT_SYMBOL",
3201084a617aSDwaipayan Ray				 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3202084a617aSDwaipayan Ray			    $fix) {
3203084a617aSDwaipayan Ray				$fixed[$fixlinenr] =~ s/^/ /;
3204084a617aSDwaipayan Ray			}
3205084a617aSDwaipayan Ray		}
3206084a617aSDwaipayan Ray
32070d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
32084ce9f970SJoe Perches# A correctly formed commit description is:
32094ce9f970SJoe Perches#    commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
32104ce9f970SJoe Perches# with the commit subject '("' prefix and '")' suffix
32114ce9f970SJoe Perches# This is a fairly compilicated block as it tests for what appears to be
32124ce9f970SJoe Perches# bare SHA-1 hash with  minimum length of 5.  It also avoids several types of
32134ce9f970SJoe Perches# possible SHA-1 matches.
32144ce9f970SJoe Perches# A commit match can span multiple lines so this block attempts to find a
32154ce9f970SJoe Perches# complete typical commit on a maximum of 3 lines
32164ce9f970SJoe Perches		if ($perl_version_ok &&
32174ce9f970SJoe Perches		    $in_commit_log && !$commit_log_possible_stack_dump &&
3218a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3219e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
32204ce9f970SJoe Perches		    (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
32214ce9f970SJoe Perches		      ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
3222aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3223369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3224bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3225fe043ea1SJoe Perches			my $init_char = "c";
3226fe043ea1SJoe Perches			my $orig_commit = "";
32270d7835fcSJoe Perches			my $short = 1;
32280d7835fcSJoe Perches			my $long = 0;
32290d7835fcSJoe Perches			my $case = 1;
32300d7835fcSJoe Perches			my $space = 1;
32310d7835fcSJoe Perches			my $id = '0123456789ab';
32320d7835fcSJoe Perches			my $orig_desc = "commit description";
32330d7835fcSJoe Perches			my $description = "";
32344ce9f970SJoe Perches			my $herectx = $herecurr;
32354ce9f970SJoe Perches			my $has_parens = 0;
32364ce9f970SJoe Perches			my $has_quotes = 0;
32370d7835fcSJoe Perches
32384ce9f970SJoe Perches			my $input = $line;
32394ce9f970SJoe Perches			if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
32404ce9f970SJoe Perches				for (my $n = 0; $n < 2; $n++) {
32414ce9f970SJoe Perches					if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
32424ce9f970SJoe Perches						$orig_desc = $1;
32434ce9f970SJoe Perches						$has_parens = 1;
32444ce9f970SJoe Perches						# Always strip leading/trailing parens then double quotes if existing
32454ce9f970SJoe Perches						$orig_desc = substr($orig_desc, 1, -1);
32464ce9f970SJoe Perches						if ($orig_desc =~ /^".*"$/) {
32474ce9f970SJoe Perches							$orig_desc = substr($orig_desc, 1, -1);
32484ce9f970SJoe Perches							$has_quotes = 1;
32494ce9f970SJoe Perches						}
32504ce9f970SJoe Perches						last;
32514ce9f970SJoe Perches					}
32524ce9f970SJoe Perches					last if ($#lines < $linenr + $n);
32534ce9f970SJoe Perches					$input .= " " . trim($rawlines[$linenr + $n]);
32544ce9f970SJoe Perches					$herectx .= "$rawlines[$linenr + $n]\n";
32554ce9f970SJoe Perches				}
32564ce9f970SJoe Perches				$herectx = $herecurr if (!$has_parens);
3257fe043ea1SJoe Perches			}
3258fe043ea1SJoe Perches
32594ce9f970SJoe Perches			if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
32604ce9f970SJoe Perches				$init_char = $1;
32614ce9f970SJoe Perches				$orig_commit = lc($2);
32624ce9f970SJoe Perches				$short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
32634ce9f970SJoe Perches				$long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
32644ce9f970SJoe Perches				$space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
32654ce9f970SJoe Perches				$case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
32664ce9f970SJoe Perches			} elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
32674ce9f970SJoe Perches				$orig_commit = lc($1);
32680d7835fcSJoe Perches			}
32690d7835fcSJoe Perches
32700d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
32710d7835fcSJoe Perches							      $id, $orig_desc);
32720d7835fcSJoe Perches
3273948b133aSHeinrich Schuchardt			if (defined($id) &&
32744ce9f970SJoe Perches			    ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
32754ce9f970SJoe Perches			    $last_git_commit_id_linenr != $linenr - 1) {
3276d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
32774ce9f970SJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
32780d7835fcSJoe Perches			}
32794ce9f970SJoe Perches			#don't report the next line if this line ends in commit and the sha1 hash is the next line
32804ce9f970SJoe Perches			$last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
3281d311cd44SJoe Perches		}
3282d311cd44SJoe Perches
328313f1937eSJoe Perches# Check for added, moved or deleted files
328413f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
328513f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
328613f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
328713f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
328813f1937eSJoe Perches		      (defined($1) || defined($2))))) {
3289a82603a8SAndrew Jeffery			$is_patch = 1;
329013f1937eSJoe Perches			$reported_maintainer_file = 1;
329113f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
329213f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
329313f1937eSJoe Perches		}
329413f1937eSJoe Perches
3295e400edb1SRob Herring# Check for adding new DT bindings not in schema format
3296e400edb1SRob Herring		if (!$in_commit_log &&
3297e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3298e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3299e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
330056ddc4cdSMauro Carvalho Chehab			     "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
3301e400edb1SRob Herring		}
3302e400edb1SRob Herring
330300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
33048905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3305000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
3306000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
33076c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
3308de7d4f0eSAndy Whitcroft		}
3309de7d4f0eSAndy Whitcroft
3310de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3311de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3312171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
3313171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3314171ae1a4SAndy Whitcroft
3315171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
3316171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3317171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
3318171ae1a4SAndy Whitcroft
331934d99219SJoe Perches			CHK("INVALID_UTF8",
3320000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
332100df344fSAndy Whitcroft		}
33220a920b5bSAndy Whitcroft
332315662b3eSJoe Perches# Check if it's the start of a commit log
332415662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
332515662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
3326eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
3327eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
332815662b3eSJoe Perches			$in_header_lines = 0;
332915662b3eSJoe Perches			$in_commit_log = 1;
3330ed43c4e5SAllen Hubbe			$has_commit_log = 1;
333115662b3eSJoe Perches		}
333215662b3eSJoe Perches
3333fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
3334fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
3335fa64205dSPasi Savanainen		if ($in_header_lines &&
3336fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3337fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
3338fa64205dSPasi Savanainen			$non_utf8_charset = 1;
3339fa64205dSPasi Savanainen		}
3340fa64205dSPasi Savanainen
3341fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
334215662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
3343fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
334415662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
334515662b3eSJoe Perches		}
334615662b3eSJoe Perches
3347d6430f71SJoe Perches# Check for absolute kernel paths in commit message
3348d6430f71SJoe Perches		if ($tree && $in_commit_log) {
3349d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
3350d6430f71SJoe Perches				my $file = $1;
3351d6430f71SJoe Perches
3352d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3353d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
3354d6430f71SJoe Perches					#
3355d6430f71SJoe Perches				} else {
3356d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
3357d6430f71SJoe Perches				}
3358d6430f71SJoe Perches			}
3359d6430f71SJoe Perches		}
3360d6430f71SJoe Perches
336166b47b4aSKees Cook# Check for various typo / spelling mistakes
336266d7a382SJoe Perches		if (defined($misspellings) &&
336366d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
33647da07c31SDwaipayan Ray			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
336566b47b4aSKees Cook				my $typo = $1;
33667da07c31SDwaipayan Ray				my $blank = copy_spacing($rawline);
33677da07c31SDwaipayan Ray				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
33687da07c31SDwaipayan Ray				my $hereptr = "$hereline$ptr\n";
336966b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
337066b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
337166b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
33720675a8fbSJean Delvare				my $msg_level = \&WARN;
33730675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
33740675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
33757da07c31SDwaipayan Ray						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
337666b47b4aSKees Cook				    $fix) {
337766b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
337866b47b4aSKees Cook				}
337966b47b4aSKees Cook			}
338066b47b4aSKees Cook		}
338166b47b4aSKees Cook
3382a8dd86bfSMatteo Croce# check for invalid commit id
3383a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3384a8dd86bfSMatteo Croce			my $id;
3385a8dd86bfSMatteo Croce			my $description;
3386a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
3387a8dd86bfSMatteo Croce			if (!defined($id)) {
3388a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
3389a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3390a8dd86bfSMatteo Croce			}
3391a8dd86bfSMatteo Croce		}
3392a8dd86bfSMatteo Croce
3393310cd06bSJoe Perches# check for repeated words separated by a single space
33948d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
33958d0325ccSAditya Srivastava		if (($rawline =~ /^\+/ || $in_commit_log) &&
33968d0325ccSAditya Srivastava		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
33971db81a68SDwaipayan Ray			pos($rawline) = 1 if (!$in_commit_log);
3398310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3399310cd06bSJoe Perches
3400310cd06bSJoe Perches				my $first = $1;
3401310cd06bSJoe Perches				my $second = $2;
34021db81a68SDwaipayan Ray				my $start_pos = $-[1];
34031db81a68SDwaipayan Ray				my $end_pos = $+[2];
3404310cd06bSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3405310cd06bSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3406310cd06bSJoe Perches					next;
3407310cd06bSJoe Perches				}
3408310cd06bSJoe Perches
34091db81a68SDwaipayan Ray				next if (lc($first) ne lc($second));
3410310cd06bSJoe Perches				next if ($first eq 'long');
3411310cd06bSJoe Perches
34121db81a68SDwaipayan Ray				# check for character before and after the word matches
34131db81a68SDwaipayan Ray				my $start_char = '';
34141db81a68SDwaipayan Ray				my $end_char = '';
34151db81a68SDwaipayan Ray				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
34161db81a68SDwaipayan Ray				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
34171db81a68SDwaipayan Ray
34181db81a68SDwaipayan Ray				next if ($start_char =~ /^\S$/);
34191db81a68SDwaipayan Ray				next if (index(" \t.,;?!", $end_char) == -1);
34201db81a68SDwaipayan Ray
34218d0325ccSAditya Srivastava				# avoid repeating hex occurrences like 'ff ff fe 09 ...'
34228d0325ccSAditya Srivastava				if ($first =~ /\b[0-9a-f]{2,}\b/i) {
34238d0325ccSAditya Srivastava					next if (!exists($allow_repeated_words{lc($first)}));
34248d0325ccSAditya Srivastava				}
34258d0325ccSAditya Srivastava
3426310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3427310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3428310cd06bSJoe Perches				    $fix) {
3429310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3430310cd06bSJoe Perches				}
3431310cd06bSJoe Perches			}
3432310cd06bSJoe Perches
3433310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3434310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3435310cd06bSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3436310cd06bSJoe Perches				my $last_word = $1;
3437310cd06bSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3438310cd06bSJoe Perches					if (WARN("REPEATED_WORD",
3439310cd06bSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3440310cd06bSJoe Perches					    $fix) {
3441310cd06bSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3442310cd06bSJoe Perches					}
3443310cd06bSJoe Perches				}
3444310cd06bSJoe Perches			}
3445310cd06bSJoe Perches		}
3446310cd06bSJoe Perches
344730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
344830670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
344900df344fSAndy Whitcroft
34500a920b5bSAndy Whitcroft#trailing whitespace
34519c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
3452c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3453d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3454d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
3455d5e616fcSJoe Perches			    $fix) {
3456194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3457d5e616fcSJoe Perches			}
3458c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3459c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34603705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
34613705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
34623705ce5bSJoe Perches			    $fix) {
3463194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
34643705ce5bSJoe Perches			}
34653705ce5bSJoe Perches
3466d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
34670a920b5bSAndy Whitcroft		}
34685368df20SAndy Whitcroft
34694783f894SJosh Triplett# Check for FSF mailing addresses.
3470109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
34711bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
34723e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
34733e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
34744783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34750675a8fbSJean Delvare			my $msg_level = \&ERROR;
34760675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
34770675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
34784783f894SJosh 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)
34794783f894SJosh Triplett		}
34804783f894SJosh Triplett
34813354957aSAndi Kleen# check for Kconfig help text having a real description
34829fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
34839fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
34843354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3485678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3486678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3487678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3488678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3489b8709bceSJoe Perches			my $ln = $linenr;
3490b8709bceSJoe Perches			my $needs_help = 0;
3491b8709bceSJoe Perches			my $has_help = 0;
3492b8709bceSJoe Perches			my $help_length = 0;
3493b8709bceSJoe Perches			while (defined $lines[$ln]) {
3494b8709bceSJoe Perches				my $f = $lines[$ln++];
34959fe287d7SAndy Whitcroft
34969fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
3497b8709bceSJoe Perches				last if ($f !~ /^[\+ ]/);	# !patch context
3498a1385803SAndy Whitcroft
3499b8709bceSJoe Perches				if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3500b8709bceSJoe Perches					$needs_help = 1;
3501b8709bceSJoe Perches					next;
3502b8709bceSJoe Perches				}
3503b8709bceSJoe Perches				if ($f =~ /^\+\s*help\s*$/) {
3504b8709bceSJoe Perches					$has_help = 1;
3505b8709bceSJoe Perches					next;
3506a1385803SAndy Whitcroft				}
3507a1385803SAndy Whitcroft
3508b8709bceSJoe Perches				$f =~ s/^.//;	# strip patch context [+ ]
3509b8709bceSJoe Perches				$f =~ s/#.*//;	# strip # directives
3510b8709bceSJoe Perches				$f =~ s/^\s+//;	# strip leading blanks
3511b8709bceSJoe Perches				next if ($f =~ /^$/);	# skip blank lines
3512678ae162SUlf Magnusson
3513b8709bceSJoe Perches				# At the end of this Kconfig block:
3514678ae162SUlf Magnusson				# This only checks context lines in the patch
3515678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3516678ae162SUlf Magnusson				# positives, even though some of these are
3517678ae162SUlf Magnusson				# common words in help texts
3518b8709bceSJoe Perches				if ($f =~ /^(?:config|menuconfig|choice|endchoice|
3519678ae162SUlf Magnusson					       if|endif|menu|endmenu|source)\b/x) {
35209fe287d7SAndy Whitcroft					last;
35219fe287d7SAndy Whitcroft				}
3522b8709bceSJoe Perches				$help_length++ if ($has_help);
35233354957aSAndi Kleen			}
3524b8709bceSJoe Perches			if ($needs_help &&
3525b8709bceSJoe Perches			    $help_length < $min_conf_desc_length) {
3526b8709bceSJoe Perches				my $stat_real = get_stat_real($linenr, $ln - 1);
3527000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
3528b8709bceSJoe Perches				     "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
352956193274SVadim Bendebury			}
35303354957aSAndi Kleen		}
35313354957aSAndi Kleen
35327ccf41a8SJoe Perches# check MAINTAINERS entries
35337ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
35347ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
35357ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3536628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3537628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3538628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3539628f91a2SJoe Perches				    $fix) {
3540628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3541628f91a2SJoe Perches				}
3542628f91a2SJoe Perches			}
35437ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
35447ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
35457ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
35467ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
35477ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
35487ccf41a8SJoe Perches				my $cur = $1;
35497ccf41a8SJoe Perches				my $curval = $2;
35507ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
35517ccf41a8SJoe Perches				my $prev = $1;
35527ccf41a8SJoe Perches				my $prevval = $2;
35537ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
35547ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
35557ccf41a8SJoe Perches				if ($curindex < 0) {
35567ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
35577ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
35587ccf41a8SJoe Perches				} else {
35597ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
35607ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
35617ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
35627ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
35637ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
35647ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
35657ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
35667ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
35677ccf41a8SJoe Perches					}
35687ccf41a8SJoe Perches				}
35697ccf41a8SJoe Perches			}
35707ccf41a8SJoe Perches		}
3571628f91a2SJoe Perches
3572c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3573c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3574c68e5878SArnaud Lacombe			my $flag = $1;
3575c68e5878SArnaud Lacombe			my $replacement = {
3576c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3577c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3578c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3579c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3580c68e5878SArnaud Lacombe			};
3581c68e5878SArnaud Lacombe
3582c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3583c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3584c68e5878SArnaud Lacombe		}
3585c68e5878SArnaud Lacombe
3586bff5da43SRob Herring# check for DT compatible documentation
35877dd05b38SFlorian Vaussard		if (defined $root &&
35887dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
35897dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
35907dd05b38SFlorian Vaussard
3591bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3592bff5da43SRob Herring
3593cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3594852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3595cc93319bSFlorian Vaussard
3596bff5da43SRob Herring			foreach my $compat (@compats) {
3597bff5da43SRob Herring				my $compat2 = $compat;
3598185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3599185d566bSRob Herring				my $compat3 = $compat;
3600185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3601185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3602bff5da43SRob Herring				if ( $? >> 8 ) {
3603bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3604bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3605bff5da43SRob Herring				}
3606bff5da43SRob Herring
36074fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
36084fbf32a6SFlorian Vaussard				my $vendor = $1;
3609852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3610bff5da43SRob Herring				if ( $? >> 8 ) {
3611bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3612cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3613bff5da43SRob Herring				}
3614bff5da43SRob Herring			}
3615bff5da43SRob Herring		}
3616bff5da43SRob Herring
36179f3a8992SRob Herring# check for using SPDX license tag at beginning of files
36189f3a8992SRob Herring		if ($realline == $checklicenseline) {
36199f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
36209f3a8992SRob Herring				$checklicenseline = 2;
36219f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
36229f3a8992SRob Herring				my $comment = "";
36239f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
36249f3a8992SRob Herring					$comment = '/*';
36259f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
36269f3a8992SRob Herring					$comment = '//';
3627c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
36289f3a8992SRob Herring					$comment = '#';
36299f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
36309f3a8992SRob Herring					$comment = '..';
36319f3a8992SRob Herring				}
36329f3a8992SRob Herring
3633fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3634fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3635fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3636ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3637fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3638fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3639fdf13693SJoe Perches				}
3640fdf13693SJoe Perches
36419f3a8992SRob Herring				if ($comment !~ /^$/ &&
3642ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
36439f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
36449f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
36453b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
36463b6e8ac9SJoe Perches					my $spdx_license = $1;
36473b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
36483b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
36493b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
36503b6e8ac9SJoe Perches					}
365150c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
365250c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
365350c92900SLubomir Rintel						my $msg_level = \&WARN;
365450c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
365550c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
365650c92900SLubomir Rintel
365750c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
365850c92900SLubomir Rintel						    $fix) {
365950c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
366050c92900SLubomir Rintel						}
366150c92900SLubomir Rintel					}
36629f3a8992SRob Herring				}
36639f3a8992SRob Herring			}
36649f3a8992SRob Herring		}
36659f3a8992SRob Herring
3666a0154cdbSJoe Perches# check for embedded filenames
3667a0154cdbSJoe Perches		if ($rawline =~ /^\+.*\Q$realfile\E/) {
3668a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3669a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3670a0154cdbSJoe Perches		}
3671a0154cdbSJoe Perches
36725368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3673d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
36745368df20SAndy Whitcroft
3675a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3676a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3677a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3678a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3679a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3680a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3681a8da38a9SJoe Perches		}
3682a8da38a9SJoe Perches
368347e0c88bSJoe Perches# line length limit (with some exclusions)
368447e0c88bSJoe Perches#
368547e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
368647e0c88bSJoe Perches#	logging functions like pr_info that end in a string
368747e0c88bSJoe Perches#	lines with a single string
368847e0c88bSJoe Perches#	#defines that are a single string
36892e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
369047e0c88bSJoe Perches#
369147e0c88bSJoe Perches# There are 3 different line length message types:
3692ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
369347e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
369447e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
369547e0c88bSJoe Perches#
369647e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
369747e0c88bSJoe Perches#
369847e0c88bSJoe Perches
3699b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
370047e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
370147e0c88bSJoe Perches
370247e0c88bSJoe Perches			# Check the allowed long line types first
370347e0c88bSJoe Perches
370447e0c88bSJoe Perches			# logging functions that end in a string that starts
370547e0c88bSJoe Perches			# before $max_line_length
370647e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
370747e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
370847e0c88bSJoe Perches				$msg_type = "";
370947e0c88bSJoe Perches
371047e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
371147e0c88bSJoe Perches			# #defines with only strings
371247e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
371347e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
371447e0c88bSJoe Perches				$msg_type = "";
371547e0c88bSJoe Perches
3716cc147506SJoe Perches			# More special cases
3717cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3718cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3719d560a5f8SJoe Perches				$msg_type = "";
3720d560a5f8SJoe Perches
37212e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
37222e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
37232e4bbbc5SAndreas Brauchli				$msg_type = "";
37242e4bbbc5SAndreas Brauchli
372547e0c88bSJoe Perches			# Otherwise set the alternate message types
372647e0c88bSJoe Perches
372747e0c88bSJoe Perches			# a comment starts before $max_line_length
372847e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
372947e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
373047e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
373147e0c88bSJoe Perches
373247e0c88bSJoe Perches			# a quoted string starts before $max_line_length
373347e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
373447e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
373547e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
373647e0c88bSJoe Perches			}
373747e0c88bSJoe Perches
373847e0c88bSJoe Perches			if ($msg_type ne "" &&
373947e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3740bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3741bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3742bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3743bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
37440a920b5bSAndy Whitcroft			}
374547e0c88bSJoe Perches		}
37460a920b5bSAndy Whitcroft
37478905a67cSAndy Whitcroft# check for adding lines without a newline.
37488905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
374947ca69b8STom Rix			if (WARN("MISSING_EOF_NEWLINE",
375047ca69b8STom Rix			         "adding a line without newline at end of file\n" . $herecurr) &&
375147ca69b8STom Rix			    $fix) {
375247ca69b8STom Rix				fix_delete_line($fixlinenr+1, "No newline at end of file");
375347ca69b8STom Rix			}
37548905a67cSAndy Whitcroft		}
37558905a67cSAndy Whitcroft
3756de93245cSAditya Srivastava# check for .L prefix local symbols in .S files
3757de93245cSAditya Srivastava		if ($realfile =~ /\.S$/ &&
3758de93245cSAditya Srivastava		    $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3759de93245cSAditya Srivastava			WARN("AVOID_L_PREFIX",
3760de93245cSAditya Srivastava			     "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr);
3761de93245cSAditya Srivastava		}
3762de93245cSAditya Srivastava
3763b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3764de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
37650a920b5bSAndy Whitcroft
37660a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3767713a09deSAntonio Borneo# more than $tabsize must use tabs.
3768c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3769c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3770c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3771d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
37723705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
37733705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
37743705ce5bSJoe Perches			    $fix) {
3775194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
37763705ce5bSJoe Perches			}
37770a920b5bSAndy Whitcroft		}
37780a920b5bSAndy Whitcroft
377908e44365SAlberto Panizzo# check for space before tabs.
378008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
378108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
37823705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
37833705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
37843705ce5bSJoe Perches			    $fix) {
3785194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3786713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3787194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3788c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
37893705ce5bSJoe Perches			}
379008e44365SAlberto Panizzo		}
379108e44365SAlberto Panizzo
37926a487211SJoe Perches# check for assignments on the start of a line
37936a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3794da7355abSAditya Srivastava			my $operator = $1;
3795da7355abSAditya Srivastava			if (CHK("ASSIGNMENT_CONTINUATIONS",
3796da7355abSAditya Srivastava				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3797da7355abSAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
3798da7355abSAditya Srivastava				# add assignment operator to the previous line, remove from current line
3799da7355abSAditya Srivastava				$fixed[$fixlinenr - 1] .= " $operator";
3800da7355abSAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3801da7355abSAditya Srivastava			}
38026a487211SJoe Perches		}
38036a487211SJoe Perches
3804d1fe9c09SJoe Perches# check for && or || at the start of a line
3805d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
38068e08f076SAditya Srivastava			my $operator = $1;
38078e08f076SAditya Srivastava			if (CHK("LOGICAL_CONTINUATIONS",
38088e08f076SAditya Srivastava				"Logical continuations should be on the previous line\n" . $hereprev) &&
38098e08f076SAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
38108e08f076SAditya Srivastava				# insert logical operator at last non-comment, non-whitepsace char on previous line
38118e08f076SAditya Srivastava				$prevline =~ /[\s$;]*$/;
38128e08f076SAditya Srivastava				my $line_end = substr($prevrawline, $-[0]);
38138e08f076SAditya Srivastava				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
38148e08f076SAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
38158e08f076SAditya Srivastava			}
3816d1fe9c09SJoe Perches		}
3817d1fe9c09SJoe Perches
3818a91e8994SJoe Perches# check indentation starts on a tab stop
38195b57980dSJoe Perches		if ($perl_version_ok &&
3820bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3821a91e8994SJoe Perches			my $indent = length($1);
3822713a09deSAntonio Borneo			if ($indent % $tabsize) {
3823a91e8994SJoe Perches				if (WARN("TABSTOP",
3824a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3825a91e8994SJoe Perches				    $fix) {
3826713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3827a91e8994SJoe Perches				}
3828a91e8994SJoe Perches			}
3829a91e8994SJoe Perches		}
3830a91e8994SJoe Perches
3831d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
38325b57980dSJoe Perches		if ($perl_version_ok &&
3833fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3834d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3835d1fe9c09SJoe Perches			my $oldindent = $1;
3836d1fe9c09SJoe Perches			my $rest = $2;
3837d1fe9c09SJoe Perches
3838d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3839d1fe9c09SJoe Perches			if ($pos >= 0) {
3840b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3841b34a26f3SJoe Perches				my $newindent = $2;
3842d1fe9c09SJoe Perches
3843d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3844713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3845713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3846d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3847d1fe9c09SJoe Perches
3848d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3849d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
38503705ce5bSJoe Perches
38513705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
38523705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
38533705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3854194f66fcSJoe Perches						$fixed[$fixlinenr] =~
38553705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
38563705ce5bSJoe Perches					}
3857d1fe9c09SJoe Perches				}
3858d1fe9c09SJoe Perches			}
3859d1fe9c09SJoe Perches		}
3860d1fe9c09SJoe Perches
38616ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
38626ab3a970SJoe Perches# avoid checking a few false positives:
38636ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
38646ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
38656ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
38666ab3a970SJoe Perches#   multiline macros that define functions
38676ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
38686ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
38696ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
38703705ce5bSJoe Perches			if (CHK("SPACING",
3871f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
38723705ce5bSJoe Perches			    $fix) {
3873194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3874f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
38753705ce5bSJoe Perches			}
3876aad4f614SJoe Perches		}
3877aad4f614SJoe Perches
387886406b1cSJoe Perches# Block comment styles
387986406b1cSJoe Perches# Networking with an initial /*
388005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3881fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
388285ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
3883c70735c2SŁukasz Stelmach		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
388405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
388505880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
388605880600SJoe Perches		}
388705880600SJoe Perches
388886406b1cSJoe Perches# Block comments use * on subsequent lines
388986406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
389086406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3891a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
389261135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3893a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
389486406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
389586406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3896a605e32eSJoe Perches		}
3897a605e32eSJoe Perches
389886406b1cSJoe Perches# Block comments use */ on trailing lines
389986406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3900c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3901c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3902c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
390386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
390486406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
390505880600SJoe Perches		}
390605880600SJoe Perches
390708eb9b80SJoe Perches# Block comment * alignment
390808eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3909af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3910af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3911af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
391208eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3913af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3914af207524SJoe Perches			my $oldindent;
391508eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3916af207524SJoe Perches			if (defined($1)) {
3917af207524SJoe Perches				$oldindent = expand_tabs($1);
3918af207524SJoe Perches			} else {
3919af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3920af207524SJoe Perches				$oldindent = expand_tabs($1);
3921af207524SJoe Perches			}
392208eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
392308eb9b80SJoe Perches			my $newindent = $1;
392408eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3925af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
392608eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
392708eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
392808eb9b80SJoe Perches			}
392908eb9b80SJoe Perches		}
393008eb9b80SJoe Perches
39317f619191SJoe Perches# check for missing blank lines after struct/union declarations
39327f619191SJoe Perches# with exceptions for various attributes and macros
39337f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
39347f619191SJoe Perches		    $line =~ /^\+/ &&
39357f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
393605dc40e6SJoe Perches		      $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param)/ ||
39377f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
39387f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
39397f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
39407f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
39417f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
39420bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
39437f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3944d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3945d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3946d752fcc8SJoe Perches			    $fix) {
3947f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3948d752fcc8SJoe Perches			}
39497f619191SJoe Perches		}
39507f619191SJoe Perches
3951365dd4eaSJoe Perches# check for multiple consecutive blank lines
3952365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3953365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3954365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3955d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3956d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3957d752fcc8SJoe Perches			    $fix) {
3958f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3959d752fcc8SJoe Perches			}
3960d752fcc8SJoe Perches
3961365dd4eaSJoe Perches			$last_blank_line = $linenr;
3962365dd4eaSJoe Perches		}
3963365dd4eaSJoe Perches
39643b617e3bSJoe Perches# check for missing blank lines after declarations
3965b5e8736aSJoe Perches# (declarations must have the same indentation and not be at the start of line)
3966b5e8736aSJoe Perches		if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3967b5e8736aSJoe Perches			# use temporaries
3968b5e8736aSJoe Perches			my $sl = $sline;
3969b5e8736aSJoe Perches			my $pl = $prevline;
3970b5e8736aSJoe Perches			# remove $Attribute/$Sparse uses to simplify comparisons
3971b5e8736aSJoe Perches			$sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3972b5e8736aSJoe Perches			$pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3973b5e8736aSJoe Perches			if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
39745a4e1fd3SJoe Perches			# function pointer declarations
3975b5e8736aSJoe Perches			     $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
39763f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
3977b5e8736aSJoe Perches			     $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
39783f7bac03SJoe Perches			# known declaration macros
3979b5e8736aSJoe Perches			     $pl =~ /^\+\s+$declaration_macros/) &&
39803f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
3981b5e8736aSJoe Perches			    !($pl =~ /^\+\s+$c90_Keywords\b/ ||
39823f7bac03SJoe Perches			# other possible extensions of declaration lines
3983b5e8736aSJoe Perches			      $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
39843f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
3985b5e8736aSJoe Perches			      $pl =~ /(?:\{\s*|\\)$/) &&
39863f7bac03SJoe Perches			# looks like a declaration
3987b5e8736aSJoe Perches			    !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
39885a4e1fd3SJoe Perches			# function pointer declarations
3989b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
39903f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
3991b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
39923f7bac03SJoe Perches			# known declaration macros
3993b5e8736aSJoe Perches			      $sl =~ /^\+\s+$declaration_macros/ ||
39943f7bac03SJoe Perches			# start of struct or union or enum
3995b5e8736aSJoe Perches			      $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
39963f7bac03SJoe Perches			# start or end of block or continuation of declaration
3997b5e8736aSJoe Perches			      $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
39983f7bac03SJoe Perches			# bitfield continuation
3999b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
40003f7bac03SJoe Perches			# other possible extensions of declaration lines
4001b5e8736aSJoe Perches			      $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
4002d752fcc8SJoe Perches				if (WARN("LINE_SPACING",
4003d752fcc8SJoe Perches					 "Missing a blank line after declarations\n" . $hereprev) &&
4004d752fcc8SJoe Perches				    $fix) {
4005f2d7e4d4SJoe Perches					fix_insert_line($fixlinenr, "\+");
4006d752fcc8SJoe Perches				}
40073b617e3bSJoe Perches			}
4008b5e8736aSJoe Perches		}
40093b617e3bSJoe Perches
40105f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
40116b4c5bebSAndy Whitcroft# Exceptions:
40126b4c5bebSAndy Whitcroft#  1) within comments
40136b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
40146b4c5bebSAndy Whitcroft#  3) hanging labels
40153705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
40165f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
40173705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
40183705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
40193705ce5bSJoe Perches			    $fix) {
4020194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
40213705ce5bSJoe Perches			}
40225f7ddae6SRaffaele Recalcati		}
40235f7ddae6SRaffaele Recalcati
4024b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
4025b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
4026b9ea10d6SAndy Whitcroft
40275751a24eSJoe Perches# check for unusual line ending [ or (
40285751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
40295751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
40305751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
40315751a24eSJoe Perches		}
40325751a24eSJoe Perches
40334dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
40344dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
40354dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
40364dbed76fSJoe Perches			$context_function = $1;
40374dbed76fSJoe Perches		}
40384dbed76fSJoe Perches
40394dbed76fSJoe Perches# check if this appears to be the end of function declaration
40404dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
40414dbed76fSJoe Perches			undef $context_function;
40424dbed76fSJoe Perches		}
40434dbed76fSJoe Perches
4044032a4c0fSJoe Perches# check indentation of any line with a bare else
4045840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
4046032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
4047032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4048032a4c0fSJoe Perches			my $tabs = length($1) + 1;
4049840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4050840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4051840080a0SJoe Perches			     defined $lines[$linenr] &&
4052840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
4053032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
4054032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
4055032a4c0fSJoe Perches			}
4056032a4c0fSJoe Perches		}
4057032a4c0fSJoe Perches
4058c00df19aSJoe Perches# check indentation of a line with a break;
4059dc58bc55SJoe Perches# if the previous line is a goto, return or break
4060dc58bc55SJoe Perches# and is indented the same # of tabs
4061c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4062c00df19aSJoe Perches			my $tabs = $1;
4063dc58bc55SJoe Perches			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4064dc58bc55SJoe Perches				if (WARN("UNNECESSARY_BREAK",
4065dc58bc55SJoe Perches					 "break is not useful after a $1\n" . $hereprev) &&
4066dc58bc55SJoe Perches				    $fix) {
4067dc58bc55SJoe Perches					fix_delete_line($fixlinenr, $rawline);
4068dc58bc55SJoe Perches				}
4069c00df19aSJoe Perches			}
4070c00df19aSJoe Perches		}
4071c00df19aSJoe Perches
4072c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
4073cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
4074000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
4075000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
4076c2fdda0dSAndy Whitcroft		}
407722f2a2efSAndy Whitcroft
407856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
407956e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
408056e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
408156e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
408256e77d70SJoe Perches		}
408356e77d70SJoe Perches
40849c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
40852b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
40862b474a1aSAndy Whitcroft		    $realline_next);
40873e469cdcSAndy Whitcroft#print "LINE<$line>\n";
4088ca819864SJoe Perches		if ($linenr > $suppress_statement &&
40891b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
4090170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4091f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4092171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
4093171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
4094171ae1a4SAndy Whitcroft
40953e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
40963e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
40973e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
40983e469cdcSAndy Whitcroft			# until we hit end of it.
40993e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
41003e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
41013e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
41023e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
41033e469cdcSAndy Whitcroft			}
4104f74bd194SAndy Whitcroft
41052b474a1aSAndy Whitcroft			# Find the real next line.
41062b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
41072b474a1aSAndy Whitcroft			if (defined $realline_next &&
41082b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
41092b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
41102b474a1aSAndy Whitcroft				$realline_next++;
41112b474a1aSAndy Whitcroft			}
41122b474a1aSAndy Whitcroft
4113171ae1a4SAndy Whitcroft			my $s = $stat;
4114171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
4115cf655043SAndy Whitcroft
4116c2fdda0dSAndy Whitcroft			# Ignore goto labels.
4117171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
4118c2fdda0dSAndy Whitcroft
4119c2fdda0dSAndy Whitcroft			# Ignore functions being called
4120171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
4121c2fdda0dSAndy Whitcroft
4122463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
4123463f2864SAndy Whitcroft
4124c45dcabdSAndy Whitcroft			# declarations always start with types
4125d2506586SAndy 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) {
4126c45dcabdSAndy Whitcroft				my $type = $1;
4127c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
4128c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
4129c45dcabdSAndy Whitcroft
41306c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
4131a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
4132c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
4133c2fdda0dSAndy Whitcroft			}
41348905a67cSAndy Whitcroft
41356c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
413665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
4137c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
41389c0ca6f9SAndy Whitcroft			}
41398905a67cSAndy Whitcroft
41408905a67cSAndy Whitcroft			# Check for any sort of function declaration.
41418905a67cSAndy Whitcroft			# int foo(something bar, other baz);
41428905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
4143171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
41448905a67cSAndy Whitcroft				my ($name_len) = length($1);
41458905a67cSAndy Whitcroft
4146cf655043SAndy Whitcroft				my $ctx = $s;
4147773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
41488905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
4149cf655043SAndy Whitcroft
41508905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
4151c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
41528905a67cSAndy Whitcroft
4153c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
41548905a67cSAndy Whitcroft					}
41558905a67cSAndy Whitcroft				}
41568905a67cSAndy Whitcroft			}
41578905a67cSAndy Whitcroft
41589c0ca6f9SAndy Whitcroft		}
41599c0ca6f9SAndy Whitcroft
416000df344fSAndy Whitcroft#
416100df344fSAndy Whitcroft# Checks which may be anchored in the context.
416200df344fSAndy Whitcroft#
416300df344fSAndy Whitcroft
416400df344fSAndy Whitcroft# Check for switch () and associated case and default
416500df344fSAndy Whitcroft# statements should be at the same indent.
416600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
416700df344fSAndy Whitcroft			my $err = '';
416800df344fSAndy Whitcroft			my $sep = '';
416900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
417000df344fSAndy Whitcroft			shift(@ctx);
417100df344fSAndy Whitcroft			for my $ctx (@ctx) {
417200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
417300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
417400df344fSAndy Whitcroft							$indent != $cindent) {
417500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
417600df344fSAndy Whitcroft					$sep = '';
417700df344fSAndy Whitcroft				} else {
417800df344fSAndy Whitcroft					$sep = "[...]\n";
417900df344fSAndy Whitcroft				}
418000df344fSAndy Whitcroft			}
418100df344fSAndy Whitcroft			if ($err ne '') {
4182000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
4183000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
4184de7d4f0eSAndy Whitcroft			}
4185de7d4f0eSAndy Whitcroft		}
4186de7d4f0eSAndy Whitcroft
4187de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
4188de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
41890fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4190773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
4191773647a0SAndy Whitcroft
41929c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
41938eef05ddSJoe Perches
41948eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
41958eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
41968eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
41978eef05ddSJoe Perches			}
41988eef05ddSJoe Perches
4199de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
4200de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
4201de7d4f0eSAndy Whitcroft
4202548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
4203548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
4204de7d4f0eSAndy Whitcroft
4205548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4206548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
4207548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
4208548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4209548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4210773647a0SAndy Whitcroft				$ctx_ln++;
4211773647a0SAndy Whitcroft			}
4212548596d5SAndy Whitcroft
421353210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
421453210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4215773647a0SAndy Whitcroft
4216773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4217000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
4218000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
421901464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
422000df344fSAndy Whitcroft			}
4221773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4222773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
4223773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
4224773647a0SAndy Whitcroft			{
42259c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
42269c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
4227000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
4228000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
422901464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
42309c0ca6f9SAndy Whitcroft				}
42319c0ca6f9SAndy Whitcroft			}
423200df344fSAndy Whitcroft		}
423300df344fSAndy Whitcroft
42344d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
4235f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
42363e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
42373e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
42383e469cdcSAndy Whitcroft					if (!defined $stat);
42394d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
42404d001e4dSAndy Whitcroft
42414d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
42424d001e4dSAndy Whitcroft
42439f5af480SJoe Perches			# remove inline comments
42449f5af480SJoe Perches			$s =~ s/$;/ /g;
42459f5af480SJoe Perches			$c =~ s/$;/ /g;
42464d001e4dSAndy Whitcroft
42474d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
42486f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
42496f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
42504d001e4dSAndy Whitcroft
42519f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
42529f5af480SJoe Perches			# none on the first line, and are going to readd them
42539f5af480SJoe Perches			# where necessary.
42549f5af480SJoe Perches			$s =~ s/\n./\n/gs;
42559f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
42569f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
42579f5af480SJoe Perches			}
42589f5af480SJoe Perches
42594d001e4dSAndy Whitcroft			# We want to check the first line inside the block
42604d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
42614d001e4dSAndy Whitcroft			#  1) any blank line termination
42624d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
42634d001e4dSAndy Whitcroft			#  3) any do (...) {
42644d001e4dSAndy Whitcroft			my $continuation = 0;
42654d001e4dSAndy Whitcroft			my $check = 0;
42664d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
42674d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
42684d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
42694d001e4dSAndy Whitcroft				$continuation = 1;
42704d001e4dSAndy Whitcroft			}
42719bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
42724d001e4dSAndy Whitcroft				$check = 1;
42734d001e4dSAndy Whitcroft				$cond_lines++;
42744d001e4dSAndy Whitcroft			}
42754d001e4dSAndy Whitcroft
42764d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
42774d001e4dSAndy Whitcroft			# preprocessor statement.
42784d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
42794d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
42804d001e4dSAndy Whitcroft				$check = 0;
42814d001e4dSAndy Whitcroft			}
42824d001e4dSAndy Whitcroft
42839bd49efeSAndy Whitcroft			my $cond_ptr = -1;
4284740504c6SAndy Whitcroft			$continuation = 0;
42859bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
42869bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
42874d001e4dSAndy Whitcroft
4288f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
4289f16fa28fSAndy Whitcroft				# is not linear.
4290f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4291f16fa28fSAndy Whitcroft					$check = 0;
4292f16fa28fSAndy Whitcroft				}
4293f16fa28fSAndy Whitcroft
42949bd49efeSAndy Whitcroft				# Ignore:
42959bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
42969bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
42979bd49efeSAndy Whitcroft				#  3) labels.
4298740504c6SAndy Whitcroft				if ($continuation ||
4299740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
43009bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
43019bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
4302740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
430330dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
43049bd49efeSAndy Whitcroft						$cond_lines++;
43059bd49efeSAndy Whitcroft					}
43064d001e4dSAndy Whitcroft				}
430730dad6ebSAndy Whitcroft			}
43084d001e4dSAndy Whitcroft
43094d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
43104d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
43114d001e4dSAndy Whitcroft
43124d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
43134d001e4dSAndy Whitcroft			# this is not this patch's fault.
43144d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
43154d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
43164d001e4dSAndy Whitcroft				$check = 0;
43174d001e4dSAndy Whitcroft			}
43184d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
43194d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
43204d001e4dSAndy Whitcroft			}
43214d001e4dSAndy Whitcroft
43229bd49efeSAndy 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";
43234d001e4dSAndy Whitcroft
43249f5af480SJoe Perches			if ($check && $s ne '' &&
4325713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
43269f5af480SJoe Perches			     ($sindent < $indent) ||
4327f6950a73SJoe Perches			     ($sindent == $indent &&
4328f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4329713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
4330000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
4331000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
43324d001e4dSAndy Whitcroft			}
43334d001e4dSAndy Whitcroft		}
43344d001e4dSAndy Whitcroft
43356c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
43366c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
43371f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
43381f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
43396c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
4340c2fdda0dSAndy Whitcroft		if ($dbg_values) {
4341c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
4342cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
4343cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
43441f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
4345c2fdda0dSAndy Whitcroft		}
43466c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
43476c72ffaaSAndy Whitcroft
434800df344fSAndy Whitcroft#ignore lines not being added
43493705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
435000df344fSAndy Whitcroft
435199ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
435299ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
435399ca38c2SJoe Perches#	struct foo bar = *(&(bar));
435499ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
435599ca38c2SJoe Perches			my $var = $1;
435699ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
435799ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
435899ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
435999ca38c2SJoe Perches			}
436099ca38c2SJoe Perches		}
436199ca38c2SJoe Perches
436211ca40a0SJoe Perches# check for dereferences that span multiple lines
436311ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
436411ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
436511ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
436611ca40a0SJoe Perches			my $ref = $1;
436711ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
436811ca40a0SJoe Perches			$ref .= $1;
436911ca40a0SJoe Perches			$ref =~ s/\s//g;
437011ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
437111ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
437211ca40a0SJoe Perches		}
437311ca40a0SJoe Perches
4374a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
4375c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4376a1ce18e4SJoe Perches			my $type = $1;
4377a1ce18e4SJoe Perches			my $var = $2;
4378207a8e84SJoe Perches			$var = "" if (!defined $var);
4379207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4380a1ce18e4SJoe Perches				my $sign = $1;
4381a1ce18e4SJoe Perches				my $pointer = $2;
4382a1ce18e4SJoe Perches
4383a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
4384a1ce18e4SJoe Perches
4385a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
4386a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4387a1ce18e4SJoe Perches				    $fix) {
4388a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
4389207a8e84SJoe Perches					my $comp_pointer = $pointer;
4390207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
4391207a8e84SJoe Perches					$decl .= $comp_pointer;
4392207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
4393207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4394a1ce18e4SJoe Perches				}
4395a1ce18e4SJoe Perches			}
4396a1ce18e4SJoe Perches		}
4397a1ce18e4SJoe Perches
4398653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
43997429c690SAndy Whitcroft		if ($dbg_type) {
44007429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
4401000d1cc1SJoe Perches				ERROR("TEST_TYPE",
4402000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
44037429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4404000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
4405000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
44067429c690SAndy Whitcroft			}
4407653d4876SAndy Whitcroft			next;
4408653d4876SAndy Whitcroft		}
4409a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
4410a1ef277eSAndy Whitcroft		if ($dbg_attr) {
44119360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
4412000d1cc1SJoe Perches				ERROR("TEST_ATTR",
4413000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
44149360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4415000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
4416000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
4417a1ef277eSAndy Whitcroft			}
4418a1ef277eSAndy Whitcroft			next;
4419a1ef277eSAndy Whitcroft		}
4420653d4876SAndy Whitcroft
4421f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
442299423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
442399423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
4424d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4425d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4426f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4427f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4428f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4429d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4430d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4431f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4432d752fcc8SJoe Perches				$fixedline = $line;
44338d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4434f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4435d752fcc8SJoe Perches			}
4436f0a594c1SAndy Whitcroft		}
4437f0a594c1SAndy Whitcroft
443800df344fSAndy Whitcroft#
443900df344fSAndy Whitcroft# Checks which are anchored on the added line.
444000df344fSAndy Whitcroft#
444100df344fSAndy Whitcroft
4442653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4443c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4444653d4876SAndy Whitcroft			my $path = $1;
4445653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4446000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4447495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4448495e9d84SJoe Perches			}
4449495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4450495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4451495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4452653d4876SAndy Whitcroft			}
4453653d4876SAndy Whitcroft		}
4454653d4876SAndy Whitcroft
445500df344fSAndy Whitcroft# no C99 // comments
445600df344fSAndy Whitcroft		if ($line =~ m{//}) {
44573705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
44583705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
44593705ce5bSJoe Perches			    $fix) {
4460194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
44613705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
44623705ce5bSJoe Perches					my $comment = trim($1);
4463194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
44643705ce5bSJoe Perches				}
44653705ce5bSJoe Perches			}
446600df344fSAndy Whitcroft		}
446700df344fSAndy Whitcroft		# Remove C99 comments.
44680a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
44696c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
44700a920b5bSAndy Whitcroft
44712b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
44722b474a1aSAndy Whitcroft# the whole statement.
44732b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
44742b474a1aSAndy Whitcroft		if (defined $realline_next &&
44752b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
44762b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
447736794822SChristoph Hellwig		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
44783cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
44793cbf62dfSAndy Whitcroft			# a prefix:
44803cbf62dfSAndy Whitcroft			#   XXX(foo);
44813cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4482653d4876SAndy Whitcroft			my $name = $1;
448370a11659SJoe Perches			$name =~ s/^\s*($Ident).*/$1/;
448487a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
44853cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
44863cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
44873cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
44883cbf62dfSAndy Whitcroft
44893cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
44902b474a1aSAndy Whitcroft				\n.}\s*$|
449148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
449248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
449348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
44942b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
44952b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
449648012058SAndy Whitcroft			    )/x) {
44972b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
44982b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
44992b474a1aSAndy Whitcroft			} else {
45002b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
45010a920b5bSAndy Whitcroft			}
45020a920b5bSAndy Whitcroft		}
45032b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
45042b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
450536794822SChristoph Hellwig		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
45062b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
45072b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
45082b474a1aSAndy Whitcroft		}
45092b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
45102b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4511000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4512000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
45132b474a1aSAndy Whitcroft		}
45140a920b5bSAndy Whitcroft
45155150bda4SJoe Eloff# check for global initialisers.
45165b8f82e1SSong Liu		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
45175b8f82e1SSong Liu		    !exclude_global_initialisers($realfile)) {
4518d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
45196d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4520d5e616fcSJoe Perches			    $fix) {
45216d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4522d5e616fcSJoe Perches			}
4523f0a594c1SAndy Whitcroft		}
45240a920b5bSAndy Whitcroft# check for static initialisers.
45256d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4526d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
45276d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4528d5e616fcSJoe Perches				      $herecurr) &&
4529d5e616fcSJoe Perches			    $fix) {
45306d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4531d5e616fcSJoe Perches			}
45320a920b5bSAndy Whitcroft		}
45330a920b5bSAndy Whitcroft
45341813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
45351813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
45361813087dSJoe Perches			my $tmp = trim($1);
45371813087dSJoe Perches			WARN("MISORDERED_TYPE",
45381813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
45391813087dSJoe Perches		}
45401813087dSJoe Perches
4541809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4542809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4543809e082eSJoe Perches			my $type = trim($1);
4544809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4545809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4546809e082eSJoe Perches			my $new_type = $type;
4547809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4548809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4549809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4550809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4551809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4552809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4553809e082eSJoe Perches			$new_type = trim($new_type);
4554809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4555809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4556809e082eSJoe Perches			    $fix) {
4557809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4558809e082eSJoe Perches			}
4559809e082eSJoe Perches		}
4560809e082eSJoe Perches
4561cb710ecaSJoe Perches# check for static const char * arrays.
4562cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4563000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4564000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4565cb710ecaSJoe Perches				$herecurr);
4566cb710ecaSJoe Perches		}
4567cb710ecaSJoe Perches
456877b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
456977b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
457077b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
457177b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
457277b8c0a8SJoe Perches			    $fix) {
457377b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
457477b8c0a8SJoe Perches			}
457577b8c0a8SJoe Perches		}
457677b8c0a8SJoe Perches
4577cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4578cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4579000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4580000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4581cb710ecaSJoe Perches				$herecurr);
4582cb710ecaSJoe Perches		}
4583cb710ecaSJoe Perches
4584ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4585ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4586ab7e23f3SJoe Perches			my $found = $1;
4587ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4588ab7e23f3SJoe Perches				WARN("CONST_CONST",
4589ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4590ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4591ab7e23f3SJoe Perches				WARN("CONST_CONST",
4592ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4593ab7e23f3SJoe Perches			}
4594ab7e23f3SJoe Perches		}
4595ab7e23f3SJoe Perches
459673169765SJoe Perches# check for const static or static <non ptr type> const declarations
459773169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
459873169765SJoe Perches		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
459973169765SJoe Perches		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
460073169765SJoe Perches			if (WARN("STATIC_CONST",
460173169765SJoe Perches				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
460273169765SJoe Perches			    $fix) {
460373169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
460473169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
460573169765SJoe Perches			}
460673169765SJoe Perches		}
460773169765SJoe Perches
46089b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
46099b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
46109b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
46119b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
46129b0fa60dSJoe Perches				$herecurr);
46139b0fa60dSJoe Perches		}
46149b0fa60dSJoe Perches
4615b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4616b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4617b598b670SJoe Perches			my $array = $1;
4618b598b670SJoe 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*\))@) {
4619b598b670SJoe Perches				my $array_div = $1;
4620b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4621b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4622b598b670SJoe Perches				    $fix) {
4623b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4624b598b670SJoe Perches				}
4625b598b670SJoe Perches			}
4626b598b670SJoe Perches		}
4627b598b670SJoe Perches
4628b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
462916b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4630b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4631b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4632b36190c5SJoe Perches			    $fix) {
4633194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4634b36190c5SJoe Perches			}
4635b36190c5SJoe Perches		}
4636b36190c5SJoe Perches
4637653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4638653d4876SAndy Whitcroft# make sense.
4639653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
46408054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4641c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
46428ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
464346d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4644000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4645000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
46460a920b5bSAndy Whitcroft		}
46470a920b5bSAndy Whitcroft
46480a920b5bSAndy Whitcroft# * goes on variable not on type
464965863862SAndy Whitcroft		# (char*[ const])
4650bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4651bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
46523705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4653d8aaf121SAndy Whitcroft
465465863862SAndy Whitcroft			# Should start with a space.
465565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
465665863862SAndy Whitcroft			# Should not end with a space.
465765863862SAndy Whitcroft			$to =~ s/\s+$//;
465865863862SAndy Whitcroft			# '*'s should not have spaces between.
4659f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
466065863862SAndy Whitcroft			}
4661d8aaf121SAndy Whitcroft
46623705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
466365863862SAndy Whitcroft			if ($from ne $to) {
46643705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
46653705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
46663705ce5bSJoe Perches				    $fix) {
46673705ce5bSJoe Perches					my $sub_from = $ident;
46683705ce5bSJoe Perches					my $sub_to = $ident;
46693705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4670194f66fcSJoe Perches					$fixed[$fixlinenr] =~
46713705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
46723705ce5bSJoe Perches				}
467365863862SAndy Whitcroft			}
4674bfcb2cc7SAndy Whitcroft		}
4675bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4676bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
46773705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4678d8aaf121SAndy Whitcroft
467965863862SAndy Whitcroft			# Should start with a space.
468065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
468165863862SAndy Whitcroft			# Should not end with a space.
468265863862SAndy Whitcroft			$to =~ s/\s+$//;
468365863862SAndy Whitcroft			# '*'s should not have spaces between.
4684f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
468565863862SAndy Whitcroft			}
468665863862SAndy Whitcroft			# Modifiers should have spaces.
468765863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
468865863862SAndy Whitcroft
46893705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4690667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
46913705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
46923705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
46933705ce5bSJoe Perches				    $fix) {
46943705ce5bSJoe Perches
46953705ce5bSJoe Perches					my $sub_from = $match;
46963705ce5bSJoe Perches					my $sub_to = $match;
46973705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4698194f66fcSJoe Perches					$fixed[$fixlinenr] =~
46993705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
47003705ce5bSJoe Perches				}
470165863862SAndy Whitcroft			}
47020a920b5bSAndy Whitcroft		}
47030a920b5bSAndy Whitcroft
47049d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
47059d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
47060675a8fbSJean Delvare			my $msg_level = \&WARN;
47070675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
47080675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
47099d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
47109d3e3c70SJoe Perches		}
47110a920b5bSAndy Whitcroft
47129d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
47138905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4714000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4715000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
47168905a67cSAndy Whitcroft		}
47178905a67cSAndy Whitcroft
471817441227SJoe Perches# check for uses of printk_ratelimit
471917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4720000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4721000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
472217441227SJoe Perches		}
472317441227SJoe Perches
4724eeef5733SJoe Perches# printk should use KERN_* levels
4725eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4726000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4727eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
472800df344fSAndy Whitcroft		}
47290a920b5bSAndy Whitcroft
4730f5eea3b0SJoe Perches# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4731f5eea3b0SJoe Perches		if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4732f5eea3b0SJoe Perches			my $printk = $1;
4733f5eea3b0SJoe Perches			my $modifier = $2;
4734f5eea3b0SJoe Perches			my $orig = $3;
4735f5eea3b0SJoe Perches			$modifier = "" if (!defined($modifier));
4736243f3803SJoe Perches			my $level = lc($orig);
4737243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
47388f26b837SJoe Perches			my $level2 = $level;
47398f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4740f5eea3b0SJoe Perches			$level .= $modifier;
4741f5eea3b0SJoe Perches			$level2 .= $modifier;
4742243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4743f5eea3b0SJoe Perches			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to $printk(KERN_$orig ...\n" . $herecurr);
4744243f3803SJoe Perches		}
4745243f3803SJoe Perches
4746f5eea3b0SJoe Perches# prefer dev_<level> to dev_printk(KERN_<LEVEL>
4747dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4748dc139313SJoe Perches			my $orig = $1;
4749dc139313SJoe Perches			my $level = lc($orig);
4750dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4751dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4752dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4753dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4754dc139313SJoe Perches		}
4755dc139313SJoe Perches
47568020b253SNicolas Boichat# trace_printk should not be used in production code.
47578020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
47588020b253SNicolas Boichat			WARN("TRACE_PRINTK",
47598020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
47608020b253SNicolas Boichat		}
47618020b253SNicolas Boichat
476291c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
476391c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
476491c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
476591c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
476691c9afafSAndy Lutomirski			WARN("ENOSYS",
476791c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
476891c9afafSAndy Lutomirski		}
476991c9afafSAndy Lutomirski
47706b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
47716b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
47726b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
47736b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
47746b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
47756b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
47766b9ea5ffSJakub Kicinski			    $fix) {
47776b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
47786b9ea5ffSJakub Kicinski			}
47796b9ea5ffSJakub Kicinski		}
47806b9ea5ffSJakub Kicinski
4781653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4782653d4876SAndy Whitcroft# or if closed on same line
47835b57980dSJoe Perches		if ($perl_version_ok &&
47842d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
47852d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
47862d453e3bSJoe Perches		    $sline !~ /}/) {
47878d182478SJoe Perches			if (ERROR("OPEN_BRACE",
47882d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
47898d182478SJoe Perches			    $fix) {
47908d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
47918d182478SJoe Perches				my $fixed_line = $rawline;
479203f49351SDwaipayan Ray				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
47938d182478SJoe Perches				my $line1 = $1;
47948d182478SJoe Perches				my $line2 = $2;
47958d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
47968d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
47978d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
47988d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
47998d182478SJoe Perches				}
48008d182478SJoe Perches			}
48010a920b5bSAndy Whitcroft		}
4802653d4876SAndy Whitcroft
48038905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
48048905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
48058905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
48068d182478SJoe Perches			if (ERROR("OPEN_BRACE",
48078d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
48088d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
48098d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
48108d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
48118d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
48128d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
48138d182478SJoe Perches				$fixedline = $rawline;
48148d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
48158d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
48168d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
48178d182478SJoe Perches				}
48188d182478SJoe Perches			}
48198905a67cSAndy Whitcroft		}
48208905a67cSAndy Whitcroft
48210c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
48223705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
48233705ce5bSJoe Perches			if (WARN("SPACING",
48243705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
48253705ce5bSJoe Perches			    $fix) {
4826194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48273705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
48283705ce5bSJoe Perches			}
48290c73b4ebSAndy Whitcroft		}
48300c73b4ebSAndy Whitcroft
483131070b5dSJoe Perches# Function pointer declarations
483231070b5dSJoe Perches# check spacing between type, funcptr, and args
483331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
483491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
483531070b5dSJoe Perches			my $declare = $1;
483631070b5dSJoe Perches			my $pre_pointer_space = $2;
483731070b5dSJoe Perches			my $post_pointer_space = $3;
483831070b5dSJoe Perches			my $funcname = $4;
483931070b5dSJoe Perches			my $post_funcname_space = $5;
484031070b5dSJoe Perches			my $pre_args_space = $6;
484131070b5dSJoe Perches
484291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
484391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
484491f72e9cSJoe Perches# don't need a space so don't warn for those.
484591f72e9cSJoe Perches			my $post_declare_space = "";
484691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
484791f72e9cSJoe Perches				$post_declare_space = $1;
484891f72e9cSJoe Perches				$declare = rtrim($declare);
484991f72e9cSJoe Perches			}
485091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
485131070b5dSJoe Perches				WARN("SPACING",
485231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
485391f72e9cSJoe Perches				$post_declare_space = " ";
485431070b5dSJoe Perches			}
485531070b5dSJoe Perches
485631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
485791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
485891f72e9cSJoe Perches# equivalent to
485991f72e9cSJoe Perches#	int  foo(int bar, ...)
486091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
486191f72e9cSJoe Perches#
486291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
486391f72e9cSJoe Perches#				WARN("SPACING",
486491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
486591f72e9cSJoe Perches#			}
486631070b5dSJoe Perches
486731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
486831070b5dSJoe Perches			if (defined $pre_pointer_space &&
486931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
487031070b5dSJoe Perches				WARN("SPACING",
487131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
487231070b5dSJoe Perches			}
487331070b5dSJoe Perches
487431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
487531070b5dSJoe Perches			if (defined $post_pointer_space &&
487631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
487731070b5dSJoe Perches				WARN("SPACING",
487831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
487931070b5dSJoe Perches			}
488031070b5dSJoe Perches
488131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
488231070b5dSJoe Perches			if (defined $post_funcname_space &&
488331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
488431070b5dSJoe Perches				WARN("SPACING",
488531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
488631070b5dSJoe Perches			}
488731070b5dSJoe Perches
488831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
488931070b5dSJoe Perches			if (defined $pre_args_space &&
489031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
489131070b5dSJoe Perches				WARN("SPACING",
489231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
489331070b5dSJoe Perches			}
489431070b5dSJoe Perches
489531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4896194f66fcSJoe Perches				$fixed[$fixlinenr] =~
489791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
489831070b5dSJoe Perches			}
489931070b5dSJoe Perches		}
490031070b5dSJoe Perches
49018d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
49028d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4903fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4904fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
49058d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
49068d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
49078d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4908fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
490938dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
49103705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
49113705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
49123705ce5bSJoe Perches				    $fix) {
4913194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
49143705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
49153705ce5bSJoe Perches				}
49168d31cfceSAndy Whitcroft			}
49178d31cfceSAndy Whitcroft		}
49188d31cfceSAndy Whitcroft
4919f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
49206c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4921c2fdda0dSAndy Whitcroft			my $name = $1;
4922773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4923773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4924c2fdda0dSAndy Whitcroft
4925c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4926773647a0SAndy Whitcroft			if ($name =~ /^(?:
4927773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4928773647a0SAndy Whitcroft				volatile|__volatile__|
4929773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4930773647a0SAndy Whitcroft				asm|__asm__)$/x)
4931773647a0SAndy Whitcroft			{
4932c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4933c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4934c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4935c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4936773647a0SAndy Whitcroft
4937773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4938c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4939c2fdda0dSAndy Whitcroft
4940c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4941c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4942773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4943c2fdda0dSAndy Whitcroft
4944c2fdda0dSAndy Whitcroft			} else {
49453705ce5bSJoe Perches				if (WARN("SPACING",
49463705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
49473705ce5bSJoe Perches					     $fix) {
4948194f66fcSJoe Perches					$fixed[$fixlinenr] =~
49493705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
49503705ce5bSJoe Perches				}
4951f0a594c1SAndy Whitcroft			}
49526c72ffaaSAndy Whitcroft		}
49539a4cad4eSEric Nelson
4954653d4876SAndy Whitcroft# Check operator spacing.
49550a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
49563705ce5bSJoe Perches			my $fixed_line = "";
49573705ce5bSJoe Perches			my $line_fixed = 0;
49583705ce5bSJoe Perches
49599c0ca6f9SAndy Whitcroft			my $ops = qr{
49609c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
49619c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
49629c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
49631f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
496484731623SJoe Perches				\?:|\?|:
49659c0ca6f9SAndy Whitcroft			}x;
4966cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
49673705ce5bSJoe Perches
49683705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
49693705ce5bSJoe Perches##			foreach my $el (@elements) {
49703705ce5bSJoe Perches##				print("el: <$el>\n");
49713705ce5bSJoe Perches##			}
49723705ce5bSJoe Perches
49733705ce5bSJoe Perches			my @fix_elements = ();
497400df344fSAndy Whitcroft			my $off = 0;
49756c72ffaaSAndy Whitcroft
49763705ce5bSJoe Perches			foreach my $el (@elements) {
49773705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
49783705ce5bSJoe Perches				$off += length($el);
49793705ce5bSJoe Perches			}
49803705ce5bSJoe Perches
49813705ce5bSJoe Perches			$off = 0;
49823705ce5bSJoe Perches
49836c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4984b34c648bSJoe Perches			my $last_after = -1;
49856c72ffaaSAndy Whitcroft
49860a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
49873705ce5bSJoe Perches
49883705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
49893705ce5bSJoe Perches
49903705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
49913705ce5bSJoe Perches
49924a0df2efSAndy Whitcroft				$off += length($elements[$n]);
49934a0df2efSAndy Whitcroft
499425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4995773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4996773647a0SAndy Whitcroft				my $cc = '';
4997773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4998773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4999773647a0SAndy Whitcroft				}
5000773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
5001773647a0SAndy Whitcroft
50024a0df2efSAndy Whitcroft				my $a = '';
50034a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
50044a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
5005cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
50064a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
50074a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
5008773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
50094a0df2efSAndy Whitcroft
50100a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
50114a0df2efSAndy Whitcroft
50124a0df2efSAndy Whitcroft				my $c = '';
50130a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
50144a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
50154a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
5016cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
50174a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
50184a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
50198b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
50204a0df2efSAndy Whitcroft				} else {
50214a0df2efSAndy Whitcroft					$c = 'E';
50220a920b5bSAndy Whitcroft				}
50230a920b5bSAndy Whitcroft
50244a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
50254a0df2efSAndy Whitcroft
50264a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
50274a0df2efSAndy Whitcroft
50286c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
5029de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
50300a920b5bSAndy Whitcroft
503174048ed8SAndy Whitcroft				# Pull out the value of this operator.
50326c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
50330a920b5bSAndy Whitcroft
50341f65f947SAndy Whitcroft				# Get the full operator variant.
50351f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
50361f65f947SAndy Whitcroft
503713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
503813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
5039d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
504013214adfSAndy Whitcroft
5041cf655043SAndy Whitcroft#				# Ignore comments
5042cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
504313214adfSAndy Whitcroft
5044d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
504513214adfSAndy Whitcroft				} elsif ($op eq ';') {
5046cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
5047cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
50483705ce5bSJoe Perches						if (ERROR("SPACING",
50493705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
5050b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
50513705ce5bSJoe Perches							$line_fixed = 1;
50523705ce5bSJoe Perches						}
5053d8aaf121SAndy Whitcroft					}
5054d8aaf121SAndy Whitcroft
5055d8aaf121SAndy Whitcroft				# // is a comment
5056d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
50570a920b5bSAndy Whitcroft
5058b00e4814SJoe Perches				#   :   when part of a bitfield
5059b00e4814SJoe Perches				} elsif ($opv eq ':B') {
5060b00e4814SJoe Perches					# skip the bitfield test for now
5061b00e4814SJoe Perches
50621f65f947SAndy Whitcroft				# No spaces for:
50631f65f947SAndy Whitcroft				#   ->
5064b00e4814SJoe Perches				} elsif ($op eq '->') {
50654a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
50663705ce5bSJoe Perches						if (ERROR("SPACING",
50673705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
5068b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
50693705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
50703705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
50713705ce5bSJoe Perches							}
5072b34c648bSJoe Perches							$line_fixed = 1;
50733705ce5bSJoe Perches						}
50740a920b5bSAndy Whitcroft					}
50750a920b5bSAndy Whitcroft
50762381097bSJoe Perches				# , must not have a space before and must have a space on the right.
50770a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
50782381097bSJoe Perches					my $rtrim_before = 0;
50792381097bSJoe Perches					my $space_after = 0;
50802381097bSJoe Perches					if ($ctx =~ /Wx./) {
50812381097bSJoe Perches						if (ERROR("SPACING",
50822381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
50832381097bSJoe Perches							$line_fixed = 1;
50842381097bSJoe Perches							$rtrim_before = 1;
50852381097bSJoe Perches						}
50862381097bSJoe Perches					}
5087cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
50883705ce5bSJoe Perches						if (ERROR("SPACING",
50893705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
50903705ce5bSJoe Perches							$line_fixed = 1;
5091b34c648bSJoe Perches							$last_after = $n;
50922381097bSJoe Perches							$space_after = 1;
50932381097bSJoe Perches						}
50942381097bSJoe Perches					}
50952381097bSJoe Perches					if ($rtrim_before || $space_after) {
50962381097bSJoe Perches						if ($rtrim_before) {
50972381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
50982381097bSJoe Perches						} else {
50992381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
51002381097bSJoe Perches						}
51012381097bSJoe Perches						if ($space_after) {
51022381097bSJoe Perches							$good .= " ";
51033705ce5bSJoe Perches						}
51040a920b5bSAndy Whitcroft					}
51050a920b5bSAndy Whitcroft
51069c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
510774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
51089c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
51099c0ca6f9SAndy Whitcroft
51109c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
51119c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
51129c0ca6f9SAndy Whitcroft				# unary operator, or a cast
51139c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
511474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
51150d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
5116cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
51173705ce5bSJoe Perches						if (ERROR("SPACING",
51183705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
5119b34c648bSJoe Perches							if ($n != $last_after + 2) {
5120b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
51213705ce5bSJoe Perches								$line_fixed = 1;
51223705ce5bSJoe Perches							}
51230a920b5bSAndy Whitcroft						}
5124b34c648bSJoe Perches					}
5125a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
5126171ae1a4SAndy Whitcroft						# A unary '*' may be const
5127171ae1a4SAndy Whitcroft
5128171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
51293705ce5bSJoe Perches						if (ERROR("SPACING",
51303705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5131b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
51323705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
51333705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
51343705ce5bSJoe Perches							}
5135b34c648bSJoe Perches							$line_fixed = 1;
51363705ce5bSJoe Perches						}
51370a920b5bSAndy Whitcroft					}
51380a920b5bSAndy Whitcroft
51390a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
51400a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
5141773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
51423705ce5bSJoe Perches						if (ERROR("SPACING",
51433705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
5144b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
51453705ce5bSJoe Perches							$line_fixed = 1;
51463705ce5bSJoe Perches						}
51470a920b5bSAndy Whitcroft					}
5148773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
5149773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
51503705ce5bSJoe Perches						if (ERROR("SPACING",
51513705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5152b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
51533705ce5bSJoe Perches							$line_fixed = 1;
51543705ce5bSJoe Perches						}
5155653d4876SAndy Whitcroft					}
5156773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
51573705ce5bSJoe Perches						if (ERROR("SPACING",
51583705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5159b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
51603705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
51613705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5162773647a0SAndy Whitcroft							}
5163b34c648bSJoe Perches							$line_fixed = 1;
51643705ce5bSJoe Perches						}
51653705ce5bSJoe Perches					}
51660a920b5bSAndy Whitcroft
51670a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
51689c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
51699c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
51709c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
5171c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
5172c2fdda0dSAndy Whitcroft					 $op eq '%')
51730a920b5bSAndy Whitcroft				{
5174d2e025f3SJoe Perches					if ($check) {
5175d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5176d2e025f3SJoe Perches							if (CHK("SPACING",
5177d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
5178d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5179d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5180d2e025f3SJoe Perches								$line_fixed = 1;
5181d2e025f3SJoe Perches							}
5182d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5183d2e025f3SJoe Perches							if (CHK("SPACING",
5184d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
5185d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5186d2e025f3SJoe Perches								$line_fixed = 1;
5187d2e025f3SJoe Perches							}
5188d2e025f3SJoe Perches						}
5189d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
51903705ce5bSJoe Perches						if (ERROR("SPACING",
51913705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
5192b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5193b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5194b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5195b34c648bSJoe Perches							}
51963705ce5bSJoe Perches							$line_fixed = 1;
51973705ce5bSJoe Perches						}
51980a920b5bSAndy Whitcroft					}
51990a920b5bSAndy Whitcroft
52001f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
52011f65f947SAndy Whitcroft				# terminating a case value or a label.
52021f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
5203263afd39SChris Down					if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
52043705ce5bSJoe Perches						if (ERROR("SPACING",
52053705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5206b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
52073705ce5bSJoe Perches							$line_fixed = 1;
52083705ce5bSJoe Perches						}
52091f65f947SAndy Whitcroft					}
52101f65f947SAndy Whitcroft
52110a920b5bSAndy Whitcroft				# All the others need spaces both sides.
5212cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
52131f65f947SAndy Whitcroft					my $ok = 0;
52141f65f947SAndy Whitcroft
521522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
52161f65f947SAndy Whitcroft					if (($op eq '<' &&
52171f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
52181f65f947SAndy Whitcroft					    ($op eq '>' &&
52191f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
52201f65f947SAndy Whitcroft					{
52211f65f947SAndy Whitcroft						$ok = 1;
52221f65f947SAndy Whitcroft					}
52231f65f947SAndy Whitcroft
5224e0df7e1fSJoe Perches					# for asm volatile statements
5225e0df7e1fSJoe Perches					# ignore a colon with another
5226e0df7e1fSJoe Perches					# colon immediately before or after
5227e0df7e1fSJoe Perches					if (($op eq ':') &&
5228e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
5229e0df7e1fSJoe Perches						$ok = 1;
5230e0df7e1fSJoe Perches					}
5231e0df7e1fSJoe Perches
523284731623SJoe Perches					# messages are ERROR, but ?: are CHK
52331f65f947SAndy Whitcroft					if ($ok == 0) {
52340675a8fbSJean Delvare						my $msg_level = \&ERROR;
52350675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
523684731623SJoe Perches
52370675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
52383705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
5239b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5240b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5241b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5242b34c648bSJoe Perches							}
52433705ce5bSJoe Perches							$line_fixed = 1;
52443705ce5bSJoe Perches						}
52450a920b5bSAndy Whitcroft					}
524622f2a2efSAndy Whitcroft				}
52474a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
52483705ce5bSJoe Perches
52493705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
52503705ce5bSJoe Perches
52513705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
52520a920b5bSAndy Whitcroft			}
52533705ce5bSJoe Perches
52543705ce5bSJoe Perches			if (($#elements % 2) == 0) {
52553705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
52563705ce5bSJoe Perches			}
52573705ce5bSJoe Perches
5258194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5259194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
52603705ce5bSJoe Perches			}
52613705ce5bSJoe Perches
52623705ce5bSJoe Perches
52630a920b5bSAndy Whitcroft		}
52640a920b5bSAndy Whitcroft
5265786b6326SJoe Perches# check for whitespace before a non-naked semicolon
5266d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
5267786b6326SJoe Perches			if (WARN("SPACING",
5268786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
5269786b6326SJoe Perches			    $fix) {
5270194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
5271786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
5272786b6326SJoe Perches			}
5273786b6326SJoe Perches		}
5274786b6326SJoe Perches
5275f0a594c1SAndy Whitcroft# check for multiple assignments
5276f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5277000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
5278000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
5279f0a594c1SAndy Whitcroft		}
5280f0a594c1SAndy Whitcroft
528122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
528222f2a2efSAndy Whitcroft## # continuation.
528322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
528422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
528522f2a2efSAndy Whitcroft##
528622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
5287e73d2715SDwaipayan Ray## 			# falsely report the parameters of functions.
528822f2a2efSAndy Whitcroft## 			my $ln = $line;
528922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
529022f2a2efSAndy Whitcroft## 			}
529122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
5292000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
5293000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
529422f2a2efSAndy Whitcroft## 			}
529522f2a2efSAndy Whitcroft## 		}
5296f0a594c1SAndy Whitcroft
52970a920b5bSAndy Whitcroft#need space before brace following if, while, etc
52986b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
52996ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
53003705ce5bSJoe Perches			if (ERROR("SPACING",
53013705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
53023705ce5bSJoe Perches			    $fix) {
53036ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
53043705ce5bSJoe Perches			}
5305de7d4f0eSAndy Whitcroft		}
5306de7d4f0eSAndy Whitcroft
5307c4a62ef9SJoe Perches## # check for blank lines before declarations
5308c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5309c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
5310c4a62ef9SJoe Perches##			WARN("SPACING",
5311c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
5312c4a62ef9SJoe Perches##		}
5313c4a62ef9SJoe Perches##
5314c4a62ef9SJoe Perches
5315de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
5316de7d4f0eSAndy Whitcroft# on the line
531794fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5318d5e616fcSJoe Perches			if (ERROR("SPACING",
5319d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
5320d5e616fcSJoe Perches			    $fix) {
5321194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5322d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
5323d5e616fcSJoe Perches			}
53240a920b5bSAndy Whitcroft		}
53250a920b5bSAndy Whitcroft
532622f2a2efSAndy Whitcroft# check spacing on square brackets
532722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
53283705ce5bSJoe Perches			if (ERROR("SPACING",
53293705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
53303705ce5bSJoe Perches			    $fix) {
5331194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53323705ce5bSJoe Perches				    s/\[\s+/\[/;
53333705ce5bSJoe Perches			}
533422f2a2efSAndy Whitcroft		}
533522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
53363705ce5bSJoe Perches			if (ERROR("SPACING",
53373705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
53383705ce5bSJoe Perches			    $fix) {
5339194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53403705ce5bSJoe Perches				    s/\s+\]/\]/;
53413705ce5bSJoe Perches			}
534222f2a2efSAndy Whitcroft		}
534322f2a2efSAndy Whitcroft
5344c45dcabdSAndy Whitcroft# check spacing on parentheses
53459c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
53469c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
53473705ce5bSJoe Perches			if (ERROR("SPACING",
53483705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
53493705ce5bSJoe Perches			    $fix) {
5350194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53513705ce5bSJoe Perches				    s/\(\s+/\(/;
53523705ce5bSJoe Perches			}
535322f2a2efSAndy Whitcroft		}
535413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5355c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
5356c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
53573705ce5bSJoe Perches			if (ERROR("SPACING",
53583705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
53593705ce5bSJoe Perches			    $fix) {
5360194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53613705ce5bSJoe Perches				    s/\s+\)/\)/;
53623705ce5bSJoe Perches			}
536322f2a2efSAndy Whitcroft		}
536422f2a2efSAndy Whitcroft
5365e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
5366e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5367e2826fd0SJoe Perches
5368e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5369ea4acbb1SJoe Perches			my $var = $1;
5370ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5371ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
5372ea4acbb1SJoe Perches			    $fix) {
5373ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5374ea4acbb1SJoe Perches			}
5375ea4acbb1SJoe Perches		}
5376ea4acbb1SJoe Perches
5377ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
5378ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
5379ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
5380ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5381ea4acbb1SJoe Perches			my $var = $2;
5382ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5383ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5384ea4acbb1SJoe Perches			    $fix) {
5385ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
5386ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
5387ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5388ea4acbb1SJoe Perches			}
5389e2826fd0SJoe Perches		}
5390e2826fd0SJoe Perches
539163b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
5392a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
5393a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
53945b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
539563b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
539663b7c73eSJoe Perches			my $if_stat = $1;
539763b7c73eSJoe Perches			my $test = substr($2, 1, -1);
539863b7c73eSJoe Perches			my $herectx;
539963b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
540063b7c73eSJoe Perches				my $match = $1;
540163b7c73eSJoe Perches				# avoid parentheses around potential macro args
540263b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
540363b7c73eSJoe Perches				if (!defined($herectx)) {
540463b7c73eSJoe Perches					$herectx = $here . "\n";
540563b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
540663b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
540763b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
540863b7c73eSJoe Perches						$herectx .=  $rl . "\n";
540963b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
541063b7c73eSJoe Perches					}
541163b7c73eSJoe Perches				}
541263b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
541363b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
541463b7c73eSJoe Perches			}
541563b7c73eSJoe Perches		}
541663b7c73eSJoe Perches
541769078651SJoe Perches# check that goto labels aren't indented (allow a single space indentation)
541869078651SJoe Perches# and ignore bitfield definitions like foo:1
541969078651SJoe Perches# Strictly, labels can have whitespace after the identifier and before the :
542069078651SJoe Perches# but this is not allowed here as many ?: uses would appear to be labels
542169078651SJoe Perches		if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
542269078651SJoe Perches		    $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
542369078651SJoe Perches		    $sline !~ /^.\s+default:/) {
54243705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
54253705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
54263705ce5bSJoe Perches			    $fix) {
5427194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54283705ce5bSJoe Perches				    s/^(.)\s+/$1/;
54293705ce5bSJoe Perches			}
54300a920b5bSAndy Whitcroft		}
54310a920b5bSAndy Whitcroft
543240873abaSJoe Perches# check if a statement with a comma should be two statements like:
543340873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
543440873abaSJoe Perches#	bar = baz();
543540873abaSJoe Perches		if (defined($stat) &&
543640873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
543740873abaSJoe Perches			my $cnt = statement_rawlines($stat);
543840873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
543940873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
544040873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
544140873abaSJoe Perches		}
544240873abaSJoe Perches
54435b9553abSJoe Perches# return is not a function
5444507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5445c45dcabdSAndy Whitcroft			my $spacing = $1;
54465b57980dSJoe Perches			if ($perl_version_ok &&
54475b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
54485b9553abSJoe Perches				my $value = $1;
54495b9553abSJoe Perches				$value = deparenthesize($value);
54505b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5451000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5452000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
54535b9553abSJoe Perches				}
5454c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5455000d1cc1SJoe Perches				ERROR("SPACING",
5456000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5457c45dcabdSAndy Whitcroft			}
5458c45dcabdSAndy Whitcroft		}
5459507e5141SJoe Perches
5460b43ae21bSJoe Perches# unnecessary return in a void function
5461b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5462b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5463b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5464b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5465b43ae21bSJoe Perches		    $linenr >= 3 &&
5466b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5467b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
54689819cf25SJoe Perches			WARN("RETURN_VOID",
5469b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
54709819cf25SJoe Perches		}
54719819cf25SJoe Perches
5472189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
54735b57980dSJoe Perches		if ($perl_version_ok &&
5474189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5475189248d8SJoe Perches			my $openparens = $1;
5476189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5477189248d8SJoe Perches			my $msg = "";
5478189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5479189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5480189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5481189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5482189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5483189248d8SJoe Perches			}
5484189248d8SJoe Perches		}
5485189248d8SJoe Perches
5486c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5487c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5488c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5489c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
54905b57980dSJoe Perches		if ($perl_version_ok &&
5491c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5492c5595fa2SJoe Perches			my $lead = $1;
5493c5595fa2SJoe Perches			my $const = $2;
5494c5595fa2SJoe Perches			my $comp = $3;
5495c5595fa2SJoe Perches			my $to = $4;
5496c5595fa2SJoe Perches			my $newcomp = $comp;
5497f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5498c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5499c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5500c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5501c5595fa2SJoe Perches			    $fix) {
5502c5595fa2SJoe Perches				if ($comp eq "<") {
5503c5595fa2SJoe Perches					$newcomp = ">";
5504c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5505c5595fa2SJoe Perches					$newcomp = ">=";
5506c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5507c5595fa2SJoe Perches					$newcomp = "<";
5508c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5509c5595fa2SJoe Perches					$newcomp = "<=";
5510c5595fa2SJoe Perches				}
5511c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5512c5595fa2SJoe Perches			}
5513c5595fa2SJoe Perches		}
5514c5595fa2SJoe Perches
5515f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5516f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
551753a3c448SAndy Whitcroft			my $name = $1;
551846b85bf9SGuenter Roeck			if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
5519000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5520f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
552153a3c448SAndy Whitcroft			}
552253a3c448SAndy Whitcroft		}
5523c45dcabdSAndy Whitcroft
55240a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
55254a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
55263705ce5bSJoe Perches			if (ERROR("SPACING",
55273705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
55283705ce5bSJoe Perches			    $fix) {
5529194f66fcSJoe Perches				$fixed[$fixlinenr] =~
55303705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
55313705ce5bSJoe Perches			}
55320a920b5bSAndy Whitcroft		}
55330a920b5bSAndy Whitcroft
5534f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5535f5fe35ddSAndy Whitcroft# statements after the conditional.
5536170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
55373e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
55383e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
55393e469cdcSAndy Whitcroft					if (!defined $stat);
5540170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5541170d3a22SAndy Whitcroft						$remain_next, $off_next);
5542170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5543170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5544170d3a22SAndy Whitcroft
5545170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5546170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5547170d3a22SAndy Whitcroft				# then count those as offsets.
5548170d3a22SAndy Whitcroft				my ($whitespace) =
5549170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5550170d3a22SAndy Whitcroft				my $offset =
5551170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5552170d3a22SAndy Whitcroft
5553170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5554170d3a22SAndy Whitcroft								$offset} = 1;
5555170d3a22SAndy Whitcroft			}
5556170d3a22SAndy Whitcroft		}
5557170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5558c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5559170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5560171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
5561481efd7bSJoe Perches			my $fixed_assign_in_if = 0;
55628905a67cSAndy Whitcroft
5563b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
556465b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
556565b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
556665b64b3bSJoe Perches				    $fix && $perl_version_ok) {
556765b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
556865b64b3bSJoe Perches						my $space = $1;
556965b64b3bSJoe Perches						my $not = $2;
557065b64b3bSJoe Perches						my $statement = $3;
557165b64b3bSJoe Perches						my $assigned = $4;
557265b64b3bSJoe Perches						my $test = $8;
557365b64b3bSJoe Perches						my $against = $9;
557465b64b3bSJoe Perches						my $brace = $15;
557565b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
557665b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
557765b64b3bSJoe Perches						my $newline = "${space}if (";
557865b64b3bSJoe Perches						$newline .= '!' if defined($not);
557965b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
558065b64b3bSJoe Perches						$newline .= "$assigned";
558165b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
558265b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
558365b64b3bSJoe Perches						$newline .= ')';
558465b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
558565b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
5586481efd7bSJoe Perches						$fixed_assign_in_if = 1;
558765b64b3bSJoe Perches					}
558865b64b3bSJoe Perches				}
55898905a67cSAndy Whitcroft			}
55908905a67cSAndy Whitcroft
55918905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
55928905a67cSAndy Whitcroft			# conditional.
5593773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
55948905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
559513214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
559653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
559753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5598773647a0SAndy Whitcroft			{
5599bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5600bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5601bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
560242bdf74cSHidetoshi Seto				my $stat_real = '';
5603bb44ad39SAndy Whitcroft
560442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
560542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5606bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5607bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5608bb44ad39SAndy Whitcroft				}
5609bb44ad39SAndy Whitcroft
5610481efd7bSJoe Perches				if (ERROR("TRAILING_STATEMENTS",
5611481efd7bSJoe Perches					  "trailing statements should be on next line\n" . $herecurr . $stat_real) &&
5612481efd7bSJoe Perches				    !$fixed_assign_in_if &&
5613481efd7bSJoe Perches				    $cond_lines == 0 &&
5614481efd7bSJoe Perches				    $fix && $perl_version_ok &&
5615481efd7bSJoe Perches				    $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
5616481efd7bSJoe Perches					my $indent = $1;
5617481efd7bSJoe Perches					my $test = $2;
5618481efd7bSJoe Perches					my $rest = rtrim($4);
5619481efd7bSJoe Perches					if ($rest =~ /;$/) {
5620481efd7bSJoe Perches						$fixed[$fixlinenr] = "\+$indent$test";
5621481efd7bSJoe Perches						fix_insert_line($fixlinenr + 1, "$indent\t$rest");
5622481efd7bSJoe Perches					}
5623481efd7bSJoe Perches				}
56248905a67cSAndy Whitcroft			}
56258905a67cSAndy Whitcroft		}
56268905a67cSAndy Whitcroft
562713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
562813214adfSAndy Whitcroft		if ($line =~ /
562913214adfSAndy Whitcroft			(?:
563013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
563113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
563213214adfSAndy Whitcroft				(?:\&\&|\|\|)
563313214adfSAndy Whitcroft			|
563413214adfSAndy Whitcroft				(?:\&\&|\|\|)
563513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
563613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
563713214adfSAndy Whitcroft			)/x)
563813214adfSAndy Whitcroft		{
5639000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5640000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
564113214adfSAndy Whitcroft		}
564213214adfSAndy Whitcroft
56438905a67cSAndy Whitcroft# if and else should not have general statements after it
564413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
564513214adfSAndy Whitcroft			my $s = $1;
564613214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
564713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5648000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5649000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
56500a920b5bSAndy Whitcroft			}
565113214adfSAndy Whitcroft		}
565239667782SAndy Whitcroft# if should not continue a brace
565339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5654000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5655048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
565639667782SAndy Whitcroft				$herecurr);
565739667782SAndy Whitcroft		}
5658a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5659a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5660a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
56613fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5662a1080bf8SAndy Whitcroft			\s*return\s+
5663a1080bf8SAndy Whitcroft		    )/xg)
5664a1080bf8SAndy Whitcroft		{
5665000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5666000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5667a1080bf8SAndy Whitcroft		}
56680a920b5bSAndy Whitcroft
56690a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
56700a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
56718b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
56720a920b5bSAndy Whitcroft		    $previndent == $indent) {
56738b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
56748b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
56758b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
56768b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
56778b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
56788b8856f4SJoe Perches				my $fixedline = $prevrawline;
56798b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
56808b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
56818b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
56828b8856f4SJoe Perches				}
56838b8856f4SJoe Perches				$fixedline = $rawline;
56848b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
56858b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
56868b8856f4SJoe Perches			}
56870a920b5bSAndy Whitcroft		}
56880a920b5bSAndy Whitcroft
56898b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5690c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5691c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5692c2fdda0dSAndy Whitcroft
5693c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5694c2fdda0dSAndy Whitcroft			# conditional.
5695773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5696c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5697c2fdda0dSAndy Whitcroft
5698c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
56998b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
57008b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
57018b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
57028b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
57038b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
57048b8856f4SJoe Perches					my $fixedline = $prevrawline;
57058b8856f4SJoe Perches					my $trailing = $rawline;
57068b8856f4SJoe Perches					$trailing =~ s/^\+//;
57078b8856f4SJoe Perches					$trailing = trim($trailing);
57088b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
57098b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
57108b8856f4SJoe Perches				}
5711c2fdda0dSAndy Whitcroft			}
5712c2fdda0dSAndy Whitcroft		}
5713c2fdda0dSAndy Whitcroft
571495e2c602SJoe Perches#Specific variable tests
5715323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5716323c1260SJoe Perches			my $var = $1;
571795e2c602SJoe Perches
571895e2c602SJoe Perches#CamelCase
5719807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5720be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
57214104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values
57224104a206SŁukasz Stelmach			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
572322735ce8SJoe Perches#Ignore Page<foo> variants
5724807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5725d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5726d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5727d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5728f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5729f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
5730f858e23aSAntonio Borneo				while ($var =~ m{\b($Ident)}g) {
57317e781f67SJoe Perches					my $word = $1;
57327e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5733d8b07710SJoe Perches					if ($check) {
5734d8b07710SJoe Perches						seed_camelcase_includes();
5735d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5736d8b07710SJoe Perches							seed_camelcase_file($realfile);
5737d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5738d8b07710SJoe Perches						}
5739d8b07710SJoe Perches					}
57407e781f67SJoe Perches					if (!defined $camelcase{$word}) {
57417e781f67SJoe Perches						$camelcase{$word} = 1;
5742be79794bSJoe Perches						CHK("CAMELCASE",
57437e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
57447e781f67SJoe Perches					}
5745323c1260SJoe Perches				}
5746323c1260SJoe Perches			}
57473445686aSJoe Perches		}
57480a920b5bSAndy Whitcroft
57490a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5750d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5751d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5752d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5753d5e616fcSJoe Perches			    $fix) {
5754194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5755d5e616fcSJoe Perches			}
57560a920b5bSAndy Whitcroft		}
57570a920b5bSAndy Whitcroft
57580e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
57590e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5760c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5761e09dec48SAndy Whitcroft			my $file = "$1.h";
5762e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5763e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5764e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
57657840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5766c45dcabdSAndy Whitcroft			{
57670e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
57680e212e0aSFabian Frederick				if ($asminclude > 0) {
5769e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5770000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5771000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5772e09dec48SAndy Whitcroft					} else {
5773000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5774000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5775e09dec48SAndy Whitcroft					}
57760a920b5bSAndy Whitcroft				}
57770a920b5bSAndy Whitcroft			}
57780e212e0aSFabian Frederick		}
57790a920b5bSAndy Whitcroft
5780653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5781653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5782cf655043SAndy Whitcroft# in a known good container
5783b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5784b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5785d8aaf121SAndy Whitcroft			my $ln = $linenr;
5786d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5787c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5788c45dcabdSAndy Whitcroft			my $ctx = '';
578908a2843eSJoe Perches			my $has_flow_statement = 0;
579008a2843eSJoe Perches			my $has_arg_concat = 0;
5791c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5792f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5793f74bd194SAndy Whitcroft			$ctx = $dstat;
5794c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5795a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5796c45dcabdSAndy Whitcroft
579708a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
579862e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
579908a2843eSJoe Perches
5800f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5801f59b64bfSJoe Perches			my $define_args = $1;
5802f59b64bfSJoe Perches			my $define_stmt = $dstat;
5803f59b64bfSJoe Perches			my @def_args = ();
5804f59b64bfSJoe Perches
5805f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5806f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5807f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
58088c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5809f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5810f59b64bfSJoe Perches			}
5811f59b64bfSJoe Perches
5812292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5813c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5814c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5815c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5816c45dcabdSAndy Whitcroft
5817c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
58182e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
58192e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
58202e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5821bf30d6edSAndy Whitcroft			{
5822c45dcabdSAndy Whitcroft			}
5823c45dcabdSAndy Whitcroft
5824342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
582533acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
582633acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5827e45bab8eSAndy Whitcroft			{
5828e45bab8eSAndy Whitcroft			}
5829e45bab8eSAndy Whitcroft
583042e15293SJoe Perches			# Make asm volatile uses seem like a generic function
583142e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
583242e15293SJoe Perches
5833c45dcabdSAndy Whitcroft			my $exceptions = qr{
5834c45dcabdSAndy Whitcroft				$Declare|
5835c45dcabdSAndy Whitcroft				module_param_named|
5836a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5837c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5838c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5839383099fdSAndy Whitcroft				__typeof__\(|
584022fd2d3eSStefani Seibold				union|
584122fd2d3eSStefani Seibold				struct|
5842ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
58436b10df42SVladimir Zapolskiy				^\"|\"$|
58446b10df42SVladimir Zapolskiy				^\[
5845c45dcabdSAndy Whitcroft			}x;
58465eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5847f59b64bfSJoe Perches
5848f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5849f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5850e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5851f59b64bfSJoe Perches
5852f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5853f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5854f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
58553cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5856356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5857f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5858f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5859e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
586072f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
58612e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5862f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5863f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5864f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
58654e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5866f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5867c45dcabdSAndy Whitcroft			{
5868e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5869e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5870e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5871e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5872f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5873f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5874f74bd194SAndy Whitcroft				} else {
5875000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5876388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5877d8aaf121SAndy Whitcroft				}
5878f59b64bfSJoe Perches
5879f59b64bfSJoe Perches			}
58805207649bSJoe Perches
58815207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
58825207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
58835207649bSJoe Perches			my $first = 1;
58845207649bSJoe Perches			$define_stmt = "";
58855207649bSJoe Perches			foreach my $l (@stmt_array) {
58865207649bSJoe Perches				$l =~ s/\\$//;
58875207649bSJoe Perches				if ($first) {
58885207649bSJoe Perches					$define_stmt = $l;
58895207649bSJoe Perches					$first = 0;
58905207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
58915207649bSJoe Perches					$define_stmt .= substr($l, 1);
58925207649bSJoe Perches				}
58935207649bSJoe Perches			}
58945207649bSJoe Perches			$define_stmt =~ s/$;//g;
58955207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
58965207649bSJoe Perches			$define_stmt = trim($define_stmt);
58975207649bSJoe Perches
5898f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5899f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5900f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
59019192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
59027fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
59037b844345SVincent Mailhol				$tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
59047fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
59057fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5906d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5907f59b64bfSJoe Perches				if ($use_cnt > 1) {
5908f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5909f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5910f59b64bfSJoe Perches				    }
59119192d41aSJoe Perches# check if any macro arguments may have other precedence issues
59127fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
59139192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
59149192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
59159192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
59169192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
59179192d41aSJoe Perches				}
59180a920b5bSAndy Whitcroft			}
59195023d347SJoe Perches
592008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
592108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
592208a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
592308a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5924e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
592508a2843eSJoe Perches
592608a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
592708a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
592808a2843eSJoe Perches			}
592908a2843eSJoe Perches
5930481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
59315023d347SJoe Perches
59325023d347SJoe Perches		} else {
59335023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5934481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5935481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
59365023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
59375023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
59385023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
59395023d347SJoe Perches			}
5940653d4876SAndy Whitcroft		}
59410a920b5bSAndy Whitcroft
5942b13edf7fSJoe Perches# do {} while (0) macro tests:
5943b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5944b13edf7fSJoe Perches# macro should not end with a semicolon
59455b57980dSJoe Perches		if ($perl_version_ok &&
5946b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5947b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5948b13edf7fSJoe Perches			my $ln = $linenr;
5949b13edf7fSJoe Perches			my $cnt = $realcnt;
5950b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5951b13edf7fSJoe Perches			my $ctx = '';
5952b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5953b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5954b13edf7fSJoe Perches			$ctx = $dstat;
5955b13edf7fSJoe Perches
5956b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
59571b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5958b13edf7fSJoe Perches
5959b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5960b13edf7fSJoe Perches				my $stmts = $2;
5961b13edf7fSJoe Perches				my $semis = $3;
5962b13edf7fSJoe Perches
5963b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5964b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5965e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5966b13edf7fSJoe Perches
5967ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5968ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5969b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5970b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5971b13edf7fSJoe Perches				}
5972b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5973b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5974b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5975b13edf7fSJoe Perches				}
5976f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5977f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5978f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5979e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5980f5ef95b1SJoe Perches
5981f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5982f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5983b13edf7fSJoe Perches			}
5984b13edf7fSJoe Perches		}
5985b13edf7fSJoe Perches
5986f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
598713214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
598813214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5989cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
599013214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5991cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5992cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5993aad4f614SJoe Perches				my @allowed = ();
5994aad4f614SJoe Perches				my $allow = 0;
599513214adfSAndy Whitcroft				my $seen = 0;
5996773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5997cf655043SAndy Whitcroft				my $ln = $linenr - 1;
599813214adfSAndy Whitcroft				for my $chunk (@chunks) {
599913214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
600013214adfSAndy Whitcroft
6001773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
6002773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
6003773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
6004773647a0SAndy Whitcroft
6005aad4f614SJoe Perches					$allowed[$allow] = 0;
6006773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
6007773647a0SAndy Whitcroft
6008773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
6009773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
6010773647a0SAndy Whitcroft
6011773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
6012cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
6013cf655043SAndy Whitcroft
6014773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
601513214adfSAndy Whitcroft
601613214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
601713214adfSAndy Whitcroft
6018aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
6019cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
6020cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
6021aad4f614SJoe Perches						$allowed[$allow] = 1;
602213214adfSAndy Whitcroft					}
602313214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
6024cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
6025aad4f614SJoe Perches						$allowed[$allow] = 1;
602613214adfSAndy Whitcroft					}
6027cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
6028cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
6029aad4f614SJoe Perches						$allowed[$allow] = 1;
603013214adfSAndy Whitcroft					}
6031aad4f614SJoe Perches					$allow++;
603213214adfSAndy Whitcroft				}
6033aad4f614SJoe Perches				if ($seen) {
6034aad4f614SJoe Perches					my $sum_allowed = 0;
6035aad4f614SJoe Perches					foreach (@allowed) {
6036aad4f614SJoe Perches						$sum_allowed += $_;
6037aad4f614SJoe Perches					}
6038aad4f614SJoe Perches					if ($sum_allowed == 0) {
6039000d1cc1SJoe Perches						WARN("BRACES",
6040000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
6041aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
6042aad4f614SJoe Perches						 $seen != $allow) {
6043aad4f614SJoe Perches						CHK("BRACES",
6044aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
6045aad4f614SJoe Perches					}
604613214adfSAndy Whitcroft				}
604713214adfSAndy Whitcroft			}
604813214adfSAndy Whitcroft		}
6049773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
605013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
6051cf655043SAndy Whitcroft			my $allowed = 0;
6052f0a594c1SAndy Whitcroft
6053cf655043SAndy Whitcroft			# Check the pre-context.
6054cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6055cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
6056cf655043SAndy Whitcroft				$allowed = 1;
6057f0a594c1SAndy Whitcroft			}
6058773647a0SAndy Whitcroft
6059773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
6060773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
6061773647a0SAndy Whitcroft
6062cf655043SAndy Whitcroft			# Check the condition.
6063cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
6064773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
6065cf655043SAndy Whitcroft			if (defined $cond) {
6066773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
6067cf655043SAndy Whitcroft			}
6068cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
6069cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
6070cf655043SAndy Whitcroft				$allowed = 1;
6071cf655043SAndy Whitcroft			}
6072cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
6073cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
6074cf655043SAndy Whitcroft				$allowed = 1;
6075cf655043SAndy Whitcroft			}
6076cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
6077cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
6078cf655043SAndy Whitcroft				$allowed = 1;
6079cf655043SAndy Whitcroft			}
6080cf655043SAndy Whitcroft			# Check the post-context.
6081cf655043SAndy Whitcroft			if (defined $chunks[1]) {
6082cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
6083cf655043SAndy Whitcroft				if (defined $cond) {
6084773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
6085cf655043SAndy Whitcroft				}
6086cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
6087cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
6088cf655043SAndy Whitcroft					$allowed = 1;
6089cf655043SAndy Whitcroft				}
6090cf655043SAndy Whitcroft			}
6091cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
6092f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
6093e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6094cf655043SAndy Whitcroft
6095000d1cc1SJoe Perches				WARN("BRACES",
6096000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
6097f0a594c1SAndy Whitcroft			}
6098f0a594c1SAndy Whitcroft		}
6099f0a594c1SAndy Whitcroft
6100e4c5babdSJoe Perches# check for single line unbalanced braces
610195330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
610295330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
6103e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6104e4c5babdSJoe Perches		}
6105e4c5babdSJoe Perches
61060979ae66SJoe Perches# check for unnecessary blank lines around braces
610777b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
6108f8e58219SJoe Perches			if (CHK("BRACES",
6109f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6110f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
6111f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
6112f8e58219SJoe Perches			}
61130979ae66SJoe Perches		}
611477b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
6115f8e58219SJoe Perches			if (CHK("BRACES",
6116f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6117f8e58219SJoe Perches			    $fix) {
6118f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
6119f8e58219SJoe Perches			}
61200979ae66SJoe Perches		}
61210979ae66SJoe Perches
61224a0df2efSAndy Whitcroft# no volatiles please
61236c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
61246c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
6125000d1cc1SJoe Perches			WARN("VOLATILE",
61268c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
61274a0df2efSAndy Whitcroft		}
61284a0df2efSAndy Whitcroft
61295e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
61305e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
61315e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
61325e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
613333acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
61345e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
61355e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
61365e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
61375e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
61385e4f6ba5SJoe Perches				     $fix &&
61395e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
61405e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
61415e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
61425e4f6ba5SJoe Perches				my $comma_close = "";
61435e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
61445e4f6ba5SJoe Perches					$comma_close = $1;
61455e4f6ba5SJoe Perches				}
61465e4f6ba5SJoe Perches
61475e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
61485e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
61495e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
61505e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
61515e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
61525e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
61535e4f6ba5SJoe Perches				$fixedline = $rawline;
61545e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
61555e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
61565e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
61575e4f6ba5SJoe Perches				}
61585e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
61595e4f6ba5SJoe Perches			}
61605e4f6ba5SJoe Perches		}
61615e4f6ba5SJoe Perches
61625e4f6ba5SJoe Perches# check for missing a space in a string concatenation
61635e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
61645e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
61655e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
61665e4f6ba5SJoe Perches		}
61675e4f6ba5SJoe Perches
616877cb8546SJoe Perches# check for an embedded function name in a string when the function is known
6169e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
6170e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
6171e4b7d309SJoe Perches# function declarations
617277cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
617377cb8546SJoe Perches		    defined($context_function) &&
6174e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6175e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
617677cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
6177e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
617877cb8546SJoe Perches		}
617977cb8546SJoe Perches
6180adb2da82SJoe Perches# check for unnecessary function tracing like uses
6181adb2da82SJoe Perches# This does not use $logFunctions because there are many instances like
6182adb2da82SJoe Perches# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6183adb2da82SJoe Perches		if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6184adb2da82SJoe Perches			if (WARN("TRACING_LOGGING",
6185adb2da82SJoe Perches				 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6186adb2da82SJoe Perches			    $fix) {
6187adb2da82SJoe Perches                                fix_delete_line($fixlinenr, $rawline);
6188adb2da82SJoe Perches			}
6189adb2da82SJoe Perches		}
6190adb2da82SJoe Perches
61915e4f6ba5SJoe Perches# check for spaces before a quoted newline
61925e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
61935e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
61945e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
61955e4f6ba5SJoe Perches			    $fix) {
61965e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
61975e4f6ba5SJoe Perches			}
61985e4f6ba5SJoe Perches
61995e4f6ba5SJoe Perches		}
62005e4f6ba5SJoe Perches
6201f17dba4fSJoe Perches# concatenated string without spaces between elements
6202d2af5aa6SJoe Perches		if ($line =~ /$String[A-Z_]/ ||
6203d2af5aa6SJoe Perches		    ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
620479682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
620579682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
620679682c0cSJoe Perches			    $fix) {
620779682c0cSJoe Perches				while ($line =~ /($String)/g) {
620879682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
620979682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
621079682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
621179682c0cSJoe Perches				}
621279682c0cSJoe Perches			}
6213f17dba4fSJoe Perches		}
6214f17dba4fSJoe Perches
621590ad30e5SJoe Perches# uncoalesced string fragments
6216d2af5aa6SJoe Perches		if ($line =~ /$String\s*[Lu]?"/) {
621779682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
621879682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
621979682c0cSJoe Perches			    $fix) {
622079682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
622179682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
622279682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
622379682c0cSJoe Perches				}
622479682c0cSJoe Perches			}
622590ad30e5SJoe Perches		}
622690ad30e5SJoe Perches
6227522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
6228522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
6229522b837cSAlexey Dobriyan		my $show_Z = 1;
62305e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6231522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
62325e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
6233522b837cSAlexey Dobriyan			# check for %L
6234522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
62355e4f6ba5SJoe Perches				WARN("PRINTF_L",
6236522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6237522b837cSAlexey Dobriyan				$show_L = 0;
62385e4f6ba5SJoe Perches			}
6239522b837cSAlexey Dobriyan			# check for %Z
6240522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6241522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
6242522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6243522b837cSAlexey Dobriyan				$show_Z = 0;
6244522b837cSAlexey Dobriyan			}
6245522b837cSAlexey Dobriyan			# check for 0x<decimal>
6246522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6247522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
62486e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
62496e300757SJoe Perches			}
62505e4f6ba5SJoe Perches		}
62515e4f6ba5SJoe Perches
62525e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
62533f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
62545e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
62555e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
62565e4f6ba5SJoe Perches		}
62575e4f6ba5SJoe Perches
625800df344fSAndy Whitcroft# warn about #if 0
6259c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
626060f89010SPrakruthi Deepak Heragu			WARN("IF_0",
626160f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
626260f89010SPrakruthi Deepak Heragu		}
626360f89010SPrakruthi Deepak Heragu
626460f89010SPrakruthi Deepak Heragu# warn about #if 1
626560f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
626660f89010SPrakruthi Deepak Heragu			WARN("IF_1",
626760f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
62684a0df2efSAndy Whitcroft		}
62694a0df2efSAndy Whitcroft
627003df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
627103df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6272100425deSJoe Perches			my $tested = quotemeta($1);
6273100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6274100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6275100425deSJoe Perches				my $func = $1;
6276100425deSJoe Perches				if (WARN('NEEDLESS_IF',
6277100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6278100425deSJoe Perches				    $fix) {
6279100425deSJoe Perches					my $do_fix = 1;
6280100425deSJoe Perches					my $leading_tabs = "";
6281100425deSJoe Perches					my $new_leading_tabs = "";
6282100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6283100425deSJoe Perches						$leading_tabs = $1;
6284100425deSJoe Perches					} else {
6285100425deSJoe Perches						$do_fix = 0;
6286100425deSJoe Perches					}
6287100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6288100425deSJoe Perches						$new_leading_tabs = $1;
6289100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6290100425deSJoe Perches							$do_fix = 0;
6291100425deSJoe Perches						}
6292100425deSJoe Perches					} else {
6293100425deSJoe Perches						$do_fix = 0;
6294100425deSJoe Perches					}
6295100425deSJoe Perches					if ($do_fix) {
6296100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
6297100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6298100425deSJoe Perches					}
6299100425deSJoe Perches				}
63004c432a8fSGreg Kroah-Hartman			}
63014c432a8fSGreg Kroah-Hartman		}
6302f0a594c1SAndy Whitcroft
6303ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
6304ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6305ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6306ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
6307ebfdc409SJoe Perches		    $linenr > 3) {
6308ebfdc409SJoe Perches			my $testval = $2;
6309ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
6310ebfdc409SJoe Perches
6311ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6312ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6313ebfdc409SJoe Perches
6314e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6315e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
6316ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
6317ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6318ebfdc409SJoe Perches			}
6319ebfdc409SJoe Perches		}
6320ebfdc409SJoe Perches
6321f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
6322dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6323f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6324f78d98f6SJoe Perches			my $level = $1;
6325f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
6326f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
6327f78d98f6SJoe Perches			    $fix) {
6328f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6329f78d98f6SJoe Perches			}
6330f78d98f6SJoe Perches		}
6331f78d98f6SJoe Perches
633245c55e92SJoe Perches# check for logging continuations
633345c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
633445c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
633545c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
633645c55e92SJoe Perches		}
633745c55e92SJoe Perches
633870eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
633970eb2275SDwaipayan Ray		if (defined $stat &&
634070eb2275SDwaipayan Ray		    $line =~ /\b$logFunctions\s*\(/ &&
634170eb2275SDwaipayan Ray		    index($stat, '"') >= 0) {
634270eb2275SDwaipayan Ray			my $lc = $stat =~ tr@\n@@;
634370eb2275SDwaipayan Ray			$lc = $lc + $linenr;
634470eb2275SDwaipayan Ray			my $stat_real = get_stat_real($linenr, $lc);
634570eb2275SDwaipayan Ray			pos($stat_real) = index($stat_real, '"');
634670eb2275SDwaipayan Ray			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
634770eb2275SDwaipayan Ray				my $pspec = $1;
634870eb2275SDwaipayan Ray				my $h = $2;
634970eb2275SDwaipayan Ray				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
635070eb2275SDwaipayan Ray				if (WARN("UNNECESSARY_MODIFIER",
635170eb2275SDwaipayan Ray					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
635270eb2275SDwaipayan Ray				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
635370eb2275SDwaipayan Ray					my $nspec = $pspec;
635470eb2275SDwaipayan Ray					$nspec =~ s/h//g;
635570eb2275SDwaipayan Ray					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
635670eb2275SDwaipayan Ray				}
635770eb2275SDwaipayan Ray			}
635870eb2275SDwaipayan Ray		}
635970eb2275SDwaipayan Ray
6360abb08a53SJoe Perches# check for mask then right shift without a parentheses
63615b57980dSJoe Perches		if ($perl_version_ok &&
6362abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6363abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6364abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
6365abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6366abb08a53SJoe Perches		}
6367abb08a53SJoe Perches
6368b75ac618SJoe Perches# check for pointer comparisons to NULL
63695b57980dSJoe Perches		if ($perl_version_ok) {
6370b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6371b75ac618SJoe Perches				my $val = $1;
6372b75ac618SJoe Perches				my $equal = "!";
6373b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
6374b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
6375b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6376b75ac618SJoe Perches					    $fix) {
6377b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6378b75ac618SJoe Perches				}
6379b75ac618SJoe Perches			}
6380b75ac618SJoe Perches		}
6381b75ac618SJoe Perches
63828716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
63838716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
63848716de38SJoe Perches			my $attr = $1;
63858716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
63868716de38SJoe Perches				my $ptr = $1;
63878716de38SJoe Perches				my $var = $2;
63888716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
63898716de38SJoe Perches				      ERROR("MISPLACED_INIT",
63908716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
63918716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
63928716de38SJoe Perches				      WARN("MISPLACED_INIT",
63938716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
63948716de38SJoe Perches				    $fix) {
6395194f66fcSJoe 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;
63968716de38SJoe Perches				}
63978716de38SJoe Perches			}
63988716de38SJoe Perches		}
63998716de38SJoe Perches
6400e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
6401e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6402e970b884SJoe Perches			my $attr = $1;
6403e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
6404e970b884SJoe Perches			my $attr_prefix = $1;
6405e970b884SJoe Perches			my $attr_type = $2;
6406e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6407e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6408e970b884SJoe Perches			    $fix) {
6409194f66fcSJoe Perches				$fixed[$fixlinenr] =~
6410e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
6411e970b884SJoe Perches			}
6412e970b884SJoe Perches		}
6413e970b884SJoe Perches
6414e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
6415e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6416e970b884SJoe Perches			my $attr = $1;
6417e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6418e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6419e970b884SJoe Perches			    $fix) {
6420194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
6421e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
6422e970b884SJoe Perches				$lead = rtrim($1);
6423e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
6424e970b884SJoe Perches				$lead = "${lead}const ";
6425194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6426e970b884SJoe Perches			}
6427e970b884SJoe Perches		}
6428e970b884SJoe Perches
6429c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
6430c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
6431c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6432c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
6433c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6434c17893c7SJoe Perches			    $fix) {
6435c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6436c17893c7SJoe Perches			}
6437c17893c7SJoe Perches		}
6438c17893c7SJoe Perches
6439fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
6440fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
6441fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6442fbdb8138SJoe Perches			my $constant_func = $1;
6443fbdb8138SJoe Perches			my $func = $constant_func;
6444fbdb8138SJoe Perches			$func =~ s/^__constant_//;
6445fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
6446fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
6447fbdb8138SJoe Perches			    $fix) {
6448194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6449fbdb8138SJoe Perches			}
6450fbdb8138SJoe Perches		}
6451fbdb8138SJoe Perches
64521a15a250SPatrick Pannuto# prefer usleep_range over udelay
645337581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
645443c1d77cSJoe Perches			my $delay = $1;
64551a15a250SPatrick Pannuto			# ignore udelay's < 10, however
645643c1d77cSJoe Perches			if (! ($delay < 10) ) {
6457000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
6458458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
645943c1d77cSJoe Perches			}
646043c1d77cSJoe Perches			if ($delay > 2000) {
646143c1d77cSJoe Perches				WARN("LONG_UDELAY",
646243c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
64631a15a250SPatrick Pannuto			}
64641a15a250SPatrick Pannuto		}
64651a15a250SPatrick Pannuto
646609ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
646709ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
646809ef8725SPatrick Pannuto			if ($1 < 20) {
6469000d1cc1SJoe Perches				WARN("MSLEEP",
6470458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
647109ef8725SPatrick Pannuto			}
647209ef8725SPatrick Pannuto		}
647309ef8725SPatrick Pannuto
647436ec1939SJoe Perches# check for comparisons of jiffies
647536ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
647636ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
647736ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
647836ec1939SJoe Perches		}
647936ec1939SJoe Perches
64809d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
64819d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
64829d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
64839d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
64849d7a34a5SJoe Perches		}
64859d7a34a5SJoe Perches
648600df344fSAndy Whitcroft# warn about #ifdefs in C files
6487c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
648800df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
648900df344fSAndy Whitcroft#			print "$herecurr";
649000df344fSAndy Whitcroft#			$clean = 0;
649100df344fSAndy Whitcroft#		}
649200df344fSAndy Whitcroft
649322f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
6494c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
64953705ce5bSJoe Perches			if (ERROR("SPACING",
64963705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
64973705ce5bSJoe Perches			    $fix) {
6498194f66fcSJoe Perches				$fixed[$fixlinenr] =~
64993705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
65003705ce5bSJoe Perches			}
65013705ce5bSJoe Perches
650222f2a2efSAndy Whitcroft		}
650322f2a2efSAndy Whitcroft
65044a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6505171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6506171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
65074a0df2efSAndy Whitcroft			my $which = $1;
65084a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6509000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6510000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
65114a0df2efSAndy Whitcroft			}
65124a0df2efSAndy Whitcroft		}
65134a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6514402c2553SMichael S. Tsirkin
6515402c2553SMichael S. Tsirkin		my $barriers = qr{
6516402c2553SMichael S. Tsirkin			mb|
6517402c2553SMichael S. Tsirkin			rmb|
6518ad83ec6cSWill Deacon			wmb
6519402c2553SMichael S. Tsirkin		}x;
6520402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6521402c2553SMichael S. Tsirkin			mb__before_atomic|
6522402c2553SMichael S. Tsirkin			mb__after_atomic|
6523402c2553SMichael S. Tsirkin			store_release|
6524402c2553SMichael S. Tsirkin			load_acquire|
6525402c2553SMichael S. Tsirkin			store_mb|
6526402c2553SMichael S. Tsirkin			(?:$barriers)
6527402c2553SMichael S. Tsirkin		}x;
6528402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6529402c2553SMichael S. Tsirkin			(?:$barriers)|
653043e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
653143e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6532402c2553SMichael S. Tsirkin		}x;
6533402c2553SMichael S. Tsirkin
6534402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
65354a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6536c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6537000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
65384a0df2efSAndy Whitcroft			}
65394a0df2efSAndy Whitcroft		}
65403ad81779SPaul E. McKenney
6541f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6542f4073b0fSMichael S. Tsirkin
6543f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6544f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6545f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6546f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6547f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6548f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6549f4073b0fSMichael S. Tsirkin		}
6550f4073b0fSMichael S. Tsirkin
6551cb426e99SJoe Perches# check for waitqueue_active without a comment.
6552cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6553cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6554cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6555cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6556cb426e99SJoe Perches			}
6557cb426e99SJoe Perches		}
65583ad81779SPaul E. McKenney
65595099a722SMarco Elver# check for data_race without a comment.
65605099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
65615099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
65625099a722SMarco Elver				WARN("DATA_RACE",
65635099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
65645099a722SMarco Elver			}
65655099a722SMarco Elver		}
65665099a722SMarco Elver
65674a0df2efSAndy Whitcroft# check of hardware specific defines
6568c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6569000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6570000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
65710a920b5bSAndy Whitcroft		}
6572653d4876SAndy Whitcroft
6573596ed45bSJoe Perches# check that the storage class is not after a type
6574596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6575000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6576596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6577596ed45bSJoe Perches		}
6578596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6579596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6580596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6581596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6582596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6583596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6584596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6585d4977c78STobias Klauser		}
6586d4977c78STobias Klauser
6587de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6588de7d4f0eSAndy Whitcroft# storage class and type.
65899c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
65909c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6591000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6592000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6593de7d4f0eSAndy Whitcroft		}
6594de7d4f0eSAndy Whitcroft
65958905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
65962b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
65972b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6598d5e616fcSJoe Perches			if (WARN("INLINE",
6599d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6600d5e616fcSJoe Perches			    $fix) {
6601194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6602d5e616fcSJoe Perches
6603d5e616fcSJoe Perches			}
66048905a67cSAndy Whitcroft		}
66058905a67cSAndy Whitcroft
66067ebe1d17SDwaipayan Ray# Check for compiler attributes
66072b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
66087ebe1d17SDwaipayan Ray		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
66097ebe1d17SDwaipayan Ray			my $attr = $1;
66107ebe1d17SDwaipayan Ray			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
66117ebe1d17SDwaipayan Ray
66127ebe1d17SDwaipayan Ray			my %attr_list = (
66130830aab0SJoe Perches				"alias"				=> "__alias",
66147ebe1d17SDwaipayan Ray				"aligned"			=> "__aligned",
66157ebe1d17SDwaipayan Ray				"always_inline"			=> "__always_inline",
66167ebe1d17SDwaipayan Ray				"assume_aligned"		=> "__assume_aligned",
66177ebe1d17SDwaipayan Ray				"cold"				=> "__cold",
66187ebe1d17SDwaipayan Ray				"const"				=> "__attribute_const__",
66197ebe1d17SDwaipayan Ray				"copy"				=> "__copy",
66207ebe1d17SDwaipayan Ray				"designated_init"		=> "__designated_init",
66217ebe1d17SDwaipayan Ray				"externally_visible"		=> "__visible",
66227ebe1d17SDwaipayan Ray				"format"			=> "printf|scanf",
66237ebe1d17SDwaipayan Ray				"gnu_inline"			=> "__gnu_inline",
66247ebe1d17SDwaipayan Ray				"malloc"			=> "__malloc",
66257ebe1d17SDwaipayan Ray				"mode"				=> "__mode",
66267ebe1d17SDwaipayan Ray				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
66277ebe1d17SDwaipayan Ray				"noclone"			=> "__noclone",
66287ebe1d17SDwaipayan Ray				"noinline"			=> "noinline",
66297ebe1d17SDwaipayan Ray				"nonstring"			=> "__nonstring",
66307ebe1d17SDwaipayan Ray				"noreturn"			=> "__noreturn",
66317ebe1d17SDwaipayan Ray				"packed"			=> "__packed",
66327ebe1d17SDwaipayan Ray				"pure"				=> "__pure",
6633339f29d9SJoe Perches				"section"			=> "__section",
66340830aab0SJoe Perches				"used"				=> "__used",
66350830aab0SJoe Perches				"weak"				=> "__weak"
66367ebe1d17SDwaipayan Ray			);
66377ebe1d17SDwaipayan Ray
66387ebe1d17SDwaipayan Ray			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6639339f29d9SJoe Perches				my $orig_attr = $1;
66407ebe1d17SDwaipayan Ray				my $params = '';
66417ebe1d17SDwaipayan Ray				$params = $2 if defined($2);
6642339f29d9SJoe Perches				my $curr_attr = $orig_attr;
66437ebe1d17SDwaipayan Ray				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
66447ebe1d17SDwaipayan Ray				if (exists($attr_list{$curr_attr})) {
6645339f29d9SJoe Perches					my $new = $attr_list{$curr_attr};
66467ebe1d17SDwaipayan Ray					if ($curr_attr eq "format" && $params) {
66477ebe1d17SDwaipayan Ray						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6648339f29d9SJoe Perches						$new = "__$1\($2";
66497ebe1d17SDwaipayan Ray					} else {
6650339f29d9SJoe Perches						$new = "$new$params";
66517ebe1d17SDwaipayan Ray					}
66527ebe1d17SDwaipayan Ray					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6653339f29d9SJoe Perches						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
66547ebe1d17SDwaipayan Ray					    $fix) {
6655339f29d9SJoe Perches						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6656339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/$remove//;
6657339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6658339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6659339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
66607ebe1d17SDwaipayan Ray					}
666139b7e287SJoe Perches				}
6662462811d9SJoe Perches			}
6663462811d9SJoe Perches
66647ebe1d17SDwaipayan Ray			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
66657ebe1d17SDwaipayan Ray			if ($attr =~ /^_*unused/) {
66667ebe1d17SDwaipayan Ray				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
66677ebe1d17SDwaipayan Ray				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6668d5e616fcSJoe Perches			}
66696061d949SJoe Perches		}
66706061d949SJoe Perches
6671619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
66725b57980dSJoe Perches		if ($perl_version_ok &&
6673619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6674619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6675619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6676619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6677619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6678619a908aSJoe Perches		}
6679619a908aSJoe Perches
6680fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6681e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6682fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6683e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6684e6176fa4SJoe Perches			my $type = $1;
6685e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6686e6176fa4SJoe Perches				$type = $1;
6687e6176fa4SJoe Perches				my $kernel_type = 'u';
6688e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6689e6176fa4SJoe Perches				$type =~ /(\d+)/;
6690e6176fa4SJoe Perches				$kernel_type .= $1;
6691e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6692e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6693e6176fa4SJoe Perches				    $fix) {
6694e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6695e6176fa4SJoe Perches				}
6696e6176fa4SJoe Perches			}
6697e6176fa4SJoe Perches		}
6698e6176fa4SJoe Perches
6699938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6700938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6701938224b5SJoe Perches			my $cast = $1;
6702938224b5SJoe Perches			my $const = $2;
6703938224b5SJoe Perches			my $suffix = "";
6704938224b5SJoe Perches			my $newconst = $const;
6705938224b5SJoe Perches			$newconst =~ s/${Int_type}$//;
6706938224b5SJoe Perches			$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6707938224b5SJoe Perches			if ($cast =~ /\blong\s+long\b/) {
6708938224b5SJoe Perches			    $suffix .= 'LL';
6709938224b5SJoe Perches			} elsif ($cast =~ /\blong\b/) {
6710938224b5SJoe Perches			    $suffix .= 'L';
6711938224b5SJoe Perches			}
67120972b8bfSJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
67130972b8bfSJoe Perches				 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
67140972b8bfSJoe Perches			    $fix) {
6715938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6716938224b5SJoe Perches			}
6717938224b5SJoe Perches		}
6718938224b5SJoe Perches
67198f53a9b8SJoe Perches# check for sizeof(&)
67208f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6721000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6722000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
67238f53a9b8SJoe Perches		}
67248f53a9b8SJoe Perches
672566c80b60SJoe Perches# check for sizeof without parenthesis
672666c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6727d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6728d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6729d5e616fcSJoe Perches			    $fix) {
6730194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6731d5e616fcSJoe Perches			}
673266c80b60SJoe Perches		}
673366c80b60SJoe Perches
673488982feaSJoe Perches# check for struct spinlock declarations
673588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
673688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
673788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
673888982feaSJoe Perches		}
673988982feaSJoe Perches
6740a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
674106668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6742a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6743caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6744caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6745d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6746d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6747d5e616fcSJoe Perches				    $fix) {
6748194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6749d5e616fcSJoe Perches				}
6750a6962d72SJoe Perches			}
6751a6962d72SJoe Perches		}
6752a6962d72SJoe Perches
67530b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
67545b57980dSJoe Perches		if ($perl_version_ok &&
67550b523769SJoe Perches		    defined $stat &&
67560b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
67570b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6758e3c6bc95STobin C. Harding			my $stat_real;
6759e3c6bc95STobin C. Harding
67600b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
67610b523769SJoe Perches			$lc = $lc + $linenr;
67620b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6763ffe07513SJoe Perches				my $specifier;
6764ffe07513SJoe Perches				my $extension;
67653bd32d6aSSakari Ailus				my $qualifier;
6766ffe07513SJoe Perches				my $bad_specifier = "";
67670b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
67680b523769SJoe Perches				$fmt =~ s/%%//g;
6769e3c6bc95STobin C. Harding
67703bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6771e3c6bc95STobin C. Harding					$specifier = $1;
6772e3c6bc95STobin C. Harding					$extension = $2;
67733bd32d6aSSakari Ailus					$qualifier = $3;
6774af612e43SSakari Ailus					if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
67753bd32d6aSSakari Ailus					    ($extension eq "f" &&
6776af612e43SSakari Ailus					     defined $qualifier && $qualifier !~ /^w/) ||
6777af612e43SSakari Ailus					    ($extension eq "4" &&
6778af612e43SSakari Ailus					     defined $qualifier && $qualifier !~ /^cc/)) {
6779e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
67800b523769SJoe Perches						last;
67810b523769SJoe Perches					}
6782e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6783e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6784e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
67850b523769SJoe Perches						}
6786e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6787e3c6bc95STobin 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");
6788e3c6bc95STobin C. Harding					}
6789e3c6bc95STobin C. Harding				}
6790e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
67912a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
67921df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
67931df7338aSSergey Senozhatsky					my $use = "";
6794e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
67951df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6796e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
67971df7338aSSergey Senozhatsky					}
67982a9f9d85STobin C. Harding
67990b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6800e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6801e3c6bc95STobin C. Harding				}
68020b523769SJoe Perches			}
68030b523769SJoe Perches		}
68040b523769SJoe Perches
6805554e165cSAndy Whitcroft# Check for misused memsets
68065b57980dSJoe Perches		if ($perl_version_ok &&
6807d1fe9c09SJoe Perches		    defined $stat &&
68089e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6809554e165cSAndy Whitcroft
6810d7c76ba7SJoe Perches			my $ms_addr = $2;
6811d1fe9c09SJoe Perches			my $ms_val = $7;
6812d1fe9c09SJoe Perches			my $ms_size = $12;
6813d7c76ba7SJoe Perches
6814554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6815554e165cSAndy Whitcroft				ERROR("MEMSET",
6816d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6817554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6818554e165cSAndy Whitcroft				WARN("MEMSET",
6819d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6820d7c76ba7SJoe Perches			}
6821d7c76ba7SJoe Perches		}
6822d7c76ba7SJoe Perches
682398a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
68245b57980dSJoe Perches#		if ($perl_version_ok &&
6825f333195dSJoe Perches#		    defined $stat &&
6826f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6827f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6828f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6829f333195dSJoe Perches#			    $fix) {
6830f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6831f333195dSJoe Perches#			}
6832f333195dSJoe Perches#		}
683398a9bba5SJoe Perches
6834b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
68355b57980dSJoe Perches#		if ($perl_version_ok &&
6836f333195dSJoe Perches#		    defined $stat &&
6837f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6838f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6839f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6840f333195dSJoe Perches#		}
6841b6117d17SMateusz Kulikowski
68428617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
68438617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
68445b57980dSJoe Perches#		if ($perl_version_ok &&
6845f333195dSJoe Perches#		    defined $stat &&
6846f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6847f333195dSJoe Perches#
6848f333195dSJoe Perches#			my $ms_val = $7;
6849f333195dSJoe Perches#
6850f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6851f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6852f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6853f333195dSJoe Perches#				    $fix) {
6854f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6855f333195dSJoe Perches#				}
6856f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6857f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6858f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6859f333195dSJoe Perches#				    $fix) {
6860f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6861f333195dSJoe Perches#				}
6862f333195dSJoe Perches#			}
6863f333195dSJoe Perches#		}
68648617cd09SMateusz Kulikowski
68655dbdb2d8SJoe Perches# strlcpy uses that should likely be strscpy
68665dbdb2d8SJoe Perches		if ($line =~ /\bstrlcpy\s*\(/) {
68675dbdb2d8SJoe Perches			WARN("STRLCPY",
68685dbdb2d8SJoe Perches			     "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
68695dbdb2d8SJoe Perches		}
68705dbdb2d8SJoe Perches
6871d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
68725b57980dSJoe Perches		if ($perl_version_ok &&
6873d1fe9c09SJoe Perches		    defined $stat &&
6874d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6875d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6876d7c76ba7SJoe Perches				my $call = $1;
6877d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6878d7c76ba7SJoe Perches				my $arg1 = $3;
6879d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6880d1fe9c09SJoe Perches				my $arg2 = $8;
6881d7c76ba7SJoe Perches				my $cast;
6882d7c76ba7SJoe Perches
6883d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6884d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6885d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6886d7c76ba7SJoe Perches					$cast = $cast1;
6887d7c76ba7SJoe Perches				} else {
6888d7c76ba7SJoe Perches					$cast = $cast2;
6889d7c76ba7SJoe Perches				}
6890d7c76ba7SJoe Perches				WARN("MINMAX",
6891d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6892554e165cSAndy Whitcroft			}
6893554e165cSAndy Whitcroft		}
6894554e165cSAndy Whitcroft
68954a273195SJoe Perches# check usleep_range arguments
68965b57980dSJoe Perches		if ($perl_version_ok &&
68974a273195SJoe Perches		    defined $stat &&
68984a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
68994a273195SJoe Perches			my $min = $1;
69004a273195SJoe Perches			my $max = $7;
69014a273195SJoe Perches			if ($min eq $max) {
69024a273195SJoe Perches				WARN("USLEEP_RANGE",
6903458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
69044a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
69054a273195SJoe Perches				 $min > $max) {
69064a273195SJoe Perches				WARN("USLEEP_RANGE",
6907458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
69084a273195SJoe Perches			}
69094a273195SJoe Perches		}
69104a273195SJoe Perches
6911823b794cSJoe Perches# check for naked sscanf
69125b57980dSJoe Perches		if ($perl_version_ok &&
6913823b794cSJoe Perches		    defined $stat &&
69146c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6915823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6916823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6917823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6918823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6919823b794cSJoe Perches			$lc = $lc + $linenr;
69202a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6921823b794cSJoe Perches			WARN("NAKED_SSCANF",
6922823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6923823b794cSJoe Perches		}
6924823b794cSJoe Perches
6925afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
69265b57980dSJoe Perches		if ($perl_version_ok &&
6927afc819abSJoe Perches		    defined $stat &&
6928afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6929afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6930afc819abSJoe Perches			$lc = $lc + $linenr;
69312a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6932afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6933afc819abSJoe Perches				my $format = $6;
6934afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6935afc819abSJoe Perches				if ($count == 1 &&
6936afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6937afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6938afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6939afc819abSJoe Perches				}
6940afc819abSJoe Perches			}
6941afc819abSJoe Perches		}
6942afc819abSJoe Perches
694370dc8a48SJoe Perches# check for new externs in .h files.
694470dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
694570dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6946d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
694770dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
694870dc8a48SJoe Perches			    $fix) {
6949194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
695070dc8a48SJoe Perches			}
695170dc8a48SJoe Perches		}
695270dc8a48SJoe Perches
6953de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6954171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6955c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6956171ae1a4SAndy Whitcroft		{
6957c45dcabdSAndy Whitcroft			my $function_name = $1;
6958c45dcabdSAndy Whitcroft			my $paren_space = $2;
6959171ae1a4SAndy Whitcroft
6960171ae1a4SAndy Whitcroft			my $s = $stat;
6961171ae1a4SAndy Whitcroft			if (defined $cond) {
6962171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6963171ae1a4SAndy Whitcroft			}
6964d8b44b58SKees Cook			if ($s =~ /^\s*;/)
6965c45dcabdSAndy Whitcroft			{
6966000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6967000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6968de7d4f0eSAndy Whitcroft			}
6969de7d4f0eSAndy Whitcroft
6970171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6971000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6972000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6973171ae1a4SAndy Whitcroft			}
69749c9ba34eSAndy Whitcroft
69759c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
69769c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
69779c9ba34eSAndy Whitcroft		{
6978000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6979000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6980171ae1a4SAndy Whitcroft		}
6981171ae1a4SAndy Whitcroft
6982a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6983a0ad7596SJoe Perches		if (defined $stat &&
6984d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6985d8b44b58SKees Cook		    $1 ne "void") {
6986d8b44b58SKees Cook			my $args = trim($1);
6987ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6988ca0d8929SJoe Perches				my $arg = trim($1);
6989d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6990ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6991ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6992ca0d8929SJoe Perches				}
6993ca0d8929SJoe Perches			}
6994ca0d8929SJoe Perches		}
6995ca0d8929SJoe Perches
6996a0ad7596SJoe Perches# check for function definitions
69975b57980dSJoe Perches		if ($perl_version_ok &&
6998a0ad7596SJoe Perches		    defined $stat &&
6999a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
7000a0ad7596SJoe Perches			$context_function = $1;
7001a0ad7596SJoe Perches
7002a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
7003a0ad7596SJoe Perches			my $ok = 0;
7004a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
7005a0ad7596SJoe Perches			my $herectx = $here . "\n";
7006a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
7007a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
7008a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
7009a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
7010a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
7011a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
7012a0ad7596SJoe Perches			}
7013a0ad7596SJoe Perches			if (!$ok) {
7014a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
7015a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
7016a0ad7596SJoe Perches			}
7017a0ad7596SJoe Perches		}
7018a0ad7596SJoe Perches
7019de7d4f0eSAndy Whitcroft# checks for new __setup's
7020de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
7021de7d4f0eSAndy Whitcroft			my $name = $1;
7022de7d4f0eSAndy Whitcroft
7023de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
7024000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
70252581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
7026de7d4f0eSAndy Whitcroft			}
7027653d4876SAndy Whitcroft		}
70289c0ca6f9SAndy Whitcroft
7029e29a70f1SJoe Perches# check for pointless casting of alloc functions
7030e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
7031000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
7032000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
70339c0ca6f9SAndy Whitcroft		}
703413214adfSAndy Whitcroft
7035a640d25cSJoe Perches# alloc style
7036a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
70375b57980dSJoe Perches		if ($perl_version_ok &&
7038e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
7039a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
7040a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7041a640d25cSJoe Perches		}
7042a640d25cSJoe Perches
704373f1d07eSGustavo A. R. Silva# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
70445b57980dSJoe Perches		if ($perl_version_ok &&
70451b4a2ed4SJoe Perches		    defined $stat &&
704673f1d07eSGustavo A. R. Silva		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
704760a55369SJoe Perches			my $oldfunc = $3;
704860a55369SJoe Perches			my $a1 = $4;
704960a55369SJoe Perches			my $a2 = $10;
705060a55369SJoe Perches			my $newfunc = "kmalloc_array";
705173f1d07eSGustavo A. R. Silva			$newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
705273f1d07eSGustavo A. R. Silva			$newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
705360a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
705460a55369SJoe Perches			my $r1 = $a1;
705560a55369SJoe Perches			my $r2 = $a2;
705660a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
705760a55369SJoe Perches				$r1 = $a2;
705860a55369SJoe Perches				$r2 = $a1;
705960a55369SJoe Perches			}
7060e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7061e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
70621b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
7063e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
7064e3d95a2aSTobin C. Harding
7065e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
70661b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
70671b4a2ed4SJoe Perches				    $cnt == 1 &&
7068e367455aSJoe Perches				    $fix) {
706973f1d07eSGustavo A. R. Silva					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
707060a55369SJoe Perches				}
707160a55369SJoe Perches			}
707260a55369SJoe Perches		}
707360a55369SJoe Perches
7074972fdea2SJoe Perches# check for krealloc arg reuse
70755b57980dSJoe Perches		if ($perl_version_ok &&
70764cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
70774cab63ceSJoe Perches		    $1 eq $3) {
7078972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
7079972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7080972fdea2SJoe Perches		}
7081972fdea2SJoe Perches
70825ce59ae0SJoe Perches# check for alloc argument mismatch
70837e6cdd7fSChristophe JAILLET		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
70845ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
70855ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
70865ce59ae0SJoe Perches		}
70875ce59ae0SJoe Perches
7088caf2a54fSJoe Perches# check for multiple semicolons
7089caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
7090d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
7091d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7092d5e616fcSJoe Perches			    $fix) {
7093194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
7094d5e616fcSJoe Perches			}
7095d1e2ad07SJoe Perches		}
7096d1e2ad07SJoe Perches
7097cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7098cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
7099cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
71000ab90191SJoe Perches			my $ull = "";
71010ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
71020ab90191SJoe Perches			if (CHK("BIT_MACRO",
71030ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
71040ab90191SJoe Perches			    $fix) {
71050ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
71060ab90191SJoe Perches			}
71070ab90191SJoe Perches		}
71080ab90191SJoe Perches
710950161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
71103e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
711150161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
71123e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
711350161266SJoe Perches		}
711450161266SJoe Perches
71152d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
71163e89ad85SJerome 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*$/) {
71172d632745SJoe Perches			my $config = $1;
71182d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
71193e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
71202d632745SJoe Perches			    $fix) {
71212d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
71222d632745SJoe Perches			}
71232d632745SJoe Perches		}
71242d632745SJoe Perches
7125f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
7126f36d3eb8SJoe Perches		my @fallthroughs = (
7127f36d3eb8SJoe Perches			'fallthrough',
7128f36d3eb8SJoe Perches			'@fallthrough@',
7129f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
7130f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7131f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7132f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7133f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7134f36d3eb8SJoe Perches		    );
7135f36d3eb8SJoe Perches		if ($raw_comment ne '') {
7136f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
7137f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
7138f36d3eb8SJoe Perches					my $msg_level = \&WARN;
7139f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
7140f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
7141f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7142f36d3eb8SJoe Perches					last;
7143f36d3eb8SJoe Perches				}
7144f36d3eb8SJoe Perches			}
7145f36d3eb8SJoe Perches		}
7146f36d3eb8SJoe Perches
7147d1e2ad07SJoe Perches# check for switch/default statements without a break;
71485b57980dSJoe Perches		if ($perl_version_ok &&
7149d1e2ad07SJoe Perches		    defined $stat &&
7150d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
7151d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
7152e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
7153e3d95a2aSTobin C. Harding
7154d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
7155d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
7156caf2a54fSJoe Perches		}
7157caf2a54fSJoe Perches
715813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
7159d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
7160d5e616fcSJoe Perches			if (WARN("USE_FUNC",
7161d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
7162d5e616fcSJoe Perches			    $fix) {
7163194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
7164d5e616fcSJoe Perches			}
716513214adfSAndy Whitcroft		}
7166773647a0SAndy Whitcroft
716762ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
716862ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
716962ec818fSJoe Perches			ERROR("DATE_TIME",
717062ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
717162ec818fSJoe Perches		}
717262ec818fSJoe Perches
71732c92488aSJoe Perches# check for use of yield()
71742c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
71752c92488aSJoe Perches			WARN("YIELD",
71762c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
71772c92488aSJoe Perches		}
71782c92488aSJoe Perches
7179179f8f40SJoe Perches# check for comparisons against true and false
7180179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7181179f8f40SJoe Perches			my $lead = $1;
7182179f8f40SJoe Perches			my $arg = $2;
7183179f8f40SJoe Perches			my $test = $3;
7184179f8f40SJoe Perches			my $otype = $4;
7185179f8f40SJoe Perches			my $trail = $5;
7186179f8f40SJoe Perches			my $op = "!";
7187179f8f40SJoe Perches
7188179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7189179f8f40SJoe Perches
7190179f8f40SJoe Perches			my $type = lc($otype);
7191179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
7192179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
7193179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
7194179f8f40SJoe Perches					$op = "";
7195179f8f40SJoe Perches				}
7196179f8f40SJoe Perches
7197179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
7198179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
7199179f8f40SJoe Perches
7200179f8f40SJoe Perches## maybe suggesting a correct construct would better
7201179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7202179f8f40SJoe Perches
7203179f8f40SJoe Perches			}
7204179f8f40SJoe Perches		}
7205179f8f40SJoe Perches
72064882720bSThomas Gleixner# check for semaphores initialized locked
72074882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
7208000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
7209000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
7210773647a0SAndy Whitcroft		}
72116712d858SJoe Perches
721267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
721367d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
7214000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
721567d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
7216773647a0SAndy Whitcroft		}
72176712d858SJoe Perches
7218ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
7219f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
7220000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
7221ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
7222f3db6639SMichael Ellerman		}
72236712d858SJoe Perches
72243d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
72253d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
72263d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
72273d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
72283d709ab5SPaul E. McKenney		}
72293d709ab5SPaul E. McKenney
72309189c7e7SJoe Perches# check for deprecated apis
72319189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
72329189c7e7SJoe Perches			my $deprecated_api = $1;
72339189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
72349189c7e7SJoe Perches			WARN("DEPRECATED_API",
72359189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
72369189c7e7SJoe Perches		}
72379189c7e7SJoe Perches
72380f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
7239d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
7240ced69da1SQuentin Monnet		if (defined($const_structs) &&
7241ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
7242d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7243000d1cc1SJoe Perches			WARN("CONST_STRUCT",
7244d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
72452b6db5cbSAndy Whitcroft		}
7246773647a0SAndy Whitcroft
7247773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
7248773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
724935cdcbfcSPeng Wang# ignore designated initializers using NR_CPUS
7250773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
7251c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7252c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7253171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7254171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
725535cdcbfcSPeng Wang		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
725635cdcbfcSPeng Wang		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
7257773647a0SAndy Whitcroft		{
7258000d1cc1SJoe Perches			WARN("NR_CPUS",
7259000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7260773647a0SAndy Whitcroft		}
72619c9ba34eSAndy Whitcroft
726252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
726352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
726452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
726552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
726652ea8506SJoe Perches		}
726752ea8506SJoe Perches
7268acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
72695b57980dSJoe Perches		if ($perl_version_ok &&
7270acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7271acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
7272acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7273acd9362cSJoe Perches		}
7274acd9362cSJoe Perches
7275fbe74541SJoe Perches# return sysfs_emit(foo, fmt, ...) fmt without newline
7276fbe74541SJoe Perches		if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7277fbe74541SJoe Perches		    substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7278fbe74541SJoe Perches			my $offset = $+[6] - 1;
7279fbe74541SJoe Perches			if (WARN("SYSFS_EMIT",
7280fbe74541SJoe Perches				 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7281fbe74541SJoe Perches			    $fix) {
7282fbe74541SJoe Perches				substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7283fbe74541SJoe Perches			}
7284fbe74541SJoe Perches		}
7285fbe74541SJoe Perches
7286de3f186fSDenis Efremov# nested likely/unlikely calls
7287de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7288de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
7289de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7290de3f186fSDenis Efremov		}
7291de3f186fSDenis Efremov
7292691d77b6SAndy Whitcroft# whine mightly about in_atomic
7293691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
7294691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
7295000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
7296000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
7297f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
7298000d1cc1SJoe Perches				WARN("IN_ATOMIC",
7299000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7300691d77b6SAndy Whitcroft			}
7301691d77b6SAndy Whitcroft		}
73021704f47bSPeter Zijlstra
73031704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
73041704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
73051704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
73061704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
73071704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
73081704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
7309000d1cc1SJoe Perches				ERROR("LOCKDEP",
7310000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
73111704f47bSPeter Zijlstra			}
73121704f47bSPeter Zijlstra		}
731388f8831cSDave Jones
7314b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7315b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7316000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
7317000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
731888f8831cSDave Jones		}
73192435880fSJoe Perches
732000180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
732100180468SJoe Perches# and whether or not function naming is typical and if
732200180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
73235b57980dSJoe Perches		if ($perl_version_ok &&
732400180468SJoe Perches		    defined $stat &&
732500180468SJoe 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*\)/) {
732600180468SJoe Perches			my $var = $1;
732700180468SJoe Perches			my $perms = $2;
732800180468SJoe Perches			my $show = $3;
732900180468SJoe Perches			my $store = $4;
733000180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
733100180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
733200180468SJoe Perches			    $store =~ /^${var}_store$/ &&
733300180468SJoe Perches			    $octal_perms eq "0644") {
733400180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
733500180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
733600180468SJoe Perches				    $fix) {
733700180468SJoe 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})/;
733800180468SJoe Perches				}
733900180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
734000180468SJoe Perches				 $store =~ /^NULL$/ &&
734100180468SJoe Perches				 $octal_perms eq "0444") {
734200180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
734300180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
734400180468SJoe Perches				    $fix) {
734500180468SJoe 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})/;
734600180468SJoe Perches				}
734700180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
734800180468SJoe Perches				 $store =~ /^${var}_store$/ &&
734900180468SJoe Perches				 $octal_perms eq "0200") {
735000180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
735100180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
735200180468SJoe Perches				    $fix) {
735300180468SJoe 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})/;
735400180468SJoe Perches				}
735500180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
735600180468SJoe Perches				 $octal_perms eq "0444" ||
735700180468SJoe Perches				 $octal_perms eq "0200") {
735800180468SJoe Perches				my $newshow = "$show";
735900180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
736000180468SJoe Perches				my $newstore = $store;
736100180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
736200180468SJoe Perches				my $rename = "";
736300180468SJoe Perches				if ($show ne $newshow) {
736400180468SJoe Perches					$rename .= " '$show' to '$newshow'";
736500180468SJoe Perches				}
736600180468SJoe Perches				if ($store ne $newstore) {
736700180468SJoe Perches					$rename .= " '$store' to '$newstore'";
736800180468SJoe Perches				}
736900180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
737000180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
737100180468SJoe Perches			} else {
737200180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
737300180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
737400180468SJoe Perches			}
737500180468SJoe Perches		}
737600180468SJoe Perches
7377515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
7378515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
737973121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
738073121534SJoe Perches#   specific definition of not visible in sysfs.
738173121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
738273121534SJoe Perches#   use the default permissions
73835b57980dSJoe Perches		if ($perl_version_ok &&
7384459cf0aeSJoe Perches		    defined $stat &&
7385515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
73862435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
73872435880fSJoe Perches				my $func = $entry->[0];
73882435880fSJoe Perches				my $arg_pos = $entry->[1];
73892435880fSJoe Perches
7390459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
7391459cf0aeSJoe Perches				$lc = $lc + $linenr;
73922a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
7393459cf0aeSJoe Perches
73942435880fSJoe Perches				my $skip_args = "";
73952435880fSJoe Perches				if ($arg_pos > 1) {
73962435880fSJoe Perches					$arg_pos--;
73972435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
73982435880fSJoe Perches				}
7399f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7400459cf0aeSJoe Perches				if ($stat =~ /$test/) {
74012435880fSJoe Perches					my $val = $1;
74022435880fSJoe Perches					$val = $6 if ($skip_args ne "");
740373121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
740473121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
740573121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
74062435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
7407459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7408f90774e1SJoe Perches					}
7409f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7410c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
7411459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
74122435880fSJoe Perches					}
7413459cf0aeSJoe Perches				}
7414459cf0aeSJoe Perches			}
7415459cf0aeSJoe Perches		}
7416459cf0aeSJoe Perches
7417459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
7418bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
741900180468SJoe Perches			my $oval = $1;
742000180468SJoe Perches			my $octal = perms_to_octal($oval);
7421f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
7422459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7423f90774e1SJoe Perches			    $fix) {
742400180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
74252435880fSJoe Perches			}
742613214adfSAndy Whitcroft		}
74275a6d20ceSBjorn Andersson
74285a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
74295a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
74305a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
74315a6d20ceSBjorn Andersson			my $valid_licenses = qr{
74325a6d20ceSBjorn Andersson						GPL|
74335a6d20ceSBjorn Andersson						GPL\ v2|
74345a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
74355a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
74365a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
74375a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
74385a6d20ceSBjorn Andersson						Proprietary
74395a6d20ceSBjorn Andersson					}x;
74405a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
74415a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
74425a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
74435a6d20ceSBjorn Andersson			}
74446e8f42dcSJoe Perches			if (!$file && $extracted_string eq '"GPL v2"') {
74456e8f42dcSJoe Perches				if (WARN("MODULE_LICENSE",
74466e8f42dcSJoe Perches				     "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) &&
74476e8f42dcSJoe Perches				    $fix) {
74486e8f42dcSJoe Perches					$fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/;
74496e8f42dcSJoe Perches				}
74506e8f42dcSJoe Perches			}
74515a6d20ceSBjorn Andersson		}
74526a8d76cbSMatteo Croce
74536a8d76cbSMatteo Croce# check for sysctl duplicate constants
74546a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
74556a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
74566a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
74576a8d76cbSMatteo Croce		}
7458515a235eSJoe Perches	}
745913214adfSAndy Whitcroft
746013214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
746113214adfSAndy Whitcroft	# so just keep quiet.
746213214adfSAndy Whitcroft	if ($#rawlines == -1) {
746313214adfSAndy Whitcroft		exit(0);
74640a920b5bSAndy Whitcroft	}
74650a920b5bSAndy Whitcroft
74668905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
74678905a67cSAndy Whitcroft	# things that appear to be patches.
74688905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
74698905a67cSAndy Whitcroft		exit(0);
74708905a67cSAndy Whitcroft	}
74718905a67cSAndy Whitcroft
7472e73d2715SDwaipayan Ray	# This is not a patch, and we are in 'no-patch' mode so
74738905a67cSAndy Whitcroft	# just keep quiet.
74748905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
74758905a67cSAndy Whitcroft		exit(0);
74768905a67cSAndy Whitcroft	}
74778905a67cSAndy Whitcroft
7478a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7479000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
7480000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
74810a920b5bSAndy Whitcroft	}
7482cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
7483cd261496SGeert Uytterhoeven		if ($signoff == 0) {
7484000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
7485000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
748648ca2d8aSDwaipayan Ray		} elsif ($authorsignoff != 1) {
748748ca2d8aSDwaipayan Ray			# authorsignoff values:
748848ca2d8aSDwaipayan Ray			# 0 -> missing sign off
748948ca2d8aSDwaipayan Ray			# 1 -> sign off identical
749048ca2d8aSDwaipayan Ray			# 2 -> names and addresses match, comments mismatch
749148ca2d8aSDwaipayan Ray			# 3 -> addresses match, names different
749248ca2d8aSDwaipayan Ray			# 4 -> names match, addresses different
749348ca2d8aSDwaipayan Ray			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
749448ca2d8aSDwaipayan Ray
749548ca2d8aSDwaipayan Ray			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
749648ca2d8aSDwaipayan Ray
749748ca2d8aSDwaipayan Ray			if ($authorsignoff == 0) {
749848ca2d8aSDwaipayan Ray				ERROR("NO_AUTHOR_SIGN_OFF",
7499cd261496SGeert Uytterhoeven				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
750048ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 2) {
750148ca2d8aSDwaipayan Ray				CHK("FROM_SIGN_OFF_MISMATCH",
750248ca2d8aSDwaipayan Ray				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
750348ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 3) {
750448ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
750548ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
750648ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 4) {
750748ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
750848ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
750948ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 5) {
751048ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
751148ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
751248ca2d8aSDwaipayan Ray			}
7513cd261496SGeert Uytterhoeven		}
75140a920b5bSAndy Whitcroft	}
75150a920b5bSAndy Whitcroft
7516f0a594c1SAndy Whitcroft	print report_dump();
751713214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
751813214adfSAndy Whitcroft		print "$filename " if ($summary_file);
75196c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
75206c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
75216c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
75226c72ffaaSAndy Whitcroft	}
75238905a67cSAndy Whitcroft
7524d2c0a235SAndy Whitcroft	if ($quiet == 0) {
7525ef212196SJoe Perches		# If there were any defects found and not already fixing them
7526ef212196SJoe Perches		if (!$clean and !$fix) {
7527ef212196SJoe Perches			print << "EOM"
7528ef212196SJoe Perches
7529ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
7530ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
7531ef212196SJoe PerchesEOM
7532ef212196SJoe Perches		}
7533d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
7534d2c0a235SAndy Whitcroft		# then suggest that.
7535d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
7536b0781216SMike Frysinger			$rpt_cleaners = 0;
7537d8469f16SJoe Perches			print << "EOM"
7538d8469f16SJoe Perches
7539d8469f16SJoe PerchesNOTE: Whitespace errors detected.
7540d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
7541d8469f16SJoe PerchesEOM
7542d2c0a235SAndy Whitcroft		}
7543d2c0a235SAndy Whitcroft	}
7544d2c0a235SAndy Whitcroft
7545d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
7546d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
7547d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
75489624b8d6SJoe Perches		my $newfile = $filename;
75499624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
75503705ce5bSJoe Perches		my $linecount = 0;
75513705ce5bSJoe Perches		my $f;
75523705ce5bSJoe Perches
7553d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7554d752fcc8SJoe Perches
75553705ce5bSJoe Perches		open($f, '>', $newfile)
75563705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
75573705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
75583705ce5bSJoe Perches			$linecount++;
75593705ce5bSJoe Perches			if ($file) {
75603705ce5bSJoe Perches				if ($linecount > 3) {
75613705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
75623705ce5bSJoe Perches					print $f $fixed_line . "\n";
75633705ce5bSJoe Perches				}
75643705ce5bSJoe Perches			} else {
75653705ce5bSJoe Perches				print $f $fixed_line . "\n";
75663705ce5bSJoe Perches			}
75673705ce5bSJoe Perches		}
75683705ce5bSJoe Perches		close($f);
75693705ce5bSJoe Perches
75703705ce5bSJoe Perches		if (!$quiet) {
75713705ce5bSJoe Perches			print << "EOM";
7572d8469f16SJoe Perches
75733705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
75743705ce5bSJoe Perches
75753705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
75763705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
75773705ce5bSJoe Perches
75783705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
75793705ce5bSJoe PerchesNo warranties, expressed or implied...
75803705ce5bSJoe PerchesEOM
75813705ce5bSJoe Perches		}
75823705ce5bSJoe Perches	}
75833705ce5bSJoe Perches
7584d8469f16SJoe Perches	if ($quiet == 0) {
7585d8469f16SJoe Perches		print "\n";
7586d8469f16SJoe Perches		if ($clean == 1) {
7587d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7588d8469f16SJoe Perches		} else {
7589d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
75900a920b5bSAndy Whitcroft		}
75910a920b5bSAndy Whitcroft	}
75920a920b5bSAndy Whitcroft	return $clean;
75930a920b5bSAndy Whitcroft}
7594