xref: /linux-6.15/scripts/checkpatch.pl (revision 5dbdb2d8)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0
3882ea1d6SJoe Perches#
4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]>
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftuse strict;
11cb77f0d6SKamil Rytarowskiuse warnings;
12c707a81dSJoe Perchesuse POSIX;
1336061e38SJoe Perchesuse File::Basename;
1436061e38SJoe Perchesuse Cwd 'abs_path';
1557230297SJoe Perchesuse Term::ANSIColor qw(:constants);
16cd261496SGeert Uytterhoevenuse Encode qw(decode encode);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $P = $0;
1936061e38SJoe Perchesmy $D = dirname(abs_path($P));
200a920b5bSAndy Whitcroft
21000d1cc1SJoe Perchesmy $V = '0.32';
220a920b5bSAndy Whitcroft
230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
240a920b5bSAndy Whitcroft
250a920b5bSAndy Whitcroftmy $quiet = 0;
260a920b5bSAndy Whitcroftmy $tree = 1;
270a920b5bSAndy Whitcroftmy $chk_signoff = 1;
280a920b5bSAndy Whitcroftmy $chk_patch = 1;
29773647a0SAndy Whitcroftmy $tst_only;
306c72ffaaSAndy Whitcroftmy $emacs = 0;
318905a67cSAndy Whitcroftmy $terse = 0;
3234d8815fSJoe Perchesmy $showfile = 0;
336c72ffaaSAndy Whitcroftmy $file = 0;
344a593c34SDu, Changbinmy $git = 0;
350dea9f1eSJoe Perchesmy %git_commits = ();
366c72ffaaSAndy Whitcroftmy $check = 0;
372ac73b4fSJoe Perchesmy $check_orig = 0;
388905a67cSAndy Whitcroftmy $summary = 1;
398905a67cSAndy Whitcroftmy $mailback = 0;
4013214adfSAndy Whitcroftmy $summary_file = 0;
41000d1cc1SJoe Perchesmy $show_types = 0;
423beb42ecSJoe Perchesmy $list_types = 0;
433705ce5bSJoe Perchesmy $fix = 0;
449624b8d6SJoe Perchesmy $fix_inplace = 0;
456c72ffaaSAndy Whitcroftmy $root;
460f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'};
470f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot);
48c2fdda0dSAndy Whitcroftmy %debug;
493445686aSJoe Perchesmy %camelcase = ();
5091bfe484SJoe Perchesmy %use_type = ();
5191bfe484SJoe Perchesmy @use = ();
5291bfe484SJoe Perchesmy %ignore_type = ();
53000d1cc1SJoe Perchesmy @ignore = ();
5477f5b10aSHannes Edermy $help = 0;
55000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
56bdc48fa1SJoe Perchesmy $max_line_length = 100;
57d62a201fSDave Hansenmy $ignore_perl_version = 0;
58d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5956193274SVadim Bendeburymy $min_conf_desc_length = 4;
6066b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
61ebfd7d62SJoe Perchesmy $codespell = 0;
62f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
63bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
64ced69da1SQuentin Monnetmy $typedefsfile;
65737c0767SJohn Brooksmy $color = "auto";
6698005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
67dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
68dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
69713a09deSAntonio Borneomy $tabsize = 8;
703e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_";
7177f5b10aSHannes Eder
7277f5b10aSHannes Edersub help {
7377f5b10aSHannes Eder	my ($exitcode) = @_;
7477f5b10aSHannes Eder
7577f5b10aSHannes Eder	print << "EOM";
7677f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7777f5b10aSHannes EderVersion: $V
7877f5b10aSHannes Eder
7977f5b10aSHannes EderOptions:
8077f5b10aSHannes Eder  -q, --quiet                quiet
8177f5b10aSHannes Eder  --no-tree                  run without a kernel tree
8277f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
8377f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
8477f5b10aSHannes Eder  --emacs                    emacs compile window format
8577f5b10aSHannes Eder  --terse                    one line per report
8634d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
874a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
884a593c34SDu, Changbin                             single git commit with:
894a593c34SDu, Changbin                               <rev>
904a593c34SDu, Changbin                               <rev>^
914a593c34SDu, Changbin                               <rev>~n
924a593c34SDu, Changbin                             multiple git commits with:
934a593c34SDu, Changbin                               <rev1>..<rev2>
944a593c34SDu, Changbin                               <rev1>...<rev2>
954a593c34SDu, Changbin                               <rev>-<count>
964a593c34SDu, Changbin                             git merges are ignored
9777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
993beb42ecSJoe Perches  --list-types               list the possible message types
10091bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
101000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
1023beb42ecSJoe Perches  --show-types               show the specific message type in the output
103bdc48fa1SJoe Perches  --max-line-length=n        set the maximum line length, (default $max_line_length)
104bdc48fa1SJoe Perches                             if exceeded, warn on patches
105bdc48fa1SJoe Perches                             requires --strict for use with --file
10656193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
107bdc48fa1SJoe Perches  --tab-size=n               set the number of spaces for tab (default $tabsize)
10877f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10977f5b10aSHannes Eder  --no-summary               suppress the per-file summary
11077f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
11177f5b10aSHannes Eder  --summary-file             include the filename in summary
11277f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
11377f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
11477f5b10aSHannes Eder                             is all off)
11577f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
11677f5b10aSHannes Eder                             literally
1173705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1183705ce5bSJoe Perches                             If correctable single-line errors exist, create
1193705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1203705ce5bSJoe Perches                             with potential errors corrected to the preferred
1213705ce5bSJoe Perches                             checkpatch style
1229624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1239624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1249624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
125d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
126d62a201fSDave Hansen                             runtime errors.
127ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
128f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
129ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
13075ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
131737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
132737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
1333e89ad85SJerome Forissier  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
1343e89ad85SJerome Forissier                             ${CONFIG_})
13577f5b10aSHannes Eder  -h, --help, --version      display this help and exit
13677f5b10aSHannes Eder
13777f5b10aSHannes EderWhen FILE is - read standard input.
13877f5b10aSHannes EderEOM
13977f5b10aSHannes Eder
14077f5b10aSHannes Eder	exit($exitcode);
14177f5b10aSHannes Eder}
14277f5b10aSHannes Eder
1433beb42ecSJoe Perchessub uniq {
1443beb42ecSJoe Perches	my %seen;
1453beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1463beb42ecSJoe Perches}
1473beb42ecSJoe Perches
1483beb42ecSJoe Perchessub list_types {
1493beb42ecSJoe Perches	my ($exitcode) = @_;
1503beb42ecSJoe Perches
1513beb42ecSJoe Perches	my $count = 0;
1523beb42ecSJoe Perches
1533beb42ecSJoe Perches	local $/ = undef;
1543beb42ecSJoe Perches
1553beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1563beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1573beb42ecSJoe Perches
1583beb42ecSJoe Perches	my $text = <$script>;
1593beb42ecSJoe Perches	close($script);
1603beb42ecSJoe Perches
1613beb42ecSJoe Perches	my @types = ();
1620547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1630547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1643beb42ecSJoe Perches		push (@types, $_);
1653beb42ecSJoe Perches	}
1663beb42ecSJoe Perches	@types = sort(uniq(@types));
1673beb42ecSJoe Perches	print("#\tMessage type\n\n");
1683beb42ecSJoe Perches	foreach my $type (@types) {
1693beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1703beb42ecSJoe Perches	}
1713beb42ecSJoe Perches
1723beb42ecSJoe Perches	exit($exitcode);
1733beb42ecSJoe Perches}
1743beb42ecSJoe Perches
175000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
176000d1cc1SJoe Perchesif (-f $conf) {
177000d1cc1SJoe Perches	my @conf_args;
178000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
179000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
180000d1cc1SJoe Perches
181000d1cc1SJoe Perches	while (<$conffile>) {
182000d1cc1SJoe Perches		my $line = $_;
183000d1cc1SJoe Perches
184000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
185000d1cc1SJoe Perches		$line =~ s/^\s*//g;
186000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
187000d1cc1SJoe Perches
188000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
189000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
190000d1cc1SJoe Perches
191000d1cc1SJoe Perches		my @words = split(" ", $line);
192000d1cc1SJoe Perches		foreach my $word (@words) {
193000d1cc1SJoe Perches			last if ($word =~ m/^#/);
194000d1cc1SJoe Perches			push (@conf_args, $word);
195000d1cc1SJoe Perches		}
196000d1cc1SJoe Perches	}
197000d1cc1SJoe Perches	close($conffile);
198000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
199000d1cc1SJoe Perches}
200000d1cc1SJoe Perches
201737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
202737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
203737c0767SJohn Brooksforeach (@ARGV) {
204737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
205737c0767SJohn Brooks		$_ = "--color=$color";
206737c0767SJohn Brooks	}
207737c0767SJohn Brooks}
208737c0767SJohn Brooks
2090a920b5bSAndy WhitcroftGetOptions(
2106c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2110a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2120a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2130a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2146c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2158905a67cSAndy Whitcroft	'terse!'	=> \$terse,
21634d8815fSJoe Perches	'showfile!'	=> \$showfile,
21777f5b10aSHannes Eder	'f|file!'	=> \$file,
2184a593c34SDu, Changbin	'g|git!'	=> \$git,
2196c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2206c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
221000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
22291bfe484SJoe Perches	'types=s'	=> \@use,
223000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2243beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2256cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
22656193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
227713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
2286c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2298905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2308905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
23113214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2323705ce5bSJoe Perches	'fix!'		=> \$fix,
2339624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
234d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
235c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
236773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
237ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
238ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
23975ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
240737c0767SJohn Brooks	'color=s'	=> \$color,
241737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
242737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
2433e89ad85SJerome Forissier	'kconfig-prefix=s'	=> \${CONFIG_},
24477f5b10aSHannes Eder	'h|help'	=> \$help,
24577f5b10aSHannes Eder	'version'	=> \$help
24677f5b10aSHannes Eder) or help(1);
24777f5b10aSHannes Eder
24877f5b10aSHannes Ederhelp(0) if ($help);
2490a920b5bSAndy Whitcroft
2503beb42ecSJoe Percheslist_types(0) if ($list_types);
2513beb42ecSJoe Perches
2529624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2532ac73b4fSJoe Perches$check_orig = $check;
2549624b8d6SJoe Perches
25532f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
25632f30ca9SJoe Perches
2570a920b5bSAndy Whitcroftmy $exit = 0;
2580a920b5bSAndy Whitcroft
2595b57980dSJoe Perchesmy $perl_version_ok = 1;
260d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2615b57980dSJoe Perches	$perl_version_ok = 0;
262d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2635b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
264d62a201fSDave Hansen}
265d62a201fSDave Hansen
26645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2670a920b5bSAndy Whitcroftif ($#ARGV < 0) {
26845107ff6SAllen Hubbe	push(@ARGV, '-');
2690a920b5bSAndy Whitcroft}
2700a920b5bSAndy Whitcroft
271737c0767SJohn Brooksif ($color =~ /^[01]$/) {
272737c0767SJohn Brooks	$color = !$color;
273737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
274737c0767SJohn Brooks	$color = 1;
275737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
276737c0767SJohn Brooks	$color = 0;
277737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
278737c0767SJohn Brooks	$color = (-t STDOUT);
279737c0767SJohn Brooks} else {
28032f30ca9SJoe Perches	die "$P: Invalid color mode: $color\n";
281737c0767SJohn Brooks}
282737c0767SJohn Brooks
283713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
28432f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
285713a09deSAntonio Borneo
28691bfe484SJoe Perchessub hash_save_array_words {
28791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
28891bfe484SJoe Perches
28991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
29091bfe484SJoe Perches	foreach my $word (@array) {
291000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
292000d1cc1SJoe Perches		$word =~ s/^\s*//g;
293000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
294000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
295000d1cc1SJoe Perches
296000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
297000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
298000d1cc1SJoe Perches
29991bfe484SJoe Perches		$hashRef->{$word}++;
300000d1cc1SJoe Perches	}
30191bfe484SJoe Perches}
30291bfe484SJoe Perches
30391bfe484SJoe Perchessub hash_show_words {
30491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
30591bfe484SJoe Perches
3063c816e49SJoe Perches	if (keys %$hashRef) {
307d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
30858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
30991bfe484SJoe Perches			print " $word";
31091bfe484SJoe Perches		}
311d8469f16SJoe Perches		print "\n";
31291bfe484SJoe Perches	}
31391bfe484SJoe Perches}
31491bfe484SJoe Perches
31591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
31691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
317000d1cc1SJoe Perches
318c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
319c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3207429c690SAndy Whitcroftmy $dbg_type = 0;
321a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
322c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
32321caa13cSAndy Whitcroft	## no critic
32421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
32521caa13cSAndy Whitcroft	die "$@" if ($@);
326c2fdda0dSAndy Whitcroft}
327c2fdda0dSAndy Whitcroft
328d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
329d2c0a235SAndy Whitcroft
3308905a67cSAndy Whitcroftif ($terse) {
3318905a67cSAndy Whitcroft	$emacs = 1;
3328905a67cSAndy Whitcroft	$quiet++;
3338905a67cSAndy Whitcroft}
3348905a67cSAndy Whitcroft
3356c72ffaaSAndy Whitcroftif ($tree) {
3366c72ffaaSAndy Whitcroft	if (defined $root) {
3376c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3386c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3396c72ffaaSAndy Whitcroft		}
3406c72ffaaSAndy Whitcroft	} else {
3416c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3426c72ffaaSAndy Whitcroft			$root = '.';
3436c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3446c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3456c72ffaaSAndy Whitcroft			$root = $1;
3466c72ffaaSAndy Whitcroft		}
3476c72ffaaSAndy Whitcroft	}
3486c72ffaaSAndy Whitcroft
3496c72ffaaSAndy Whitcroft	if (!defined $root) {
3500a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3510a920b5bSAndy Whitcroft		exit(2);
3520a920b5bSAndy Whitcroft	}
3536c72ffaaSAndy Whitcroft}
3546c72ffaaSAndy Whitcroft
3556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3566c72ffaaSAndy Whitcroft
3572ceb532bSAndy Whitcroftour $Ident	= qr{
3582ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3592ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3602ceb532bSAndy Whitcroft		}x;
3616c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3626c72ffaaSAndy Whitcroftour $Sparse	= qr{
3636c72ffaaSAndy Whitcroft			__user|
3646c72ffaaSAndy Whitcroft			__kernel|
3656c72ffaaSAndy Whitcroft			__force|
3666c72ffaaSAndy Whitcroft			__iomem|
3676c72ffaaSAndy Whitcroft			__must_check|
368417495edSAndy Whitcroft			__kprobes|
369165e72a6SSven Eckelmann			__ref|
37033aa4597SGeert Uytterhoeven			__refconst|
37133aa4597SGeert Uytterhoeven			__refdata|
372ad315455SBoqun Feng			__rcu|
373ad315455SBoqun Feng			__private
3746c72ffaaSAndy Whitcroft		}x;
375e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3808716de38SJoe Perches
38152131292SWolfram Sang# Notes to $Attribute:
38252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3836c72ffaaSAndy Whitcroftour $Attribute	= qr{
3846c72ffaaSAndy Whitcroft			const|
38503f1df7dSJoe Perches			__percpu|
38603f1df7dSJoe Perches			__nocast|
38703f1df7dSJoe Perches			__safe|
38846d832f5SMichael S. Tsirkin			__bitwise|
38903f1df7dSJoe Perches			__packed__|
39003f1df7dSJoe Perches			__packed2__|
39103f1df7dSJoe Perches			__naked|
39203f1df7dSJoe Perches			__maybe_unused|
39303f1df7dSJoe Perches			__always_unused|
39403f1df7dSJoe Perches			__noreturn|
39503f1df7dSJoe Perches			__used|
39603f1df7dSJoe Perches			__cold|
397e23ef1f3SJoe Perches			__pure|
39803f1df7dSJoe Perches			__noclone|
39903f1df7dSJoe Perches			__deprecated|
4006c72ffaaSAndy Whitcroft			__read_mostly|
401c5967e98SJoe Perches			__ro_after_init|
4026c72ffaaSAndy Whitcroft			__kprobes|
4038716de38SJoe Perches			$InitAttribute|
40424e1d81aSAndy Whitcroft			____cacheline_aligned|
40524e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
4065fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
4075fe3af11SAndy Whitcroft			__weak
4086c72ffaaSAndy Whitcroft		  }x;
409c45dcabdSAndy Whitcroftour $Modifier;
41091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
4116c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
4126c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
4136c72ffaaSAndy Whitcroft
41495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
41595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
41695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
41795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4182435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
419c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
420326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
421326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
422326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
42374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4242435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
425326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
426447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
42723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4286c72ffaaSAndy Whitcroftour $Operators	= qr{
4296c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4306c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
43123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4326c72ffaaSAndy Whitcroft		  }x;
4336c72ffaaSAndy Whitcroft
43491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
43591cb5195SJoe Perches
436ab7e23f3SJoe Perchesour $BasicType;
4378905a67cSAndy Whitcroftour $NonptrType;
4381813087dSJoe Perchesour $NonptrTypeMisordered;
4398716de38SJoe Perchesour $NonptrTypeWithAttr;
4408905a67cSAndy Whitcroftour $Type;
4411813087dSJoe Perchesour $TypeMisordered;
4428905a67cSAndy Whitcroftour $Declare;
4431813087dSJoe Perchesour $DeclareMisordered;
4448905a67cSAndy Whitcroft
44515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
44615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
447171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
448171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
449171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
450171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
451171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
452171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
453171ae1a4SAndy Whitcroft}x;
454171ae1a4SAndy Whitcroft
45515662b3eSJoe Perchesour $UTF8	= qr{
45615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
45715662b3eSJoe Perches	| $NON_ASCII_UTF8
45815662b3eSJoe Perches}x;
45915662b3eSJoe Perches
460e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
461021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
462021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
463021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
464021158b4SJoe Perches)};
465e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
466fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4678ed22cadSAndy Whitcroft	atomic_t
4688ed22cadSAndy Whitcroft)};
469e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
470e6176fa4SJoe Perches	$typeC99Typedefs\b|
471e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
472e6176fa4SJoe Perches	$typeKernelTypedefs\b
473e6176fa4SJoe Perches)};
4748ed22cadSAndy Whitcroft
4756d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4766d32f7a3SJoe Perches
477691e669bSJoe Perchesour $logFunctions = qr{(?x:
478758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4797d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
48087bd499aSJoe Perches	TP_printk|
4816e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
482b0531722SJoe Perches	panic|
48306668727SJoe Perches	MODULE_[A-Z_]+|
48406668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
485691e669bSJoe Perches)};
486691e669bSJoe Perches
487e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
488e29a70f1SJoe Perches	(?:(?:devm_)?
489e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
490e29a70f1SJoe Perches		kstrdup(?:_const)? |
491e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
492461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
493e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
494e29a70f1SJoe Perches	dma_alloc_coherent
495e29a70f1SJoe Perches)};
496e29a70f1SJoe Perches
49720112475SJoe Perchesour $signature_tags = qr{(?xi:
49820112475SJoe Perches	Signed-off-by:|
499d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
50020112475SJoe Perches	Acked-by:|
50120112475SJoe Perches	Tested-by:|
50220112475SJoe Perches	Reviewed-by:|
50320112475SJoe Perches	Reported-by:|
5048543ae12SMugunthan V N	Suggested-by:|
50520112475SJoe Perches	To:|
50620112475SJoe Perches	Cc:
50720112475SJoe Perches)};
50820112475SJoe Perches
509831242abSAditya Srivastavasub edit_distance_min {
510831242abSAditya Srivastava	my (@arr) = @_;
511831242abSAditya Srivastava	my $len = scalar @arr;
512831242abSAditya Srivastava	if ((scalar @arr) < 1) {
513831242abSAditya Srivastava		# if underflow, return
514831242abSAditya Srivastava		return;
515831242abSAditya Srivastava	}
516831242abSAditya Srivastava	my $min = $arr[0];
517831242abSAditya Srivastava	for my $i (0 .. ($len-1)) {
518831242abSAditya Srivastava		if ($arr[$i] < $min) {
519831242abSAditya Srivastava			$min = $arr[$i];
520831242abSAditya Srivastava		}
521831242abSAditya Srivastava	}
522831242abSAditya Srivastava	return $min;
523831242abSAditya Srivastava}
524831242abSAditya Srivastava
525831242abSAditya Srivastavasub get_edit_distance {
526831242abSAditya Srivastava	my ($str1, $str2) = @_;
527831242abSAditya Srivastava	$str1 = lc($str1);
528831242abSAditya Srivastava	$str2 = lc($str2);
529831242abSAditya Srivastava	$str1 =~ s/-//g;
530831242abSAditya Srivastava	$str2 =~ s/-//g;
531831242abSAditya Srivastava	my $len1 = length($str1);
532831242abSAditya Srivastava	my $len2 = length($str2);
533831242abSAditya Srivastava	# two dimensional array storing minimum edit distance
534831242abSAditya Srivastava	my @distance;
535831242abSAditya Srivastava	for my $i (0 .. $len1) {
536831242abSAditya Srivastava		for my $j (0 .. $len2) {
537831242abSAditya Srivastava			if ($i == 0) {
538831242abSAditya Srivastava				$distance[$i][$j] = $j;
539831242abSAditya Srivastava			} elsif ($j == 0) {
540831242abSAditya Srivastava				$distance[$i][$j] = $i;
541831242abSAditya Srivastava			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
542831242abSAditya Srivastava				$distance[$i][$j] = $distance[$i - 1][$j - 1];
543831242abSAditya Srivastava			} else {
544831242abSAditya Srivastava				my $dist1 = $distance[$i][$j - 1]; #insert distance
545831242abSAditya Srivastava				my $dist2 = $distance[$i - 1][$j]; # remove
546831242abSAditya Srivastava				my $dist3 = $distance[$i - 1][$j - 1]; #replace
547831242abSAditya Srivastava				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
548831242abSAditya Srivastava			}
549831242abSAditya Srivastava		}
550831242abSAditya Srivastava	}
551831242abSAditya Srivastava	return $distance[$len1][$len2];
552831242abSAditya Srivastava}
553831242abSAditya Srivastava
554831242abSAditya Srivastavasub find_standard_signature {
555831242abSAditya Srivastava	my ($sign_off) = @_;
556831242abSAditya Srivastava	my @standard_signature_tags = (
557831242abSAditya Srivastava		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
558831242abSAditya Srivastava		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
559831242abSAditya Srivastava	);
560831242abSAditya Srivastava	foreach my $signature (@standard_signature_tags) {
561831242abSAditya Srivastava		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
562831242abSAditya Srivastava	}
563831242abSAditya Srivastava
564831242abSAditya Srivastava	return "";
565831242abSAditya Srivastava}
566831242abSAditya Srivastava
5671813087dSJoe Perchesour @typeListMisordered = (
5681813087dSJoe Perches	qr{char\s+(?:un)?signed},
5691813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
5701813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
5711813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
5721813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
5731813087dSJoe Perches	qr{short\s+(?:un)?signed},
5741813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5751813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5761813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5771813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5781813087dSJoe Perches	qr{int\s+(?:un)?signed},
5791813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5801813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5811813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5821813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5831813087dSJoe Perches	qr{long\s+(?:un)?signed},
5841813087dSJoe Perches);
5851813087dSJoe Perches
5868905a67cSAndy Whitcroftour @typeList = (
5878905a67cSAndy Whitcroft	qr{void},
5880c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5890c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5900c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5910c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5920c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5930c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5940c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5950c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5960c773d9dSJoe Perches	qr{(?:un)?signed},
5978905a67cSAndy Whitcroft	qr{float},
5988905a67cSAndy Whitcroft	qr{double},
5998905a67cSAndy Whitcroft	qr{bool},
6008905a67cSAndy Whitcroft	qr{struct\s+$Ident},
6018905a67cSAndy Whitcroft	qr{union\s+$Ident},
6028905a67cSAndy Whitcroft	qr{enum\s+$Ident},
6038905a67cSAndy Whitcroft	qr{${Ident}_t},
6048905a67cSAndy Whitcroft	qr{${Ident}_handler},
6058905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
6061813087dSJoe Perches	@typeListMisordered,
6078905a67cSAndy Whitcroft);
608938224b5SJoe Perches
609938224b5SJoe Perchesour $C90_int_types = qr{(?x:
610938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
611938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
612938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
613938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
614938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
615938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
616938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
617938224b5SJoe Perches
618938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
619938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
620938224b5SJoe Perches	long\s+(?:un)?signed|
621938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
622938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
623938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
624938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
625938224b5SJoe Perches
626938224b5SJoe Perches	int\s+(?:un)?signed|
627938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
628938224b5SJoe Perches)};
629938224b5SJoe Perches
630485ff23eSAlex Dowadour @typeListFile = ();
6318716de38SJoe Perchesour @typeListWithAttr = (
6328716de38SJoe Perches	@typeList,
6338716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
6348716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
6358716de38SJoe Perches);
6368716de38SJoe Perches
637c45dcabdSAndy Whitcroftour @modifierList = (
638c45dcabdSAndy Whitcroft	qr{fastcall},
639c45dcabdSAndy Whitcroft);
640485ff23eSAlex Dowadour @modifierListFile = ();
6418905a67cSAndy Whitcroft
6422435880fSJoe Perchesour @mode_permission_funcs = (
6432435880fSJoe Perches	["module_param", 3],
6442435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
6452435880fSJoe Perches	["module_param_array_named", 5],
6462435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
6472435880fSJoe Perches	["proc_create(?:_data|)", 2],
648459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
649459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
650459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
651459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
652459cf0aeSJoe Perches	["__ATTR", 2],
6532435880fSJoe Perches);
6542435880fSJoe Perches
6551a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
6561a3dcf2eSJoe Perches
657515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
658515a235eSJoe Perchesour $mode_perms_search = "";
659515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
660515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
661515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
662515a235eSJoe Perches}
66300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
664515a235eSJoe Perches
6659189c7e7SJoe Perchesour %deprecated_apis = (
6669189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
6679189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
6689189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
6699189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
6709189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
6719189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
6729189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
6739189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
6749189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
6759189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6769189c7e7SJoe Perches);
6779189c7e7SJoe Perches
6789189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6799189c7e7SJoe Perchesour $deprecated_apis_search = "";
6809189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6819189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6829189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6839189c7e7SJoe Perches}
6849189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6859189c7e7SJoe Perches
686b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
687b392c64fSJoe Perches	S_IWUGO		|
688b392c64fSJoe Perches	S_IWOTH		|
689b392c64fSJoe Perches	S_IRWXUGO	|
690b392c64fSJoe Perches	S_IALLUGO	|
691b392c64fSJoe Perches	0[0-7][0-7][2367]
692b392c64fSJoe Perches}x;
693b392c64fSJoe Perches
694f90774e1SJoe Perchesour %mode_permission_string_types = (
695f90774e1SJoe Perches	"S_IRWXU" => 0700,
696f90774e1SJoe Perches	"S_IRUSR" => 0400,
697f90774e1SJoe Perches	"S_IWUSR" => 0200,
698f90774e1SJoe Perches	"S_IXUSR" => 0100,
699f90774e1SJoe Perches	"S_IRWXG" => 0070,
700f90774e1SJoe Perches	"S_IRGRP" => 0040,
701f90774e1SJoe Perches	"S_IWGRP" => 0020,
702f90774e1SJoe Perches	"S_IXGRP" => 0010,
703f90774e1SJoe Perches	"S_IRWXO" => 0007,
704f90774e1SJoe Perches	"S_IROTH" => 0004,
705f90774e1SJoe Perches	"S_IWOTH" => 0002,
706f90774e1SJoe Perches	"S_IXOTH" => 0001,
707f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
708f90774e1SJoe Perches	"S_IRUGO" => 0444,
709f90774e1SJoe Perches	"S_IWUGO" => 0222,
710f90774e1SJoe Perches	"S_IXUGO" => 0111,
711f90774e1SJoe Perches);
712f90774e1SJoe Perches
713f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
714f90774e1SJoe Perchesour $mode_perms_string_search = "";
715f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
716f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
717f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
718f90774e1SJoe Perches}
71900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
72000180468SJoe Perchesour $multi_mode_perms_string_search = qr{
72100180468SJoe Perches	${single_mode_perms_string_search}
72200180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
72300180468SJoe Perches}x;
72400180468SJoe Perches
72500180468SJoe Perchessub perms_to_octal {
72600180468SJoe Perches	my ($string) = @_;
72700180468SJoe Perches
72800180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
72900180468SJoe Perches
73000180468SJoe Perches	my $val = "";
73100180468SJoe Perches	my $oval = "";
73200180468SJoe Perches	my $to = 0;
73300180468SJoe Perches	my $curpos = 0;
73400180468SJoe Perches	my $lastpos = 0;
73500180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
73600180468SJoe Perches		$curpos = pos($string);
73700180468SJoe Perches		my $match = $2;
73800180468SJoe Perches		my $omatch = $1;
73900180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
74000180468SJoe Perches		$lastpos = $curpos;
74100180468SJoe Perches		$to |= $mode_permission_string_types{$match};
74200180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
74300180468SJoe Perches		$val .= $match;
74400180468SJoe Perches		$oval .= $omatch;
74500180468SJoe Perches	}
74600180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
74700180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
74800180468SJoe Perches	return sprintf("%04o", $to);
74900180468SJoe Perches}
750f90774e1SJoe Perches
7517840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
7527840a94cSWolfram Sang	irq|
753cdcee686SSergey Ryazanov	memory|
754cdcee686SSergey Ryazanov	time|
755cdcee686SSergey Ryazanov	reboot
7567840a94cSWolfram Sang)};
7577840a94cSWolfram Sang# memory.h: ARM has a custom one
7587840a94cSWolfram Sang
75966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
76066b47b4aSKees Cookmy $misspellings;
76166b47b4aSKees Cookmy %spelling_fix;
76236061e38SJoe Perches
76336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
76466b47b4aSKees Cook	while (<$spelling>) {
76566b47b4aSKees Cook		my $line = $_;
76666b47b4aSKees Cook
76766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
76866b47b4aSKees Cook		$line =~ s/^\s*//g;
76966b47b4aSKees Cook
77066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
77166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
77266b47b4aSKees Cook
77366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
77466b47b4aSKees Cook
77566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
77666b47b4aSKees Cook	}
77766b47b4aSKees Cook	close($spelling);
77836061e38SJoe Perches} else {
77936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
78036061e38SJoe Perches}
78166b47b4aSKees Cook
782ebfd7d62SJoe Perchesif ($codespell) {
783ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
784ebfd7d62SJoe Perches		while (<$spelling>) {
785ebfd7d62SJoe Perches			my $line = $_;
786ebfd7d62SJoe Perches
787ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
788ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
789ebfd7d62SJoe Perches
790ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
791ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
792ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
793ebfd7d62SJoe Perches
794ebfd7d62SJoe Perches			$line =~ s/,.*$//;
795ebfd7d62SJoe Perches
796ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
797ebfd7d62SJoe Perches
798ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
799ebfd7d62SJoe Perches		}
800ebfd7d62SJoe Perches		close($spelling);
801ebfd7d62SJoe Perches	} else {
802ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
803ebfd7d62SJoe Perches	}
804ebfd7d62SJoe Perches}
805ebfd7d62SJoe Perches
806ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
807ebfd7d62SJoe Perches
80875ad8c57SJerome Forissiersub read_words {
80975ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
81075ad8c57SJerome Forissier
81175ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
81275ad8c57SJerome Forissier		while (<$words>) {
813bf1fa1daSJoe Perches			my $line = $_;
814bf1fa1daSJoe Perches
815bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
816bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
817bf1fa1daSJoe Perches
818bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
819bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
820bf1fa1daSJoe Perches			if ($line =~ /\s/) {
82175ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
822bf1fa1daSJoe Perches				next;
823bf1fa1daSJoe Perches			}
824bf1fa1daSJoe Perches
825ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
82675ad8c57SJerome Forissier			$$wordsRef .= $line;
827bf1fa1daSJoe Perches		}
82875ad8c57SJerome Forissier		close($file);
82975ad8c57SJerome Forissier		return 1;
830bf1fa1daSJoe Perches	}
831bf1fa1daSJoe Perches
83275ad8c57SJerome Forissier	return 0;
83375ad8c57SJerome Forissier}
83475ad8c57SJerome Forissier
835ced69da1SQuentin Monnetmy $const_structs;
836ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
83775ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
83875ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
839ced69da1SQuentin Monnet}
84075ad8c57SJerome Forissier
841ced69da1SQuentin Monnetif (defined($typedefsfile)) {
842ced69da1SQuentin Monnet	my $typeOtherTypedefs;
84375ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
84475ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
845ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
84675ad8c57SJerome Forissier}
84775ad8c57SJerome Forissier
8488905a67cSAndy Whitcroftsub build_types {
849485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
850485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
8511813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
8528716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
853c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
854ab7e23f3SJoe Perches	$BasicType	= qr{
855ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
856ab7e23f3SJoe Perches				(?:${all}\b)
857ab7e23f3SJoe Perches		}x;
8588905a67cSAndy Whitcroft	$NonptrType	= qr{
859d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
860cf655043SAndy Whitcroft			(?:
8616b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
8628ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
863c45dcabdSAndy Whitcroft				(?:${all}\b)
864cf655043SAndy Whitcroft			)
865c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
8668905a67cSAndy Whitcroft		  }x;
8671813087dSJoe Perches	$NonptrTypeMisordered	= qr{
8681813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
8691813087dSJoe Perches			(?:
8701813087dSJoe Perches				(?:${Misordered}\b)
8711813087dSJoe Perches			)
8721813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
8731813087dSJoe Perches		  }x;
8748716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
8758716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
8768716de38SJoe Perches			(?:
8778716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8788716de38SJoe Perches				(?:$typeTypedefs\b)|
8798716de38SJoe Perches				(?:${allWithAttr}\b)
8808716de38SJoe Perches			)
8818716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8828716de38SJoe Perches		  }x;
8838905a67cSAndy Whitcroft	$Type	= qr{
884c45dcabdSAndy Whitcroft			$NonptrType
8857b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
886c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8878905a67cSAndy Whitcroft		  }x;
8881813087dSJoe Perches	$TypeMisordered	= qr{
8891813087dSJoe Perches			$NonptrTypeMisordered
8907b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
8911813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8921813087dSJoe Perches		  }x;
89391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8941813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8958905a67cSAndy Whitcroft}
8968905a67cSAndy Whitcroftbuild_types();
8976c72ffaaSAndy Whitcroft
8987d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
899d1fe9c09SJoe Perches
900d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
901d1fe9c09SJoe Perches# requires at least perl version v5.10.0
902d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
903d1fe9c09SJoe Perches
904d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
9052435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
906c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
9077d2367afSJoe Perches
908f8422308SJoe Perchesour $declaration_macros = qr{(?x:
9093e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
910fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
9113d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
912f8422308SJoe Perches)};
913f8422308SJoe Perches
9148d0325ccSAditya Srivastavaour %allow_repeated_words = (
9158d0325ccSAditya Srivastava	add => '',
9168d0325ccSAditya Srivastava	added => '',
9178d0325ccSAditya Srivastava	bad => '',
9188d0325ccSAditya Srivastava	be => '',
9198d0325ccSAditya Srivastava);
9208d0325ccSAditya Srivastava
9217d2367afSJoe Perchessub deparenthesize {
9227d2367afSJoe Perches	my ($string) = @_;
9237d2367afSJoe Perches	return "" if (!defined($string));
9245b9553abSJoe Perches
9255b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
9265b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
9275b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
9285b9553abSJoe Perches	}
9295b9553abSJoe Perches
9307d2367afSJoe Perches	$string =~ s@\s+@ @g;
9315b9553abSJoe Perches
9327d2367afSJoe Perches	return $string;
9337d2367afSJoe Perches}
9347d2367afSJoe Perches
9353445686aSJoe Perchessub seed_camelcase_file {
9363445686aSJoe Perches	my ($file) = @_;
9373445686aSJoe Perches
9383445686aSJoe Perches	return if (!(-f $file));
9393445686aSJoe Perches
9403445686aSJoe Perches	local $/;
9413445686aSJoe Perches
9423445686aSJoe Perches	open(my $include_file, '<', "$file")
9433445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
9443445686aSJoe Perches	my $text = <$include_file>;
9453445686aSJoe Perches	close($include_file);
9463445686aSJoe Perches
9473445686aSJoe Perches	my @lines = split('\n', $text);
9483445686aSJoe Perches
9493445686aSJoe Perches	foreach my $line (@lines) {
9503445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
9513445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
9523445686aSJoe Perches			$camelcase{$1} = 1;
95311ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
95411ea516aSJoe Perches			$camelcase{$1} = 1;
95511ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
9563445686aSJoe Perches			$camelcase{$1} = 1;
9573445686aSJoe Perches		}
9583445686aSJoe Perches	}
9593445686aSJoe Perches}
9603445686aSJoe Perches
961cd28b119SJoe Perchesour %maintained_status = ();
962cd28b119SJoe Perches
96385b0ee18SJoe Perchessub is_maintained_obsolete {
96485b0ee18SJoe Perches	my ($filename) = @_;
96585b0ee18SJoe Perches
966f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
96785b0ee18SJoe Perches
968cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
969cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
970cd28b119SJoe Perches	}
97185b0ee18SJoe Perches
972cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
97385b0ee18SJoe Perches}
97485b0ee18SJoe Perches
9753b6e8ac9SJoe Perchessub is_SPDX_License_valid {
9763b6e8ac9SJoe Perches	my ($license) = @_;
9773b6e8ac9SJoe Perches
9780f7f635bSJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
9793b6e8ac9SJoe Perches
98056294112SJoe Perches	my $root_path = abs_path($root);
98156294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
9823b6e8ac9SJoe Perches	return 0 if ($status ne "");
9833b6e8ac9SJoe Perches	return 1;
9843b6e8ac9SJoe Perches}
9853b6e8ac9SJoe Perches
9863445686aSJoe Perchesmy $camelcase_seeded = 0;
9873445686aSJoe Perchessub seed_camelcase_includes {
9883445686aSJoe Perches	return if ($camelcase_seeded);
9893445686aSJoe Perches
9903445686aSJoe Perches	my $files;
991c707a81dSJoe Perches	my $camelcase_cache = "";
992c707a81dSJoe Perches	my @include_files = ();
993c707a81dSJoe Perches
994c707a81dSJoe Perches	$camelcase_seeded = 1;
995351b2a1fSJoe Perches
9960f7f635bSJoe Perches	if (-e "$gitroot") {
997dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
998351b2a1fSJoe Perches		chomp $git_last_include_commit;
999c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1000c707a81dSJoe Perches	} else {
1001c707a81dSJoe Perches		my $last_mod_date = 0;
1002c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
1003c707a81dSJoe Perches		@include_files = split('\n', $files);
1004c707a81dSJoe Perches		foreach my $file (@include_files) {
1005c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
1006c707a81dSJoe Perches						   localtime((stat $file)[9]));
1007c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
1008c707a81dSJoe Perches		}
1009c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1010c707a81dSJoe Perches	}
1011c707a81dSJoe Perches
1012c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
1013c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
1014c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
1015351b2a1fSJoe Perches		while (<$camelcase_file>) {
1016351b2a1fSJoe Perches			chomp;
1017351b2a1fSJoe Perches			$camelcase{$_} = 1;
1018351b2a1fSJoe Perches		}
1019351b2a1fSJoe Perches		close($camelcase_file);
1020351b2a1fSJoe Perches
1021351b2a1fSJoe Perches		return;
1022351b2a1fSJoe Perches	}
1023c707a81dSJoe Perches
10240f7f635bSJoe Perches	if (-e "$gitroot") {
1025dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
1026c707a81dSJoe Perches		@include_files = split('\n', $files);
10273445686aSJoe Perches	}
1028c707a81dSJoe Perches
10293445686aSJoe Perches	foreach my $file (@include_files) {
10303445686aSJoe Perches		seed_camelcase_file($file);
10313445686aSJoe Perches	}
1032351b2a1fSJoe Perches
1033c707a81dSJoe Perches	if ($camelcase_cache ne "") {
1034351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
1035c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
1036c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
1037351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1038351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
1039351b2a1fSJoe Perches		}
1040351b2a1fSJoe Perches		close($camelcase_file);
1041351b2a1fSJoe Perches	}
10423445686aSJoe Perches}
10433445686aSJoe Perches
1044f5f61325SJoe Perchessub git_is_single_file {
1045f5f61325SJoe Perches	my ($filename) = @_;
1046f5f61325SJoe Perches
1047f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1048f5f61325SJoe Perches
1049f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1050f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
1051f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
1052f5f61325SJoe Perches}
1053f5f61325SJoe Perches
1054d311cd44SJoe Perchessub git_commit_info {
1055d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
1056d311cd44SJoe Perches
10570f7f635bSJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1058d311cd44SJoe Perches
1059dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1060d311cd44SJoe Perches	$output =~ s/^\s*//gm;
1061d311cd44SJoe Perches	my @lines = split("\n", $output);
1062d311cd44SJoe Perches
10630d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
10640d7835fcSJoe Perches
10655a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1066d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1067d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1068d311cd44SJoe Perches#
1069d311cd44SJoe Perches#		echo "checking commits $1..."
1070d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1071d311cd44SJoe Perches#		while read line ; do
1072d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1073d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1074d311cd44SJoe Perches#		done
1075d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
1076948b133aSHeinrich Schuchardt		$id = undef;
1077d311cd44SJoe Perches	} else {
1078d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1079d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1080d311cd44SJoe Perches	}
1081d311cd44SJoe Perches
1082d311cd44SJoe Perches	return ($id, $desc);
1083d311cd44SJoe Perches}
1084d311cd44SJoe Perches
10856c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
10860a920b5bSAndy Whitcroft
108700df344fSAndy Whitcroftmy @rawlines = ();
1088c2fdda0dSAndy Whitcroftmy @lines = ();
10893705ce5bSJoe Perchesmy @fixed = ();
1090d752fcc8SJoe Perchesmy @fixed_inserted = ();
1091d752fcc8SJoe Perchesmy @fixed_deleted = ();
1092194f66fcSJoe Perchesmy $fixlinenr = -1;
1093194f66fcSJoe Perches
10944a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10954a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10960f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
10974a593c34SDu, Changbin
10984a593c34SDu, Changbinif ($git) {
10994a593c34SDu, Changbin	my @commits = ();
11000dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
11014a593c34SDu, Changbin		my $git_range;
110228898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
110328898fd1SJoe Perches			$git_range = "-$2 $1";
11044a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
11054a593c34SDu, Changbin			$git_range = "$commit_expr";
11064a593c34SDu, Changbin		} else {
11070dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
11080dea9f1eSJoe Perches		}
1109dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
11100dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
111128898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
111228898fd1SJoe Perches			next if (!defined($1) || !defined($2));
11130dea9f1eSJoe Perches			my $sha1 = $1;
11140dea9f1eSJoe Perches			my $subject = $2;
11150dea9f1eSJoe Perches			unshift(@commits, $sha1);
11160dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
11174a593c34SDu, Changbin		}
11184a593c34SDu, Changbin	}
11194a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
11204a593c34SDu, Changbin	@ARGV = @commits;
11214a593c34SDu, Changbin}
11224a593c34SDu, Changbin
1123c2fdda0dSAndy Whitcroftmy $vname;
112498005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
11256c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
112621caa13cSAndy Whitcroft	my $FILE;
1127f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1128f5f61325SJoe Perches	my $oldfile = $file;
1129f5f61325SJoe Perches	$file = 1 if ($is_git_file);
11304a593c34SDu, Changbin	if ($git) {
11314a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
11324a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
11334a593c34SDu, Changbin	} elsif ($file) {
113421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
11356c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
113621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
113721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
11386c72ffaaSAndy Whitcroft	} else {
113921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
11406c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
11416c72ffaaSAndy Whitcroft	}
1142c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1143c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
11444a593c34SDu, Changbin	} elsif ($git) {
11450dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1146c2fdda0dSAndy Whitcroft	} else {
1147c2fdda0dSAndy Whitcroft		$vname = $filename;
1148c2fdda0dSAndy Whitcroft	}
114921caa13cSAndy Whitcroft	while (<$FILE>) {
11500a920b5bSAndy Whitcroft		chomp;
115100df344fSAndy Whitcroft		push(@rawlines, $_);
1152c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
11536c72ffaaSAndy Whitcroft	}
115421caa13cSAndy Whitcroft	close($FILE);
1155d8469f16SJoe Perches
1156d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1157d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1158d8469f16SJoe Perches		print "$vname\n";
1159d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1160d8469f16SJoe Perches	}
1161d8469f16SJoe Perches
1162c2fdda0dSAndy Whitcroft	if (!process($filename)) {
11630a920b5bSAndy Whitcroft		$exit = 1;
11640a920b5bSAndy Whitcroft	}
116500df344fSAndy Whitcroft	@rawlines = ();
116613214adfSAndy Whitcroft	@lines = ();
11673705ce5bSJoe Perches	@fixed = ();
1168d752fcc8SJoe Perches	@fixed_inserted = ();
1169d752fcc8SJoe Perches	@fixed_deleted = ();
1170194f66fcSJoe Perches	$fixlinenr = -1;
1171485ff23eSAlex Dowad	@modifierListFile = ();
1172485ff23eSAlex Dowad	@typeListFile = ();
1173485ff23eSAlex Dowad	build_types();
1174f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
11750a920b5bSAndy Whitcroft}
11760a920b5bSAndy Whitcroft
1177d8469f16SJoe Perchesif (!$quiet) {
11783c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
11793c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
11803c816e49SJoe Perches
11815b57980dSJoe Perches	if (!$perl_version_ok) {
1182d8469f16SJoe Perches		print << "EOM"
1183d8469f16SJoe Perches
1184d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
11855b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1186d8469f16SJoe PerchesEOM
1187d8469f16SJoe Perches	}
1188d8469f16SJoe Perches	if ($exit) {
1189d8469f16SJoe Perches		print << "EOM"
1190d8469f16SJoe Perches
1191d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1192d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1193d8469f16SJoe PerchesEOM
1194d8469f16SJoe Perches	}
1195d8469f16SJoe Perches}
1196d8469f16SJoe Perches
11970a920b5bSAndy Whitcroftexit($exit);
11980a920b5bSAndy Whitcroft
11990a920b5bSAndy Whitcroftsub top_of_kernel_tree {
12006c72ffaaSAndy Whitcroft	my ($root) = @_;
12016c72ffaaSAndy Whitcroft
12026c72ffaaSAndy Whitcroft	my @tree_check = (
12036c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
12046c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
12056c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
12066c72ffaaSAndy Whitcroft	);
12076c72ffaaSAndy Whitcroft
12086c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
12096c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
12100a920b5bSAndy Whitcroft			return 0;
12110a920b5bSAndy Whitcroft		}
12126c72ffaaSAndy Whitcroft	}
12136c72ffaaSAndy Whitcroft	return 1;
12146c72ffaaSAndy Whitcroft}
12150a920b5bSAndy Whitcroft
121620112475SJoe Perchessub parse_email {
121720112475SJoe Perches	my ($formatted_email) = @_;
121820112475SJoe Perches
121920112475SJoe Perches	my $name = "";
1220fccaebf0SDwaipayan Ray	my $quoted = "";
1221dfa05c28SJoe Perches	my $name_comment = "";
122220112475SJoe Perches	my $address = "";
122320112475SJoe Perches	my $comment = "";
122420112475SJoe Perches
122520112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
122620112475SJoe Perches		$name = $1;
122720112475SJoe Perches		$address = $2;
122820112475SJoe Perches		$comment = $3 if defined $3;
122920112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
123020112475SJoe Perches		$address = $1;
123120112475SJoe Perches		$comment = $2 if defined $2;
123220112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
123320112475SJoe Perches		$address = $1;
123420112475SJoe Perches		$comment = $2 if defined $2;
123585e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
123620112475SJoe Perches		$name = $formatted_email;
12373705ce5bSJoe Perches		$name = trim($name);
123820112475SJoe Perches		$name =~ s/^\"|\"$//g;
123920112475SJoe Perches		# If there's a name left after stripping spaces and
124020112475SJoe Perches		# leading quotes, and the address doesn't have both
124120112475SJoe Perches		# leading and trailing angle brackets, the address
124220112475SJoe Perches		# is invalid. ie:
124320112475SJoe Perches		#   "joe smith [email protected]" bad
124420112475SJoe Perches		#   "joe smith <[email protected]" bad
124520112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
124620112475SJoe Perches			$name = "";
124720112475SJoe Perches			$address = "";
124820112475SJoe Perches			$comment = "";
124920112475SJoe Perches		}
125020112475SJoe Perches	}
125120112475SJoe Perches
1252fccaebf0SDwaipayan Ray	# Extract comments from names excluding quoted parts
1253fccaebf0SDwaipayan Ray	# "John D. (Doe)" - Do not extract
1254fccaebf0SDwaipayan Ray	if ($name =~ s/\"(.+)\"//) {
1255fccaebf0SDwaipayan Ray		$quoted = $1;
1256dfa05c28SJoe Perches	}
1257fccaebf0SDwaipayan Ray	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1258fccaebf0SDwaipayan Ray		$name_comment .= trim($1);
1259fccaebf0SDwaipayan Ray	}
1260fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
1261fccaebf0SDwaipayan Ray	$name = trim("$quoted $name");
1262fccaebf0SDwaipayan Ray
12633705ce5bSJoe Perches	$address = trim($address);
126420112475SJoe Perches	$address =~ s/^\<|\>$//g;
1265fccaebf0SDwaipayan Ray	$comment = trim($comment);
126620112475SJoe Perches
126720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
126820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
126920112475SJoe Perches		$name = "\"$name\"";
127020112475SJoe Perches	}
127120112475SJoe Perches
1272dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
127320112475SJoe Perches}
127420112475SJoe Perches
127520112475SJoe Perchessub format_email {
127648ca2d8aSDwaipayan Ray	my ($name, $name_comment, $address, $comment) = @_;
127720112475SJoe Perches
127820112475SJoe Perches	my $formatted_email;
127920112475SJoe Perches
1280fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
12813705ce5bSJoe Perches	$address = trim($address);
1282fccaebf0SDwaipayan Ray	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
128320112475SJoe Perches
128420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
128520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
128620112475SJoe Perches		$name = "\"$name\"";
128720112475SJoe Perches	}
128820112475SJoe Perches
1289fccaebf0SDwaipayan Ray	$name_comment = trim($name_comment);
1290fccaebf0SDwaipayan Ray	$name_comment = " $name_comment" if ($name_comment ne "");
1291fccaebf0SDwaipayan Ray	$comment = trim($comment);
1292fccaebf0SDwaipayan Ray	$comment = " $comment" if ($comment ne "");
1293fccaebf0SDwaipayan Ray
129420112475SJoe Perches	if ("$name" eq "") {
129520112475SJoe Perches		$formatted_email = "$address";
129620112475SJoe Perches	} else {
129748ca2d8aSDwaipayan Ray		$formatted_email = "$name$name_comment <$address>";
129820112475SJoe Perches	}
129948ca2d8aSDwaipayan Ray	$formatted_email .= "$comment";
130020112475SJoe Perches	return $formatted_email;
130120112475SJoe Perches}
130220112475SJoe Perches
1303dfa05c28SJoe Perchessub reformat_email {
1304dfa05c28SJoe Perches	my ($email) = @_;
1305dfa05c28SJoe Perches
1306dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
130748ca2d8aSDwaipayan Ray	return format_email($email_name, $name_comment, $email_address, $comment);
1308dfa05c28SJoe Perches}
1309dfa05c28SJoe Perches
1310dfa05c28SJoe Perchessub same_email_addresses {
1311fccaebf0SDwaipayan Ray	my ($email1, $email2) = @_;
1312dfa05c28SJoe Perches
1313dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1314dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1315dfa05c28SJoe Perches
131648ca2d8aSDwaipayan Ray	return $email1_name eq $email2_name &&
131748ca2d8aSDwaipayan Ray	       $email1_address eq $email2_address &&
131848ca2d8aSDwaipayan Ray	       $name1_comment eq $name2_comment &&
131948ca2d8aSDwaipayan Ray	       $comment1 eq $comment2;
132048ca2d8aSDwaipayan Ray}
1321dfa05c28SJoe Perches
1322d311cd44SJoe Perchessub which {
1323d311cd44SJoe Perches	my ($bin) = @_;
1324d311cd44SJoe Perches
1325d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1326d311cd44SJoe Perches		if (-e "$path/$bin") {
1327d311cd44SJoe Perches			return "$path/$bin";
1328d311cd44SJoe Perches		}
1329d311cd44SJoe Perches	}
1330d311cd44SJoe Perches
1331d311cd44SJoe Perches	return "";
1332d311cd44SJoe Perches}
1333d311cd44SJoe Perches
1334000d1cc1SJoe Perchessub which_conf {
1335000d1cc1SJoe Perches	my ($conf) = @_;
1336000d1cc1SJoe Perches
1337000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1338000d1cc1SJoe Perches		if (-e "$path/$conf") {
1339000d1cc1SJoe Perches			return "$path/$conf";
1340000d1cc1SJoe Perches		}
1341000d1cc1SJoe Perches	}
1342000d1cc1SJoe Perches
1343000d1cc1SJoe Perches	return "";
1344000d1cc1SJoe Perches}
1345000d1cc1SJoe Perches
13460a920b5bSAndy Whitcroftsub expand_tabs {
13470a920b5bSAndy Whitcroft	my ($str) = @_;
13480a920b5bSAndy Whitcroft
13490a920b5bSAndy Whitcroft	my $res = '';
13500a920b5bSAndy Whitcroft	my $n = 0;
13510a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
13520a920b5bSAndy Whitcroft		if ($c eq "\t") {
13530a920b5bSAndy Whitcroft			$res .= ' ';
13540a920b5bSAndy Whitcroft			$n++;
1355713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
13560a920b5bSAndy Whitcroft				$res .= ' ';
13570a920b5bSAndy Whitcroft			}
13580a920b5bSAndy Whitcroft			next;
13590a920b5bSAndy Whitcroft		}
13600a920b5bSAndy Whitcroft		$res .= $c;
13610a920b5bSAndy Whitcroft		$n++;
13620a920b5bSAndy Whitcroft	}
13630a920b5bSAndy Whitcroft
13640a920b5bSAndy Whitcroft	return $res;
13650a920b5bSAndy Whitcroft}
13666c72ffaaSAndy Whitcroftsub copy_spacing {
1367773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
13686c72ffaaSAndy Whitcroft	return $res;
13696c72ffaaSAndy Whitcroft}
13700a920b5bSAndy Whitcroft
13714a0df2efSAndy Whitcroftsub line_stats {
13724a0df2efSAndy Whitcroft	my ($line) = @_;
13734a0df2efSAndy Whitcroft
13744a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
13754a0df2efSAndy Whitcroft	$line =~ s/^.//;
13764a0df2efSAndy Whitcroft	$line = expand_tabs($line);
13774a0df2efSAndy Whitcroft
13784a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
13794a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
13804a0df2efSAndy Whitcroft
13814a0df2efSAndy Whitcroft	return (length($line), length($white));
13824a0df2efSAndy Whitcroft}
13834a0df2efSAndy Whitcroft
1384773647a0SAndy Whitcroftmy $sanitise_quote = '';
1385773647a0SAndy Whitcroft
1386773647a0SAndy Whitcroftsub sanitise_line_reset {
1387773647a0SAndy Whitcroft	my ($in_comment) = @_;
1388773647a0SAndy Whitcroft
1389773647a0SAndy Whitcroft	if ($in_comment) {
1390773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1391773647a0SAndy Whitcroft	} else {
1392773647a0SAndy Whitcroft		$sanitise_quote = '';
1393773647a0SAndy Whitcroft	}
1394773647a0SAndy Whitcroft}
139500df344fSAndy Whitcroftsub sanitise_line {
139600df344fSAndy Whitcroft	my ($line) = @_;
139700df344fSAndy Whitcroft
139800df344fSAndy Whitcroft	my $res = '';
139900df344fSAndy Whitcroft	my $l = '';
140000df344fSAndy Whitcroft
1401c2fdda0dSAndy Whitcroft	my $qlen = 0;
1402773647a0SAndy Whitcroft	my $off = 0;
1403773647a0SAndy Whitcroft	my $c;
140400df344fSAndy Whitcroft
1405773647a0SAndy Whitcroft	# Always copy over the diff marker.
1406773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1407773647a0SAndy Whitcroft
1408773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1409773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1410773647a0SAndy Whitcroft
14118d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1412773647a0SAndy Whitcroft		# and end, all to $;.
1413773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1414773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1415773647a0SAndy Whitcroft
1416773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1417773647a0SAndy Whitcroft			$off++;
141800df344fSAndy Whitcroft			next;
1419773647a0SAndy Whitcroft		}
142081bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1421773647a0SAndy Whitcroft			$sanitise_quote = '';
1422773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1423773647a0SAndy Whitcroft			$off++;
1424773647a0SAndy Whitcroft			next;
1425773647a0SAndy Whitcroft		}
1426113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1427113f04a8SDaniel Walker			$sanitise_quote = '//';
1428113f04a8SDaniel Walker
1429113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1430113f04a8SDaniel Walker			$off++;
1431113f04a8SDaniel Walker			next;
1432113f04a8SDaniel Walker		}
1433773647a0SAndy Whitcroft
1434773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1435773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1436773647a0SAndy Whitcroft		    $c eq "\\") {
1437773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1438773647a0SAndy Whitcroft			$off++;
1439773647a0SAndy Whitcroft			next;
1440773647a0SAndy Whitcroft		}
1441773647a0SAndy Whitcroft		# Regular quotes.
1442773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1443773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1444773647a0SAndy Whitcroft				$sanitise_quote = $c;
1445773647a0SAndy Whitcroft
1446773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1447773647a0SAndy Whitcroft				next;
1448773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1449773647a0SAndy Whitcroft				$sanitise_quote = '';
145000df344fSAndy Whitcroft			}
145100df344fSAndy Whitcroft		}
1452773647a0SAndy Whitcroft
1453fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1454773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1455773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1456113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1457113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1458773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1459773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
146000df344fSAndy Whitcroft		} else {
1461773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
146200df344fSAndy Whitcroft		}
1463c2fdda0dSAndy Whitcroft	}
1464c2fdda0dSAndy Whitcroft
1465113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1466113f04a8SDaniel Walker		$sanitise_quote = '';
1467113f04a8SDaniel Walker	}
1468113f04a8SDaniel Walker
1469c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1470c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1471c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1472c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1473c2fdda0dSAndy Whitcroft
1474c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1475c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1476c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1477c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1478c2fdda0dSAndy Whitcroft	}
1479c2fdda0dSAndy Whitcroft
1480dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1481dadf680dSJoe Perches		my $match = $1;
1482dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1483dadf680dSJoe Perches	}
1484dadf680dSJoe Perches
148500df344fSAndy Whitcroft	return $res;
148600df344fSAndy Whitcroft}
148700df344fSAndy Whitcroft
1488a6962d72SJoe Perchessub get_quoted_string {
1489a6962d72SJoe Perches	my ($line, $rawline) = @_;
1490a6962d72SJoe Perches
1491478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
149233acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1493a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1494a6962d72SJoe Perches}
1495a6962d72SJoe Perches
14968905a67cSAndy Whitcroftsub ctx_statement_block {
14978905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
14988905a67cSAndy Whitcroft	my $line = $linenr - 1;
14998905a67cSAndy Whitcroft	my $blk = '';
15008905a67cSAndy Whitcroft	my $soff = $off;
15018905a67cSAndy Whitcroft	my $coff = $off - 1;
1502773647a0SAndy Whitcroft	my $coff_set = 0;
15038905a67cSAndy Whitcroft
150413214adfSAndy Whitcroft	my $loff = 0;
150513214adfSAndy Whitcroft
15068905a67cSAndy Whitcroft	my $type = '';
15078905a67cSAndy Whitcroft	my $level = 0;
1508a2750645SAndy Whitcroft	my @stack = ();
1509cf655043SAndy Whitcroft	my $p;
15108905a67cSAndy Whitcroft	my $c;
15118905a67cSAndy Whitcroft	my $len = 0;
151213214adfSAndy Whitcroft
151313214adfSAndy Whitcroft	my $remainder;
15148905a67cSAndy Whitcroft	while (1) {
1515a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1516a2750645SAndy Whitcroft
1517773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
15188905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
15198905a67cSAndy Whitcroft		# context.
15208905a67cSAndy Whitcroft		if ($off >= $len) {
15218905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1522dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1523c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
15248905a67cSAndy Whitcroft				$remain--;
152513214adfSAndy Whitcroft				$loff = $len;
1526c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
15278905a67cSAndy Whitcroft				$len = length($blk);
15288905a67cSAndy Whitcroft				$line++;
15298905a67cSAndy Whitcroft				last;
15308905a67cSAndy Whitcroft			}
15318905a67cSAndy Whitcroft			# Bail if there is no further context.
15328905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
153313214adfSAndy Whitcroft			if ($off >= $len) {
15348905a67cSAndy Whitcroft				last;
15358905a67cSAndy Whitcroft			}
1536f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1537f74bd194SAndy Whitcroft				$level++;
1538f74bd194SAndy Whitcroft				$type = '#';
1539f74bd194SAndy Whitcroft			}
15408905a67cSAndy Whitcroft		}
1541cf655043SAndy Whitcroft		$p = $c;
15428905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
154313214adfSAndy Whitcroft		$remainder = substr($blk, $off);
15448905a67cSAndy Whitcroft
1545773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
15464635f4fbSAndy Whitcroft
15474635f4fbSAndy Whitcroft		# Handle nested #if/#else.
15484635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
15494635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
15504635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
15514635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
15524635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
15534635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
15544635f4fbSAndy Whitcroft		}
15554635f4fbSAndy Whitcroft
15568905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
15578905a67cSAndy Whitcroft		# outermost level.
15588905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
15598905a67cSAndy Whitcroft			last;
15608905a67cSAndy Whitcroft		}
15618905a67cSAndy Whitcroft
156213214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1563773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1564773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1565773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1566773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1567773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1568773647a0SAndy Whitcroft			$coff_set = 1;
1569773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1570773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
157113214adfSAndy Whitcroft		}
157213214adfSAndy Whitcroft
15738905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
15748905a67cSAndy Whitcroft			$level++;
15758905a67cSAndy Whitcroft			$type = '(';
15768905a67cSAndy Whitcroft		}
15778905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
15788905a67cSAndy Whitcroft			$level--;
15798905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
15808905a67cSAndy Whitcroft
15818905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
15828905a67cSAndy Whitcroft				$coff = $off;
1583773647a0SAndy Whitcroft				$coff_set = 1;
1584773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
15858905a67cSAndy Whitcroft			}
15868905a67cSAndy Whitcroft		}
15878905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
15888905a67cSAndy Whitcroft			$level++;
15898905a67cSAndy Whitcroft			$type = '{';
15908905a67cSAndy Whitcroft		}
15918905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
15928905a67cSAndy Whitcroft			$level--;
15938905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
15948905a67cSAndy Whitcroft
15958905a67cSAndy Whitcroft			if ($level == 0) {
1596b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1597b998e001SPatrick Pannuto					$off++;
1598b998e001SPatrick Pannuto				}
15998905a67cSAndy Whitcroft				last;
16008905a67cSAndy Whitcroft			}
16018905a67cSAndy Whitcroft		}
1602f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1603f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1604f74bd194SAndy Whitcroft			$level--;
1605f74bd194SAndy Whitcroft			$type = '';
1606f74bd194SAndy Whitcroft			$off++;
1607f74bd194SAndy Whitcroft			last;
1608f74bd194SAndy Whitcroft		}
16098905a67cSAndy Whitcroft		$off++;
16108905a67cSAndy Whitcroft	}
1611a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
161213214adfSAndy Whitcroft	if ($off == $len) {
1613a3bb97a7SAndy Whitcroft		$loff = $len + 1;
161413214adfSAndy Whitcroft		$line++;
161513214adfSAndy Whitcroft		$remain--;
161613214adfSAndy Whitcroft	}
16178905a67cSAndy Whitcroft
16188905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
16198905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
16208905a67cSAndy Whitcroft
16218905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
16228905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
16238905a67cSAndy Whitcroft
1624773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
162513214adfSAndy Whitcroft
162613214adfSAndy Whitcroft	return ($statement, $condition,
162713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
162813214adfSAndy Whitcroft}
162913214adfSAndy Whitcroft
1630cf655043SAndy Whitcroftsub statement_lines {
1631cf655043SAndy Whitcroft	my ($stmt) = @_;
1632cf655043SAndy Whitcroft
1633cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1634cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1635cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1636cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1637cf655043SAndy Whitcroft
1638cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1639cf655043SAndy Whitcroft
1640cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1641cf655043SAndy Whitcroft}
1642cf655043SAndy Whitcroft
1643cf655043SAndy Whitcroftsub statement_rawlines {
1644cf655043SAndy Whitcroft	my ($stmt) = @_;
1645cf655043SAndy Whitcroft
1646cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1647cf655043SAndy Whitcroft
1648cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1649cf655043SAndy Whitcroft}
1650cf655043SAndy Whitcroft
1651cf655043SAndy Whitcroftsub statement_block_size {
1652cf655043SAndy Whitcroft	my ($stmt) = @_;
1653cf655043SAndy Whitcroft
1654cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1655cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1656cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1657cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1658cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1659cf655043SAndy Whitcroft
1660cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1661cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1662cf655043SAndy Whitcroft
1663cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1664cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1665cf655043SAndy Whitcroft
1666cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1667cf655043SAndy Whitcroft		return $stmt_lines;
1668cf655043SAndy Whitcroft	} else {
1669cf655043SAndy Whitcroft		return $stmt_statements;
1670cf655043SAndy Whitcroft	}
1671cf655043SAndy Whitcroft}
1672cf655043SAndy Whitcroft
167313214adfSAndy Whitcroftsub ctx_statement_full {
167413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
167513214adfSAndy Whitcroft	my ($statement, $condition, $level);
167613214adfSAndy Whitcroft
167713214adfSAndy Whitcroft	my (@chunks);
167813214adfSAndy Whitcroft
1679cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
168013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
168113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1682773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
168313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1684cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1685cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1686cf655043SAndy Whitcroft	}
1687cf655043SAndy Whitcroft
1688cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1689cf655043SAndy Whitcroft	# could continue the statement.
1690cf655043SAndy Whitcroft	for (;;) {
169113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
169213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1693cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1694773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1695cf655043SAndy Whitcroft		#print "C: push\n";
1696cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
169713214adfSAndy Whitcroft	}
169813214adfSAndy Whitcroft
169913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
17008905a67cSAndy Whitcroft}
17018905a67cSAndy Whitcroft
17024a0df2efSAndy Whitcroftsub ctx_block_get {
1703f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
17044a0df2efSAndy Whitcroft	my $line;
17054a0df2efSAndy Whitcroft	my $start = $linenr - 1;
17064a0df2efSAndy Whitcroft	my $blk = '';
17074a0df2efSAndy Whitcroft	my @o;
17084a0df2efSAndy Whitcroft	my @c;
17094a0df2efSAndy Whitcroft	my @res = ();
17104a0df2efSAndy Whitcroft
1711f0a594c1SAndy Whitcroft	my $level = 0;
17124635f4fbSAndy Whitcroft	my @stack = ($level);
171300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
171400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
171500df344fSAndy Whitcroft		$remain--;
171600df344fSAndy Whitcroft
171700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
17184635f4fbSAndy Whitcroft
17194635f4fbSAndy Whitcroft		# Handle nested #if/#else.
172001464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
17214635f4fbSAndy Whitcroft			push(@stack, $level);
172201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
17234635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
172401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
17254635f4fbSAndy Whitcroft			$level = pop(@stack);
17264635f4fbSAndy Whitcroft		}
17274635f4fbSAndy Whitcroft
172801464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1729f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1730f0a594c1SAndy Whitcroft			if ($off > 0) {
1731f0a594c1SAndy Whitcroft				$off--;
1732f0a594c1SAndy Whitcroft				next;
1733f0a594c1SAndy Whitcroft			}
17344a0df2efSAndy Whitcroft
1735f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1736f0a594c1SAndy Whitcroft				$level--;
1737f0a594c1SAndy Whitcroft				last if ($level == 0);
1738f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1739f0a594c1SAndy Whitcroft				$level++;
1740f0a594c1SAndy Whitcroft			}
1741f0a594c1SAndy Whitcroft		}
17424a0df2efSAndy Whitcroft
1743f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
174400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
17454a0df2efSAndy Whitcroft		}
17464a0df2efSAndy Whitcroft
1747f0a594c1SAndy Whitcroft		last if ($level == 0);
17484a0df2efSAndy Whitcroft	}
17494a0df2efSAndy Whitcroft
1750f0a594c1SAndy Whitcroft	return ($level, @res);
17514a0df2efSAndy Whitcroft}
17524a0df2efSAndy Whitcroftsub ctx_block_outer {
17534a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
17544a0df2efSAndy Whitcroft
1755f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1756f0a594c1SAndy Whitcroft	return @r;
17574a0df2efSAndy Whitcroft}
17584a0df2efSAndy Whitcroftsub ctx_block {
17594a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
17604a0df2efSAndy Whitcroft
1761f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1762f0a594c1SAndy Whitcroft	return @r;
1763653d4876SAndy Whitcroft}
1764653d4876SAndy Whitcroftsub ctx_statement {
1765f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1766f0a594c1SAndy Whitcroft
1767f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1768f0a594c1SAndy Whitcroft	return @r;
1769f0a594c1SAndy Whitcroft}
1770f0a594c1SAndy Whitcroftsub ctx_block_level {
1771653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1772653d4876SAndy Whitcroft
1773f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
17744a0df2efSAndy Whitcroft}
17759c0ca6f9SAndy Whitcroftsub ctx_statement_level {
17769c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
17779c0ca6f9SAndy Whitcroft
17789c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
17799c0ca6f9SAndy Whitcroft}
17804a0df2efSAndy Whitcroft
17814a0df2efSAndy Whitcroftsub ctx_locate_comment {
17824a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17834a0df2efSAndy Whitcroft
1784a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1785a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1786a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1787a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1788a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1789a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1790a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1791a55ee0ccSJoe Perches
17924a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1793a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
17944a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
17954a0df2efSAndy Whitcroft
17964a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
17974a0df2efSAndy Whitcroft	# comment.
17984a0df2efSAndy Whitcroft	my $in_comment = 0;
17994a0df2efSAndy Whitcroft	$current_comment = '';
18004a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
180100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
180200df344fSAndy Whitcroft		#warn "           $line\n";
18034a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
18044a0df2efSAndy Whitcroft			$in_comment = 1;
18054a0df2efSAndy Whitcroft		}
18064a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
18074a0df2efSAndy Whitcroft			$in_comment = 1;
18084a0df2efSAndy Whitcroft		}
18094a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
18104a0df2efSAndy Whitcroft			$current_comment = '';
18114a0df2efSAndy Whitcroft		}
18124a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
18134a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
18144a0df2efSAndy Whitcroft			$in_comment = 0;
18154a0df2efSAndy Whitcroft		}
18164a0df2efSAndy Whitcroft	}
18174a0df2efSAndy Whitcroft
18184a0df2efSAndy Whitcroft	chomp($current_comment);
18194a0df2efSAndy Whitcroft	return($current_comment);
18204a0df2efSAndy Whitcroft}
18214a0df2efSAndy Whitcroftsub ctx_has_comment {
18224a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
18234a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
18244a0df2efSAndy Whitcroft
182500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
18264a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
18274a0df2efSAndy Whitcroft
18284a0df2efSAndy Whitcroft	return ($cmt ne '');
18294a0df2efSAndy Whitcroft}
18304a0df2efSAndy Whitcroft
18314d001e4dSAndy Whitcroftsub raw_line {
18324d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
18334d001e4dSAndy Whitcroft
18344d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
18354d001e4dSAndy Whitcroft	$cnt++;
18364d001e4dSAndy Whitcroft
18374d001e4dSAndy Whitcroft	my $line;
18384d001e4dSAndy Whitcroft	while ($cnt) {
18394d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
18404d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
18414d001e4dSAndy Whitcroft		$cnt--;
18424d001e4dSAndy Whitcroft	}
18434d001e4dSAndy Whitcroft
18444d001e4dSAndy Whitcroft	return $line;
18454d001e4dSAndy Whitcroft}
18464d001e4dSAndy Whitcroft
18472a9f9d85STobin C. Hardingsub get_stat_real {
18482a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
18492a9f9d85STobin C. Harding
18502a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
18512a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
18522a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
18532a9f9d85STobin C. Harding	}
18542a9f9d85STobin C. Harding
18552a9f9d85STobin C. Harding	return $stat_real;
18562a9f9d85STobin C. Harding}
18572a9f9d85STobin C. Harding
1858e3d95a2aSTobin C. Hardingsub get_stat_here {
1859e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1860e3d95a2aSTobin C. Harding
1861e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1862e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1863e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1864e3d95a2aSTobin C. Harding	}
1865e3d95a2aSTobin C. Harding
1866e3d95a2aSTobin C. Harding	return $herectx;
1867e3d95a2aSTobin C. Harding}
1868e3d95a2aSTobin C. Harding
18690a920b5bSAndy Whitcroftsub cat_vet {
18700a920b5bSAndy Whitcroft	my ($vet) = @_;
18719c0ca6f9SAndy Whitcroft	my ($res, $coded);
18720a920b5bSAndy Whitcroft
18739c0ca6f9SAndy Whitcroft	$res = '';
18746c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
18756c72ffaaSAndy Whitcroft		$res .= $1;
18766c72ffaaSAndy Whitcroft		if ($2 ne '') {
18779c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
18786c72ffaaSAndy Whitcroft			$res .= $coded;
18796c72ffaaSAndy Whitcroft		}
18809c0ca6f9SAndy Whitcroft	}
18819c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
18820a920b5bSAndy Whitcroft
18839c0ca6f9SAndy Whitcroft	return $res;
18840a920b5bSAndy Whitcroft}
18850a920b5bSAndy Whitcroft
1886c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1887cf655043SAndy Whitcroftmy $av_pending;
1888c2fdda0dSAndy Whitcroftmy @av_paren_type;
18891f65f947SAndy Whitcroftmy $av_pend_colon;
1890c2fdda0dSAndy Whitcroft
1891c2fdda0dSAndy Whitcroftsub annotate_reset {
1892c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1893cf655043SAndy Whitcroft	$av_pending = '_';
1894cf655043SAndy Whitcroft	@av_paren_type = ('E');
18951f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1896c2fdda0dSAndy Whitcroft}
1897c2fdda0dSAndy Whitcroft
18986c72ffaaSAndy Whitcroftsub annotate_values {
18996c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
19006c72ffaaSAndy Whitcroft
19016c72ffaaSAndy Whitcroft	my $res;
19021f65f947SAndy Whitcroft	my $var = '_' x length($stream);
19036c72ffaaSAndy Whitcroft	my $cur = $stream;
19046c72ffaaSAndy Whitcroft
1905c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
19066c72ffaaSAndy Whitcroft
19076c72ffaaSAndy Whitcroft	while (length($cur)) {
1908773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1909cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1910171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
19116c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1912c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1913c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1914cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1915c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
19166c72ffaaSAndy Whitcroft			}
19176c72ffaaSAndy Whitcroft
1918c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
19199446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
19209446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1921addcdceaSAndy Whitcroft			$type = 'c';
19229446ef56SAndy Whitcroft
1923e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1924c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
19256c72ffaaSAndy Whitcroft			$type = 'T';
19266c72ffaaSAndy Whitcroft
1927389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1928389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1929389a2fe5SAndy Whitcroft			$type = 'T';
1930389a2fe5SAndy Whitcroft
1931c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1932171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1933c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1934171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1935171ae1a4SAndy Whitcroft			if ($2 ne '') {
1936cf655043SAndy Whitcroft				$av_pending = 'N';
1937171ae1a4SAndy Whitcroft			}
1938171ae1a4SAndy Whitcroft			$type = 'E';
1939171ae1a4SAndy Whitcroft
1940c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1941171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1942171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1943171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
19446c72ffaaSAndy Whitcroft
1945c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1946cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1947c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1948cf655043SAndy Whitcroft
1949cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1950cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1951171ae1a4SAndy Whitcroft			$type = 'E';
1952cf655043SAndy Whitcroft
1953c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1954cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1955cf655043SAndy Whitcroft			$av_preprocessor = 1;
1956cf655043SAndy Whitcroft
1957cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1958cf655043SAndy Whitcroft
1959171ae1a4SAndy Whitcroft			$type = 'E';
1960cf655043SAndy Whitcroft
1961c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1962cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1963cf655043SAndy Whitcroft
1964cf655043SAndy Whitcroft			$av_preprocessor = 1;
1965cf655043SAndy Whitcroft
1966cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1967cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1968cf655043SAndy Whitcroft			pop(@av_paren_type);
1969cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1970171ae1a4SAndy Whitcroft			$type = 'E';
19716c72ffaaSAndy Whitcroft
19726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1973c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
19746c72ffaaSAndy Whitcroft
1975171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1976171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1977171ae1a4SAndy Whitcroft			$av_pending = $type;
1978171ae1a4SAndy Whitcroft			$type = 'N';
1979171ae1a4SAndy Whitcroft
19806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1981c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
19826c72ffaaSAndy Whitcroft			if (defined $2) {
1983cf655043SAndy Whitcroft				$av_pending = 'V';
19846c72ffaaSAndy Whitcroft			}
19856c72ffaaSAndy Whitcroft			$type = 'N';
19866c72ffaaSAndy Whitcroft
198714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1988c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
198914b111c1SAndy Whitcroft			$av_pending = 'E';
19906c72ffaaSAndy Whitcroft			$type = 'N';
19916c72ffaaSAndy Whitcroft
19921f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
19931f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
19941f65f947SAndy Whitcroft			$av_pend_colon = 'C';
19951f65f947SAndy Whitcroft			$type = 'N';
19961f65f947SAndy Whitcroft
199714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1998c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
19996c72ffaaSAndy Whitcroft			$type = 'N';
20006c72ffaaSAndy Whitcroft
20016c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
2002c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
2003cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
2004cf655043SAndy Whitcroft			$av_pending = '_';
20056c72ffaaSAndy Whitcroft			$type = 'N';
20066c72ffaaSAndy Whitcroft
20076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
2008cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
2009cf655043SAndy Whitcroft			if ($new_type ne '_') {
2010cf655043SAndy Whitcroft				$type = $new_type;
2011c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
2012c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
20136c72ffaaSAndy Whitcroft			} else {
2014c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
20156c72ffaaSAndy Whitcroft			}
20166c72ffaaSAndy Whitcroft
2017c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
2018c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
2019c8cb2ca3SAndy Whitcroft			$type = 'V';
2020cf655043SAndy Whitcroft			$av_pending = 'V';
20216c72ffaaSAndy Whitcroft
20228e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
20238e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
20241f65f947SAndy Whitcroft				$av_pend_colon = 'B';
20258e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
20268e761b04SAndy Whitcroft				$av_pend_colon = 'L';
20271f65f947SAndy Whitcroft			}
20281f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
20291f65f947SAndy Whitcroft			$type = 'V';
20301f65f947SAndy Whitcroft
20316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
2032c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
20336c72ffaaSAndy Whitcroft			$type = 'V';
20346c72ffaaSAndy Whitcroft
20356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
2036c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
20376c72ffaaSAndy Whitcroft			$type = 'N';
20386c72ffaaSAndy Whitcroft
2039cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
2040c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
204113214adfSAndy Whitcroft			$type = 'E';
20421f65f947SAndy Whitcroft			$av_pend_colon = 'O';
204313214adfSAndy Whitcroft
20448e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
20458e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
20468e761b04SAndy Whitcroft			$type = 'C';
20478e761b04SAndy Whitcroft
20481f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
20491f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
20501f65f947SAndy Whitcroft			$type = 'N';
20511f65f947SAndy Whitcroft
20521f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
20531f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
20541f65f947SAndy Whitcroft
20551f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
20561f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
20571f65f947SAndy Whitcroft				$type = 'E';
20581f65f947SAndy Whitcroft			} else {
20591f65f947SAndy Whitcroft				$type = 'N';
20601f65f947SAndy Whitcroft			}
20611f65f947SAndy Whitcroft			$av_pend_colon = 'O';
20621f65f947SAndy Whitcroft
20638e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
206413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
20656c72ffaaSAndy Whitcroft			$type = 'N';
20666c72ffaaSAndy Whitcroft
20670d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
206874048ed8SAndy Whitcroft			my $variant;
206974048ed8SAndy Whitcroft
207074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
207174048ed8SAndy Whitcroft			if ($type eq 'V') {
207274048ed8SAndy Whitcroft				$variant = 'B';
207374048ed8SAndy Whitcroft			} else {
207474048ed8SAndy Whitcroft				$variant = 'U';
207574048ed8SAndy Whitcroft			}
207674048ed8SAndy Whitcroft
207774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
207874048ed8SAndy Whitcroft			$type = 'N';
207974048ed8SAndy Whitcroft
20806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2081c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
20826c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
20836c72ffaaSAndy Whitcroft				$type = 'N';
20846c72ffaaSAndy Whitcroft			}
20856c72ffaaSAndy Whitcroft
20866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2087c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
20886c72ffaaSAndy Whitcroft		}
20896c72ffaaSAndy Whitcroft		if (defined $1) {
20906c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
20916c72ffaaSAndy Whitcroft			$res .= $type x length($1);
20926c72ffaaSAndy Whitcroft		}
20936c72ffaaSAndy Whitcroft	}
20946c72ffaaSAndy Whitcroft
20951f65f947SAndy Whitcroft	return ($res, $var);
20966c72ffaaSAndy Whitcroft}
20976c72ffaaSAndy Whitcroft
20988905a67cSAndy Whitcroftsub possible {
209913214adfSAndy Whitcroft	my ($possible, $line) = @_;
21009a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
21010776e594SAndy Whitcroft		^(?:
21020776e594SAndy Whitcroft			$Modifier|
21030776e594SAndy Whitcroft			$Storage|
21040776e594SAndy Whitcroft			$Type|
21059a974fdbSAndy Whitcroft			DEFINE_\S+
21069a974fdbSAndy Whitcroft		)$|
21079a974fdbSAndy Whitcroft		^(?:
21080776e594SAndy Whitcroft			goto|
21090776e594SAndy Whitcroft			return|
21100776e594SAndy Whitcroft			case|
21110776e594SAndy Whitcroft			else|
21120776e594SAndy Whitcroft			asm|__asm__|
211389a88353SAndy Whitcroft			do|
211489a88353SAndy Whitcroft			\#|
211589a88353SAndy Whitcroft			\#\#|
21169a974fdbSAndy Whitcroft		)(?:\s|$)|
21170776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
21189a974fdbSAndy Whitcroft	    )}x;
21199a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
21209a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2121c45dcabdSAndy Whitcroft		# Check for modifiers.
2122c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2123c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2124c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2125c45dcabdSAndy Whitcroft
2126c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2127c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2128d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
21299a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2130d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2131485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2132d2506586SAndy Whitcroft				}
21339a974fdbSAndy Whitcroft			}
2134c45dcabdSAndy Whitcroft
2135c45dcabdSAndy Whitcroft		} else {
213613214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2137485ff23eSAlex Dowad			push(@typeListFile, $possible);
2138c45dcabdSAndy Whitcroft		}
21398905a67cSAndy Whitcroft		build_types();
21400776e594SAndy Whitcroft	} else {
21410776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
21428905a67cSAndy Whitcroft	}
21438905a67cSAndy Whitcroft}
21448905a67cSAndy Whitcroft
21456c72ffaaSAndy Whitcroftmy $prefix = '';
21466c72ffaaSAndy Whitcroft
2147000d1cc1SJoe Perchessub show_type {
2148cbec18afSJoe Perches	my ($type) = @_;
214991bfe484SJoe Perches
2150522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2151522b837cSAlexey Dobriyan
2152cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2153cbec18afSJoe Perches
2154cbec18afSJoe Perches	return !defined $ignore_type{$type};
2155000d1cc1SJoe Perches}
2156000d1cc1SJoe Perches
2157f0a594c1SAndy Whitcroftsub report {
2158cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2159cbec18afSJoe Perches
2160cbec18afSJoe Perches	if (!show_type($type) ||
2161cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2162773647a0SAndy Whitcroft		return 0;
2163773647a0SAndy Whitcroft	}
216457230297SJoe Perches	my $output = '';
2165737c0767SJohn Brooks	if ($color) {
216657230297SJoe Perches		if ($level eq 'ERROR') {
216757230297SJoe Perches			$output .= RED;
216857230297SJoe Perches		} elsif ($level eq 'WARNING') {
216957230297SJoe Perches			$output .= YELLOW;
2170000d1cc1SJoe Perches		} else {
217157230297SJoe Perches			$output .= GREEN;
2172000d1cc1SJoe Perches		}
217357230297SJoe Perches	}
217457230297SJoe Perches	$output .= $prefix . $level . ':';
217557230297SJoe Perches	if ($show_types) {
2176737c0767SJohn Brooks		$output .= BLUE if ($color);
217757230297SJoe Perches		$output .= "$type:";
217857230297SJoe Perches	}
2179737c0767SJohn Brooks	$output .= RESET if ($color);
218057230297SJoe Perches	$output .= ' ' . $msg . "\n";
218134d8815fSJoe Perches
218234d8815fSJoe Perches	if ($showfile) {
218334d8815fSJoe Perches		my @lines = split("\n", $output, -1);
218434d8815fSJoe Perches		splice(@lines, 1, 1);
218534d8815fSJoe Perches		$output = join("\n", @lines);
218634d8815fSJoe Perches	}
218757230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
21888905a67cSAndy Whitcroft
218957230297SJoe Perches	push(our @report, $output);
2190773647a0SAndy Whitcroft
2191773647a0SAndy Whitcroft	return 1;
2192f0a594c1SAndy Whitcroft}
2193cbec18afSJoe Perches
2194f0a594c1SAndy Whitcroftsub report_dump {
219513214adfSAndy Whitcroft	our @report;
2196f0a594c1SAndy Whitcroft}
2197000d1cc1SJoe Perches
2198d752fcc8SJoe Perchessub fixup_current_range {
2199d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2200d752fcc8SJoe Perches
2201d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2202d752fcc8SJoe Perches		my $o = $1;
2203d752fcc8SJoe Perches		my $l = $2;
2204d752fcc8SJoe Perches		my $no = $o + $offset;
2205d752fcc8SJoe Perches		my $nl = $l + $length;
2206d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2207d752fcc8SJoe Perches	}
2208d752fcc8SJoe Perches}
2209d752fcc8SJoe Perches
2210d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2211d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2212d752fcc8SJoe Perches
2213d752fcc8SJoe Perches	my $range_last_linenr = 0;
2214d752fcc8SJoe Perches	my $delta_offset = 0;
2215d752fcc8SJoe Perches
2216d752fcc8SJoe Perches	my $old_linenr = 0;
2217d752fcc8SJoe Perches	my $new_linenr = 0;
2218d752fcc8SJoe Perches
2219d752fcc8SJoe Perches	my $next_insert = 0;
2220d752fcc8SJoe Perches	my $next_delete = 0;
2221d752fcc8SJoe Perches
2222d752fcc8SJoe Perches	my @lines = ();
2223d752fcc8SJoe Perches
2224d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2225d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2226d752fcc8SJoe Perches
2227d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2228d752fcc8SJoe Perches		my $save_line = 1;
2229d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2230323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2231d752fcc8SJoe Perches			$delta_offset = 0;
2232d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2233d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2234d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2235d752fcc8SJoe Perches		}
2236d752fcc8SJoe Perches
2237d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2238d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2239d752fcc8SJoe Perches			$save_line = 0;
2240d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2241d752fcc8SJoe Perches		}
2242d752fcc8SJoe Perches
2243d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2244d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2245d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2246d752fcc8SJoe Perches			$new_linenr++;
2247d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2248d752fcc8SJoe Perches		}
2249d752fcc8SJoe Perches
2250d752fcc8SJoe Perches		if ($save_line) {
2251d752fcc8SJoe Perches			push(@lines, $line);
2252d752fcc8SJoe Perches			$new_linenr++;
2253d752fcc8SJoe Perches		}
2254d752fcc8SJoe Perches
2255d752fcc8SJoe Perches		$old_linenr++;
2256d752fcc8SJoe Perches	}
2257d752fcc8SJoe Perches
2258d752fcc8SJoe Perches	return @lines;
2259d752fcc8SJoe Perches}
2260d752fcc8SJoe Perches
2261f2d7e4d4SJoe Perchessub fix_insert_line {
2262f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2263f2d7e4d4SJoe Perches
2264f2d7e4d4SJoe Perches	my $inserted = {
2265f2d7e4d4SJoe Perches		LINENR => $linenr,
2266f2d7e4d4SJoe Perches		LINE => $line,
2267f2d7e4d4SJoe Perches	};
2268f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2269f2d7e4d4SJoe Perches}
2270f2d7e4d4SJoe Perches
2271f2d7e4d4SJoe Perchessub fix_delete_line {
2272f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2273f2d7e4d4SJoe Perches
2274f2d7e4d4SJoe Perches	my $deleted = {
2275f2d7e4d4SJoe Perches		LINENR => $linenr,
2276f2d7e4d4SJoe Perches		LINE => $line,
2277f2d7e4d4SJoe Perches	};
2278f2d7e4d4SJoe Perches
2279f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2280f2d7e4d4SJoe Perches}
2281f2d7e4d4SJoe Perches
2282de7d4f0eSAndy Whitcroftsub ERROR {
2283cbec18afSJoe Perches	my ($type, $msg) = @_;
2284cbec18afSJoe Perches
2285cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2286de7d4f0eSAndy Whitcroft		our $clean = 0;
22876c72ffaaSAndy Whitcroft		our $cnt_error++;
22883705ce5bSJoe Perches		return 1;
2289de7d4f0eSAndy Whitcroft	}
22903705ce5bSJoe Perches	return 0;
2291773647a0SAndy Whitcroft}
2292de7d4f0eSAndy Whitcroftsub WARN {
2293cbec18afSJoe Perches	my ($type, $msg) = @_;
2294cbec18afSJoe Perches
2295cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2296de7d4f0eSAndy Whitcroft		our $clean = 0;
22976c72ffaaSAndy Whitcroft		our $cnt_warn++;
22983705ce5bSJoe Perches		return 1;
2299de7d4f0eSAndy Whitcroft	}
23003705ce5bSJoe Perches	return 0;
2301773647a0SAndy Whitcroft}
2302de7d4f0eSAndy Whitcroftsub CHK {
2303cbec18afSJoe Perches	my ($type, $msg) = @_;
2304cbec18afSJoe Perches
2305cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2306de7d4f0eSAndy Whitcroft		our $clean = 0;
23076c72ffaaSAndy Whitcroft		our $cnt_chk++;
23083705ce5bSJoe Perches		return 1;
23096c72ffaaSAndy Whitcroft	}
23103705ce5bSJoe Perches	return 0;
2311de7d4f0eSAndy Whitcroft}
2312de7d4f0eSAndy Whitcroft
23136ecd9674SAndy Whitcroftsub check_absolute_file {
23146ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
23156ecd9674SAndy Whitcroft	my $file = $absolute;
23166ecd9674SAndy Whitcroft
23176ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
23186ecd9674SAndy Whitcroft
23196ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
23206ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
23216ecd9674SAndy Whitcroft		if (-f "$root/$file") {
23226ecd9674SAndy Whitcroft			##print "file<$file>\n";
23236ecd9674SAndy Whitcroft			last;
23246ecd9674SAndy Whitcroft		}
23256ecd9674SAndy Whitcroft	}
23266ecd9674SAndy Whitcroft	if (! -f _)  {
23276ecd9674SAndy Whitcroft		return 0;
23286ecd9674SAndy Whitcroft	}
23296ecd9674SAndy Whitcroft
23306ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
23316ecd9674SAndy Whitcroft	my $prefix = $absolute;
23326ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
23336ecd9674SAndy Whitcroft
23346ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
23356ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2336000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2337000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
23386ecd9674SAndy Whitcroft	}
23396ecd9674SAndy Whitcroft}
23406ecd9674SAndy Whitcroft
23413705ce5bSJoe Perchessub trim {
23423705ce5bSJoe Perches	my ($string) = @_;
23433705ce5bSJoe Perches
2344b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2345b34c648bSJoe Perches
2346b34c648bSJoe Perches	return $string;
2347b34c648bSJoe Perches}
2348b34c648bSJoe Perches
2349b34c648bSJoe Perchessub ltrim {
2350b34c648bSJoe Perches	my ($string) = @_;
2351b34c648bSJoe Perches
2352b34c648bSJoe Perches	$string =~ s/^\s+//;
2353b34c648bSJoe Perches
2354b34c648bSJoe Perches	return $string;
2355b34c648bSJoe Perches}
2356b34c648bSJoe Perches
2357b34c648bSJoe Perchessub rtrim {
2358b34c648bSJoe Perches	my ($string) = @_;
2359b34c648bSJoe Perches
2360b34c648bSJoe Perches	$string =~ s/\s+$//;
23613705ce5bSJoe Perches
23623705ce5bSJoe Perches	return $string;
23633705ce5bSJoe Perches}
23643705ce5bSJoe Perches
236552ea8506SJoe Perchessub string_find_replace {
236652ea8506SJoe Perches	my ($string, $find, $replace) = @_;
236752ea8506SJoe Perches
236852ea8506SJoe Perches	$string =~ s/$find/$replace/g;
236952ea8506SJoe Perches
237052ea8506SJoe Perches	return $string;
237152ea8506SJoe Perches}
237252ea8506SJoe Perches
23733705ce5bSJoe Perchessub tabify {
23743705ce5bSJoe Perches	my ($leading) = @_;
23753705ce5bSJoe Perches
2376713a09deSAntonio Borneo	my $source_indent = $tabsize;
23773705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
23783705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
23793705ce5bSJoe Perches
23803705ce5bSJoe Perches	#convert leading spaces to tabs
23813705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
23823705ce5bSJoe Perches	#Remove spaces before a tab
23833705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
23843705ce5bSJoe Perches
23853705ce5bSJoe Perches	return "$leading";
23863705ce5bSJoe Perches}
23873705ce5bSJoe Perches
2388d1fe9c09SJoe Perchessub pos_last_openparen {
2389d1fe9c09SJoe Perches	my ($line) = @_;
2390d1fe9c09SJoe Perches
2391d1fe9c09SJoe Perches	my $pos = 0;
2392d1fe9c09SJoe Perches
2393d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2394d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2395d1fe9c09SJoe Perches
2396d1fe9c09SJoe Perches	my $last_openparen = 0;
2397d1fe9c09SJoe Perches
2398d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2399d1fe9c09SJoe Perches		return -1;
2400d1fe9c09SJoe Perches	}
2401d1fe9c09SJoe Perches
2402d1fe9c09SJoe Perches	my $len = length($line);
2403d1fe9c09SJoe Perches
2404d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2405d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2406d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2407d1fe9c09SJoe Perches			$pos += length($1) - 1;
2408d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2409d1fe9c09SJoe Perches			$last_openparen = $pos;
2410d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2411d1fe9c09SJoe Perches			last;
2412d1fe9c09SJoe Perches		}
2413d1fe9c09SJoe Perches	}
2414d1fe9c09SJoe Perches
241591cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2416d1fe9c09SJoe Perches}
2417d1fe9c09SJoe Perches
2418f36d3eb8SJoe Perchessub get_raw_comment {
2419f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2420f36d3eb8SJoe Perches	my $comment = '';
2421f36d3eb8SJoe Perches
2422f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2423f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2424f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2425f36d3eb8SJoe Perches		}
2426f36d3eb8SJoe Perches	}
2427f36d3eb8SJoe Perches
2428f36d3eb8SJoe Perches	return $comment;
2429f36d3eb8SJoe Perches}
2430f36d3eb8SJoe Perches
24310a920b5bSAndy Whitcroftsub process {
24320a920b5bSAndy Whitcroft	my $filename = shift;
24330a920b5bSAndy Whitcroft
24340a920b5bSAndy Whitcroft	my $linenr=0;
24350a920b5bSAndy Whitcroft	my $prevline="";
2436c2fdda0dSAndy Whitcroft	my $prevrawline="";
24370a920b5bSAndy Whitcroft	my $stashline="";
2438c2fdda0dSAndy Whitcroft	my $stashrawline="";
24390a920b5bSAndy Whitcroft
24404a0df2efSAndy Whitcroft	my $length;
24410a920b5bSAndy Whitcroft	my $indent;
24420a920b5bSAndy Whitcroft	my $previndent=0;
24430a920b5bSAndy Whitcroft	my $stashindent=0;
24440a920b5bSAndy Whitcroft
2445de7d4f0eSAndy Whitcroft	our $clean = 1;
24460a920b5bSAndy Whitcroft	my $signoff = 0;
2447cd261496SGeert Uytterhoeven	my $author = '';
2448cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
244948ca2d8aSDwaipayan Ray	my $author_sob = '';
24500a920b5bSAndy Whitcroft	my $is_patch = 0;
2451133712a2SRob Herring	my $is_binding_patch = -1;
245229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
245315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
245444d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2455ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2456490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2457bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
24582a076f40SJoe Perches	my $commit_log_long_line = 0;
2459e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
246013f1937eSJoe Perches	my $reported_maintainer_file = 0;
2461fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2462fa64205dSPasi Savanainen
2463365dd4eaSJoe Perches	my $last_blank_line = 0;
24645e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2465365dd4eaSJoe Perches
246613214adfSAndy Whitcroft	our @report = ();
24676c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
24686c72ffaaSAndy Whitcroft	our $cnt_error = 0;
24696c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
24706c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
24716c72ffaaSAndy Whitcroft
24720a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
24730a920b5bSAndy Whitcroft	my $realfile = '';
24740a920b5bSAndy Whitcroft	my $realline = 0;
24750a920b5bSAndy Whitcroft	my $realcnt = 0;
24760a920b5bSAndy Whitcroft	my $here = '';
247777cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
24780a920b5bSAndy Whitcroft	my $in_comment = 0;
2479c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
24800a920b5bSAndy Whitcroft	my $first_line = 0;
24811e855726SWolfram Sang	my $p1_prefix = '';
24820a920b5bSAndy Whitcroft
248313214adfSAndy Whitcroft	my $prev_values = 'E';
248413214adfSAndy Whitcroft
248513214adfSAndy Whitcroft	# suppression flags
2486773647a0SAndy Whitcroft	my %suppress_ifbraces;
2487170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
24882b474a1aSAndy Whitcroft	my %suppress_export;
24893e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2490653d4876SAndy Whitcroft
24917e51f197SJoe Perches	my %signatures = ();
2492323c1260SJoe Perches
2493c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2494de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2495c2fdda0dSAndy Whitcroft	#
2496de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2497de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2498773647a0SAndy Whitcroft
2499d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2500d8b07710SJoe Perches
25019f3a8992SRob Herring	my $checklicenseline = 1;
25029f3a8992SRob Herring
2503773647a0SAndy Whitcroft	sanitise_line_reset();
2504c2fdda0dSAndy Whitcroft	my $line;
2505c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2506773647a0SAndy Whitcroft		$linenr++;
2507773647a0SAndy Whitcroft		$line = $rawline;
2508c2fdda0dSAndy Whitcroft
25093705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
25103705ce5bSJoe Perches
2511773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2512de7d4f0eSAndy Whitcroft			$setup_docs = 0;
25132581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2514de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2515de7d4f0eSAndy Whitcroft			}
2516773647a0SAndy Whitcroft			#next;
2517de7d4f0eSAndy Whitcroft		}
251874fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2519773647a0SAndy Whitcroft			$realline=$1-1;
2520773647a0SAndy Whitcroft			if (defined $2) {
2521773647a0SAndy Whitcroft				$realcnt=$3+1;
2522773647a0SAndy Whitcroft			} else {
2523773647a0SAndy Whitcroft				$realcnt=1+1;
2524773647a0SAndy Whitcroft			}
2525c45dcabdSAndy Whitcroft			$in_comment = 0;
2526773647a0SAndy Whitcroft
2527773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2528773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2529773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2530773647a0SAndy Whitcroft			# at context start.
2531773647a0SAndy Whitcroft			my $edge;
253201fa9147SAndy Whitcroft			my $cnt = $realcnt;
253301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
253401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
253501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
253601fa9147SAndy Whitcroft				$cnt--;
253701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2538721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2539fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2540fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2541fae17daeSAndy Whitcroft					($edge) = $1;
2542fae17daeSAndy Whitcroft					last;
2543fae17daeSAndy Whitcroft				}
2544773647a0SAndy Whitcroft			}
2545773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2546773647a0SAndy Whitcroft				$in_comment = 1;
2547773647a0SAndy Whitcroft			}
2548773647a0SAndy Whitcroft
2549773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2550773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2551773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2552773647a0SAndy Whitcroft			if (!defined $edge &&
255383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2554773647a0SAndy Whitcroft			{
2555773647a0SAndy Whitcroft				$in_comment = 1;
2556773647a0SAndy Whitcroft			}
2557773647a0SAndy Whitcroft
2558773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2559773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2560773647a0SAndy Whitcroft
2561171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2562773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2563171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2564773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2565773647a0SAndy Whitcroft		}
2566773647a0SAndy Whitcroft		push(@lines, $line);
2567773647a0SAndy Whitcroft
2568773647a0SAndy Whitcroft		if ($realcnt > 1) {
2569773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2570773647a0SAndy Whitcroft		} else {
2571773647a0SAndy Whitcroft			$realcnt = 0;
2572773647a0SAndy Whitcroft		}
2573773647a0SAndy Whitcroft
2574773647a0SAndy Whitcroft		#print "==>$rawline\n";
2575773647a0SAndy Whitcroft		#print "-->$line\n";
2576de7d4f0eSAndy Whitcroft
2577de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2578de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2579de7d4f0eSAndy Whitcroft		}
2580de7d4f0eSAndy Whitcroft	}
2581de7d4f0eSAndy Whitcroft
25826c72ffaaSAndy Whitcroft	$prefix = '';
25836c72ffaaSAndy Whitcroft
2584773647a0SAndy Whitcroft	$realcnt = 0;
2585773647a0SAndy Whitcroft	$linenr = 0;
2586194f66fcSJoe Perches	$fixlinenr = -1;
25870a920b5bSAndy Whitcroft	foreach my $line (@lines) {
25880a920b5bSAndy Whitcroft		$linenr++;
2589194f66fcSJoe Perches		$fixlinenr++;
25901b5539b1SJoe Perches		my $sline = $line;	#copy of $line
25911b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
25920a920b5bSAndy Whitcroft
2593c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2594f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
25956c72ffaaSAndy Whitcroft
259612c253abSJoe Perches# check if it's a mode change, rename or start of a patch
259712c253abSJoe Perches		if (!$in_commit_log &&
259812c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
259912c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
260012c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
260112c253abSJoe Perches			$is_patch = 1;
260212c253abSJoe Perches		}
260312c253abSJoe Perches
26040a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2605e518e9a5SJoe Perches		if (!$in_commit_log &&
260674fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
260774fd4f34SJoe Perches			my $context = $4;
26080a920b5bSAndy Whitcroft			$is_patch = 1;
26094a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
26100a920b5bSAndy Whitcroft			$realline=$1-1;
26110a920b5bSAndy Whitcroft			if (defined $2) {
26120a920b5bSAndy Whitcroft				$realcnt=$3+1;
26130a920b5bSAndy Whitcroft			} else {
26140a920b5bSAndy Whitcroft				$realcnt=1+1;
26150a920b5bSAndy Whitcroft			}
2616c2fdda0dSAndy Whitcroft			annotate_reset();
261713214adfSAndy Whitcroft			$prev_values = 'E';
261813214adfSAndy Whitcroft
2619773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2620170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
26212b474a1aSAndy Whitcroft			%suppress_export = ();
26223e469cdcSAndy Whitcroft			$suppress_statement = 0;
262374fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
262474fd4f34SJoe Perches				$context_function = $1;
262574fd4f34SJoe Perches			} else {
262674fd4f34SJoe Perches				undef $context_function;
262774fd4f34SJoe Perches			}
26280a920b5bSAndy Whitcroft			next;
26290a920b5bSAndy Whitcroft
26304a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
26314a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
26324a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2633773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
26340a920b5bSAndy Whitcroft			$realline++;
2635d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
26360a920b5bSAndy Whitcroft
26374a0df2efSAndy Whitcroft			# Measure the line length and indent.
2638c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
26390a920b5bSAndy Whitcroft
26400a920b5bSAndy Whitcroft			# Track the previous line.
26410a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
26420a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2643c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2644c2fdda0dSAndy Whitcroft
2645773647a0SAndy Whitcroft			#warn "line<$line>\n";
26466c72ffaaSAndy Whitcroft
2647d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2648d8aaf121SAndy Whitcroft			$realcnt--;
26490a920b5bSAndy Whitcroft		}
26500a920b5bSAndy Whitcroft
2651cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2652cc77cdcaSAndy Whitcroft
26536c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
26546c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2655773647a0SAndy Whitcroft
26562ac73b4fSJoe Perches		my $found_file = 0;
2657773647a0SAndy Whitcroft		# extract the filename as it passes
26583bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
26593bf9a009SRabin Vincent			$realfile = $1;
26602b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2661270c49a0SJoe Perches			$in_commit_log = 0;
26622ac73b4fSJoe Perches			$found_file = 1;
26633bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2664773647a0SAndy Whitcroft			$realfile = $1;
26652b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2666270c49a0SJoe Perches			$in_commit_log = 0;
26671e855726SWolfram Sang
26681e855726SWolfram Sang			$p1_prefix = $1;
2669e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2670e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2671000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2672000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
26731e855726SWolfram Sang			}
2674773647a0SAndy Whitcroft
2675c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2676000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2677000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2678773647a0SAndy Whitcroft			}
26792ac73b4fSJoe Perches			$found_file = 1;
26802ac73b4fSJoe Perches		}
26812ac73b4fSJoe Perches
268234d8815fSJoe Perches#make up the handle for any error we report on this line
268334d8815fSJoe Perches		if ($showfile) {
268434d8815fSJoe Perches			$prefix = "$realfile:$realline: "
268534d8815fSJoe Perches		} elsif ($emacs) {
26867d3a9f67SJoe Perches			if ($file) {
26877d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
26887d3a9f67SJoe Perches			} else {
268934d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
269034d8815fSJoe Perches			}
26917d3a9f67SJoe Perches		}
269234d8815fSJoe Perches
26932ac73b4fSJoe Perches		if ($found_file) {
269485b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
269585b0ee18SJoe Perches				WARN("OBSOLETE",
269685b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
269785b0ee18SJoe Perches			}
26987bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
26992ac73b4fSJoe Perches				$check = 1;
27002ac73b4fSJoe Perches			} else {
27012ac73b4fSJoe Perches				$check = $check_orig;
27022ac73b4fSJoe Perches			}
27039f3a8992SRob Herring			$checklicenseline = 1;
2704133712a2SRob Herring
2705133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2706133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2707133712a2SRob Herring
2708133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2709133712a2SRob Herring
2710133712a2SRob Herring				if (($last_binding_patch != -1) &&
2711133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2712133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2713858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2714133712a2SRob Herring				}
2715133712a2SRob Herring			}
2716133712a2SRob Herring
2717773647a0SAndy Whitcroft			next;
2718773647a0SAndy Whitcroft		}
2719773647a0SAndy Whitcroft
2720389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
27210a920b5bSAndy Whitcroft
2722c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2723c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2724c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
27250a920b5bSAndy Whitcroft
27266c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
27276c72ffaaSAndy Whitcroft
2728490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2729490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2730490b292cSJoe Perches		if ($in_commit_log) {
2731490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2732490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2733490b292cSJoe Perches			}
2734490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2735490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2736490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2737490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2738490b292cSJoe Perches		}
2739490b292cSJoe Perches
2740e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2741e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
274213e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
274313e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2744e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2745e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2746e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2747e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2748e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2749e518e9a5SJoe Perches		}
2750e518e9a5SJoe Perches
27513bf9a009SRabin Vincent# Check for incorrect file permissions
27523bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
27533bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
275404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
275504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2756000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2757000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
27583bf9a009SRabin Vincent			}
27593bf9a009SRabin Vincent		}
27603bf9a009SRabin Vincent
2761cd261496SGeert Uytterhoeven# Check the patch for a From:
2762cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2763cd261496SGeert Uytterhoeven			$author = $1;
2764e7f929f3SDwaipayan Ray			my $curline = $linenr;
2765e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2766e7f929f3SDwaipayan Ray				$author .= $1;
2767e7f929f3SDwaipayan Ray			}
2768cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2769cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2770dfa05c28SJoe Perches			$author = reformat_email($author);
2771cd261496SGeert Uytterhoeven		}
2772cd261496SGeert Uytterhoeven
277320112475SJoe Perches# Check the patch for a signoff:
2774dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
27754a0df2efSAndy Whitcroft			$signoff++;
277615662b3eSJoe Perches			$in_commit_log = 0;
277748ca2d8aSDwaipayan Ray			if ($author ne ''  && $authorsignoff != 1) {
2778fccaebf0SDwaipayan Ray				if (same_email_addresses($1, $author)) {
2779cd261496SGeert Uytterhoeven					$authorsignoff = 1;
278048ca2d8aSDwaipayan Ray				} else {
278148ca2d8aSDwaipayan Ray					my $ctx = $1;
278248ca2d8aSDwaipayan Ray					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
278348ca2d8aSDwaipayan Ray					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
278448ca2d8aSDwaipayan Ray
278548ca2d8aSDwaipayan Ray					if ($email_address eq $author_address && $email_name eq $author_name) {
278648ca2d8aSDwaipayan Ray						$author_sob = $ctx;
278748ca2d8aSDwaipayan Ray						$authorsignoff = 2;
278848ca2d8aSDwaipayan Ray					} elsif ($email_address eq $author_address) {
278948ca2d8aSDwaipayan Ray						$author_sob = $ctx;
279048ca2d8aSDwaipayan Ray						$authorsignoff = 3;
279148ca2d8aSDwaipayan Ray					} elsif ($email_name eq $author_name) {
279248ca2d8aSDwaipayan Ray						$author_sob = $ctx;
279348ca2d8aSDwaipayan Ray						$authorsignoff = 4;
279448ca2d8aSDwaipayan Ray
279548ca2d8aSDwaipayan Ray						my $address1 = $email_address;
279648ca2d8aSDwaipayan Ray						my $address2 = $author_address;
279748ca2d8aSDwaipayan Ray
279848ca2d8aSDwaipayan Ray						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
279948ca2d8aSDwaipayan Ray							$address1 = "$1$2";
280048ca2d8aSDwaipayan Ray						}
280148ca2d8aSDwaipayan Ray						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
280248ca2d8aSDwaipayan Ray							$address2 = "$1$2";
280348ca2d8aSDwaipayan Ray						}
280448ca2d8aSDwaipayan Ray						if ($address1 eq $address2) {
280548ca2d8aSDwaipayan Ray							$authorsignoff = 5;
280648ca2d8aSDwaipayan Ray						}
280748ca2d8aSDwaipayan Ray					}
2808cd261496SGeert Uytterhoeven				}
2809cd261496SGeert Uytterhoeven			}
28100a920b5bSAndy Whitcroft		}
281120112475SJoe Perches
281244d303ebSJoe Perches# Check for patch separator
281344d303ebSJoe Perches		if ($line =~ /^---$/) {
281444d303ebSJoe Perches			$has_patch_separator = 1;
281544d303ebSJoe Perches			$in_commit_log = 0;
281644d303ebSJoe Perches		}
281744d303ebSJoe Perches
2818e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2819e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2820e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2821e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2822e0d975b1SJoe Perches		}
2823e0d975b1SJoe Perches
282420112475SJoe Perches# Check signature styles
2825270c49a0SJoe Perches		if (!$in_header_lines &&
2826ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
282720112475SJoe Perches			my $space_before = $1;
282820112475SJoe Perches			my $sign_off = $2;
282920112475SJoe Perches			my $space_after = $3;
283020112475SJoe Perches			my $email = $4;
283120112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
283220112475SJoe Perches
2833ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2834831242abSAditya Srivastava				my $suggested_signature = find_standard_signature($sign_off);
2835831242abSAditya Srivastava				if ($suggested_signature eq "") {
2836ce0338dfSJoe Perches					WARN("BAD_SIGN_OFF",
2837ce0338dfSJoe Perches					     "Non-standard signature: $sign_off\n" . $herecurr);
2838831242abSAditya Srivastava				} else {
2839831242abSAditya Srivastava					if (WARN("BAD_SIGN_OFF",
2840831242abSAditya Srivastava						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2841831242abSAditya Srivastava					    $fix) {
2842831242abSAditya Srivastava						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2843831242abSAditya Srivastava					}
2844831242abSAditya Srivastava				}
2845ce0338dfSJoe Perches			}
284620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
28473705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28483705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
28493705ce5bSJoe Perches				    $fix) {
2850194f66fcSJoe Perches					$fixed[$fixlinenr] =
28513705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28523705ce5bSJoe Perches				}
285320112475SJoe Perches			}
285420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
28553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28563705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
28573705ce5bSJoe Perches				    $fix) {
2858194f66fcSJoe Perches					$fixed[$fixlinenr] =
28593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28603705ce5bSJoe Perches				}
28613705ce5bSJoe Perches
286220112475SJoe Perches			}
286320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
28643705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28653705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
28663705ce5bSJoe Perches				    $fix) {
2867194f66fcSJoe Perches					$fixed[$fixlinenr] =
28683705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28693705ce5bSJoe Perches				}
287020112475SJoe Perches			}
287120112475SJoe Perches
2872dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
287348ca2d8aSDwaipayan Ray			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
287420112475SJoe Perches			if ($suggested_email eq "") {
2875000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2876000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
287720112475SJoe Perches			} else {
287820112475SJoe Perches				my $dequoted = $suggested_email;
287920112475SJoe Perches				$dequoted =~ s/^"//;
288020112475SJoe Perches				$dequoted =~ s/" </ </;
288120112475SJoe Perches				# Don't force email to have quotes
288220112475SJoe Perches				# Allow just an angle bracketed address
2883fccaebf0SDwaipayan Ray				if (!same_email_addresses($email, $suggested_email)) {
2884fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2885fccaebf0SDwaipayan Ray						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2886fccaebf0SDwaipayan Ray					    $fix) {
2887fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2888fccaebf0SDwaipayan Ray					}
2889fccaebf0SDwaipayan Ray				}
2890fccaebf0SDwaipayan Ray
2891fccaebf0SDwaipayan Ray				# Address part shouldn't have comments
2892fccaebf0SDwaipayan Ray				my $stripped_address = $email_address;
2893fccaebf0SDwaipayan Ray				$stripped_address =~ s/\([^\(\)]*\)//g;
2894fccaebf0SDwaipayan Ray				if ($email_address ne $stripped_address) {
2895fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2896fccaebf0SDwaipayan Ray						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2897fccaebf0SDwaipayan Ray					    $fix) {
2898fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2899fccaebf0SDwaipayan Ray					}
2900fccaebf0SDwaipayan Ray				}
2901fccaebf0SDwaipayan Ray
2902fccaebf0SDwaipayan Ray				# Only one name comment should be allowed
2903fccaebf0SDwaipayan Ray				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2904fccaebf0SDwaipayan Ray				if ($comment_count > 1) {
2905000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2906fccaebf0SDwaipayan Ray					     "Use a single name comment in email: '$email'\n" . $herecurr);
2907fccaebf0SDwaipayan Ray				}
2908fccaebf0SDwaipayan Ray
2909fccaebf0SDwaipayan Ray
2910fccaebf0SDwaipayan Ray				# [email protected] or [email protected] shouldn't
2911e73d2715SDwaipayan Ray				# have an email name. In addition comments should strictly
2912fccaebf0SDwaipayan Ray				# begin with a #
2913fccaebf0SDwaipayan Ray				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2914fccaebf0SDwaipayan Ray					if (($comment ne "" && $comment !~ /^#.+/) ||
2915fccaebf0SDwaipayan Ray					    ($email_name ne "")) {
2916fccaebf0SDwaipayan Ray						my $cur_name = $email_name;
2917fccaebf0SDwaipayan Ray						my $new_comment = $comment;
2918fccaebf0SDwaipayan Ray						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2919fccaebf0SDwaipayan Ray
2920fccaebf0SDwaipayan Ray						# Remove brackets enclosing comment text
2921fccaebf0SDwaipayan Ray						# and # from start of comments to get comment text
2922fccaebf0SDwaipayan Ray						$new_comment =~ s/^\((.*)\)$/$1/;
2923fccaebf0SDwaipayan Ray						$new_comment =~ s/^\[(.*)\]$/$1/;
2924fccaebf0SDwaipayan Ray						$new_comment =~ s/^[\s\#]+|\s+$//g;
2925fccaebf0SDwaipayan Ray
2926fccaebf0SDwaipayan Ray						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2927fccaebf0SDwaipayan Ray						$new_comment = " # $new_comment" if ($new_comment ne "");
2928fccaebf0SDwaipayan Ray						my $new_email = "$email_address$new_comment";
2929fccaebf0SDwaipayan Ray
2930fccaebf0SDwaipayan Ray						if (WARN("BAD_STABLE_ADDRESS_STYLE",
2931fccaebf0SDwaipayan Ray							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2932fccaebf0SDwaipayan Ray						    $fix) {
2933fccaebf0SDwaipayan Ray							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2934fccaebf0SDwaipayan Ray						}
2935fccaebf0SDwaipayan Ray					}
2936fccaebf0SDwaipayan Ray				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2937fccaebf0SDwaipayan Ray					my $new_comment = $comment;
2938fccaebf0SDwaipayan Ray
2939fccaebf0SDwaipayan Ray					# Extract comment text from within brackets or
2940fccaebf0SDwaipayan Ray					# c89 style /*...*/ comments
2941fccaebf0SDwaipayan Ray					$new_comment =~ s/^\[(.*)\]$/$1/;
2942fccaebf0SDwaipayan Ray					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2943fccaebf0SDwaipayan Ray
2944fccaebf0SDwaipayan Ray					$new_comment = trim($new_comment);
2945fccaebf0SDwaipayan Ray					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2946fccaebf0SDwaipayan Ray					$new_comment = "($new_comment)" if ($new_comment ne "");
2947fccaebf0SDwaipayan Ray					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2948fccaebf0SDwaipayan Ray
2949fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2950fccaebf0SDwaipayan Ray						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2951fccaebf0SDwaipayan Ray					    $fix) {
2952fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2953fccaebf0SDwaipayan Ray					}
295420112475SJoe Perches				}
29550a920b5bSAndy Whitcroft			}
29567e51f197SJoe Perches
29577e51f197SJoe Perches# Check for duplicate signatures
29587e51f197SJoe Perches			my $sig_nospace = $line;
29597e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
29607e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
29617e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
29627e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
29637e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
29647e51f197SJoe Perches			} else {
29657e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
29667e51f197SJoe Perches			}
29676c5d24eeSSean Christopherson
29686c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
29696c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
29706c5d24eeSSean Christopherson				if ($email eq $author) {
29716c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29726c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
29736c5d24eeSSean Christopherson				}
29746c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
29756c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29766c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
29776c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
29786c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29796c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29806c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
29816c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29826c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29836c5d24eeSSean Christopherson				}
29846c5d24eeSSean Christopherson			}
29850a920b5bSAndy Whitcroft		}
29860a920b5bSAndy Whitcroft
2987a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2988a2fe16b9SJoe Perches		if ($in_header_lines &&
2989a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2990a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2991a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2992a2fe16b9SJoe Perches		}
2993a2fe16b9SJoe Perches
299444d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
299544d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
29967580c5b9SAditya Srivastava			if (ERROR("GERRIT_CHANGE_ID",
29977580c5b9SAditya Srivastava			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
29987580c5b9SAditya Srivastava			    $fix) {
29997580c5b9SAditya Srivastava                                fix_delete_line($fixlinenr, $rawline);
30007580c5b9SAditya Srivastava                        }
30017ebd05efSChristopher Covington		}
30027ebd05efSChristopher Covington
3003369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
3004369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3005369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3006369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3007369c8dd3SJoe Perches					# timestamp
3008634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3009634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3010634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3011634cffccSJoe Perches					# stack dump address styles
3012369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
3013369c8dd3SJoe Perches		}
3014369c8dd3SJoe Perches
30152a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
30162a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
3017bf4daf12SJoe Perches		    length($line) > 75 &&
3018bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3019bf4daf12SJoe Perches					# file delta changes
3020bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3021bf4daf12SJoe Perches					# filename then :
302227b379afSAditya Srivastava		      $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
302327b379afSAditya Srivastava					# A Fixes: or Link: line or signature tag line
3024bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
30252a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
30262a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
30272a076f40SJoe Perches			$commit_log_long_line = 1;
30282a076f40SJoe Perches		}
30292a076f40SJoe Perches
3030bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
3031bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
3032bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
3033bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
3034bf4daf12SJoe Perches		}
3035bf4daf12SJoe Perches
3036084a617aSDwaipayan Ray# Check for lines starting with a #
3037084a617aSDwaipayan Ray		if ($in_commit_log && $line =~ /^#/) {
3038084a617aSDwaipayan Ray			if (WARN("COMMIT_COMMENT_SYMBOL",
3039084a617aSDwaipayan Ray				 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3040084a617aSDwaipayan Ray			    $fix) {
3041084a617aSDwaipayan Ray				$fixed[$fixlinenr] =~ s/^/ /;
3042084a617aSDwaipayan Ray			}
3043084a617aSDwaipayan Ray		}
3044084a617aSDwaipayan Ray
30450d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
3046369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3047a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3048e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
3049fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3050aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3051369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3052bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3053fe043ea1SJoe Perches			my $init_char = "c";
3054fe043ea1SJoe Perches			my $orig_commit = "";
30550d7835fcSJoe Perches			my $short = 1;
30560d7835fcSJoe Perches			my $long = 0;
30570d7835fcSJoe Perches			my $case = 1;
30580d7835fcSJoe Perches			my $space = 1;
30590d7835fcSJoe Perches			my $hasdesc = 0;
306019c146a6SJoe Perches			my $hasparens = 0;
30610d7835fcSJoe Perches			my $id = '0123456789ab';
30620d7835fcSJoe Perches			my $orig_desc = "commit description";
30630d7835fcSJoe Perches			my $description = "";
30640d7835fcSJoe Perches
3065fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3066fe043ea1SJoe Perches				$init_char = $1;
3067fe043ea1SJoe Perches				$orig_commit = lc($2);
3068fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3069fe043ea1SJoe Perches				$orig_commit = lc($1);
3070fe043ea1SJoe Perches			}
3071fe043ea1SJoe Perches
30720d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
30730d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
30740d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
30750d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
30760d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
30770d7835fcSJoe Perches				$orig_desc = $1;
307819c146a6SJoe Perches				$hasparens = 1;
30790d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
30800d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
30810d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
30820d7835fcSJoe Perches				$orig_desc = $1;
308319c146a6SJoe Perches				$hasparens = 1;
3084b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3085b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
3086b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3087b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3088b671fde0SJoe Perches				$orig_desc = $1;
3089b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3090b671fde0SJoe Perches				$orig_desc .= " " . $1;
309119c146a6SJoe Perches				$hasparens = 1;
30920d7835fcSJoe Perches			}
30930d7835fcSJoe Perches
30940d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
30950d7835fcSJoe Perches							      $id, $orig_desc);
30960d7835fcSJoe Perches
3097948b133aSHeinrich Schuchardt			if (defined($id) &&
3098948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
3099d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
31000d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
31010d7835fcSJoe Perches			}
3102d311cd44SJoe Perches		}
3103d311cd44SJoe Perches
310413f1937eSJoe Perches# Check for added, moved or deleted files
310513f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
310613f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
310713f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
310813f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
310913f1937eSJoe Perches		      (defined($1) || defined($2))))) {
3110a82603a8SAndrew Jeffery			$is_patch = 1;
311113f1937eSJoe Perches			$reported_maintainer_file = 1;
311213f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
311313f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
311413f1937eSJoe Perches		}
311513f1937eSJoe Perches
3116e400edb1SRob Herring# Check for adding new DT bindings not in schema format
3117e400edb1SRob Herring		if (!$in_commit_log &&
3118e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3119e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3120e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
3121e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3122e400edb1SRob Herring		}
3123e400edb1SRob Herring
312400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
31258905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3126000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
3127000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
31286c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
3129de7d4f0eSAndy Whitcroft		}
3130de7d4f0eSAndy Whitcroft
3131de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3132de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3133171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
3134171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3135171ae1a4SAndy Whitcroft
3136171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
3137171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3138171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
3139171ae1a4SAndy Whitcroft
314034d99219SJoe Perches			CHK("INVALID_UTF8",
3141000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
314200df344fSAndy Whitcroft		}
31430a920b5bSAndy Whitcroft
314415662b3eSJoe Perches# Check if it's the start of a commit log
314515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
314615662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
3147eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
3148eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
314915662b3eSJoe Perches			$in_header_lines = 0;
315015662b3eSJoe Perches			$in_commit_log = 1;
3151ed43c4e5SAllen Hubbe			$has_commit_log = 1;
315215662b3eSJoe Perches		}
315315662b3eSJoe Perches
3154fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
3155fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
3156fa64205dSPasi Savanainen		if ($in_header_lines &&
3157fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3158fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
3159fa64205dSPasi Savanainen			$non_utf8_charset = 1;
3160fa64205dSPasi Savanainen		}
3161fa64205dSPasi Savanainen
3162fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
316315662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
3164fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
316515662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
316615662b3eSJoe Perches		}
316715662b3eSJoe Perches
3168d6430f71SJoe Perches# Check for absolute kernel paths in commit message
3169d6430f71SJoe Perches		if ($tree && $in_commit_log) {
3170d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
3171d6430f71SJoe Perches				my $file = $1;
3172d6430f71SJoe Perches
3173d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3174d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
3175d6430f71SJoe Perches					#
3176d6430f71SJoe Perches				} else {
3177d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
3178d6430f71SJoe Perches				}
3179d6430f71SJoe Perches			}
3180d6430f71SJoe Perches		}
3181d6430f71SJoe Perches
318266b47b4aSKees Cook# Check for various typo / spelling mistakes
318366d7a382SJoe Perches		if (defined($misspellings) &&
318466d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
31857da07c31SDwaipayan Ray			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
318666b47b4aSKees Cook				my $typo = $1;
31877da07c31SDwaipayan Ray				my $blank = copy_spacing($rawline);
31887da07c31SDwaipayan Ray				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
31897da07c31SDwaipayan Ray				my $hereptr = "$hereline$ptr\n";
319066b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
319166b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
319266b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
31930675a8fbSJean Delvare				my $msg_level = \&WARN;
31940675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
31950675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
31967da07c31SDwaipayan Ray						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
319766b47b4aSKees Cook				    $fix) {
319866b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
319966b47b4aSKees Cook				}
320066b47b4aSKees Cook			}
320166b47b4aSKees Cook		}
320266b47b4aSKees Cook
3203a8dd86bfSMatteo Croce# check for invalid commit id
3204a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3205a8dd86bfSMatteo Croce			my $id;
3206a8dd86bfSMatteo Croce			my $description;
3207a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
3208a8dd86bfSMatteo Croce			if (!defined($id)) {
3209a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
3210a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3211a8dd86bfSMatteo Croce			}
3212a8dd86bfSMatteo Croce		}
3213a8dd86bfSMatteo Croce
3214310cd06bSJoe Perches# check for repeated words separated by a single space
32158d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
32168d0325ccSAditya Srivastava		if (($rawline =~ /^\+/ || $in_commit_log) &&
32178d0325ccSAditya Srivastava		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
32181db81a68SDwaipayan Ray			pos($rawline) = 1 if (!$in_commit_log);
3219310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3220310cd06bSJoe Perches
3221310cd06bSJoe Perches				my $first = $1;
3222310cd06bSJoe Perches				my $second = $2;
32231db81a68SDwaipayan Ray				my $start_pos = $-[1];
32241db81a68SDwaipayan Ray				my $end_pos = $+[2];
3225310cd06bSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3226310cd06bSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3227310cd06bSJoe Perches					next;
3228310cd06bSJoe Perches				}
3229310cd06bSJoe Perches
32301db81a68SDwaipayan Ray				next if (lc($first) ne lc($second));
3231310cd06bSJoe Perches				next if ($first eq 'long');
3232310cd06bSJoe Perches
32331db81a68SDwaipayan Ray				# check for character before and after the word matches
32341db81a68SDwaipayan Ray				my $start_char = '';
32351db81a68SDwaipayan Ray				my $end_char = '';
32361db81a68SDwaipayan Ray				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
32371db81a68SDwaipayan Ray				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
32381db81a68SDwaipayan Ray
32391db81a68SDwaipayan Ray				next if ($start_char =~ /^\S$/);
32401db81a68SDwaipayan Ray				next if (index(" \t.,;?!", $end_char) == -1);
32411db81a68SDwaipayan Ray
32428d0325ccSAditya Srivastava                                # avoid repeating hex occurrences like 'ff ff fe 09 ...'
32438d0325ccSAditya Srivastava                                if ($first =~ /\b[0-9a-f]{2,}\b/i) {
32448d0325ccSAditya Srivastava                                        next if (!exists($allow_repeated_words{lc($first)}));
32458d0325ccSAditya Srivastava                                }
32468d0325ccSAditya Srivastava
3247310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3248310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3249310cd06bSJoe Perches				    $fix) {
3250310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3251310cd06bSJoe Perches				}
3252310cd06bSJoe Perches			}
3253310cd06bSJoe Perches
3254310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3255310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3256310cd06bSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3257310cd06bSJoe Perches				my $last_word = $1;
3258310cd06bSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3259310cd06bSJoe Perches					if (WARN("REPEATED_WORD",
3260310cd06bSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3261310cd06bSJoe Perches					    $fix) {
3262310cd06bSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3263310cd06bSJoe Perches					}
3264310cd06bSJoe Perches				}
3265310cd06bSJoe Perches			}
3266310cd06bSJoe Perches		}
3267310cd06bSJoe Perches
326830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
326930670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
327000df344fSAndy Whitcroft
32710a920b5bSAndy Whitcroft#trailing whitespace
32729c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
3273c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3274d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3275d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
3276d5e616fcSJoe Perches			    $fix) {
3277194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3278d5e616fcSJoe Perches			}
3279c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3280c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32813705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
32823705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
32833705ce5bSJoe Perches			    $fix) {
3284194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
32853705ce5bSJoe Perches			}
32863705ce5bSJoe Perches
3287d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32880a920b5bSAndy Whitcroft		}
32895368df20SAndy Whitcroft
32904783f894SJosh Triplett# Check for FSF mailing addresses.
3291109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
32921bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
32933e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
32943e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
32954783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32960675a8fbSJean Delvare			my $msg_level = \&ERROR;
32970675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
32980675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
32994783f894SJosh Triplett				      "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
33004783f894SJosh Triplett		}
33014783f894SJosh Triplett
33023354957aSAndi Kleen# check for Kconfig help text having a real description
33039fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
33049fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
33053354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3306678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3307678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3308678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3309678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
33103354957aSAndi Kleen			my $length = 0;
33119fe287d7SAndy Whitcroft			my $cnt = $realcnt;
33129fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
33139fe287d7SAndy Whitcroft			my $f;
3314a1385803SAndy Whitcroft			my $is_start = 0;
33159fe287d7SAndy Whitcroft			my $is_end = 0;
3316a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
33179fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
33189fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
33199fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
33209fe287d7SAndy Whitcroft
33219fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
33228d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3323a1385803SAndy Whitcroft
332486adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3325a1385803SAndy Whitcroft					$is_start = 1;
332622a4ac02SMasahiro Yamada				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
3327a1385803SAndy Whitcroft					$length = -1;
3328a1385803SAndy Whitcroft				}
3329a1385803SAndy Whitcroft
33309fe287d7SAndy Whitcroft				$f =~ s/^.//;
33313354957aSAndi Kleen				$f =~ s/#.*//;
33323354957aSAndi Kleen				$f =~ s/^\s+//;
33333354957aSAndi Kleen				next if ($f =~ /^$/);
3334678ae162SUlf Magnusson
3335678ae162SUlf Magnusson				# This only checks context lines in the patch
3336678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3337678ae162SUlf Magnusson				# positives, even though some of these are
3338678ae162SUlf Magnusson				# common words in help texts
3339678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3340678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
33419fe287d7SAndy Whitcroft					$is_end = 1;
33429fe287d7SAndy Whitcroft					last;
33439fe287d7SAndy Whitcroft				}
33443354957aSAndi Kleen				$length++;
33453354957aSAndi Kleen			}
334656193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3347000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
334856193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
334956193274SVadim Bendebury			}
3350a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
33513354957aSAndi Kleen		}
33523354957aSAndi Kleen
33537ccf41a8SJoe Perches# check MAINTAINERS entries
33547ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
33557ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
33567ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3357628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3358628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3359628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3360628f91a2SJoe Perches				    $fix) {
3361628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3362628f91a2SJoe Perches				}
3363628f91a2SJoe Perches			}
33647ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
33657ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
33667ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
33677ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
33687ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
33697ccf41a8SJoe Perches				my $cur = $1;
33707ccf41a8SJoe Perches				my $curval = $2;
33717ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
33727ccf41a8SJoe Perches				my $prev = $1;
33737ccf41a8SJoe Perches				my $prevval = $2;
33747ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
33757ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
33767ccf41a8SJoe Perches				if ($curindex < 0) {
33777ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
33787ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
33797ccf41a8SJoe Perches				} else {
33807ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
33817ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33827ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
33837ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
33847ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
33857ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
33867ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33877ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
33887ccf41a8SJoe Perches					}
33897ccf41a8SJoe Perches				}
33907ccf41a8SJoe Perches			}
33917ccf41a8SJoe Perches		}
3392628f91a2SJoe Perches
3393327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3394327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3395327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3396327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3397327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3398327953e9SChristoph Jaeger		}
3399327953e9SChristoph Jaeger
3400c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3401c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3402c68e5878SArnaud Lacombe			my $flag = $1;
3403c68e5878SArnaud Lacombe			my $replacement = {
3404c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3405c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3406c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3407c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3408c68e5878SArnaud Lacombe			};
3409c68e5878SArnaud Lacombe
3410c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3411c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3412c68e5878SArnaud Lacombe		}
3413c68e5878SArnaud Lacombe
3414bff5da43SRob Herring# check for DT compatible documentation
34157dd05b38SFlorian Vaussard		if (defined $root &&
34167dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
34177dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
34187dd05b38SFlorian Vaussard
3419bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3420bff5da43SRob Herring
3421cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3422852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3423cc93319bSFlorian Vaussard
3424bff5da43SRob Herring			foreach my $compat (@compats) {
3425bff5da43SRob Herring				my $compat2 = $compat;
3426185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3427185d566bSRob Herring				my $compat3 = $compat;
3428185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3429185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3430bff5da43SRob Herring				if ( $? >> 8 ) {
3431bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3432bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3433bff5da43SRob Herring				}
3434bff5da43SRob Herring
34354fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
34364fbf32a6SFlorian Vaussard				my $vendor = $1;
3437852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3438bff5da43SRob Herring				if ( $? >> 8 ) {
3439bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3440cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3441bff5da43SRob Herring				}
3442bff5da43SRob Herring			}
3443bff5da43SRob Herring		}
3444bff5da43SRob Herring
34459f3a8992SRob Herring# check for using SPDX license tag at beginning of files
34469f3a8992SRob Herring		if ($realline == $checklicenseline) {
34479f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
34489f3a8992SRob Herring				$checklicenseline = 2;
34499f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
34509f3a8992SRob Herring				my $comment = "";
34519f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
34529f3a8992SRob Herring					$comment = '/*';
34539f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
34549f3a8992SRob Herring					$comment = '//';
3455c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
34569f3a8992SRob Herring					$comment = '#';
34579f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
34589f3a8992SRob Herring					$comment = '..';
34599f3a8992SRob Herring				}
34609f3a8992SRob Herring
3461fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3462fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3463fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3464ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3465fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3466fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3467fdf13693SJoe Perches				}
3468fdf13693SJoe Perches
34699f3a8992SRob Herring				if ($comment !~ /^$/ &&
3470ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
34719f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
34729f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
34733b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
34743b6e8ac9SJoe Perches					my $spdx_license = $1;
34753b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
34763b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
34773b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
34783b6e8ac9SJoe Perches					}
347950c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
348050c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
348150c92900SLubomir Rintel						my $msg_level = \&WARN;
348250c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
348350c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
348450c92900SLubomir Rintel
348550c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
348650c92900SLubomir Rintel						    $fix) {
348750c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
348850c92900SLubomir Rintel						}
348950c92900SLubomir Rintel					}
34909f3a8992SRob Herring				}
34919f3a8992SRob Herring			}
34929f3a8992SRob Herring		}
34939f3a8992SRob Herring
3494a0154cdbSJoe Perches# check for embedded filenames
3495a0154cdbSJoe Perches		if ($rawline =~ /^\+.*\Q$realfile\E/) {
3496a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3497a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3498a0154cdbSJoe Perches		}
3499a0154cdbSJoe Perches
35005368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3501d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
35025368df20SAndy Whitcroft
3503a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3504a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3505a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3506a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3507a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3508a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3509a8da38a9SJoe Perches		}
3510a8da38a9SJoe Perches
351147e0c88bSJoe Perches# line length limit (with some exclusions)
351247e0c88bSJoe Perches#
351347e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
351447e0c88bSJoe Perches#	logging functions like pr_info that end in a string
351547e0c88bSJoe Perches#	lines with a single string
351647e0c88bSJoe Perches#	#defines that are a single string
35172e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
351847e0c88bSJoe Perches#
351947e0c88bSJoe Perches# There are 3 different line length message types:
3520ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
352147e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
352247e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
352347e0c88bSJoe Perches#
352447e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
352547e0c88bSJoe Perches#
352647e0c88bSJoe Perches
3527b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
352847e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
352947e0c88bSJoe Perches
353047e0c88bSJoe Perches			# Check the allowed long line types first
353147e0c88bSJoe Perches
353247e0c88bSJoe Perches			# logging functions that end in a string that starts
353347e0c88bSJoe Perches			# before $max_line_length
353447e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
353547e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
353647e0c88bSJoe Perches				$msg_type = "";
353747e0c88bSJoe Perches
353847e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
353947e0c88bSJoe Perches			# #defines with only strings
354047e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
354147e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
354247e0c88bSJoe Perches				$msg_type = "";
354347e0c88bSJoe Perches
3544cc147506SJoe Perches			# More special cases
3545cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3546cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3547d560a5f8SJoe Perches				$msg_type = "";
3548d560a5f8SJoe Perches
35492e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
35502e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
35512e4bbbc5SAndreas Brauchli				$msg_type = "";
35522e4bbbc5SAndreas Brauchli
355347e0c88bSJoe Perches			# Otherwise set the alternate message types
355447e0c88bSJoe Perches
355547e0c88bSJoe Perches			# a comment starts before $max_line_length
355647e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
355747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
355847e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
355947e0c88bSJoe Perches
356047e0c88bSJoe Perches			# a quoted string starts before $max_line_length
356147e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
356247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
356347e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
356447e0c88bSJoe Perches			}
356547e0c88bSJoe Perches
356647e0c88bSJoe Perches			if ($msg_type ne "" &&
356747e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3568bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3569bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3570bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3571bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
35720a920b5bSAndy Whitcroft			}
357347e0c88bSJoe Perches		}
35740a920b5bSAndy Whitcroft
35758905a67cSAndy Whitcroft# check for adding lines without a newline.
35768905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
357747ca69b8STom Rix			if (WARN("MISSING_EOF_NEWLINE",
357847ca69b8STom Rix			         "adding a line without newline at end of file\n" . $herecurr) &&
357947ca69b8STom Rix			    $fix) {
358047ca69b8STom Rix				fix_delete_line($fixlinenr+1, "No newline at end of file");
358147ca69b8STom Rix			}
35828905a67cSAndy Whitcroft		}
35838905a67cSAndy Whitcroft
3584b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3585de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
35860a920b5bSAndy Whitcroft
35870a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3588713a09deSAntonio Borneo# more than $tabsize must use tabs.
3589c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3590c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3591c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3592d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
35933705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
35943705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
35953705ce5bSJoe Perches			    $fix) {
3596194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
35973705ce5bSJoe Perches			}
35980a920b5bSAndy Whitcroft		}
35990a920b5bSAndy Whitcroft
360008e44365SAlberto Panizzo# check for space before tabs.
360108e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
360208e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
36033705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
36043705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
36053705ce5bSJoe Perches			    $fix) {
3606194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3607713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3608194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3609c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
36103705ce5bSJoe Perches			}
361108e44365SAlberto Panizzo		}
361208e44365SAlberto Panizzo
36136a487211SJoe Perches# check for assignments on the start of a line
36146a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3615da7355abSAditya Srivastava			my $operator = $1;
3616da7355abSAditya Srivastava			if (CHK("ASSIGNMENT_CONTINUATIONS",
3617da7355abSAditya Srivastava				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3618da7355abSAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
3619da7355abSAditya Srivastava				# add assignment operator to the previous line, remove from current line
3620da7355abSAditya Srivastava				$fixed[$fixlinenr - 1] .= " $operator";
3621da7355abSAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3622da7355abSAditya Srivastava			}
36236a487211SJoe Perches		}
36246a487211SJoe Perches
3625d1fe9c09SJoe Perches# check for && or || at the start of a line
3626d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
36278e08f076SAditya Srivastava			my $operator = $1;
36288e08f076SAditya Srivastava			if (CHK("LOGICAL_CONTINUATIONS",
36298e08f076SAditya Srivastava				"Logical continuations should be on the previous line\n" . $hereprev) &&
36308e08f076SAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
36318e08f076SAditya Srivastava				# insert logical operator at last non-comment, non-whitepsace char on previous line
36328e08f076SAditya Srivastava				$prevline =~ /[\s$;]*$/;
36338e08f076SAditya Srivastava				my $line_end = substr($prevrawline, $-[0]);
36348e08f076SAditya Srivastava				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
36358e08f076SAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
36368e08f076SAditya Srivastava			}
3637d1fe9c09SJoe Perches		}
3638d1fe9c09SJoe Perches
3639a91e8994SJoe Perches# check indentation starts on a tab stop
36405b57980dSJoe Perches		if ($perl_version_ok &&
3641bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3642a91e8994SJoe Perches			my $indent = length($1);
3643713a09deSAntonio Borneo			if ($indent % $tabsize) {
3644a91e8994SJoe Perches				if (WARN("TABSTOP",
3645a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3646a91e8994SJoe Perches				    $fix) {
3647713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3648a91e8994SJoe Perches				}
3649a91e8994SJoe Perches			}
3650a91e8994SJoe Perches		}
3651a91e8994SJoe Perches
3652d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
36535b57980dSJoe Perches		if ($perl_version_ok &&
3654fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3655d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3656d1fe9c09SJoe Perches			my $oldindent = $1;
3657d1fe9c09SJoe Perches			my $rest = $2;
3658d1fe9c09SJoe Perches
3659d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3660d1fe9c09SJoe Perches			if ($pos >= 0) {
3661b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3662b34a26f3SJoe Perches				my $newindent = $2;
3663d1fe9c09SJoe Perches
3664d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3665713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3666713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3667d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3668d1fe9c09SJoe Perches
3669d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3670d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
36713705ce5bSJoe Perches
36723705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
36733705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
36743705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3675194f66fcSJoe Perches						$fixed[$fixlinenr] =~
36763705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
36773705ce5bSJoe Perches					}
3678d1fe9c09SJoe Perches				}
3679d1fe9c09SJoe Perches			}
3680d1fe9c09SJoe Perches		}
3681d1fe9c09SJoe Perches
36826ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
36836ab3a970SJoe Perches# avoid checking a few false positives:
36846ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
36856ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
36866ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
36876ab3a970SJoe Perches#   multiline macros that define functions
36886ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
36896ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
36906ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
36913705ce5bSJoe Perches			if (CHK("SPACING",
3692f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
36933705ce5bSJoe Perches			    $fix) {
3694194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3695f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
36963705ce5bSJoe Perches			}
3697aad4f614SJoe Perches		}
3698aad4f614SJoe Perches
369986406b1cSJoe Perches# Block comment styles
370086406b1cSJoe Perches# Networking with an initial /*
370105880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3702fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
370385ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
3704c70735c2SŁukasz Stelmach		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
370505880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
370605880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
370705880600SJoe Perches		}
370805880600SJoe Perches
370986406b1cSJoe Perches# Block comments use * on subsequent lines
371086406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
371186406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3712a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
371361135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3714a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
371586406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
371686406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3717a605e32eSJoe Perches		}
3718a605e32eSJoe Perches
371986406b1cSJoe Perches# Block comments use */ on trailing lines
372086406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3721c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3722c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3723c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
372486406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
372586406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
372605880600SJoe Perches		}
372705880600SJoe Perches
372808eb9b80SJoe Perches# Block comment * alignment
372908eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3730af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3731af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3732af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
373308eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3734af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3735af207524SJoe Perches			my $oldindent;
373608eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3737af207524SJoe Perches			if (defined($1)) {
3738af207524SJoe Perches				$oldindent = expand_tabs($1);
3739af207524SJoe Perches			} else {
3740af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3741af207524SJoe Perches				$oldindent = expand_tabs($1);
3742af207524SJoe Perches			}
374308eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
374408eb9b80SJoe Perches			my $newindent = $1;
374508eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3746af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
374708eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
374808eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
374908eb9b80SJoe Perches			}
375008eb9b80SJoe Perches		}
375108eb9b80SJoe Perches
37527f619191SJoe Perches# check for missing blank lines after struct/union declarations
37537f619191SJoe Perches# with exceptions for various attributes and macros
37547f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
37557f619191SJoe Perches		    $line =~ /^\+/ &&
37567f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
37577f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
37587f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
37597f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
37607f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
37617f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
37627f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
37630bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
37647f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3765d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3766d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3767d752fcc8SJoe Perches			    $fix) {
3768f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3769d752fcc8SJoe Perches			}
37707f619191SJoe Perches		}
37717f619191SJoe Perches
3772365dd4eaSJoe Perches# check for multiple consecutive blank lines
3773365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3774365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3775365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3776d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3777d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3778d752fcc8SJoe Perches			    $fix) {
3779f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3780d752fcc8SJoe Perches			}
3781d752fcc8SJoe Perches
3782365dd4eaSJoe Perches			$last_blank_line = $linenr;
3783365dd4eaSJoe Perches		}
3784365dd4eaSJoe Perches
37853b617e3bSJoe Perches# check for missing blank lines after declarations
37863f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
37873f7bac03SJoe Perches			# actual declarations
37883f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
37895a4e1fd3SJoe Perches			# function pointer declarations
37905a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
37913f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
37923f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
37933f7bac03SJoe Perches			# known declaration macros
37943f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
37953f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
37963f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
37973f7bac03SJoe Perches			# other possible extensions of declaration lines
37983f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
37993f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
38003f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
38013f7bac03SJoe Perches			# looks like a declaration
38023f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
38035a4e1fd3SJoe Perches			# function pointer declarations
38045a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
38053f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
38063f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
38073f7bac03SJoe Perches			# known declaration macros
38083f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
38093f7bac03SJoe Perches			# start of struct or union or enum
3810328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
38113f7bac03SJoe Perches			# start or end of block or continuation of declaration
38123f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
38133f7bac03SJoe Perches			# bitfield continuation
38143f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
38153f7bac03SJoe Perches			# other possible extensions of declaration lines
38163f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
38173f7bac03SJoe Perches			# indentation of previous and current line are the same
38183f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3819d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3820d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3821d752fcc8SJoe Perches			    $fix) {
3822f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3823d752fcc8SJoe Perches			}
38243b617e3bSJoe Perches		}
38253b617e3bSJoe Perches
38265f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
38276b4c5bebSAndy Whitcroft# Exceptions:
38286b4c5bebSAndy Whitcroft#  1) within comments
38296b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
38306b4c5bebSAndy Whitcroft#  3) hanging labels
38313705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
38325f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
38333705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
38343705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
38353705ce5bSJoe Perches			    $fix) {
3836194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
38373705ce5bSJoe Perches			}
38385f7ddae6SRaffaele Recalcati		}
38395f7ddae6SRaffaele Recalcati
3840b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3841b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3842b9ea10d6SAndy Whitcroft
38435751a24eSJoe Perches# check for unusual line ending [ or (
38445751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
38455751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
38465751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
38475751a24eSJoe Perches		}
38485751a24eSJoe Perches
38494dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
38504dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
38514dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
38524dbed76fSJoe Perches			$context_function = $1;
38534dbed76fSJoe Perches		}
38544dbed76fSJoe Perches
38554dbed76fSJoe Perches# check if this appears to be the end of function declaration
38564dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
38574dbed76fSJoe Perches			undef $context_function;
38584dbed76fSJoe Perches		}
38594dbed76fSJoe Perches
3860032a4c0fSJoe Perches# check indentation of any line with a bare else
3861840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3862032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3863032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3864032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3865840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3866840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3867840080a0SJoe Perches			     defined $lines[$linenr] &&
3868840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3869032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3870032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3871032a4c0fSJoe Perches			}
3872032a4c0fSJoe Perches		}
3873032a4c0fSJoe Perches
3874c00df19aSJoe Perches# check indentation of a line with a break;
3875dc58bc55SJoe Perches# if the previous line is a goto, return or break
3876dc58bc55SJoe Perches# and is indented the same # of tabs
3877c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3878c00df19aSJoe Perches			my $tabs = $1;
3879dc58bc55SJoe Perches			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3880dc58bc55SJoe Perches				if (WARN("UNNECESSARY_BREAK",
3881dc58bc55SJoe Perches					 "break is not useful after a $1\n" . $hereprev) &&
3882dc58bc55SJoe Perches				    $fix) {
3883dc58bc55SJoe Perches					fix_delete_line($fixlinenr, $rawline);
3884dc58bc55SJoe Perches				}
3885c00df19aSJoe Perches			}
3886c00df19aSJoe Perches		}
3887c00df19aSJoe Perches
3888c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3889cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3890000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3891000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3892c2fdda0dSAndy Whitcroft		}
389322f2a2efSAndy Whitcroft
389456e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
389556e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
389656e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
389756e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
389856e77d70SJoe Perches		}
389956e77d70SJoe Perches
39009c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
39012b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
39022b474a1aSAndy Whitcroft		    $realline_next);
39033e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3904ca819864SJoe Perches		if ($linenr > $suppress_statement &&
39051b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3906170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3907f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3908171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3909171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3910171ae1a4SAndy Whitcroft
39113e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
39123e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
39133e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
39143e469cdcSAndy Whitcroft			# until we hit end of it.
39153e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
39163e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
39173e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
39183e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
39193e469cdcSAndy Whitcroft			}
3920f74bd194SAndy Whitcroft
39212b474a1aSAndy Whitcroft			# Find the real next line.
39222b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
39232b474a1aSAndy Whitcroft			if (defined $realline_next &&
39242b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
39252b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
39262b474a1aSAndy Whitcroft				$realline_next++;
39272b474a1aSAndy Whitcroft			}
39282b474a1aSAndy Whitcroft
3929171ae1a4SAndy Whitcroft			my $s = $stat;
3930171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3931cf655043SAndy Whitcroft
3932c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3933171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3934c2fdda0dSAndy Whitcroft
3935c2fdda0dSAndy Whitcroft			# Ignore functions being called
3936171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3937c2fdda0dSAndy Whitcroft
3938463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3939463f2864SAndy Whitcroft
3940c45dcabdSAndy Whitcroft			# declarations always start with types
3941d2506586SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
3942c45dcabdSAndy Whitcroft				my $type = $1;
3943c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3944c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3945c45dcabdSAndy Whitcroft
39466c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3947a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3948c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3949c2fdda0dSAndy Whitcroft			}
39508905a67cSAndy Whitcroft
39516c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
395265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3953c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
39549c0ca6f9SAndy Whitcroft			}
39558905a67cSAndy Whitcroft
39568905a67cSAndy Whitcroft			# Check for any sort of function declaration.
39578905a67cSAndy Whitcroft			# int foo(something bar, other baz);
39588905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3959171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
39608905a67cSAndy Whitcroft				my ($name_len) = length($1);
39618905a67cSAndy Whitcroft
3962cf655043SAndy Whitcroft				my $ctx = $s;
3963773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
39648905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3965cf655043SAndy Whitcroft
39668905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3967c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
39688905a67cSAndy Whitcroft
3969c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
39708905a67cSAndy Whitcroft					}
39718905a67cSAndy Whitcroft				}
39728905a67cSAndy Whitcroft			}
39738905a67cSAndy Whitcroft
39749c0ca6f9SAndy Whitcroft		}
39759c0ca6f9SAndy Whitcroft
397600df344fSAndy Whitcroft#
397700df344fSAndy Whitcroft# Checks which may be anchored in the context.
397800df344fSAndy Whitcroft#
397900df344fSAndy Whitcroft
398000df344fSAndy Whitcroft# Check for switch () and associated case and default
398100df344fSAndy Whitcroft# statements should be at the same indent.
398200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
398300df344fSAndy Whitcroft			my $err = '';
398400df344fSAndy Whitcroft			my $sep = '';
398500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
398600df344fSAndy Whitcroft			shift(@ctx);
398700df344fSAndy Whitcroft			for my $ctx (@ctx) {
398800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
398900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
399000df344fSAndy Whitcroft							$indent != $cindent) {
399100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
399200df344fSAndy Whitcroft					$sep = '';
399300df344fSAndy Whitcroft				} else {
399400df344fSAndy Whitcroft					$sep = "[...]\n";
399500df344fSAndy Whitcroft				}
399600df344fSAndy Whitcroft			}
399700df344fSAndy Whitcroft			if ($err ne '') {
3998000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3999000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
4000de7d4f0eSAndy Whitcroft			}
4001de7d4f0eSAndy Whitcroft		}
4002de7d4f0eSAndy Whitcroft
4003de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
4004de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
40050fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4006773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
4007773647a0SAndy Whitcroft
40089c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
40098eef05ddSJoe Perches
40108eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
40118eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
40128eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
40138eef05ddSJoe Perches			}
40148eef05ddSJoe Perches
4015de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
4016de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
4017de7d4f0eSAndy Whitcroft
4018548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
4019548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
4020de7d4f0eSAndy Whitcroft
4021548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4022548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
4023548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
4024548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4025548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4026773647a0SAndy Whitcroft				$ctx_ln++;
4027773647a0SAndy Whitcroft			}
4028548596d5SAndy Whitcroft
402953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
403053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4031773647a0SAndy Whitcroft
4032773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4033000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
4034000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
403501464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
403600df344fSAndy Whitcroft			}
4037773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4038773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
4039773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
4040773647a0SAndy Whitcroft			{
40419c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
40429c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
4043000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
4044000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
404501464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
40469c0ca6f9SAndy Whitcroft				}
40479c0ca6f9SAndy Whitcroft			}
404800df344fSAndy Whitcroft		}
404900df344fSAndy Whitcroft
40504d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
4051f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
40523e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
40533e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
40543e469cdcSAndy Whitcroft					if (!defined $stat);
40554d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
40564d001e4dSAndy Whitcroft
40574d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
40584d001e4dSAndy Whitcroft
40599f5af480SJoe Perches			# remove inline comments
40609f5af480SJoe Perches			$s =~ s/$;/ /g;
40619f5af480SJoe Perches			$c =~ s/$;/ /g;
40624d001e4dSAndy Whitcroft
40634d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
40646f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
40656f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
40664d001e4dSAndy Whitcroft
40679f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
40689f5af480SJoe Perches			# none on the first line, and are going to readd them
40699f5af480SJoe Perches			# where necessary.
40709f5af480SJoe Perches			$s =~ s/\n./\n/gs;
40719f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
40729f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
40739f5af480SJoe Perches			}
40749f5af480SJoe Perches
40754d001e4dSAndy Whitcroft			# We want to check the first line inside the block
40764d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
40774d001e4dSAndy Whitcroft			#  1) any blank line termination
40784d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
40794d001e4dSAndy Whitcroft			#  3) any do (...) {
40804d001e4dSAndy Whitcroft			my $continuation = 0;
40814d001e4dSAndy Whitcroft			my $check = 0;
40824d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
40834d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
40844d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
40854d001e4dSAndy Whitcroft				$continuation = 1;
40864d001e4dSAndy Whitcroft			}
40879bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
40884d001e4dSAndy Whitcroft				$check = 1;
40894d001e4dSAndy Whitcroft				$cond_lines++;
40904d001e4dSAndy Whitcroft			}
40914d001e4dSAndy Whitcroft
40924d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
40934d001e4dSAndy Whitcroft			# preprocessor statement.
40944d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
40954d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
40964d001e4dSAndy Whitcroft				$check = 0;
40974d001e4dSAndy Whitcroft			}
40984d001e4dSAndy Whitcroft
40999bd49efeSAndy Whitcroft			my $cond_ptr = -1;
4100740504c6SAndy Whitcroft			$continuation = 0;
41019bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
41029bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
41034d001e4dSAndy Whitcroft
4104f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
4105f16fa28fSAndy Whitcroft				# is not linear.
4106f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4107f16fa28fSAndy Whitcroft					$check = 0;
4108f16fa28fSAndy Whitcroft				}
4109f16fa28fSAndy Whitcroft
41109bd49efeSAndy Whitcroft				# Ignore:
41119bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
41129bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
41139bd49efeSAndy Whitcroft				#  3) labels.
4114740504c6SAndy Whitcroft				if ($continuation ||
4115740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
41169bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
41179bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
4118740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
411930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
41209bd49efeSAndy Whitcroft						$cond_lines++;
41219bd49efeSAndy Whitcroft					}
41224d001e4dSAndy Whitcroft				}
412330dad6ebSAndy Whitcroft			}
41244d001e4dSAndy Whitcroft
41254d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
41264d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
41274d001e4dSAndy Whitcroft
41284d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
41294d001e4dSAndy Whitcroft			# this is not this patch's fault.
41304d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
41314d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
41324d001e4dSAndy Whitcroft				$check = 0;
41334d001e4dSAndy Whitcroft			}
41344d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
41354d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
41364d001e4dSAndy Whitcroft			}
41374d001e4dSAndy Whitcroft
41389bd49efeSAndy Whitcroft			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
41394d001e4dSAndy Whitcroft
41409f5af480SJoe Perches			if ($check && $s ne '' &&
4141713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
41429f5af480SJoe Perches			     ($sindent < $indent) ||
4143f6950a73SJoe Perches			     ($sindent == $indent &&
4144f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4145713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
4146000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
4147000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
41484d001e4dSAndy Whitcroft			}
41494d001e4dSAndy Whitcroft		}
41504d001e4dSAndy Whitcroft
41516c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
41526c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
41531f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
41541f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
41556c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
4156c2fdda0dSAndy Whitcroft		if ($dbg_values) {
4157c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
4158cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
4159cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
41601f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
4161c2fdda0dSAndy Whitcroft		}
41626c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
41636c72ffaaSAndy Whitcroft
416400df344fSAndy Whitcroft#ignore lines not being added
41653705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
416600df344fSAndy Whitcroft
416799ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
416899ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
416999ca38c2SJoe Perches#	struct foo bar = *(&(bar));
417099ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
417199ca38c2SJoe Perches			my $var = $1;
417299ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
417399ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
417499ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
417599ca38c2SJoe Perches			}
417699ca38c2SJoe Perches		}
417799ca38c2SJoe Perches
417811ca40a0SJoe Perches# check for dereferences that span multiple lines
417911ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
418011ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
418111ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
418211ca40a0SJoe Perches			my $ref = $1;
418311ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
418411ca40a0SJoe Perches			$ref .= $1;
418511ca40a0SJoe Perches			$ref =~ s/\s//g;
418611ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
418711ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
418811ca40a0SJoe Perches		}
418911ca40a0SJoe Perches
4190a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
4191c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4192a1ce18e4SJoe Perches			my $type = $1;
4193a1ce18e4SJoe Perches			my $var = $2;
4194207a8e84SJoe Perches			$var = "" if (!defined $var);
4195207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4196a1ce18e4SJoe Perches				my $sign = $1;
4197a1ce18e4SJoe Perches				my $pointer = $2;
4198a1ce18e4SJoe Perches
4199a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
4200a1ce18e4SJoe Perches
4201a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
4202a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4203a1ce18e4SJoe Perches				    $fix) {
4204a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
4205207a8e84SJoe Perches					my $comp_pointer = $pointer;
4206207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
4207207a8e84SJoe Perches					$decl .= $comp_pointer;
4208207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
4209207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4210a1ce18e4SJoe Perches				}
4211a1ce18e4SJoe Perches			}
4212a1ce18e4SJoe Perches		}
4213a1ce18e4SJoe Perches
4214653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
42157429c690SAndy Whitcroft		if ($dbg_type) {
42167429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
4217000d1cc1SJoe Perches				ERROR("TEST_TYPE",
4218000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
42197429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4220000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
4221000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
42227429c690SAndy Whitcroft			}
4223653d4876SAndy Whitcroft			next;
4224653d4876SAndy Whitcroft		}
4225a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
4226a1ef277eSAndy Whitcroft		if ($dbg_attr) {
42279360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
4228000d1cc1SJoe Perches				ERROR("TEST_ATTR",
4229000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
42309360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4231000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
4232000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
4233a1ef277eSAndy Whitcroft			}
4234a1ef277eSAndy Whitcroft			next;
4235a1ef277eSAndy Whitcroft		}
4236653d4876SAndy Whitcroft
4237f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
423899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
423999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
4240d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4241d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4242f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4243f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4244f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4245d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4246d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4247f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4248d752fcc8SJoe Perches				$fixedline = $line;
42498d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4250f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4251d752fcc8SJoe Perches			}
4252f0a594c1SAndy Whitcroft		}
4253f0a594c1SAndy Whitcroft
425400df344fSAndy Whitcroft#
425500df344fSAndy Whitcroft# Checks which are anchored on the added line.
425600df344fSAndy Whitcroft#
425700df344fSAndy Whitcroft
4258653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4259c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4260653d4876SAndy Whitcroft			my $path = $1;
4261653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4262000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4263495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4264495e9d84SJoe Perches			}
4265495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4266495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4267495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4268653d4876SAndy Whitcroft			}
4269653d4876SAndy Whitcroft		}
4270653d4876SAndy Whitcroft
427100df344fSAndy Whitcroft# no C99 // comments
427200df344fSAndy Whitcroft		if ($line =~ m{//}) {
42733705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
42743705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
42753705ce5bSJoe Perches			    $fix) {
4276194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
42773705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
42783705ce5bSJoe Perches					my $comment = trim($1);
4279194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
42803705ce5bSJoe Perches				}
42813705ce5bSJoe Perches			}
428200df344fSAndy Whitcroft		}
428300df344fSAndy Whitcroft		# Remove C99 comments.
42840a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
42856c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
42860a920b5bSAndy Whitcroft
42872b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
42882b474a1aSAndy Whitcroft# the whole statement.
42892b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
42902b474a1aSAndy Whitcroft		if (defined $realline_next &&
42912b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
42922b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
42932b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
42942b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
42953cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
42963cbf62dfSAndy Whitcroft			# a prefix:
42973cbf62dfSAndy Whitcroft			#   XXX(foo);
42983cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4299653d4876SAndy Whitcroft			my $name = $1;
430087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
43013cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
43023cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
43033cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
43043cbf62dfSAndy Whitcroft
43053cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
43062b474a1aSAndy Whitcroft				\n.}\s*$|
430748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
430848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
430948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
43102b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
43112b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
431248012058SAndy Whitcroft			    )/x) {
43132b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
43142b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
43152b474a1aSAndy Whitcroft			} else {
43162b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
43170a920b5bSAndy Whitcroft			}
43180a920b5bSAndy Whitcroft		}
43192b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
43202b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
43212b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
43222b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
43232b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
43242b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
43252b474a1aSAndy Whitcroft		}
43262b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
43272b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4328000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4329000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
43302b474a1aSAndy Whitcroft		}
43310a920b5bSAndy Whitcroft
43325150bda4SJoe Eloff# check for global initialisers.
43336d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
4334d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
43356d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4336d5e616fcSJoe Perches			    $fix) {
43376d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4338d5e616fcSJoe Perches			}
4339f0a594c1SAndy Whitcroft		}
43400a920b5bSAndy Whitcroft# check for static initialisers.
43416d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4342d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
43436d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4344d5e616fcSJoe Perches				      $herecurr) &&
4345d5e616fcSJoe Perches			    $fix) {
43466d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4347d5e616fcSJoe Perches			}
43480a920b5bSAndy Whitcroft		}
43490a920b5bSAndy Whitcroft
43501813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
43511813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
43521813087dSJoe Perches			my $tmp = trim($1);
43531813087dSJoe Perches			WARN("MISORDERED_TYPE",
43541813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
43551813087dSJoe Perches		}
43561813087dSJoe Perches
4357809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4358809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4359809e082eSJoe Perches			my $type = trim($1);
4360809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4361809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4362809e082eSJoe Perches			my $new_type = $type;
4363809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4364809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4365809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4366809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4367809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4368809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4369809e082eSJoe Perches			$new_type = trim($new_type);
4370809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4371809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4372809e082eSJoe Perches			    $fix) {
4373809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4374809e082eSJoe Perches			}
4375809e082eSJoe Perches		}
4376809e082eSJoe Perches
4377cb710ecaSJoe Perches# check for static const char * arrays.
4378cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4379000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4380000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4381cb710ecaSJoe Perches				$herecurr);
4382cb710ecaSJoe Perches		}
4383cb710ecaSJoe Perches
438477b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
438577b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
438677b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
438777b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
438877b8c0a8SJoe Perches			    $fix) {
438977b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
439077b8c0a8SJoe Perches			}
439177b8c0a8SJoe Perches		}
439277b8c0a8SJoe Perches
4393cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4394cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4395000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4396000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4397cb710ecaSJoe Perches				$herecurr);
4398cb710ecaSJoe Perches		}
4399cb710ecaSJoe Perches
4400ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4401ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4402ab7e23f3SJoe Perches			my $found = $1;
4403ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4404ab7e23f3SJoe Perches				WARN("CONST_CONST",
4405ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4406ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4407ab7e23f3SJoe Perches				WARN("CONST_CONST",
4408ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4409ab7e23f3SJoe Perches			}
4410ab7e23f3SJoe Perches		}
4411ab7e23f3SJoe Perches
441273169765SJoe Perches# check for const static or static <non ptr type> const declarations
441373169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
441473169765SJoe Perches		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
441573169765SJoe Perches		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
441673169765SJoe Perches			if (WARN("STATIC_CONST",
441773169765SJoe Perches				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
441873169765SJoe Perches			    $fix) {
441973169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
442073169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
442173169765SJoe Perches			}
442273169765SJoe Perches		}
442373169765SJoe Perches
44249b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
44259b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
44269b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
44279b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
44289b0fa60dSJoe Perches				$herecurr);
44299b0fa60dSJoe Perches               }
44309b0fa60dSJoe Perches
4431b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4432b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4433b598b670SJoe Perches			my $array = $1;
4434b598b670SJoe Perches			if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4435b598b670SJoe Perches				my $array_div = $1;
4436b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4437b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4438b598b670SJoe Perches				    $fix) {
4439b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4440b598b670SJoe Perches				}
4441b598b670SJoe Perches			}
4442b598b670SJoe Perches		}
4443b598b670SJoe Perches
4444b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
444516b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4446b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4447b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4448b36190c5SJoe Perches			    $fix) {
4449194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4450b36190c5SJoe Perches			}
4451b36190c5SJoe Perches		}
4452b36190c5SJoe Perches
4453653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4454653d4876SAndy Whitcroft# make sense.
4455653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
44568054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4457c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
44588ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
445946d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4460000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4461000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
44620a920b5bSAndy Whitcroft		}
44630a920b5bSAndy Whitcroft
44640a920b5bSAndy Whitcroft# * goes on variable not on type
446565863862SAndy Whitcroft		# (char*[ const])
4466bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4467bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
44683705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4469d8aaf121SAndy Whitcroft
447065863862SAndy Whitcroft			# Should start with a space.
447165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
447265863862SAndy Whitcroft			# Should not end with a space.
447365863862SAndy Whitcroft			$to =~ s/\s+$//;
447465863862SAndy Whitcroft			# '*'s should not have spaces between.
4475f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
447665863862SAndy Whitcroft			}
4477d8aaf121SAndy Whitcroft
44783705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
447965863862SAndy Whitcroft			if ($from ne $to) {
44803705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
44813705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
44823705ce5bSJoe Perches				    $fix) {
44833705ce5bSJoe Perches					my $sub_from = $ident;
44843705ce5bSJoe Perches					my $sub_to = $ident;
44853705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4486194f66fcSJoe Perches					$fixed[$fixlinenr] =~
44873705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
44883705ce5bSJoe Perches				}
448965863862SAndy Whitcroft			}
4490bfcb2cc7SAndy Whitcroft		}
4491bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4492bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
44933705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4494d8aaf121SAndy Whitcroft
449565863862SAndy Whitcroft			# Should start with a space.
449665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
449765863862SAndy Whitcroft			# Should not end with a space.
449865863862SAndy Whitcroft			$to =~ s/\s+$//;
449965863862SAndy Whitcroft			# '*'s should not have spaces between.
4500f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
450165863862SAndy Whitcroft			}
450265863862SAndy Whitcroft			# Modifiers should have spaces.
450365863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
450465863862SAndy Whitcroft
45053705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4506667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
45073705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
45083705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
45093705ce5bSJoe Perches				    $fix) {
45103705ce5bSJoe Perches
45113705ce5bSJoe Perches					my $sub_from = $match;
45123705ce5bSJoe Perches					my $sub_to = $match;
45133705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4514194f66fcSJoe Perches					$fixed[$fixlinenr] =~
45153705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
45163705ce5bSJoe Perches				}
451765863862SAndy Whitcroft			}
45180a920b5bSAndy Whitcroft		}
45190a920b5bSAndy Whitcroft
45209d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
45219d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
45220675a8fbSJean Delvare			my $msg_level = \&WARN;
45230675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
45240675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
45259d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
45269d3e3c70SJoe Perches		}
45270a920b5bSAndy Whitcroft
45289d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
45298905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4530000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4531000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
45328905a67cSAndy Whitcroft		}
45338905a67cSAndy Whitcroft
453417441227SJoe Perches# check for uses of printk_ratelimit
453517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4536000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4537000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
453817441227SJoe Perches		}
453917441227SJoe Perches
4540eeef5733SJoe Perches# printk should use KERN_* levels
4541eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4542000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4543eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
454400df344fSAndy Whitcroft		}
45450a920b5bSAndy Whitcroft
4546f5eea3b0SJoe Perches# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4547f5eea3b0SJoe Perches		if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4548f5eea3b0SJoe Perches			my $printk = $1;
4549f5eea3b0SJoe Perches			my $modifier = $2;
4550f5eea3b0SJoe Perches			my $orig = $3;
4551f5eea3b0SJoe Perches			$modifier = "" if (!defined($modifier));
4552243f3803SJoe Perches			my $level = lc($orig);
4553243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
45548f26b837SJoe Perches			my $level2 = $level;
45558f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4556f5eea3b0SJoe Perches			$level .= $modifier;
4557f5eea3b0SJoe Perches			$level2 .= $modifier;
4558243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4559f5eea3b0SJoe Perches			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to $printk(KERN_$orig ...\n" . $herecurr);
4560243f3803SJoe Perches		}
4561243f3803SJoe Perches
4562f5eea3b0SJoe Perches# prefer dev_<level> to dev_printk(KERN_<LEVEL>
4563dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4564dc139313SJoe Perches			my $orig = $1;
4565dc139313SJoe Perches			my $level = lc($orig);
4566dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4567dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4568dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4569dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4570dc139313SJoe Perches		}
4571dc139313SJoe Perches
45728020b253SNicolas Boichat# trace_printk should not be used in production code.
45738020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
45748020b253SNicolas Boichat			WARN("TRACE_PRINTK",
45758020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
45768020b253SNicolas Boichat		}
45778020b253SNicolas Boichat
457891c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
457991c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
458091c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
458191c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
458291c9afafSAndy Lutomirski			WARN("ENOSYS",
458391c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
458491c9afafSAndy Lutomirski		}
458591c9afafSAndy Lutomirski
45866b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
45876b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
45886b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
45896b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
45906b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
45916b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
45926b9ea5ffSJakub Kicinski			    $fix) {
45936b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
45946b9ea5ffSJakub Kicinski			}
45956b9ea5ffSJakub Kicinski		}
45966b9ea5ffSJakub Kicinski
4597653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4598653d4876SAndy Whitcroft# or if closed on same line
45995b57980dSJoe Perches		if ($perl_version_ok &&
46002d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
46012d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
46022d453e3bSJoe Perches		    $sline !~ /}/) {
46038d182478SJoe Perches			if (ERROR("OPEN_BRACE",
46042d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
46058d182478SJoe Perches			    $fix) {
46068d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
46078d182478SJoe Perches				my $fixed_line = $rawline;
460803f49351SDwaipayan Ray				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
46098d182478SJoe Perches				my $line1 = $1;
46108d182478SJoe Perches				my $line2 = $2;
46118d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
46128d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
46138d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
46148d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
46158d182478SJoe Perches				}
46168d182478SJoe Perches			}
46170a920b5bSAndy Whitcroft		}
4618653d4876SAndy Whitcroft
46198905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
46208905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
46218905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
46228d182478SJoe Perches			if (ERROR("OPEN_BRACE",
46238d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
46248d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
46258d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
46268d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
46278d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
46288d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
46298d182478SJoe Perches				$fixedline = $rawline;
46308d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
46318d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
46328d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46338d182478SJoe Perches				}
46348d182478SJoe Perches			}
46358905a67cSAndy Whitcroft		}
46368905a67cSAndy Whitcroft
46370c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
46383705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
46393705ce5bSJoe Perches			if (WARN("SPACING",
46403705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
46413705ce5bSJoe Perches			    $fix) {
4642194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46433705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
46443705ce5bSJoe Perches			}
46450c73b4ebSAndy Whitcroft		}
46460c73b4ebSAndy Whitcroft
464731070b5dSJoe Perches# Function pointer declarations
464831070b5dSJoe Perches# check spacing between type, funcptr, and args
464931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
465091f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
465131070b5dSJoe Perches			my $declare = $1;
465231070b5dSJoe Perches			my $pre_pointer_space = $2;
465331070b5dSJoe Perches			my $post_pointer_space = $3;
465431070b5dSJoe Perches			my $funcname = $4;
465531070b5dSJoe Perches			my $post_funcname_space = $5;
465631070b5dSJoe Perches			my $pre_args_space = $6;
465731070b5dSJoe Perches
465891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
465991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
466091f72e9cSJoe Perches# don't need a space so don't warn for those.
466191f72e9cSJoe Perches			my $post_declare_space = "";
466291f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
466391f72e9cSJoe Perches				$post_declare_space = $1;
466491f72e9cSJoe Perches				$declare = rtrim($declare);
466591f72e9cSJoe Perches			}
466691f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
466731070b5dSJoe Perches				WARN("SPACING",
466831070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
466991f72e9cSJoe Perches				$post_declare_space = " ";
467031070b5dSJoe Perches			}
467131070b5dSJoe Perches
467231070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
467391f72e9cSJoe Perches# This test is not currently implemented because these declarations are
467491f72e9cSJoe Perches# equivalent to
467591f72e9cSJoe Perches#	int  foo(int bar, ...)
467691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
467791f72e9cSJoe Perches#
467891f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
467991f72e9cSJoe Perches#				WARN("SPACING",
468091f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
468191f72e9cSJoe Perches#			}
468231070b5dSJoe Perches
468331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
468431070b5dSJoe Perches			if (defined $pre_pointer_space &&
468531070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
468631070b5dSJoe Perches				WARN("SPACING",
468731070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
468831070b5dSJoe Perches			}
468931070b5dSJoe Perches
469031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
469131070b5dSJoe Perches			if (defined $post_pointer_space &&
469231070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
469331070b5dSJoe Perches				WARN("SPACING",
469431070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
469531070b5dSJoe Perches			}
469631070b5dSJoe Perches
469731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
469831070b5dSJoe Perches			if (defined $post_funcname_space &&
469931070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
470031070b5dSJoe Perches				WARN("SPACING",
470131070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
470231070b5dSJoe Perches			}
470331070b5dSJoe Perches
470431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
470531070b5dSJoe Perches			if (defined $pre_args_space &&
470631070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
470731070b5dSJoe Perches				WARN("SPACING",
470831070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
470931070b5dSJoe Perches			}
471031070b5dSJoe Perches
471131070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4712194f66fcSJoe Perches				$fixed[$fixlinenr] =~
471391f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
471431070b5dSJoe Perches			}
471531070b5dSJoe Perches		}
471631070b5dSJoe Perches
47178d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
47188d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4719fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4720fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
47218d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
47228d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
47238d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4724fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
472538dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
47263705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
47273705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
47283705ce5bSJoe Perches				    $fix) {
4729194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
47303705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
47313705ce5bSJoe Perches				}
47328d31cfceSAndy Whitcroft			}
47338d31cfceSAndy Whitcroft		}
47348d31cfceSAndy Whitcroft
4735f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
47366c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4737c2fdda0dSAndy Whitcroft			my $name = $1;
4738773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4739773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4740c2fdda0dSAndy Whitcroft
4741c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4742773647a0SAndy Whitcroft			if ($name =~ /^(?:
4743773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4744773647a0SAndy Whitcroft				volatile|__volatile__|
4745773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4746773647a0SAndy Whitcroft				asm|__asm__)$/x)
4747773647a0SAndy Whitcroft			{
4748c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4749c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4750c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4751c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4752773647a0SAndy Whitcroft
4753773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4754c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4755c2fdda0dSAndy Whitcroft
4756c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4757c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4758773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4759c2fdda0dSAndy Whitcroft
4760c2fdda0dSAndy Whitcroft			} else {
47613705ce5bSJoe Perches				if (WARN("SPACING",
47623705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
47633705ce5bSJoe Perches					     $fix) {
4764194f66fcSJoe Perches					$fixed[$fixlinenr] =~
47653705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
47663705ce5bSJoe Perches				}
4767f0a594c1SAndy Whitcroft			}
47686c72ffaaSAndy Whitcroft		}
47699a4cad4eSEric Nelson
4770653d4876SAndy Whitcroft# Check operator spacing.
47710a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
47723705ce5bSJoe Perches			my $fixed_line = "";
47733705ce5bSJoe Perches			my $line_fixed = 0;
47743705ce5bSJoe Perches
47759c0ca6f9SAndy Whitcroft			my $ops = qr{
47769c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
47779c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
47789c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
47791f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
478084731623SJoe Perches				\?:|\?|:
47819c0ca6f9SAndy Whitcroft			}x;
4782cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
47833705ce5bSJoe Perches
47843705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
47853705ce5bSJoe Perches##			foreach my $el (@elements) {
47863705ce5bSJoe Perches##				print("el: <$el>\n");
47873705ce5bSJoe Perches##			}
47883705ce5bSJoe Perches
47893705ce5bSJoe Perches			my @fix_elements = ();
479000df344fSAndy Whitcroft			my $off = 0;
47916c72ffaaSAndy Whitcroft
47923705ce5bSJoe Perches			foreach my $el (@elements) {
47933705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
47943705ce5bSJoe Perches				$off += length($el);
47953705ce5bSJoe Perches			}
47963705ce5bSJoe Perches
47973705ce5bSJoe Perches			$off = 0;
47983705ce5bSJoe Perches
47996c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4800b34c648bSJoe Perches			my $last_after = -1;
48016c72ffaaSAndy Whitcroft
48020a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
48033705ce5bSJoe Perches
48043705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
48053705ce5bSJoe Perches
48063705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
48073705ce5bSJoe Perches
48084a0df2efSAndy Whitcroft				$off += length($elements[$n]);
48094a0df2efSAndy Whitcroft
481025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4811773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4812773647a0SAndy Whitcroft				my $cc = '';
4813773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4814773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4815773647a0SAndy Whitcroft				}
4816773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4817773647a0SAndy Whitcroft
48184a0df2efSAndy Whitcroft				my $a = '';
48194a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
48204a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4821cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
48224a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
48234a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4824773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
48254a0df2efSAndy Whitcroft
48260a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
48274a0df2efSAndy Whitcroft
48284a0df2efSAndy Whitcroft				my $c = '';
48290a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
48304a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
48314a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4832cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
48334a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
48344a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
48358b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
48364a0df2efSAndy Whitcroft				} else {
48374a0df2efSAndy Whitcroft					$c = 'E';
48380a920b5bSAndy Whitcroft				}
48390a920b5bSAndy Whitcroft
48404a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
48414a0df2efSAndy Whitcroft
48424a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
48434a0df2efSAndy Whitcroft
48446c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4845de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
48460a920b5bSAndy Whitcroft
484774048ed8SAndy Whitcroft				# Pull out the value of this operator.
48486c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
48490a920b5bSAndy Whitcroft
48501f65f947SAndy Whitcroft				# Get the full operator variant.
48511f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
48521f65f947SAndy Whitcroft
485313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
485413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4855d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
485613214adfSAndy Whitcroft
4857cf655043SAndy Whitcroft#				# Ignore comments
4858cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
485913214adfSAndy Whitcroft
4860d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
486113214adfSAndy Whitcroft				} elsif ($op eq ';') {
4862cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4863cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
48643705ce5bSJoe Perches						if (ERROR("SPACING",
48653705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4866b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
48673705ce5bSJoe Perches							$line_fixed = 1;
48683705ce5bSJoe Perches						}
4869d8aaf121SAndy Whitcroft					}
4870d8aaf121SAndy Whitcroft
4871d8aaf121SAndy Whitcroft				# // is a comment
4872d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
48730a920b5bSAndy Whitcroft
4874b00e4814SJoe Perches				#   :   when part of a bitfield
4875b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4876b00e4814SJoe Perches					# skip the bitfield test for now
4877b00e4814SJoe Perches
48781f65f947SAndy Whitcroft				# No spaces for:
48791f65f947SAndy Whitcroft				#   ->
4880b00e4814SJoe Perches				} elsif ($op eq '->') {
48814a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
48823705ce5bSJoe Perches						if (ERROR("SPACING",
48833705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4884b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
48853705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
48863705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
48873705ce5bSJoe Perches							}
4888b34c648bSJoe Perches							$line_fixed = 1;
48893705ce5bSJoe Perches						}
48900a920b5bSAndy Whitcroft					}
48910a920b5bSAndy Whitcroft
48922381097bSJoe Perches				# , must not have a space before and must have a space on the right.
48930a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
48942381097bSJoe Perches					my $rtrim_before = 0;
48952381097bSJoe Perches					my $space_after = 0;
48962381097bSJoe Perches					if ($ctx =~ /Wx./) {
48972381097bSJoe Perches						if (ERROR("SPACING",
48982381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
48992381097bSJoe Perches							$line_fixed = 1;
49002381097bSJoe Perches							$rtrim_before = 1;
49012381097bSJoe Perches						}
49022381097bSJoe Perches					}
4903cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
49043705ce5bSJoe Perches						if (ERROR("SPACING",
49053705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
49063705ce5bSJoe Perches							$line_fixed = 1;
4907b34c648bSJoe Perches							$last_after = $n;
49082381097bSJoe Perches							$space_after = 1;
49092381097bSJoe Perches						}
49102381097bSJoe Perches					}
49112381097bSJoe Perches					if ($rtrim_before || $space_after) {
49122381097bSJoe Perches						if ($rtrim_before) {
49132381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
49142381097bSJoe Perches						} else {
49152381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
49162381097bSJoe Perches						}
49172381097bSJoe Perches						if ($space_after) {
49182381097bSJoe Perches							$good .= " ";
49193705ce5bSJoe Perches						}
49200a920b5bSAndy Whitcroft					}
49210a920b5bSAndy Whitcroft
49229c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
492374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
49249c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
49259c0ca6f9SAndy Whitcroft
49269c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
49279c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
49289c0ca6f9SAndy Whitcroft				# unary operator, or a cast
49299c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
493074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
49310d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4932cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
49333705ce5bSJoe Perches						if (ERROR("SPACING",
49343705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4935b34c648bSJoe Perches							if ($n != $last_after + 2) {
4936b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
49373705ce5bSJoe Perches								$line_fixed = 1;
49383705ce5bSJoe Perches							}
49390a920b5bSAndy Whitcroft						}
4940b34c648bSJoe Perches					}
4941a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4942171ae1a4SAndy Whitcroft						# A unary '*' may be const
4943171ae1a4SAndy Whitcroft
4944171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
49453705ce5bSJoe Perches						if (ERROR("SPACING",
49463705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4947b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
49483705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
49493705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
49503705ce5bSJoe Perches							}
4951b34c648bSJoe Perches							$line_fixed = 1;
49523705ce5bSJoe Perches						}
49530a920b5bSAndy Whitcroft					}
49540a920b5bSAndy Whitcroft
49550a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
49560a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4957773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
49583705ce5bSJoe Perches						if (ERROR("SPACING",
49593705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4960b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
49613705ce5bSJoe Perches							$line_fixed = 1;
49623705ce5bSJoe Perches						}
49630a920b5bSAndy Whitcroft					}
4964773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4965773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
49663705ce5bSJoe Perches						if (ERROR("SPACING",
49673705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4968b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
49693705ce5bSJoe Perches							$line_fixed = 1;
49703705ce5bSJoe Perches						}
4971653d4876SAndy Whitcroft					}
4972773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
49733705ce5bSJoe Perches						if (ERROR("SPACING",
49743705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4975b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
49763705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
49773705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4978773647a0SAndy Whitcroft							}
4979b34c648bSJoe Perches							$line_fixed = 1;
49803705ce5bSJoe Perches						}
49813705ce5bSJoe Perches					}
49820a920b5bSAndy Whitcroft
49830a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
49849c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
49859c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
49869c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4987c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4988c2fdda0dSAndy Whitcroft					 $op eq '%')
49890a920b5bSAndy Whitcroft				{
4990d2e025f3SJoe Perches					if ($check) {
4991d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4992d2e025f3SJoe Perches							if (CHK("SPACING",
4993d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4994d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4995d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4996d2e025f3SJoe Perches								$line_fixed = 1;
4997d2e025f3SJoe Perches							}
4998d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4999d2e025f3SJoe Perches							if (CHK("SPACING",
5000d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
5001d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5002d2e025f3SJoe Perches								$line_fixed = 1;
5003d2e025f3SJoe Perches							}
5004d2e025f3SJoe Perches						}
5005d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
50063705ce5bSJoe Perches						if (ERROR("SPACING",
50073705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
5008b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5009b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5010b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5011b34c648bSJoe Perches							}
50123705ce5bSJoe Perches							$line_fixed = 1;
50133705ce5bSJoe Perches						}
50140a920b5bSAndy Whitcroft					}
50150a920b5bSAndy Whitcroft
50161f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
50171f65f947SAndy Whitcroft				# terminating a case value or a label.
50181f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
50191f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
50203705ce5bSJoe Perches						if (ERROR("SPACING",
50213705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5022b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
50233705ce5bSJoe Perches							$line_fixed = 1;
50243705ce5bSJoe Perches						}
50251f65f947SAndy Whitcroft					}
50261f65f947SAndy Whitcroft
50270a920b5bSAndy Whitcroft				# All the others need spaces both sides.
5028cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
50291f65f947SAndy Whitcroft					my $ok = 0;
50301f65f947SAndy Whitcroft
503122f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
50321f65f947SAndy Whitcroft					if (($op eq '<' &&
50331f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
50341f65f947SAndy Whitcroft					    ($op eq '>' &&
50351f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
50361f65f947SAndy Whitcroft					{
50371f65f947SAndy Whitcroft						$ok = 1;
50381f65f947SAndy Whitcroft					}
50391f65f947SAndy Whitcroft
5040e0df7e1fSJoe Perches					# for asm volatile statements
5041e0df7e1fSJoe Perches					# ignore a colon with another
5042e0df7e1fSJoe Perches					# colon immediately before or after
5043e0df7e1fSJoe Perches					if (($op eq ':') &&
5044e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
5045e0df7e1fSJoe Perches						$ok = 1;
5046e0df7e1fSJoe Perches					}
5047e0df7e1fSJoe Perches
504884731623SJoe Perches					# messages are ERROR, but ?: are CHK
50491f65f947SAndy Whitcroft					if ($ok == 0) {
50500675a8fbSJean Delvare						my $msg_level = \&ERROR;
50510675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
505284731623SJoe Perches
50530675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
50543705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
5055b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5056b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5057b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5058b34c648bSJoe Perches							}
50593705ce5bSJoe Perches							$line_fixed = 1;
50603705ce5bSJoe Perches						}
50610a920b5bSAndy Whitcroft					}
506222f2a2efSAndy Whitcroft				}
50634a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
50643705ce5bSJoe Perches
50653705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
50663705ce5bSJoe Perches
50673705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
50680a920b5bSAndy Whitcroft			}
50693705ce5bSJoe Perches
50703705ce5bSJoe Perches			if (($#elements % 2) == 0) {
50713705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
50723705ce5bSJoe Perches			}
50733705ce5bSJoe Perches
5074194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5075194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
50763705ce5bSJoe Perches			}
50773705ce5bSJoe Perches
50783705ce5bSJoe Perches
50790a920b5bSAndy Whitcroft		}
50800a920b5bSAndy Whitcroft
5081786b6326SJoe Perches# check for whitespace before a non-naked semicolon
5082d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
5083786b6326SJoe Perches			if (WARN("SPACING",
5084786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
5085786b6326SJoe Perches			    $fix) {
5086194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
5087786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
5088786b6326SJoe Perches			}
5089786b6326SJoe Perches		}
5090786b6326SJoe Perches
5091f0a594c1SAndy Whitcroft# check for multiple assignments
5092f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5093000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
5094000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
5095f0a594c1SAndy Whitcroft		}
5096f0a594c1SAndy Whitcroft
509722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
509822f2a2efSAndy Whitcroft## # continuation.
509922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
510022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
510122f2a2efSAndy Whitcroft##
510222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
5103e73d2715SDwaipayan Ray## 			# falsely report the parameters of functions.
510422f2a2efSAndy Whitcroft## 			my $ln = $line;
510522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
510622f2a2efSAndy Whitcroft## 			}
510722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
5108000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
5109000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
511022f2a2efSAndy Whitcroft## 			}
511122f2a2efSAndy Whitcroft## 		}
5112f0a594c1SAndy Whitcroft
51130a920b5bSAndy Whitcroft#need space before brace following if, while, etc
51146b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
51156ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
51163705ce5bSJoe Perches			if (ERROR("SPACING",
51173705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
51183705ce5bSJoe Perches			    $fix) {
51196ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
51203705ce5bSJoe Perches			}
5121de7d4f0eSAndy Whitcroft		}
5122de7d4f0eSAndy Whitcroft
5123c4a62ef9SJoe Perches## # check for blank lines before declarations
5124c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5125c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
5126c4a62ef9SJoe Perches##			WARN("SPACING",
5127c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
5128c4a62ef9SJoe Perches##		}
5129c4a62ef9SJoe Perches##
5130c4a62ef9SJoe Perches
5131de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
5132de7d4f0eSAndy Whitcroft# on the line
513394fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5134d5e616fcSJoe Perches			if (ERROR("SPACING",
5135d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
5136d5e616fcSJoe Perches			    $fix) {
5137194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5138d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
5139d5e616fcSJoe Perches			}
51400a920b5bSAndy Whitcroft		}
51410a920b5bSAndy Whitcroft
514222f2a2efSAndy Whitcroft# check spacing on square brackets
514322f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
51443705ce5bSJoe Perches			if (ERROR("SPACING",
51453705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
51463705ce5bSJoe Perches			    $fix) {
5147194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51483705ce5bSJoe Perches				    s/\[\s+/\[/;
51493705ce5bSJoe Perches			}
515022f2a2efSAndy Whitcroft		}
515122f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
51523705ce5bSJoe Perches			if (ERROR("SPACING",
51533705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
51543705ce5bSJoe Perches			    $fix) {
5155194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51563705ce5bSJoe Perches				    s/\s+\]/\]/;
51573705ce5bSJoe Perches			}
515822f2a2efSAndy Whitcroft		}
515922f2a2efSAndy Whitcroft
5160c45dcabdSAndy Whitcroft# check spacing on parentheses
51619c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
51629c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
51633705ce5bSJoe Perches			if (ERROR("SPACING",
51643705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
51653705ce5bSJoe Perches			    $fix) {
5166194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51673705ce5bSJoe Perches				    s/\(\s+/\(/;
51683705ce5bSJoe Perches			}
516922f2a2efSAndy Whitcroft		}
517013214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5171c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
5172c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
51733705ce5bSJoe Perches			if (ERROR("SPACING",
51743705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
51753705ce5bSJoe Perches			    $fix) {
5176194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51773705ce5bSJoe Perches				    s/\s+\)/\)/;
51783705ce5bSJoe Perches			}
517922f2a2efSAndy Whitcroft		}
518022f2a2efSAndy Whitcroft
5181e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
5182e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5183e2826fd0SJoe Perches
5184e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5185ea4acbb1SJoe Perches			my $var = $1;
5186ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5187ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
5188ea4acbb1SJoe Perches			    $fix) {
5189ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5190ea4acbb1SJoe Perches			}
5191ea4acbb1SJoe Perches		}
5192ea4acbb1SJoe Perches
5193ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
5194ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
5195ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
5196ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5197ea4acbb1SJoe Perches			my $var = $2;
5198ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5199ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5200ea4acbb1SJoe Perches			    $fix) {
5201ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
5202ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
5203ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5204ea4acbb1SJoe Perches			}
5205e2826fd0SJoe Perches		}
5206e2826fd0SJoe Perches
520763b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
5208a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
5209a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
52105b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
521163b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
521263b7c73eSJoe Perches			my $if_stat = $1;
521363b7c73eSJoe Perches			my $test = substr($2, 1, -1);
521463b7c73eSJoe Perches			my $herectx;
521563b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
521663b7c73eSJoe Perches				my $match = $1;
521763b7c73eSJoe Perches				# avoid parentheses around potential macro args
521863b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
521963b7c73eSJoe Perches				if (!defined($herectx)) {
522063b7c73eSJoe Perches					$herectx = $here . "\n";
522163b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
522263b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
522363b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
522463b7c73eSJoe Perches						$herectx .=  $rl . "\n";
522563b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
522663b7c73eSJoe Perches					}
522763b7c73eSJoe Perches				}
522863b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
522963b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
523063b7c73eSJoe Perches			}
523163b7c73eSJoe Perches		}
523263b7c73eSJoe Perches
52330a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
52344a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
52350a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
52363705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
52373705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
52383705ce5bSJoe Perches			    $fix) {
5239194f66fcSJoe Perches				$fixed[$fixlinenr] =~
52403705ce5bSJoe Perches				    s/^(.)\s+/$1/;
52413705ce5bSJoe Perches			}
52420a920b5bSAndy Whitcroft		}
52430a920b5bSAndy Whitcroft
524440873abaSJoe Perches# check if a statement with a comma should be two statements like:
524540873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
524640873abaSJoe Perches#	bar = baz();
524740873abaSJoe Perches		if (defined($stat) &&
524840873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
524940873abaSJoe Perches			my $cnt = statement_rawlines($stat);
525040873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
525140873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
525240873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
525340873abaSJoe Perches		}
525440873abaSJoe Perches
52555b9553abSJoe Perches# return is not a function
5256507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5257c45dcabdSAndy Whitcroft			my $spacing = $1;
52585b57980dSJoe Perches			if ($perl_version_ok &&
52595b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
52605b9553abSJoe Perches				my $value = $1;
52615b9553abSJoe Perches				$value = deparenthesize($value);
52625b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5263000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5264000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
52655b9553abSJoe Perches				}
5266c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5267000d1cc1SJoe Perches				ERROR("SPACING",
5268000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5269c45dcabdSAndy Whitcroft			}
5270c45dcabdSAndy Whitcroft		}
5271507e5141SJoe Perches
5272b43ae21bSJoe Perches# unnecessary return in a void function
5273b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5274b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5275b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5276b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5277b43ae21bSJoe Perches		    $linenr >= 3 &&
5278b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5279b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
52809819cf25SJoe Perches			WARN("RETURN_VOID",
5281b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
52829819cf25SJoe Perches               }
52839819cf25SJoe Perches
5284189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
52855b57980dSJoe Perches		if ($perl_version_ok &&
5286189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5287189248d8SJoe Perches			my $openparens = $1;
5288189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5289189248d8SJoe Perches			my $msg = "";
5290189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5291189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5292189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5293189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5294189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5295189248d8SJoe Perches			}
5296189248d8SJoe Perches		}
5297189248d8SJoe Perches
5298c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5299c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5300c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5301c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
53025b57980dSJoe Perches		if ($perl_version_ok &&
5303c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5304c5595fa2SJoe Perches			my $lead = $1;
5305c5595fa2SJoe Perches			my $const = $2;
5306c5595fa2SJoe Perches			my $comp = $3;
5307c5595fa2SJoe Perches			my $to = $4;
5308c5595fa2SJoe Perches			my $newcomp = $comp;
5309f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5310c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5311c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5312c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5313c5595fa2SJoe Perches			    $fix) {
5314c5595fa2SJoe Perches				if ($comp eq "<") {
5315c5595fa2SJoe Perches					$newcomp = ">";
5316c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5317c5595fa2SJoe Perches					$newcomp = ">=";
5318c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5319c5595fa2SJoe Perches					$newcomp = "<";
5320c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5321c5595fa2SJoe Perches					$newcomp = "<=";
5322c5595fa2SJoe Perches				}
5323c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5324c5595fa2SJoe Perches			}
5325c5595fa2SJoe Perches		}
5326c5595fa2SJoe Perches
5327f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5328f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
532953a3c448SAndy Whitcroft			my $name = $1;
533053a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
5331000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5332f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
533353a3c448SAndy Whitcroft			}
533453a3c448SAndy Whitcroft		}
5335c45dcabdSAndy Whitcroft
53360a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
53374a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
53383705ce5bSJoe Perches			if (ERROR("SPACING",
53393705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
53403705ce5bSJoe Perches			    $fix) {
5341194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53423705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
53433705ce5bSJoe Perches			}
53440a920b5bSAndy Whitcroft		}
53450a920b5bSAndy Whitcroft
5346f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5347f5fe35ddSAndy Whitcroft# statements after the conditional.
5348170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
53493e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
53503e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
53513e469cdcSAndy Whitcroft					if (!defined $stat);
5352170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5353170d3a22SAndy Whitcroft						$remain_next, $off_next);
5354170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5355170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5356170d3a22SAndy Whitcroft
5357170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5358170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5359170d3a22SAndy Whitcroft				# then count those as offsets.
5360170d3a22SAndy Whitcroft				my ($whitespace) =
5361170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5362170d3a22SAndy Whitcroft				my $offset =
5363170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5364170d3a22SAndy Whitcroft
5365170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5366170d3a22SAndy Whitcroft								$offset} = 1;
5367170d3a22SAndy Whitcroft			}
5368170d3a22SAndy Whitcroft		}
5369170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5370c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5371170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5372171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
53738905a67cSAndy Whitcroft
5374b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
537565b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
537665b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
537765b64b3bSJoe Perches				    $fix && $perl_version_ok) {
537865b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
537965b64b3bSJoe Perches						my $space = $1;
538065b64b3bSJoe Perches						my $not = $2;
538165b64b3bSJoe Perches						my $statement = $3;
538265b64b3bSJoe Perches						my $assigned = $4;
538365b64b3bSJoe Perches						my $test = $8;
538465b64b3bSJoe Perches						my $against = $9;
538565b64b3bSJoe Perches						my $brace = $15;
538665b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
538765b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
538865b64b3bSJoe Perches						my $newline = "${space}if (";
538965b64b3bSJoe Perches						$newline .= '!' if defined($not);
539065b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
539165b64b3bSJoe Perches						$newline .= "$assigned";
539265b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
539365b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
539465b64b3bSJoe Perches						$newline .= ')';
539565b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
539665b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
539765b64b3bSJoe Perches					}
539865b64b3bSJoe Perches				}
53998905a67cSAndy Whitcroft			}
54008905a67cSAndy Whitcroft
54018905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
54028905a67cSAndy Whitcroft			# conditional.
5403773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
54048905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
540513214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
540653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
540753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5408773647a0SAndy Whitcroft			{
5409bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5410bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5411bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
541242bdf74cSHidetoshi Seto				my $stat_real = '';
5413bb44ad39SAndy Whitcroft
541442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
541542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5416bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5417bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5418bb44ad39SAndy Whitcroft				}
5419bb44ad39SAndy Whitcroft
5420000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5421000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
54228905a67cSAndy Whitcroft			}
54238905a67cSAndy Whitcroft		}
54248905a67cSAndy Whitcroft
542513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
542613214adfSAndy Whitcroft		if ($line =~ /
542713214adfSAndy Whitcroft			(?:
542813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
542913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
543013214adfSAndy Whitcroft				(?:\&\&|\|\|)
543113214adfSAndy Whitcroft			|
543213214adfSAndy Whitcroft				(?:\&\&|\|\|)
543313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
543413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
543513214adfSAndy Whitcroft			)/x)
543613214adfSAndy Whitcroft		{
5437000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5438000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
543913214adfSAndy Whitcroft		}
544013214adfSAndy Whitcroft
54418905a67cSAndy Whitcroft# if and else should not have general statements after it
544213214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
544313214adfSAndy Whitcroft			my $s = $1;
544413214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
544513214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5446000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5447000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
54480a920b5bSAndy Whitcroft			}
544913214adfSAndy Whitcroft		}
545039667782SAndy Whitcroft# if should not continue a brace
545139667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5452000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5453048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
545439667782SAndy Whitcroft				$herecurr);
545539667782SAndy Whitcroft		}
5456a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5457a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5458a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
54593fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5460a1080bf8SAndy Whitcroft			\s*return\s+
5461a1080bf8SAndy Whitcroft		    )/xg)
5462a1080bf8SAndy Whitcroft		{
5463000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5464000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5465a1080bf8SAndy Whitcroft		}
54660a920b5bSAndy Whitcroft
54670a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
54680a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
54698b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
54700a920b5bSAndy Whitcroft		    $previndent == $indent) {
54718b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
54728b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
54738b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
54748b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
54758b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
54768b8856f4SJoe Perches				my $fixedline = $prevrawline;
54778b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
54788b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
54798b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54808b8856f4SJoe Perches				}
54818b8856f4SJoe Perches				$fixedline = $rawline;
54828b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
54838b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
54848b8856f4SJoe Perches			}
54850a920b5bSAndy Whitcroft		}
54860a920b5bSAndy Whitcroft
54878b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5488c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5489c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5490c2fdda0dSAndy Whitcroft
5491c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5492c2fdda0dSAndy Whitcroft			# conditional.
5493773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5494c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5495c2fdda0dSAndy Whitcroft
5496c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
54978b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
54988b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
54998b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
55008b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
55018b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
55028b8856f4SJoe Perches					my $fixedline = $prevrawline;
55038b8856f4SJoe Perches					my $trailing = $rawline;
55048b8856f4SJoe Perches					$trailing =~ s/^\+//;
55058b8856f4SJoe Perches					$trailing = trim($trailing);
55068b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
55078b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
55088b8856f4SJoe Perches				}
5509c2fdda0dSAndy Whitcroft			}
5510c2fdda0dSAndy Whitcroft		}
5511c2fdda0dSAndy Whitcroft
551295e2c602SJoe Perches#Specific variable tests
5513323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5514323c1260SJoe Perches			my $var = $1;
551595e2c602SJoe Perches
551695e2c602SJoe Perches#CamelCase
5517807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5518be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
55194104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values
55204104a206SŁukasz Stelmach			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
552122735ce8SJoe Perches#Ignore Page<foo> variants
5522807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5523d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5524d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5525d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5526f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5527f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
55287e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
55297e781f67SJoe Perches					my $word = $1;
55307e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5531d8b07710SJoe Perches					if ($check) {
5532d8b07710SJoe Perches						seed_camelcase_includes();
5533d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5534d8b07710SJoe Perches							seed_camelcase_file($realfile);
5535d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5536d8b07710SJoe Perches						}
5537d8b07710SJoe Perches					}
55387e781f67SJoe Perches					if (!defined $camelcase{$word}) {
55397e781f67SJoe Perches						$camelcase{$word} = 1;
5540be79794bSJoe Perches						CHK("CAMELCASE",
55417e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
55427e781f67SJoe Perches					}
5543323c1260SJoe Perches				}
5544323c1260SJoe Perches			}
55453445686aSJoe Perches		}
55460a920b5bSAndy Whitcroft
55470a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5548d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5549d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5550d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5551d5e616fcSJoe Perches			    $fix) {
5552194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5553d5e616fcSJoe Perches			}
55540a920b5bSAndy Whitcroft		}
55550a920b5bSAndy Whitcroft
55560e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
55570e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5558c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5559e09dec48SAndy Whitcroft			my $file = "$1.h";
5560e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5561e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5562e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
55637840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5564c45dcabdSAndy Whitcroft			{
55650e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
55660e212e0aSFabian Frederick				if ($asminclude > 0) {
5567e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5568000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5569000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5570e09dec48SAndy Whitcroft					} else {
5571000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5572000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5573e09dec48SAndy Whitcroft					}
55740a920b5bSAndy Whitcroft				}
55750a920b5bSAndy Whitcroft			}
55760e212e0aSFabian Frederick		}
55770a920b5bSAndy Whitcroft
5578653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5579653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5580cf655043SAndy Whitcroft# in a known good container
5581b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5582b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5583d8aaf121SAndy Whitcroft			my $ln = $linenr;
5584d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5585c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5586c45dcabdSAndy Whitcroft			my $ctx = '';
558708a2843eSJoe Perches			my $has_flow_statement = 0;
558808a2843eSJoe Perches			my $has_arg_concat = 0;
5589c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5590f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5591f74bd194SAndy Whitcroft			$ctx = $dstat;
5592c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5593a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5594c45dcabdSAndy Whitcroft
559508a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
559662e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
559708a2843eSJoe Perches
5598f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5599f59b64bfSJoe Perches			my $define_args = $1;
5600f59b64bfSJoe Perches			my $define_stmt = $dstat;
5601f59b64bfSJoe Perches			my @def_args = ();
5602f59b64bfSJoe Perches
5603f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5604f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5605f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
56068c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5607f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5608f59b64bfSJoe Perches			}
5609f59b64bfSJoe Perches
5610292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5611c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5612c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5613c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5614c45dcabdSAndy Whitcroft
5615c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
56162e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
56172e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
56182e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5619bf30d6edSAndy Whitcroft			{
5620c45dcabdSAndy Whitcroft			}
5621c45dcabdSAndy Whitcroft
5622342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
562333acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
562433acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5625e45bab8eSAndy Whitcroft			{
5626e45bab8eSAndy Whitcroft			}
5627e45bab8eSAndy Whitcroft
562842e15293SJoe Perches			# Make asm volatile uses seem like a generic function
562942e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
563042e15293SJoe Perches
5631c45dcabdSAndy Whitcroft			my $exceptions = qr{
5632c45dcabdSAndy Whitcroft				$Declare|
5633c45dcabdSAndy Whitcroft				module_param_named|
5634a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5635c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5636c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5637383099fdSAndy Whitcroft				__typeof__\(|
563822fd2d3eSStefani Seibold				union|
563922fd2d3eSStefani Seibold				struct|
5640ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
56416b10df42SVladimir Zapolskiy				^\"|\"$|
56426b10df42SVladimir Zapolskiy				^\[
5643c45dcabdSAndy Whitcroft			}x;
56445eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5645f59b64bfSJoe Perches
5646f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5647f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5648e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5649f59b64bfSJoe Perches
5650f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5651f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5652f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
56533cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5654356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5655f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5656f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5657e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
565872f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
56592e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5660f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5661f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5662f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
56634e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5664f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5665c45dcabdSAndy Whitcroft			{
5666e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5667e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5668e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5669e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5670f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5671f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5672f74bd194SAndy Whitcroft				} else {
5673000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5674388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5675d8aaf121SAndy Whitcroft				}
5676f59b64bfSJoe Perches
5677f59b64bfSJoe Perches			}
56785207649bSJoe Perches
56795207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
56805207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
56815207649bSJoe Perches			my $first = 1;
56825207649bSJoe Perches			$define_stmt = "";
56835207649bSJoe Perches			foreach my $l (@stmt_array) {
56845207649bSJoe Perches				$l =~ s/\\$//;
56855207649bSJoe Perches				if ($first) {
56865207649bSJoe Perches					$define_stmt = $l;
56875207649bSJoe Perches					$first = 0;
56885207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
56895207649bSJoe Perches					$define_stmt .= substr($l, 1);
56905207649bSJoe Perches				}
56915207649bSJoe Perches			}
56925207649bSJoe Perches			$define_stmt =~ s/$;//g;
56935207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
56945207649bSJoe Perches			$define_stmt = trim($define_stmt);
56955207649bSJoe Perches
5696f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5697f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5698f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
56999192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
57007fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
57016dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
57027fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
57037fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5704d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5705f59b64bfSJoe Perches				if ($use_cnt > 1) {
5706f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5707f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5708f59b64bfSJoe Perches				    }
57099192d41aSJoe Perches# check if any macro arguments may have other precedence issues
57107fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
57119192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
57129192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
57139192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
57149192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
57159192d41aSJoe Perches				}
57160a920b5bSAndy Whitcroft			}
57175023d347SJoe Perches
571808a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
571908a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
572008a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
572108a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5722e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
572308a2843eSJoe Perches
572408a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
572508a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
572608a2843eSJoe Perches			}
572708a2843eSJoe Perches
5728481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
57295023d347SJoe Perches
57305023d347SJoe Perches		} else {
57315023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5732481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5733481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
57345023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
57355023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
57365023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
57375023d347SJoe Perches			}
5738653d4876SAndy Whitcroft		}
57390a920b5bSAndy Whitcroft
5740b13edf7fSJoe Perches# do {} while (0) macro tests:
5741b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5742b13edf7fSJoe Perches# macro should not end with a semicolon
57435b57980dSJoe Perches		if ($perl_version_ok &&
5744b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5745b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5746b13edf7fSJoe Perches			my $ln = $linenr;
5747b13edf7fSJoe Perches			my $cnt = $realcnt;
5748b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5749b13edf7fSJoe Perches			my $ctx = '';
5750b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5751b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5752b13edf7fSJoe Perches			$ctx = $dstat;
5753b13edf7fSJoe Perches
5754b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
57551b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5756b13edf7fSJoe Perches
5757b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5758b13edf7fSJoe Perches				my $stmts = $2;
5759b13edf7fSJoe Perches				my $semis = $3;
5760b13edf7fSJoe Perches
5761b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5762b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5763e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5764b13edf7fSJoe Perches
5765ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5766ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5767b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5768b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5769b13edf7fSJoe Perches				}
5770b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5771b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5772b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5773b13edf7fSJoe Perches				}
5774f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5775f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5776f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5777e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5778f5ef95b1SJoe Perches
5779f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5780f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5781b13edf7fSJoe Perches			}
5782b13edf7fSJoe Perches		}
5783b13edf7fSJoe Perches
5784f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
578513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
578613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5787cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
578813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5789cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5790cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5791aad4f614SJoe Perches				my @allowed = ();
5792aad4f614SJoe Perches				my $allow = 0;
579313214adfSAndy Whitcroft				my $seen = 0;
5794773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5795cf655043SAndy Whitcroft				my $ln = $linenr - 1;
579613214adfSAndy Whitcroft				for my $chunk (@chunks) {
579713214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
579813214adfSAndy Whitcroft
5799773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5800773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5801773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5802773647a0SAndy Whitcroft
5803aad4f614SJoe Perches					$allowed[$allow] = 0;
5804773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5805773647a0SAndy Whitcroft
5806773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5807773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5808773647a0SAndy Whitcroft
5809773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5810cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5811cf655043SAndy Whitcroft
5812773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
581313214adfSAndy Whitcroft
581413214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
581513214adfSAndy Whitcroft
5816aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5817cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5818cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5819aad4f614SJoe Perches						$allowed[$allow] = 1;
582013214adfSAndy Whitcroft					}
582113214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5822cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5823aad4f614SJoe Perches						$allowed[$allow] = 1;
582413214adfSAndy Whitcroft					}
5825cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5826cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5827aad4f614SJoe Perches						$allowed[$allow] = 1;
582813214adfSAndy Whitcroft					}
5829aad4f614SJoe Perches					$allow++;
583013214adfSAndy Whitcroft				}
5831aad4f614SJoe Perches				if ($seen) {
5832aad4f614SJoe Perches					my $sum_allowed = 0;
5833aad4f614SJoe Perches					foreach (@allowed) {
5834aad4f614SJoe Perches						$sum_allowed += $_;
5835aad4f614SJoe Perches					}
5836aad4f614SJoe Perches					if ($sum_allowed == 0) {
5837000d1cc1SJoe Perches						WARN("BRACES",
5838000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5839aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5840aad4f614SJoe Perches						 $seen != $allow) {
5841aad4f614SJoe Perches						CHK("BRACES",
5842aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5843aad4f614SJoe Perches					}
584413214adfSAndy Whitcroft				}
584513214adfSAndy Whitcroft			}
584613214adfSAndy Whitcroft		}
5847773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
584813214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5849cf655043SAndy Whitcroft			my $allowed = 0;
5850f0a594c1SAndy Whitcroft
5851cf655043SAndy Whitcroft			# Check the pre-context.
5852cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5853cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5854cf655043SAndy Whitcroft				$allowed = 1;
5855f0a594c1SAndy Whitcroft			}
5856773647a0SAndy Whitcroft
5857773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5858773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5859773647a0SAndy Whitcroft
5860cf655043SAndy Whitcroft			# Check the condition.
5861cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5862773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5863cf655043SAndy Whitcroft			if (defined $cond) {
5864773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5865cf655043SAndy Whitcroft			}
5866cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5867cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5868cf655043SAndy Whitcroft				$allowed = 1;
5869cf655043SAndy Whitcroft			}
5870cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5871cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5872cf655043SAndy Whitcroft				$allowed = 1;
5873cf655043SAndy Whitcroft			}
5874cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5875cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5876cf655043SAndy Whitcroft				$allowed = 1;
5877cf655043SAndy Whitcroft			}
5878cf655043SAndy Whitcroft			# Check the post-context.
5879cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5880cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5881cf655043SAndy Whitcroft				if (defined $cond) {
5882773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5883cf655043SAndy Whitcroft				}
5884cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5885cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5886cf655043SAndy Whitcroft					$allowed = 1;
5887cf655043SAndy Whitcroft				}
5888cf655043SAndy Whitcroft			}
5889cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5890f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5891e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5892cf655043SAndy Whitcroft
5893000d1cc1SJoe Perches				WARN("BRACES",
5894000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5895f0a594c1SAndy Whitcroft			}
5896f0a594c1SAndy Whitcroft		}
5897f0a594c1SAndy Whitcroft
5898e4c5babdSJoe Perches# check for single line unbalanced braces
589995330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
590095330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5901e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5902e4c5babdSJoe Perches		}
5903e4c5babdSJoe Perches
59040979ae66SJoe Perches# check for unnecessary blank lines around braces
590577b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5906f8e58219SJoe Perches			if (CHK("BRACES",
5907f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5908f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5909f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5910f8e58219SJoe Perches			}
59110979ae66SJoe Perches		}
591277b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5913f8e58219SJoe Perches			if (CHK("BRACES",
5914f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5915f8e58219SJoe Perches			    $fix) {
5916f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5917f8e58219SJoe Perches			}
59180979ae66SJoe Perches		}
59190979ae66SJoe Perches
59204a0df2efSAndy Whitcroft# no volatiles please
59216c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
59226c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5923000d1cc1SJoe Perches			WARN("VOLATILE",
59248c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
59254a0df2efSAndy Whitcroft		}
59264a0df2efSAndy Whitcroft
59275e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
59285e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
59295e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
59305e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
593133acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
59325e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
59335e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
59345e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
59355e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
59365e4f6ba5SJoe Perches				     $fix &&
59375e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
59385e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
59395e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
59405e4f6ba5SJoe Perches				my $comma_close = "";
59415e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
59425e4f6ba5SJoe Perches					$comma_close = $1;
59435e4f6ba5SJoe Perches				}
59445e4f6ba5SJoe Perches
59455e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
59465e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
59475e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
59485e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
59495e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
59505e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
59515e4f6ba5SJoe Perches				$fixedline = $rawline;
59525e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
59535e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
59545e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
59555e4f6ba5SJoe Perches				}
59565e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
59575e4f6ba5SJoe Perches			}
59585e4f6ba5SJoe Perches		}
59595e4f6ba5SJoe Perches
59605e4f6ba5SJoe Perches# check for missing a space in a string concatenation
59615e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
59625e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
59635e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
59645e4f6ba5SJoe Perches		}
59655e4f6ba5SJoe Perches
596677cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5967e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5968e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5969e4b7d309SJoe Perches# function declarations
597077cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
597177cb8546SJoe Perches		    defined($context_function) &&
5972e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5973e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
597477cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5975e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
597677cb8546SJoe Perches		}
597777cb8546SJoe Perches
59785e4f6ba5SJoe Perches# check for spaces before a quoted newline
59795e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
59805e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
59815e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
59825e4f6ba5SJoe Perches			    $fix) {
59835e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
59845e4f6ba5SJoe Perches			}
59855e4f6ba5SJoe Perches
59865e4f6ba5SJoe Perches		}
59875e4f6ba5SJoe Perches
5988f17dba4fSJoe Perches# concatenated string without spaces between elements
598979682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
599079682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
599179682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
599279682c0cSJoe Perches			    $fix) {
599379682c0cSJoe Perches				while ($line =~ /($String)/g) {
599479682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
599579682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
599679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
599779682c0cSJoe Perches				}
599879682c0cSJoe Perches			}
5999f17dba4fSJoe Perches		}
6000f17dba4fSJoe Perches
600190ad30e5SJoe Perches# uncoalesced string fragments
600233acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
600379682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
600479682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
600579682c0cSJoe Perches			    $fix) {
600679682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
600779682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
600879682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
600979682c0cSJoe Perches				}
601079682c0cSJoe Perches			}
601190ad30e5SJoe Perches		}
601290ad30e5SJoe Perches
6013522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
6014522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
6015522b837cSAlexey Dobriyan		my $show_Z = 1;
60165e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6017522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
60185e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
6019522b837cSAlexey Dobriyan			# check for %L
6020522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
60215e4f6ba5SJoe Perches				WARN("PRINTF_L",
6022522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6023522b837cSAlexey Dobriyan				$show_L = 0;
60245e4f6ba5SJoe Perches			}
6025522b837cSAlexey Dobriyan			# check for %Z
6026522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6027522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
6028522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6029522b837cSAlexey Dobriyan				$show_Z = 0;
6030522b837cSAlexey Dobriyan			}
6031522b837cSAlexey Dobriyan			# check for 0x<decimal>
6032522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6033522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
60346e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
60356e300757SJoe Perches			}
60365e4f6ba5SJoe Perches		}
60375e4f6ba5SJoe Perches
60385e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
60393f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
60405e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
60415e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
60425e4f6ba5SJoe Perches		}
60435e4f6ba5SJoe Perches
604400df344fSAndy Whitcroft# warn about #if 0
6045c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
604660f89010SPrakruthi Deepak Heragu			WARN("IF_0",
604760f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
604860f89010SPrakruthi Deepak Heragu		}
604960f89010SPrakruthi Deepak Heragu
605060f89010SPrakruthi Deepak Heragu# warn about #if 1
605160f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
605260f89010SPrakruthi Deepak Heragu			WARN("IF_1",
605360f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
60544a0df2efSAndy Whitcroft		}
60554a0df2efSAndy Whitcroft
605603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
605703df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6058100425deSJoe Perches			my $tested = quotemeta($1);
6059100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6060100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6061100425deSJoe Perches				my $func = $1;
6062100425deSJoe Perches				if (WARN('NEEDLESS_IF',
6063100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6064100425deSJoe Perches				    $fix) {
6065100425deSJoe Perches					my $do_fix = 1;
6066100425deSJoe Perches					my $leading_tabs = "";
6067100425deSJoe Perches					my $new_leading_tabs = "";
6068100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6069100425deSJoe Perches						$leading_tabs = $1;
6070100425deSJoe Perches					} else {
6071100425deSJoe Perches						$do_fix = 0;
6072100425deSJoe Perches					}
6073100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6074100425deSJoe Perches						$new_leading_tabs = $1;
6075100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6076100425deSJoe Perches							$do_fix = 0;
6077100425deSJoe Perches						}
6078100425deSJoe Perches					} else {
6079100425deSJoe Perches						$do_fix = 0;
6080100425deSJoe Perches					}
6081100425deSJoe Perches					if ($do_fix) {
6082100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
6083100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6084100425deSJoe Perches					}
6085100425deSJoe Perches				}
60864c432a8fSGreg Kroah-Hartman			}
60874c432a8fSGreg Kroah-Hartman		}
6088f0a594c1SAndy Whitcroft
6089ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
6090ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6091ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6092ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
6093ebfdc409SJoe Perches		    $linenr > 3) {
6094ebfdc409SJoe Perches			my $testval = $2;
6095ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
6096ebfdc409SJoe Perches
6097ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6098ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6099ebfdc409SJoe Perches
6100e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6101e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
6102ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
6103ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6104ebfdc409SJoe Perches			}
6105ebfdc409SJoe Perches		}
6106ebfdc409SJoe Perches
6107f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
6108dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6109f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6110f78d98f6SJoe Perches			my $level = $1;
6111f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
6112f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
6113f78d98f6SJoe Perches			    $fix) {
6114f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6115f78d98f6SJoe Perches			}
6116f78d98f6SJoe Perches		}
6117f78d98f6SJoe Perches
611845c55e92SJoe Perches# check for logging continuations
611945c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
612045c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
612145c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
612245c55e92SJoe Perches		}
612345c55e92SJoe Perches
612470eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
612570eb2275SDwaipayan Ray		if (defined $stat &&
612670eb2275SDwaipayan Ray		    $line =~ /\b$logFunctions\s*\(/ &&
612770eb2275SDwaipayan Ray		    index($stat, '"') >= 0) {
612870eb2275SDwaipayan Ray			my $lc = $stat =~ tr@\n@@;
612970eb2275SDwaipayan Ray			$lc = $lc + $linenr;
613070eb2275SDwaipayan Ray			my $stat_real = get_stat_real($linenr, $lc);
613170eb2275SDwaipayan Ray			pos($stat_real) = index($stat_real, '"');
613270eb2275SDwaipayan Ray			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
613370eb2275SDwaipayan Ray				my $pspec = $1;
613470eb2275SDwaipayan Ray				my $h = $2;
613570eb2275SDwaipayan Ray				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
613670eb2275SDwaipayan Ray				if (WARN("UNNECESSARY_MODIFIER",
613770eb2275SDwaipayan Ray					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
613870eb2275SDwaipayan Ray				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
613970eb2275SDwaipayan Ray					my $nspec = $pspec;
614070eb2275SDwaipayan Ray					$nspec =~ s/h//g;
614170eb2275SDwaipayan Ray					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
614270eb2275SDwaipayan Ray				}
614370eb2275SDwaipayan Ray			}
614470eb2275SDwaipayan Ray		}
614570eb2275SDwaipayan Ray
6146abb08a53SJoe Perches# check for mask then right shift without a parentheses
61475b57980dSJoe Perches		if ($perl_version_ok &&
6148abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6149abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6150abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
6151abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6152abb08a53SJoe Perches		}
6153abb08a53SJoe Perches
6154b75ac618SJoe Perches# check for pointer comparisons to NULL
61555b57980dSJoe Perches		if ($perl_version_ok) {
6156b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6157b75ac618SJoe Perches				my $val = $1;
6158b75ac618SJoe Perches				my $equal = "!";
6159b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
6160b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
6161b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6162b75ac618SJoe Perches					    $fix) {
6163b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6164b75ac618SJoe Perches				}
6165b75ac618SJoe Perches			}
6166b75ac618SJoe Perches		}
6167b75ac618SJoe Perches
61688716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
61698716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
61708716de38SJoe Perches			my $attr = $1;
61718716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
61728716de38SJoe Perches				my $ptr = $1;
61738716de38SJoe Perches				my $var = $2;
61748716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
61758716de38SJoe Perches				      ERROR("MISPLACED_INIT",
61768716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
61778716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
61788716de38SJoe Perches				      WARN("MISPLACED_INIT",
61798716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
61808716de38SJoe Perches				    $fix) {
6181194f66fcSJoe 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;
61828716de38SJoe Perches				}
61838716de38SJoe Perches			}
61848716de38SJoe Perches		}
61858716de38SJoe Perches
6186e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
6187e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6188e970b884SJoe Perches			my $attr = $1;
6189e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
6190e970b884SJoe Perches			my $attr_prefix = $1;
6191e970b884SJoe Perches			my $attr_type = $2;
6192e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6193e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6194e970b884SJoe Perches			    $fix) {
6195194f66fcSJoe Perches				$fixed[$fixlinenr] =~
6196e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
6197e970b884SJoe Perches			}
6198e970b884SJoe Perches		}
6199e970b884SJoe Perches
6200e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
6201e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6202e970b884SJoe Perches			my $attr = $1;
6203e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6204e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6205e970b884SJoe Perches			    $fix) {
6206194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
6207e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
6208e970b884SJoe Perches				$lead = rtrim($1);
6209e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
6210e970b884SJoe Perches				$lead = "${lead}const ";
6211194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6212e970b884SJoe Perches			}
6213e970b884SJoe Perches		}
6214e970b884SJoe Perches
6215c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
6216c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
6217c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6218c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
6219c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6220c17893c7SJoe Perches			    $fix) {
6221c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6222c17893c7SJoe Perches			}
6223c17893c7SJoe Perches		}
6224c17893c7SJoe Perches
6225fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
6226fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
6227fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6228fbdb8138SJoe Perches			my $constant_func = $1;
6229fbdb8138SJoe Perches			my $func = $constant_func;
6230fbdb8138SJoe Perches			$func =~ s/^__constant_//;
6231fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
6232fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
6233fbdb8138SJoe Perches			    $fix) {
6234194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6235fbdb8138SJoe Perches			}
6236fbdb8138SJoe Perches		}
6237fbdb8138SJoe Perches
62381a15a250SPatrick Pannuto# prefer usleep_range over udelay
623937581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
624043c1d77cSJoe Perches			my $delay = $1;
62411a15a250SPatrick Pannuto			# ignore udelay's < 10, however
624243c1d77cSJoe Perches			if (! ($delay < 10) ) {
6243000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
6244458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
624543c1d77cSJoe Perches			}
624643c1d77cSJoe Perches			if ($delay > 2000) {
624743c1d77cSJoe Perches				WARN("LONG_UDELAY",
624843c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
62491a15a250SPatrick Pannuto			}
62501a15a250SPatrick Pannuto		}
62511a15a250SPatrick Pannuto
625209ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
625309ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
625409ef8725SPatrick Pannuto			if ($1 < 20) {
6255000d1cc1SJoe Perches				WARN("MSLEEP",
6256458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
625709ef8725SPatrick Pannuto			}
625809ef8725SPatrick Pannuto		}
625909ef8725SPatrick Pannuto
626036ec1939SJoe Perches# check for comparisons of jiffies
626136ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
626236ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
626336ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
626436ec1939SJoe Perches		}
626536ec1939SJoe Perches
62669d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
62679d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
62689d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
62699d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
62709d7a34a5SJoe Perches		}
62719d7a34a5SJoe Perches
627200df344fSAndy Whitcroft# warn about #ifdefs in C files
6273c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
627400df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
627500df344fSAndy Whitcroft#			print "$herecurr";
627600df344fSAndy Whitcroft#			$clean = 0;
627700df344fSAndy Whitcroft#		}
627800df344fSAndy Whitcroft
627922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
6280c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
62813705ce5bSJoe Perches			if (ERROR("SPACING",
62823705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
62833705ce5bSJoe Perches			    $fix) {
6284194f66fcSJoe Perches				$fixed[$fixlinenr] =~
62853705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
62863705ce5bSJoe Perches			}
62873705ce5bSJoe Perches
628822f2a2efSAndy Whitcroft		}
628922f2a2efSAndy Whitcroft
62904a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6291171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6292171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
62934a0df2efSAndy Whitcroft			my $which = $1;
62944a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6295000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6296000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
62974a0df2efSAndy Whitcroft			}
62984a0df2efSAndy Whitcroft		}
62994a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6300402c2553SMichael S. Tsirkin
6301402c2553SMichael S. Tsirkin		my $barriers = qr{
6302402c2553SMichael S. Tsirkin			mb|
6303402c2553SMichael S. Tsirkin			rmb|
6304ad83ec6cSWill Deacon			wmb
6305402c2553SMichael S. Tsirkin		}x;
6306402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6307402c2553SMichael S. Tsirkin			mb__before_atomic|
6308402c2553SMichael S. Tsirkin			mb__after_atomic|
6309402c2553SMichael S. Tsirkin			store_release|
6310402c2553SMichael S. Tsirkin			load_acquire|
6311402c2553SMichael S. Tsirkin			store_mb|
6312402c2553SMichael S. Tsirkin			(?:$barriers)
6313402c2553SMichael S. Tsirkin		}x;
6314402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6315402c2553SMichael S. Tsirkin			(?:$barriers)|
631643e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
631743e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6318402c2553SMichael S. Tsirkin		}x;
6319402c2553SMichael S. Tsirkin
6320402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
63214a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6322c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6323000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
63244a0df2efSAndy Whitcroft			}
63254a0df2efSAndy Whitcroft		}
63263ad81779SPaul E. McKenney
6327f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6328f4073b0fSMichael S. Tsirkin
6329f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6330f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6331f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6332f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6333f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6334f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6335f4073b0fSMichael S. Tsirkin		}
6336f4073b0fSMichael S. Tsirkin
6337cb426e99SJoe Perches# check for waitqueue_active without a comment.
6338cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6339cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6340cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6341cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6342cb426e99SJoe Perches			}
6343cb426e99SJoe Perches		}
63443ad81779SPaul E. McKenney
63455099a722SMarco Elver# check for data_race without a comment.
63465099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
63475099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
63485099a722SMarco Elver				WARN("DATA_RACE",
63495099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
63505099a722SMarco Elver			}
63515099a722SMarco Elver		}
63525099a722SMarco Elver
63534a0df2efSAndy Whitcroft# check of hardware specific defines
6354c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6355000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6356000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
63570a920b5bSAndy Whitcroft		}
6358653d4876SAndy Whitcroft
6359596ed45bSJoe Perches# check that the storage class is not after a type
6360596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6361000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6362596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6363596ed45bSJoe Perches		}
6364596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6365596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6366596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6367596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6368596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6369596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6370596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6371d4977c78STobias Klauser		}
6372d4977c78STobias Klauser
6373de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6374de7d4f0eSAndy Whitcroft# storage class and type.
63759c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
63769c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6377000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6378000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6379de7d4f0eSAndy Whitcroft		}
6380de7d4f0eSAndy Whitcroft
63818905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
63822b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
63832b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6384d5e616fcSJoe Perches			if (WARN("INLINE",
6385d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6386d5e616fcSJoe Perches			    $fix) {
6387194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6388d5e616fcSJoe Perches
6389d5e616fcSJoe Perches			}
63908905a67cSAndy Whitcroft		}
63918905a67cSAndy Whitcroft
63927ebe1d17SDwaipayan Ray# Check for compiler attributes
63932b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
63947ebe1d17SDwaipayan Ray		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
63957ebe1d17SDwaipayan Ray			my $attr = $1;
63967ebe1d17SDwaipayan Ray			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
63977ebe1d17SDwaipayan Ray
63987ebe1d17SDwaipayan Ray			my %attr_list = (
63990830aab0SJoe Perches				"alias"				=> "__alias",
64007ebe1d17SDwaipayan Ray				"aligned"			=> "__aligned",
64017ebe1d17SDwaipayan Ray				"always_inline"			=> "__always_inline",
64027ebe1d17SDwaipayan Ray				"assume_aligned"		=> "__assume_aligned",
64037ebe1d17SDwaipayan Ray				"cold"				=> "__cold",
64047ebe1d17SDwaipayan Ray				"const"				=> "__attribute_const__",
64057ebe1d17SDwaipayan Ray				"copy"				=> "__copy",
64067ebe1d17SDwaipayan Ray				"designated_init"		=> "__designated_init",
64077ebe1d17SDwaipayan Ray				"externally_visible"		=> "__visible",
64087ebe1d17SDwaipayan Ray				"format"			=> "printf|scanf",
64097ebe1d17SDwaipayan Ray				"gnu_inline"			=> "__gnu_inline",
64107ebe1d17SDwaipayan Ray				"malloc"			=> "__malloc",
64117ebe1d17SDwaipayan Ray				"mode"				=> "__mode",
64127ebe1d17SDwaipayan Ray				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
64137ebe1d17SDwaipayan Ray				"noclone"			=> "__noclone",
64147ebe1d17SDwaipayan Ray				"noinline"			=> "noinline",
64157ebe1d17SDwaipayan Ray				"nonstring"			=> "__nonstring",
64167ebe1d17SDwaipayan Ray				"noreturn"			=> "__noreturn",
64177ebe1d17SDwaipayan Ray				"packed"			=> "__packed",
64187ebe1d17SDwaipayan Ray				"pure"				=> "__pure",
6419339f29d9SJoe Perches				"section"			=> "__section",
64200830aab0SJoe Perches				"used"				=> "__used",
64210830aab0SJoe Perches				"weak"				=> "__weak"
64227ebe1d17SDwaipayan Ray			);
64237ebe1d17SDwaipayan Ray
64247ebe1d17SDwaipayan Ray			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6425339f29d9SJoe Perches				my $orig_attr = $1;
64267ebe1d17SDwaipayan Ray				my $params = '';
64277ebe1d17SDwaipayan Ray				$params = $2 if defined($2);
6428339f29d9SJoe Perches				my $curr_attr = $orig_attr;
64297ebe1d17SDwaipayan Ray				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
64307ebe1d17SDwaipayan Ray				if (exists($attr_list{$curr_attr})) {
6431339f29d9SJoe Perches					my $new = $attr_list{$curr_attr};
64327ebe1d17SDwaipayan Ray					if ($curr_attr eq "format" && $params) {
64337ebe1d17SDwaipayan Ray						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6434339f29d9SJoe Perches						$new = "__$1\($2";
64357ebe1d17SDwaipayan Ray					} else {
6436339f29d9SJoe Perches						$new = "$new$params";
64377ebe1d17SDwaipayan Ray					}
64387ebe1d17SDwaipayan Ray					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6439339f29d9SJoe Perches						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
64407ebe1d17SDwaipayan Ray					    $fix) {
6441339f29d9SJoe Perches						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6442339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/$remove//;
6443339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6444339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6445339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
64467ebe1d17SDwaipayan Ray					}
644739b7e287SJoe Perches				}
6448462811d9SJoe Perches			}
6449462811d9SJoe Perches
64507ebe1d17SDwaipayan Ray			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
64517ebe1d17SDwaipayan Ray			if ($attr =~ /^_*unused/) {
64527ebe1d17SDwaipayan Ray				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
64537ebe1d17SDwaipayan Ray				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6454d5e616fcSJoe Perches			}
64556061d949SJoe Perches		}
64566061d949SJoe Perches
6457619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
64585b57980dSJoe Perches		if ($perl_version_ok &&
6459619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6460619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6461619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6462619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6463619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6464619a908aSJoe Perches		}
6465619a908aSJoe Perches
6466fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6467e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6468fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6469e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6470e6176fa4SJoe Perches			my $type = $1;
6471e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6472e6176fa4SJoe Perches				$type = $1;
6473e6176fa4SJoe Perches				my $kernel_type = 'u';
6474e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6475e6176fa4SJoe Perches				$type =~ /(\d+)/;
6476e6176fa4SJoe Perches				$kernel_type .= $1;
6477e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6478e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6479e6176fa4SJoe Perches				    $fix) {
6480e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6481e6176fa4SJoe Perches				}
6482e6176fa4SJoe Perches			}
6483e6176fa4SJoe Perches		}
6484e6176fa4SJoe Perches
6485938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6486938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6487938224b5SJoe Perches			my $cast = $1;
6488938224b5SJoe Perches			my $const = $2;
6489938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6490938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6491938224b5SJoe Perches			    $fix) {
6492938224b5SJoe Perches				my $suffix = "";
6493938224b5SJoe Perches				my $newconst = $const;
6494938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6495938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6496938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6497938224b5SJoe Perches					$suffix .= 'LL';
6498938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6499938224b5SJoe Perches					$suffix .= 'L';
6500938224b5SJoe Perches				}
6501938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6502938224b5SJoe Perches			}
6503938224b5SJoe Perches		}
6504938224b5SJoe Perches
65058f53a9b8SJoe Perches# check for sizeof(&)
65068f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6507000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6508000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
65098f53a9b8SJoe Perches		}
65108f53a9b8SJoe Perches
651166c80b60SJoe Perches# check for sizeof without parenthesis
651266c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6513d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6514d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6515d5e616fcSJoe Perches			    $fix) {
6516194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6517d5e616fcSJoe Perches			}
651866c80b60SJoe Perches		}
651966c80b60SJoe Perches
652088982feaSJoe Perches# check for struct spinlock declarations
652188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
652288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
652388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
652488982feaSJoe Perches		}
652588982feaSJoe Perches
6526a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
652706668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6528a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6529caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6530caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6531d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6532d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6533d5e616fcSJoe Perches				    $fix) {
6534194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6535d5e616fcSJoe Perches				}
6536a6962d72SJoe Perches			}
6537a6962d72SJoe Perches		}
6538a6962d72SJoe Perches
65390b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
65405b57980dSJoe Perches		if ($perl_version_ok &&
65410b523769SJoe Perches		    defined $stat &&
65420b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
65430b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6544e3c6bc95STobin C. Harding			my $stat_real;
6545e3c6bc95STobin C. Harding
65460b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
65470b523769SJoe Perches			$lc = $lc + $linenr;
65480b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6549ffe07513SJoe Perches				my $specifier;
6550ffe07513SJoe Perches				my $extension;
65513bd32d6aSSakari Ailus				my $qualifier;
6552ffe07513SJoe Perches				my $bad_specifier = "";
65530b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
65540b523769SJoe Perches				$fmt =~ s/%%//g;
6555e3c6bc95STobin C. Harding
65563bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6557e3c6bc95STobin C. Harding					$specifier = $1;
6558e3c6bc95STobin C. Harding					$extension = $2;
65593bd32d6aSSakari Ailus					$qualifier = $3;
6560361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
65613bd32d6aSSakari Ailus					    ($extension eq "f" &&
65623bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6563e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
65640b523769SJoe Perches						last;
65650b523769SJoe Perches					}
6566e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6567e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6568e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
65690b523769SJoe Perches						}
6570e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6571e3c6bc95STobin 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");
6572e3c6bc95STobin C. Harding					}
6573e3c6bc95STobin C. Harding				}
6574e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
65752a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
65761df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
65771df7338aSSergey Senozhatsky					my $use = "";
6578e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
65791df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6580e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
65811df7338aSSergey Senozhatsky					}
65822a9f9d85STobin C. Harding
65830b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6584e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6585e3c6bc95STobin C. Harding				}
65860b523769SJoe Perches			}
65870b523769SJoe Perches		}
65880b523769SJoe Perches
6589554e165cSAndy Whitcroft# Check for misused memsets
65905b57980dSJoe Perches		if ($perl_version_ok &&
6591d1fe9c09SJoe Perches		    defined $stat &&
65929e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6593554e165cSAndy Whitcroft
6594d7c76ba7SJoe Perches			my $ms_addr = $2;
6595d1fe9c09SJoe Perches			my $ms_val = $7;
6596d1fe9c09SJoe Perches			my $ms_size = $12;
6597d7c76ba7SJoe Perches
6598554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6599554e165cSAndy Whitcroft				ERROR("MEMSET",
6600d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6601554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6602554e165cSAndy Whitcroft				WARN("MEMSET",
6603d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6604d7c76ba7SJoe Perches			}
6605d7c76ba7SJoe Perches		}
6606d7c76ba7SJoe Perches
660798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
66085b57980dSJoe Perches#		if ($perl_version_ok &&
6609f333195dSJoe Perches#		    defined $stat &&
6610f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6611f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6612f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6613f333195dSJoe Perches#			    $fix) {
6614f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6615f333195dSJoe Perches#			}
6616f333195dSJoe Perches#		}
661798a9bba5SJoe Perches
6618b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
66195b57980dSJoe Perches#		if ($perl_version_ok &&
6620f333195dSJoe Perches#		    defined $stat &&
6621f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6622f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6623f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6624f333195dSJoe Perches#		}
6625b6117d17SMateusz Kulikowski
66268617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
66278617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
66285b57980dSJoe Perches#		if ($perl_version_ok &&
6629f333195dSJoe Perches#		    defined $stat &&
6630f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6631f333195dSJoe Perches#
6632f333195dSJoe Perches#			my $ms_val = $7;
6633f333195dSJoe Perches#
6634f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6635f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6636f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6637f333195dSJoe Perches#				    $fix) {
6638f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6639f333195dSJoe Perches#				}
6640f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6641f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6642f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6643f333195dSJoe Perches#				    $fix) {
6644f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6645f333195dSJoe Perches#				}
6646f333195dSJoe Perches#			}
6647f333195dSJoe Perches#		}
66488617cd09SMateusz Kulikowski
6649*5dbdb2d8SJoe Perches# strlcpy uses that should likely be strscpy
6650*5dbdb2d8SJoe Perches		if ($line =~ /\bstrlcpy\s*\(/) {
6651*5dbdb2d8SJoe Perches			WARN("STRLCPY",
6652*5dbdb2d8SJoe Perches			     "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6653*5dbdb2d8SJoe Perches		}
6654*5dbdb2d8SJoe Perches
6655d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
66565b57980dSJoe Perches		if ($perl_version_ok &&
6657d1fe9c09SJoe Perches		    defined $stat &&
6658d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6659d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6660d7c76ba7SJoe Perches				my $call = $1;
6661d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6662d7c76ba7SJoe Perches				my $arg1 = $3;
6663d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6664d1fe9c09SJoe Perches				my $arg2 = $8;
6665d7c76ba7SJoe Perches				my $cast;
6666d7c76ba7SJoe Perches
6667d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6668d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6669d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6670d7c76ba7SJoe Perches					$cast = $cast1;
6671d7c76ba7SJoe Perches				} else {
6672d7c76ba7SJoe Perches					$cast = $cast2;
6673d7c76ba7SJoe Perches				}
6674d7c76ba7SJoe Perches				WARN("MINMAX",
6675d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6676554e165cSAndy Whitcroft			}
6677554e165cSAndy Whitcroft		}
6678554e165cSAndy Whitcroft
66794a273195SJoe Perches# check usleep_range arguments
66805b57980dSJoe Perches		if ($perl_version_ok &&
66814a273195SJoe Perches		    defined $stat &&
66824a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
66834a273195SJoe Perches			my $min = $1;
66844a273195SJoe Perches			my $max = $7;
66854a273195SJoe Perches			if ($min eq $max) {
66864a273195SJoe Perches				WARN("USLEEP_RANGE",
6687458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
66884a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
66894a273195SJoe Perches				 $min > $max) {
66904a273195SJoe Perches				WARN("USLEEP_RANGE",
6691458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
66924a273195SJoe Perches			}
66934a273195SJoe Perches		}
66944a273195SJoe Perches
6695823b794cSJoe Perches# check for naked sscanf
66965b57980dSJoe Perches		if ($perl_version_ok &&
6697823b794cSJoe Perches		    defined $stat &&
66986c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6699823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6700823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6701823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6702823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6703823b794cSJoe Perches			$lc = $lc + $linenr;
67042a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6705823b794cSJoe Perches			WARN("NAKED_SSCANF",
6706823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6707823b794cSJoe Perches		}
6708823b794cSJoe Perches
6709afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
67105b57980dSJoe Perches		if ($perl_version_ok &&
6711afc819abSJoe Perches		    defined $stat &&
6712afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6713afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6714afc819abSJoe Perches			$lc = $lc + $linenr;
67152a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6716afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6717afc819abSJoe Perches				my $format = $6;
6718afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6719afc819abSJoe Perches				if ($count == 1 &&
6720afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6721afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6722afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6723afc819abSJoe Perches				}
6724afc819abSJoe Perches			}
6725afc819abSJoe Perches		}
6726afc819abSJoe Perches
672770dc8a48SJoe Perches# check for new externs in .h files.
672870dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
672970dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6730d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
673170dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
673270dc8a48SJoe Perches			    $fix) {
6733194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
673470dc8a48SJoe Perches			}
673570dc8a48SJoe Perches		}
673670dc8a48SJoe Perches
6737de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6738171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6739c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6740171ae1a4SAndy Whitcroft		{
6741c45dcabdSAndy Whitcroft			my $function_name = $1;
6742c45dcabdSAndy Whitcroft			my $paren_space = $2;
6743171ae1a4SAndy Whitcroft
6744171ae1a4SAndy Whitcroft			my $s = $stat;
6745171ae1a4SAndy Whitcroft			if (defined $cond) {
6746171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6747171ae1a4SAndy Whitcroft			}
6748d8b44b58SKees Cook			if ($s =~ /^\s*;/)
6749c45dcabdSAndy Whitcroft			{
6750000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6751000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6752de7d4f0eSAndy Whitcroft			}
6753de7d4f0eSAndy Whitcroft
6754171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6755000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6756000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6757171ae1a4SAndy Whitcroft			}
67589c9ba34eSAndy Whitcroft
67599c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
67609c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
67619c9ba34eSAndy Whitcroft		{
6762000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6763000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6764171ae1a4SAndy Whitcroft		}
6765171ae1a4SAndy Whitcroft
6766a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6767a0ad7596SJoe Perches		if (defined $stat &&
6768d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6769d8b44b58SKees Cook		    $1 ne "void") {
6770d8b44b58SKees Cook			my $args = trim($1);
6771ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6772ca0d8929SJoe Perches				my $arg = trim($1);
6773d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6774ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6775ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6776ca0d8929SJoe Perches				}
6777ca0d8929SJoe Perches			}
6778ca0d8929SJoe Perches		}
6779ca0d8929SJoe Perches
6780a0ad7596SJoe Perches# check for function definitions
67815b57980dSJoe Perches		if ($perl_version_ok &&
6782a0ad7596SJoe Perches		    defined $stat &&
6783a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6784a0ad7596SJoe Perches			$context_function = $1;
6785a0ad7596SJoe Perches
6786a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6787a0ad7596SJoe Perches			my $ok = 0;
6788a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6789a0ad7596SJoe Perches			my $herectx = $here . "\n";
6790a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6791a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6792a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6793a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6794a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6795a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6796a0ad7596SJoe Perches			}
6797a0ad7596SJoe Perches			if (!$ok) {
6798a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6799a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6800a0ad7596SJoe Perches			}
6801a0ad7596SJoe Perches		}
6802a0ad7596SJoe Perches
6803de7d4f0eSAndy Whitcroft# checks for new __setup's
6804de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6805de7d4f0eSAndy Whitcroft			my $name = $1;
6806de7d4f0eSAndy Whitcroft
6807de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6808000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
68092581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6810de7d4f0eSAndy Whitcroft			}
6811653d4876SAndy Whitcroft		}
68129c0ca6f9SAndy Whitcroft
6813e29a70f1SJoe Perches# check for pointless casting of alloc functions
6814e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6815000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6816000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
68179c0ca6f9SAndy Whitcroft		}
681813214adfSAndy Whitcroft
6819a640d25cSJoe Perches# alloc style
6820a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
68215b57980dSJoe Perches		if ($perl_version_ok &&
6822e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6823a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6824a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6825a640d25cSJoe Perches		}
6826a640d25cSJoe Perches
682760a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
68285b57980dSJoe Perches		if ($perl_version_ok &&
68291b4a2ed4SJoe Perches		    defined $stat &&
68301b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
683160a55369SJoe Perches			my $oldfunc = $3;
683260a55369SJoe Perches			my $a1 = $4;
683360a55369SJoe Perches			my $a2 = $10;
683460a55369SJoe Perches			my $newfunc = "kmalloc_array";
683560a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
683660a55369SJoe Perches			my $r1 = $a1;
683760a55369SJoe Perches			my $r2 = $a2;
683860a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
683960a55369SJoe Perches				$r1 = $a2;
684060a55369SJoe Perches				$r2 = $a1;
684160a55369SJoe Perches			}
6842e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6843e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
68441b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6845e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6846e3d95a2aSTobin C. Harding
6847e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
68481b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
68491b4a2ed4SJoe Perches				    $cnt == 1 &&
6850e367455aSJoe Perches				    $fix) {
6851194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
685260a55369SJoe Perches				}
685360a55369SJoe Perches			}
685460a55369SJoe Perches		}
685560a55369SJoe Perches
6856972fdea2SJoe Perches# check for krealloc arg reuse
68575b57980dSJoe Perches		if ($perl_version_ok &&
68584cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
68594cab63ceSJoe Perches		    $1 eq $3) {
6860972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6861972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6862972fdea2SJoe Perches		}
6863972fdea2SJoe Perches
68645ce59ae0SJoe Perches# check for alloc argument mismatch
68655ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
68665ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
68675ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
68685ce59ae0SJoe Perches		}
68695ce59ae0SJoe Perches
6870caf2a54fSJoe Perches# check for multiple semicolons
6871caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6872d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6873d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6874d5e616fcSJoe Perches			    $fix) {
6875194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6876d5e616fcSJoe Perches			}
6877d1e2ad07SJoe Perches		}
6878d1e2ad07SJoe Perches
6879cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6880cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6881cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
68820ab90191SJoe Perches			my $ull = "";
68830ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
68840ab90191SJoe Perches			if (CHK("BIT_MACRO",
68850ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
68860ab90191SJoe Perches			    $fix) {
68870ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
68880ab90191SJoe Perches			}
68890ab90191SJoe Perches		}
68900ab90191SJoe Perches
689150161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
68923e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
689350161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
68943e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
689550161266SJoe Perches		}
689650161266SJoe Perches
68972d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
68983e89ad85SJerome 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*$/) {
68992d632745SJoe Perches			my $config = $1;
69002d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
69013e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
69022d632745SJoe Perches			    $fix) {
69032d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
69042d632745SJoe Perches			}
69052d632745SJoe Perches		}
69062d632745SJoe Perches
6907f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6908f36d3eb8SJoe Perches		my @fallthroughs = (
6909f36d3eb8SJoe Perches			'fallthrough',
6910f36d3eb8SJoe Perches			'@fallthrough@',
6911f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6912f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6913f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6914f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6915f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6916f36d3eb8SJoe Perches		    );
6917f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6918f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6919f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6920f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6921f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6922f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6923f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6924f36d3eb8SJoe Perches					last;
6925f36d3eb8SJoe Perches				}
6926f36d3eb8SJoe Perches			}
6927f36d3eb8SJoe Perches		}
6928f36d3eb8SJoe Perches
6929d1e2ad07SJoe Perches# check for switch/default statements without a break;
69305b57980dSJoe Perches		if ($perl_version_ok &&
6931d1e2ad07SJoe Perches		    defined $stat &&
6932d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6933d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6934e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6935e3d95a2aSTobin C. Harding
6936d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6937d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6938caf2a54fSJoe Perches		}
6939caf2a54fSJoe Perches
694013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6941d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6942d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6943d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6944d5e616fcSJoe Perches			    $fix) {
6945194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6946d5e616fcSJoe Perches			}
694713214adfSAndy Whitcroft		}
6948773647a0SAndy Whitcroft
694962ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
695062ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
695162ec818fSJoe Perches			ERROR("DATE_TIME",
695262ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
695362ec818fSJoe Perches		}
695462ec818fSJoe Perches
69552c92488aSJoe Perches# check for use of yield()
69562c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
69572c92488aSJoe Perches			WARN("YIELD",
69582c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
69592c92488aSJoe Perches		}
69602c92488aSJoe Perches
6961179f8f40SJoe Perches# check for comparisons against true and false
6962179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6963179f8f40SJoe Perches			my $lead = $1;
6964179f8f40SJoe Perches			my $arg = $2;
6965179f8f40SJoe Perches			my $test = $3;
6966179f8f40SJoe Perches			my $otype = $4;
6967179f8f40SJoe Perches			my $trail = $5;
6968179f8f40SJoe Perches			my $op = "!";
6969179f8f40SJoe Perches
6970179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6971179f8f40SJoe Perches
6972179f8f40SJoe Perches			my $type = lc($otype);
6973179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6974179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6975179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6976179f8f40SJoe Perches					$op = "";
6977179f8f40SJoe Perches				}
6978179f8f40SJoe Perches
6979179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6980179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6981179f8f40SJoe Perches
6982179f8f40SJoe Perches## maybe suggesting a correct construct would better
6983179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6984179f8f40SJoe Perches
6985179f8f40SJoe Perches			}
6986179f8f40SJoe Perches		}
6987179f8f40SJoe Perches
69884882720bSThomas Gleixner# check for semaphores initialized locked
69894882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6990000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6991000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6992773647a0SAndy Whitcroft		}
69936712d858SJoe Perches
699467d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
699567d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6996000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
699767d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6998773647a0SAndy Whitcroft		}
69996712d858SJoe Perches
7000ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
7001f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
7002000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
7003ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
7004f3db6639SMichael Ellerman		}
70056712d858SJoe Perches
70063d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
70073d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
70083d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
70093d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
70103d709ab5SPaul E. McKenney		}
70113d709ab5SPaul E. McKenney
70129189c7e7SJoe Perches# check for deprecated apis
70139189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
70149189c7e7SJoe Perches			my $deprecated_api = $1;
70159189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
70169189c7e7SJoe Perches			WARN("DEPRECATED_API",
70179189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
70189189c7e7SJoe Perches		}
70199189c7e7SJoe Perches
70200f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
7021d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
7022ced69da1SQuentin Monnet		if (defined($const_structs) &&
7023ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
7024d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7025000d1cc1SJoe Perches			WARN("CONST_STRUCT",
7026d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
70272b6db5cbSAndy Whitcroft		}
7028773647a0SAndy Whitcroft
7029773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
7030773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
7031773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
7032c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7033c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7034171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7035171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7036171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
7037773647a0SAndy Whitcroft		{
7038000d1cc1SJoe Perches			WARN("NR_CPUS",
7039000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7040773647a0SAndy Whitcroft		}
70419c9ba34eSAndy Whitcroft
704252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
704352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
704452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
704552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
704652ea8506SJoe Perches		}
704752ea8506SJoe Perches
7048acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
70495b57980dSJoe Perches		if ($perl_version_ok &&
7050acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7051acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
7052acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7053acd9362cSJoe Perches		}
7054acd9362cSJoe Perches
7055de3f186fSDenis Efremov# nested likely/unlikely calls
7056de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7057de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
7058de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7059de3f186fSDenis Efremov		}
7060de3f186fSDenis Efremov
7061691d77b6SAndy Whitcroft# whine mightly about in_atomic
7062691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
7063691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
7064000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
7065000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
7066f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
7067000d1cc1SJoe Perches				WARN("IN_ATOMIC",
7068000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7069691d77b6SAndy Whitcroft			}
7070691d77b6SAndy Whitcroft		}
70711704f47bSPeter Zijlstra
70720f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
70730f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
70740f5225b0SPeter Zijlstra			ERROR("LOCKING",
70750f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
70760f5225b0SPeter Zijlstra		}
70770f5225b0SPeter Zijlstra
70781704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
70791704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
70801704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
70811704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
70821704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
70831704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
7084000d1cc1SJoe Perches				ERROR("LOCKDEP",
7085000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
70861704f47bSPeter Zijlstra			}
70871704f47bSPeter Zijlstra		}
708888f8831cSDave Jones
7089b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7090b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7091000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
7092000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
709388f8831cSDave Jones		}
70942435880fSJoe Perches
709500180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
709600180468SJoe Perches# and whether or not function naming is typical and if
709700180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
70985b57980dSJoe Perches		if ($perl_version_ok &&
709900180468SJoe Perches		    defined $stat &&
710000180468SJoe 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*\)/) {
710100180468SJoe Perches			my $var = $1;
710200180468SJoe Perches			my $perms = $2;
710300180468SJoe Perches			my $show = $3;
710400180468SJoe Perches			my $store = $4;
710500180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
710600180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
710700180468SJoe Perches			    $store =~ /^${var}_store$/ &&
710800180468SJoe Perches			    $octal_perms eq "0644") {
710900180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
711000180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
711100180468SJoe Perches				    $fix) {
711200180468SJoe 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})/;
711300180468SJoe Perches				}
711400180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
711500180468SJoe Perches				 $store =~ /^NULL$/ &&
711600180468SJoe Perches				 $octal_perms eq "0444") {
711700180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
711800180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
711900180468SJoe Perches				    $fix) {
712000180468SJoe 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})/;
712100180468SJoe Perches				}
712200180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
712300180468SJoe Perches				 $store =~ /^${var}_store$/ &&
712400180468SJoe Perches				 $octal_perms eq "0200") {
712500180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
712600180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
712700180468SJoe Perches				    $fix) {
712800180468SJoe 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})/;
712900180468SJoe Perches				}
713000180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
713100180468SJoe Perches				 $octal_perms eq "0444" ||
713200180468SJoe Perches				 $octal_perms eq "0200") {
713300180468SJoe Perches				my $newshow = "$show";
713400180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
713500180468SJoe Perches				my $newstore = $store;
713600180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
713700180468SJoe Perches				my $rename = "";
713800180468SJoe Perches				if ($show ne $newshow) {
713900180468SJoe Perches					$rename .= " '$show' to '$newshow'";
714000180468SJoe Perches				}
714100180468SJoe Perches				if ($store ne $newstore) {
714200180468SJoe Perches					$rename .= " '$store' to '$newstore'";
714300180468SJoe Perches				}
714400180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
714500180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
714600180468SJoe Perches			} else {
714700180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
714800180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
714900180468SJoe Perches			}
715000180468SJoe Perches		}
715100180468SJoe Perches
7152515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
7153515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
715473121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
715573121534SJoe Perches#   specific definition of not visible in sysfs.
715673121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
715773121534SJoe Perches#   use the default permissions
71585b57980dSJoe Perches		if ($perl_version_ok &&
7159459cf0aeSJoe Perches		    defined $stat &&
7160515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
71612435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
71622435880fSJoe Perches				my $func = $entry->[0];
71632435880fSJoe Perches				my $arg_pos = $entry->[1];
71642435880fSJoe Perches
7165459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
7166459cf0aeSJoe Perches				$lc = $lc + $linenr;
71672a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
7168459cf0aeSJoe Perches
71692435880fSJoe Perches				my $skip_args = "";
71702435880fSJoe Perches				if ($arg_pos > 1) {
71712435880fSJoe Perches					$arg_pos--;
71722435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
71732435880fSJoe Perches				}
7174f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7175459cf0aeSJoe Perches				if ($stat =~ /$test/) {
71762435880fSJoe Perches					my $val = $1;
71772435880fSJoe Perches					$val = $6 if ($skip_args ne "");
717873121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
717973121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
718073121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
71812435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
7182459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7183f90774e1SJoe Perches					}
7184f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7185c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
7186459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
71872435880fSJoe Perches					}
7188459cf0aeSJoe Perches				}
7189459cf0aeSJoe Perches			}
7190459cf0aeSJoe Perches		}
7191459cf0aeSJoe Perches
7192459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
7193bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
719400180468SJoe Perches			my $oval = $1;
719500180468SJoe Perches			my $octal = perms_to_octal($oval);
7196f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
7197459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7198f90774e1SJoe Perches			    $fix) {
719900180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
72002435880fSJoe Perches			}
720113214adfSAndy Whitcroft		}
72025a6d20ceSBjorn Andersson
72035a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
72045a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
72055a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
72065a6d20ceSBjorn Andersson			my $valid_licenses = qr{
72075a6d20ceSBjorn Andersson						GPL|
72085a6d20ceSBjorn Andersson						GPL\ v2|
72095a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
72105a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
72115a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
72125a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
72135a6d20ceSBjorn Andersson						Proprietary
72145a6d20ceSBjorn Andersson					}x;
72155a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
72165a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
72175a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
72185a6d20ceSBjorn Andersson			}
72195a6d20ceSBjorn Andersson		}
72206a8d76cbSMatteo Croce
72216a8d76cbSMatteo Croce# check for sysctl duplicate constants
72226a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
72236a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
72246a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
72256a8d76cbSMatteo Croce		}
7226515a235eSJoe Perches	}
722713214adfSAndy Whitcroft
722813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
722913214adfSAndy Whitcroft	# so just keep quiet.
723013214adfSAndy Whitcroft	if ($#rawlines == -1) {
723113214adfSAndy Whitcroft		exit(0);
72320a920b5bSAndy Whitcroft	}
72330a920b5bSAndy Whitcroft
72348905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
72358905a67cSAndy Whitcroft	# things that appear to be patches.
72368905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
72378905a67cSAndy Whitcroft		exit(0);
72388905a67cSAndy Whitcroft	}
72398905a67cSAndy Whitcroft
7240e73d2715SDwaipayan Ray	# This is not a patch, and we are in 'no-patch' mode so
72418905a67cSAndy Whitcroft	# just keep quiet.
72428905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
72438905a67cSAndy Whitcroft		exit(0);
72448905a67cSAndy Whitcroft	}
72458905a67cSAndy Whitcroft
7246a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7247000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
7248000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
72490a920b5bSAndy Whitcroft	}
7250cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
7251cd261496SGeert Uytterhoeven		if ($signoff == 0) {
7252000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
7253000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
725448ca2d8aSDwaipayan Ray		} elsif ($authorsignoff != 1) {
725548ca2d8aSDwaipayan Ray			# authorsignoff values:
725648ca2d8aSDwaipayan Ray			# 0 -> missing sign off
725748ca2d8aSDwaipayan Ray			# 1 -> sign off identical
725848ca2d8aSDwaipayan Ray			# 2 -> names and addresses match, comments mismatch
725948ca2d8aSDwaipayan Ray			# 3 -> addresses match, names different
726048ca2d8aSDwaipayan Ray			# 4 -> names match, addresses different
726148ca2d8aSDwaipayan Ray			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
726248ca2d8aSDwaipayan Ray
726348ca2d8aSDwaipayan Ray			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
726448ca2d8aSDwaipayan Ray
726548ca2d8aSDwaipayan Ray			if ($authorsignoff == 0) {
726648ca2d8aSDwaipayan Ray				ERROR("NO_AUTHOR_SIGN_OFF",
7267cd261496SGeert Uytterhoeven				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
726848ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 2) {
726948ca2d8aSDwaipayan Ray				CHK("FROM_SIGN_OFF_MISMATCH",
727048ca2d8aSDwaipayan Ray				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
727148ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 3) {
727248ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
727348ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
727448ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 4) {
727548ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
727648ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
727748ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 5) {
727848ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
727948ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
728048ca2d8aSDwaipayan Ray			}
7281cd261496SGeert Uytterhoeven		}
72820a920b5bSAndy Whitcroft	}
72830a920b5bSAndy Whitcroft
7284f0a594c1SAndy Whitcroft	print report_dump();
728513214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
728613214adfSAndy Whitcroft		print "$filename " if ($summary_file);
72876c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
72886c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
72896c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
72906c72ffaaSAndy Whitcroft	}
72918905a67cSAndy Whitcroft
7292d2c0a235SAndy Whitcroft	if ($quiet == 0) {
7293ef212196SJoe Perches		# If there were any defects found and not already fixing them
7294ef212196SJoe Perches		if (!$clean and !$fix) {
7295ef212196SJoe Perches			print << "EOM"
7296ef212196SJoe Perches
7297ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
7298ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
7299ef212196SJoe PerchesEOM
7300ef212196SJoe Perches		}
7301d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
7302d2c0a235SAndy Whitcroft		# then suggest that.
7303d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
7304b0781216SMike Frysinger			$rpt_cleaners = 0;
7305d8469f16SJoe Perches			print << "EOM"
7306d8469f16SJoe Perches
7307d8469f16SJoe PerchesNOTE: Whitespace errors detected.
7308d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
7309d8469f16SJoe PerchesEOM
7310d2c0a235SAndy Whitcroft		}
7311d2c0a235SAndy Whitcroft	}
7312d2c0a235SAndy Whitcroft
7313d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
7314d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
7315d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
73169624b8d6SJoe Perches		my $newfile = $filename;
73179624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
73183705ce5bSJoe Perches		my $linecount = 0;
73193705ce5bSJoe Perches		my $f;
73203705ce5bSJoe Perches
7321d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7322d752fcc8SJoe Perches
73233705ce5bSJoe Perches		open($f, '>', $newfile)
73243705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
73253705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
73263705ce5bSJoe Perches			$linecount++;
73273705ce5bSJoe Perches			if ($file) {
73283705ce5bSJoe Perches				if ($linecount > 3) {
73293705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
73303705ce5bSJoe Perches					print $f $fixed_line . "\n";
73313705ce5bSJoe Perches				}
73323705ce5bSJoe Perches			} else {
73333705ce5bSJoe Perches				print $f $fixed_line . "\n";
73343705ce5bSJoe Perches			}
73353705ce5bSJoe Perches		}
73363705ce5bSJoe Perches		close($f);
73373705ce5bSJoe Perches
73383705ce5bSJoe Perches		if (!$quiet) {
73393705ce5bSJoe Perches			print << "EOM";
7340d8469f16SJoe Perches
73413705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
73423705ce5bSJoe Perches
73433705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
73443705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
73453705ce5bSJoe Perches
73463705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
73473705ce5bSJoe PerchesNo warranties, expressed or implied...
73483705ce5bSJoe PerchesEOM
73493705ce5bSJoe Perches		}
73503705ce5bSJoe Perches	}
73513705ce5bSJoe Perches
7352d8469f16SJoe Perches	if ($quiet == 0) {
7353d8469f16SJoe Perches		print "\n";
7354d8469f16SJoe Perches		if ($clean == 1) {
7355d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7356d8469f16SJoe Perches		} else {
7357d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
73580a920b5bSAndy Whitcroft		}
73590a920b5bSAndy Whitcroft	}
73600a920b5bSAndy Whitcroft	return $clean;
73610a920b5bSAndy Whitcroft}
7362