xref: /linux-6.15/scripts/checkpatch.pl (revision d6ccdd67)
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)};
5798ea0114eSMickaël Salaünour $typeStdioTypedefs = qr{(?x:
5808ea0114eSMickaël Salaün	FILE
5818ea0114eSMickaël Salaün)};
582e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
583e6176fa4SJoe Perches	$typeC99Typedefs\b|
584e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
5858ea0114eSMickaël Salaün	$typeKernelTypedefs\b|
5868ea0114eSMickaë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
62344c31888SMatthieu Baertsour @link_tags = qw(Link Closes);
624f94e40eaSMatthieu Baerts
625f94e40eaSMatthieu Baerts#Create a search and print patterns for all these strings to be used directly below
626f94e40eaSMatthieu Baertsour $link_tags_search = "";
627f94e40eaSMatthieu Baertsour $link_tags_print = "";
628f94e40eaSMatthieu Baertsforeach my $entry (@link_tags) {
629f94e40eaSMatthieu Baerts	if ($link_tags_search ne "") {
630f94e40eaSMatthieu Baerts		$link_tags_search .= '|';
631f94e40eaSMatthieu Baerts		$link_tags_print .= ' or ';
632f94e40eaSMatthieu Baerts	}
633f94e40eaSMatthieu Baerts	$entry .= ':';
634f94e40eaSMatthieu Baerts	$link_tags_search .= $entry;
635f94e40eaSMatthieu Baerts	$link_tags_print .= "'$entry'";
636f94e40eaSMatthieu Baerts}
637f94e40eaSMatthieu Baerts$link_tags_search = "(?:${link_tags_search})";
638f94e40eaSMatthieu Baerts
639adb2da82SJoe Perchesour $tracing_logging_tags = qr{(?xi:
640adb2da82SJoe Perches	[=-]*> |
641adb2da82SJoe Perches	<[=-]* |
642adb2da82SJoe Perches	\[ |
643adb2da82SJoe Perches	\] |
644adb2da82SJoe Perches	start |
645adb2da82SJoe Perches	called |
646adb2da82SJoe Perches	entered |
647adb2da82SJoe Perches	entry |
648adb2da82SJoe Perches	enter |
649adb2da82SJoe Perches	in |
650adb2da82SJoe Perches	inside |
651adb2da82SJoe Perches	here |
652adb2da82SJoe Perches	begin |
653adb2da82SJoe Perches	exit |
654adb2da82SJoe Perches	end |
655adb2da82SJoe Perches	done |
656adb2da82SJoe Perches	leave |
657adb2da82SJoe Perches	completed |
658adb2da82SJoe Perches	out |
659adb2da82SJoe Perches	return |
660adb2da82SJoe Perches	[\.\!:\s]*
661adb2da82SJoe Perches)};
662adb2da82SJoe Perches
663831242abSAditya Srivastavasub edit_distance_min {
664831242abSAditya Srivastava	my (@arr) = @_;
665831242abSAditya Srivastava	my $len = scalar @arr;
666831242abSAditya Srivastava	if ((scalar @arr) < 1) {
667831242abSAditya Srivastava		# if underflow, return
668831242abSAditya Srivastava		return;
669831242abSAditya Srivastava	}
670831242abSAditya Srivastava	my $min = $arr[0];
671831242abSAditya Srivastava	for my $i (0 .. ($len-1)) {
672831242abSAditya Srivastava		if ($arr[$i] < $min) {
673831242abSAditya Srivastava			$min = $arr[$i];
674831242abSAditya Srivastava		}
675831242abSAditya Srivastava	}
676831242abSAditya Srivastava	return $min;
677831242abSAditya Srivastava}
678831242abSAditya Srivastava
679831242abSAditya Srivastavasub get_edit_distance {
680831242abSAditya Srivastava	my ($str1, $str2) = @_;
681831242abSAditya Srivastava	$str1 = lc($str1);
682831242abSAditya Srivastava	$str2 = lc($str2);
683831242abSAditya Srivastava	$str1 =~ s/-//g;
684831242abSAditya Srivastava	$str2 =~ s/-//g;
685831242abSAditya Srivastava	my $len1 = length($str1);
686831242abSAditya Srivastava	my $len2 = length($str2);
687831242abSAditya Srivastava	# two dimensional array storing minimum edit distance
688831242abSAditya Srivastava	my @distance;
689831242abSAditya Srivastava	for my $i (0 .. $len1) {
690831242abSAditya Srivastava		for my $j (0 .. $len2) {
691831242abSAditya Srivastava			if ($i == 0) {
692831242abSAditya Srivastava				$distance[$i][$j] = $j;
693831242abSAditya Srivastava			} elsif ($j == 0) {
694831242abSAditya Srivastava				$distance[$i][$j] = $i;
695831242abSAditya Srivastava			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
696831242abSAditya Srivastava				$distance[$i][$j] = $distance[$i - 1][$j - 1];
697831242abSAditya Srivastava			} else {
698831242abSAditya Srivastava				my $dist1 = $distance[$i][$j - 1]; #insert distance
699831242abSAditya Srivastava				my $dist2 = $distance[$i - 1][$j]; # remove
700831242abSAditya Srivastava				my $dist3 = $distance[$i - 1][$j - 1]; #replace
701831242abSAditya Srivastava				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
702831242abSAditya Srivastava			}
703831242abSAditya Srivastava		}
704831242abSAditya Srivastava	}
705831242abSAditya Srivastava	return $distance[$len1][$len2];
706831242abSAditya Srivastava}
707831242abSAditya Srivastava
708831242abSAditya Srivastavasub find_standard_signature {
709831242abSAditya Srivastava	my ($sign_off) = @_;
710831242abSAditya Srivastava	my @standard_signature_tags = (
711831242abSAditya Srivastava		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
712831242abSAditya Srivastava		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
713831242abSAditya Srivastava	);
714831242abSAditya Srivastava	foreach my $signature (@standard_signature_tags) {
715831242abSAditya Srivastava		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
716831242abSAditya Srivastava	}
717831242abSAditya Srivastava
718831242abSAditya Srivastava	return "";
719831242abSAditya Srivastava}
720831242abSAditya Srivastava
7219b71f79fSBjorn Helgaasour $obsolete_archives = qr{(?xi:
7229b71f79fSBjorn Helgaas	\Qfreedesktop.org/archives/dri-devel\E |
7239b71f79fSBjorn Helgaas	\Qlists.infradead.org\E |
7249b71f79fSBjorn Helgaas	\Qlkml.org\E |
7259b71f79fSBjorn Helgaas	\Qmail-archive.com\E |
7269b71f79fSBjorn Helgaas	\Qmailman.alsa-project.org/pipermail\E |
7279b71f79fSBjorn Helgaas	\Qmarc.info\E |
7289b71f79fSBjorn Helgaas	\Qozlabs.org/pipermail\E |
7299b71f79fSBjorn Helgaas	\Qspinics.net\E
7309b71f79fSBjorn Helgaas)};
7319b71f79fSBjorn Helgaas
7321813087dSJoe Perchesour @typeListMisordered = (
7331813087dSJoe Perches	qr{char\s+(?:un)?signed},
7341813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
7351813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
7361813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
7371813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
7381813087dSJoe Perches	qr{short\s+(?:un)?signed},
7391813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
7401813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
7411813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
7421813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
7431813087dSJoe Perches	qr{int\s+(?:un)?signed},
7441813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
7451813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
7461813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
7471813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
7481813087dSJoe Perches	qr{long\s+(?:un)?signed},
7491813087dSJoe Perches);
7501813087dSJoe Perches
7518905a67cSAndy Whitcroftour @typeList = (
7528905a67cSAndy Whitcroft	qr{void},
7530c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
7540c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
7550c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
7560c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
7570c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
7580c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
7590c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
7600c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
7610c773d9dSJoe Perches	qr{(?:un)?signed},
7628905a67cSAndy Whitcroft	qr{float},
7638905a67cSAndy Whitcroft	qr{double},
7648905a67cSAndy Whitcroft	qr{bool},
7658905a67cSAndy Whitcroft	qr{struct\s+$Ident},
7668905a67cSAndy Whitcroft	qr{union\s+$Ident},
7678905a67cSAndy Whitcroft	qr{enum\s+$Ident},
7688905a67cSAndy Whitcroft	qr{${Ident}_t},
7698905a67cSAndy Whitcroft	qr{${Ident}_handler},
7708905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
7711813087dSJoe Perches	@typeListMisordered,
7728905a67cSAndy Whitcroft);
773938224b5SJoe Perches
774938224b5SJoe Perchesour $C90_int_types = qr{(?x:
775938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
776938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
777938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
778938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
779938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
780938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
781938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
782938224b5SJoe Perches
783938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
784938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
785938224b5SJoe Perches	long\s+(?:un)?signed|
786938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
787938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
788938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
789938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
790938224b5SJoe Perches
791938224b5SJoe Perches	int\s+(?:un)?signed|
792938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
793938224b5SJoe Perches)};
794938224b5SJoe Perches
795485ff23eSAlex Dowadour @typeListFile = ();
7968716de38SJoe Perchesour @typeListWithAttr = (
7978716de38SJoe Perches	@typeList,
7988716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
7998716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
8008716de38SJoe Perches);
8018716de38SJoe Perches
802c45dcabdSAndy Whitcroftour @modifierList = (
803c45dcabdSAndy Whitcroft	qr{fastcall},
804c45dcabdSAndy Whitcroft);
805485ff23eSAlex Dowadour @modifierListFile = ();
8068905a67cSAndy Whitcroft
8072435880fSJoe Perchesour @mode_permission_funcs = (
8082435880fSJoe Perches	["module_param", 3],
8092435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
8102435880fSJoe Perches	["module_param_array_named", 5],
8112435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
8122435880fSJoe Perches	["proc_create(?:_data|)", 2],
813459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
814459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
815459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
816459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
817459cf0aeSJoe Perches	["__ATTR", 2],
8182435880fSJoe Perches);
8192435880fSJoe Perches
8201a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
8211a3dcf2eSJoe Perches
822515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
823515a235eSJoe Perchesour $mode_perms_search = "";
824515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
825515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
826515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
827515a235eSJoe Perches}
82800180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
829515a235eSJoe Perches
8309189c7e7SJoe Perchesour %deprecated_apis = (
8319189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
8329189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
8339189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
8349189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
8359189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
8369189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
8379189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
8389189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
8399189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
8409189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
841defdaff1SIra Weiny	"kmap"					=> "kmap_local_page",
842a3ea42ffSIra Weiny	"kunmap"				=> "kunmap_local",
843defdaff1SIra Weiny	"kmap_atomic"				=> "kmap_local_page",
844a3ea42ffSIra Weiny	"kunmap_atomic"				=> "kunmap_local",
8459189c7e7SJoe Perches);
8469189c7e7SJoe Perches
8479189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
8489189c7e7SJoe Perchesour $deprecated_apis_search = "";
8499189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
8509189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
8519189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
8529189c7e7SJoe Perches}
8539189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
8549189c7e7SJoe Perches
855b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
856b392c64fSJoe Perches	S_IWUGO		|
857b392c64fSJoe Perches	S_IWOTH		|
858b392c64fSJoe Perches	S_IRWXUGO	|
859b392c64fSJoe Perches	S_IALLUGO	|
860b392c64fSJoe Perches	0[0-7][0-7][2367]
861b392c64fSJoe Perches}x;
862b392c64fSJoe Perches
863f90774e1SJoe Perchesour %mode_permission_string_types = (
864f90774e1SJoe Perches	"S_IRWXU" => 0700,
865f90774e1SJoe Perches	"S_IRUSR" => 0400,
866f90774e1SJoe Perches	"S_IWUSR" => 0200,
867f90774e1SJoe Perches	"S_IXUSR" => 0100,
868f90774e1SJoe Perches	"S_IRWXG" => 0070,
869f90774e1SJoe Perches	"S_IRGRP" => 0040,
870f90774e1SJoe Perches	"S_IWGRP" => 0020,
871f90774e1SJoe Perches	"S_IXGRP" => 0010,
872f90774e1SJoe Perches	"S_IRWXO" => 0007,
873f90774e1SJoe Perches	"S_IROTH" => 0004,
874f90774e1SJoe Perches	"S_IWOTH" => 0002,
875f90774e1SJoe Perches	"S_IXOTH" => 0001,
876f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
877f90774e1SJoe Perches	"S_IRUGO" => 0444,
878f90774e1SJoe Perches	"S_IWUGO" => 0222,
879f90774e1SJoe Perches	"S_IXUGO" => 0111,
880f90774e1SJoe Perches);
881f90774e1SJoe Perches
882f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
883f90774e1SJoe Perchesour $mode_perms_string_search = "";
884f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
885f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
886f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
887f90774e1SJoe Perches}
88800180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
88900180468SJoe Perchesour $multi_mode_perms_string_search = qr{
89000180468SJoe Perches	${single_mode_perms_string_search}
89100180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
89200180468SJoe Perches}x;
89300180468SJoe Perches
89400180468SJoe Perchessub perms_to_octal {
89500180468SJoe Perches	my ($string) = @_;
89600180468SJoe Perches
89700180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
89800180468SJoe Perches
89900180468SJoe Perches	my $val = "";
90000180468SJoe Perches	my $oval = "";
90100180468SJoe Perches	my $to = 0;
90200180468SJoe Perches	my $curpos = 0;
90300180468SJoe Perches	my $lastpos = 0;
90400180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
90500180468SJoe Perches		$curpos = pos($string);
90600180468SJoe Perches		my $match = $2;
90700180468SJoe Perches		my $omatch = $1;
90800180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
90900180468SJoe Perches		$lastpos = $curpos;
91000180468SJoe Perches		$to |= $mode_permission_string_types{$match};
91100180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
91200180468SJoe Perches		$val .= $match;
91300180468SJoe Perches		$oval .= $omatch;
91400180468SJoe Perches	}
91500180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
91600180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
91700180468SJoe Perches	return sprintf("%04o", $to);
91800180468SJoe Perches}
919f90774e1SJoe Perches
9207840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
9217840a94cSWolfram Sang	irq|
922cdcee686SSergey Ryazanov	memory|
923cdcee686SSergey Ryazanov	time|
924cdcee686SSergey Ryazanov	reboot
9257840a94cSWolfram Sang)};
9267840a94cSWolfram Sang# memory.h: ARM has a custom one
9277840a94cSWolfram Sang
92866b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
92966b47b4aSKees Cookmy $misspellings;
93066b47b4aSKees Cookmy %spelling_fix;
93136061e38SJoe Perches
93236061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
93366b47b4aSKees Cook	while (<$spelling>) {
93466b47b4aSKees Cook		my $line = $_;
93566b47b4aSKees Cook
93666b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
93766b47b4aSKees Cook		$line =~ s/^\s*//g;
93866b47b4aSKees Cook
93966b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
94066b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
94166b47b4aSKees Cook
94266b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
94366b47b4aSKees Cook
94466b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
94566b47b4aSKees Cook	}
94666b47b4aSKees Cook	close($spelling);
94736061e38SJoe Perches} else {
94836061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
94936061e38SJoe Perches}
95066b47b4aSKees Cook
951ebfd7d62SJoe Perchesif ($codespell) {
952ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
953ebfd7d62SJoe Perches		while (<$spelling>) {
954ebfd7d62SJoe Perches			my $line = $_;
955ebfd7d62SJoe Perches
956ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
957ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
958ebfd7d62SJoe Perches
959ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
960ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
961ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
962ebfd7d62SJoe Perches
963ebfd7d62SJoe Perches			$line =~ s/,.*$//;
964ebfd7d62SJoe Perches
965ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
966ebfd7d62SJoe Perches
967ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
968ebfd7d62SJoe Perches		}
969ebfd7d62SJoe Perches		close($spelling);
970ebfd7d62SJoe Perches	} else {
971ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
972ebfd7d62SJoe Perches	}
973ebfd7d62SJoe Perches}
974ebfd7d62SJoe Perches
975ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
976ebfd7d62SJoe Perches
97775ad8c57SJerome Forissiersub read_words {
97875ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
97975ad8c57SJerome Forissier
98075ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
98175ad8c57SJerome Forissier		while (<$words>) {
982bf1fa1daSJoe Perches			my $line = $_;
983bf1fa1daSJoe Perches
984bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
985bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
986bf1fa1daSJoe Perches
987bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
988bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
989bf1fa1daSJoe Perches			if ($line =~ /\s/) {
99075ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
991bf1fa1daSJoe Perches				next;
992bf1fa1daSJoe Perches			}
993bf1fa1daSJoe Perches
994ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
99575ad8c57SJerome Forissier			$$wordsRef .= $line;
996bf1fa1daSJoe Perches		}
99775ad8c57SJerome Forissier		close($file);
99875ad8c57SJerome Forissier		return 1;
999bf1fa1daSJoe Perches	}
1000bf1fa1daSJoe Perches
100175ad8c57SJerome Forissier	return 0;
100275ad8c57SJerome Forissier}
100375ad8c57SJerome Forissier
1004ced69da1SQuentin Monnetmy $const_structs;
1005ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
100675ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
100775ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
1008ced69da1SQuentin Monnet}
100975ad8c57SJerome Forissier
1010ced69da1SQuentin Monnetif (defined($typedefsfile)) {
1011ced69da1SQuentin Monnet	my $typeOtherTypedefs;
101275ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
101375ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
1014ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
101575ad8c57SJerome Forissier}
101675ad8c57SJerome Forissier
10178905a67cSAndy Whitcroftsub build_types {
1018485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
1019485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
10201813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
10218716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
1022c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
1023ab7e23f3SJoe Perches	$BasicType	= qr{
1024ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
1025ab7e23f3SJoe Perches				(?:${all}\b)
1026ab7e23f3SJoe Perches		}x;
10278905a67cSAndy Whitcroft	$NonptrType	= qr{
1028d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
1029cf655043SAndy Whitcroft			(?:
10306b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
10318ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
1032c45dcabdSAndy Whitcroft				(?:${all}\b)
1033cf655043SAndy Whitcroft			)
1034c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
10358905a67cSAndy Whitcroft		  }x;
10361813087dSJoe Perches	$NonptrTypeMisordered	= qr{
10371813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
10381813087dSJoe Perches			(?:
10391813087dSJoe Perches				(?:${Misordered}\b)
10401813087dSJoe Perches			)
10411813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
10421813087dSJoe Perches		  }x;
10438716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
10448716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
10458716de38SJoe Perches			(?:
10468716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
10478716de38SJoe Perches				(?:$typeTypedefs\b)|
10488716de38SJoe Perches				(?:${allWithAttr}\b)
10498716de38SJoe Perches			)
10508716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
10518716de38SJoe Perches		  }x;
10528905a67cSAndy Whitcroft	$Type	= qr{
1053c45dcabdSAndy Whitcroft			$NonptrType
10547b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
1055c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
10568905a67cSAndy Whitcroft		  }x;
10571813087dSJoe Perches	$TypeMisordered	= qr{
10581813087dSJoe Perches			$NonptrTypeMisordered
10597b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
10601813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
10611813087dSJoe Perches		  }x;
106291cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
10631813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
10648905a67cSAndy Whitcroft}
10658905a67cSAndy Whitcroftbuild_types();
10666c72ffaaSAndy Whitcroft
10677d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
1068d1fe9c09SJoe Perches
1069d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1070d1fe9c09SJoe Perches# requires at least perl version v5.10.0
1071d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
1072d1fe9c09SJoe Perches
1073d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
10742435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
1075c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
10767d2367afSJoe Perches
1077f8422308SJoe Perchesour $declaration_macros = qr{(?x:
10783e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
1079fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
1080dcea7964SJoe Perches	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(|
1081dcea7964SJoe Perches	(?:$Storage\s+)?(?:XA_STATE|XA_STATE_ORDER)\s*\(
1082f8422308SJoe Perches)};
1083f8422308SJoe Perches
10848d0325ccSAditya Srivastavaour %allow_repeated_words = (
10858d0325ccSAditya Srivastava	add => '',
10868d0325ccSAditya Srivastava	added => '',
10878d0325ccSAditya Srivastava	bad => '',
10888d0325ccSAditya Srivastava	be => '',
10898d0325ccSAditya Srivastava);
10908d0325ccSAditya Srivastava
10917d2367afSJoe Perchessub deparenthesize {
10927d2367afSJoe Perches	my ($string) = @_;
10937d2367afSJoe Perches	return "" if (!defined($string));
10945b9553abSJoe Perches
10955b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
10965b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
10975b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
10985b9553abSJoe Perches	}
10995b9553abSJoe Perches
11007d2367afSJoe Perches	$string =~ s@\s+@ @g;
11015b9553abSJoe Perches
11027d2367afSJoe Perches	return $string;
11037d2367afSJoe Perches}
11047d2367afSJoe Perches
11053445686aSJoe Perchessub seed_camelcase_file {
11063445686aSJoe Perches	my ($file) = @_;
11073445686aSJoe Perches
11083445686aSJoe Perches	return if (!(-f $file));
11093445686aSJoe Perches
11103445686aSJoe Perches	local $/;
11113445686aSJoe Perches
11123445686aSJoe Perches	open(my $include_file, '<', "$file")
11133445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
11143445686aSJoe Perches	my $text = <$include_file>;
11153445686aSJoe Perches	close($include_file);
11163445686aSJoe Perches
11173445686aSJoe Perches	my @lines = split('\n', $text);
11183445686aSJoe Perches
11193445686aSJoe Perches	foreach my $line (@lines) {
11203445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
11213445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
11223445686aSJoe Perches			$camelcase{$1} = 1;
112311ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
112411ea516aSJoe Perches			$camelcase{$1} = 1;
112511ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
11263445686aSJoe Perches			$camelcase{$1} = 1;
11273445686aSJoe Perches		}
11283445686aSJoe Perches	}
11293445686aSJoe Perches}
11303445686aSJoe Perches
1131cd28b119SJoe Perchesour %maintained_status = ();
1132cd28b119SJoe Perches
113385b0ee18SJoe Perchessub is_maintained_obsolete {
113485b0ee18SJoe Perches	my ($filename) = @_;
113585b0ee18SJoe Perches
1136f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
113785b0ee18SJoe Perches
1138cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
1139cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1140cd28b119SJoe Perches	}
114185b0ee18SJoe Perches
1142cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
114385b0ee18SJoe Perches}
114485b0ee18SJoe Perches
11453b6e8ac9SJoe Perchessub is_SPDX_License_valid {
11463b6e8ac9SJoe Perches	my ($license) = @_;
11473b6e8ac9SJoe Perches
1148f9363b31SGuenter Roeck	return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
11493b6e8ac9SJoe Perches
115056294112SJoe Perches	my $root_path = abs_path($root);
1151f9363b31SGuenter Roeck	my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
11523b6e8ac9SJoe Perches	return 0 if ($status ne "");
11533b6e8ac9SJoe Perches	return 1;
11543b6e8ac9SJoe Perches}
11553b6e8ac9SJoe Perches
11563445686aSJoe Perchesmy $camelcase_seeded = 0;
11573445686aSJoe Perchessub seed_camelcase_includes {
11583445686aSJoe Perches	return if ($camelcase_seeded);
11593445686aSJoe Perches
11603445686aSJoe Perches	my $files;
1161c707a81dSJoe Perches	my $camelcase_cache = "";
1162c707a81dSJoe Perches	my @include_files = ();
1163c707a81dSJoe Perches
1164c707a81dSJoe Perches	$camelcase_seeded = 1;
1165351b2a1fSJoe Perches
11660f7f635bSJoe Perches	if (-e "$gitroot") {
1167dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
1168351b2a1fSJoe Perches		chomp $git_last_include_commit;
1169c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1170c707a81dSJoe Perches	} else {
1171c707a81dSJoe Perches		my $last_mod_date = 0;
1172c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
1173c707a81dSJoe Perches		@include_files = split('\n', $files);
1174c707a81dSJoe Perches		foreach my $file (@include_files) {
1175c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
1176c707a81dSJoe Perches						   localtime((stat $file)[9]));
1177c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
1178c707a81dSJoe Perches		}
1179c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1180c707a81dSJoe Perches	}
1181c707a81dSJoe Perches
1182c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
1183c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
1184c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
1185351b2a1fSJoe Perches		while (<$camelcase_file>) {
1186351b2a1fSJoe Perches			chomp;
1187351b2a1fSJoe Perches			$camelcase{$_} = 1;
1188351b2a1fSJoe Perches		}
1189351b2a1fSJoe Perches		close($camelcase_file);
1190351b2a1fSJoe Perches
1191351b2a1fSJoe Perches		return;
1192351b2a1fSJoe Perches	}
1193c707a81dSJoe Perches
11940f7f635bSJoe Perches	if (-e "$gitroot") {
1195dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
1196c707a81dSJoe Perches		@include_files = split('\n', $files);
11973445686aSJoe Perches	}
1198c707a81dSJoe Perches
11993445686aSJoe Perches	foreach my $file (@include_files) {
12003445686aSJoe Perches		seed_camelcase_file($file);
12013445686aSJoe Perches	}
1202351b2a1fSJoe Perches
1203c707a81dSJoe Perches	if ($camelcase_cache ne "") {
1204351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
1205c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
1206c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
1207351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1208351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
1209351b2a1fSJoe Perches		}
1210351b2a1fSJoe Perches		close($camelcase_file);
1211351b2a1fSJoe Perches	}
12123445686aSJoe Perches}
12133445686aSJoe Perches
1214f5f61325SJoe Perchessub git_is_single_file {
1215f5f61325SJoe Perches	my ($filename) = @_;
1216f5f61325SJoe Perches
1217f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1218f5f61325SJoe Perches
1219f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1220f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
1221f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
1222f5f61325SJoe Perches}
1223f5f61325SJoe Perches
1224d311cd44SJoe Perchessub git_commit_info {
1225d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
1226d311cd44SJoe Perches
12270f7f635bSJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1228d311cd44SJoe Perches
1229dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1230d311cd44SJoe Perches	$output =~ s/^\s*//gm;
1231d311cd44SJoe Perches	my @lines = split("\n", $output);
1232d311cd44SJoe Perches
12330d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
12340d7835fcSJoe Perches
12355a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1236d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1237d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1238d311cd44SJoe Perches#
1239d311cd44SJoe Perches#		echo "checking commits $1..."
1240d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1241d311cd44SJoe Perches#		while read line ; do
1242d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1243d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1244d311cd44SJoe Perches#		done
12454ce9f970SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
12464ce9f970SJoe Perches		 $lines[0] =~ /^fatal: bad object $commit/) {
1247948b133aSHeinrich Schuchardt		$id = undef;
1248d311cd44SJoe Perches	} else {
1249d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1250d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1251d311cd44SJoe Perches	}
1252d311cd44SJoe Perches
1253d311cd44SJoe Perches	return ($id, $desc);
1254d311cd44SJoe Perches}
1255d311cd44SJoe Perches
12566c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
12570a920b5bSAndy Whitcroft
125800df344fSAndy Whitcroftmy @rawlines = ();
1259c2fdda0dSAndy Whitcroftmy @lines = ();
12603705ce5bSJoe Perchesmy @fixed = ();
1261d752fcc8SJoe Perchesmy @fixed_inserted = ();
1262d752fcc8SJoe Perchesmy @fixed_deleted = ();
1263194f66fcSJoe Perchesmy $fixlinenr = -1;
1264194f66fcSJoe Perches
12654a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
12664a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
12670f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
12684a593c34SDu, Changbin
12694a593c34SDu, Changbinif ($git) {
12704a593c34SDu, Changbin	my @commits = ();
12710dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
12724a593c34SDu, Changbin		my $git_range;
127328898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
127428898fd1SJoe Perches			$git_range = "-$2 $1";
12754a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
12764a593c34SDu, Changbin			$git_range = "$commit_expr";
12774a593c34SDu, Changbin		} else {
12780dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
12790dea9f1eSJoe Perches		}
1280dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
12810dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
128228898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
128328898fd1SJoe Perches			next if (!defined($1) || !defined($2));
12840dea9f1eSJoe Perches			my $sha1 = $1;
12850dea9f1eSJoe Perches			my $subject = $2;
12860dea9f1eSJoe Perches			unshift(@commits, $sha1);
12870dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
12884a593c34SDu, Changbin		}
12894a593c34SDu, Changbin	}
12904a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
12914a593c34SDu, Changbin	@ARGV = @commits;
12924a593c34SDu, Changbin}
12934a593c34SDu, Changbin
1294c2fdda0dSAndy Whitcroftmy $vname;
129598005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
12966c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
129721caa13cSAndy Whitcroft	my $FILE;
1298f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1299f5f61325SJoe Perches	my $oldfile = $file;
1300f5f61325SJoe Perches	$file = 1 if ($is_git_file);
13014a593c34SDu, Changbin	if ($git) {
13024a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
13034a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
13044a593c34SDu, Changbin	} elsif ($file) {
130521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
13066c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
130721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
130821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
13096c72ffaaSAndy Whitcroft	} else {
131021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
13116c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
13126c72ffaaSAndy Whitcroft	}
1313c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1314c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
13154a593c34SDu, Changbin	} elsif ($git) {
13160dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1317c2fdda0dSAndy Whitcroft	} else {
1318c2fdda0dSAndy Whitcroft		$vname = $filename;
1319c2fdda0dSAndy Whitcroft	}
132021caa13cSAndy Whitcroft	while (<$FILE>) {
13210a920b5bSAndy Whitcroft		chomp;
132200df344fSAndy Whitcroft		push(@rawlines, $_);
1323c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
13246c72ffaaSAndy Whitcroft	}
132521caa13cSAndy Whitcroft	close($FILE);
1326d8469f16SJoe Perches
1327d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1328d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1329d8469f16SJoe Perches		print "$vname\n";
1330d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1331d8469f16SJoe Perches	}
1332d8469f16SJoe Perches
1333c2fdda0dSAndy Whitcroft	if (!process($filename)) {
13340a920b5bSAndy Whitcroft		$exit = 1;
13350a920b5bSAndy Whitcroft	}
133600df344fSAndy Whitcroft	@rawlines = ();
133713214adfSAndy Whitcroft	@lines = ();
13383705ce5bSJoe Perches	@fixed = ();
1339d752fcc8SJoe Perches	@fixed_inserted = ();
1340d752fcc8SJoe Perches	@fixed_deleted = ();
1341194f66fcSJoe Perches	$fixlinenr = -1;
1342485ff23eSAlex Dowad	@modifierListFile = ();
1343485ff23eSAlex Dowad	@typeListFile = ();
1344485ff23eSAlex Dowad	build_types();
1345f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
13460a920b5bSAndy Whitcroft}
13470a920b5bSAndy Whitcroft
1348d8469f16SJoe Perchesif (!$quiet) {
13493c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
13503c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
13513c816e49SJoe Perches
13525b57980dSJoe Perches	if (!$perl_version_ok) {
1353d8469f16SJoe Perches		print << "EOM"
1354d8469f16SJoe Perches
1355d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
13565b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1357d8469f16SJoe PerchesEOM
1358d8469f16SJoe Perches	}
1359d8469f16SJoe Perches	if ($exit) {
1360d8469f16SJoe Perches		print << "EOM"
1361d8469f16SJoe Perches
1362d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1363d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1364d8469f16SJoe PerchesEOM
1365d8469f16SJoe Perches	}
1366d8469f16SJoe Perches}
1367d8469f16SJoe Perches
13680a920b5bSAndy Whitcroftexit($exit);
13690a920b5bSAndy Whitcroft
13700a920b5bSAndy Whitcroftsub top_of_kernel_tree {
13716c72ffaaSAndy Whitcroft	my ($root) = @_;
13726c72ffaaSAndy Whitcroft
13736c72ffaaSAndy Whitcroft	my @tree_check = (
13746c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
13756c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
13766c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
13776c72ffaaSAndy Whitcroft	);
13786c72ffaaSAndy Whitcroft
13796c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
13806c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
13810a920b5bSAndy Whitcroft			return 0;
13820a920b5bSAndy Whitcroft		}
13836c72ffaaSAndy Whitcroft	}
13846c72ffaaSAndy Whitcroft	return 1;
13856c72ffaaSAndy Whitcroft}
13860a920b5bSAndy Whitcroft
138720112475SJoe Perchessub parse_email {
138820112475SJoe Perches	my ($formatted_email) = @_;
138920112475SJoe Perches
139020112475SJoe Perches	my $name = "";
1391fccaebf0SDwaipayan Ray	my $quoted = "";
1392dfa05c28SJoe Perches	my $name_comment = "";
139320112475SJoe Perches	my $address = "";
139420112475SJoe Perches	my $comment = "";
139520112475SJoe Perches
139620112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
139720112475SJoe Perches		$name = $1;
139820112475SJoe Perches		$address = $2;
139920112475SJoe Perches		$comment = $3 if defined $3;
140020112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
140120112475SJoe Perches		$address = $1;
140220112475SJoe Perches		$comment = $2 if defined $2;
140320112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
140420112475SJoe Perches		$address = $1;
140520112475SJoe Perches		$comment = $2 if defined $2;
140685e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
140720112475SJoe Perches		$name = $formatted_email;
14083705ce5bSJoe Perches		$name = trim($name);
140920112475SJoe Perches		$name =~ s/^\"|\"$//g;
141020112475SJoe Perches		# If there's a name left after stripping spaces and
141120112475SJoe Perches		# leading quotes, and the address doesn't have both
141220112475SJoe Perches		# leading and trailing angle brackets, the address
141320112475SJoe Perches		# is invalid. ie:
141420112475SJoe Perches		#   "joe smith [email protected]" bad
141520112475SJoe Perches		#   "joe smith <[email protected]" bad
141620112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
141720112475SJoe Perches			$name = "";
141820112475SJoe Perches			$address = "";
141920112475SJoe Perches			$comment = "";
142020112475SJoe Perches		}
142120112475SJoe Perches	}
142220112475SJoe Perches
1423fccaebf0SDwaipayan Ray	# Extract comments from names excluding quoted parts
1424fccaebf0SDwaipayan Ray	# "John D. (Doe)" - Do not extract
1425fccaebf0SDwaipayan Ray	if ($name =~ s/\"(.+)\"//) {
1426fccaebf0SDwaipayan Ray		$quoted = $1;
1427dfa05c28SJoe Perches	}
1428fccaebf0SDwaipayan Ray	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1429fccaebf0SDwaipayan Ray		$name_comment .= trim($1);
1430fccaebf0SDwaipayan Ray	}
1431fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
1432fccaebf0SDwaipayan Ray	$name = trim("$quoted $name");
1433fccaebf0SDwaipayan Ray
14343705ce5bSJoe Perches	$address = trim($address);
143520112475SJoe Perches	$address =~ s/^\<|\>$//g;
1436fccaebf0SDwaipayan Ray	$comment = trim($comment);
143720112475SJoe Perches
143820112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
143920112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
144020112475SJoe Perches		$name = "\"$name\"";
144120112475SJoe Perches	}
144220112475SJoe Perches
1443dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
144420112475SJoe Perches}
144520112475SJoe Perches
144620112475SJoe Perchessub format_email {
144748ca2d8aSDwaipayan Ray	my ($name, $name_comment, $address, $comment) = @_;
144820112475SJoe Perches
144920112475SJoe Perches	my $formatted_email;
145020112475SJoe Perches
1451fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
14523705ce5bSJoe Perches	$address = trim($address);
1453fccaebf0SDwaipayan Ray	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
145420112475SJoe Perches
145520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
145620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
145720112475SJoe Perches		$name = "\"$name\"";
145820112475SJoe Perches	}
145920112475SJoe Perches
1460fccaebf0SDwaipayan Ray	$name_comment = trim($name_comment);
1461fccaebf0SDwaipayan Ray	$name_comment = " $name_comment" if ($name_comment ne "");
1462fccaebf0SDwaipayan Ray	$comment = trim($comment);
1463fccaebf0SDwaipayan Ray	$comment = " $comment" if ($comment ne "");
1464fccaebf0SDwaipayan Ray
146520112475SJoe Perches	if ("$name" eq "") {
146620112475SJoe Perches		$formatted_email = "$address";
146720112475SJoe Perches	} else {
146848ca2d8aSDwaipayan Ray		$formatted_email = "$name$name_comment <$address>";
146920112475SJoe Perches	}
147048ca2d8aSDwaipayan Ray	$formatted_email .= "$comment";
147120112475SJoe Perches	return $formatted_email;
147220112475SJoe Perches}
147320112475SJoe Perches
1474dfa05c28SJoe Perchessub reformat_email {
1475dfa05c28SJoe Perches	my ($email) = @_;
1476dfa05c28SJoe Perches
1477dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
147848ca2d8aSDwaipayan Ray	return format_email($email_name, $name_comment, $email_address, $comment);
1479dfa05c28SJoe Perches}
1480dfa05c28SJoe Perches
1481dfa05c28SJoe Perchessub same_email_addresses {
1482fccaebf0SDwaipayan Ray	my ($email1, $email2) = @_;
1483dfa05c28SJoe Perches
1484dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1485dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1486dfa05c28SJoe Perches
148748ca2d8aSDwaipayan Ray	return $email1_name eq $email2_name &&
148848ca2d8aSDwaipayan Ray	       $email1_address eq $email2_address &&
148948ca2d8aSDwaipayan Ray	       $name1_comment eq $name2_comment &&
149048ca2d8aSDwaipayan Ray	       $comment1 eq $comment2;
149148ca2d8aSDwaipayan Ray}
1492dfa05c28SJoe Perches
1493d311cd44SJoe Perchessub which {
1494d311cd44SJoe Perches	my ($bin) = @_;
1495d311cd44SJoe Perches
1496d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1497d311cd44SJoe Perches		if (-e "$path/$bin") {
1498d311cd44SJoe Perches			return "$path/$bin";
1499d311cd44SJoe Perches		}
1500d311cd44SJoe Perches	}
1501d311cd44SJoe Perches
1502d311cd44SJoe Perches	return "";
1503d311cd44SJoe Perches}
1504d311cd44SJoe Perches
1505000d1cc1SJoe Perchessub which_conf {
1506000d1cc1SJoe Perches	my ($conf) = @_;
1507000d1cc1SJoe Perches
1508000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1509000d1cc1SJoe Perches		if (-e "$path/$conf") {
1510000d1cc1SJoe Perches			return "$path/$conf";
1511000d1cc1SJoe Perches		}
1512000d1cc1SJoe Perches	}
1513000d1cc1SJoe Perches
1514000d1cc1SJoe Perches	return "";
1515000d1cc1SJoe Perches}
1516000d1cc1SJoe Perches
15170a920b5bSAndy Whitcroftsub expand_tabs {
15180a920b5bSAndy Whitcroft	my ($str) = @_;
15190a920b5bSAndy Whitcroft
15200a920b5bSAndy Whitcroft	my $res = '';
15210a920b5bSAndy Whitcroft	my $n = 0;
15220a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
15230a920b5bSAndy Whitcroft		if ($c eq "\t") {
15240a920b5bSAndy Whitcroft			$res .= ' ';
15250a920b5bSAndy Whitcroft			$n++;
1526713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
15270a920b5bSAndy Whitcroft				$res .= ' ';
15280a920b5bSAndy Whitcroft			}
15290a920b5bSAndy Whitcroft			next;
15300a920b5bSAndy Whitcroft		}
15310a920b5bSAndy Whitcroft		$res .= $c;
15320a920b5bSAndy Whitcroft		$n++;
15330a920b5bSAndy Whitcroft	}
15340a920b5bSAndy Whitcroft
15350a920b5bSAndy Whitcroft	return $res;
15360a920b5bSAndy Whitcroft}
15376c72ffaaSAndy Whitcroftsub copy_spacing {
1538773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
15396c72ffaaSAndy Whitcroft	return $res;
15406c72ffaaSAndy Whitcroft}
15410a920b5bSAndy Whitcroft
15424a0df2efSAndy Whitcroftsub line_stats {
15434a0df2efSAndy Whitcroft	my ($line) = @_;
15444a0df2efSAndy Whitcroft
15454a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
15464a0df2efSAndy Whitcroft	$line =~ s/^.//;
15474a0df2efSAndy Whitcroft	$line = expand_tabs($line);
15484a0df2efSAndy Whitcroft
15494a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
15504a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
15514a0df2efSAndy Whitcroft
15524a0df2efSAndy Whitcroft	return (length($line), length($white));
15534a0df2efSAndy Whitcroft}
15544a0df2efSAndy Whitcroft
1555773647a0SAndy Whitcroftmy $sanitise_quote = '';
1556773647a0SAndy Whitcroft
1557773647a0SAndy Whitcroftsub sanitise_line_reset {
1558773647a0SAndy Whitcroft	my ($in_comment) = @_;
1559773647a0SAndy Whitcroft
1560773647a0SAndy Whitcroft	if ($in_comment) {
1561773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1562773647a0SAndy Whitcroft	} else {
1563773647a0SAndy Whitcroft		$sanitise_quote = '';
1564773647a0SAndy Whitcroft	}
1565773647a0SAndy Whitcroft}
156600df344fSAndy Whitcroftsub sanitise_line {
156700df344fSAndy Whitcroft	my ($line) = @_;
156800df344fSAndy Whitcroft
156900df344fSAndy Whitcroft	my $res = '';
157000df344fSAndy Whitcroft	my $l = '';
157100df344fSAndy Whitcroft
1572c2fdda0dSAndy Whitcroft	my $qlen = 0;
1573773647a0SAndy Whitcroft	my $off = 0;
1574773647a0SAndy Whitcroft	my $c;
157500df344fSAndy Whitcroft
1576773647a0SAndy Whitcroft	# Always copy over the diff marker.
1577773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1578773647a0SAndy Whitcroft
1579773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1580773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1581773647a0SAndy Whitcroft
15828d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1583773647a0SAndy Whitcroft		# and end, all to $;.
1584773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1585773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1586773647a0SAndy Whitcroft
1587773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1588773647a0SAndy Whitcroft			$off++;
158900df344fSAndy Whitcroft			next;
1590773647a0SAndy Whitcroft		}
159181bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1592773647a0SAndy Whitcroft			$sanitise_quote = '';
1593773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1594773647a0SAndy Whitcroft			$off++;
1595773647a0SAndy Whitcroft			next;
1596773647a0SAndy Whitcroft		}
1597113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1598113f04a8SDaniel Walker			$sanitise_quote = '//';
1599113f04a8SDaniel Walker
1600113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1601113f04a8SDaniel Walker			$off++;
1602113f04a8SDaniel Walker			next;
1603113f04a8SDaniel Walker		}
1604773647a0SAndy Whitcroft
1605773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1606773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1607773647a0SAndy Whitcroft		    $c eq "\\") {
1608773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1609773647a0SAndy Whitcroft			$off++;
1610773647a0SAndy Whitcroft			next;
1611773647a0SAndy Whitcroft		}
1612773647a0SAndy Whitcroft		# Regular quotes.
1613773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1614773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1615773647a0SAndy Whitcroft				$sanitise_quote = $c;
1616773647a0SAndy Whitcroft
1617773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1618773647a0SAndy Whitcroft				next;
1619773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1620773647a0SAndy Whitcroft				$sanitise_quote = '';
162100df344fSAndy Whitcroft			}
162200df344fSAndy Whitcroft		}
1623773647a0SAndy Whitcroft
1624fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1625773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1626773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1627113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1628113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1629773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1630773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
163100df344fSAndy Whitcroft		} else {
1632773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
163300df344fSAndy Whitcroft		}
1634c2fdda0dSAndy Whitcroft	}
1635c2fdda0dSAndy Whitcroft
1636113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1637113f04a8SDaniel Walker		$sanitise_quote = '';
1638113f04a8SDaniel Walker	}
1639113f04a8SDaniel Walker
1640c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1641c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1642c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1643c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1644c2fdda0dSAndy Whitcroft
1645c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1646c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1647c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1648c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1649c2fdda0dSAndy Whitcroft	}
1650c2fdda0dSAndy Whitcroft
1651dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1652dadf680dSJoe Perches		my $match = $1;
1653dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1654dadf680dSJoe Perches	}
1655dadf680dSJoe Perches
165600df344fSAndy Whitcroft	return $res;
165700df344fSAndy Whitcroft}
165800df344fSAndy Whitcroft
1659a6962d72SJoe Perchessub get_quoted_string {
1660a6962d72SJoe Perches	my ($line, $rawline) = @_;
1661a6962d72SJoe Perches
1662478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
166333acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1664a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1665a6962d72SJoe Perches}
1666a6962d72SJoe Perches
16678905a67cSAndy Whitcroftsub ctx_statement_block {
16688905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16698905a67cSAndy Whitcroft	my $line = $linenr - 1;
16708905a67cSAndy Whitcroft	my $blk = '';
16718905a67cSAndy Whitcroft	my $soff = $off;
16728905a67cSAndy Whitcroft	my $coff = $off - 1;
1673773647a0SAndy Whitcroft	my $coff_set = 0;
16748905a67cSAndy Whitcroft
167513214adfSAndy Whitcroft	my $loff = 0;
167613214adfSAndy Whitcroft
16778905a67cSAndy Whitcroft	my $type = '';
16788905a67cSAndy Whitcroft	my $level = 0;
1679a2750645SAndy Whitcroft	my @stack = ();
1680cf655043SAndy Whitcroft	my $p;
16818905a67cSAndy Whitcroft	my $c;
16828905a67cSAndy Whitcroft	my $len = 0;
168313214adfSAndy Whitcroft
168413214adfSAndy Whitcroft	my $remainder;
16858905a67cSAndy Whitcroft	while (1) {
1686a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1687a2750645SAndy Whitcroft
1688773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
16898905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
16908905a67cSAndy Whitcroft		# context.
16918905a67cSAndy Whitcroft		if ($off >= $len) {
16928905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1693dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1694c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
16958905a67cSAndy Whitcroft				$remain--;
169613214adfSAndy Whitcroft				$loff = $len;
1697c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
16988905a67cSAndy Whitcroft				$len = length($blk);
16998905a67cSAndy Whitcroft				$line++;
17008905a67cSAndy Whitcroft				last;
17018905a67cSAndy Whitcroft			}
17028905a67cSAndy Whitcroft			# Bail if there is no further context.
17038905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
170413214adfSAndy Whitcroft			if ($off >= $len) {
17058905a67cSAndy Whitcroft				last;
17068905a67cSAndy Whitcroft			}
1707f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1708f74bd194SAndy Whitcroft				$level++;
1709f74bd194SAndy Whitcroft				$type = '#';
1710f74bd194SAndy Whitcroft			}
17118905a67cSAndy Whitcroft		}
1712cf655043SAndy Whitcroft		$p = $c;
17138905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
171413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
17158905a67cSAndy Whitcroft
1716773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
17174635f4fbSAndy Whitcroft
17184635f4fbSAndy Whitcroft		# Handle nested #if/#else.
17194635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
17204635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
17214635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
17224635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
17234635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
17244635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
17254635f4fbSAndy Whitcroft		}
17264635f4fbSAndy Whitcroft
17278905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
17288905a67cSAndy Whitcroft		# outermost level.
17298905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
17308905a67cSAndy Whitcroft			last;
17318905a67cSAndy Whitcroft		}
17328905a67cSAndy Whitcroft
173313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1734773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1735773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1736773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1737773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1738773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1739773647a0SAndy Whitcroft			$coff_set = 1;
1740773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1741773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
174213214adfSAndy Whitcroft		}
174313214adfSAndy Whitcroft
17448905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
17458905a67cSAndy Whitcroft			$level++;
17468905a67cSAndy Whitcroft			$type = '(';
17478905a67cSAndy Whitcroft		}
17488905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
17498905a67cSAndy Whitcroft			$level--;
17508905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
17518905a67cSAndy Whitcroft
17528905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
17538905a67cSAndy Whitcroft				$coff = $off;
1754773647a0SAndy Whitcroft				$coff_set = 1;
1755773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
17568905a67cSAndy Whitcroft			}
17578905a67cSAndy Whitcroft		}
17588905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
17598905a67cSAndy Whitcroft			$level++;
17608905a67cSAndy Whitcroft			$type = '{';
17618905a67cSAndy Whitcroft		}
17628905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
17638905a67cSAndy Whitcroft			$level--;
17648905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
17658905a67cSAndy Whitcroft
17668905a67cSAndy Whitcroft			if ($level == 0) {
1767b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1768b998e001SPatrick Pannuto					$off++;
1769b998e001SPatrick Pannuto				}
17708905a67cSAndy Whitcroft				last;
17718905a67cSAndy Whitcroft			}
17728905a67cSAndy Whitcroft		}
1773f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1774f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1775f74bd194SAndy Whitcroft			$level--;
1776f74bd194SAndy Whitcroft			$type = '';
1777f74bd194SAndy Whitcroft			$off++;
1778f74bd194SAndy Whitcroft			last;
1779f74bd194SAndy Whitcroft		}
17808905a67cSAndy Whitcroft		$off++;
17818905a67cSAndy Whitcroft	}
1782a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
178313214adfSAndy Whitcroft	if ($off == $len) {
1784a3bb97a7SAndy Whitcroft		$loff = $len + 1;
178513214adfSAndy Whitcroft		$line++;
178613214adfSAndy Whitcroft		$remain--;
178713214adfSAndy Whitcroft	}
17888905a67cSAndy Whitcroft
17898905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
17908905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
17918905a67cSAndy Whitcroft
17928905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
17938905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
17948905a67cSAndy Whitcroft
1795773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
179613214adfSAndy Whitcroft
179713214adfSAndy Whitcroft	return ($statement, $condition,
179813214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
179913214adfSAndy Whitcroft}
180013214adfSAndy Whitcroft
1801cf655043SAndy Whitcroftsub statement_lines {
1802cf655043SAndy Whitcroft	my ($stmt) = @_;
1803cf655043SAndy Whitcroft
1804cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1805cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1806cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1807cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1808cf655043SAndy Whitcroft
1809cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1810cf655043SAndy Whitcroft
1811cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1812cf655043SAndy Whitcroft}
1813cf655043SAndy Whitcroft
1814cf655043SAndy Whitcroftsub statement_rawlines {
1815cf655043SAndy Whitcroft	my ($stmt) = @_;
1816cf655043SAndy Whitcroft
1817cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1818cf655043SAndy Whitcroft
1819cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1820cf655043SAndy Whitcroft}
1821cf655043SAndy Whitcroft
1822cf655043SAndy Whitcroftsub statement_block_size {
1823cf655043SAndy Whitcroft	my ($stmt) = @_;
1824cf655043SAndy Whitcroft
1825cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1826cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1827cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1828cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1829cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1830cf655043SAndy Whitcroft
1831cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1832cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1833cf655043SAndy Whitcroft
1834cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1835cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1836cf655043SAndy Whitcroft
1837cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1838cf655043SAndy Whitcroft		return $stmt_lines;
1839cf655043SAndy Whitcroft	} else {
1840cf655043SAndy Whitcroft		return $stmt_statements;
1841cf655043SAndy Whitcroft	}
1842cf655043SAndy Whitcroft}
1843cf655043SAndy Whitcroft
184413214adfSAndy Whitcroftsub ctx_statement_full {
184513214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
184613214adfSAndy Whitcroft	my ($statement, $condition, $level);
184713214adfSAndy Whitcroft
184813214adfSAndy Whitcroft	my (@chunks);
184913214adfSAndy Whitcroft
1850cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
185113214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
185213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1853773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
185413214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1855cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1856cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1857cf655043SAndy Whitcroft	}
1858cf655043SAndy Whitcroft
1859cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1860cf655043SAndy Whitcroft	# could continue the statement.
1861cf655043SAndy Whitcroft	for (;;) {
186213214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
186313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1864cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1865773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1866cf655043SAndy Whitcroft		#print "C: push\n";
1867cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
186813214adfSAndy Whitcroft	}
186913214adfSAndy Whitcroft
187013214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
18718905a67cSAndy Whitcroft}
18728905a67cSAndy Whitcroft
18734a0df2efSAndy Whitcroftsub ctx_block_get {
1874f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
18754a0df2efSAndy Whitcroft	my $line;
18764a0df2efSAndy Whitcroft	my $start = $linenr - 1;
18774a0df2efSAndy Whitcroft	my $blk = '';
18784a0df2efSAndy Whitcroft	my @o;
18794a0df2efSAndy Whitcroft	my @c;
18804a0df2efSAndy Whitcroft	my @res = ();
18814a0df2efSAndy Whitcroft
1882f0a594c1SAndy Whitcroft	my $level = 0;
18834635f4fbSAndy Whitcroft	my @stack = ($level);
188400df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
188500df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
188600df344fSAndy Whitcroft		$remain--;
188700df344fSAndy Whitcroft
188800df344fSAndy Whitcroft		$blk .= $rawlines[$line];
18894635f4fbSAndy Whitcroft
18904635f4fbSAndy Whitcroft		# Handle nested #if/#else.
189101464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
18924635f4fbSAndy Whitcroft			push(@stack, $level);
189301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
18944635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
189501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
18964635f4fbSAndy Whitcroft			$level = pop(@stack);
18974635f4fbSAndy Whitcroft		}
18984635f4fbSAndy Whitcroft
189901464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1900f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1901f0a594c1SAndy Whitcroft			if ($off > 0) {
1902f0a594c1SAndy Whitcroft				$off--;
1903f0a594c1SAndy Whitcroft				next;
1904f0a594c1SAndy Whitcroft			}
19054a0df2efSAndy Whitcroft
1906f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1907f0a594c1SAndy Whitcroft				$level--;
1908f0a594c1SAndy Whitcroft				last if ($level == 0);
1909f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1910f0a594c1SAndy Whitcroft				$level++;
1911f0a594c1SAndy Whitcroft			}
1912f0a594c1SAndy Whitcroft		}
19134a0df2efSAndy Whitcroft
1914f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
191500df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
19164a0df2efSAndy Whitcroft		}
19174a0df2efSAndy Whitcroft
1918f0a594c1SAndy Whitcroft		last if ($level == 0);
19194a0df2efSAndy Whitcroft	}
19204a0df2efSAndy Whitcroft
1921f0a594c1SAndy Whitcroft	return ($level, @res);
19224a0df2efSAndy Whitcroft}
19234a0df2efSAndy Whitcroftsub ctx_block_outer {
19244a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
19254a0df2efSAndy Whitcroft
1926f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1927f0a594c1SAndy Whitcroft	return @r;
19284a0df2efSAndy Whitcroft}
19294a0df2efSAndy Whitcroftsub ctx_block {
19304a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
19314a0df2efSAndy Whitcroft
1932f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1933f0a594c1SAndy Whitcroft	return @r;
1934653d4876SAndy Whitcroft}
1935653d4876SAndy Whitcroftsub ctx_statement {
1936f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1937f0a594c1SAndy Whitcroft
1938f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1939f0a594c1SAndy Whitcroft	return @r;
1940f0a594c1SAndy Whitcroft}
1941f0a594c1SAndy Whitcroftsub ctx_block_level {
1942653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1943653d4876SAndy Whitcroft
1944f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
19454a0df2efSAndy Whitcroft}
19469c0ca6f9SAndy Whitcroftsub ctx_statement_level {
19479c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
19489c0ca6f9SAndy Whitcroft
19499c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
19509c0ca6f9SAndy Whitcroft}
19514a0df2efSAndy Whitcroft
19524a0df2efSAndy Whitcroftsub ctx_locate_comment {
19534a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
19544a0df2efSAndy Whitcroft
1955a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1956a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1957a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1958a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1959a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1960a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1961a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1962a55ee0ccSJoe Perches
19634a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1964a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
19654a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
19664a0df2efSAndy Whitcroft
19674a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
19684a0df2efSAndy Whitcroft	# comment.
19694a0df2efSAndy Whitcroft	my $in_comment = 0;
19704a0df2efSAndy Whitcroft	$current_comment = '';
19714a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
197200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
197300df344fSAndy Whitcroft		#warn "           $line\n";
19744a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
19754a0df2efSAndy Whitcroft			$in_comment = 1;
19764a0df2efSAndy Whitcroft		}
19774a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
19784a0df2efSAndy Whitcroft			$in_comment = 1;
19794a0df2efSAndy Whitcroft		}
19804a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
19814a0df2efSAndy Whitcroft			$current_comment = '';
19824a0df2efSAndy Whitcroft		}
19834a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
19844a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
19854a0df2efSAndy Whitcroft			$in_comment = 0;
19864a0df2efSAndy Whitcroft		}
19874a0df2efSAndy Whitcroft	}
19884a0df2efSAndy Whitcroft
19894a0df2efSAndy Whitcroft	chomp($current_comment);
19904a0df2efSAndy Whitcroft	return($current_comment);
19914a0df2efSAndy Whitcroft}
19924a0df2efSAndy Whitcroftsub ctx_has_comment {
19934a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
19944a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
19954a0df2efSAndy Whitcroft
199600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
19974a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
19984a0df2efSAndy Whitcroft
19994a0df2efSAndy Whitcroft	return ($cmt ne '');
20004a0df2efSAndy Whitcroft}
20014a0df2efSAndy Whitcroft
20024d001e4dSAndy Whitcroftsub raw_line {
20034d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
20044d001e4dSAndy Whitcroft
20054d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
20064d001e4dSAndy Whitcroft	$cnt++;
20074d001e4dSAndy Whitcroft
20084d001e4dSAndy Whitcroft	my $line;
20094d001e4dSAndy Whitcroft	while ($cnt) {
20104d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
20114d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
20124d001e4dSAndy Whitcroft		$cnt--;
20134d001e4dSAndy Whitcroft	}
20144d001e4dSAndy Whitcroft
20154d001e4dSAndy Whitcroft	return $line;
20164d001e4dSAndy Whitcroft}
20174d001e4dSAndy Whitcroft
20182a9f9d85STobin C. Hardingsub get_stat_real {
20192a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
20202a9f9d85STobin C. Harding
20212a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
20222a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
20232a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
20242a9f9d85STobin C. Harding	}
20252a9f9d85STobin C. Harding
20262a9f9d85STobin C. Harding	return $stat_real;
20272a9f9d85STobin C. Harding}
20282a9f9d85STobin C. Harding
2029e3d95a2aSTobin C. Hardingsub get_stat_here {
2030e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
2031e3d95a2aSTobin C. Harding
2032e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
2033e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
2034e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
2035e3d95a2aSTobin C. Harding	}
2036e3d95a2aSTobin C. Harding
2037e3d95a2aSTobin C. Harding	return $herectx;
2038e3d95a2aSTobin C. Harding}
2039e3d95a2aSTobin C. Harding
20400a920b5bSAndy Whitcroftsub cat_vet {
20410a920b5bSAndy Whitcroft	my ($vet) = @_;
20429c0ca6f9SAndy Whitcroft	my ($res, $coded);
20430a920b5bSAndy Whitcroft
20449c0ca6f9SAndy Whitcroft	$res = '';
20456c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
20466c72ffaaSAndy Whitcroft		$res .= $1;
20476c72ffaaSAndy Whitcroft		if ($2 ne '') {
20489c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
20496c72ffaaSAndy Whitcroft			$res .= $coded;
20506c72ffaaSAndy Whitcroft		}
20519c0ca6f9SAndy Whitcroft	}
20529c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
20530a920b5bSAndy Whitcroft
20549c0ca6f9SAndy Whitcroft	return $res;
20550a920b5bSAndy Whitcroft}
20560a920b5bSAndy Whitcroft
2057c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
2058cf655043SAndy Whitcroftmy $av_pending;
2059c2fdda0dSAndy Whitcroftmy @av_paren_type;
20601f65f947SAndy Whitcroftmy $av_pend_colon;
2061c2fdda0dSAndy Whitcroft
2062c2fdda0dSAndy Whitcroftsub annotate_reset {
2063c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
2064cf655043SAndy Whitcroft	$av_pending = '_';
2065cf655043SAndy Whitcroft	@av_paren_type = ('E');
20661f65f947SAndy Whitcroft	$av_pend_colon = 'O';
2067c2fdda0dSAndy Whitcroft}
2068c2fdda0dSAndy Whitcroft
20696c72ffaaSAndy Whitcroftsub annotate_values {
20706c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
20716c72ffaaSAndy Whitcroft
20726c72ffaaSAndy Whitcroft	my $res;
20731f65f947SAndy Whitcroft	my $var = '_' x length($stream);
20746c72ffaaSAndy Whitcroft	my $cur = $stream;
20756c72ffaaSAndy Whitcroft
2076c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
20776c72ffaaSAndy Whitcroft
20786c72ffaaSAndy Whitcroft	while (length($cur)) {
2079773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
2080cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
2081171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
20826c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
2083c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
2084c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
2085cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
2086c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
20876c72ffaaSAndy Whitcroft			}
20886c72ffaaSAndy Whitcroft
2089c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
20909446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
20919446ef56SAndy Whitcroft			push(@av_paren_type, $type);
2092addcdceaSAndy Whitcroft			$type = 'c';
20939446ef56SAndy Whitcroft
2094e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
2095c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
20966c72ffaaSAndy Whitcroft			$type = 'T';
20976c72ffaaSAndy Whitcroft
2098389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
2099389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
2100389a2fe5SAndy Whitcroft			$type = 'T';
2101389a2fe5SAndy Whitcroft
2102c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
2103171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
2104c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
2105171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
2106171ae1a4SAndy Whitcroft			if ($2 ne '') {
2107cf655043SAndy Whitcroft				$av_pending = 'N';
2108171ae1a4SAndy Whitcroft			}
2109171ae1a4SAndy Whitcroft			$type = 'E';
2110171ae1a4SAndy Whitcroft
2111c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
2112171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
2113171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
2114171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
21156c72ffaaSAndy Whitcroft
2116c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
2117cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
2118c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
2119cf655043SAndy Whitcroft
2120cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2121cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2122171ae1a4SAndy Whitcroft			$type = 'E';
2123cf655043SAndy Whitcroft
2124c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
2125cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2126cf655043SAndy Whitcroft			$av_preprocessor = 1;
2127cf655043SAndy Whitcroft
2128cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2129cf655043SAndy Whitcroft
2130171ae1a4SAndy Whitcroft			$type = 'E';
2131cf655043SAndy Whitcroft
2132c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
2133cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
2134cf655043SAndy Whitcroft
2135cf655043SAndy Whitcroft			$av_preprocessor = 1;
2136cf655043SAndy Whitcroft
2137cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
2138cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
2139cf655043SAndy Whitcroft			pop(@av_paren_type);
2140cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2141171ae1a4SAndy Whitcroft			$type = 'E';
21426c72ffaaSAndy Whitcroft
21436c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
2144c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
21456c72ffaaSAndy Whitcroft
2146171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2147171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
2148171ae1a4SAndy Whitcroft			$av_pending = $type;
2149171ae1a4SAndy Whitcroft			$type = 'N';
2150171ae1a4SAndy Whitcroft
21516c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
2152c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
21536c72ffaaSAndy Whitcroft			if (defined $2) {
2154cf655043SAndy Whitcroft				$av_pending = 'V';
21556c72ffaaSAndy Whitcroft			}
21566c72ffaaSAndy Whitcroft			$type = 'N';
21576c72ffaaSAndy Whitcroft
215814b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
2159c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
216014b111c1SAndy Whitcroft			$av_pending = 'E';
21616c72ffaaSAndy Whitcroft			$type = 'N';
21626c72ffaaSAndy Whitcroft
21631f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
21641f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
21651f65f947SAndy Whitcroft			$av_pend_colon = 'C';
21661f65f947SAndy Whitcroft			$type = 'N';
21671f65f947SAndy Whitcroft
216814b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
2169c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
21706c72ffaaSAndy Whitcroft			$type = 'N';
21716c72ffaaSAndy Whitcroft
21726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
2173c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
2174cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
2175cf655043SAndy Whitcroft			$av_pending = '_';
21766c72ffaaSAndy Whitcroft			$type = 'N';
21776c72ffaaSAndy Whitcroft
21786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
2179cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
2180cf655043SAndy Whitcroft			if ($new_type ne '_') {
2181cf655043SAndy Whitcroft				$type = $new_type;
2182c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
2183c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
21846c72ffaaSAndy Whitcroft			} else {
2185c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
21866c72ffaaSAndy Whitcroft			}
21876c72ffaaSAndy Whitcroft
2188c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
2189c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
2190c8cb2ca3SAndy Whitcroft			$type = 'V';
2191cf655043SAndy Whitcroft			$av_pending = 'V';
21926c72ffaaSAndy Whitcroft
21938e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
21948e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
21951f65f947SAndy Whitcroft				$av_pend_colon = 'B';
21968e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
21978e761b04SAndy Whitcroft				$av_pend_colon = 'L';
21981f65f947SAndy Whitcroft			}
21991f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
22001f65f947SAndy Whitcroft			$type = 'V';
22011f65f947SAndy Whitcroft
22026c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
2203c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
22046c72ffaaSAndy Whitcroft			$type = 'V';
22056c72ffaaSAndy Whitcroft
22066c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
2207c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
22086c72ffaaSAndy Whitcroft			$type = 'N';
22096c72ffaaSAndy Whitcroft
2210cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
2211c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
221213214adfSAndy Whitcroft			$type = 'E';
22131f65f947SAndy Whitcroft			$av_pend_colon = 'O';
221413214adfSAndy Whitcroft
22158e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
22168e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
22178e761b04SAndy Whitcroft			$type = 'C';
22188e761b04SAndy Whitcroft
22191f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
22201f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
22211f65f947SAndy Whitcroft			$type = 'N';
22221f65f947SAndy Whitcroft
22231f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
22241f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
22251f65f947SAndy Whitcroft
22261f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
22271f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
22281f65f947SAndy Whitcroft				$type = 'E';
22291f65f947SAndy Whitcroft			} else {
22301f65f947SAndy Whitcroft				$type = 'N';
22311f65f947SAndy Whitcroft			}
22321f65f947SAndy Whitcroft			$av_pend_colon = 'O';
22331f65f947SAndy Whitcroft
22348e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
223513214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
22366c72ffaaSAndy Whitcroft			$type = 'N';
22376c72ffaaSAndy Whitcroft
22380d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
223974048ed8SAndy Whitcroft			my $variant;
224074048ed8SAndy Whitcroft
224174048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
224274048ed8SAndy Whitcroft			if ($type eq 'V') {
224374048ed8SAndy Whitcroft				$variant = 'B';
224474048ed8SAndy Whitcroft			} else {
224574048ed8SAndy Whitcroft				$variant = 'U';
224674048ed8SAndy Whitcroft			}
224774048ed8SAndy Whitcroft
224874048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
224974048ed8SAndy Whitcroft			$type = 'N';
225074048ed8SAndy Whitcroft
22516c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2252c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
22536c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
22546c72ffaaSAndy Whitcroft				$type = 'N';
22556c72ffaaSAndy Whitcroft			}
22566c72ffaaSAndy Whitcroft
22576c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2258c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
22596c72ffaaSAndy Whitcroft		}
22606c72ffaaSAndy Whitcroft		if (defined $1) {
22616c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
22626c72ffaaSAndy Whitcroft			$res .= $type x length($1);
22636c72ffaaSAndy Whitcroft		}
22646c72ffaaSAndy Whitcroft	}
22656c72ffaaSAndy Whitcroft
22661f65f947SAndy Whitcroft	return ($res, $var);
22676c72ffaaSAndy Whitcroft}
22686c72ffaaSAndy Whitcroft
22698905a67cSAndy Whitcroftsub possible {
227013214adfSAndy Whitcroft	my ($possible, $line) = @_;
22719a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
22720776e594SAndy Whitcroft		^(?:
22730776e594SAndy Whitcroft			$Modifier|
22740776e594SAndy Whitcroft			$Storage|
22750776e594SAndy Whitcroft			$Type|
22769a974fdbSAndy Whitcroft			DEFINE_\S+
22779a974fdbSAndy Whitcroft		)$|
22789a974fdbSAndy Whitcroft		^(?:
22790776e594SAndy Whitcroft			goto|
22800776e594SAndy Whitcroft			return|
22810776e594SAndy Whitcroft			case|
22820776e594SAndy Whitcroft			else|
22830776e594SAndy Whitcroft			asm|__asm__|
228489a88353SAndy Whitcroft			do|
228589a88353SAndy Whitcroft			\#|
228689a88353SAndy Whitcroft			\#\#|
22879a974fdbSAndy Whitcroft		)(?:\s|$)|
22880776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
22899a974fdbSAndy Whitcroft	    )}x;
22909a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
22919a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2292c45dcabdSAndy Whitcroft		# Check for modifiers.
2293c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2294c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2295c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2296c45dcabdSAndy Whitcroft
2297c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2298c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2299d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
23009a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2301d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2302485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2303d2506586SAndy Whitcroft				}
23049a974fdbSAndy Whitcroft			}
2305c45dcabdSAndy Whitcroft
2306c45dcabdSAndy Whitcroft		} else {
230713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2308485ff23eSAlex Dowad			push(@typeListFile, $possible);
2309c45dcabdSAndy Whitcroft		}
23108905a67cSAndy Whitcroft		build_types();
23110776e594SAndy Whitcroft	} else {
23120776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
23138905a67cSAndy Whitcroft	}
23148905a67cSAndy Whitcroft}
23158905a67cSAndy Whitcroft
23166c72ffaaSAndy Whitcroftmy $prefix = '';
23176c72ffaaSAndy Whitcroft
2318000d1cc1SJoe Perchessub show_type {
2319cbec18afSJoe Perches	my ($type) = @_;
232091bfe484SJoe Perches
2321522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2322522b837cSAlexey Dobriyan
2323cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2324cbec18afSJoe Perches
2325cbec18afSJoe Perches	return !defined $ignore_type{$type};
2326000d1cc1SJoe Perches}
2327000d1cc1SJoe Perches
2328f0a594c1SAndy Whitcroftsub report {
2329cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2330cbec18afSJoe Perches
2331cbec18afSJoe Perches	if (!show_type($type) ||
2332cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2333773647a0SAndy Whitcroft		return 0;
2334773647a0SAndy Whitcroft	}
233557230297SJoe Perches	my $output = '';
2336737c0767SJohn Brooks	if ($color) {
233757230297SJoe Perches		if ($level eq 'ERROR') {
233857230297SJoe Perches			$output .= RED;
233957230297SJoe Perches		} elsif ($level eq 'WARNING') {
234057230297SJoe Perches			$output .= YELLOW;
2341000d1cc1SJoe Perches		} else {
234257230297SJoe Perches			$output .= GREEN;
2343000d1cc1SJoe Perches		}
234457230297SJoe Perches	}
234557230297SJoe Perches	$output .= $prefix . $level . ':';
234657230297SJoe Perches	if ($show_types) {
2347737c0767SJohn Brooks		$output .= BLUE if ($color);
234857230297SJoe Perches		$output .= "$type:";
234957230297SJoe Perches	}
2350737c0767SJohn Brooks	$output .= RESET if ($color);
235157230297SJoe Perches	$output .= ' ' . $msg . "\n";
235234d8815fSJoe Perches
235334d8815fSJoe Perches	if ($showfile) {
235434d8815fSJoe Perches		my @lines = split("\n", $output, -1);
235534d8815fSJoe Perches		splice(@lines, 1, 1);
235634d8815fSJoe Perches		$output = join("\n", @lines);
235734d8815fSJoe Perches	}
235852178ce0SDwaipayan Ray
235952178ce0SDwaipayan Ray	if ($terse) {
236052178ce0SDwaipayan Ray		$output = (split('\n', $output))[0] . "\n";
236152178ce0SDwaipayan Ray	}
236252178ce0SDwaipayan Ray
236352178ce0SDwaipayan Ray	if ($verbose && exists($verbose_messages{$type}) &&
236452178ce0SDwaipayan Ray	    !exists($verbose_emitted{$type})) {
236552178ce0SDwaipayan Ray		$output .= $verbose_messages{$type} . "\n\n";
236652178ce0SDwaipayan Ray		$verbose_emitted{$type} = 1;
236752178ce0SDwaipayan Ray	}
23688905a67cSAndy Whitcroft
236957230297SJoe Perches	push(our @report, $output);
2370773647a0SAndy Whitcroft
2371773647a0SAndy Whitcroft	return 1;
2372f0a594c1SAndy Whitcroft}
2373cbec18afSJoe Perches
2374f0a594c1SAndy Whitcroftsub report_dump {
237513214adfSAndy Whitcroft	our @report;
2376f0a594c1SAndy Whitcroft}
2377000d1cc1SJoe Perches
2378d752fcc8SJoe Perchessub fixup_current_range {
2379d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2380d752fcc8SJoe Perches
2381d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2382d752fcc8SJoe Perches		my $o = $1;
2383d752fcc8SJoe Perches		my $l = $2;
2384d752fcc8SJoe Perches		my $no = $o + $offset;
2385d752fcc8SJoe Perches		my $nl = $l + $length;
2386d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2387d752fcc8SJoe Perches	}
2388d752fcc8SJoe Perches}
2389d752fcc8SJoe Perches
2390d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2391d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2392d752fcc8SJoe Perches
2393d752fcc8SJoe Perches	my $range_last_linenr = 0;
2394d752fcc8SJoe Perches	my $delta_offset = 0;
2395d752fcc8SJoe Perches
2396d752fcc8SJoe Perches	my $old_linenr = 0;
2397d752fcc8SJoe Perches	my $new_linenr = 0;
2398d752fcc8SJoe Perches
2399d752fcc8SJoe Perches	my $next_insert = 0;
2400d752fcc8SJoe Perches	my $next_delete = 0;
2401d752fcc8SJoe Perches
2402d752fcc8SJoe Perches	my @lines = ();
2403d752fcc8SJoe Perches
2404d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2405d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2406d752fcc8SJoe Perches
2407d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2408d752fcc8SJoe Perches		my $save_line = 1;
2409d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2410323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2411d752fcc8SJoe Perches			$delta_offset = 0;
2412d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2413d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2414d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2415d752fcc8SJoe Perches		}
2416d752fcc8SJoe Perches
2417d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2418d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2419d752fcc8SJoe Perches			$save_line = 0;
2420d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2421d752fcc8SJoe Perches		}
2422d752fcc8SJoe Perches
2423d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2424d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2425d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2426d752fcc8SJoe Perches			$new_linenr++;
2427d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2428d752fcc8SJoe Perches		}
2429d752fcc8SJoe Perches
2430d752fcc8SJoe Perches		if ($save_line) {
2431d752fcc8SJoe Perches			push(@lines, $line);
2432d752fcc8SJoe Perches			$new_linenr++;
2433d752fcc8SJoe Perches		}
2434d752fcc8SJoe Perches
2435d752fcc8SJoe Perches		$old_linenr++;
2436d752fcc8SJoe Perches	}
2437d752fcc8SJoe Perches
2438d752fcc8SJoe Perches	return @lines;
2439d752fcc8SJoe Perches}
2440d752fcc8SJoe Perches
2441f2d7e4d4SJoe Perchessub fix_insert_line {
2442f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2443f2d7e4d4SJoe Perches
2444f2d7e4d4SJoe Perches	my $inserted = {
2445f2d7e4d4SJoe Perches		LINENR => $linenr,
2446f2d7e4d4SJoe Perches		LINE => $line,
2447f2d7e4d4SJoe Perches	};
2448f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2449f2d7e4d4SJoe Perches}
2450f2d7e4d4SJoe Perches
2451f2d7e4d4SJoe Perchessub fix_delete_line {
2452f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2453f2d7e4d4SJoe Perches
2454f2d7e4d4SJoe Perches	my $deleted = {
2455f2d7e4d4SJoe Perches		LINENR => $linenr,
2456f2d7e4d4SJoe Perches		LINE => $line,
2457f2d7e4d4SJoe Perches	};
2458f2d7e4d4SJoe Perches
2459f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2460f2d7e4d4SJoe Perches}
2461f2d7e4d4SJoe Perches
2462de7d4f0eSAndy Whitcroftsub ERROR {
2463cbec18afSJoe Perches	my ($type, $msg) = @_;
2464cbec18afSJoe Perches
2465cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2466de7d4f0eSAndy Whitcroft		our $clean = 0;
24676c72ffaaSAndy Whitcroft		our $cnt_error++;
24683705ce5bSJoe Perches		return 1;
2469de7d4f0eSAndy Whitcroft	}
24703705ce5bSJoe Perches	return 0;
2471773647a0SAndy Whitcroft}
2472de7d4f0eSAndy Whitcroftsub WARN {
2473cbec18afSJoe Perches	my ($type, $msg) = @_;
2474cbec18afSJoe Perches
2475cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2476de7d4f0eSAndy Whitcroft		our $clean = 0;
24776c72ffaaSAndy Whitcroft		our $cnt_warn++;
24783705ce5bSJoe Perches		return 1;
2479de7d4f0eSAndy Whitcroft	}
24803705ce5bSJoe Perches	return 0;
2481773647a0SAndy Whitcroft}
2482de7d4f0eSAndy Whitcroftsub CHK {
2483cbec18afSJoe Perches	my ($type, $msg) = @_;
2484cbec18afSJoe Perches
2485cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2486de7d4f0eSAndy Whitcroft		our $clean = 0;
24876c72ffaaSAndy Whitcroft		our $cnt_chk++;
24883705ce5bSJoe Perches		return 1;
24896c72ffaaSAndy Whitcroft	}
24903705ce5bSJoe Perches	return 0;
2491de7d4f0eSAndy Whitcroft}
2492de7d4f0eSAndy Whitcroft
24936ecd9674SAndy Whitcroftsub check_absolute_file {
24946ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
24956ecd9674SAndy Whitcroft	my $file = $absolute;
24966ecd9674SAndy Whitcroft
24976ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
24986ecd9674SAndy Whitcroft
24996ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
25006ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
25016ecd9674SAndy Whitcroft		if (-f "$root/$file") {
25026ecd9674SAndy Whitcroft			##print "file<$file>\n";
25036ecd9674SAndy Whitcroft			last;
25046ecd9674SAndy Whitcroft		}
25056ecd9674SAndy Whitcroft	}
25066ecd9674SAndy Whitcroft	if (! -f _)  {
25076ecd9674SAndy Whitcroft		return 0;
25086ecd9674SAndy Whitcroft	}
25096ecd9674SAndy Whitcroft
25106ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
25116ecd9674SAndy Whitcroft	my $prefix = $absolute;
25126ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
25136ecd9674SAndy Whitcroft
25146ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
25156ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2516000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2517000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
25186ecd9674SAndy Whitcroft	}
25196ecd9674SAndy Whitcroft}
25206ecd9674SAndy Whitcroft
25213705ce5bSJoe Perchessub trim {
25223705ce5bSJoe Perches	my ($string) = @_;
25233705ce5bSJoe Perches
2524b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2525b34c648bSJoe Perches
2526b34c648bSJoe Perches	return $string;
2527b34c648bSJoe Perches}
2528b34c648bSJoe Perches
2529b34c648bSJoe Perchessub ltrim {
2530b34c648bSJoe Perches	my ($string) = @_;
2531b34c648bSJoe Perches
2532b34c648bSJoe Perches	$string =~ s/^\s+//;
2533b34c648bSJoe Perches
2534b34c648bSJoe Perches	return $string;
2535b34c648bSJoe Perches}
2536b34c648bSJoe Perches
2537b34c648bSJoe Perchessub rtrim {
2538b34c648bSJoe Perches	my ($string) = @_;
2539b34c648bSJoe Perches
2540b34c648bSJoe Perches	$string =~ s/\s+$//;
25413705ce5bSJoe Perches
25423705ce5bSJoe Perches	return $string;
25433705ce5bSJoe Perches}
25443705ce5bSJoe Perches
254552ea8506SJoe Perchessub string_find_replace {
254652ea8506SJoe Perches	my ($string, $find, $replace) = @_;
254752ea8506SJoe Perches
254852ea8506SJoe Perches	$string =~ s/$find/$replace/g;
254952ea8506SJoe Perches
255052ea8506SJoe Perches	return $string;
255152ea8506SJoe Perches}
255252ea8506SJoe Perches
25533705ce5bSJoe Perchessub tabify {
25543705ce5bSJoe Perches	my ($leading) = @_;
25553705ce5bSJoe Perches
2556713a09deSAntonio Borneo	my $source_indent = $tabsize;
25573705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
25583705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
25593705ce5bSJoe Perches
25603705ce5bSJoe Perches	#convert leading spaces to tabs
25613705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
25623705ce5bSJoe Perches	#Remove spaces before a tab
25633705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
25643705ce5bSJoe Perches
25653705ce5bSJoe Perches	return "$leading";
25663705ce5bSJoe Perches}
25673705ce5bSJoe Perches
2568d1fe9c09SJoe Perchessub pos_last_openparen {
2569d1fe9c09SJoe Perches	my ($line) = @_;
2570d1fe9c09SJoe Perches
2571d1fe9c09SJoe Perches	my $pos = 0;
2572d1fe9c09SJoe Perches
2573d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2574d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2575d1fe9c09SJoe Perches
2576d1fe9c09SJoe Perches	my $last_openparen = 0;
2577d1fe9c09SJoe Perches
2578d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2579d1fe9c09SJoe Perches		return -1;
2580d1fe9c09SJoe Perches	}
2581d1fe9c09SJoe Perches
2582d1fe9c09SJoe Perches	my $len = length($line);
2583d1fe9c09SJoe Perches
2584d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2585d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2586d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2587d1fe9c09SJoe Perches			$pos += length($1) - 1;
2588d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2589d1fe9c09SJoe Perches			$last_openparen = $pos;
2590d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2591d1fe9c09SJoe Perches			last;
2592d1fe9c09SJoe Perches		}
2593d1fe9c09SJoe Perches	}
2594d1fe9c09SJoe Perches
259591cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2596d1fe9c09SJoe Perches}
2597d1fe9c09SJoe Perches
2598f36d3eb8SJoe Perchessub get_raw_comment {
2599f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2600f36d3eb8SJoe Perches	my $comment = '';
2601f36d3eb8SJoe Perches
2602f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2603f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2604f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2605f36d3eb8SJoe Perches		}
2606f36d3eb8SJoe Perches	}
2607f36d3eb8SJoe Perches
2608f36d3eb8SJoe Perches	return $comment;
2609f36d3eb8SJoe Perches}
2610f36d3eb8SJoe Perches
26115b8f82e1SSong Liusub exclude_global_initialisers {
26125b8f82e1SSong Liu	my ($realfile) = @_;
26135b8f82e1SSong Liu
26145b8f82e1SSong Liu	# Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
26155b8f82e1SSong Liu	return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
26165b8f82e1SSong Liu		$realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
26175b8f82e1SSong Liu		$realfile =~ m@/bpf/.*\.bpf\.c$@;
26185b8f82e1SSong Liu}
26195b8f82e1SSong Liu
26200a920b5bSAndy Whitcroftsub process {
26210a920b5bSAndy Whitcroft	my $filename = shift;
26220a920b5bSAndy Whitcroft
26230a920b5bSAndy Whitcroft	my $linenr=0;
26240a920b5bSAndy Whitcroft	my $prevline="";
2625c2fdda0dSAndy Whitcroft	my $prevrawline="";
26260a920b5bSAndy Whitcroft	my $stashline="";
2627c2fdda0dSAndy Whitcroft	my $stashrawline="";
26280a920b5bSAndy Whitcroft
26294a0df2efSAndy Whitcroft	my $length;
26300a920b5bSAndy Whitcroft	my $indent;
26310a920b5bSAndy Whitcroft	my $previndent=0;
26320a920b5bSAndy Whitcroft	my $stashindent=0;
26330a920b5bSAndy Whitcroft
2634de7d4f0eSAndy Whitcroft	our $clean = 1;
26350a920b5bSAndy Whitcroft	my $signoff = 0;
2636cd261496SGeert Uytterhoeven	my $author = '';
2637cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
263848ca2d8aSDwaipayan Ray	my $author_sob = '';
26390a920b5bSAndy Whitcroft	my $is_patch = 0;
2640133712a2SRob Herring	my $is_binding_patch = -1;
264129ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
264215662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
264344d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2644ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2645490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2646bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
26472a076f40SJoe Perches	my $commit_log_long_line = 0;
2648e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
264913f1937eSJoe Perches	my $reported_maintainer_file = 0;
2650fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2651fa64205dSPasi Savanainen
26524ce9f970SJoe Perches	my $last_git_commit_id_linenr = -1;
26534ce9f970SJoe Perches
2654365dd4eaSJoe Perches	my $last_blank_line = 0;
26555e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2656365dd4eaSJoe Perches
265713214adfSAndy Whitcroft	our @report = ();
26586c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
26596c72ffaaSAndy Whitcroft	our $cnt_error = 0;
26606c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
26616c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
26626c72ffaaSAndy Whitcroft
26630a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
26640a920b5bSAndy Whitcroft	my $realfile = '';
26650a920b5bSAndy Whitcroft	my $realline = 0;
26660a920b5bSAndy Whitcroft	my $realcnt = 0;
26670a920b5bSAndy Whitcroft	my $here = '';
266877cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
26690a920b5bSAndy Whitcroft	my $in_comment = 0;
2670c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
26710a920b5bSAndy Whitcroft	my $first_line = 0;
26721e855726SWolfram Sang	my $p1_prefix = '';
26730a920b5bSAndy Whitcroft
267413214adfSAndy Whitcroft	my $prev_values = 'E';
267513214adfSAndy Whitcroft
267613214adfSAndy Whitcroft	# suppression flags
2677773647a0SAndy Whitcroft	my %suppress_ifbraces;
2678170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
26792b474a1aSAndy Whitcroft	my %suppress_export;
26803e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2681653d4876SAndy Whitcroft
26827e51f197SJoe Perches	my %signatures = ();
2683323c1260SJoe Perches
2684c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2685de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2686c2fdda0dSAndy Whitcroft	#
2687de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2688de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2689773647a0SAndy Whitcroft
2690d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2691d8b07710SJoe Perches
26929f3a8992SRob Herring	my $checklicenseline = 1;
26939f3a8992SRob Herring
2694773647a0SAndy Whitcroft	sanitise_line_reset();
2695c2fdda0dSAndy Whitcroft	my $line;
2696c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2697773647a0SAndy Whitcroft		$linenr++;
2698773647a0SAndy Whitcroft		$line = $rawline;
2699c2fdda0dSAndy Whitcroft
27003705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
27013705ce5bSJoe Perches
2702773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2703de7d4f0eSAndy Whitcroft			$setup_docs = 0;
27042581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2705de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2706de7d4f0eSAndy Whitcroft			}
2707773647a0SAndy Whitcroft			#next;
2708de7d4f0eSAndy Whitcroft		}
270974fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2710773647a0SAndy Whitcroft			$realline=$1-1;
2711773647a0SAndy Whitcroft			if (defined $2) {
2712773647a0SAndy Whitcroft				$realcnt=$3+1;
2713773647a0SAndy Whitcroft			} else {
2714773647a0SAndy Whitcroft				$realcnt=1+1;
2715773647a0SAndy Whitcroft			}
2716c45dcabdSAndy Whitcroft			$in_comment = 0;
2717773647a0SAndy Whitcroft
2718773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2719773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2720773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2721773647a0SAndy Whitcroft			# at context start.
2722773647a0SAndy Whitcroft			my $edge;
272301fa9147SAndy Whitcroft			my $cnt = $realcnt;
272401fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
272501fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
272601fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
272701fa9147SAndy Whitcroft				$cnt--;
272801fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2729721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2730fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2731fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2732fae17daeSAndy Whitcroft					($edge) = $1;
2733fae17daeSAndy Whitcroft					last;
2734fae17daeSAndy Whitcroft				}
2735773647a0SAndy Whitcroft			}
2736773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2737773647a0SAndy Whitcroft				$in_comment = 1;
2738773647a0SAndy Whitcroft			}
2739773647a0SAndy Whitcroft
2740773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2741773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2742773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2743773647a0SAndy Whitcroft			if (!defined $edge &&
274483242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2745773647a0SAndy Whitcroft			{
2746773647a0SAndy Whitcroft				$in_comment = 1;
2747773647a0SAndy Whitcroft			}
2748773647a0SAndy Whitcroft
2749773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2750773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2751773647a0SAndy Whitcroft
2752171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2753773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2754171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2755773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2756773647a0SAndy Whitcroft		}
2757773647a0SAndy Whitcroft		push(@lines, $line);
2758773647a0SAndy Whitcroft
2759773647a0SAndy Whitcroft		if ($realcnt > 1) {
2760773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2761773647a0SAndy Whitcroft		} else {
2762773647a0SAndy Whitcroft			$realcnt = 0;
2763773647a0SAndy Whitcroft		}
2764773647a0SAndy Whitcroft
2765773647a0SAndy Whitcroft		#print "==>$rawline\n";
2766773647a0SAndy Whitcroft		#print "-->$line\n";
2767de7d4f0eSAndy Whitcroft
2768de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2769de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2770de7d4f0eSAndy Whitcroft		}
2771de7d4f0eSAndy Whitcroft	}
2772de7d4f0eSAndy Whitcroft
27736c72ffaaSAndy Whitcroft	$prefix = '';
27746c72ffaaSAndy Whitcroft
2775773647a0SAndy Whitcroft	$realcnt = 0;
2776773647a0SAndy Whitcroft	$linenr = 0;
2777194f66fcSJoe Perches	$fixlinenr = -1;
27780a920b5bSAndy Whitcroft	foreach my $line (@lines) {
27790a920b5bSAndy Whitcroft		$linenr++;
2780194f66fcSJoe Perches		$fixlinenr++;
27811b5539b1SJoe Perches		my $sline = $line;	#copy of $line
27821b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
27830a920b5bSAndy Whitcroft
2784c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2785f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
27866c72ffaaSAndy Whitcroft
278712c253abSJoe Perches# check if it's a mode change, rename or start of a patch
278812c253abSJoe Perches		if (!$in_commit_log &&
278912c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
279012c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
279112c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
279212c253abSJoe Perches			$is_patch = 1;
279312c253abSJoe Perches		}
279412c253abSJoe Perches
27950a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2796e518e9a5SJoe Perches		if (!$in_commit_log &&
279774fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
279874fd4f34SJoe Perches			my $context = $4;
27990a920b5bSAndy Whitcroft			$is_patch = 1;
28004a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
28010a920b5bSAndy Whitcroft			$realline=$1-1;
28020a920b5bSAndy Whitcroft			if (defined $2) {
28030a920b5bSAndy Whitcroft				$realcnt=$3+1;
28040a920b5bSAndy Whitcroft			} else {
28050a920b5bSAndy Whitcroft				$realcnt=1+1;
28060a920b5bSAndy Whitcroft			}
2807c2fdda0dSAndy Whitcroft			annotate_reset();
280813214adfSAndy Whitcroft			$prev_values = 'E';
280913214adfSAndy Whitcroft
2810773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2811170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
28122b474a1aSAndy Whitcroft			%suppress_export = ();
28133e469cdcSAndy Whitcroft			$suppress_statement = 0;
281474fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
281574fd4f34SJoe Perches				$context_function = $1;
281674fd4f34SJoe Perches			} else {
281774fd4f34SJoe Perches				undef $context_function;
281874fd4f34SJoe Perches			}
28190a920b5bSAndy Whitcroft			next;
28200a920b5bSAndy Whitcroft
28214a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
28224a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
28234a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2824773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
28250a920b5bSAndy Whitcroft			$realline++;
2826d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
28270a920b5bSAndy Whitcroft
28284a0df2efSAndy Whitcroft			# Measure the line length and indent.
2829c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
28300a920b5bSAndy Whitcroft
28310a920b5bSAndy Whitcroft			# Track the previous line.
28320a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
28330a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2834c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2835c2fdda0dSAndy Whitcroft
2836773647a0SAndy Whitcroft			#warn "line<$line>\n";
28376c72ffaaSAndy Whitcroft
2838d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2839d8aaf121SAndy Whitcroft			$realcnt--;
28400a920b5bSAndy Whitcroft		}
28410a920b5bSAndy Whitcroft
2842cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2843cc77cdcaSAndy Whitcroft
28446c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
28456c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2846773647a0SAndy Whitcroft
28472ac73b4fSJoe Perches		my $found_file = 0;
2848773647a0SAndy Whitcroft		# extract the filename as it passes
28493bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
28503bf9a009SRabin Vincent			$realfile = $1;
28512b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2852270c49a0SJoe Perches			$in_commit_log = 0;
28532ac73b4fSJoe Perches			$found_file = 1;
28543bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2855773647a0SAndy Whitcroft			$realfile = $1;
28562b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2857270c49a0SJoe Perches			$in_commit_log = 0;
28581e855726SWolfram Sang
28591e855726SWolfram Sang			$p1_prefix = $1;
2860e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2861e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2862000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2863000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
28641e855726SWolfram Sang			}
2865773647a0SAndy Whitcroft
2866c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2867000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2868000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2869773647a0SAndy Whitcroft			}
28702ac73b4fSJoe Perches			$found_file = 1;
28712ac73b4fSJoe Perches		}
28722ac73b4fSJoe Perches
287334d8815fSJoe Perches#make up the handle for any error we report on this line
287434d8815fSJoe Perches		if ($showfile) {
287534d8815fSJoe Perches			$prefix = "$realfile:$realline: "
287634d8815fSJoe Perches		} elsif ($emacs) {
28777d3a9f67SJoe Perches			if ($file) {
28787d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
28797d3a9f67SJoe Perches			} else {
288034d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
288134d8815fSJoe Perches			}
28827d3a9f67SJoe Perches		}
288334d8815fSJoe Perches
28842ac73b4fSJoe Perches		if ($found_file) {
288585b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
288685b0ee18SJoe Perches				WARN("OBSOLETE",
288785b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
288885b0ee18SJoe Perches			}
28897bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
28902ac73b4fSJoe Perches				$check = 1;
28912ac73b4fSJoe Perches			} else {
28922ac73b4fSJoe Perches				$check = $check_orig;
28932ac73b4fSJoe Perches			}
28949f3a8992SRob Herring			$checklicenseline = 1;
2895133712a2SRob Herring
2896133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2897133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2898133712a2SRob Herring
2899133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2900133712a2SRob Herring
2901133712a2SRob Herring				if (($last_binding_patch != -1) &&
2902133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2903133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2904858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2905133712a2SRob Herring				}
2906133712a2SRob Herring			}
2907133712a2SRob Herring
2908773647a0SAndy Whitcroft			next;
2909773647a0SAndy Whitcroft		}
2910773647a0SAndy Whitcroft
2911389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
29120a920b5bSAndy Whitcroft
2913c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2914c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2915c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
29160a920b5bSAndy Whitcroft
29176c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
29186c72ffaaSAndy Whitcroft
2919490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2920490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2921490b292cSJoe Perches		if ($in_commit_log) {
2922490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2923490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2924490b292cSJoe Perches			}
2925490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2926490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2927490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2928490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2929490b292cSJoe Perches		}
2930490b292cSJoe Perches
2931e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2932e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
293313e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
293413e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2935e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2936e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2937e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2938e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2939e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2940e518e9a5SJoe Perches		}
2941e518e9a5SJoe Perches
29423bf9a009SRabin Vincent# Check for incorrect file permissions
29433bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
29443bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
294504db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
294604db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2947000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2948000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
29493bf9a009SRabin Vincent			}
29503bf9a009SRabin Vincent		}
29513bf9a009SRabin Vincent
2952cd261496SGeert Uytterhoeven# Check the patch for a From:
2953cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2954cd261496SGeert Uytterhoeven			$author = $1;
2955e7f929f3SDwaipayan Ray			my $curline = $linenr;
2956e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2957e7f929f3SDwaipayan Ray				$author .= $1;
2958e7f929f3SDwaipayan Ray			}
2959cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2960cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2961dfa05c28SJoe Perches			$author = reformat_email($author);
2962cd261496SGeert Uytterhoeven		}
2963cd261496SGeert Uytterhoeven
296420112475SJoe Perches# Check the patch for a signoff:
2965dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
29664a0df2efSAndy Whitcroft			$signoff++;
296715662b3eSJoe Perches			$in_commit_log = 0;
296848ca2d8aSDwaipayan Ray			if ($author ne ''  && $authorsignoff != 1) {
2969fccaebf0SDwaipayan Ray				if (same_email_addresses($1, $author)) {
2970cd261496SGeert Uytterhoeven					$authorsignoff = 1;
297148ca2d8aSDwaipayan Ray				} else {
297248ca2d8aSDwaipayan Ray					my $ctx = $1;
297348ca2d8aSDwaipayan Ray					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
297448ca2d8aSDwaipayan Ray					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
297548ca2d8aSDwaipayan Ray
2976046fc741SMimi Zohar					if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
297748ca2d8aSDwaipayan Ray						$author_sob = $ctx;
297848ca2d8aSDwaipayan Ray						$authorsignoff = 2;
2979046fc741SMimi Zohar					} elsif (lc $email_address eq lc $author_address) {
298048ca2d8aSDwaipayan Ray						$author_sob = $ctx;
298148ca2d8aSDwaipayan Ray						$authorsignoff = 3;
298248ca2d8aSDwaipayan Ray					} elsif ($email_name eq $author_name) {
298348ca2d8aSDwaipayan Ray						$author_sob = $ctx;
298448ca2d8aSDwaipayan Ray						$authorsignoff = 4;
298548ca2d8aSDwaipayan Ray
298648ca2d8aSDwaipayan Ray						my $address1 = $email_address;
298748ca2d8aSDwaipayan Ray						my $address2 = $author_address;
298848ca2d8aSDwaipayan Ray
298948ca2d8aSDwaipayan Ray						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
299048ca2d8aSDwaipayan Ray							$address1 = "$1$2";
299148ca2d8aSDwaipayan Ray						}
299248ca2d8aSDwaipayan Ray						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
299348ca2d8aSDwaipayan Ray							$address2 = "$1$2";
299448ca2d8aSDwaipayan Ray						}
299548ca2d8aSDwaipayan Ray						if ($address1 eq $address2) {
299648ca2d8aSDwaipayan Ray							$authorsignoff = 5;
299748ca2d8aSDwaipayan Ray						}
299848ca2d8aSDwaipayan Ray					}
2999cd261496SGeert Uytterhoeven				}
3000cd261496SGeert Uytterhoeven			}
30010a920b5bSAndy Whitcroft		}
300220112475SJoe Perches
300344d303ebSJoe Perches# Check for patch separator
300444d303ebSJoe Perches		if ($line =~ /^---$/) {
300544d303ebSJoe Perches			$has_patch_separator = 1;
300644d303ebSJoe Perches			$in_commit_log = 0;
300744d303ebSJoe Perches		}
300844d303ebSJoe Perches
3009e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
3010e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
3011e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
3012e0d975b1SJoe Perches			$reported_maintainer_file = 1;
3013e0d975b1SJoe Perches		}
3014e0d975b1SJoe Perches
301520112475SJoe Perches# Check signature styles
3016270c49a0SJoe Perches		if (!$in_header_lines &&
3017ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
301820112475SJoe Perches			my $space_before = $1;
301920112475SJoe Perches			my $sign_off = $2;
302020112475SJoe Perches			my $space_after = $3;
302120112475SJoe Perches			my $email = $4;
302220112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
302320112475SJoe Perches
3024ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
3025831242abSAditya Srivastava				my $suggested_signature = find_standard_signature($sign_off);
3026831242abSAditya Srivastava				if ($suggested_signature eq "") {
3027ce0338dfSJoe Perches					WARN("BAD_SIGN_OFF",
3028ce0338dfSJoe Perches					     "Non-standard signature: $sign_off\n" . $herecurr);
3029831242abSAditya Srivastava				} else {
3030831242abSAditya Srivastava					if (WARN("BAD_SIGN_OFF",
3031831242abSAditya Srivastava						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
3032831242abSAditya Srivastava					    $fix) {
3033831242abSAditya Srivastava						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
3034831242abSAditya Srivastava					}
3035831242abSAditya Srivastava				}
3036ce0338dfSJoe Perches			}
303720112475SJoe Perches			if (defined $space_before && $space_before ne "") {
30383705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30393705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
30403705ce5bSJoe Perches				    $fix) {
3041194f66fcSJoe Perches					$fixed[$fixlinenr] =
30423705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30433705ce5bSJoe Perches				}
304420112475SJoe Perches			}
304520112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
30463705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30473705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
30483705ce5bSJoe Perches				    $fix) {
3049194f66fcSJoe Perches					$fixed[$fixlinenr] =
30503705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30513705ce5bSJoe Perches				}
30523705ce5bSJoe Perches
305320112475SJoe Perches			}
305420112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
30553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30563705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
30573705ce5bSJoe Perches				    $fix) {
3058194f66fcSJoe Perches					$fixed[$fixlinenr] =
30593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30603705ce5bSJoe Perches				}
306120112475SJoe Perches			}
306220112475SJoe Perches
3063dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
306448ca2d8aSDwaipayan Ray			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
306520112475SJoe Perches			if ($suggested_email eq "") {
3066000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
3067000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
306820112475SJoe Perches			} else {
306920112475SJoe Perches				my $dequoted = $suggested_email;
307020112475SJoe Perches				$dequoted =~ s/^"//;
307120112475SJoe Perches				$dequoted =~ s/" </ </;
307220112475SJoe Perches				# Don't force email to have quotes
307320112475SJoe Perches				# Allow just an angle bracketed address
3074fccaebf0SDwaipayan Ray				if (!same_email_addresses($email, $suggested_email)) {
3075fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3076fccaebf0SDwaipayan Ray						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3077fccaebf0SDwaipayan Ray					    $fix) {
3078fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3079fccaebf0SDwaipayan Ray					}
3080fccaebf0SDwaipayan Ray				}
3081fccaebf0SDwaipayan Ray
3082fccaebf0SDwaipayan Ray				# Address part shouldn't have comments
3083fccaebf0SDwaipayan Ray				my $stripped_address = $email_address;
3084fccaebf0SDwaipayan Ray				$stripped_address =~ s/\([^\(\)]*\)//g;
3085fccaebf0SDwaipayan Ray				if ($email_address ne $stripped_address) {
3086fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3087fccaebf0SDwaipayan Ray						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3088fccaebf0SDwaipayan Ray					    $fix) {
3089fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3090fccaebf0SDwaipayan Ray					}
3091fccaebf0SDwaipayan Ray				}
3092fccaebf0SDwaipayan Ray
3093fccaebf0SDwaipayan Ray				# Only one name comment should be allowed
3094fccaebf0SDwaipayan Ray				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3095fccaebf0SDwaipayan Ray				if ($comment_count > 1) {
3096000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
3097fccaebf0SDwaipayan Ray					     "Use a single name comment in email: '$email'\n" . $herecurr);
3098fccaebf0SDwaipayan Ray				}
3099fccaebf0SDwaipayan Ray
3100fccaebf0SDwaipayan Ray
3101fccaebf0SDwaipayan Ray				# [email protected] or [email protected] shouldn't
3102e73d2715SDwaipayan Ray				# have an email name. In addition comments should strictly
3103fccaebf0SDwaipayan Ray				# begin with a #
3104fccaebf0SDwaipayan Ray				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3105fccaebf0SDwaipayan Ray					if (($comment ne "" && $comment !~ /^#.+/) ||
3106fccaebf0SDwaipayan Ray					    ($email_name ne "")) {
3107fccaebf0SDwaipayan Ray						my $cur_name = $email_name;
3108fccaebf0SDwaipayan Ray						my $new_comment = $comment;
3109fccaebf0SDwaipayan Ray						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3110fccaebf0SDwaipayan Ray
3111fccaebf0SDwaipayan Ray						# Remove brackets enclosing comment text
3112fccaebf0SDwaipayan Ray						# and # from start of comments to get comment text
3113fccaebf0SDwaipayan Ray						$new_comment =~ s/^\((.*)\)$/$1/;
3114fccaebf0SDwaipayan Ray						$new_comment =~ s/^\[(.*)\]$/$1/;
3115fccaebf0SDwaipayan Ray						$new_comment =~ s/^[\s\#]+|\s+$//g;
3116fccaebf0SDwaipayan Ray
3117fccaebf0SDwaipayan Ray						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3118fccaebf0SDwaipayan Ray						$new_comment = " # $new_comment" if ($new_comment ne "");
3119fccaebf0SDwaipayan Ray						my $new_email = "$email_address$new_comment";
3120fccaebf0SDwaipayan Ray
3121fccaebf0SDwaipayan Ray						if (WARN("BAD_STABLE_ADDRESS_STYLE",
3122fccaebf0SDwaipayan Ray							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3123fccaebf0SDwaipayan Ray						    $fix) {
3124fccaebf0SDwaipayan Ray							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3125fccaebf0SDwaipayan Ray						}
3126fccaebf0SDwaipayan Ray					}
3127fccaebf0SDwaipayan Ray				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3128fccaebf0SDwaipayan Ray					my $new_comment = $comment;
3129fccaebf0SDwaipayan Ray
3130fccaebf0SDwaipayan Ray					# Extract comment text from within brackets or
3131fccaebf0SDwaipayan Ray					# c89 style /*...*/ comments
3132fccaebf0SDwaipayan Ray					$new_comment =~ s/^\[(.*)\]$/$1/;
3133fccaebf0SDwaipayan Ray					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3134fccaebf0SDwaipayan Ray
3135fccaebf0SDwaipayan Ray					$new_comment = trim($new_comment);
3136fccaebf0SDwaipayan Ray					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3137fccaebf0SDwaipayan Ray					$new_comment = "($new_comment)" if ($new_comment ne "");
3138fccaebf0SDwaipayan Ray					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3139fccaebf0SDwaipayan Ray
3140fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3141fccaebf0SDwaipayan Ray						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3142fccaebf0SDwaipayan Ray					    $fix) {
3143fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3144fccaebf0SDwaipayan Ray					}
314520112475SJoe Perches				}
31460a920b5bSAndy Whitcroft			}
31477e51f197SJoe Perches
31487e51f197SJoe Perches# Check for duplicate signatures
31497e51f197SJoe Perches			my $sig_nospace = $line;
31507e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
31517e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
31527e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
31537e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
31547e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
31557e51f197SJoe Perches			} else {
31567e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
31577e51f197SJoe Perches			}
31586c5d24eeSSean Christopherson
31596c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
31606c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
31616c5d24eeSSean Christopherson				if ($email eq $author) {
31626c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31631916f777SThorsten Leemhuis					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . $herecurr);
31646c5d24eeSSean Christopherson				}
31656c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
31666c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31671916f777SThorsten Leemhuis					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr);
31681916f777SThorsten Leemhuis				} elsif ($rawlines[$linenr] !~ /^signed-off-by:\s*(.*)/i) {
31696c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31701916f777SThorsten Leemhuis					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr . $rawlines[$linenr] . "\n");
31716c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
31726c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31731916f777SThorsten Leemhuis					     "Co-developed-by and Signed-off-by: name/email do not match\n" . $herecurr . $rawlines[$linenr] . "\n");
31746c5d24eeSSean Christopherson				}
31756c5d24eeSSean Christopherson			}
3176d7f1d71eSKai Wasserbäch
317744c31888SMatthieu Baerts# check if Reported-by: is followed by a Closes: tag
3178d7f1d71eSKai Wasserbäch			if ($sign_off =~ /^reported(?:|-and-tested)-by:$/i) {
3179d7f1d71eSKai Wasserbäch				if (!defined $lines[$linenr]) {
3180d7f1d71eSKai Wasserbäch					WARN("BAD_REPORTED_BY_LINK",
318144c31888SMatthieu Baerts					     "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . "\n");
3182*d6ccdd67SMatthieu Baerts				} elsif ($rawlines[$linenr] !~ /^closes:\s*/i) {
3183d7f1d71eSKai Wasserbäch					WARN("BAD_REPORTED_BY_LINK",
318444c31888SMatthieu Baerts					     "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . $rawlines[$linenr] . "\n");
31850a920b5bSAndy Whitcroft				}
3186d7f1d71eSKai Wasserbäch			}
3187d7f1d71eSKai Wasserbäch		}
3188d7f1d71eSKai Wasserbäch
31890a920b5bSAndy Whitcroft
3190bd17e036SNiklas Söderlund# Check Fixes: styles is correct
3191bd17e036SNiklas Söderlund		if (!$in_header_lines &&
3192bd17e036SNiklas Söderlund		    $line =~ /^\s*fixes:?\s*(?:commit\s*)?[0-9a-f]{5,}\b/i) {
3193bd17e036SNiklas Söderlund			my $orig_commit = "";
3194bd17e036SNiklas Söderlund			my $id = "0123456789ab";
3195bd17e036SNiklas Söderlund			my $title = "commit title";
3196bd17e036SNiklas Söderlund			my $tag_case = 1;
3197bd17e036SNiklas Söderlund			my $tag_space = 1;
3198bd17e036SNiklas Söderlund			my $id_length = 1;
3199bd17e036SNiklas Söderlund			my $id_case = 1;
3200bd17e036SNiklas Söderlund			my $title_has_quotes = 0;
3201bd17e036SNiklas Söderlund
3202bd17e036SNiklas Söderlund			if ($line =~ /(\s*fixes:?)\s+([0-9a-f]{5,})\s+($balanced_parens)/i) {
3203bd17e036SNiklas Söderlund				my $tag = $1;
3204bd17e036SNiklas Söderlund				$orig_commit = $2;
3205bd17e036SNiklas Söderlund				$title = $3;
3206bd17e036SNiklas Söderlund
3207bd17e036SNiklas Söderlund				$tag_case = 0 if $tag eq "Fixes:";
3208bd17e036SNiklas Söderlund				$tag_space = 0 if ($line =~ /^fixes:? [0-9a-f]{5,} ($balanced_parens)/i);
3209bd17e036SNiklas Söderlund
3210bd17e036SNiklas Söderlund				$id_length = 0 if ($orig_commit =~ /^[0-9a-f]{12}$/i);
3211bd17e036SNiklas Söderlund				$id_case = 0 if ($orig_commit !~ /[A-F]/);
3212bd17e036SNiklas Söderlund
3213bd17e036SNiklas Söderlund				# Always strip leading/trailing parens then double quotes if existing
3214bd17e036SNiklas Söderlund				$title = substr($title, 1, -1);
3215bd17e036SNiklas Söderlund				if ($title =~ /^".*"$/) {
3216bd17e036SNiklas Söderlund					$title = substr($title, 1, -1);
3217bd17e036SNiklas Söderlund					$title_has_quotes = 1;
3218bd17e036SNiklas Söderlund				}
3219bd17e036SNiklas Söderlund			}
3220bd17e036SNiklas Söderlund
3221bd17e036SNiklas Söderlund			my ($cid, $ctitle) = git_commit_info($orig_commit, $id,
3222bd17e036SNiklas Söderlund							     $title);
3223bd17e036SNiklas Söderlund
3224bd17e036SNiklas Söderlund			if ($ctitle ne $title || $tag_case || $tag_space ||
3225bd17e036SNiklas Söderlund			    $id_length || $id_case || !$title_has_quotes) {
3226bd17e036SNiklas Söderlund				if (WARN("BAD_FIXES_TAG",
3227bd17e036SNiklas Söderlund				     "Please use correct Fixes: style 'Fixes: <12 chars of sha1> (\"<title line>\")' - ie: 'Fixes: $cid (\"$ctitle\")'\n" . $herecurr) &&
3228bd17e036SNiklas Söderlund				    $fix) {
3229bd17e036SNiklas Söderlund					$fixed[$fixlinenr] = "Fixes: $cid (\"$ctitle\")";
3230bd17e036SNiklas Söderlund				}
3231bd17e036SNiklas Söderlund			}
3232bd17e036SNiklas Söderlund		}
3233bd17e036SNiklas Söderlund
3234a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
3235a2fe16b9SJoe Perches		if ($in_header_lines &&
3236a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3237a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
3238a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3239a2fe16b9SJoe Perches		}
3240a2fe16b9SJoe Perches
324144d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
324244d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
32437580c5b9SAditya Srivastava			if (ERROR("GERRIT_CHANGE_ID",
32447580c5b9SAditya Srivastava			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
32457580c5b9SAditya Srivastava			    $fix) {
32467580c5b9SAditya Srivastava				fix_delete_line($fixlinenr, $rawline);
32477580c5b9SAditya Srivastava			}
32487ebd05efSChristopher Covington		}
32497ebd05efSChristopher Covington
3250369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
3251369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3252369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3253369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3254369c8dd3SJoe Perches					# timestamp
3255634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3256634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3257634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3258634cffccSJoe Perches					# stack dump address styles
3259369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
3260369c8dd3SJoe Perches		}
3261369c8dd3SJoe Perches
32622a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
32632a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
3264bf4daf12SJoe Perches		    length($line) > 75 &&
3265bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3266bf4daf12SJoe Perches					# file delta changes
326736f8b348SJerome Forissier		      $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\.\-\+]+:/ ||
3268bf4daf12SJoe Perches					# filename then :
3269f94e40eaSMatthieu Baerts		      $line =~ /^\s*(?:Fixes:|$link_tags_search|$signature_tags)/i ||
3270f94e40eaSMatthieu Baerts					# A Fixes:, link or signature tag line
3271bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
32722a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
32732a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
32742a076f40SJoe Perches			$commit_log_long_line = 1;
32752a076f40SJoe Perches		}
32762a076f40SJoe Perches
3277bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
3278bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
3279bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
3280bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
3281bf4daf12SJoe Perches		}
3282bf4daf12SJoe Perches
328376f381bbSKai Wasserbäch# Check for odd tags before a URI/URL
328476f381bbSKai Wasserbäch		if ($in_commit_log &&
3285f94e40eaSMatthieu Baerts		    $line =~ /^\s*(\w+:)\s*http/ && $1 !~ /^$link_tags_search$/) {
328676f381bbSKai Wasserbäch			if ($1 =~ /^v(?:ersion)?\d+/i) {
328776f381bbSKai Wasserbäch				WARN("COMMIT_LOG_VERSIONING",
328876f381bbSKai Wasserbäch				     "Patch version information should be after the --- line\n" . $herecurr);
328976f381bbSKai Wasserbäch			} else {
329076f381bbSKai Wasserbäch				WARN("COMMIT_LOG_USE_LINK",
3291f94e40eaSMatthieu Baerts				     "Unknown link reference '$1', use $link_tags_print instead\n" . $herecurr);
329276f381bbSKai Wasserbäch			}
329376f381bbSKai Wasserbäch		}
329476f381bbSKai Wasserbäch
3295*d6ccdd67SMatthieu Baerts# Check for misuse of the link tags
3296*d6ccdd67SMatthieu Baerts		if ($in_commit_log &&
3297*d6ccdd67SMatthieu Baerts		    $line =~ /^\s*(\w+:)\s*(\S+)/) {
3298*d6ccdd67SMatthieu Baerts			my $tag = $1;
3299*d6ccdd67SMatthieu Baerts			my $value = $2;
3300*d6ccdd67SMatthieu Baerts			if ($tag =~ /^$link_tags_search$/ && $value !~ m{^https?://}) {
3301*d6ccdd67SMatthieu Baerts				WARN("COMMIT_LOG_WRONG_LINK",
3302*d6ccdd67SMatthieu Baerts				     "'$tag' should be followed by a public http(s) link\n" . $herecurr);
3303*d6ccdd67SMatthieu Baerts			}
3304*d6ccdd67SMatthieu Baerts		}
3305*d6ccdd67SMatthieu Baerts
3306084a617aSDwaipayan Ray# Check for lines starting with a #
3307084a617aSDwaipayan Ray		if ($in_commit_log && $line =~ /^#/) {
3308084a617aSDwaipayan Ray			if (WARN("COMMIT_COMMENT_SYMBOL",
3309084a617aSDwaipayan Ray				 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3310084a617aSDwaipayan Ray			    $fix) {
3311084a617aSDwaipayan Ray				$fixed[$fixlinenr] =~ s/^/ /;
3312084a617aSDwaipayan Ray			}
3313084a617aSDwaipayan Ray		}
3314084a617aSDwaipayan Ray
33150d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
33164ce9f970SJoe Perches# A correctly formed commit description is:
33174ce9f970SJoe Perches#    commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
33184ce9f970SJoe Perches# with the commit subject '("' prefix and '")' suffix
33194ce9f970SJoe Perches# This is a fairly compilicated block as it tests for what appears to be
33204ce9f970SJoe Perches# bare SHA-1 hash with  minimum length of 5.  It also avoids several types of
33214ce9f970SJoe Perches# possible SHA-1 matches.
33224ce9f970SJoe Perches# A commit match can span multiple lines so this block attempts to find a
33234ce9f970SJoe Perches# complete typical commit on a maximum of 3 lines
33244ce9f970SJoe Perches		if ($perl_version_ok &&
33254ce9f970SJoe Perches		    $in_commit_log && !$commit_log_possible_stack_dump &&
3326a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3327e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
33284ce9f970SJoe Perches		    (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
33294ce9f970SJoe Perches		      ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
3330aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3331369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3332bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3333fe043ea1SJoe Perches			my $init_char = "c";
3334fe043ea1SJoe Perches			my $orig_commit = "";
33350d7835fcSJoe Perches			my $short = 1;
33360d7835fcSJoe Perches			my $long = 0;
33370d7835fcSJoe Perches			my $case = 1;
33380d7835fcSJoe Perches			my $space = 1;
33390d7835fcSJoe Perches			my $id = '0123456789ab';
33400d7835fcSJoe Perches			my $orig_desc = "commit description";
33410d7835fcSJoe Perches			my $description = "";
33424ce9f970SJoe Perches			my $herectx = $herecurr;
33434ce9f970SJoe Perches			my $has_parens = 0;
33444ce9f970SJoe Perches			my $has_quotes = 0;
33450d7835fcSJoe Perches
33464ce9f970SJoe Perches			my $input = $line;
33474ce9f970SJoe Perches			if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
33484ce9f970SJoe Perches				for (my $n = 0; $n < 2; $n++) {
33494ce9f970SJoe Perches					if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
33504ce9f970SJoe Perches						$orig_desc = $1;
33514ce9f970SJoe Perches						$has_parens = 1;
33524ce9f970SJoe Perches						# Always strip leading/trailing parens then double quotes if existing
33534ce9f970SJoe Perches						$orig_desc = substr($orig_desc, 1, -1);
33544ce9f970SJoe Perches						if ($orig_desc =~ /^".*"$/) {
33554ce9f970SJoe Perches							$orig_desc = substr($orig_desc, 1, -1);
33564ce9f970SJoe Perches							$has_quotes = 1;
33574ce9f970SJoe Perches						}
33584ce9f970SJoe Perches						last;
33594ce9f970SJoe Perches					}
33604ce9f970SJoe Perches					last if ($#lines < $linenr + $n);
33614ce9f970SJoe Perches					$input .= " " . trim($rawlines[$linenr + $n]);
33624ce9f970SJoe Perches					$herectx .= "$rawlines[$linenr + $n]\n";
33634ce9f970SJoe Perches				}
33644ce9f970SJoe Perches				$herectx = $herecurr if (!$has_parens);
3365fe043ea1SJoe Perches			}
3366fe043ea1SJoe Perches
33674ce9f970SJoe Perches			if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
33684ce9f970SJoe Perches				$init_char = $1;
33694ce9f970SJoe Perches				$orig_commit = lc($2);
33704ce9f970SJoe Perches				$short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
33714ce9f970SJoe Perches				$long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
33724ce9f970SJoe Perches				$space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
33734ce9f970SJoe Perches				$case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
33744ce9f970SJoe Perches			} elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
33754ce9f970SJoe Perches				$orig_commit = lc($1);
33760d7835fcSJoe Perches			}
33770d7835fcSJoe Perches
33780d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
33790d7835fcSJoe Perches							      $id, $orig_desc);
33800d7835fcSJoe Perches
3381948b133aSHeinrich Schuchardt			if (defined($id) &&
33824ce9f970SJoe Perches			    ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
33834ce9f970SJoe Perches			    $last_git_commit_id_linenr != $linenr - 1) {
3384d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
33854ce9f970SJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
33860d7835fcSJoe Perches			}
33874ce9f970SJoe Perches			#don't report the next line if this line ends in commit and the sha1 hash is the next line
33884ce9f970SJoe Perches			$last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
3389d311cd44SJoe Perches		}
3390d311cd44SJoe Perches
33919b71f79fSBjorn Helgaas# Check for mailing list archives other than lore.kernel.org
33929b71f79fSBjorn Helgaas		if ($rawline =~ m{http.*\b$obsolete_archives}) {
33939b71f79fSBjorn Helgaas			WARN("PREFER_LORE_ARCHIVE",
33949b71f79fSBjorn Helgaas			     "Use lore.kernel.org archive links when possible - see https://lore.kernel.org/lists.html\n" . $herecurr);
33959b71f79fSBjorn Helgaas		}
33969b71f79fSBjorn Helgaas
339713f1937eSJoe Perches# Check for added, moved or deleted files
339813f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
339913f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
340013f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
340113f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
340213f1937eSJoe Perches		      (defined($1) || defined($2))))) {
3403a82603a8SAndrew Jeffery			$is_patch = 1;
340413f1937eSJoe Perches			$reported_maintainer_file = 1;
340513f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
340613f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
340713f1937eSJoe Perches		}
340813f1937eSJoe Perches
3409e400edb1SRob Herring# Check for adding new DT bindings not in schema format
3410e400edb1SRob Herring		if (!$in_commit_log &&
3411e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3412e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3413e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
341456ddc4cdSMauro Carvalho Chehab			     "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
3415e400edb1SRob Herring		}
3416e400edb1SRob Herring
341700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
34188905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3419000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
3420000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
34216c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
3422de7d4f0eSAndy Whitcroft		}
3423de7d4f0eSAndy Whitcroft
3424de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3425de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3426171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
3427171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3428171ae1a4SAndy Whitcroft
3429171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
3430171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3431171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
3432171ae1a4SAndy Whitcroft
343334d99219SJoe Perches			CHK("INVALID_UTF8",
3434000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
343500df344fSAndy Whitcroft		}
34360a920b5bSAndy Whitcroft
343715662b3eSJoe Perches# Check if it's the start of a commit log
343815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
343915662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
3440eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
3441eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
344215662b3eSJoe Perches			$in_header_lines = 0;
344315662b3eSJoe Perches			$in_commit_log = 1;
3444ed43c4e5SAllen Hubbe			$has_commit_log = 1;
344515662b3eSJoe Perches		}
344615662b3eSJoe Perches
3447fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
3448fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
3449fa64205dSPasi Savanainen		if ($in_header_lines &&
3450fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3451fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
3452fa64205dSPasi Savanainen			$non_utf8_charset = 1;
3453fa64205dSPasi Savanainen		}
3454fa64205dSPasi Savanainen
3455fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
345615662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
3457fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
345815662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
345915662b3eSJoe Perches		}
346015662b3eSJoe Perches
3461d6430f71SJoe Perches# Check for absolute kernel paths in commit message
3462d6430f71SJoe Perches		if ($tree && $in_commit_log) {
3463d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
3464d6430f71SJoe Perches				my $file = $1;
3465d6430f71SJoe Perches
3466d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3467d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
3468d6430f71SJoe Perches					#
3469d6430f71SJoe Perches				} else {
3470d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
3471d6430f71SJoe Perches				}
3472d6430f71SJoe Perches			}
3473d6430f71SJoe Perches		}
3474d6430f71SJoe Perches
347566b47b4aSKees Cook# Check for various typo / spelling mistakes
347666d7a382SJoe Perches		if (defined($misspellings) &&
347766d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
34787da07c31SDwaipayan Ray			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
347966b47b4aSKees Cook				my $typo = $1;
34807da07c31SDwaipayan Ray				my $blank = copy_spacing($rawline);
34817da07c31SDwaipayan Ray				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
34827da07c31SDwaipayan Ray				my $hereptr = "$hereline$ptr\n";
348366b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
348466b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
348566b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
34860675a8fbSJean Delvare				my $msg_level = \&WARN;
34870675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
34880675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
34897da07c31SDwaipayan Ray						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
349066b47b4aSKees Cook				    $fix) {
349166b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
349266b47b4aSKees Cook				}
349366b47b4aSKees Cook			}
349466b47b4aSKees Cook		}
349566b47b4aSKees Cook
3496a8dd86bfSMatteo Croce# check for invalid commit id
3497a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3498a8dd86bfSMatteo Croce			my $id;
3499a8dd86bfSMatteo Croce			my $description;
3500a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
3501a8dd86bfSMatteo Croce			if (!defined($id)) {
3502a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
3503a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3504a8dd86bfSMatteo Croce			}
3505a8dd86bfSMatteo Croce		}
3506a8dd86bfSMatteo Croce
3507310cd06bSJoe Perches# check for repeated words separated by a single space
35088d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
35098d0325ccSAditya Srivastava		if (($rawline =~ /^\+/ || $in_commit_log) &&
35108d0325ccSAditya Srivastava		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
35111db81a68SDwaipayan Ray			pos($rawline) = 1 if (!$in_commit_log);
3512310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3513310cd06bSJoe Perches
3514310cd06bSJoe Perches				my $first = $1;
3515310cd06bSJoe Perches				my $second = $2;
35161db81a68SDwaipayan Ray				my $start_pos = $-[1];
35171db81a68SDwaipayan Ray				my $end_pos = $+[2];
3518310cd06bSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3519310cd06bSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3520310cd06bSJoe Perches					next;
3521310cd06bSJoe Perches				}
3522310cd06bSJoe Perches
35231db81a68SDwaipayan Ray				next if (lc($first) ne lc($second));
3524310cd06bSJoe Perches				next if ($first eq 'long');
3525310cd06bSJoe Perches
35261db81a68SDwaipayan Ray				# check for character before and after the word matches
35271db81a68SDwaipayan Ray				my $start_char = '';
35281db81a68SDwaipayan Ray				my $end_char = '';
35291db81a68SDwaipayan Ray				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
35301db81a68SDwaipayan Ray				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
35311db81a68SDwaipayan Ray
35321db81a68SDwaipayan Ray				next if ($start_char =~ /^\S$/);
35331db81a68SDwaipayan Ray				next if (index(" \t.,;?!", $end_char) == -1);
35341db81a68SDwaipayan Ray
35358d0325ccSAditya Srivastava				# avoid repeating hex occurrences like 'ff ff fe 09 ...'
35368d0325ccSAditya Srivastava				if ($first =~ /\b[0-9a-f]{2,}\b/i) {
35378d0325ccSAditya Srivastava					next if (!exists($allow_repeated_words{lc($first)}));
35388d0325ccSAditya Srivastava				}
35398d0325ccSAditya Srivastava
3540310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3541310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3542310cd06bSJoe Perches				    $fix) {
3543310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3544310cd06bSJoe Perches				}
3545310cd06bSJoe Perches			}
3546310cd06bSJoe Perches
3547310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3548310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3549310cd06bSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3550310cd06bSJoe Perches				my $last_word = $1;
3551310cd06bSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3552310cd06bSJoe Perches					if (WARN("REPEATED_WORD",
3553310cd06bSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3554310cd06bSJoe Perches					    $fix) {
3555310cd06bSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3556310cd06bSJoe Perches					}
3557310cd06bSJoe Perches				}
3558310cd06bSJoe Perches			}
3559310cd06bSJoe Perches		}
3560310cd06bSJoe Perches
356130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
356230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
356300df344fSAndy Whitcroft
35640a920b5bSAndy Whitcroft#trailing whitespace
35659c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
3566c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3567d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3568d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
3569d5e616fcSJoe Perches			    $fix) {
3570194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3571d5e616fcSJoe Perches			}
3572c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3573c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35743705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
35753705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
35763705ce5bSJoe Perches			    $fix) {
3577194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
35783705ce5bSJoe Perches			}
35793705ce5bSJoe Perches
3580d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
35810a920b5bSAndy Whitcroft		}
35825368df20SAndy Whitcroft
35834783f894SJosh Triplett# Check for FSF mailing addresses.
3584109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
35851bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
35863e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
35873e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
35884783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35890675a8fbSJean Delvare			my $msg_level = \&ERROR;
35900675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
35910675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
35924783f894SJosh 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)
35934783f894SJosh Triplett		}
35944783f894SJosh Triplett
35953354957aSAndi Kleen# check for Kconfig help text having a real description
35969fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
35979fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
35983354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3599678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3600678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3601678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3602678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3603b8709bceSJoe Perches			my $ln = $linenr;
3604b8709bceSJoe Perches			my $needs_help = 0;
3605b8709bceSJoe Perches			my $has_help = 0;
3606b8709bceSJoe Perches			my $help_length = 0;
3607b8709bceSJoe Perches			while (defined $lines[$ln]) {
3608b8709bceSJoe Perches				my $f = $lines[$ln++];
36099fe287d7SAndy Whitcroft
36109fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
3611b8709bceSJoe Perches				last if ($f !~ /^[\+ ]/);	# !patch context
3612a1385803SAndy Whitcroft
3613b8709bceSJoe Perches				if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3614b8709bceSJoe Perches					$needs_help = 1;
3615b8709bceSJoe Perches					next;
3616b8709bceSJoe Perches				}
3617b8709bceSJoe Perches				if ($f =~ /^\+\s*help\s*$/) {
3618b8709bceSJoe Perches					$has_help = 1;
3619b8709bceSJoe Perches					next;
3620a1385803SAndy Whitcroft				}
3621a1385803SAndy Whitcroft
3622b8709bceSJoe Perches				$f =~ s/^.//;	# strip patch context [+ ]
3623b8709bceSJoe Perches				$f =~ s/#.*//;	# strip # directives
3624b8709bceSJoe Perches				$f =~ s/^\s+//;	# strip leading blanks
3625b8709bceSJoe Perches				next if ($f =~ /^$/);	# skip blank lines
3626678ae162SUlf Magnusson
3627b8709bceSJoe Perches				# At the end of this Kconfig block:
3628678ae162SUlf Magnusson				# This only checks context lines in the patch
3629678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3630678ae162SUlf Magnusson				# positives, even though some of these are
3631678ae162SUlf Magnusson				# common words in help texts
3632b8709bceSJoe Perches				if ($f =~ /^(?:config|menuconfig|choice|endchoice|
3633678ae162SUlf Magnusson					       if|endif|menu|endmenu|source)\b/x) {
36349fe287d7SAndy Whitcroft					last;
36359fe287d7SAndy Whitcroft				}
3636b8709bceSJoe Perches				$help_length++ if ($has_help);
36373354957aSAndi Kleen			}
3638b8709bceSJoe Perches			if ($needs_help &&
3639b8709bceSJoe Perches			    $help_length < $min_conf_desc_length) {
3640b8709bceSJoe Perches				my $stat_real = get_stat_real($linenr, $ln - 1);
3641000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
3642b8709bceSJoe Perches				     "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
364356193274SVadim Bendebury			}
36443354957aSAndi Kleen		}
36453354957aSAndi Kleen
36467ccf41a8SJoe Perches# check MAINTAINERS entries
36477ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
36487ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
36497ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3650628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3651628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3652628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3653628f91a2SJoe Perches				    $fix) {
3654628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3655628f91a2SJoe Perches				}
3656628f91a2SJoe Perches			}
36577ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
36587ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
36597ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
36607ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
36617ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
36627ccf41a8SJoe Perches				my $cur = $1;
36637ccf41a8SJoe Perches				my $curval = $2;
36647ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
36657ccf41a8SJoe Perches				my $prev = $1;
36667ccf41a8SJoe Perches				my $prevval = $2;
36677ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
36687ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
36697ccf41a8SJoe Perches				if ($curindex < 0) {
36707ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
36717ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
36727ccf41a8SJoe Perches				} else {
36737ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
36747ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
36757ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
36767ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
36777ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
36787ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
36797ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
36807ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
36817ccf41a8SJoe Perches					}
36827ccf41a8SJoe Perches				}
36837ccf41a8SJoe Perches			}
36847ccf41a8SJoe Perches		}
3685628f91a2SJoe Perches
3686c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3687c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3688c68e5878SArnaud Lacombe			my $flag = $1;
3689c68e5878SArnaud Lacombe			my $replacement = {
3690c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3691c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3692c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3693c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3694c68e5878SArnaud Lacombe			};
3695c68e5878SArnaud Lacombe
3696c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3697c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3698c68e5878SArnaud Lacombe		}
3699c68e5878SArnaud Lacombe
3700bff5da43SRob Herring# check for DT compatible documentation
37017dd05b38SFlorian Vaussard		if (defined $root &&
37027dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
37037dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
37047dd05b38SFlorian Vaussard
3705bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3706bff5da43SRob Herring
3707cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3708852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3709cc93319bSFlorian Vaussard
3710bff5da43SRob Herring			foreach my $compat (@compats) {
3711bff5da43SRob Herring				my $compat2 = $compat;
3712185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3713185d566bSRob Herring				my $compat3 = $compat;
3714185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3715185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3716bff5da43SRob Herring				if ( $? >> 8 ) {
3717bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3718bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3719bff5da43SRob Herring				}
3720bff5da43SRob Herring
37214fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
37224fbf32a6SFlorian Vaussard				my $vendor = $1;
3723852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3724bff5da43SRob Herring				if ( $? >> 8 ) {
3725bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3726cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3727bff5da43SRob Herring				}
3728bff5da43SRob Herring			}
3729bff5da43SRob Herring		}
3730bff5da43SRob Herring
37319f3a8992SRob Herring# check for using SPDX license tag at beginning of files
37329f3a8992SRob Herring		if ($realline == $checklicenseline) {
37339f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
37349f3a8992SRob Herring				$checklicenseline = 2;
37359f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
37369f3a8992SRob Herring				my $comment = "";
37379f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
37389f3a8992SRob Herring					$comment = '/*';
3739d1d84b5fSMiguel Ojeda				} elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
37409f3a8992SRob Herring					$comment = '//';
3741c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
37429f3a8992SRob Herring					$comment = '#';
37439f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
37449f3a8992SRob Herring					$comment = '..';
37459f3a8992SRob Herring				}
37469f3a8992SRob Herring
3747fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3748fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3749fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3750ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3751fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3752fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3753fdf13693SJoe Perches				}
3754fdf13693SJoe Perches
37559f3a8992SRob Herring				if ($comment !~ /^$/ &&
3756ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
37579f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
37589f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
37593b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
37603b6e8ac9SJoe Perches					my $spdx_license = $1;
37613b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
37623b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
37633b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
37643b6e8ac9SJoe Perches					}
376550c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
376650c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
376750c92900SLubomir Rintel						my $msg_level = \&WARN;
376850c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
376950c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
377050c92900SLubomir Rintel
377150c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
377250c92900SLubomir Rintel						    $fix) {
377350c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
377450c92900SLubomir Rintel						}
377550c92900SLubomir Rintel					}
37769f3a8992SRob Herring				}
37779f3a8992SRob Herring			}
37789f3a8992SRob Herring		}
37799f3a8992SRob Herring
3780a0154cdbSJoe Perches# check for embedded filenames
378136217357SJoe Perches		if ($rawline =~ /^\+.*\b\Q$realfile\E\b/) {
3782a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3783a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3784a0154cdbSJoe Perches		}
3785a0154cdbSJoe Perches
37865368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3787d1d84b5fSMiguel Ojeda		next if ($realfile !~ /\.(h|c|rs|s|S|sh|dtsi|dts)$/);
37885368df20SAndy Whitcroft
3789a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3790a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3791a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3792a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3793a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3794a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3795a8da38a9SJoe Perches		}
3796a8da38a9SJoe Perches
379747e0c88bSJoe Perches# line length limit (with some exclusions)
379847e0c88bSJoe Perches#
379947e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
380047e0c88bSJoe Perches#	logging functions like pr_info that end in a string
380147e0c88bSJoe Perches#	lines with a single string
380247e0c88bSJoe Perches#	#defines that are a single string
38032e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
380447e0c88bSJoe Perches#
380547e0c88bSJoe Perches# There are 3 different line length message types:
3806ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
380747e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
380847e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
380947e0c88bSJoe Perches#
381047e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
381147e0c88bSJoe Perches#
381247e0c88bSJoe Perches
3813b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
381447e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
381547e0c88bSJoe Perches
381647e0c88bSJoe Perches			# Check the allowed long line types first
381747e0c88bSJoe Perches
381847e0c88bSJoe Perches			# logging functions that end in a string that starts
381947e0c88bSJoe Perches			# before $max_line_length
382047e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
382147e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
382247e0c88bSJoe Perches				$msg_type = "";
382347e0c88bSJoe Perches
382447e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
382547e0c88bSJoe Perches			# #defines with only strings
382647e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
382747e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
382847e0c88bSJoe Perches				$msg_type = "";
382947e0c88bSJoe Perches
3830cc147506SJoe Perches			# More special cases
3831cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3832cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3833d560a5f8SJoe Perches				$msg_type = "";
3834d560a5f8SJoe Perches
38352e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
38362e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
38372e4bbbc5SAndreas Brauchli				$msg_type = "";
38382e4bbbc5SAndreas Brauchli
383947e0c88bSJoe Perches			# Otherwise set the alternate message types
384047e0c88bSJoe Perches
384147e0c88bSJoe Perches			# a comment starts before $max_line_length
384247e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
384347e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
384447e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
384547e0c88bSJoe Perches
384647e0c88bSJoe Perches			# a quoted string starts before $max_line_length
384747e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
384847e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
384947e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
385047e0c88bSJoe Perches			}
385147e0c88bSJoe Perches
385247e0c88bSJoe Perches			if ($msg_type ne "" &&
385347e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3854bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3855bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3856bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3857bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
38580a920b5bSAndy Whitcroft			}
385947e0c88bSJoe Perches		}
38600a920b5bSAndy Whitcroft
38618905a67cSAndy Whitcroft# check for adding lines without a newline.
38628905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
386347ca69b8STom Rix			if (WARN("MISSING_EOF_NEWLINE",
386447ca69b8STom Rix			         "adding a line without newline at end of file\n" . $herecurr) &&
386547ca69b8STom Rix			    $fix) {
386647ca69b8STom Rix				fix_delete_line($fixlinenr+1, "No newline at end of file");
386747ca69b8STom Rix			}
38688905a67cSAndy Whitcroft		}
38698905a67cSAndy Whitcroft
3870de93245cSAditya Srivastava# check for .L prefix local symbols in .S files
3871de93245cSAditya Srivastava		if ($realfile =~ /\.S$/ &&
3872de93245cSAditya Srivastava		    $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3873de93245cSAditya Srivastava			WARN("AVOID_L_PREFIX",
3874f4bf1cd4SJonathan Corbet			     "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/core-api/asm-annotations.rst\n" . $herecurr);
3875de93245cSAditya Srivastava		}
3876de93245cSAditya Srivastava
3877b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3878de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
38790a920b5bSAndy Whitcroft
38800a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3881713a09deSAntonio Borneo# more than $tabsize must use tabs.
3882c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3883c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3884c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3885d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
38863705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
38873705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
38883705ce5bSJoe Perches			    $fix) {
3889194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
38903705ce5bSJoe Perches			}
38910a920b5bSAndy Whitcroft		}
38920a920b5bSAndy Whitcroft
389308e44365SAlberto Panizzo# check for space before tabs.
389408e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
389508e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
38963705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
38973705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
38983705ce5bSJoe Perches			    $fix) {
3899194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3900713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3901194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3902c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
39033705ce5bSJoe Perches			}
390408e44365SAlberto Panizzo		}
390508e44365SAlberto Panizzo
39066a487211SJoe Perches# check for assignments on the start of a line
39076a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3908da7355abSAditya Srivastava			my $operator = $1;
3909da7355abSAditya Srivastava			if (CHK("ASSIGNMENT_CONTINUATIONS",
3910da7355abSAditya Srivastava				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3911da7355abSAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
3912da7355abSAditya Srivastava				# add assignment operator to the previous line, remove from current line
3913da7355abSAditya Srivastava				$fixed[$fixlinenr - 1] .= " $operator";
3914da7355abSAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3915da7355abSAditya Srivastava			}
39166a487211SJoe Perches		}
39176a487211SJoe Perches
3918d1fe9c09SJoe Perches# check for && or || at the start of a line
3919d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
39208e08f076SAditya Srivastava			my $operator = $1;
39218e08f076SAditya Srivastava			if (CHK("LOGICAL_CONTINUATIONS",
39228e08f076SAditya Srivastava				"Logical continuations should be on the previous line\n" . $hereprev) &&
39238e08f076SAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
39248e08f076SAditya Srivastava				# insert logical operator at last non-comment, non-whitepsace char on previous line
39258e08f076SAditya Srivastava				$prevline =~ /[\s$;]*$/;
39268e08f076SAditya Srivastava				my $line_end = substr($prevrawline, $-[0]);
39278e08f076SAditya Srivastava				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
39288e08f076SAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
39298e08f076SAditya Srivastava			}
3930d1fe9c09SJoe Perches		}
3931d1fe9c09SJoe Perches
3932a91e8994SJoe Perches# check indentation starts on a tab stop
39335b57980dSJoe Perches		if ($perl_version_ok &&
3934bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3935a91e8994SJoe Perches			my $indent = length($1);
3936713a09deSAntonio Borneo			if ($indent % $tabsize) {
3937a91e8994SJoe Perches				if (WARN("TABSTOP",
3938a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3939a91e8994SJoe Perches				    $fix) {
3940713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3941a91e8994SJoe Perches				}
3942a91e8994SJoe Perches			}
3943a91e8994SJoe Perches		}
3944a91e8994SJoe Perches
3945d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
39465b57980dSJoe Perches		if ($perl_version_ok &&
3947fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3948d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3949d1fe9c09SJoe Perches			my $oldindent = $1;
3950d1fe9c09SJoe Perches			my $rest = $2;
3951d1fe9c09SJoe Perches
3952d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3953d1fe9c09SJoe Perches			if ($pos >= 0) {
3954b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3955b34a26f3SJoe Perches				my $newindent = $2;
3956d1fe9c09SJoe Perches
3957d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3958713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3959713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3960d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3961d1fe9c09SJoe Perches
3962d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3963d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
39643705ce5bSJoe Perches
39653705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
39663705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
39673705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3968194f66fcSJoe Perches						$fixed[$fixlinenr] =~
39693705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
39703705ce5bSJoe Perches					}
3971d1fe9c09SJoe Perches				}
3972d1fe9c09SJoe Perches			}
3973d1fe9c09SJoe Perches		}
3974d1fe9c09SJoe Perches
39756ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
39766ab3a970SJoe Perches# avoid checking a few false positives:
39776ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
39786ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
39796ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
39806ab3a970SJoe Perches#   multiline macros that define functions
39816ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
39826ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
39836ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
39843705ce5bSJoe Perches			if (CHK("SPACING",
3985f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
39863705ce5bSJoe Perches			    $fix) {
3987194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3988f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
39893705ce5bSJoe Perches			}
3990aad4f614SJoe Perches		}
3991aad4f614SJoe Perches
399286406b1cSJoe Perches# Block comment styles
399386406b1cSJoe Perches# Networking with an initial /*
399405880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3995fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
399685ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
3997c70735c2SŁukasz Stelmach		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
399805880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
399905880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
400005880600SJoe Perches		}
400105880600SJoe Perches
400286406b1cSJoe Perches# Block comments use * on subsequent lines
400386406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
400486406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
4005a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
400661135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
4007a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
400886406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
400986406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
4010a605e32eSJoe Perches		}
4011a605e32eSJoe Perches
401286406b1cSJoe Perches# Block comments use */ on trailing lines
401386406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
4014c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
4015c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
4016c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
401786406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
401886406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
401905880600SJoe Perches		}
402005880600SJoe Perches
402108eb9b80SJoe Perches# Block comment * alignment
402208eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
4023af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
4024af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
4025af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
402608eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
4027af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
4028af207524SJoe Perches			my $oldindent;
402908eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
4030af207524SJoe Perches			if (defined($1)) {
4031af207524SJoe Perches				$oldindent = expand_tabs($1);
4032af207524SJoe Perches			} else {
4033af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
4034af207524SJoe Perches				$oldindent = expand_tabs($1);
4035af207524SJoe Perches			}
403608eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
403708eb9b80SJoe Perches			my $newindent = $1;
403808eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
4039af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
404008eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
404108eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
404208eb9b80SJoe Perches			}
404308eb9b80SJoe Perches		}
404408eb9b80SJoe Perches
40457f619191SJoe Perches# check for missing blank lines after struct/union declarations
40467f619191SJoe Perches# with exceptions for various attributes and macros
40477f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
40487f619191SJoe Perches		    $line =~ /^\+/ &&
40497f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
405005dc40e6SJoe Perches		      $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param)/ ||
40517f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
40527f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
40537f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
40547f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
40557f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
40560bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
40577f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
4058d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
4059d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
4060d752fcc8SJoe Perches			    $fix) {
4061f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
4062d752fcc8SJoe Perches			}
40637f619191SJoe Perches		}
40647f619191SJoe Perches
4065365dd4eaSJoe Perches# check for multiple consecutive blank lines
4066365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
4067365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
4068365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
4069d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
4070d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
4071d752fcc8SJoe Perches			    $fix) {
4072f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4073d752fcc8SJoe Perches			}
4074d752fcc8SJoe Perches
4075365dd4eaSJoe Perches			$last_blank_line = $linenr;
4076365dd4eaSJoe Perches		}
4077365dd4eaSJoe Perches
40783b617e3bSJoe Perches# check for missing blank lines after declarations
4079b5e8736aSJoe Perches# (declarations must have the same indentation and not be at the start of line)
4080b5e8736aSJoe Perches		if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
4081b5e8736aSJoe Perches			# use temporaries
4082b5e8736aSJoe Perches			my $sl = $sline;
4083b5e8736aSJoe Perches			my $pl = $prevline;
4084b5e8736aSJoe Perches			# remove $Attribute/$Sparse uses to simplify comparisons
4085b5e8736aSJoe Perches			$sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4086b5e8736aSJoe Perches			$pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4087b5e8736aSJoe Perches			if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
40885a4e1fd3SJoe Perches			# function pointer declarations
4089b5e8736aSJoe Perches			     $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
40903f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
4091b5e8736aSJoe Perches			     $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
40923f7bac03SJoe Perches			# known declaration macros
4093b5e8736aSJoe Perches			     $pl =~ /^\+\s+$declaration_macros/) &&
40943f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
4095b5e8736aSJoe Perches			    !($pl =~ /^\+\s+$c90_Keywords\b/ ||
40963f7bac03SJoe Perches			# other possible extensions of declaration lines
4097b5e8736aSJoe Perches			      $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
40983f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
4099b5e8736aSJoe Perches			      $pl =~ /(?:\{\s*|\\)$/) &&
41003f7bac03SJoe Perches			# looks like a declaration
4101b5e8736aSJoe Perches			    !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
41025a4e1fd3SJoe Perches			# function pointer declarations
4103b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
41043f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
4105b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
41063f7bac03SJoe Perches			# known declaration macros
4107b5e8736aSJoe Perches			      $sl =~ /^\+\s+$declaration_macros/ ||
41083f7bac03SJoe Perches			# start of struct or union or enum
4109b5e8736aSJoe Perches			      $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
41103f7bac03SJoe Perches			# start or end of block or continuation of declaration
4111b5e8736aSJoe Perches			      $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
41123f7bac03SJoe Perches			# bitfield continuation
4113b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
41143f7bac03SJoe Perches			# other possible extensions of declaration lines
4115b5e8736aSJoe Perches			      $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
4116d752fcc8SJoe Perches				if (WARN("LINE_SPACING",
4117d752fcc8SJoe Perches					 "Missing a blank line after declarations\n" . $hereprev) &&
4118d752fcc8SJoe Perches				    $fix) {
4119f2d7e4d4SJoe Perches					fix_insert_line($fixlinenr, "\+");
4120d752fcc8SJoe Perches				}
41213b617e3bSJoe Perches			}
4122b5e8736aSJoe Perches		}
41233b617e3bSJoe Perches
41245f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
41256b4c5bebSAndy Whitcroft# Exceptions:
41266b4c5bebSAndy Whitcroft#  1) within comments
41276b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
41286b4c5bebSAndy Whitcroft#  3) hanging labels
41293705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
41305f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
41313705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
41323705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
41333705ce5bSJoe Perches			    $fix) {
4134194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
41353705ce5bSJoe Perches			}
41365f7ddae6SRaffaele Recalcati		}
41375f7ddae6SRaffaele Recalcati
4138b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
4139b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
4140b9ea10d6SAndy Whitcroft
41415751a24eSJoe Perches# check for unusual line ending [ or (
41425751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
41435751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
41445751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
41455751a24eSJoe Perches		}
41465751a24eSJoe Perches
41474dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
41484dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
41494dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
41504dbed76fSJoe Perches			$context_function = $1;
41514dbed76fSJoe Perches		}
41524dbed76fSJoe Perches
41534dbed76fSJoe Perches# check if this appears to be the end of function declaration
41544dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
41554dbed76fSJoe Perches			undef $context_function;
41564dbed76fSJoe Perches		}
41574dbed76fSJoe Perches
4158032a4c0fSJoe Perches# check indentation of any line with a bare else
4159840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
4160032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
4161032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4162032a4c0fSJoe Perches			my $tabs = length($1) + 1;
4163840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4164840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4165840080a0SJoe Perches			     defined $lines[$linenr] &&
4166840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
4167032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
4168032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
4169032a4c0fSJoe Perches			}
4170032a4c0fSJoe Perches		}
4171032a4c0fSJoe Perches
4172c00df19aSJoe Perches# check indentation of a line with a break;
4173dc58bc55SJoe Perches# if the previous line is a goto, return or break
4174dc58bc55SJoe Perches# and is indented the same # of tabs
4175c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4176c00df19aSJoe Perches			my $tabs = $1;
4177dc58bc55SJoe Perches			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4178dc58bc55SJoe Perches				if (WARN("UNNECESSARY_BREAK",
4179dc58bc55SJoe Perches					 "break is not useful after a $1\n" . $hereprev) &&
4180dc58bc55SJoe Perches				    $fix) {
4181dc58bc55SJoe Perches					fix_delete_line($fixlinenr, $rawline);
4182dc58bc55SJoe Perches				}
4183c00df19aSJoe Perches			}
4184c00df19aSJoe Perches		}
4185c00df19aSJoe Perches
4186c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
4187cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
4188000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
4189000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
4190c2fdda0dSAndy Whitcroft		}
419122f2a2efSAndy Whitcroft
419256e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
419356e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
419456e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
419556e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
419656e77d70SJoe Perches		}
419756e77d70SJoe Perches
41989c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
41992b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
42002b474a1aSAndy Whitcroft		    $realline_next);
42013e469cdcSAndy Whitcroft#print "LINE<$line>\n";
4202ca819864SJoe Perches		if ($linenr > $suppress_statement &&
42031b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
4204170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4205f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4206171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
4207171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
4208171ae1a4SAndy Whitcroft
42093e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
42103e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
42113e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
42123e469cdcSAndy Whitcroft			# until we hit end of it.
42133e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
42143e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
42153e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
42163e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
42173e469cdcSAndy Whitcroft			}
4218f74bd194SAndy Whitcroft
42192b474a1aSAndy Whitcroft			# Find the real next line.
42202b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
42212b474a1aSAndy Whitcroft			if (defined $realline_next &&
42222b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
42232b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
42242b474a1aSAndy Whitcroft				$realline_next++;
42252b474a1aSAndy Whitcroft			}
42262b474a1aSAndy Whitcroft
4227171ae1a4SAndy Whitcroft			my $s = $stat;
4228171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
4229cf655043SAndy Whitcroft
4230c2fdda0dSAndy Whitcroft			# Ignore goto labels.
4231171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
4232c2fdda0dSAndy Whitcroft
4233c2fdda0dSAndy Whitcroft			# Ignore functions being called
4234171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
4235c2fdda0dSAndy Whitcroft
4236463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
4237463f2864SAndy Whitcroft
4238c45dcabdSAndy Whitcroft			# declarations always start with types
4239d2506586SAndy 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) {
4240c45dcabdSAndy Whitcroft				my $type = $1;
4241c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
4242c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
4243c45dcabdSAndy Whitcroft
42446c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
4245a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
4246c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
4247c2fdda0dSAndy Whitcroft			}
42488905a67cSAndy Whitcroft
42496c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
425065863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
4251c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
42529c0ca6f9SAndy Whitcroft			}
42538905a67cSAndy Whitcroft
42548905a67cSAndy Whitcroft			# Check for any sort of function declaration.
42558905a67cSAndy Whitcroft			# int foo(something bar, other baz);
42568905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
4257171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
42588905a67cSAndy Whitcroft				my ($name_len) = length($1);
42598905a67cSAndy Whitcroft
4260cf655043SAndy Whitcroft				my $ctx = $s;
4261773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
42628905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
4263cf655043SAndy Whitcroft
42648905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
4265c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
42668905a67cSAndy Whitcroft
4267c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
42688905a67cSAndy Whitcroft					}
42698905a67cSAndy Whitcroft				}
42708905a67cSAndy Whitcroft			}
42718905a67cSAndy Whitcroft
42729c0ca6f9SAndy Whitcroft		}
42739c0ca6f9SAndy Whitcroft
427400df344fSAndy Whitcroft#
427500df344fSAndy Whitcroft# Checks which may be anchored in the context.
427600df344fSAndy Whitcroft#
427700df344fSAndy Whitcroft
427800df344fSAndy Whitcroft# Check for switch () and associated case and default
427900df344fSAndy Whitcroft# statements should be at the same indent.
428000df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
428100df344fSAndy Whitcroft			my $err = '';
428200df344fSAndy Whitcroft			my $sep = '';
428300df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
428400df344fSAndy Whitcroft			shift(@ctx);
428500df344fSAndy Whitcroft			for my $ctx (@ctx) {
428600df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
428700df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
428800df344fSAndy Whitcroft							$indent != $cindent) {
428900df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
429000df344fSAndy Whitcroft					$sep = '';
429100df344fSAndy Whitcroft				} else {
429200df344fSAndy Whitcroft					$sep = "[...]\n";
429300df344fSAndy Whitcroft				}
429400df344fSAndy Whitcroft			}
429500df344fSAndy Whitcroft			if ($err ne '') {
4296000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
4297000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
4298de7d4f0eSAndy Whitcroft			}
4299de7d4f0eSAndy Whitcroft		}
4300de7d4f0eSAndy Whitcroft
4301de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
4302de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
43030fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4304773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
4305773647a0SAndy Whitcroft
43069c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
43078eef05ddSJoe Perches
43088eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
43098eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
43108eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
43118eef05ddSJoe Perches			}
43128eef05ddSJoe Perches
4313de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
4314de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
4315de7d4f0eSAndy Whitcroft
4316548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
4317548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
4318de7d4f0eSAndy Whitcroft
4319548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4320548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
4321548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
4322548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4323548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4324773647a0SAndy Whitcroft				$ctx_ln++;
4325773647a0SAndy Whitcroft			}
4326548596d5SAndy Whitcroft
432753210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
432853210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4329773647a0SAndy Whitcroft
4330773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4331000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
4332000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
433301464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
433400df344fSAndy Whitcroft			}
4335773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4336773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
4337773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
4338773647a0SAndy Whitcroft			{
43399c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
43409c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
4341000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
4342000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
434301464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
43449c0ca6f9SAndy Whitcroft				}
43459c0ca6f9SAndy Whitcroft			}
434600df344fSAndy Whitcroft		}
434700df344fSAndy Whitcroft
43484d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
4349f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
43503e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
43513e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
43523e469cdcSAndy Whitcroft					if (!defined $stat);
43534d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
43544d001e4dSAndy Whitcroft
43554d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
43564d001e4dSAndy Whitcroft
43579f5af480SJoe Perches			# remove inline comments
43589f5af480SJoe Perches			$s =~ s/$;/ /g;
43599f5af480SJoe Perches			$c =~ s/$;/ /g;
43604d001e4dSAndy Whitcroft
43614d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
43626f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
43636f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
43644d001e4dSAndy Whitcroft
43659f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
43669f5af480SJoe Perches			# none on the first line, and are going to readd them
43679f5af480SJoe Perches			# where necessary.
43689f5af480SJoe Perches			$s =~ s/\n./\n/gs;
43699f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
43709f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
43719f5af480SJoe Perches			}
43729f5af480SJoe Perches
43734d001e4dSAndy Whitcroft			# We want to check the first line inside the block
43744d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
43754d001e4dSAndy Whitcroft			#  1) any blank line termination
43764d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
43774d001e4dSAndy Whitcroft			#  3) any do (...) {
43784d001e4dSAndy Whitcroft			my $continuation = 0;
43794d001e4dSAndy Whitcroft			my $check = 0;
43804d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
43814d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
43824d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
43834d001e4dSAndy Whitcroft				$continuation = 1;
43844d001e4dSAndy Whitcroft			}
43859bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
43864d001e4dSAndy Whitcroft				$check = 1;
43874d001e4dSAndy Whitcroft				$cond_lines++;
43884d001e4dSAndy Whitcroft			}
43894d001e4dSAndy Whitcroft
43904d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
43914d001e4dSAndy Whitcroft			# preprocessor statement.
43924d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
43934d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
43944d001e4dSAndy Whitcroft				$check = 0;
43954d001e4dSAndy Whitcroft			}
43964d001e4dSAndy Whitcroft
43979bd49efeSAndy Whitcroft			my $cond_ptr = -1;
4398740504c6SAndy Whitcroft			$continuation = 0;
43999bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
44009bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
44014d001e4dSAndy Whitcroft
4402f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
4403f16fa28fSAndy Whitcroft				# is not linear.
4404f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4405f16fa28fSAndy Whitcroft					$check = 0;
4406f16fa28fSAndy Whitcroft				}
4407f16fa28fSAndy Whitcroft
44089bd49efeSAndy Whitcroft				# Ignore:
44099bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
44109bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
44119bd49efeSAndy Whitcroft				#  3) labels.
4412740504c6SAndy Whitcroft				if ($continuation ||
4413740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
44149bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
44159bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
4416740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
441730dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
44189bd49efeSAndy Whitcroft						$cond_lines++;
44199bd49efeSAndy Whitcroft					}
44204d001e4dSAndy Whitcroft				}
442130dad6ebSAndy Whitcroft			}
44224d001e4dSAndy Whitcroft
44234d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
44244d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
44254d001e4dSAndy Whitcroft
44264d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
44274d001e4dSAndy Whitcroft			# this is not this patch's fault.
44284d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
44294d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
44304d001e4dSAndy Whitcroft				$check = 0;
44314d001e4dSAndy Whitcroft			}
44324d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
44334d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
44344d001e4dSAndy Whitcroft			}
44354d001e4dSAndy Whitcroft
44369bd49efeSAndy 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";
44374d001e4dSAndy Whitcroft
44389f5af480SJoe Perches			if ($check && $s ne '' &&
4439713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
44409f5af480SJoe Perches			     ($sindent < $indent) ||
4441f6950a73SJoe Perches			     ($sindent == $indent &&
4442f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4443713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
4444000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
4445000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
44464d001e4dSAndy Whitcroft			}
44474d001e4dSAndy Whitcroft		}
44484d001e4dSAndy Whitcroft
44496c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
44506c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
44511f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
44521f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
44536c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
4454c2fdda0dSAndy Whitcroft		if ($dbg_values) {
4455c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
4456cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
4457cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
44581f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
4459c2fdda0dSAndy Whitcroft		}
44606c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
44616c72ffaaSAndy Whitcroft
446200df344fSAndy Whitcroft#ignore lines not being added
44633705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
446400df344fSAndy Whitcroft
446599ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
446699ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
446799ca38c2SJoe Perches#	struct foo bar = *(&(bar));
446899ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
446999ca38c2SJoe Perches			my $var = $1;
447099ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
447199ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
447299ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
447399ca38c2SJoe Perches			}
447499ca38c2SJoe Perches		}
447599ca38c2SJoe Perches
447611ca40a0SJoe Perches# check for dereferences that span multiple lines
447711ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
447811ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
447911ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
448011ca40a0SJoe Perches			my $ref = $1;
448111ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
448211ca40a0SJoe Perches			$ref .= $1;
448311ca40a0SJoe Perches			$ref =~ s/\s//g;
448411ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
448511ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
448611ca40a0SJoe Perches		}
448711ca40a0SJoe Perches
4488a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
4489c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4490a1ce18e4SJoe Perches			my $type = $1;
4491a1ce18e4SJoe Perches			my $var = $2;
4492207a8e84SJoe Perches			$var = "" if (!defined $var);
4493207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4494a1ce18e4SJoe Perches				my $sign = $1;
4495a1ce18e4SJoe Perches				my $pointer = $2;
4496a1ce18e4SJoe Perches
4497a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
4498a1ce18e4SJoe Perches
4499a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
4500a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4501a1ce18e4SJoe Perches				    $fix) {
4502a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
4503207a8e84SJoe Perches					my $comp_pointer = $pointer;
4504207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
4505207a8e84SJoe Perches					$decl .= $comp_pointer;
4506207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
4507207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4508a1ce18e4SJoe Perches				}
4509a1ce18e4SJoe Perches			}
4510a1ce18e4SJoe Perches		}
4511a1ce18e4SJoe Perches
4512653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
45137429c690SAndy Whitcroft		if ($dbg_type) {
45147429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
4515000d1cc1SJoe Perches				ERROR("TEST_TYPE",
4516000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
45177429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4518000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
4519000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
45207429c690SAndy Whitcroft			}
4521653d4876SAndy Whitcroft			next;
4522653d4876SAndy Whitcroft		}
4523a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
4524a1ef277eSAndy Whitcroft		if ($dbg_attr) {
45259360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
4526000d1cc1SJoe Perches				ERROR("TEST_ATTR",
4527000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
45289360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4529000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
4530000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
4531a1ef277eSAndy Whitcroft			}
4532a1ef277eSAndy Whitcroft			next;
4533a1ef277eSAndy Whitcroft		}
4534653d4876SAndy Whitcroft
4535f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
453699423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
453799423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
4538d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4539d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4540f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4541f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4542f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4543d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4544d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4545f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4546d752fcc8SJoe Perches				$fixedline = $line;
45478d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4548f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4549d752fcc8SJoe Perches			}
4550f0a594c1SAndy Whitcroft		}
4551f0a594c1SAndy Whitcroft
455200df344fSAndy Whitcroft#
455300df344fSAndy Whitcroft# Checks which are anchored on the added line.
455400df344fSAndy Whitcroft#
455500df344fSAndy Whitcroft
4556653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4557c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4558653d4876SAndy Whitcroft			my $path = $1;
4559653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4560000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4561495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4562495e9d84SJoe Perches			}
4563495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4564495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4565495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4566653d4876SAndy Whitcroft			}
4567653d4876SAndy Whitcroft		}
4568653d4876SAndy Whitcroft
456900df344fSAndy Whitcroft# no C99 // comments
457000df344fSAndy Whitcroft		if ($line =~ m{//}) {
45713705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
45723705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
45733705ce5bSJoe Perches			    $fix) {
4574194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
45753705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
45763705ce5bSJoe Perches					my $comment = trim($1);
4577194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
45783705ce5bSJoe Perches				}
45793705ce5bSJoe Perches			}
458000df344fSAndy Whitcroft		}
458100df344fSAndy Whitcroft		# Remove C99 comments.
45820a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
45836c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
45840a920b5bSAndy Whitcroft
45852b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
45862b474a1aSAndy Whitcroft# the whole statement.
45872b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
45882b474a1aSAndy Whitcroft		if (defined $realline_next &&
45892b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
45902b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
459136794822SChristoph Hellwig		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
45923cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
45933cbf62dfSAndy Whitcroft			# a prefix:
45943cbf62dfSAndy Whitcroft			#   XXX(foo);
45953cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4596653d4876SAndy Whitcroft			my $name = $1;
459770a11659SJoe Perches			$name =~ s/^\s*($Ident).*/$1/;
459887a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
45993cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
46003cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
46013cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
46023cbf62dfSAndy Whitcroft
46033cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
46042b474a1aSAndy Whitcroft				\n.}\s*$|
460548012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
460648012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
460748012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
46082b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
46092b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
461048012058SAndy Whitcroft			    )/x) {
46112b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
46122b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
46132b474a1aSAndy Whitcroft			} else {
46142b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
46150a920b5bSAndy Whitcroft			}
46160a920b5bSAndy Whitcroft		}
46172b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
46182b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
461936794822SChristoph Hellwig		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
46202b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
46212b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
46222b474a1aSAndy Whitcroft		}
46232b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
46242b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4625000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4626000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
46272b474a1aSAndy Whitcroft		}
46280a920b5bSAndy Whitcroft
46295150bda4SJoe Eloff# check for global initialisers.
46305b8f82e1SSong Liu		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
46315b8f82e1SSong Liu		    !exclude_global_initialisers($realfile)) {
4632d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
46336d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4634d5e616fcSJoe Perches			    $fix) {
46356d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4636d5e616fcSJoe Perches			}
4637f0a594c1SAndy Whitcroft		}
46380a920b5bSAndy Whitcroft# check for static initialisers.
46396d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4640d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
46416d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4642d5e616fcSJoe Perches				      $herecurr) &&
4643d5e616fcSJoe Perches			    $fix) {
46446d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4645d5e616fcSJoe Perches			}
46460a920b5bSAndy Whitcroft		}
46470a920b5bSAndy Whitcroft
46481813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
46491813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
46501813087dSJoe Perches			my $tmp = trim($1);
46511813087dSJoe Perches			WARN("MISORDERED_TYPE",
46521813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
46531813087dSJoe Perches		}
46541813087dSJoe Perches
4655809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4656809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4657809e082eSJoe Perches			my $type = trim($1);
4658809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4659809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4660809e082eSJoe Perches			my $new_type = $type;
4661809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4662809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4663809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4664809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4665809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4666809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4667809e082eSJoe Perches			$new_type = trim($new_type);
4668809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4669809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4670809e082eSJoe Perches			    $fix) {
4671809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4672809e082eSJoe Perches			}
4673809e082eSJoe Perches		}
4674809e082eSJoe Perches
4675cb710ecaSJoe Perches# check for static const char * arrays.
4676cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4677000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4678000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4679cb710ecaSJoe Perches				$herecurr);
4680cb710ecaSJoe Perches		}
4681cb710ecaSJoe Perches
468277b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
468377b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
468477b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
468577b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
468677b8c0a8SJoe Perches			    $fix) {
468777b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
468877b8c0a8SJoe Perches			}
468977b8c0a8SJoe Perches		}
469077b8c0a8SJoe Perches
4691cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4692cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4693000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4694000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4695cb710ecaSJoe Perches				$herecurr);
4696cb710ecaSJoe Perches		}
4697cb710ecaSJoe Perches
4698ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4699ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4700ab7e23f3SJoe Perches			my $found = $1;
4701ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4702ab7e23f3SJoe Perches				WARN("CONST_CONST",
4703ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4704ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4705ab7e23f3SJoe Perches				WARN("CONST_CONST",
4706ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4707ab7e23f3SJoe Perches			}
4708ab7e23f3SJoe Perches		}
4709ab7e23f3SJoe Perches
471073169765SJoe Perches# check for const static or static <non ptr type> const declarations
471173169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
471273169765SJoe Perches		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
471373169765SJoe Perches		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
471473169765SJoe Perches			if (WARN("STATIC_CONST",
471573169765SJoe Perches				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
471673169765SJoe Perches			    $fix) {
471773169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
471873169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
471973169765SJoe Perches			}
472073169765SJoe Perches		}
472173169765SJoe Perches
47229b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
47239b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
47249b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
47259b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
47269b0fa60dSJoe Perches				$herecurr);
47279b0fa60dSJoe Perches		}
47289b0fa60dSJoe Perches
4729b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4730b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4731b598b670SJoe Perches			my $array = $1;
4732b598b670SJoe 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*\))@) {
4733b598b670SJoe Perches				my $array_div = $1;
4734b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4735b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4736b598b670SJoe Perches				    $fix) {
4737b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4738b598b670SJoe Perches				}
4739b598b670SJoe Perches			}
4740b598b670SJoe Perches		}
4741b598b670SJoe Perches
4742b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
474316b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4744b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4745b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4746b36190c5SJoe Perches			    $fix) {
4747194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4748b36190c5SJoe Perches			}
4749b36190c5SJoe Perches		}
4750b36190c5SJoe Perches
4751653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4752653d4876SAndy Whitcroft# make sense.
4753653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
47548054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4755c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
47568ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
475746d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4758000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4759000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
47600a920b5bSAndy Whitcroft		}
47610a920b5bSAndy Whitcroft
47620a920b5bSAndy Whitcroft# * goes on variable not on type
476365863862SAndy Whitcroft		# (char*[ const])
4764bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4765bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
47663705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4767d8aaf121SAndy Whitcroft
476865863862SAndy Whitcroft			# Should start with a space.
476965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
477065863862SAndy Whitcroft			# Should not end with a space.
477165863862SAndy Whitcroft			$to =~ s/\s+$//;
477265863862SAndy Whitcroft			# '*'s should not have spaces between.
4773f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
477465863862SAndy Whitcroft			}
4775d8aaf121SAndy Whitcroft
47763705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
477765863862SAndy Whitcroft			if ($from ne $to) {
47783705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
47793705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
47803705ce5bSJoe Perches				    $fix) {
47813705ce5bSJoe Perches					my $sub_from = $ident;
47823705ce5bSJoe Perches					my $sub_to = $ident;
47833705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4784194f66fcSJoe Perches					$fixed[$fixlinenr] =~
47853705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
47863705ce5bSJoe Perches				}
478765863862SAndy Whitcroft			}
4788bfcb2cc7SAndy Whitcroft		}
4789bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4790bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
47913705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4792d8aaf121SAndy Whitcroft
479365863862SAndy Whitcroft			# Should start with a space.
479465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
479565863862SAndy Whitcroft			# Should not end with a space.
479665863862SAndy Whitcroft			$to =~ s/\s+$//;
479765863862SAndy Whitcroft			# '*'s should not have spaces between.
4798f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
479965863862SAndy Whitcroft			}
480065863862SAndy Whitcroft			# Modifiers should have spaces.
480165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
480265863862SAndy Whitcroft
48033705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4804667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
48053705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
48063705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
48073705ce5bSJoe Perches				    $fix) {
48083705ce5bSJoe Perches
48093705ce5bSJoe Perches					my $sub_from = $match;
48103705ce5bSJoe Perches					my $sub_to = $match;
48113705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4812194f66fcSJoe Perches					$fixed[$fixlinenr] =~
48133705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
48143705ce5bSJoe Perches				}
481565863862SAndy Whitcroft			}
48160a920b5bSAndy Whitcroft		}
48170a920b5bSAndy Whitcroft
481869d517e6SDavid Hildenbrand# do not use BUG() or variants
481969d517e6SDavid Hildenbrand		if ($line =~ /\b(?!AA_|BUILD_|DCCP_|IDA_|KVM_|RWLOCK_|snd_|SPIN_)(?:[a-zA-Z_]*_)?BUG(?:_ON)?(?:_[A-Z_]+)?\s*\(/) {
48200675a8fbSJean Delvare			my $msg_level = \&WARN;
48210675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
48220675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
482369d517e6SDavid Hildenbrand				      "Do not crash the kernel unless it is absolutely unavoidable--use WARN_ON_ONCE() plus recovery code (if feasible) instead of BUG() or variants\n" . $herecurr);
48249d3e3c70SJoe Perches		}
48250a920b5bSAndy Whitcroft
48269d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
48278905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4828000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4829000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
48308905a67cSAndy Whitcroft		}
48318905a67cSAndy Whitcroft
483217441227SJoe Perches# check for uses of printk_ratelimit
483317441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4834000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4835000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
483617441227SJoe Perches		}
483717441227SJoe Perches
4838eeef5733SJoe Perches# printk should use KERN_* levels
4839eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4840000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4841eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
484200df344fSAndy Whitcroft		}
48430a920b5bSAndy Whitcroft
4844f5eea3b0SJoe Perches# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4845f5eea3b0SJoe Perches		if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4846f5eea3b0SJoe Perches			my $printk = $1;
4847f5eea3b0SJoe Perches			my $modifier = $2;
4848f5eea3b0SJoe Perches			my $orig = $3;
4849f5eea3b0SJoe Perches			$modifier = "" if (!defined($modifier));
4850243f3803SJoe Perches			my $level = lc($orig);
4851243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
48528f26b837SJoe Perches			my $level2 = $level;
48538f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4854f5eea3b0SJoe Perches			$level .= $modifier;
4855f5eea3b0SJoe Perches			$level2 .= $modifier;
4856243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4857f5eea3b0SJoe Perches			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to $printk(KERN_$orig ...\n" . $herecurr);
4858243f3803SJoe Perches		}
4859243f3803SJoe Perches
4860f5eea3b0SJoe Perches# prefer dev_<level> to dev_printk(KERN_<LEVEL>
4861dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4862dc139313SJoe Perches			my $orig = $1;
4863dc139313SJoe Perches			my $level = lc($orig);
4864dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4865dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4866dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4867dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4868dc139313SJoe Perches		}
4869dc139313SJoe Perches
48708020b253SNicolas Boichat# trace_printk should not be used in production code.
48718020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
48728020b253SNicolas Boichat			WARN("TRACE_PRINTK",
48738020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
48748020b253SNicolas Boichat		}
48758020b253SNicolas Boichat
487691c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
487791c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
487891c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
487991c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
488091c9afafSAndy Lutomirski			WARN("ENOSYS",
488191c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
488291c9afafSAndy Lutomirski		}
488391c9afafSAndy Lutomirski
48846b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
48856b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
48866b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
48876b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
48886b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
48896b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
48906b9ea5ffSJakub Kicinski			    $fix) {
48916b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
48926b9ea5ffSJakub Kicinski			}
48936b9ea5ffSJakub Kicinski		}
48946b9ea5ffSJakub Kicinski
4895653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4896653d4876SAndy Whitcroft# or if closed on same line
48975b57980dSJoe Perches		if ($perl_version_ok &&
48982d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
48992d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
49002d453e3bSJoe Perches		    $sline !~ /}/) {
49018d182478SJoe Perches			if (ERROR("OPEN_BRACE",
49022d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
49038d182478SJoe Perches			    $fix) {
49048d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49058d182478SJoe Perches				my $fixed_line = $rawline;
490603f49351SDwaipayan Ray				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
49078d182478SJoe Perches				my $line1 = $1;
49088d182478SJoe Perches				my $line2 = $2;
49098d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
49108d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
49118d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
49128d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
49138d182478SJoe Perches				}
49148d182478SJoe Perches			}
49150a920b5bSAndy Whitcroft		}
4916653d4876SAndy Whitcroft
49178905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
49188905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
49198905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
49208d182478SJoe Perches			if (ERROR("OPEN_BRACE",
49218d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
49228d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49238d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49248d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49258d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
49268d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
49278d182478SJoe Perches				$fixedline = $rawline;
49288d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
49298d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
49308d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49318d182478SJoe Perches				}
49328d182478SJoe Perches			}
49338905a67cSAndy Whitcroft		}
49348905a67cSAndy Whitcroft
49350c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
49363705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
49373705ce5bSJoe Perches			if (WARN("SPACING",
49383705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
49393705ce5bSJoe Perches			    $fix) {
4940194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49413705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
49423705ce5bSJoe Perches			}
49430c73b4ebSAndy Whitcroft		}
49440c73b4ebSAndy Whitcroft
494531070b5dSJoe Perches# Function pointer declarations
494631070b5dSJoe Perches# check spacing between type, funcptr, and args
494731070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
494891f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
494931070b5dSJoe Perches			my $declare = $1;
495031070b5dSJoe Perches			my $pre_pointer_space = $2;
495131070b5dSJoe Perches			my $post_pointer_space = $3;
495231070b5dSJoe Perches			my $funcname = $4;
495331070b5dSJoe Perches			my $post_funcname_space = $5;
495431070b5dSJoe Perches			my $pre_args_space = $6;
495531070b5dSJoe Perches
495691f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
495791f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
495891f72e9cSJoe Perches# don't need a space so don't warn for those.
495991f72e9cSJoe Perches			my $post_declare_space = "";
496091f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
496191f72e9cSJoe Perches				$post_declare_space = $1;
496291f72e9cSJoe Perches				$declare = rtrim($declare);
496391f72e9cSJoe Perches			}
496491f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
496531070b5dSJoe Perches				WARN("SPACING",
496631070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
496791f72e9cSJoe Perches				$post_declare_space = " ";
496831070b5dSJoe Perches			}
496931070b5dSJoe Perches
497031070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
497191f72e9cSJoe Perches# This test is not currently implemented because these declarations are
497291f72e9cSJoe Perches# equivalent to
497391f72e9cSJoe Perches#	int  foo(int bar, ...)
497491f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
497591f72e9cSJoe Perches#
497691f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
497791f72e9cSJoe Perches#				WARN("SPACING",
497891f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
497991f72e9cSJoe Perches#			}
498031070b5dSJoe Perches
498131070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
498231070b5dSJoe Perches			if (defined $pre_pointer_space &&
498331070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
498431070b5dSJoe Perches				WARN("SPACING",
498531070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
498631070b5dSJoe Perches			}
498731070b5dSJoe Perches
498831070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
498931070b5dSJoe Perches			if (defined $post_pointer_space &&
499031070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
499131070b5dSJoe Perches				WARN("SPACING",
499231070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
499331070b5dSJoe Perches			}
499431070b5dSJoe Perches
499531070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
499631070b5dSJoe Perches			if (defined $post_funcname_space &&
499731070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
499831070b5dSJoe Perches				WARN("SPACING",
499931070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
500031070b5dSJoe Perches			}
500131070b5dSJoe Perches
500231070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
500331070b5dSJoe Perches			if (defined $pre_args_space &&
500431070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
500531070b5dSJoe Perches				WARN("SPACING",
500631070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
500731070b5dSJoe Perches			}
500831070b5dSJoe Perches
500931070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
5010194f66fcSJoe Perches				$fixed[$fixlinenr] =~
501191f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
501231070b5dSJoe Perches			}
501331070b5dSJoe Perches		}
501431070b5dSJoe Perches
50158d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
50168d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
5017fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
5018fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
50198d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
50208d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
50218d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
5022fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
502338dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
50243705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
50253705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
50263705ce5bSJoe Perches				    $fix) {
5027194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
50283705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
50293705ce5bSJoe Perches				}
50308d31cfceSAndy Whitcroft			}
50318d31cfceSAndy Whitcroft		}
50328d31cfceSAndy Whitcroft
5033f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
50346c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
5035c2fdda0dSAndy Whitcroft			my $name = $1;
5036773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
5037773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
5038c2fdda0dSAndy Whitcroft
5039c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
5040773647a0SAndy Whitcroft			if ($name =~ /^(?:
5041773647a0SAndy Whitcroft				if|for|while|switch|return|case|
5042773647a0SAndy Whitcroft				volatile|__volatile__|
5043773647a0SAndy Whitcroft				__attribute__|format|__extension__|
5044773647a0SAndy Whitcroft				asm|__asm__)$/x)
5045773647a0SAndy Whitcroft			{
5046c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
5047c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
5048c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
5049c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
5050773647a0SAndy Whitcroft
5051773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
5052c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
5053c2fdda0dSAndy Whitcroft
5054c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
5055c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
5056773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
5057c2fdda0dSAndy Whitcroft
5058c2fdda0dSAndy Whitcroft			} else {
50593705ce5bSJoe Perches				if (WARN("SPACING",
50603705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
50613705ce5bSJoe Perches					     $fix) {
5062194f66fcSJoe Perches					$fixed[$fixlinenr] =~
50633705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
50643705ce5bSJoe Perches				}
5065f0a594c1SAndy Whitcroft			}
50666c72ffaaSAndy Whitcroft		}
50679a4cad4eSEric Nelson
5068653d4876SAndy Whitcroft# Check operator spacing.
50690a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
50703705ce5bSJoe Perches			my $fixed_line = "";
50713705ce5bSJoe Perches			my $line_fixed = 0;
50723705ce5bSJoe Perches
50739c0ca6f9SAndy Whitcroft			my $ops = qr{
50749c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
50759c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
50769c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
50771f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
507884731623SJoe Perches				\?:|\?|:
50799c0ca6f9SAndy Whitcroft			}x;
5080cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
50813705ce5bSJoe Perches
50823705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
50833705ce5bSJoe Perches##			foreach my $el (@elements) {
50843705ce5bSJoe Perches##				print("el: <$el>\n");
50853705ce5bSJoe Perches##			}
50863705ce5bSJoe Perches
50873705ce5bSJoe Perches			my @fix_elements = ();
508800df344fSAndy Whitcroft			my $off = 0;
50896c72ffaaSAndy Whitcroft
50903705ce5bSJoe Perches			foreach my $el (@elements) {
50913705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
50923705ce5bSJoe Perches				$off += length($el);
50933705ce5bSJoe Perches			}
50943705ce5bSJoe Perches
50953705ce5bSJoe Perches			$off = 0;
50963705ce5bSJoe Perches
50976c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
5098b34c648bSJoe Perches			my $last_after = -1;
50996c72ffaaSAndy Whitcroft
51000a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
51013705ce5bSJoe Perches
51023705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
51033705ce5bSJoe Perches
51043705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
51053705ce5bSJoe Perches
51064a0df2efSAndy Whitcroft				$off += length($elements[$n]);
51074a0df2efSAndy Whitcroft
510825985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
5109773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
5110773647a0SAndy Whitcroft				my $cc = '';
5111773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
5112773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
5113773647a0SAndy Whitcroft				}
5114773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
5115773647a0SAndy Whitcroft
51164a0df2efSAndy Whitcroft				my $a = '';
51174a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
51184a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
5119cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
51204a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
51214a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
5122773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
51234a0df2efSAndy Whitcroft
51240a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
51254a0df2efSAndy Whitcroft
51264a0df2efSAndy Whitcroft				my $c = '';
51270a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
51284a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
51294a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
5130cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
51314a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
51324a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
51338b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
51344a0df2efSAndy Whitcroft				} else {
51354a0df2efSAndy Whitcroft					$c = 'E';
51360a920b5bSAndy Whitcroft				}
51370a920b5bSAndy Whitcroft
51384a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
51394a0df2efSAndy Whitcroft
51404a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
51414a0df2efSAndy Whitcroft
51426c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
5143de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
51440a920b5bSAndy Whitcroft
514574048ed8SAndy Whitcroft				# Pull out the value of this operator.
51466c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
51470a920b5bSAndy Whitcroft
51481f65f947SAndy Whitcroft				# Get the full operator variant.
51491f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
51501f65f947SAndy Whitcroft
515113214adfSAndy Whitcroft				# Ignore operators passed as parameters.
515213214adfSAndy Whitcroft				if ($op_type ne 'V' &&
5153d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
515413214adfSAndy Whitcroft
5155cf655043SAndy Whitcroft#				# Ignore comments
5156cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
515713214adfSAndy Whitcroft
5158d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
515913214adfSAndy Whitcroft				} elsif ($op eq ';') {
5160cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
5161cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
51623705ce5bSJoe Perches						if (ERROR("SPACING",
51633705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
5164b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
51653705ce5bSJoe Perches							$line_fixed = 1;
51663705ce5bSJoe Perches						}
5167d8aaf121SAndy Whitcroft					}
5168d8aaf121SAndy Whitcroft
5169d8aaf121SAndy Whitcroft				# // is a comment
5170d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
51710a920b5bSAndy Whitcroft
5172b00e4814SJoe Perches				#   :   when part of a bitfield
5173b00e4814SJoe Perches				} elsif ($opv eq ':B') {
5174b00e4814SJoe Perches					# skip the bitfield test for now
5175b00e4814SJoe Perches
51761f65f947SAndy Whitcroft				# No spaces for:
51771f65f947SAndy Whitcroft				#   ->
5178b00e4814SJoe Perches				} elsif ($op eq '->') {
51794a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
51803705ce5bSJoe Perches						if (ERROR("SPACING",
51813705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
5182b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
51833705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
51843705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
51853705ce5bSJoe Perches							}
5186b34c648bSJoe Perches							$line_fixed = 1;
51873705ce5bSJoe Perches						}
51880a920b5bSAndy Whitcroft					}
51890a920b5bSAndy Whitcroft
51902381097bSJoe Perches				# , must not have a space before and must have a space on the right.
51910a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
51922381097bSJoe Perches					my $rtrim_before = 0;
51932381097bSJoe Perches					my $space_after = 0;
51942381097bSJoe Perches					if ($ctx =~ /Wx./) {
51952381097bSJoe Perches						if (ERROR("SPACING",
51962381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
51972381097bSJoe Perches							$line_fixed = 1;
51982381097bSJoe Perches							$rtrim_before = 1;
51992381097bSJoe Perches						}
52002381097bSJoe Perches					}
5201cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
52023705ce5bSJoe Perches						if (ERROR("SPACING",
52033705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
52043705ce5bSJoe Perches							$line_fixed = 1;
5205b34c648bSJoe Perches							$last_after = $n;
52062381097bSJoe Perches							$space_after = 1;
52072381097bSJoe Perches						}
52082381097bSJoe Perches					}
52092381097bSJoe Perches					if ($rtrim_before || $space_after) {
52102381097bSJoe Perches						if ($rtrim_before) {
52112381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
52122381097bSJoe Perches						} else {
52132381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
52142381097bSJoe Perches						}
52152381097bSJoe Perches						if ($space_after) {
52162381097bSJoe Perches							$good .= " ";
52173705ce5bSJoe Perches						}
52180a920b5bSAndy Whitcroft					}
52190a920b5bSAndy Whitcroft
52209c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
522174048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
52229c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
52239c0ca6f9SAndy Whitcroft
52249c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
52259c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
52269c0ca6f9SAndy Whitcroft				# unary operator, or a cast
52279c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
522874048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
52290d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
5230cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
52313705ce5bSJoe Perches						if (ERROR("SPACING",
52323705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
5233b34c648bSJoe Perches							if ($n != $last_after + 2) {
5234b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
52353705ce5bSJoe Perches								$line_fixed = 1;
52363705ce5bSJoe Perches							}
52370a920b5bSAndy Whitcroft						}
5238b34c648bSJoe Perches					}
5239a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
5240171ae1a4SAndy Whitcroft						# A unary '*' may be const
5241171ae1a4SAndy Whitcroft
5242171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
52433705ce5bSJoe Perches						if (ERROR("SPACING",
52443705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5245b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
52463705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
52473705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
52483705ce5bSJoe Perches							}
5249b34c648bSJoe Perches							$line_fixed = 1;
52503705ce5bSJoe Perches						}
52510a920b5bSAndy Whitcroft					}
52520a920b5bSAndy Whitcroft
52530a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
52540a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
5255773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
52563705ce5bSJoe Perches						if (ERROR("SPACING",
52573705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
5258b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
52593705ce5bSJoe Perches							$line_fixed = 1;
52603705ce5bSJoe Perches						}
52610a920b5bSAndy Whitcroft					}
5262773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
5263773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
52643705ce5bSJoe Perches						if (ERROR("SPACING",
52653705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5266b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
52673705ce5bSJoe Perches							$line_fixed = 1;
52683705ce5bSJoe Perches						}
5269653d4876SAndy Whitcroft					}
5270773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
52713705ce5bSJoe Perches						if (ERROR("SPACING",
52723705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5273b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
52743705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
52753705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5276773647a0SAndy Whitcroft							}
5277b34c648bSJoe Perches							$line_fixed = 1;
52783705ce5bSJoe Perches						}
52793705ce5bSJoe Perches					}
52800a920b5bSAndy Whitcroft
52810a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
52829c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
52839c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
52849c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
5285c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
5286c2fdda0dSAndy Whitcroft					 $op eq '%')
52870a920b5bSAndy Whitcroft				{
5288d2e025f3SJoe Perches					if ($check) {
5289d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5290d2e025f3SJoe Perches							if (CHK("SPACING",
5291d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
5292d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5293d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5294d2e025f3SJoe Perches								$line_fixed = 1;
5295d2e025f3SJoe Perches							}
5296d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5297d2e025f3SJoe Perches							if (CHK("SPACING",
5298d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
5299d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5300d2e025f3SJoe Perches								$line_fixed = 1;
5301d2e025f3SJoe Perches							}
5302d2e025f3SJoe Perches						}
5303d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
53043705ce5bSJoe Perches						if (ERROR("SPACING",
53053705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
5306b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5307b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5308b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5309b34c648bSJoe Perches							}
53103705ce5bSJoe Perches							$line_fixed = 1;
53113705ce5bSJoe Perches						}
53120a920b5bSAndy Whitcroft					}
53130a920b5bSAndy Whitcroft
53141f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
53151f65f947SAndy Whitcroft				# terminating a case value or a label.
53161f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
5317263afd39SChris Down					if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
53183705ce5bSJoe Perches						if (ERROR("SPACING",
53193705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5320b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
53213705ce5bSJoe Perches							$line_fixed = 1;
53223705ce5bSJoe Perches						}
53231f65f947SAndy Whitcroft					}
53241f65f947SAndy Whitcroft
53250a920b5bSAndy Whitcroft				# All the others need spaces both sides.
5326cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
53271f65f947SAndy Whitcroft					my $ok = 0;
53281f65f947SAndy Whitcroft
532922f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
53301f65f947SAndy Whitcroft					if (($op eq '<' &&
53311f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
53321f65f947SAndy Whitcroft					    ($op eq '>' &&
53331f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
53341f65f947SAndy Whitcroft					{
53351f65f947SAndy Whitcroft						$ok = 1;
53361f65f947SAndy Whitcroft					}
53371f65f947SAndy Whitcroft
5338e0df7e1fSJoe Perches					# for asm volatile statements
5339e0df7e1fSJoe Perches					# ignore a colon with another
5340e0df7e1fSJoe Perches					# colon immediately before or after
5341e0df7e1fSJoe Perches					if (($op eq ':') &&
5342e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
5343e0df7e1fSJoe Perches						$ok = 1;
5344e0df7e1fSJoe Perches					}
5345e0df7e1fSJoe Perches
534684731623SJoe Perches					# messages are ERROR, but ?: are CHK
53471f65f947SAndy Whitcroft					if ($ok == 0) {
53480675a8fbSJean Delvare						my $msg_level = \&ERROR;
53490675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
535084731623SJoe Perches
53510675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
53523705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
5353b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5354b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5355b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5356b34c648bSJoe Perches							}
53573705ce5bSJoe Perches							$line_fixed = 1;
53583705ce5bSJoe Perches						}
53590a920b5bSAndy Whitcroft					}
536022f2a2efSAndy Whitcroft				}
53614a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
53623705ce5bSJoe Perches
53633705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
53643705ce5bSJoe Perches
53653705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
53660a920b5bSAndy Whitcroft			}
53673705ce5bSJoe Perches
53683705ce5bSJoe Perches			if (($#elements % 2) == 0) {
53693705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
53703705ce5bSJoe Perches			}
53713705ce5bSJoe Perches
5372194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5373194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
53743705ce5bSJoe Perches			}
53753705ce5bSJoe Perches
53763705ce5bSJoe Perches
53770a920b5bSAndy Whitcroft		}
53780a920b5bSAndy Whitcroft
5379786b6326SJoe Perches# check for whitespace before a non-naked semicolon
5380d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
5381786b6326SJoe Perches			if (WARN("SPACING",
5382786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
5383786b6326SJoe Perches			    $fix) {
5384194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
5385786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
5386786b6326SJoe Perches			}
5387786b6326SJoe Perches		}
5388786b6326SJoe Perches
5389f0a594c1SAndy Whitcroft# check for multiple assignments
5390f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5391000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
5392000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
5393f0a594c1SAndy Whitcroft		}
5394f0a594c1SAndy Whitcroft
539522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
539622f2a2efSAndy Whitcroft## # continuation.
539722f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
539822f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
539922f2a2efSAndy Whitcroft##
540022f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
5401e73d2715SDwaipayan Ray## 			# falsely report the parameters of functions.
540222f2a2efSAndy Whitcroft## 			my $ln = $line;
540322f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
540422f2a2efSAndy Whitcroft## 			}
540522f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
5406000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
5407000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
540822f2a2efSAndy Whitcroft## 			}
540922f2a2efSAndy Whitcroft## 		}
5410f0a594c1SAndy Whitcroft
54110a920b5bSAndy Whitcroft#need space before brace following if, while, etc
54126b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
54136ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
54143705ce5bSJoe Perches			if (ERROR("SPACING",
54153705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
54163705ce5bSJoe Perches			    $fix) {
54176ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
54183705ce5bSJoe Perches			}
5419de7d4f0eSAndy Whitcroft		}
5420de7d4f0eSAndy Whitcroft
5421c4a62ef9SJoe Perches## # check for blank lines before declarations
5422c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5423c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
5424c4a62ef9SJoe Perches##			WARN("SPACING",
5425c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
5426c4a62ef9SJoe Perches##		}
5427c4a62ef9SJoe Perches##
5428c4a62ef9SJoe Perches
5429de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
5430de7d4f0eSAndy Whitcroft# on the line
543194fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5432d5e616fcSJoe Perches			if (ERROR("SPACING",
5433d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
5434d5e616fcSJoe Perches			    $fix) {
5435194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5436d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
5437d5e616fcSJoe Perches			}
54380a920b5bSAndy Whitcroft		}
54390a920b5bSAndy Whitcroft
544022f2a2efSAndy Whitcroft# check spacing on square brackets
544122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
54423705ce5bSJoe Perches			if (ERROR("SPACING",
54433705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
54443705ce5bSJoe Perches			    $fix) {
5445194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54463705ce5bSJoe Perches				    s/\[\s+/\[/;
54473705ce5bSJoe Perches			}
544822f2a2efSAndy Whitcroft		}
544922f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
54503705ce5bSJoe Perches			if (ERROR("SPACING",
54513705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
54523705ce5bSJoe Perches			    $fix) {
5453194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54543705ce5bSJoe Perches				    s/\s+\]/\]/;
54553705ce5bSJoe Perches			}
545622f2a2efSAndy Whitcroft		}
545722f2a2efSAndy Whitcroft
5458c45dcabdSAndy Whitcroft# check spacing on parentheses
54599c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
54609c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
54613705ce5bSJoe Perches			if (ERROR("SPACING",
54623705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
54633705ce5bSJoe Perches			    $fix) {
5464194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54653705ce5bSJoe Perches				    s/\(\s+/\(/;
54663705ce5bSJoe Perches			}
546722f2a2efSAndy Whitcroft		}
546813214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5469c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
5470c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
54713705ce5bSJoe Perches			if (ERROR("SPACING",
54723705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
54733705ce5bSJoe Perches			    $fix) {
5474194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54753705ce5bSJoe Perches				    s/\s+\)/\)/;
54763705ce5bSJoe Perches			}
547722f2a2efSAndy Whitcroft		}
547822f2a2efSAndy Whitcroft
5479e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
5480e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5481e2826fd0SJoe Perches
5482e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5483ea4acbb1SJoe Perches			my $var = $1;
5484ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5485ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
5486ea4acbb1SJoe Perches			    $fix) {
5487ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5488ea4acbb1SJoe Perches			}
5489ea4acbb1SJoe Perches		}
5490ea4acbb1SJoe Perches
5491ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
5492ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
5493ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
5494ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5495ea4acbb1SJoe Perches			my $var = $2;
5496ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5497ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5498ea4acbb1SJoe Perches			    $fix) {
5499ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
5500ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
5501ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5502ea4acbb1SJoe Perches			}
5503e2826fd0SJoe Perches		}
5504e2826fd0SJoe Perches
550563b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
5506a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
5507a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
55085b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
550963b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
551063b7c73eSJoe Perches			my $if_stat = $1;
551163b7c73eSJoe Perches			my $test = substr($2, 1, -1);
551263b7c73eSJoe Perches			my $herectx;
551363b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
551463b7c73eSJoe Perches				my $match = $1;
551563b7c73eSJoe Perches				# avoid parentheses around potential macro args
551663b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
551763b7c73eSJoe Perches				if (!defined($herectx)) {
551863b7c73eSJoe Perches					$herectx = $here . "\n";
551963b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
552063b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
552163b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
552263b7c73eSJoe Perches						$herectx .=  $rl . "\n";
552363b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
552463b7c73eSJoe Perches					}
552563b7c73eSJoe Perches				}
552663b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
552763b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
552863b7c73eSJoe Perches			}
552963b7c73eSJoe Perches		}
553063b7c73eSJoe Perches
553169078651SJoe Perches# check that goto labels aren't indented (allow a single space indentation)
553269078651SJoe Perches# and ignore bitfield definitions like foo:1
553369078651SJoe Perches# Strictly, labels can have whitespace after the identifier and before the :
553469078651SJoe Perches# but this is not allowed here as many ?: uses would appear to be labels
553569078651SJoe Perches		if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
553669078651SJoe Perches		    $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
553769078651SJoe Perches		    $sline !~ /^.\s+default:/) {
55383705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
55393705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
55403705ce5bSJoe Perches			    $fix) {
5541194f66fcSJoe Perches				$fixed[$fixlinenr] =~
55423705ce5bSJoe Perches				    s/^(.)\s+/$1/;
55433705ce5bSJoe Perches			}
55440a920b5bSAndy Whitcroft		}
55450a920b5bSAndy Whitcroft
554640873abaSJoe Perches# check if a statement with a comma should be two statements like:
554740873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
554840873abaSJoe Perches#	bar = baz();
554940873abaSJoe Perches		if (defined($stat) &&
555040873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
555140873abaSJoe Perches			my $cnt = statement_rawlines($stat);
555240873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
555340873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
555440873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
555540873abaSJoe Perches		}
555640873abaSJoe Perches
55575b9553abSJoe Perches# return is not a function
5558507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5559c45dcabdSAndy Whitcroft			my $spacing = $1;
55605b57980dSJoe Perches			if ($perl_version_ok &&
55615b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
55625b9553abSJoe Perches				my $value = $1;
55635b9553abSJoe Perches				$value = deparenthesize($value);
55645b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5565000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5566000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
55675b9553abSJoe Perches				}
5568c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5569000d1cc1SJoe Perches				ERROR("SPACING",
5570000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5571c45dcabdSAndy Whitcroft			}
5572c45dcabdSAndy Whitcroft		}
5573507e5141SJoe Perches
5574b43ae21bSJoe Perches# unnecessary return in a void function
5575b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5576b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5577b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5578b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5579b43ae21bSJoe Perches		    $linenr >= 3 &&
5580b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5581b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
55829819cf25SJoe Perches			WARN("RETURN_VOID",
5583b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
55849819cf25SJoe Perches		}
55859819cf25SJoe Perches
5586189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
55875b57980dSJoe Perches		if ($perl_version_ok &&
5588189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5589189248d8SJoe Perches			my $openparens = $1;
5590189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5591189248d8SJoe Perches			my $msg = "";
5592189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5593189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5594189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5595189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5596189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5597189248d8SJoe Perches			}
5598189248d8SJoe Perches		}
5599189248d8SJoe Perches
5600c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5601c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5602c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5603c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
56045b57980dSJoe Perches		if ($perl_version_ok &&
5605c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5606c5595fa2SJoe Perches			my $lead = $1;
5607c5595fa2SJoe Perches			my $const = $2;
5608c5595fa2SJoe Perches			my $comp = $3;
5609c5595fa2SJoe Perches			my $to = $4;
5610c5595fa2SJoe Perches			my $newcomp = $comp;
5611f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5612c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5613c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5614c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5615c5595fa2SJoe Perches			    $fix) {
5616c5595fa2SJoe Perches				if ($comp eq "<") {
5617c5595fa2SJoe Perches					$newcomp = ">";
5618c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5619c5595fa2SJoe Perches					$newcomp = ">=";
5620c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5621c5595fa2SJoe Perches					$newcomp = "<";
5622c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5623c5595fa2SJoe Perches					$newcomp = "<=";
5624c5595fa2SJoe Perches				}
5625c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5626c5595fa2SJoe Perches			}
5627c5595fa2SJoe Perches		}
5628c5595fa2SJoe Perches
5629f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5630f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
563153a3c448SAndy Whitcroft			my $name = $1;
563246b85bf9SGuenter Roeck			if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
5633000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5634f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
563553a3c448SAndy Whitcroft			}
563653a3c448SAndy Whitcroft		}
5637c45dcabdSAndy Whitcroft
56380a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
56394a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
56403705ce5bSJoe Perches			if (ERROR("SPACING",
56413705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
56423705ce5bSJoe Perches			    $fix) {
5643194f66fcSJoe Perches				$fixed[$fixlinenr] =~
56443705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
56453705ce5bSJoe Perches			}
56460a920b5bSAndy Whitcroft		}
56470a920b5bSAndy Whitcroft
5648f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5649f5fe35ddSAndy Whitcroft# statements after the conditional.
5650170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
56513e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
56523e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
56533e469cdcSAndy Whitcroft					if (!defined $stat);
5654170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5655170d3a22SAndy Whitcroft						$remain_next, $off_next);
5656170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5657170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5658170d3a22SAndy Whitcroft
5659170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5660170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5661170d3a22SAndy Whitcroft				# then count those as offsets.
5662170d3a22SAndy Whitcroft				my ($whitespace) =
5663170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5664170d3a22SAndy Whitcroft				my $offset =
5665170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5666170d3a22SAndy Whitcroft
5667170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5668170d3a22SAndy Whitcroft								$offset} = 1;
5669170d3a22SAndy Whitcroft			}
5670170d3a22SAndy Whitcroft		}
5671170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5672c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5673170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5674171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
5675481efd7bSJoe Perches			my $fixed_assign_in_if = 0;
56768905a67cSAndy Whitcroft
5677b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
567865b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
567965b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
568065b64b3bSJoe Perches				    $fix && $perl_version_ok) {
568165b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
568265b64b3bSJoe Perches						my $space = $1;
568365b64b3bSJoe Perches						my $not = $2;
568465b64b3bSJoe Perches						my $statement = $3;
568565b64b3bSJoe Perches						my $assigned = $4;
568665b64b3bSJoe Perches						my $test = $8;
568765b64b3bSJoe Perches						my $against = $9;
568865b64b3bSJoe Perches						my $brace = $15;
568965b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
569065b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
569165b64b3bSJoe Perches						my $newline = "${space}if (";
569265b64b3bSJoe Perches						$newline .= '!' if defined($not);
569365b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
569465b64b3bSJoe Perches						$newline .= "$assigned";
569565b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
569665b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
569765b64b3bSJoe Perches						$newline .= ')';
569865b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
569965b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
5700481efd7bSJoe Perches						$fixed_assign_in_if = 1;
570165b64b3bSJoe Perches					}
570265b64b3bSJoe Perches				}
57038905a67cSAndy Whitcroft			}
57048905a67cSAndy Whitcroft
57058905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
57068905a67cSAndy Whitcroft			# conditional.
5707773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
57088905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
570913214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
571053210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
571153210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5712773647a0SAndy Whitcroft			{
5713bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5714bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5715bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
571642bdf74cSHidetoshi Seto				my $stat_real = '';
5717bb44ad39SAndy Whitcroft
571842bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
571942bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5720bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5721bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5722bb44ad39SAndy Whitcroft				}
5723bb44ad39SAndy Whitcroft
5724481efd7bSJoe Perches				if (ERROR("TRAILING_STATEMENTS",
5725481efd7bSJoe Perches					  "trailing statements should be on next line\n" . $herecurr . $stat_real) &&
5726481efd7bSJoe Perches				    !$fixed_assign_in_if &&
5727481efd7bSJoe Perches				    $cond_lines == 0 &&
5728481efd7bSJoe Perches				    $fix && $perl_version_ok &&
5729481efd7bSJoe Perches				    $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
5730481efd7bSJoe Perches					my $indent = $1;
5731481efd7bSJoe Perches					my $test = $2;
5732481efd7bSJoe Perches					my $rest = rtrim($4);
5733481efd7bSJoe Perches					if ($rest =~ /;$/) {
5734481efd7bSJoe Perches						$fixed[$fixlinenr] = "\+$indent$test";
5735481efd7bSJoe Perches						fix_insert_line($fixlinenr + 1, "$indent\t$rest");
5736481efd7bSJoe Perches					}
5737481efd7bSJoe Perches				}
57388905a67cSAndy Whitcroft			}
57398905a67cSAndy Whitcroft		}
57408905a67cSAndy Whitcroft
574113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
574213214adfSAndy Whitcroft		if ($line =~ /
574313214adfSAndy Whitcroft			(?:
574413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
574513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
574613214adfSAndy Whitcroft				(?:\&\&|\|\|)
574713214adfSAndy Whitcroft			|
574813214adfSAndy Whitcroft				(?:\&\&|\|\|)
574913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
575013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
575113214adfSAndy Whitcroft			)/x)
575213214adfSAndy Whitcroft		{
5753000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5754000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
575513214adfSAndy Whitcroft		}
575613214adfSAndy Whitcroft
57578905a67cSAndy Whitcroft# if and else should not have general statements after it
575813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
575913214adfSAndy Whitcroft			my $s = $1;
576013214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
576113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5762000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5763000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
57640a920b5bSAndy Whitcroft			}
576513214adfSAndy Whitcroft		}
576639667782SAndy Whitcroft# if should not continue a brace
576739667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5768000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5769048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
577039667782SAndy Whitcroft				$herecurr);
577139667782SAndy Whitcroft		}
5772a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5773a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5774a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
57753fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5776a1080bf8SAndy Whitcroft			\s*return\s+
5777a1080bf8SAndy Whitcroft		    )/xg)
5778a1080bf8SAndy Whitcroft		{
5779000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5780000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5781a1080bf8SAndy Whitcroft		}
57820a920b5bSAndy Whitcroft
57830a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
57840a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
57858b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
57860a920b5bSAndy Whitcroft		    $previndent == $indent) {
57878b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
57888b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
57898b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
57908b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
57918b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
57928b8856f4SJoe Perches				my $fixedline = $prevrawline;
57938b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
57948b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
57958b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
57968b8856f4SJoe Perches				}
57978b8856f4SJoe Perches				$fixedline = $rawline;
57988b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
57998b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
58008b8856f4SJoe Perches			}
58010a920b5bSAndy Whitcroft		}
58020a920b5bSAndy Whitcroft
58038b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5804c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5805c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5806c2fdda0dSAndy Whitcroft
5807c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5808c2fdda0dSAndy Whitcroft			# conditional.
5809773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5810c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5811c2fdda0dSAndy Whitcroft
5812c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
58138b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
58148b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
58158b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
58168b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
58178b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
58188b8856f4SJoe Perches					my $fixedline = $prevrawline;
58198b8856f4SJoe Perches					my $trailing = $rawline;
58208b8856f4SJoe Perches					$trailing =~ s/^\+//;
58218b8856f4SJoe Perches					$trailing = trim($trailing);
58228b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
58238b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
58248b8856f4SJoe Perches				}
5825c2fdda0dSAndy Whitcroft			}
5826c2fdda0dSAndy Whitcroft		}
5827c2fdda0dSAndy Whitcroft
582895e2c602SJoe Perches#Specific variable tests
5829323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5830323c1260SJoe Perches			my $var = $1;
583195e2c602SJoe Perches
583295e2c602SJoe Perches#CamelCase
5833807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5834be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
58354104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values
58364104a206SŁukasz Stelmach			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
583722735ce8SJoe Perches#Ignore Page<foo> variants
5838807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5839d99a4158SGerhard Engleder#Ignore ETHTOOL_LINK_MODE_<foo> variants
5840d99a4158SGerhard Engleder			    $var !~ /^ETHTOOL_LINK_MODE_/ &&
5841d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5842d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5843d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5844f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5845f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
5846f858e23aSAntonio Borneo				while ($var =~ m{\b($Ident)}g) {
58477e781f67SJoe Perches					my $word = $1;
58487e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5849d8b07710SJoe Perches					if ($check) {
5850d8b07710SJoe Perches						seed_camelcase_includes();
5851d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5852d8b07710SJoe Perches							seed_camelcase_file($realfile);
5853d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5854d8b07710SJoe Perches						}
5855d8b07710SJoe Perches					}
58567e781f67SJoe Perches					if (!defined $camelcase{$word}) {
58577e781f67SJoe Perches						$camelcase{$word} = 1;
5858be79794bSJoe Perches						CHK("CAMELCASE",
58597e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
58607e781f67SJoe Perches					}
5861323c1260SJoe Perches				}
5862323c1260SJoe Perches			}
58633445686aSJoe Perches		}
58640a920b5bSAndy Whitcroft
58650a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5866d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5867d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5868d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5869d5e616fcSJoe Perches			    $fix) {
5870194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5871d5e616fcSJoe Perches			}
58720a920b5bSAndy Whitcroft		}
58730a920b5bSAndy Whitcroft
58740e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
58750e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5876c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5877e09dec48SAndy Whitcroft			my $file = "$1.h";
5878e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5879e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5880e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
58817840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5882c45dcabdSAndy Whitcroft			{
58830e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
58840e212e0aSFabian Frederick				if ($asminclude > 0) {
5885e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5886000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5887000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5888e09dec48SAndy Whitcroft					} else {
5889000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5890000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5891e09dec48SAndy Whitcroft					}
58920a920b5bSAndy Whitcroft				}
58930a920b5bSAndy Whitcroft			}
58940e212e0aSFabian Frederick		}
58950a920b5bSAndy Whitcroft
5896653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5897653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5898cf655043SAndy Whitcroft# in a known good container
5899b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5900b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5901d8aaf121SAndy Whitcroft			my $ln = $linenr;
5902d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5903c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5904c45dcabdSAndy Whitcroft			my $ctx = '';
590508a2843eSJoe Perches			my $has_flow_statement = 0;
590608a2843eSJoe Perches			my $has_arg_concat = 0;
5907c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5908f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5909f74bd194SAndy Whitcroft			$ctx = $dstat;
5910c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5911a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5912c45dcabdSAndy Whitcroft
591308a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
591462e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
591508a2843eSJoe Perches
5916f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5917f59b64bfSJoe Perches			my $define_args = $1;
5918f59b64bfSJoe Perches			my $define_stmt = $dstat;
5919f59b64bfSJoe Perches			my @def_args = ();
5920f59b64bfSJoe Perches
5921f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5922f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5923f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
59248c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5925f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5926f59b64bfSJoe Perches			}
5927f59b64bfSJoe Perches
5928292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5929c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5930c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5931c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5932c45dcabdSAndy Whitcroft
5933c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
59342e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
59352e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
59362e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5937bf30d6edSAndy Whitcroft			{
5938c45dcabdSAndy Whitcroft			}
5939c45dcabdSAndy Whitcroft
5940342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
594133acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
594233acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5943e45bab8eSAndy Whitcroft			{
5944e45bab8eSAndy Whitcroft			}
5945e45bab8eSAndy Whitcroft
594642e15293SJoe Perches			# Make asm volatile uses seem like a generic function
594742e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
594842e15293SJoe Perches
5949c45dcabdSAndy Whitcroft			my $exceptions = qr{
5950c45dcabdSAndy Whitcroft				$Declare|
5951c45dcabdSAndy Whitcroft				module_param_named|
5952a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5953c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5954c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5955383099fdSAndy Whitcroft				__typeof__\(|
595622fd2d3eSStefani Seibold				union|
595722fd2d3eSStefani Seibold				struct|
5958ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
59596b10df42SVladimir Zapolskiy				^\"|\"$|
59606b10df42SVladimir Zapolskiy				^\[
5961c45dcabdSAndy Whitcroft			}x;
59625eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5963f59b64bfSJoe Perches
5964f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5965f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5966e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5967f59b64bfSJoe Perches
5968f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5969f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5970f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
59713cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5972356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5973f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5974f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5975e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
5976dc3f4deeSStanislaw Gruszka			    $dstat !~ /^case\b/ &&					# case ...
597772f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
59782e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5979f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5980f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5981f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
59824e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5983f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5984c45dcabdSAndy Whitcroft			{
5985e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5986e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5987e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5988e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5989f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5990f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5991f74bd194SAndy Whitcroft				} else {
5992000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5993388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5994d8aaf121SAndy Whitcroft				}
5995f59b64bfSJoe Perches
5996f59b64bfSJoe Perches			}
59975207649bSJoe Perches
59985207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
59995207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
60005207649bSJoe Perches			my $first = 1;
60015207649bSJoe Perches			$define_stmt = "";
60025207649bSJoe Perches			foreach my $l (@stmt_array) {
60035207649bSJoe Perches				$l =~ s/\\$//;
60045207649bSJoe Perches				if ($first) {
60055207649bSJoe Perches					$define_stmt = $l;
60065207649bSJoe Perches					$first = 0;
60075207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
60085207649bSJoe Perches					$define_stmt .= substr($l, 1);
60095207649bSJoe Perches				}
60105207649bSJoe Perches			}
60115207649bSJoe Perches			$define_stmt =~ s/$;//g;
60125207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
60135207649bSJoe Perches			$define_stmt = trim($define_stmt);
60145207649bSJoe Perches
6015f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
6016f59b64bfSJoe Perches			foreach my $arg (@def_args) {
6017f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
60189192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
60197fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
60207b844345SVincent 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;
60217fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
60227fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
6023d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
6024f59b64bfSJoe Perches				if ($use_cnt > 1) {
6025f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
6026f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
6027f59b64bfSJoe Perches				    }
60289192d41aSJoe Perches# check if any macro arguments may have other precedence issues
60297fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
60309192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
60319192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
60329192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
60339192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
60349192d41aSJoe Perches				}
60350a920b5bSAndy Whitcroft			}
60365023d347SJoe Perches
603708a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
603808a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
603908a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
604008a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
6041e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
604208a2843eSJoe Perches
604308a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
604408a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
604508a2843eSJoe Perches			}
604608a2843eSJoe Perches
6047481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
60485023d347SJoe Perches
60495023d347SJoe Perches		} else {
60505023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
6051481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
6052481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
60535023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
60545023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
60555023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
60565023d347SJoe Perches			}
6057653d4876SAndy Whitcroft		}
60580a920b5bSAndy Whitcroft
6059b13edf7fSJoe Perches# do {} while (0) macro tests:
6060b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
6061b13edf7fSJoe Perches# macro should not end with a semicolon
60625b57980dSJoe Perches		if ($perl_version_ok &&
6063b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
6064b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
6065b13edf7fSJoe Perches			my $ln = $linenr;
6066b13edf7fSJoe Perches			my $cnt = $realcnt;
6067b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
6068b13edf7fSJoe Perches			my $ctx = '';
6069b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
6070b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
6071b13edf7fSJoe Perches			$ctx = $dstat;
6072b13edf7fSJoe Perches
6073b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
60741b36b201SJoe Perches			$dstat =~ s/$;/ /g;
6075b13edf7fSJoe Perches
6076b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
6077b13edf7fSJoe Perches				my $stmts = $2;
6078b13edf7fSJoe Perches				my $semis = $3;
6079b13edf7fSJoe Perches
6080b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
6081b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
6082e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6083b13edf7fSJoe Perches
6084ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
6085ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
6086b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
6087b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
6088b13edf7fSJoe Perches				}
6089b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
6090b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
6091b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
6092b13edf7fSJoe Perches				}
6093f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
6094f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
6095f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
6096e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6097f5ef95b1SJoe Perches
6098f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
6099f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
6100b13edf7fSJoe Perches			}
6101b13edf7fSJoe Perches		}
6102b13edf7fSJoe Perches
6103f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
610413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
610513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
6106cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
610713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
6108cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
6109cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
6110aad4f614SJoe Perches				my @allowed = ();
6111aad4f614SJoe Perches				my $allow = 0;
611213214adfSAndy Whitcroft				my $seen = 0;
6113773647a0SAndy Whitcroft				my $herectx = $here . "\n";
6114cf655043SAndy Whitcroft				my $ln = $linenr - 1;
611513214adfSAndy Whitcroft				for my $chunk (@chunks) {
611613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
611713214adfSAndy Whitcroft
6118773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
6119773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
6120773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
6121773647a0SAndy Whitcroft
6122aad4f614SJoe Perches					$allowed[$allow] = 0;
6123773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
6124773647a0SAndy Whitcroft
6125773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
6126773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
6127773647a0SAndy Whitcroft
6128773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
6129cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
6130cf655043SAndy Whitcroft
6131773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
613213214adfSAndy Whitcroft
613313214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
613413214adfSAndy Whitcroft
6135aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
6136cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
6137cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
6138aad4f614SJoe Perches						$allowed[$allow] = 1;
613913214adfSAndy Whitcroft					}
614013214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
6141cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
6142aad4f614SJoe Perches						$allowed[$allow] = 1;
614313214adfSAndy Whitcroft					}
6144cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
6145cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
6146aad4f614SJoe Perches						$allowed[$allow] = 1;
614713214adfSAndy Whitcroft					}
6148aad4f614SJoe Perches					$allow++;
614913214adfSAndy Whitcroft				}
6150aad4f614SJoe Perches				if ($seen) {
6151aad4f614SJoe Perches					my $sum_allowed = 0;
6152aad4f614SJoe Perches					foreach (@allowed) {
6153aad4f614SJoe Perches						$sum_allowed += $_;
6154aad4f614SJoe Perches					}
6155aad4f614SJoe Perches					if ($sum_allowed == 0) {
6156000d1cc1SJoe Perches						WARN("BRACES",
6157000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
6158aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
6159aad4f614SJoe Perches						 $seen != $allow) {
6160aad4f614SJoe Perches						CHK("BRACES",
6161aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
6162aad4f614SJoe Perches					}
616313214adfSAndy Whitcroft				}
616413214adfSAndy Whitcroft			}
616513214adfSAndy Whitcroft		}
6166773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
616713214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
6168cf655043SAndy Whitcroft			my $allowed = 0;
6169f0a594c1SAndy Whitcroft
6170cf655043SAndy Whitcroft			# Check the pre-context.
6171cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6172cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
6173cf655043SAndy Whitcroft				$allowed = 1;
6174f0a594c1SAndy Whitcroft			}
6175773647a0SAndy Whitcroft
6176773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
6177773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
6178773647a0SAndy Whitcroft
6179cf655043SAndy Whitcroft			# Check the condition.
6180cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
6181773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
6182cf655043SAndy Whitcroft			if (defined $cond) {
6183773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
6184cf655043SAndy Whitcroft			}
6185cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
6186cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
6187cf655043SAndy Whitcroft				$allowed = 1;
6188cf655043SAndy Whitcroft			}
6189cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
6190cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
6191cf655043SAndy Whitcroft				$allowed = 1;
6192cf655043SAndy Whitcroft			}
6193cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
6194cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
6195cf655043SAndy Whitcroft				$allowed = 1;
6196cf655043SAndy Whitcroft			}
6197cf655043SAndy Whitcroft			# Check the post-context.
6198cf655043SAndy Whitcroft			if (defined $chunks[1]) {
6199cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
6200cf655043SAndy Whitcroft				if (defined $cond) {
6201773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
6202cf655043SAndy Whitcroft				}
6203cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
6204cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
6205cf655043SAndy Whitcroft					$allowed = 1;
6206cf655043SAndy Whitcroft				}
6207cf655043SAndy Whitcroft			}
6208cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
6209f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
6210e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6211cf655043SAndy Whitcroft
6212000d1cc1SJoe Perches				WARN("BRACES",
6213000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
6214f0a594c1SAndy Whitcroft			}
6215f0a594c1SAndy Whitcroft		}
6216f0a594c1SAndy Whitcroft
6217e4c5babdSJoe Perches# check for single line unbalanced braces
621895330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
621995330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
6220e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6221e4c5babdSJoe Perches		}
6222e4c5babdSJoe Perches
62230979ae66SJoe Perches# check for unnecessary blank lines around braces
622477b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
6225f8e58219SJoe Perches			if (CHK("BRACES",
6226f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6227f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
6228f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
6229f8e58219SJoe Perches			}
62300979ae66SJoe Perches		}
623177b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
6232f8e58219SJoe Perches			if (CHK("BRACES",
6233f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6234f8e58219SJoe Perches			    $fix) {
6235f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
6236f8e58219SJoe Perches			}
62370979ae66SJoe Perches		}
62380979ae66SJoe Perches
62394a0df2efSAndy Whitcroft# no volatiles please
62406c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
62416c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
6242000d1cc1SJoe Perches			WARN("VOLATILE",
62438c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
62444a0df2efSAndy Whitcroft		}
62454a0df2efSAndy Whitcroft
62465e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
62475e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
62485e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
62495e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
625033acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
62515e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
62525e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
62535e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
62545e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
62555e4f6ba5SJoe Perches				     $fix &&
62565e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
62575e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
62585e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
62595e4f6ba5SJoe Perches				my $comma_close = "";
62605e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
62615e4f6ba5SJoe Perches					$comma_close = $1;
62625e4f6ba5SJoe Perches				}
62635e4f6ba5SJoe Perches
62645e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
62655e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
62665e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
62675e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
62685e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
62695e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
62705e4f6ba5SJoe Perches				$fixedline = $rawline;
62715e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
62725e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
62735e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
62745e4f6ba5SJoe Perches				}
62755e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
62765e4f6ba5SJoe Perches			}
62775e4f6ba5SJoe Perches		}
62785e4f6ba5SJoe Perches
62795e4f6ba5SJoe Perches# check for missing a space in a string concatenation
62805e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
62815e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
62825e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
62835e4f6ba5SJoe Perches		}
62845e4f6ba5SJoe Perches
628577cb8546SJoe Perches# check for an embedded function name in a string when the function is known
6286e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
6287e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
6288e4b7d309SJoe Perches# function declarations
628977cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
629077cb8546SJoe Perches		    defined($context_function) &&
6291e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6292e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
629377cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
6294e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
629577cb8546SJoe Perches		}
629677cb8546SJoe Perches
6297adb2da82SJoe Perches# check for unnecessary function tracing like uses
6298adb2da82SJoe Perches# This does not use $logFunctions because there are many instances like
6299adb2da82SJoe Perches# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6300adb2da82SJoe Perches		if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6301adb2da82SJoe Perches			if (WARN("TRACING_LOGGING",
6302adb2da82SJoe Perches				 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6303adb2da82SJoe Perches			    $fix) {
6304adb2da82SJoe Perches                                fix_delete_line($fixlinenr, $rawline);
6305adb2da82SJoe Perches			}
6306adb2da82SJoe Perches		}
6307adb2da82SJoe Perches
63085e4f6ba5SJoe Perches# check for spaces before a quoted newline
63095e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
63105e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
63115e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
63125e4f6ba5SJoe Perches			    $fix) {
63135e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
63145e4f6ba5SJoe Perches			}
63155e4f6ba5SJoe Perches
63165e4f6ba5SJoe Perches		}
63175e4f6ba5SJoe Perches
6318f17dba4fSJoe Perches# concatenated string without spaces between elements
6319d2af5aa6SJoe Perches		if ($line =~ /$String[A-Z_]/ ||
6320d2af5aa6SJoe Perches		    ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
632179682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
632279682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
632379682c0cSJoe Perches			    $fix) {
632479682c0cSJoe Perches				while ($line =~ /($String)/g) {
632579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
632679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
632779682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
632879682c0cSJoe Perches				}
632979682c0cSJoe Perches			}
6330f17dba4fSJoe Perches		}
6331f17dba4fSJoe Perches
633290ad30e5SJoe Perches# uncoalesced string fragments
6333d2af5aa6SJoe Perches		if ($line =~ /$String\s*[Lu]?"/) {
633479682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
633579682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
633679682c0cSJoe Perches			    $fix) {
633779682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
633879682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
633979682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
634079682c0cSJoe Perches				}
634179682c0cSJoe Perches			}
634290ad30e5SJoe Perches		}
634390ad30e5SJoe Perches
6344522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
6345522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
6346522b837cSAlexey Dobriyan		my $show_Z = 1;
63475e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6348522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
63495e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
6350522b837cSAlexey Dobriyan			# check for %L
6351522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
63525e4f6ba5SJoe Perches				WARN("PRINTF_L",
6353522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6354522b837cSAlexey Dobriyan				$show_L = 0;
63555e4f6ba5SJoe Perches			}
6356522b837cSAlexey Dobriyan			# check for %Z
6357522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6358522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
6359522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6360522b837cSAlexey Dobriyan				$show_Z = 0;
6361522b837cSAlexey Dobriyan			}
6362522b837cSAlexey Dobriyan			# check for 0x<decimal>
6363522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6364522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
63656e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
63666e300757SJoe Perches			}
63675e4f6ba5SJoe Perches		}
63685e4f6ba5SJoe Perches
63695e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
63703f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
63715e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
63725e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
63735e4f6ba5SJoe Perches		}
63745e4f6ba5SJoe Perches
637500df344fSAndy Whitcroft# warn about #if 0
6376c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
637760f89010SPrakruthi Deepak Heragu			WARN("IF_0",
637860f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
637960f89010SPrakruthi Deepak Heragu		}
638060f89010SPrakruthi Deepak Heragu
638160f89010SPrakruthi Deepak Heragu# warn about #if 1
638260f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
638360f89010SPrakruthi Deepak Heragu			WARN("IF_1",
638460f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
63854a0df2efSAndy Whitcroft		}
63864a0df2efSAndy Whitcroft
638703df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
638803df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6389100425deSJoe Perches			my $tested = quotemeta($1);
6390100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6391100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6392100425deSJoe Perches				my $func = $1;
6393100425deSJoe Perches				if (WARN('NEEDLESS_IF',
6394100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6395100425deSJoe Perches				    $fix) {
6396100425deSJoe Perches					my $do_fix = 1;
6397100425deSJoe Perches					my $leading_tabs = "";
6398100425deSJoe Perches					my $new_leading_tabs = "";
6399100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6400100425deSJoe Perches						$leading_tabs = $1;
6401100425deSJoe Perches					} else {
6402100425deSJoe Perches						$do_fix = 0;
6403100425deSJoe Perches					}
6404100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6405100425deSJoe Perches						$new_leading_tabs = $1;
6406100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6407100425deSJoe Perches							$do_fix = 0;
6408100425deSJoe Perches						}
6409100425deSJoe Perches					} else {
6410100425deSJoe Perches						$do_fix = 0;
6411100425deSJoe Perches					}
6412100425deSJoe Perches					if ($do_fix) {
6413100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
6414100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6415100425deSJoe Perches					}
6416100425deSJoe Perches				}
64174c432a8fSGreg Kroah-Hartman			}
64184c432a8fSGreg Kroah-Hartman		}
6419f0a594c1SAndy Whitcroft
6420ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
6421ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6422ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6423ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
6424ebfdc409SJoe Perches		    $linenr > 3) {
6425ebfdc409SJoe Perches			my $testval = $2;
6426ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
6427ebfdc409SJoe Perches
6428ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6429ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6430ebfdc409SJoe Perches
6431e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6432e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
6433ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
6434ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6435ebfdc409SJoe Perches			}
6436ebfdc409SJoe Perches		}
6437ebfdc409SJoe Perches
6438f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
6439dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6440f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6441f78d98f6SJoe Perches			my $level = $1;
6442f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
6443f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
6444f78d98f6SJoe Perches			    $fix) {
6445f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6446f78d98f6SJoe Perches			}
6447f78d98f6SJoe Perches		}
6448f78d98f6SJoe Perches
644945c55e92SJoe Perches# check for logging continuations
645045c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
645145c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
645245c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
645345c55e92SJoe Perches		}
645445c55e92SJoe Perches
645570eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
645670eb2275SDwaipayan Ray		if (defined $stat &&
645770eb2275SDwaipayan Ray		    $line =~ /\b$logFunctions\s*\(/ &&
645870eb2275SDwaipayan Ray		    index($stat, '"') >= 0) {
645970eb2275SDwaipayan Ray			my $lc = $stat =~ tr@\n@@;
646070eb2275SDwaipayan Ray			$lc = $lc + $linenr;
646170eb2275SDwaipayan Ray			my $stat_real = get_stat_real($linenr, $lc);
646270eb2275SDwaipayan Ray			pos($stat_real) = index($stat_real, '"');
646370eb2275SDwaipayan Ray			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
646470eb2275SDwaipayan Ray				my $pspec = $1;
646570eb2275SDwaipayan Ray				my $h = $2;
646670eb2275SDwaipayan Ray				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
646770eb2275SDwaipayan Ray				if (WARN("UNNECESSARY_MODIFIER",
646870eb2275SDwaipayan Ray					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
646970eb2275SDwaipayan Ray				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
647070eb2275SDwaipayan Ray					my $nspec = $pspec;
647170eb2275SDwaipayan Ray					$nspec =~ s/h//g;
647270eb2275SDwaipayan Ray					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
647370eb2275SDwaipayan Ray				}
647470eb2275SDwaipayan Ray			}
647570eb2275SDwaipayan Ray		}
647670eb2275SDwaipayan Ray
6477abb08a53SJoe Perches# check for mask then right shift without a parentheses
64785b57980dSJoe Perches		if ($perl_version_ok &&
6479abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6480abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6481abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
6482abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6483abb08a53SJoe Perches		}
6484abb08a53SJoe Perches
6485b75ac618SJoe Perches# check for pointer comparisons to NULL
64865b57980dSJoe Perches		if ($perl_version_ok) {
6487b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6488b75ac618SJoe Perches				my $val = $1;
6489b75ac618SJoe Perches				my $equal = "!";
6490b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
6491b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
6492b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6493b75ac618SJoe Perches					    $fix) {
6494b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6495b75ac618SJoe Perches				}
6496b75ac618SJoe Perches			}
6497b75ac618SJoe Perches		}
6498b75ac618SJoe Perches
64998716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
65008716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
65018716de38SJoe Perches			my $attr = $1;
65028716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
65038716de38SJoe Perches				my $ptr = $1;
65048716de38SJoe Perches				my $var = $2;
65058716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
65068716de38SJoe Perches				      ERROR("MISPLACED_INIT",
65078716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
65088716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
65098716de38SJoe Perches				      WARN("MISPLACED_INIT",
65108716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
65118716de38SJoe Perches				    $fix) {
6512194f66fcSJoe 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;
65138716de38SJoe Perches				}
65148716de38SJoe Perches			}
65158716de38SJoe Perches		}
65168716de38SJoe Perches
6517e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
6518e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6519e970b884SJoe Perches			my $attr = $1;
6520e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
6521e970b884SJoe Perches			my $attr_prefix = $1;
6522e970b884SJoe Perches			my $attr_type = $2;
6523e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6524e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6525e970b884SJoe Perches			    $fix) {
6526194f66fcSJoe Perches				$fixed[$fixlinenr] =~
6527e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
6528e970b884SJoe Perches			}
6529e970b884SJoe Perches		}
6530e970b884SJoe Perches
6531e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
6532e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6533e970b884SJoe Perches			my $attr = $1;
6534e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6535e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6536e970b884SJoe Perches			    $fix) {
6537194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
6538e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
6539e970b884SJoe Perches				$lead = rtrim($1);
6540e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
6541e970b884SJoe Perches				$lead = "${lead}const ";
6542194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6543e970b884SJoe Perches			}
6544e970b884SJoe Perches		}
6545e970b884SJoe Perches
6546c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
6547c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
6548c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6549c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
6550c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6551c17893c7SJoe Perches			    $fix) {
6552c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6553c17893c7SJoe Perches			}
6554c17893c7SJoe Perches		}
6555c17893c7SJoe Perches
6556fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
6557fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
6558fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6559fbdb8138SJoe Perches			my $constant_func = $1;
6560fbdb8138SJoe Perches			my $func = $constant_func;
6561fbdb8138SJoe Perches			$func =~ s/^__constant_//;
6562fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
6563fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
6564fbdb8138SJoe Perches			    $fix) {
6565194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6566fbdb8138SJoe Perches			}
6567fbdb8138SJoe Perches		}
6568fbdb8138SJoe Perches
65691a15a250SPatrick Pannuto# prefer usleep_range over udelay
657037581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
657143c1d77cSJoe Perches			my $delay = $1;
65721a15a250SPatrick Pannuto			# ignore udelay's < 10, however
657343c1d77cSJoe Perches			if (! ($delay < 10) ) {
6574000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
6575458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
657643c1d77cSJoe Perches			}
657743c1d77cSJoe Perches			if ($delay > 2000) {
657843c1d77cSJoe Perches				WARN("LONG_UDELAY",
657943c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
65801a15a250SPatrick Pannuto			}
65811a15a250SPatrick Pannuto		}
65821a15a250SPatrick Pannuto
658309ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
658409ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
658509ef8725SPatrick Pannuto			if ($1 < 20) {
6586000d1cc1SJoe Perches				WARN("MSLEEP",
6587458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
658809ef8725SPatrick Pannuto			}
658909ef8725SPatrick Pannuto		}
659009ef8725SPatrick Pannuto
659136ec1939SJoe Perches# check for comparisons of jiffies
659236ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
659336ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
659436ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
659536ec1939SJoe Perches		}
659636ec1939SJoe Perches
65979d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
65989d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
65999d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
66009d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
66019d7a34a5SJoe Perches		}
66029d7a34a5SJoe Perches
660300df344fSAndy Whitcroft# warn about #ifdefs in C files
6604c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
660500df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
660600df344fSAndy Whitcroft#			print "$herecurr";
660700df344fSAndy Whitcroft#			$clean = 0;
660800df344fSAndy Whitcroft#		}
660900df344fSAndy Whitcroft
661022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
6611c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
66123705ce5bSJoe Perches			if (ERROR("SPACING",
66133705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
66143705ce5bSJoe Perches			    $fix) {
6615194f66fcSJoe Perches				$fixed[$fixlinenr] =~
66163705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
66173705ce5bSJoe Perches			}
66183705ce5bSJoe Perches
661922f2a2efSAndy Whitcroft		}
662022f2a2efSAndy Whitcroft
66214a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6622171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6623171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
66244a0df2efSAndy Whitcroft			my $which = $1;
66254a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6626000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6627000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
66284a0df2efSAndy Whitcroft			}
66294a0df2efSAndy Whitcroft		}
66304a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6631402c2553SMichael S. Tsirkin
6632402c2553SMichael S. Tsirkin		my $barriers = qr{
6633402c2553SMichael S. Tsirkin			mb|
6634402c2553SMichael S. Tsirkin			rmb|
6635ad83ec6cSWill Deacon			wmb
6636402c2553SMichael S. Tsirkin		}x;
6637402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6638402c2553SMichael S. Tsirkin			mb__before_atomic|
6639402c2553SMichael S. Tsirkin			mb__after_atomic|
6640402c2553SMichael S. Tsirkin			store_release|
6641402c2553SMichael S. Tsirkin			load_acquire|
6642402c2553SMichael S. Tsirkin			store_mb|
6643402c2553SMichael S. Tsirkin			(?:$barriers)
6644402c2553SMichael S. Tsirkin		}x;
6645402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6646402c2553SMichael S. Tsirkin			(?:$barriers)|
664743e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
664843e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6649402c2553SMichael S. Tsirkin		}x;
6650402c2553SMichael S. Tsirkin
6651402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
66524a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6653c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6654000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
66554a0df2efSAndy Whitcroft			}
66564a0df2efSAndy Whitcroft		}
66573ad81779SPaul E. McKenney
6658f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6659f4073b0fSMichael S. Tsirkin
6660f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6661f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6662f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6663f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6664f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6665f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6666f4073b0fSMichael S. Tsirkin		}
6667f4073b0fSMichael S. Tsirkin
6668cb426e99SJoe Perches# check for waitqueue_active without a comment.
6669cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6670cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6671cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6672cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6673cb426e99SJoe Perches			}
6674cb426e99SJoe Perches		}
66753ad81779SPaul E. McKenney
66765099a722SMarco Elver# check for data_race without a comment.
66775099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
66785099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
66795099a722SMarco Elver				WARN("DATA_RACE",
66805099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
66815099a722SMarco Elver			}
66825099a722SMarco Elver		}
66835099a722SMarco Elver
66844a0df2efSAndy Whitcroft# check of hardware specific defines
6685c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6686000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6687000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
66880a920b5bSAndy Whitcroft		}
6689653d4876SAndy Whitcroft
6690596ed45bSJoe Perches# check that the storage class is not after a type
6691596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6692000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6693596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6694596ed45bSJoe Perches		}
6695596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6696596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6697596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6698596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6699596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6700596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6701596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6702d4977c78STobias Klauser		}
6703d4977c78STobias Klauser
6704de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6705de7d4f0eSAndy Whitcroft# storage class and type.
67069c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
67079c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6708000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6709000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6710de7d4f0eSAndy Whitcroft		}
6711de7d4f0eSAndy Whitcroft
67128905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
67132b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
67142b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6715d5e616fcSJoe Perches			if (WARN("INLINE",
6716d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6717d5e616fcSJoe Perches			    $fix) {
6718194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6719d5e616fcSJoe Perches
6720d5e616fcSJoe Perches			}
67218905a67cSAndy Whitcroft		}
67228905a67cSAndy Whitcroft
67237ebe1d17SDwaipayan Ray# Check for compiler attributes
67242b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
67257ebe1d17SDwaipayan Ray		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
67267ebe1d17SDwaipayan Ray			my $attr = $1;
67277ebe1d17SDwaipayan Ray			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
67287ebe1d17SDwaipayan Ray
67297ebe1d17SDwaipayan Ray			my %attr_list = (
67300830aab0SJoe Perches				"alias"				=> "__alias",
67317ebe1d17SDwaipayan Ray				"aligned"			=> "__aligned",
67327ebe1d17SDwaipayan Ray				"always_inline"			=> "__always_inline",
67337ebe1d17SDwaipayan Ray				"assume_aligned"		=> "__assume_aligned",
67347ebe1d17SDwaipayan Ray				"cold"				=> "__cold",
67357ebe1d17SDwaipayan Ray				"const"				=> "__attribute_const__",
67367ebe1d17SDwaipayan Ray				"copy"				=> "__copy",
67377ebe1d17SDwaipayan Ray				"designated_init"		=> "__designated_init",
67387ebe1d17SDwaipayan Ray				"externally_visible"		=> "__visible",
67397ebe1d17SDwaipayan Ray				"format"			=> "printf|scanf",
67407ebe1d17SDwaipayan Ray				"gnu_inline"			=> "__gnu_inline",
67417ebe1d17SDwaipayan Ray				"malloc"			=> "__malloc",
67427ebe1d17SDwaipayan Ray				"mode"				=> "__mode",
67437ebe1d17SDwaipayan Ray				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
67447ebe1d17SDwaipayan Ray				"noclone"			=> "__noclone",
67457ebe1d17SDwaipayan Ray				"noinline"			=> "noinline",
67467ebe1d17SDwaipayan Ray				"nonstring"			=> "__nonstring",
67477ebe1d17SDwaipayan Ray				"noreturn"			=> "__noreturn",
67487ebe1d17SDwaipayan Ray				"packed"			=> "__packed",
67497ebe1d17SDwaipayan Ray				"pure"				=> "__pure",
6750339f29d9SJoe Perches				"section"			=> "__section",
67510830aab0SJoe Perches				"used"				=> "__used",
67520830aab0SJoe Perches				"weak"				=> "__weak"
67537ebe1d17SDwaipayan Ray			);
67547ebe1d17SDwaipayan Ray
67557ebe1d17SDwaipayan Ray			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6756339f29d9SJoe Perches				my $orig_attr = $1;
67577ebe1d17SDwaipayan Ray				my $params = '';
67587ebe1d17SDwaipayan Ray				$params = $2 if defined($2);
6759339f29d9SJoe Perches				my $curr_attr = $orig_attr;
67607ebe1d17SDwaipayan Ray				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
67617ebe1d17SDwaipayan Ray				if (exists($attr_list{$curr_attr})) {
6762339f29d9SJoe Perches					my $new = $attr_list{$curr_attr};
67637ebe1d17SDwaipayan Ray					if ($curr_attr eq "format" && $params) {
67647ebe1d17SDwaipayan Ray						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6765339f29d9SJoe Perches						$new = "__$1\($2";
67667ebe1d17SDwaipayan Ray					} else {
6767339f29d9SJoe Perches						$new = "$new$params";
67687ebe1d17SDwaipayan Ray					}
67697ebe1d17SDwaipayan Ray					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6770339f29d9SJoe Perches						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
67717ebe1d17SDwaipayan Ray					    $fix) {
6772339f29d9SJoe Perches						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6773339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/$remove//;
6774339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6775339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6776339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
67777ebe1d17SDwaipayan Ray					}
677839b7e287SJoe Perches				}
6779462811d9SJoe Perches			}
6780462811d9SJoe Perches
67817ebe1d17SDwaipayan Ray			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
67827ebe1d17SDwaipayan Ray			if ($attr =~ /^_*unused/) {
67837ebe1d17SDwaipayan Ray				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
67847ebe1d17SDwaipayan Ray				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6785d5e616fcSJoe Perches			}
67866061d949SJoe Perches		}
67876061d949SJoe Perches
6788619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
67895b57980dSJoe Perches		if ($perl_version_ok &&
6790619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6791619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6792619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6793619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6794619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6795619a908aSJoe Perches		}
6796619a908aSJoe Perches
6797fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6798e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6799fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6800e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6801e6176fa4SJoe Perches			my $type = $1;
6802e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6803e6176fa4SJoe Perches				$type = $1;
6804e6176fa4SJoe Perches				my $kernel_type = 'u';
6805e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6806e6176fa4SJoe Perches				$type =~ /(\d+)/;
6807e6176fa4SJoe Perches				$kernel_type .= $1;
6808e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6809e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6810e6176fa4SJoe Perches				    $fix) {
6811e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6812e6176fa4SJoe Perches				}
6813e6176fa4SJoe Perches			}
6814e6176fa4SJoe Perches		}
6815e6176fa4SJoe Perches
6816938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6817938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6818938224b5SJoe Perches			my $cast = $1;
6819938224b5SJoe Perches			my $const = $2;
6820938224b5SJoe Perches			my $suffix = "";
6821938224b5SJoe Perches			my $newconst = $const;
6822938224b5SJoe Perches			$newconst =~ s/${Int_type}$//;
6823938224b5SJoe Perches			$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6824938224b5SJoe Perches			if ($cast =~ /\blong\s+long\b/) {
6825938224b5SJoe Perches			    $suffix .= 'LL';
6826938224b5SJoe Perches			} elsif ($cast =~ /\blong\b/) {
6827938224b5SJoe Perches			    $suffix .= 'L';
6828938224b5SJoe Perches			}
68290972b8bfSJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
68300972b8bfSJoe Perches				 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
68310972b8bfSJoe Perches			    $fix) {
6832938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6833938224b5SJoe Perches			}
6834938224b5SJoe Perches		}
6835938224b5SJoe Perches
68368f53a9b8SJoe Perches# check for sizeof(&)
68378f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6838000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6839000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
68408f53a9b8SJoe Perches		}
68418f53a9b8SJoe Perches
684266c80b60SJoe Perches# check for sizeof without parenthesis
684366c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6844d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6845d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6846d5e616fcSJoe Perches			    $fix) {
6847194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6848d5e616fcSJoe Perches			}
684966c80b60SJoe Perches		}
685066c80b60SJoe Perches
685188982feaSJoe Perches# check for struct spinlock declarations
685288982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
685388982feaSJoe Perches			WARN("USE_SPINLOCK_T",
685488982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
685588982feaSJoe Perches		}
685688982feaSJoe Perches
6857a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
685806668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6859a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6860caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6861caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6862d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6863d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6864d5e616fcSJoe Perches				    $fix) {
6865194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6866d5e616fcSJoe Perches				}
6867a6962d72SJoe Perches			}
6868a6962d72SJoe Perches		}
6869a6962d72SJoe Perches
68700b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
68715b57980dSJoe Perches		if ($perl_version_ok &&
68720b523769SJoe Perches		    defined $stat &&
68730b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
68740b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6875e3c6bc95STobin C. Harding			my $stat_real;
6876e3c6bc95STobin C. Harding
68770b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
68780b523769SJoe Perches			$lc = $lc + $linenr;
68790b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6880ffe07513SJoe Perches				my $specifier;
6881ffe07513SJoe Perches				my $extension;
68823bd32d6aSSakari Ailus				my $qualifier;
6883ffe07513SJoe Perches				my $bad_specifier = "";
68840b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
68850b523769SJoe Perches				$fmt =~ s/%%//g;
6886e3c6bc95STobin C. Harding
68873bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6888e3c6bc95STobin C. Harding					$specifier = $1;
6889e3c6bc95STobin C. Harding					$extension = $2;
68903bd32d6aSSakari Ailus					$qualifier = $3;
6891af612e43SSakari Ailus					if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
68923bd32d6aSSakari Ailus					    ($extension eq "f" &&
6893af612e43SSakari Ailus					     defined $qualifier && $qualifier !~ /^w/) ||
6894af612e43SSakari Ailus					    ($extension eq "4" &&
6895af612e43SSakari Ailus					     defined $qualifier && $qualifier !~ /^cc/)) {
6896e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
68970b523769SJoe Perches						last;
68980b523769SJoe Perches					}
6899e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6900e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6901e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
69020b523769SJoe Perches						}
6903e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6904e3c6bc95STobin 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");
6905e3c6bc95STobin C. Harding					}
6906e3c6bc95STobin C. Harding				}
6907e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
69082a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
6909de48fa1aSMiguel Ojeda					my $msg_level = \&WARN;
69101df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
69111df7338aSSergey Senozhatsky					my $use = "";
6912e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
69131df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6914e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6915de48fa1aSMiguel Ojeda					} elsif ($bad_specifier =~ /pA/) {
6916de48fa1aSMiguel Ojeda						$use =  " - '%pA' is only intended to be used from Rust code";
6917de48fa1aSMiguel Ojeda						$msg_level = \&ERROR;
69181df7338aSSergey Senozhatsky					}
69192a9f9d85STobin C. Harding
6920de48fa1aSMiguel Ojeda					&{$msg_level}("VSPRINTF_POINTER_EXTENSION",
6921e3c6bc95STobin C. Harding						      "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6922e3c6bc95STobin C. Harding				}
69230b523769SJoe Perches			}
69240b523769SJoe Perches		}
69250b523769SJoe Perches
6926554e165cSAndy Whitcroft# Check for misused memsets
69275b57980dSJoe Perches		if ($perl_version_ok &&
6928d1fe9c09SJoe Perches		    defined $stat &&
69299e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6930554e165cSAndy Whitcroft
6931d7c76ba7SJoe Perches			my $ms_addr = $2;
6932d1fe9c09SJoe Perches			my $ms_val = $7;
6933d1fe9c09SJoe Perches			my $ms_size = $12;
6934d7c76ba7SJoe Perches
6935554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6936554e165cSAndy Whitcroft				ERROR("MEMSET",
6937d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6938554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6939554e165cSAndy Whitcroft				WARN("MEMSET",
6940d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6941d7c76ba7SJoe Perches			}
6942d7c76ba7SJoe Perches		}
6943d7c76ba7SJoe Perches
694498a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
69455b57980dSJoe Perches#		if ($perl_version_ok &&
6946f333195dSJoe Perches#		    defined $stat &&
6947f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6948f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6949f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6950f333195dSJoe Perches#			    $fix) {
6951f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6952f333195dSJoe Perches#			}
6953f333195dSJoe Perches#		}
695498a9bba5SJoe Perches
6955b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
69565b57980dSJoe Perches#		if ($perl_version_ok &&
6957f333195dSJoe Perches#		    defined $stat &&
6958f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6959f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6960f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6961f333195dSJoe Perches#		}
6962b6117d17SMateusz Kulikowski
69638617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
69648617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
69655b57980dSJoe Perches#		if ($perl_version_ok &&
6966f333195dSJoe Perches#		    defined $stat &&
6967f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6968f333195dSJoe Perches#
6969f333195dSJoe Perches#			my $ms_val = $7;
6970f333195dSJoe Perches#
6971f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6972f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6973f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6974f333195dSJoe Perches#				    $fix) {
6975f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6976f333195dSJoe Perches#				}
6977f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6978f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6979f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6980f333195dSJoe Perches#				    $fix) {
6981f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6982f333195dSJoe Perches#				}
6983f333195dSJoe Perches#			}
6984f333195dSJoe Perches#		}
69858617cd09SMateusz Kulikowski
69865dbdb2d8SJoe Perches# strlcpy uses that should likely be strscpy
69875dbdb2d8SJoe Perches		if ($line =~ /\bstrlcpy\s*\(/) {
69885dbdb2d8SJoe Perches			WARN("STRLCPY",
69895dbdb2d8SJoe Perches			     "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
69905dbdb2d8SJoe Perches		}
69915dbdb2d8SJoe Perches
6992d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
69935b57980dSJoe Perches		if ($perl_version_ok &&
6994d1fe9c09SJoe Perches		    defined $stat &&
6995d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6996d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6997d7c76ba7SJoe Perches				my $call = $1;
6998d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6999d7c76ba7SJoe Perches				my $arg1 = $3;
7000d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
7001d1fe9c09SJoe Perches				my $arg2 = $8;
7002d7c76ba7SJoe Perches				my $cast;
7003d7c76ba7SJoe Perches
7004d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
7005d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
7006d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
7007d7c76ba7SJoe Perches					$cast = $cast1;
7008d7c76ba7SJoe Perches				} else {
7009d7c76ba7SJoe Perches					$cast = $cast2;
7010d7c76ba7SJoe Perches				}
7011d7c76ba7SJoe Perches				WARN("MINMAX",
7012d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
7013554e165cSAndy Whitcroft			}
7014554e165cSAndy Whitcroft		}
7015554e165cSAndy Whitcroft
70164a273195SJoe Perches# check usleep_range arguments
70175b57980dSJoe Perches		if ($perl_version_ok &&
70184a273195SJoe Perches		    defined $stat &&
70194a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
70204a273195SJoe Perches			my $min = $1;
70214a273195SJoe Perches			my $max = $7;
70224a273195SJoe Perches			if ($min eq $max) {
70234a273195SJoe Perches				WARN("USLEEP_RANGE",
7024458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
70254a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
70264a273195SJoe Perches				 $min > $max) {
70274a273195SJoe Perches				WARN("USLEEP_RANGE",
7028458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
70294a273195SJoe Perches			}
70304a273195SJoe Perches		}
70314a273195SJoe Perches
7032823b794cSJoe Perches# check for naked sscanf
70335b57980dSJoe Perches		if ($perl_version_ok &&
7034823b794cSJoe Perches		    defined $stat &&
70356c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
7036823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
7037823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
7038823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
7039823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
7040823b794cSJoe Perches			$lc = $lc + $linenr;
70412a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
7042823b794cSJoe Perches			WARN("NAKED_SSCANF",
7043823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
7044823b794cSJoe Perches		}
7045823b794cSJoe Perches
7046afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
70475b57980dSJoe Perches		if ($perl_version_ok &&
7048afc819abSJoe Perches		    defined $stat &&
7049afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
7050afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
7051afc819abSJoe Perches			$lc = $lc + $linenr;
70522a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
7053afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
7054afc819abSJoe Perches				my $format = $6;
7055afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
7056afc819abSJoe Perches				if ($count == 1 &&
7057afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
7058afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
7059afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
7060afc819abSJoe Perches				}
7061afc819abSJoe Perches			}
7062afc819abSJoe Perches		}
7063afc819abSJoe Perches
706470dc8a48SJoe Perches# check for new externs in .h files.
706570dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
706670dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
7067d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
706870dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
706970dc8a48SJoe Perches			    $fix) {
7070194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
707170dc8a48SJoe Perches			}
707270dc8a48SJoe Perches		}
707370dc8a48SJoe Perches
7074de7d4f0eSAndy Whitcroft# check for new externs in .c files.
7075171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
7076c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
7077171ae1a4SAndy Whitcroft		{
7078c45dcabdSAndy Whitcroft			my $function_name = $1;
7079c45dcabdSAndy Whitcroft			my $paren_space = $2;
7080171ae1a4SAndy Whitcroft
7081171ae1a4SAndy Whitcroft			my $s = $stat;
7082171ae1a4SAndy Whitcroft			if (defined $cond) {
7083171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
7084171ae1a4SAndy Whitcroft			}
7085d8b44b58SKees Cook			if ($s =~ /^\s*;/)
7086c45dcabdSAndy Whitcroft			{
7087000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
7088000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
7089de7d4f0eSAndy Whitcroft			}
7090de7d4f0eSAndy Whitcroft
7091171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
7092000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
7093000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
7094171ae1a4SAndy Whitcroft			}
70959c9ba34eSAndy Whitcroft
70969c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
70979c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
70989c9ba34eSAndy Whitcroft		{
7099000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
7100000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
7101171ae1a4SAndy Whitcroft		}
7102171ae1a4SAndy Whitcroft
7103a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
7104a0ad7596SJoe Perches		if (defined $stat &&
7105d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
7106d8b44b58SKees Cook		    $1 ne "void") {
7107d8b44b58SKees Cook			my $args = trim($1);
7108ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
7109ca0d8929SJoe Perches				my $arg = trim($1);
7110d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
7111ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
7112ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
7113ca0d8929SJoe Perches				}
7114ca0d8929SJoe Perches			}
7115ca0d8929SJoe Perches		}
7116ca0d8929SJoe Perches
7117a0ad7596SJoe Perches# check for function definitions
71185b57980dSJoe Perches		if ($perl_version_ok &&
7119a0ad7596SJoe Perches		    defined $stat &&
7120a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
7121a0ad7596SJoe Perches			$context_function = $1;
7122a0ad7596SJoe Perches
7123a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
7124a0ad7596SJoe Perches			my $ok = 0;
7125a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
7126a0ad7596SJoe Perches			my $herectx = $here . "\n";
7127a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
7128a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
7129a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
7130a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
7131a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
7132a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
7133a0ad7596SJoe Perches			}
7134a0ad7596SJoe Perches			if (!$ok) {
7135a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
7136a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
7137a0ad7596SJoe Perches			}
7138a0ad7596SJoe Perches		}
7139a0ad7596SJoe Perches
7140de7d4f0eSAndy Whitcroft# checks for new __setup's
7141de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
7142de7d4f0eSAndy Whitcroft			my $name = $1;
7143de7d4f0eSAndy Whitcroft
7144de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
7145000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
71462581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
7147de7d4f0eSAndy Whitcroft			}
7148653d4876SAndy Whitcroft		}
71499c0ca6f9SAndy Whitcroft
7150e29a70f1SJoe Perches# check for pointless casting of alloc functions
7151e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
7152000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
7153000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
71549c0ca6f9SAndy Whitcroft		}
715513214adfSAndy Whitcroft
7156a640d25cSJoe Perches# alloc style
7157a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
71585b57980dSJoe Perches		if ($perl_version_ok &&
7159e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
7160a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
7161a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7162a640d25cSJoe Perches		}
7163a640d25cSJoe Perches
716473f1d07eSGustavo A. R. Silva# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
71655b57980dSJoe Perches		if ($perl_version_ok &&
71661b4a2ed4SJoe Perches		    defined $stat &&
716773f1d07eSGustavo A. R. Silva		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
716860a55369SJoe Perches			my $oldfunc = $3;
716960a55369SJoe Perches			my $a1 = $4;
717060a55369SJoe Perches			my $a2 = $10;
717160a55369SJoe Perches			my $newfunc = "kmalloc_array";
717273f1d07eSGustavo A. R. Silva			$newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
717373f1d07eSGustavo A. R. Silva			$newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
717460a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
717560a55369SJoe Perches			my $r1 = $a1;
717660a55369SJoe Perches			my $r2 = $a2;
717760a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
717860a55369SJoe Perches				$r1 = $a2;
717960a55369SJoe Perches				$r2 = $a1;
718060a55369SJoe Perches			}
7181e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7182e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
71831b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
7184e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
7185e3d95a2aSTobin C. Harding
7186e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
71871b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
71881b4a2ed4SJoe Perches				    $cnt == 1 &&
7189e367455aSJoe Perches				    $fix) {
719073f1d07eSGustavo 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;
719160a55369SJoe Perches				}
719260a55369SJoe Perches			}
719360a55369SJoe Perches		}
719460a55369SJoe Perches
7195972fdea2SJoe Perches# check for krealloc arg reuse
71965b57980dSJoe Perches		if ($perl_version_ok &&
71974cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
71984cab63ceSJoe Perches		    $1 eq $3) {
7199972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
7200972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7201972fdea2SJoe Perches		}
7202972fdea2SJoe Perches
72035ce59ae0SJoe Perches# check for alloc argument mismatch
72043965292aSLiao Chang		if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
72055ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
72065ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
72075ce59ae0SJoe Perches		}
72085ce59ae0SJoe Perches
7209caf2a54fSJoe Perches# check for multiple semicolons
7210caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
7211d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
7212d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7213d5e616fcSJoe Perches			    $fix) {
7214194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
7215d5e616fcSJoe Perches			}
7216d1e2ad07SJoe Perches		}
7217d1e2ad07SJoe Perches
7218cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7219cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
7220cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
72210ab90191SJoe Perches			my $ull = "";
72220ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
72230ab90191SJoe Perches			if (CHK("BIT_MACRO",
72240ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
72250ab90191SJoe Perches			    $fix) {
72260ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
72270ab90191SJoe Perches			}
72280ab90191SJoe Perches		}
72290ab90191SJoe Perches
723050161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
72313e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
723250161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
72333e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
723450161266SJoe Perches		}
723550161266SJoe Perches
72362d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
72373e89ad85SJerome 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*$/) {
72382d632745SJoe Perches			my $config = $1;
72392d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
72403e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
72412d632745SJoe Perches			    $fix) {
72422d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
72432d632745SJoe Perches			}
72442d632745SJoe Perches		}
72452d632745SJoe Perches
7246f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
7247f36d3eb8SJoe Perches		my @fallthroughs = (
7248f36d3eb8SJoe Perches			'fallthrough',
7249f36d3eb8SJoe Perches			'@fallthrough@',
7250f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
7251f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7252f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7253f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7254f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7255f36d3eb8SJoe Perches		    );
7256f36d3eb8SJoe Perches		if ($raw_comment ne '') {
7257f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
7258f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
7259f36d3eb8SJoe Perches					my $msg_level = \&WARN;
7260f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
7261f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
7262f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7263f36d3eb8SJoe Perches					last;
7264f36d3eb8SJoe Perches				}
7265f36d3eb8SJoe Perches			}
7266f36d3eb8SJoe Perches		}
7267f36d3eb8SJoe Perches
7268d1e2ad07SJoe Perches# check for switch/default statements without a break;
72695b57980dSJoe Perches		if ($perl_version_ok &&
7270d1e2ad07SJoe Perches		    defined $stat &&
7271d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
7272d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
7273e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
7274e3d95a2aSTobin C. Harding
7275d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
7276d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
7277caf2a54fSJoe Perches		}
7278caf2a54fSJoe Perches
727913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
7280d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
7281d5e616fcSJoe Perches			if (WARN("USE_FUNC",
7282d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
7283d5e616fcSJoe Perches			    $fix) {
7284194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
7285d5e616fcSJoe Perches			}
728613214adfSAndy Whitcroft		}
7287773647a0SAndy Whitcroft
728862ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
728962ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
729062ec818fSJoe Perches			ERROR("DATE_TIME",
729162ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
729262ec818fSJoe Perches		}
729362ec818fSJoe Perches
72942c92488aSJoe Perches# check for use of yield()
72952c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
72962c92488aSJoe Perches			WARN("YIELD",
72972c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
72982c92488aSJoe Perches		}
72992c92488aSJoe Perches
7300179f8f40SJoe Perches# check for comparisons against true and false
7301179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7302179f8f40SJoe Perches			my $lead = $1;
7303179f8f40SJoe Perches			my $arg = $2;
7304179f8f40SJoe Perches			my $test = $3;
7305179f8f40SJoe Perches			my $otype = $4;
7306179f8f40SJoe Perches			my $trail = $5;
7307179f8f40SJoe Perches			my $op = "!";
7308179f8f40SJoe Perches
7309179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7310179f8f40SJoe Perches
7311179f8f40SJoe Perches			my $type = lc($otype);
7312179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
7313179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
7314179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
7315179f8f40SJoe Perches					$op = "";
7316179f8f40SJoe Perches				}
7317179f8f40SJoe Perches
7318179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
7319179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
7320179f8f40SJoe Perches
7321179f8f40SJoe Perches## maybe suggesting a correct construct would better
7322179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7323179f8f40SJoe Perches
7324179f8f40SJoe Perches			}
7325179f8f40SJoe Perches		}
7326179f8f40SJoe Perches
73274882720bSThomas Gleixner# check for semaphores initialized locked
73284882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
7329000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
7330000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
7331773647a0SAndy Whitcroft		}
73326712d858SJoe Perches
733367d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
733467d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
7335000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
733667d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
7337773647a0SAndy Whitcroft		}
73386712d858SJoe Perches
7339ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
7340f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
7341000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
7342ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
7343f3db6639SMichael Ellerman		}
73446712d858SJoe Perches
73453d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
73463d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
73473d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
73483d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
73493d709ab5SPaul E. McKenney		}
73503d709ab5SPaul E. McKenney
73519189c7e7SJoe Perches# check for deprecated apis
73529189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
73539189c7e7SJoe Perches			my $deprecated_api = $1;
73549189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
73559189c7e7SJoe Perches			WARN("DEPRECATED_API",
73569189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
73579189c7e7SJoe Perches		}
73589189c7e7SJoe Perches
73590f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
7360d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
7361ced69da1SQuentin Monnet		if (defined($const_structs) &&
7362ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
7363d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7364000d1cc1SJoe Perches			WARN("CONST_STRUCT",
7365d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
73662b6db5cbSAndy Whitcroft		}
7367773647a0SAndy Whitcroft
7368773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
7369773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
737035cdcbfcSPeng Wang# ignore designated initializers using NR_CPUS
7371773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
7372c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7373c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7374171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7375171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
737635cdcbfcSPeng Wang		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
737735cdcbfcSPeng Wang		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
7378773647a0SAndy Whitcroft		{
7379000d1cc1SJoe Perches			WARN("NR_CPUS",
7380000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7381773647a0SAndy Whitcroft		}
73829c9ba34eSAndy Whitcroft
738352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
738452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
738552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
738652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
738752ea8506SJoe Perches		}
738852ea8506SJoe Perches
7389acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
73905b57980dSJoe Perches		if ($perl_version_ok &&
7391acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7392acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
7393acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7394acd9362cSJoe Perches		}
7395acd9362cSJoe Perches
7396fbe74541SJoe Perches# return sysfs_emit(foo, fmt, ...) fmt without newline
7397fbe74541SJoe Perches		if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7398fbe74541SJoe Perches		    substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7399fbe74541SJoe Perches			my $offset = $+[6] - 1;
7400fbe74541SJoe Perches			if (WARN("SYSFS_EMIT",
7401fbe74541SJoe Perches				 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7402fbe74541SJoe Perches			    $fix) {
7403fbe74541SJoe Perches				substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7404fbe74541SJoe Perches			}
7405fbe74541SJoe Perches		}
7406fbe74541SJoe Perches
7407de3f186fSDenis Efremov# nested likely/unlikely calls
7408de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7409de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
7410de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7411de3f186fSDenis Efremov		}
7412de3f186fSDenis Efremov
7413691d77b6SAndy Whitcroft# whine mightly about in_atomic
7414691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
7415691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
7416000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
7417000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
7418f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
7419000d1cc1SJoe Perches				WARN("IN_ATOMIC",
7420000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7421691d77b6SAndy Whitcroft			}
7422691d77b6SAndy Whitcroft		}
74231704f47bSPeter Zijlstra
74241704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
74251704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
74261704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
74271704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
74281704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
74291704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
7430000d1cc1SJoe Perches				ERROR("LOCKDEP",
7431000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
74321704f47bSPeter Zijlstra			}
74331704f47bSPeter Zijlstra		}
743488f8831cSDave Jones
7435b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7436b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7437000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
7438000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
743988f8831cSDave Jones		}
74402435880fSJoe Perches
744100180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
744200180468SJoe Perches# and whether or not function naming is typical and if
744300180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
74445b57980dSJoe Perches		if ($perl_version_ok &&
744500180468SJoe Perches		    defined $stat &&
744600180468SJoe 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*\)/) {
744700180468SJoe Perches			my $var = $1;
744800180468SJoe Perches			my $perms = $2;
744900180468SJoe Perches			my $show = $3;
745000180468SJoe Perches			my $store = $4;
745100180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
745200180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
745300180468SJoe Perches			    $store =~ /^${var}_store$/ &&
745400180468SJoe Perches			    $octal_perms eq "0644") {
745500180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
745600180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
745700180468SJoe Perches				    $fix) {
745800180468SJoe 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})/;
745900180468SJoe Perches				}
746000180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
746100180468SJoe Perches				 $store =~ /^NULL$/ &&
746200180468SJoe Perches				 $octal_perms eq "0444") {
746300180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
746400180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
746500180468SJoe Perches				    $fix) {
746600180468SJoe 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})/;
746700180468SJoe Perches				}
746800180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
746900180468SJoe Perches				 $store =~ /^${var}_store$/ &&
747000180468SJoe Perches				 $octal_perms eq "0200") {
747100180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
747200180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
747300180468SJoe Perches				    $fix) {
747400180468SJoe 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})/;
747500180468SJoe Perches				}
747600180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
747700180468SJoe Perches				 $octal_perms eq "0444" ||
747800180468SJoe Perches				 $octal_perms eq "0200") {
747900180468SJoe Perches				my $newshow = "$show";
748000180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
748100180468SJoe Perches				my $newstore = $store;
748200180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
748300180468SJoe Perches				my $rename = "";
748400180468SJoe Perches				if ($show ne $newshow) {
748500180468SJoe Perches					$rename .= " '$show' to '$newshow'";
748600180468SJoe Perches				}
748700180468SJoe Perches				if ($store ne $newstore) {
748800180468SJoe Perches					$rename .= " '$store' to '$newstore'";
748900180468SJoe Perches				}
749000180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
749100180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
749200180468SJoe Perches			} else {
749300180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
749400180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
749500180468SJoe Perches			}
749600180468SJoe Perches		}
749700180468SJoe Perches
7498515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
7499515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
750073121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
750173121534SJoe Perches#   specific definition of not visible in sysfs.
750273121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
750373121534SJoe Perches#   use the default permissions
75045b57980dSJoe Perches		if ($perl_version_ok &&
7505459cf0aeSJoe Perches		    defined $stat &&
7506515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
75072435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
75082435880fSJoe Perches				my $func = $entry->[0];
75092435880fSJoe Perches				my $arg_pos = $entry->[1];
75102435880fSJoe Perches
7511459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
7512459cf0aeSJoe Perches				$lc = $lc + $linenr;
75132a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
7514459cf0aeSJoe Perches
75152435880fSJoe Perches				my $skip_args = "";
75162435880fSJoe Perches				if ($arg_pos > 1) {
75172435880fSJoe Perches					$arg_pos--;
75182435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
75192435880fSJoe Perches				}
7520f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7521459cf0aeSJoe Perches				if ($stat =~ /$test/) {
75222435880fSJoe Perches					my $val = $1;
75232435880fSJoe Perches					$val = $6 if ($skip_args ne "");
752473121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
752573121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
752673121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
75272435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
7528459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7529f90774e1SJoe Perches					}
7530f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7531c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
7532459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
75332435880fSJoe Perches					}
7534459cf0aeSJoe Perches				}
7535459cf0aeSJoe Perches			}
7536459cf0aeSJoe Perches		}
7537459cf0aeSJoe Perches
7538459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
7539bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
754000180468SJoe Perches			my $oval = $1;
754100180468SJoe Perches			my $octal = perms_to_octal($oval);
7542f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
7543459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7544f90774e1SJoe Perches			    $fix) {
754500180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
75462435880fSJoe Perches			}
754713214adfSAndy Whitcroft		}
75485a6d20ceSBjorn Andersson
75495a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
75505a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
75515a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
75525a6d20ceSBjorn Andersson			my $valid_licenses = qr{
75535a6d20ceSBjorn Andersson						GPL|
75545a6d20ceSBjorn Andersson						GPL\ v2|
75555a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
75565a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
75575a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
75585a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
75595a6d20ceSBjorn Andersson						Proprietary
75605a6d20ceSBjorn Andersson					}x;
75615a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
75625a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
75635a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
75645a6d20ceSBjorn Andersson			}
75656e8f42dcSJoe Perches			if (!$file && $extracted_string eq '"GPL v2"') {
75666e8f42dcSJoe Perches				if (WARN("MODULE_LICENSE",
75676e8f42dcSJoe Perches				     "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) &&
75686e8f42dcSJoe Perches				    $fix) {
75696e8f42dcSJoe Perches					$fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/;
75706e8f42dcSJoe Perches				}
75716e8f42dcSJoe Perches			}
75725a6d20ceSBjorn Andersson		}
75736a8d76cbSMatteo Croce
75746a8d76cbSMatteo Croce# check for sysctl duplicate constants
75756a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
75766a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
75776a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
75786a8d76cbSMatteo Croce		}
7579515a235eSJoe Perches	}
758013214adfSAndy Whitcroft
758113214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
758213214adfSAndy Whitcroft	# so just keep quiet.
758313214adfSAndy Whitcroft	if ($#rawlines == -1) {
758413214adfSAndy Whitcroft		exit(0);
75850a920b5bSAndy Whitcroft	}
75860a920b5bSAndy Whitcroft
75878905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
75888905a67cSAndy Whitcroft	# things that appear to be patches.
75898905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
75908905a67cSAndy Whitcroft		exit(0);
75918905a67cSAndy Whitcroft	}
75928905a67cSAndy Whitcroft
7593e73d2715SDwaipayan Ray	# This is not a patch, and we are in 'no-patch' mode so
75948905a67cSAndy Whitcroft	# just keep quiet.
75958905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
75968905a67cSAndy Whitcroft		exit(0);
75978905a67cSAndy Whitcroft	}
75988905a67cSAndy Whitcroft
7599a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7600000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
7601000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
76020a920b5bSAndy Whitcroft	}
7603cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
7604cd261496SGeert Uytterhoeven		if ($signoff == 0) {
7605000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
7606000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
760748ca2d8aSDwaipayan Ray		} elsif ($authorsignoff != 1) {
760848ca2d8aSDwaipayan Ray			# authorsignoff values:
760948ca2d8aSDwaipayan Ray			# 0 -> missing sign off
761048ca2d8aSDwaipayan Ray			# 1 -> sign off identical
761148ca2d8aSDwaipayan Ray			# 2 -> names and addresses match, comments mismatch
761248ca2d8aSDwaipayan Ray			# 3 -> addresses match, names different
761348ca2d8aSDwaipayan Ray			# 4 -> names match, addresses different
761448ca2d8aSDwaipayan Ray			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
761548ca2d8aSDwaipayan Ray
761648ca2d8aSDwaipayan Ray			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
761748ca2d8aSDwaipayan Ray
761848ca2d8aSDwaipayan Ray			if ($authorsignoff == 0) {
761948ca2d8aSDwaipayan Ray				ERROR("NO_AUTHOR_SIGN_OFF",
7620cd261496SGeert Uytterhoeven				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
762148ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 2) {
762248ca2d8aSDwaipayan Ray				CHK("FROM_SIGN_OFF_MISMATCH",
762348ca2d8aSDwaipayan Ray				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
762448ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 3) {
762548ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
762648ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
762748ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 4) {
762848ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
762948ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
763048ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 5) {
763148ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
763248ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
763348ca2d8aSDwaipayan Ray			}
7634cd261496SGeert Uytterhoeven		}
76350a920b5bSAndy Whitcroft	}
76360a920b5bSAndy Whitcroft
7637f0a594c1SAndy Whitcroft	print report_dump();
763813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
763913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
76406c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
76416c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
76426c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
76436c72ffaaSAndy Whitcroft	}
76448905a67cSAndy Whitcroft
7645d2c0a235SAndy Whitcroft	if ($quiet == 0) {
7646ef212196SJoe Perches		# If there were any defects found and not already fixing them
7647ef212196SJoe Perches		if (!$clean and !$fix) {
7648ef212196SJoe Perches			print << "EOM"
7649ef212196SJoe Perches
7650ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
7651ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
7652ef212196SJoe PerchesEOM
7653ef212196SJoe Perches		}
7654d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
7655d2c0a235SAndy Whitcroft		# then suggest that.
7656d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
7657b0781216SMike Frysinger			$rpt_cleaners = 0;
7658d8469f16SJoe Perches			print << "EOM"
7659d8469f16SJoe Perches
7660d8469f16SJoe PerchesNOTE: Whitespace errors detected.
7661d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
7662d8469f16SJoe PerchesEOM
7663d2c0a235SAndy Whitcroft		}
7664d2c0a235SAndy Whitcroft	}
7665d2c0a235SAndy Whitcroft
7666d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
7667d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
7668d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
76699624b8d6SJoe Perches		my $newfile = $filename;
76709624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
76713705ce5bSJoe Perches		my $linecount = 0;
76723705ce5bSJoe Perches		my $f;
76733705ce5bSJoe Perches
7674d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7675d752fcc8SJoe Perches
76763705ce5bSJoe Perches		open($f, '>', $newfile)
76773705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
76783705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
76793705ce5bSJoe Perches			$linecount++;
76803705ce5bSJoe Perches			if ($file) {
76813705ce5bSJoe Perches				if ($linecount > 3) {
76823705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
76833705ce5bSJoe Perches					print $f $fixed_line . "\n";
76843705ce5bSJoe Perches				}
76853705ce5bSJoe Perches			} else {
76863705ce5bSJoe Perches				print $f $fixed_line . "\n";
76873705ce5bSJoe Perches			}
76883705ce5bSJoe Perches		}
76893705ce5bSJoe Perches		close($f);
76903705ce5bSJoe Perches
76913705ce5bSJoe Perches		if (!$quiet) {
76923705ce5bSJoe Perches			print << "EOM";
7693d8469f16SJoe Perches
76943705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
76953705ce5bSJoe Perches
76963705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
76973705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
76983705ce5bSJoe Perches
76993705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
77003705ce5bSJoe PerchesNo warranties, expressed or implied...
77013705ce5bSJoe PerchesEOM
77023705ce5bSJoe Perches		}
77033705ce5bSJoe Perches	}
77043705ce5bSJoe Perches
7705d8469f16SJoe Perches	if ($quiet == 0) {
7706d8469f16SJoe Perches		print "\n";
7707d8469f16SJoe Perches		if ($clean == 1) {
7708d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7709d8469f16SJoe Perches		} else {
7710d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
77110a920b5bSAndy Whitcroft		}
77120a920b5bSAndy Whitcroft	}
77130a920b5bSAndy Whitcroft	return $clean;
77140a920b5bSAndy Whitcroft}
7715