xref: /linux-6.15/scripts/checkpatch.pl (revision fccaebf0)
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
5091813087dSJoe Perchesour @typeListMisordered = (
5101813087dSJoe Perches	qr{char\s+(?:un)?signed},
5111813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
5121813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
5131813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
5141813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
5151813087dSJoe Perches	qr{short\s+(?:un)?signed},
5161813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5171813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5181813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5191813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5201813087dSJoe Perches	qr{int\s+(?:un)?signed},
5211813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5221813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5231813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5241813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5251813087dSJoe Perches	qr{long\s+(?:un)?signed},
5261813087dSJoe Perches);
5271813087dSJoe Perches
5288905a67cSAndy Whitcroftour @typeList = (
5298905a67cSAndy Whitcroft	qr{void},
5300c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5310c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5320c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5330c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5340c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5350c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5360c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5370c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5380c773d9dSJoe Perches	qr{(?:un)?signed},
5398905a67cSAndy Whitcroft	qr{float},
5408905a67cSAndy Whitcroft	qr{double},
5418905a67cSAndy Whitcroft	qr{bool},
5428905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5438905a67cSAndy Whitcroft	qr{union\s+$Ident},
5448905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5458905a67cSAndy Whitcroft	qr{${Ident}_t},
5468905a67cSAndy Whitcroft	qr{${Ident}_handler},
5478905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5481813087dSJoe Perches	@typeListMisordered,
5498905a67cSAndy Whitcroft);
550938224b5SJoe Perches
551938224b5SJoe Perchesour $C90_int_types = qr{(?x:
552938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
553938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
554938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
555938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
556938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
557938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
558938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
559938224b5SJoe Perches
560938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
561938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
562938224b5SJoe Perches	long\s+(?:un)?signed|
563938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
564938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
565938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
566938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
567938224b5SJoe Perches
568938224b5SJoe Perches	int\s+(?:un)?signed|
569938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
570938224b5SJoe Perches)};
571938224b5SJoe Perches
572485ff23eSAlex Dowadour @typeListFile = ();
5738716de38SJoe Perchesour @typeListWithAttr = (
5748716de38SJoe Perches	@typeList,
5758716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5768716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5778716de38SJoe Perches);
5788716de38SJoe Perches
579c45dcabdSAndy Whitcroftour @modifierList = (
580c45dcabdSAndy Whitcroft	qr{fastcall},
581c45dcabdSAndy Whitcroft);
582485ff23eSAlex Dowadour @modifierListFile = ();
5838905a67cSAndy Whitcroft
5842435880fSJoe Perchesour @mode_permission_funcs = (
5852435880fSJoe Perches	["module_param", 3],
5862435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5872435880fSJoe Perches	["module_param_array_named", 5],
5882435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5892435880fSJoe Perches	["proc_create(?:_data|)", 2],
590459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
591459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
592459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
593459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
594459cf0aeSJoe Perches	["__ATTR", 2],
5952435880fSJoe Perches);
5962435880fSJoe Perches
5971a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
5981a3dcf2eSJoe Perches
599515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
600515a235eSJoe Perchesour $mode_perms_search = "";
601515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
602515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
603515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
604515a235eSJoe Perches}
60500180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
606515a235eSJoe Perches
6079189c7e7SJoe Perchesour %deprecated_apis = (
6089189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
6099189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
6109189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
6119189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
6129189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
6139189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
6149189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
6159189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
6169189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
6179189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6189189c7e7SJoe Perches);
6199189c7e7SJoe Perches
6209189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6219189c7e7SJoe Perchesour $deprecated_apis_search = "";
6229189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6239189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6249189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6259189c7e7SJoe Perches}
6269189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6279189c7e7SJoe Perches
628b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
629b392c64fSJoe Perches	S_IWUGO		|
630b392c64fSJoe Perches	S_IWOTH		|
631b392c64fSJoe Perches	S_IRWXUGO	|
632b392c64fSJoe Perches	S_IALLUGO	|
633b392c64fSJoe Perches	0[0-7][0-7][2367]
634b392c64fSJoe Perches}x;
635b392c64fSJoe Perches
636f90774e1SJoe Perchesour %mode_permission_string_types = (
637f90774e1SJoe Perches	"S_IRWXU" => 0700,
638f90774e1SJoe Perches	"S_IRUSR" => 0400,
639f90774e1SJoe Perches	"S_IWUSR" => 0200,
640f90774e1SJoe Perches	"S_IXUSR" => 0100,
641f90774e1SJoe Perches	"S_IRWXG" => 0070,
642f90774e1SJoe Perches	"S_IRGRP" => 0040,
643f90774e1SJoe Perches	"S_IWGRP" => 0020,
644f90774e1SJoe Perches	"S_IXGRP" => 0010,
645f90774e1SJoe Perches	"S_IRWXO" => 0007,
646f90774e1SJoe Perches	"S_IROTH" => 0004,
647f90774e1SJoe Perches	"S_IWOTH" => 0002,
648f90774e1SJoe Perches	"S_IXOTH" => 0001,
649f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
650f90774e1SJoe Perches	"S_IRUGO" => 0444,
651f90774e1SJoe Perches	"S_IWUGO" => 0222,
652f90774e1SJoe Perches	"S_IXUGO" => 0111,
653f90774e1SJoe Perches);
654f90774e1SJoe Perches
655f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
656f90774e1SJoe Perchesour $mode_perms_string_search = "";
657f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
658f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
659f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
660f90774e1SJoe Perches}
66100180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
66200180468SJoe Perchesour $multi_mode_perms_string_search = qr{
66300180468SJoe Perches	${single_mode_perms_string_search}
66400180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
66500180468SJoe Perches}x;
66600180468SJoe Perches
66700180468SJoe Perchessub perms_to_octal {
66800180468SJoe Perches	my ($string) = @_;
66900180468SJoe Perches
67000180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
67100180468SJoe Perches
67200180468SJoe Perches	my $val = "";
67300180468SJoe Perches	my $oval = "";
67400180468SJoe Perches	my $to = 0;
67500180468SJoe Perches	my $curpos = 0;
67600180468SJoe Perches	my $lastpos = 0;
67700180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
67800180468SJoe Perches		$curpos = pos($string);
67900180468SJoe Perches		my $match = $2;
68000180468SJoe Perches		my $omatch = $1;
68100180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
68200180468SJoe Perches		$lastpos = $curpos;
68300180468SJoe Perches		$to |= $mode_permission_string_types{$match};
68400180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
68500180468SJoe Perches		$val .= $match;
68600180468SJoe Perches		$oval .= $omatch;
68700180468SJoe Perches	}
68800180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
68900180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
69000180468SJoe Perches	return sprintf("%04o", $to);
69100180468SJoe Perches}
692f90774e1SJoe Perches
6937840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6947840a94cSWolfram Sang	irq|
695cdcee686SSergey Ryazanov	memory|
696cdcee686SSergey Ryazanov	time|
697cdcee686SSergey Ryazanov	reboot
6987840a94cSWolfram Sang)};
6997840a94cSWolfram Sang# memory.h: ARM has a custom one
7007840a94cSWolfram Sang
70166b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
70266b47b4aSKees Cookmy $misspellings;
70366b47b4aSKees Cookmy %spelling_fix;
70436061e38SJoe Perches
70536061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
70666b47b4aSKees Cook	while (<$spelling>) {
70766b47b4aSKees Cook		my $line = $_;
70866b47b4aSKees Cook
70966b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
71066b47b4aSKees Cook		$line =~ s/^\s*//g;
71166b47b4aSKees Cook
71266b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
71366b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
71466b47b4aSKees Cook
71566b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
71666b47b4aSKees Cook
71766b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
71866b47b4aSKees Cook	}
71966b47b4aSKees Cook	close($spelling);
72036061e38SJoe Perches} else {
72136061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
72236061e38SJoe Perches}
72366b47b4aSKees Cook
724ebfd7d62SJoe Perchesif ($codespell) {
725ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
726ebfd7d62SJoe Perches		while (<$spelling>) {
727ebfd7d62SJoe Perches			my $line = $_;
728ebfd7d62SJoe Perches
729ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
730ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
731ebfd7d62SJoe Perches
732ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
733ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
734ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
735ebfd7d62SJoe Perches
736ebfd7d62SJoe Perches			$line =~ s/,.*$//;
737ebfd7d62SJoe Perches
738ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
739ebfd7d62SJoe Perches
740ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
741ebfd7d62SJoe Perches		}
742ebfd7d62SJoe Perches		close($spelling);
743ebfd7d62SJoe Perches	} else {
744ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
745ebfd7d62SJoe Perches	}
746ebfd7d62SJoe Perches}
747ebfd7d62SJoe Perches
748ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
749ebfd7d62SJoe Perches
75075ad8c57SJerome Forissiersub read_words {
75175ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
75275ad8c57SJerome Forissier
75375ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
75475ad8c57SJerome Forissier		while (<$words>) {
755bf1fa1daSJoe Perches			my $line = $_;
756bf1fa1daSJoe Perches
757bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
758bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
759bf1fa1daSJoe Perches
760bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
761bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
762bf1fa1daSJoe Perches			if ($line =~ /\s/) {
76375ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
764bf1fa1daSJoe Perches				next;
765bf1fa1daSJoe Perches			}
766bf1fa1daSJoe Perches
767ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
76875ad8c57SJerome Forissier			$$wordsRef .= $line;
769bf1fa1daSJoe Perches		}
77075ad8c57SJerome Forissier		close($file);
77175ad8c57SJerome Forissier		return 1;
772bf1fa1daSJoe Perches	}
773bf1fa1daSJoe Perches
77475ad8c57SJerome Forissier	return 0;
77575ad8c57SJerome Forissier}
77675ad8c57SJerome Forissier
777ced69da1SQuentin Monnetmy $const_structs;
778ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
77975ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
78075ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
781ced69da1SQuentin Monnet}
78275ad8c57SJerome Forissier
783ced69da1SQuentin Monnetif (defined($typedefsfile)) {
784ced69da1SQuentin Monnet	my $typeOtherTypedefs;
78575ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
78675ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
787ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
78875ad8c57SJerome Forissier}
78975ad8c57SJerome Forissier
7908905a67cSAndy Whitcroftsub build_types {
791485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
792485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7931813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7948716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
795c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
796ab7e23f3SJoe Perches	$BasicType	= qr{
797ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
798ab7e23f3SJoe Perches				(?:${all}\b)
799ab7e23f3SJoe Perches		}x;
8008905a67cSAndy Whitcroft	$NonptrType	= qr{
801d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
802cf655043SAndy Whitcroft			(?:
8036b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
8048ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
805c45dcabdSAndy Whitcroft				(?:${all}\b)
806cf655043SAndy Whitcroft			)
807c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
8088905a67cSAndy Whitcroft		  }x;
8091813087dSJoe Perches	$NonptrTypeMisordered	= qr{
8101813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
8111813087dSJoe Perches			(?:
8121813087dSJoe Perches				(?:${Misordered}\b)
8131813087dSJoe Perches			)
8141813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
8151813087dSJoe Perches		  }x;
8168716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
8178716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
8188716de38SJoe Perches			(?:
8198716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8208716de38SJoe Perches				(?:$typeTypedefs\b)|
8218716de38SJoe Perches				(?:${allWithAttr}\b)
8228716de38SJoe Perches			)
8238716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8248716de38SJoe Perches		  }x;
8258905a67cSAndy Whitcroft	$Type	= qr{
826c45dcabdSAndy Whitcroft			$NonptrType
8277b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
828c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8298905a67cSAndy Whitcroft		  }x;
8301813087dSJoe Perches	$TypeMisordered	= qr{
8311813087dSJoe Perches			$NonptrTypeMisordered
8327b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
8331813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8341813087dSJoe Perches		  }x;
83591cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8361813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8378905a67cSAndy Whitcroft}
8388905a67cSAndy Whitcroftbuild_types();
8396c72ffaaSAndy Whitcroft
8407d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
841d1fe9c09SJoe Perches
842d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
843d1fe9c09SJoe Perches# requires at least perl version v5.10.0
844d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
845d1fe9c09SJoe Perches
846d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8472435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
848c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8497d2367afSJoe Perches
850f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8513e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
852fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8533d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
854f8422308SJoe Perches)};
855f8422308SJoe Perches
8568d0325ccSAditya Srivastavaour %allow_repeated_words = (
8578d0325ccSAditya Srivastava	add => '',
8588d0325ccSAditya Srivastava	added => '',
8598d0325ccSAditya Srivastava	bad => '',
8608d0325ccSAditya Srivastava	be => '',
8618d0325ccSAditya Srivastava);
8628d0325ccSAditya Srivastava
8637d2367afSJoe Perchessub deparenthesize {
8647d2367afSJoe Perches	my ($string) = @_;
8657d2367afSJoe Perches	return "" if (!defined($string));
8665b9553abSJoe Perches
8675b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8685b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8695b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8705b9553abSJoe Perches	}
8715b9553abSJoe Perches
8727d2367afSJoe Perches	$string =~ s@\s+@ @g;
8735b9553abSJoe Perches
8747d2367afSJoe Perches	return $string;
8757d2367afSJoe Perches}
8767d2367afSJoe Perches
8773445686aSJoe Perchessub seed_camelcase_file {
8783445686aSJoe Perches	my ($file) = @_;
8793445686aSJoe Perches
8803445686aSJoe Perches	return if (!(-f $file));
8813445686aSJoe Perches
8823445686aSJoe Perches	local $/;
8833445686aSJoe Perches
8843445686aSJoe Perches	open(my $include_file, '<', "$file")
8853445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8863445686aSJoe Perches	my $text = <$include_file>;
8873445686aSJoe Perches	close($include_file);
8883445686aSJoe Perches
8893445686aSJoe Perches	my @lines = split('\n', $text);
8903445686aSJoe Perches
8913445686aSJoe Perches	foreach my $line (@lines) {
8923445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8933445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8943445686aSJoe Perches			$camelcase{$1} = 1;
89511ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
89611ea516aSJoe Perches			$camelcase{$1} = 1;
89711ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8983445686aSJoe Perches			$camelcase{$1} = 1;
8993445686aSJoe Perches		}
9003445686aSJoe Perches	}
9013445686aSJoe Perches}
9023445686aSJoe Perches
903cd28b119SJoe Perchesour %maintained_status = ();
904cd28b119SJoe Perches
90585b0ee18SJoe Perchessub is_maintained_obsolete {
90685b0ee18SJoe Perches	my ($filename) = @_;
90785b0ee18SJoe Perches
908f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
90985b0ee18SJoe Perches
910cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
911cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
912cd28b119SJoe Perches	}
91385b0ee18SJoe Perches
914cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
91585b0ee18SJoe Perches}
91685b0ee18SJoe Perches
9173b6e8ac9SJoe Perchessub is_SPDX_License_valid {
9183b6e8ac9SJoe Perches	my ($license) = @_;
9193b6e8ac9SJoe Perches
9200f7f635bSJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
9213b6e8ac9SJoe Perches
92256294112SJoe Perches	my $root_path = abs_path($root);
92356294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
9243b6e8ac9SJoe Perches	return 0 if ($status ne "");
9253b6e8ac9SJoe Perches	return 1;
9263b6e8ac9SJoe Perches}
9273b6e8ac9SJoe Perches
9283445686aSJoe Perchesmy $camelcase_seeded = 0;
9293445686aSJoe Perchessub seed_camelcase_includes {
9303445686aSJoe Perches	return if ($camelcase_seeded);
9313445686aSJoe Perches
9323445686aSJoe Perches	my $files;
933c707a81dSJoe Perches	my $camelcase_cache = "";
934c707a81dSJoe Perches	my @include_files = ();
935c707a81dSJoe Perches
936c707a81dSJoe Perches	$camelcase_seeded = 1;
937351b2a1fSJoe Perches
9380f7f635bSJoe Perches	if (-e "$gitroot") {
939dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
940351b2a1fSJoe Perches		chomp $git_last_include_commit;
941c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
942c707a81dSJoe Perches	} else {
943c707a81dSJoe Perches		my $last_mod_date = 0;
944c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
945c707a81dSJoe Perches		@include_files = split('\n', $files);
946c707a81dSJoe Perches		foreach my $file (@include_files) {
947c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
948c707a81dSJoe Perches						   localtime((stat $file)[9]));
949c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
950c707a81dSJoe Perches		}
951c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
952c707a81dSJoe Perches	}
953c707a81dSJoe Perches
954c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
955c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
956c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
957351b2a1fSJoe Perches		while (<$camelcase_file>) {
958351b2a1fSJoe Perches			chomp;
959351b2a1fSJoe Perches			$camelcase{$_} = 1;
960351b2a1fSJoe Perches		}
961351b2a1fSJoe Perches		close($camelcase_file);
962351b2a1fSJoe Perches
963351b2a1fSJoe Perches		return;
964351b2a1fSJoe Perches	}
965c707a81dSJoe Perches
9660f7f635bSJoe Perches	if (-e "$gitroot") {
967dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
968c707a81dSJoe Perches		@include_files = split('\n', $files);
9693445686aSJoe Perches	}
970c707a81dSJoe Perches
9713445686aSJoe Perches	foreach my $file (@include_files) {
9723445686aSJoe Perches		seed_camelcase_file($file);
9733445686aSJoe Perches	}
974351b2a1fSJoe Perches
975c707a81dSJoe Perches	if ($camelcase_cache ne "") {
976351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
977c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
978c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
979351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
980351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
981351b2a1fSJoe Perches		}
982351b2a1fSJoe Perches		close($camelcase_file);
983351b2a1fSJoe Perches	}
9843445686aSJoe Perches}
9853445686aSJoe Perches
986f5f61325SJoe Perchessub git_is_single_file {
987f5f61325SJoe Perches	my ($filename) = @_;
988f5f61325SJoe Perches
989f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
990f5f61325SJoe Perches
991f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
992f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
993f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
994f5f61325SJoe Perches}
995f5f61325SJoe Perches
996d311cd44SJoe Perchessub git_commit_info {
997d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
998d311cd44SJoe Perches
9990f7f635bSJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1000d311cd44SJoe Perches
1001dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1002d311cd44SJoe Perches	$output =~ s/^\s*//gm;
1003d311cd44SJoe Perches	my @lines = split("\n", $output);
1004d311cd44SJoe Perches
10050d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
10060d7835fcSJoe Perches
10075a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1008d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1009d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1010d311cd44SJoe Perches#
1011d311cd44SJoe Perches#		echo "checking commits $1..."
1012d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1013d311cd44SJoe Perches#		while read line ; do
1014d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1015d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1016d311cd44SJoe Perches#		done
1017d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
1018948b133aSHeinrich Schuchardt		$id = undef;
1019d311cd44SJoe Perches	} else {
1020d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1021d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1022d311cd44SJoe Perches	}
1023d311cd44SJoe Perches
1024d311cd44SJoe Perches	return ($id, $desc);
1025d311cd44SJoe Perches}
1026d311cd44SJoe Perches
10276c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
10280a920b5bSAndy Whitcroft
102900df344fSAndy Whitcroftmy @rawlines = ();
1030c2fdda0dSAndy Whitcroftmy @lines = ();
10313705ce5bSJoe Perchesmy @fixed = ();
1032d752fcc8SJoe Perchesmy @fixed_inserted = ();
1033d752fcc8SJoe Perchesmy @fixed_deleted = ();
1034194f66fcSJoe Perchesmy $fixlinenr = -1;
1035194f66fcSJoe Perches
10364a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10374a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10380f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
10394a593c34SDu, Changbin
10404a593c34SDu, Changbinif ($git) {
10414a593c34SDu, Changbin	my @commits = ();
10420dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
10434a593c34SDu, Changbin		my $git_range;
104428898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
104528898fd1SJoe Perches			$git_range = "-$2 $1";
10464a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
10474a593c34SDu, Changbin			$git_range = "$commit_expr";
10484a593c34SDu, Changbin		} else {
10490dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
10500dea9f1eSJoe Perches		}
1051dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10520dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
105328898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
105428898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10550dea9f1eSJoe Perches			my $sha1 = $1;
10560dea9f1eSJoe Perches			my $subject = $2;
10570dea9f1eSJoe Perches			unshift(@commits, $sha1);
10580dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10594a593c34SDu, Changbin		}
10604a593c34SDu, Changbin	}
10614a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10624a593c34SDu, Changbin	@ARGV = @commits;
10634a593c34SDu, Changbin}
10644a593c34SDu, Changbin
1065c2fdda0dSAndy Whitcroftmy $vname;
106698005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
10676c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
106821caa13cSAndy Whitcroft	my $FILE;
1069f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1070f5f61325SJoe Perches	my $oldfile = $file;
1071f5f61325SJoe Perches	$file = 1 if ($is_git_file);
10724a593c34SDu, Changbin	if ($git) {
10734a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10744a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10754a593c34SDu, Changbin	} elsif ($file) {
107621caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10776c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
107821caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
107921caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10806c72ffaaSAndy Whitcroft	} else {
108121caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10826c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10836c72ffaaSAndy Whitcroft	}
1084c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1085c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10864a593c34SDu, Changbin	} elsif ($git) {
10870dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1088c2fdda0dSAndy Whitcroft	} else {
1089c2fdda0dSAndy Whitcroft		$vname = $filename;
1090c2fdda0dSAndy Whitcroft	}
109121caa13cSAndy Whitcroft	while (<$FILE>) {
10920a920b5bSAndy Whitcroft		chomp;
109300df344fSAndy Whitcroft		push(@rawlines, $_);
1094c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
10956c72ffaaSAndy Whitcroft	}
109621caa13cSAndy Whitcroft	close($FILE);
1097d8469f16SJoe Perches
1098d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1099d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1100d8469f16SJoe Perches		print "$vname\n";
1101d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1102d8469f16SJoe Perches	}
1103d8469f16SJoe Perches
1104c2fdda0dSAndy Whitcroft	if (!process($filename)) {
11050a920b5bSAndy Whitcroft		$exit = 1;
11060a920b5bSAndy Whitcroft	}
110700df344fSAndy Whitcroft	@rawlines = ();
110813214adfSAndy Whitcroft	@lines = ();
11093705ce5bSJoe Perches	@fixed = ();
1110d752fcc8SJoe Perches	@fixed_inserted = ();
1111d752fcc8SJoe Perches	@fixed_deleted = ();
1112194f66fcSJoe Perches	$fixlinenr = -1;
1113485ff23eSAlex Dowad	@modifierListFile = ();
1114485ff23eSAlex Dowad	@typeListFile = ();
1115485ff23eSAlex Dowad	build_types();
1116f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
11170a920b5bSAndy Whitcroft}
11180a920b5bSAndy Whitcroft
1119d8469f16SJoe Perchesif (!$quiet) {
11203c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
11213c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
11223c816e49SJoe Perches
11235b57980dSJoe Perches	if (!$perl_version_ok) {
1124d8469f16SJoe Perches		print << "EOM"
1125d8469f16SJoe Perches
1126d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
11275b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1128d8469f16SJoe PerchesEOM
1129d8469f16SJoe Perches	}
1130d8469f16SJoe Perches	if ($exit) {
1131d8469f16SJoe Perches		print << "EOM"
1132d8469f16SJoe Perches
1133d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1134d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1135d8469f16SJoe PerchesEOM
1136d8469f16SJoe Perches	}
1137d8469f16SJoe Perches}
1138d8469f16SJoe Perches
11390a920b5bSAndy Whitcroftexit($exit);
11400a920b5bSAndy Whitcroft
11410a920b5bSAndy Whitcroftsub top_of_kernel_tree {
11426c72ffaaSAndy Whitcroft	my ($root) = @_;
11436c72ffaaSAndy Whitcroft
11446c72ffaaSAndy Whitcroft	my @tree_check = (
11456c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
11466c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
11476c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
11486c72ffaaSAndy Whitcroft	);
11496c72ffaaSAndy Whitcroft
11506c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
11516c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
11520a920b5bSAndy Whitcroft			return 0;
11530a920b5bSAndy Whitcroft		}
11546c72ffaaSAndy Whitcroft	}
11556c72ffaaSAndy Whitcroft	return 1;
11566c72ffaaSAndy Whitcroft}
11570a920b5bSAndy Whitcroft
115820112475SJoe Perchessub parse_email {
115920112475SJoe Perches	my ($formatted_email) = @_;
116020112475SJoe Perches
116120112475SJoe Perches	my $name = "";
1162*fccaebf0SDwaipayan Ray	my $quoted = "";
1163dfa05c28SJoe Perches	my $name_comment = "";
116420112475SJoe Perches	my $address = "";
116520112475SJoe Perches	my $comment = "";
116620112475SJoe Perches
116720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
116820112475SJoe Perches		$name = $1;
116920112475SJoe Perches		$address = $2;
117020112475SJoe Perches		$comment = $3 if defined $3;
117120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
117220112475SJoe Perches		$address = $1;
117320112475SJoe Perches		$comment = $2 if defined $2;
117420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
117520112475SJoe Perches		$address = $1;
117620112475SJoe Perches		$comment = $2 if defined $2;
117785e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
117820112475SJoe Perches		$name = $formatted_email;
11793705ce5bSJoe Perches		$name = trim($name);
118020112475SJoe Perches		$name =~ s/^\"|\"$//g;
118120112475SJoe Perches		# If there's a name left after stripping spaces and
118220112475SJoe Perches		# leading quotes, and the address doesn't have both
118320112475SJoe Perches		# leading and trailing angle brackets, the address
118420112475SJoe Perches		# is invalid. ie:
118520112475SJoe Perches		#   "joe smith [email protected]" bad
118620112475SJoe Perches		#   "joe smith <[email protected]" bad
118720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
118820112475SJoe Perches			$name = "";
118920112475SJoe Perches			$address = "";
119020112475SJoe Perches			$comment = "";
119120112475SJoe Perches		}
119220112475SJoe Perches	}
119320112475SJoe Perches
1194*fccaebf0SDwaipayan Ray	# Extract comments from names excluding quoted parts
1195*fccaebf0SDwaipayan Ray	# "John D. (Doe)" - Do not extract
1196*fccaebf0SDwaipayan Ray	if ($name =~ s/\"(.+)\"//) {
1197*fccaebf0SDwaipayan Ray		$quoted = $1;
1198dfa05c28SJoe Perches	}
1199*fccaebf0SDwaipayan Ray	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1200*fccaebf0SDwaipayan Ray		$name_comment .= trim($1);
1201*fccaebf0SDwaipayan Ray	}
1202*fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
1203*fccaebf0SDwaipayan Ray	$name = trim("$quoted $name");
1204*fccaebf0SDwaipayan Ray
12053705ce5bSJoe Perches	$address = trim($address);
120620112475SJoe Perches	$address =~ s/^\<|\>$//g;
1207*fccaebf0SDwaipayan Ray	$comment = trim($comment);
120820112475SJoe Perches
120920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
121020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
121120112475SJoe Perches		$name = "\"$name\"";
121220112475SJoe Perches	}
121320112475SJoe Perches
1214dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
121520112475SJoe Perches}
121620112475SJoe Perches
121720112475SJoe Perchessub format_email {
121848ca2d8aSDwaipayan Ray	my ($name, $name_comment, $address, $comment) = @_;
121920112475SJoe Perches
122020112475SJoe Perches	my $formatted_email;
122120112475SJoe Perches
1222*fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
12233705ce5bSJoe Perches	$address = trim($address);
1224*fccaebf0SDwaipayan Ray	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
122520112475SJoe Perches
122620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
122720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
122820112475SJoe Perches		$name = "\"$name\"";
122920112475SJoe Perches	}
123020112475SJoe Perches
1231*fccaebf0SDwaipayan Ray	$name_comment = trim($name_comment);
1232*fccaebf0SDwaipayan Ray	$name_comment = " $name_comment" if ($name_comment ne "");
1233*fccaebf0SDwaipayan Ray	$comment = trim($comment);
1234*fccaebf0SDwaipayan Ray	$comment = " $comment" if ($comment ne "");
1235*fccaebf0SDwaipayan Ray
123620112475SJoe Perches	if ("$name" eq "") {
123720112475SJoe Perches		$formatted_email = "$address";
123820112475SJoe Perches	} else {
123948ca2d8aSDwaipayan Ray		$formatted_email = "$name$name_comment <$address>";
124020112475SJoe Perches	}
124148ca2d8aSDwaipayan Ray	$formatted_email .= "$comment";
124220112475SJoe Perches	return $formatted_email;
124320112475SJoe Perches}
124420112475SJoe Perches
1245dfa05c28SJoe Perchessub reformat_email {
1246dfa05c28SJoe Perches	my ($email) = @_;
1247dfa05c28SJoe Perches
1248dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
124948ca2d8aSDwaipayan Ray	return format_email($email_name, $name_comment, $email_address, $comment);
1250dfa05c28SJoe Perches}
1251dfa05c28SJoe Perches
1252dfa05c28SJoe Perchessub same_email_addresses {
1253*fccaebf0SDwaipayan Ray	my ($email1, $email2) = @_;
1254dfa05c28SJoe Perches
1255dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1256dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1257dfa05c28SJoe Perches
125848ca2d8aSDwaipayan Ray	return $email1_name eq $email2_name &&
125948ca2d8aSDwaipayan Ray	       $email1_address eq $email2_address &&
126048ca2d8aSDwaipayan Ray	       $name1_comment eq $name2_comment &&
126148ca2d8aSDwaipayan Ray	       $comment1 eq $comment2;
126248ca2d8aSDwaipayan Ray}
1263dfa05c28SJoe Perches
1264d311cd44SJoe Perchessub which {
1265d311cd44SJoe Perches	my ($bin) = @_;
1266d311cd44SJoe Perches
1267d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1268d311cd44SJoe Perches		if (-e "$path/$bin") {
1269d311cd44SJoe Perches			return "$path/$bin";
1270d311cd44SJoe Perches		}
1271d311cd44SJoe Perches	}
1272d311cd44SJoe Perches
1273d311cd44SJoe Perches	return "";
1274d311cd44SJoe Perches}
1275d311cd44SJoe Perches
1276000d1cc1SJoe Perchessub which_conf {
1277000d1cc1SJoe Perches	my ($conf) = @_;
1278000d1cc1SJoe Perches
1279000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1280000d1cc1SJoe Perches		if (-e "$path/$conf") {
1281000d1cc1SJoe Perches			return "$path/$conf";
1282000d1cc1SJoe Perches		}
1283000d1cc1SJoe Perches	}
1284000d1cc1SJoe Perches
1285000d1cc1SJoe Perches	return "";
1286000d1cc1SJoe Perches}
1287000d1cc1SJoe Perches
12880a920b5bSAndy Whitcroftsub expand_tabs {
12890a920b5bSAndy Whitcroft	my ($str) = @_;
12900a920b5bSAndy Whitcroft
12910a920b5bSAndy Whitcroft	my $res = '';
12920a920b5bSAndy Whitcroft	my $n = 0;
12930a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12940a920b5bSAndy Whitcroft		if ($c eq "\t") {
12950a920b5bSAndy Whitcroft			$res .= ' ';
12960a920b5bSAndy Whitcroft			$n++;
1297713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
12980a920b5bSAndy Whitcroft				$res .= ' ';
12990a920b5bSAndy Whitcroft			}
13000a920b5bSAndy Whitcroft			next;
13010a920b5bSAndy Whitcroft		}
13020a920b5bSAndy Whitcroft		$res .= $c;
13030a920b5bSAndy Whitcroft		$n++;
13040a920b5bSAndy Whitcroft	}
13050a920b5bSAndy Whitcroft
13060a920b5bSAndy Whitcroft	return $res;
13070a920b5bSAndy Whitcroft}
13086c72ffaaSAndy Whitcroftsub copy_spacing {
1309773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
13106c72ffaaSAndy Whitcroft	return $res;
13116c72ffaaSAndy Whitcroft}
13120a920b5bSAndy Whitcroft
13134a0df2efSAndy Whitcroftsub line_stats {
13144a0df2efSAndy Whitcroft	my ($line) = @_;
13154a0df2efSAndy Whitcroft
13164a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
13174a0df2efSAndy Whitcroft	$line =~ s/^.//;
13184a0df2efSAndy Whitcroft	$line = expand_tabs($line);
13194a0df2efSAndy Whitcroft
13204a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
13214a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
13224a0df2efSAndy Whitcroft
13234a0df2efSAndy Whitcroft	return (length($line), length($white));
13244a0df2efSAndy Whitcroft}
13254a0df2efSAndy Whitcroft
1326773647a0SAndy Whitcroftmy $sanitise_quote = '';
1327773647a0SAndy Whitcroft
1328773647a0SAndy Whitcroftsub sanitise_line_reset {
1329773647a0SAndy Whitcroft	my ($in_comment) = @_;
1330773647a0SAndy Whitcroft
1331773647a0SAndy Whitcroft	if ($in_comment) {
1332773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1333773647a0SAndy Whitcroft	} else {
1334773647a0SAndy Whitcroft		$sanitise_quote = '';
1335773647a0SAndy Whitcroft	}
1336773647a0SAndy Whitcroft}
133700df344fSAndy Whitcroftsub sanitise_line {
133800df344fSAndy Whitcroft	my ($line) = @_;
133900df344fSAndy Whitcroft
134000df344fSAndy Whitcroft	my $res = '';
134100df344fSAndy Whitcroft	my $l = '';
134200df344fSAndy Whitcroft
1343c2fdda0dSAndy Whitcroft	my $qlen = 0;
1344773647a0SAndy Whitcroft	my $off = 0;
1345773647a0SAndy Whitcroft	my $c;
134600df344fSAndy Whitcroft
1347773647a0SAndy Whitcroft	# Always copy over the diff marker.
1348773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1349773647a0SAndy Whitcroft
1350773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1351773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1352773647a0SAndy Whitcroft
13538d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1354773647a0SAndy Whitcroft		# and end, all to $;.
1355773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1356773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1357773647a0SAndy Whitcroft
1358773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1359773647a0SAndy Whitcroft			$off++;
136000df344fSAndy Whitcroft			next;
1361773647a0SAndy Whitcroft		}
136281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1363773647a0SAndy Whitcroft			$sanitise_quote = '';
1364773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1365773647a0SAndy Whitcroft			$off++;
1366773647a0SAndy Whitcroft			next;
1367773647a0SAndy Whitcroft		}
1368113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1369113f04a8SDaniel Walker			$sanitise_quote = '//';
1370113f04a8SDaniel Walker
1371113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1372113f04a8SDaniel Walker			$off++;
1373113f04a8SDaniel Walker			next;
1374113f04a8SDaniel Walker		}
1375773647a0SAndy Whitcroft
1376773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1377773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1378773647a0SAndy Whitcroft		    $c eq "\\") {
1379773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1380773647a0SAndy Whitcroft			$off++;
1381773647a0SAndy Whitcroft			next;
1382773647a0SAndy Whitcroft		}
1383773647a0SAndy Whitcroft		# Regular quotes.
1384773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1385773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1386773647a0SAndy Whitcroft				$sanitise_quote = $c;
1387773647a0SAndy Whitcroft
1388773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1389773647a0SAndy Whitcroft				next;
1390773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1391773647a0SAndy Whitcroft				$sanitise_quote = '';
139200df344fSAndy Whitcroft			}
139300df344fSAndy Whitcroft		}
1394773647a0SAndy Whitcroft
1395fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1396773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1397773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1398113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1399113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1400773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1401773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
140200df344fSAndy Whitcroft		} else {
1403773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
140400df344fSAndy Whitcroft		}
1405c2fdda0dSAndy Whitcroft	}
1406c2fdda0dSAndy Whitcroft
1407113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1408113f04a8SDaniel Walker		$sanitise_quote = '';
1409113f04a8SDaniel Walker	}
1410113f04a8SDaniel Walker
1411c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1412c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1413c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1414c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1415c2fdda0dSAndy Whitcroft
1416c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1417c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1418c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1419c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1420c2fdda0dSAndy Whitcroft	}
1421c2fdda0dSAndy Whitcroft
1422dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1423dadf680dSJoe Perches		my $match = $1;
1424dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1425dadf680dSJoe Perches	}
1426dadf680dSJoe Perches
142700df344fSAndy Whitcroft	return $res;
142800df344fSAndy Whitcroft}
142900df344fSAndy Whitcroft
1430a6962d72SJoe Perchessub get_quoted_string {
1431a6962d72SJoe Perches	my ($line, $rawline) = @_;
1432a6962d72SJoe Perches
1433478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
143433acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1435a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1436a6962d72SJoe Perches}
1437a6962d72SJoe Perches
14388905a67cSAndy Whitcroftsub ctx_statement_block {
14398905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
14408905a67cSAndy Whitcroft	my $line = $linenr - 1;
14418905a67cSAndy Whitcroft	my $blk = '';
14428905a67cSAndy Whitcroft	my $soff = $off;
14438905a67cSAndy Whitcroft	my $coff = $off - 1;
1444773647a0SAndy Whitcroft	my $coff_set = 0;
14458905a67cSAndy Whitcroft
144613214adfSAndy Whitcroft	my $loff = 0;
144713214adfSAndy Whitcroft
14488905a67cSAndy Whitcroft	my $type = '';
14498905a67cSAndy Whitcroft	my $level = 0;
1450a2750645SAndy Whitcroft	my @stack = ();
1451cf655043SAndy Whitcroft	my $p;
14528905a67cSAndy Whitcroft	my $c;
14538905a67cSAndy Whitcroft	my $len = 0;
145413214adfSAndy Whitcroft
145513214adfSAndy Whitcroft	my $remainder;
14568905a67cSAndy Whitcroft	while (1) {
1457a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1458a2750645SAndy Whitcroft
1459773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
14608905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
14618905a67cSAndy Whitcroft		# context.
14628905a67cSAndy Whitcroft		if ($off >= $len) {
14638905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1464dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1465c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
14668905a67cSAndy Whitcroft				$remain--;
146713214adfSAndy Whitcroft				$loff = $len;
1468c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
14698905a67cSAndy Whitcroft				$len = length($blk);
14708905a67cSAndy Whitcroft				$line++;
14718905a67cSAndy Whitcroft				last;
14728905a67cSAndy Whitcroft			}
14738905a67cSAndy Whitcroft			# Bail if there is no further context.
14748905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
147513214adfSAndy Whitcroft			if ($off >= $len) {
14768905a67cSAndy Whitcroft				last;
14778905a67cSAndy Whitcroft			}
1478f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1479f74bd194SAndy Whitcroft				$level++;
1480f74bd194SAndy Whitcroft				$type = '#';
1481f74bd194SAndy Whitcroft			}
14828905a67cSAndy Whitcroft		}
1483cf655043SAndy Whitcroft		$p = $c;
14848905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
148513214adfSAndy Whitcroft		$remainder = substr($blk, $off);
14868905a67cSAndy Whitcroft
1487773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
14884635f4fbSAndy Whitcroft
14894635f4fbSAndy Whitcroft		# Handle nested #if/#else.
14904635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
14914635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
14924635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
14934635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14944635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14954635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14964635f4fbSAndy Whitcroft		}
14974635f4fbSAndy Whitcroft
14988905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14998905a67cSAndy Whitcroft		# outermost level.
15008905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
15018905a67cSAndy Whitcroft			last;
15028905a67cSAndy Whitcroft		}
15038905a67cSAndy Whitcroft
150413214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1505773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1506773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1507773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1508773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1509773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1510773647a0SAndy Whitcroft			$coff_set = 1;
1511773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1512773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
151313214adfSAndy Whitcroft		}
151413214adfSAndy Whitcroft
15158905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
15168905a67cSAndy Whitcroft			$level++;
15178905a67cSAndy Whitcroft			$type = '(';
15188905a67cSAndy Whitcroft		}
15198905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
15208905a67cSAndy Whitcroft			$level--;
15218905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
15228905a67cSAndy Whitcroft
15238905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
15248905a67cSAndy Whitcroft				$coff = $off;
1525773647a0SAndy Whitcroft				$coff_set = 1;
1526773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
15278905a67cSAndy Whitcroft			}
15288905a67cSAndy Whitcroft		}
15298905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
15308905a67cSAndy Whitcroft			$level++;
15318905a67cSAndy Whitcroft			$type = '{';
15328905a67cSAndy Whitcroft		}
15338905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
15348905a67cSAndy Whitcroft			$level--;
15358905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
15368905a67cSAndy Whitcroft
15378905a67cSAndy Whitcroft			if ($level == 0) {
1538b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1539b998e001SPatrick Pannuto					$off++;
1540b998e001SPatrick Pannuto				}
15418905a67cSAndy Whitcroft				last;
15428905a67cSAndy Whitcroft			}
15438905a67cSAndy Whitcroft		}
1544f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1545f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1546f74bd194SAndy Whitcroft			$level--;
1547f74bd194SAndy Whitcroft			$type = '';
1548f74bd194SAndy Whitcroft			$off++;
1549f74bd194SAndy Whitcroft			last;
1550f74bd194SAndy Whitcroft		}
15518905a67cSAndy Whitcroft		$off++;
15528905a67cSAndy Whitcroft	}
1553a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
155413214adfSAndy Whitcroft	if ($off == $len) {
1555a3bb97a7SAndy Whitcroft		$loff = $len + 1;
155613214adfSAndy Whitcroft		$line++;
155713214adfSAndy Whitcroft		$remain--;
155813214adfSAndy Whitcroft	}
15598905a67cSAndy Whitcroft
15608905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
15618905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
15628905a67cSAndy Whitcroft
15638905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
15648905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
15658905a67cSAndy Whitcroft
1566773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
156713214adfSAndy Whitcroft
156813214adfSAndy Whitcroft	return ($statement, $condition,
156913214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
157013214adfSAndy Whitcroft}
157113214adfSAndy Whitcroft
1572cf655043SAndy Whitcroftsub statement_lines {
1573cf655043SAndy Whitcroft	my ($stmt) = @_;
1574cf655043SAndy Whitcroft
1575cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1576cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1577cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1578cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1579cf655043SAndy Whitcroft
1580cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1581cf655043SAndy Whitcroft
1582cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1583cf655043SAndy Whitcroft}
1584cf655043SAndy Whitcroft
1585cf655043SAndy Whitcroftsub statement_rawlines {
1586cf655043SAndy Whitcroft	my ($stmt) = @_;
1587cf655043SAndy Whitcroft
1588cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1589cf655043SAndy Whitcroft
1590cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1591cf655043SAndy Whitcroft}
1592cf655043SAndy Whitcroft
1593cf655043SAndy Whitcroftsub statement_block_size {
1594cf655043SAndy Whitcroft	my ($stmt) = @_;
1595cf655043SAndy Whitcroft
1596cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1597cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1598cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1599cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1600cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1601cf655043SAndy Whitcroft
1602cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1603cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1604cf655043SAndy Whitcroft
1605cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1606cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1607cf655043SAndy Whitcroft
1608cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1609cf655043SAndy Whitcroft		return $stmt_lines;
1610cf655043SAndy Whitcroft	} else {
1611cf655043SAndy Whitcroft		return $stmt_statements;
1612cf655043SAndy Whitcroft	}
1613cf655043SAndy Whitcroft}
1614cf655043SAndy Whitcroft
161513214adfSAndy Whitcroftsub ctx_statement_full {
161613214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
161713214adfSAndy Whitcroft	my ($statement, $condition, $level);
161813214adfSAndy Whitcroft
161913214adfSAndy Whitcroft	my (@chunks);
162013214adfSAndy Whitcroft
1621cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
162213214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
162313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1624773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
162513214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1626cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1627cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1628cf655043SAndy Whitcroft	}
1629cf655043SAndy Whitcroft
1630cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1631cf655043SAndy Whitcroft	# could continue the statement.
1632cf655043SAndy Whitcroft	for (;;) {
163313214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
163413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1635cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1636773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1637cf655043SAndy Whitcroft		#print "C: push\n";
1638cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
163913214adfSAndy Whitcroft	}
164013214adfSAndy Whitcroft
164113214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
16428905a67cSAndy Whitcroft}
16438905a67cSAndy Whitcroft
16444a0df2efSAndy Whitcroftsub ctx_block_get {
1645f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
16464a0df2efSAndy Whitcroft	my $line;
16474a0df2efSAndy Whitcroft	my $start = $linenr - 1;
16484a0df2efSAndy Whitcroft	my $blk = '';
16494a0df2efSAndy Whitcroft	my @o;
16504a0df2efSAndy Whitcroft	my @c;
16514a0df2efSAndy Whitcroft	my @res = ();
16524a0df2efSAndy Whitcroft
1653f0a594c1SAndy Whitcroft	my $level = 0;
16544635f4fbSAndy Whitcroft	my @stack = ($level);
165500df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
165600df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
165700df344fSAndy Whitcroft		$remain--;
165800df344fSAndy Whitcroft
165900df344fSAndy Whitcroft		$blk .= $rawlines[$line];
16604635f4fbSAndy Whitcroft
16614635f4fbSAndy Whitcroft		# Handle nested #if/#else.
166201464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
16634635f4fbSAndy Whitcroft			push(@stack, $level);
166401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
16654635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
166601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
16674635f4fbSAndy Whitcroft			$level = pop(@stack);
16684635f4fbSAndy Whitcroft		}
16694635f4fbSAndy Whitcroft
167001464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1671f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1672f0a594c1SAndy Whitcroft			if ($off > 0) {
1673f0a594c1SAndy Whitcroft				$off--;
1674f0a594c1SAndy Whitcroft				next;
1675f0a594c1SAndy Whitcroft			}
16764a0df2efSAndy Whitcroft
1677f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1678f0a594c1SAndy Whitcroft				$level--;
1679f0a594c1SAndy Whitcroft				last if ($level == 0);
1680f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1681f0a594c1SAndy Whitcroft				$level++;
1682f0a594c1SAndy Whitcroft			}
1683f0a594c1SAndy Whitcroft		}
16844a0df2efSAndy Whitcroft
1685f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
168600df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
16874a0df2efSAndy Whitcroft		}
16884a0df2efSAndy Whitcroft
1689f0a594c1SAndy Whitcroft		last if ($level == 0);
16904a0df2efSAndy Whitcroft	}
16914a0df2efSAndy Whitcroft
1692f0a594c1SAndy Whitcroft	return ($level, @res);
16934a0df2efSAndy Whitcroft}
16944a0df2efSAndy Whitcroftsub ctx_block_outer {
16954a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16964a0df2efSAndy Whitcroft
1697f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1698f0a594c1SAndy Whitcroft	return @r;
16994a0df2efSAndy Whitcroft}
17004a0df2efSAndy Whitcroftsub ctx_block {
17014a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
17024a0df2efSAndy Whitcroft
1703f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1704f0a594c1SAndy Whitcroft	return @r;
1705653d4876SAndy Whitcroft}
1706653d4876SAndy Whitcroftsub ctx_statement {
1707f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1708f0a594c1SAndy Whitcroft
1709f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1710f0a594c1SAndy Whitcroft	return @r;
1711f0a594c1SAndy Whitcroft}
1712f0a594c1SAndy Whitcroftsub ctx_block_level {
1713653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1714653d4876SAndy Whitcroft
1715f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
17164a0df2efSAndy Whitcroft}
17179c0ca6f9SAndy Whitcroftsub ctx_statement_level {
17189c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
17199c0ca6f9SAndy Whitcroft
17209c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
17219c0ca6f9SAndy Whitcroft}
17224a0df2efSAndy Whitcroft
17234a0df2efSAndy Whitcroftsub ctx_locate_comment {
17244a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17254a0df2efSAndy Whitcroft
1726a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1727a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1728a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1729a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1730a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1731a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1732a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1733a55ee0ccSJoe Perches
17344a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1735a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
17364a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
17374a0df2efSAndy Whitcroft
17384a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
17394a0df2efSAndy Whitcroft	# comment.
17404a0df2efSAndy Whitcroft	my $in_comment = 0;
17414a0df2efSAndy Whitcroft	$current_comment = '';
17424a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
174300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
174400df344fSAndy Whitcroft		#warn "           $line\n";
17454a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
17464a0df2efSAndy Whitcroft			$in_comment = 1;
17474a0df2efSAndy Whitcroft		}
17484a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
17494a0df2efSAndy Whitcroft			$in_comment = 1;
17504a0df2efSAndy Whitcroft		}
17514a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
17524a0df2efSAndy Whitcroft			$current_comment = '';
17534a0df2efSAndy Whitcroft		}
17544a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
17554a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
17564a0df2efSAndy Whitcroft			$in_comment = 0;
17574a0df2efSAndy Whitcroft		}
17584a0df2efSAndy Whitcroft	}
17594a0df2efSAndy Whitcroft
17604a0df2efSAndy Whitcroft	chomp($current_comment);
17614a0df2efSAndy Whitcroft	return($current_comment);
17624a0df2efSAndy Whitcroft}
17634a0df2efSAndy Whitcroftsub ctx_has_comment {
17644a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17654a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
17664a0df2efSAndy Whitcroft
176700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
17684a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
17694a0df2efSAndy Whitcroft
17704a0df2efSAndy Whitcroft	return ($cmt ne '');
17714a0df2efSAndy Whitcroft}
17724a0df2efSAndy Whitcroft
17734d001e4dSAndy Whitcroftsub raw_line {
17744d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
17754d001e4dSAndy Whitcroft
17764d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
17774d001e4dSAndy Whitcroft	$cnt++;
17784d001e4dSAndy Whitcroft
17794d001e4dSAndy Whitcroft	my $line;
17804d001e4dSAndy Whitcroft	while ($cnt) {
17814d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
17824d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
17834d001e4dSAndy Whitcroft		$cnt--;
17844d001e4dSAndy Whitcroft	}
17854d001e4dSAndy Whitcroft
17864d001e4dSAndy Whitcroft	return $line;
17874d001e4dSAndy Whitcroft}
17884d001e4dSAndy Whitcroft
17892a9f9d85STobin C. Hardingsub get_stat_real {
17902a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
17912a9f9d85STobin C. Harding
17922a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
17932a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
17942a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
17952a9f9d85STobin C. Harding	}
17962a9f9d85STobin C. Harding
17972a9f9d85STobin C. Harding	return $stat_real;
17982a9f9d85STobin C. Harding}
17992a9f9d85STobin C. Harding
1800e3d95a2aSTobin C. Hardingsub get_stat_here {
1801e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1802e3d95a2aSTobin C. Harding
1803e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1804e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1805e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1806e3d95a2aSTobin C. Harding	}
1807e3d95a2aSTobin C. Harding
1808e3d95a2aSTobin C. Harding	return $herectx;
1809e3d95a2aSTobin C. Harding}
1810e3d95a2aSTobin C. Harding
18110a920b5bSAndy Whitcroftsub cat_vet {
18120a920b5bSAndy Whitcroft	my ($vet) = @_;
18139c0ca6f9SAndy Whitcroft	my ($res, $coded);
18140a920b5bSAndy Whitcroft
18159c0ca6f9SAndy Whitcroft	$res = '';
18166c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
18176c72ffaaSAndy Whitcroft		$res .= $1;
18186c72ffaaSAndy Whitcroft		if ($2 ne '') {
18199c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
18206c72ffaaSAndy Whitcroft			$res .= $coded;
18216c72ffaaSAndy Whitcroft		}
18229c0ca6f9SAndy Whitcroft	}
18239c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
18240a920b5bSAndy Whitcroft
18259c0ca6f9SAndy Whitcroft	return $res;
18260a920b5bSAndy Whitcroft}
18270a920b5bSAndy Whitcroft
1828c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1829cf655043SAndy Whitcroftmy $av_pending;
1830c2fdda0dSAndy Whitcroftmy @av_paren_type;
18311f65f947SAndy Whitcroftmy $av_pend_colon;
1832c2fdda0dSAndy Whitcroft
1833c2fdda0dSAndy Whitcroftsub annotate_reset {
1834c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1835cf655043SAndy Whitcroft	$av_pending = '_';
1836cf655043SAndy Whitcroft	@av_paren_type = ('E');
18371f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1838c2fdda0dSAndy Whitcroft}
1839c2fdda0dSAndy Whitcroft
18406c72ffaaSAndy Whitcroftsub annotate_values {
18416c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
18426c72ffaaSAndy Whitcroft
18436c72ffaaSAndy Whitcroft	my $res;
18441f65f947SAndy Whitcroft	my $var = '_' x length($stream);
18456c72ffaaSAndy Whitcroft	my $cur = $stream;
18466c72ffaaSAndy Whitcroft
1847c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
18486c72ffaaSAndy Whitcroft
18496c72ffaaSAndy Whitcroft	while (length($cur)) {
1850773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1851cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1852171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
18536c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1854c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1855c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1856cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1857c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
18586c72ffaaSAndy Whitcroft			}
18596c72ffaaSAndy Whitcroft
1860c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
18619446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
18629446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1863addcdceaSAndy Whitcroft			$type = 'c';
18649446ef56SAndy Whitcroft
1865e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1866c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
18676c72ffaaSAndy Whitcroft			$type = 'T';
18686c72ffaaSAndy Whitcroft
1869389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1870389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1871389a2fe5SAndy Whitcroft			$type = 'T';
1872389a2fe5SAndy Whitcroft
1873c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1874171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1875c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1876171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1877171ae1a4SAndy Whitcroft			if ($2 ne '') {
1878cf655043SAndy Whitcroft				$av_pending = 'N';
1879171ae1a4SAndy Whitcroft			}
1880171ae1a4SAndy Whitcroft			$type = 'E';
1881171ae1a4SAndy Whitcroft
1882c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1883171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1884171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1885171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
18866c72ffaaSAndy Whitcroft
1887c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1888cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1889c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1890cf655043SAndy Whitcroft
1891cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1892cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1893171ae1a4SAndy Whitcroft			$type = 'E';
1894cf655043SAndy Whitcroft
1895c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1896cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1897cf655043SAndy Whitcroft			$av_preprocessor = 1;
1898cf655043SAndy Whitcroft
1899cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1900cf655043SAndy Whitcroft
1901171ae1a4SAndy Whitcroft			$type = 'E';
1902cf655043SAndy Whitcroft
1903c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1904cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1905cf655043SAndy Whitcroft
1906cf655043SAndy Whitcroft			$av_preprocessor = 1;
1907cf655043SAndy Whitcroft
1908cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1909cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1910cf655043SAndy Whitcroft			pop(@av_paren_type);
1911cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1912171ae1a4SAndy Whitcroft			$type = 'E';
19136c72ffaaSAndy Whitcroft
19146c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1915c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
19166c72ffaaSAndy Whitcroft
1917171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1918171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1919171ae1a4SAndy Whitcroft			$av_pending = $type;
1920171ae1a4SAndy Whitcroft			$type = 'N';
1921171ae1a4SAndy Whitcroft
19226c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1923c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
19246c72ffaaSAndy Whitcroft			if (defined $2) {
1925cf655043SAndy Whitcroft				$av_pending = 'V';
19266c72ffaaSAndy Whitcroft			}
19276c72ffaaSAndy Whitcroft			$type = 'N';
19286c72ffaaSAndy Whitcroft
192914b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1930c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
193114b111c1SAndy Whitcroft			$av_pending = 'E';
19326c72ffaaSAndy Whitcroft			$type = 'N';
19336c72ffaaSAndy Whitcroft
19341f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
19351f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
19361f65f947SAndy Whitcroft			$av_pend_colon = 'C';
19371f65f947SAndy Whitcroft			$type = 'N';
19381f65f947SAndy Whitcroft
193914b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1940c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
19416c72ffaaSAndy Whitcroft			$type = 'N';
19426c72ffaaSAndy Whitcroft
19436c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1944c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1945cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1946cf655043SAndy Whitcroft			$av_pending = '_';
19476c72ffaaSAndy Whitcroft			$type = 'N';
19486c72ffaaSAndy Whitcroft
19496c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1950cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1951cf655043SAndy Whitcroft			if ($new_type ne '_') {
1952cf655043SAndy Whitcroft				$type = $new_type;
1953c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1954c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
19556c72ffaaSAndy Whitcroft			} else {
1956c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
19576c72ffaaSAndy Whitcroft			}
19586c72ffaaSAndy Whitcroft
1959c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1960c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1961c8cb2ca3SAndy Whitcroft			$type = 'V';
1962cf655043SAndy Whitcroft			$av_pending = 'V';
19636c72ffaaSAndy Whitcroft
19648e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
19658e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
19661f65f947SAndy Whitcroft				$av_pend_colon = 'B';
19678e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
19688e761b04SAndy Whitcroft				$av_pend_colon = 'L';
19691f65f947SAndy Whitcroft			}
19701f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
19711f65f947SAndy Whitcroft			$type = 'V';
19721f65f947SAndy Whitcroft
19736c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1974c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
19756c72ffaaSAndy Whitcroft			$type = 'V';
19766c72ffaaSAndy Whitcroft
19776c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1978c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
19796c72ffaaSAndy Whitcroft			$type = 'N';
19806c72ffaaSAndy Whitcroft
1981cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1982c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
198313214adfSAndy Whitcroft			$type = 'E';
19841f65f947SAndy Whitcroft			$av_pend_colon = 'O';
198513214adfSAndy Whitcroft
19868e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
19878e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
19888e761b04SAndy Whitcroft			$type = 'C';
19898e761b04SAndy Whitcroft
19901f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
19911f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
19921f65f947SAndy Whitcroft			$type = 'N';
19931f65f947SAndy Whitcroft
19941f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
19951f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
19961f65f947SAndy Whitcroft
19971f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
19981f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
19991f65f947SAndy Whitcroft				$type = 'E';
20001f65f947SAndy Whitcroft			} else {
20011f65f947SAndy Whitcroft				$type = 'N';
20021f65f947SAndy Whitcroft			}
20031f65f947SAndy Whitcroft			$av_pend_colon = 'O';
20041f65f947SAndy Whitcroft
20058e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
200613214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
20076c72ffaaSAndy Whitcroft			$type = 'N';
20086c72ffaaSAndy Whitcroft
20090d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
201074048ed8SAndy Whitcroft			my $variant;
201174048ed8SAndy Whitcroft
201274048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
201374048ed8SAndy Whitcroft			if ($type eq 'V') {
201474048ed8SAndy Whitcroft				$variant = 'B';
201574048ed8SAndy Whitcroft			} else {
201674048ed8SAndy Whitcroft				$variant = 'U';
201774048ed8SAndy Whitcroft			}
201874048ed8SAndy Whitcroft
201974048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
202074048ed8SAndy Whitcroft			$type = 'N';
202174048ed8SAndy Whitcroft
20226c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2023c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
20246c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
20256c72ffaaSAndy Whitcroft				$type = 'N';
20266c72ffaaSAndy Whitcroft			}
20276c72ffaaSAndy Whitcroft
20286c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2029c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
20306c72ffaaSAndy Whitcroft		}
20316c72ffaaSAndy Whitcroft		if (defined $1) {
20326c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
20336c72ffaaSAndy Whitcroft			$res .= $type x length($1);
20346c72ffaaSAndy Whitcroft		}
20356c72ffaaSAndy Whitcroft	}
20366c72ffaaSAndy Whitcroft
20371f65f947SAndy Whitcroft	return ($res, $var);
20386c72ffaaSAndy Whitcroft}
20396c72ffaaSAndy Whitcroft
20408905a67cSAndy Whitcroftsub possible {
204113214adfSAndy Whitcroft	my ($possible, $line) = @_;
20429a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
20430776e594SAndy Whitcroft		^(?:
20440776e594SAndy Whitcroft			$Modifier|
20450776e594SAndy Whitcroft			$Storage|
20460776e594SAndy Whitcroft			$Type|
20479a974fdbSAndy Whitcroft			DEFINE_\S+
20489a974fdbSAndy Whitcroft		)$|
20499a974fdbSAndy Whitcroft		^(?:
20500776e594SAndy Whitcroft			goto|
20510776e594SAndy Whitcroft			return|
20520776e594SAndy Whitcroft			case|
20530776e594SAndy Whitcroft			else|
20540776e594SAndy Whitcroft			asm|__asm__|
205589a88353SAndy Whitcroft			do|
205689a88353SAndy Whitcroft			\#|
205789a88353SAndy Whitcroft			\#\#|
20589a974fdbSAndy Whitcroft		)(?:\s|$)|
20590776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
20609a974fdbSAndy Whitcroft	    )}x;
20619a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
20629a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2063c45dcabdSAndy Whitcroft		# Check for modifiers.
2064c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2065c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2066c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2067c45dcabdSAndy Whitcroft
2068c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2069c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2070d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
20719a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2072d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2073485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2074d2506586SAndy Whitcroft				}
20759a974fdbSAndy Whitcroft			}
2076c45dcabdSAndy Whitcroft
2077c45dcabdSAndy Whitcroft		} else {
207813214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2079485ff23eSAlex Dowad			push(@typeListFile, $possible);
2080c45dcabdSAndy Whitcroft		}
20818905a67cSAndy Whitcroft		build_types();
20820776e594SAndy Whitcroft	} else {
20830776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
20848905a67cSAndy Whitcroft	}
20858905a67cSAndy Whitcroft}
20868905a67cSAndy Whitcroft
20876c72ffaaSAndy Whitcroftmy $prefix = '';
20886c72ffaaSAndy Whitcroft
2089000d1cc1SJoe Perchessub show_type {
2090cbec18afSJoe Perches	my ($type) = @_;
209191bfe484SJoe Perches
2092522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2093522b837cSAlexey Dobriyan
2094cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2095cbec18afSJoe Perches
2096cbec18afSJoe Perches	return !defined $ignore_type{$type};
2097000d1cc1SJoe Perches}
2098000d1cc1SJoe Perches
2099f0a594c1SAndy Whitcroftsub report {
2100cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2101cbec18afSJoe Perches
2102cbec18afSJoe Perches	if (!show_type($type) ||
2103cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2104773647a0SAndy Whitcroft		return 0;
2105773647a0SAndy Whitcroft	}
210657230297SJoe Perches	my $output = '';
2107737c0767SJohn Brooks	if ($color) {
210857230297SJoe Perches		if ($level eq 'ERROR') {
210957230297SJoe Perches			$output .= RED;
211057230297SJoe Perches		} elsif ($level eq 'WARNING') {
211157230297SJoe Perches			$output .= YELLOW;
2112000d1cc1SJoe Perches		} else {
211357230297SJoe Perches			$output .= GREEN;
2114000d1cc1SJoe Perches		}
211557230297SJoe Perches	}
211657230297SJoe Perches	$output .= $prefix . $level . ':';
211757230297SJoe Perches	if ($show_types) {
2118737c0767SJohn Brooks		$output .= BLUE if ($color);
211957230297SJoe Perches		$output .= "$type:";
212057230297SJoe Perches	}
2121737c0767SJohn Brooks	$output .= RESET if ($color);
212257230297SJoe Perches	$output .= ' ' . $msg . "\n";
212334d8815fSJoe Perches
212434d8815fSJoe Perches	if ($showfile) {
212534d8815fSJoe Perches		my @lines = split("\n", $output, -1);
212634d8815fSJoe Perches		splice(@lines, 1, 1);
212734d8815fSJoe Perches		$output = join("\n", @lines);
212834d8815fSJoe Perches	}
212957230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
21308905a67cSAndy Whitcroft
213157230297SJoe Perches	push(our @report, $output);
2132773647a0SAndy Whitcroft
2133773647a0SAndy Whitcroft	return 1;
2134f0a594c1SAndy Whitcroft}
2135cbec18afSJoe Perches
2136f0a594c1SAndy Whitcroftsub report_dump {
213713214adfSAndy Whitcroft	our @report;
2138f0a594c1SAndy Whitcroft}
2139000d1cc1SJoe Perches
2140d752fcc8SJoe Perchessub fixup_current_range {
2141d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2142d752fcc8SJoe Perches
2143d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2144d752fcc8SJoe Perches		my $o = $1;
2145d752fcc8SJoe Perches		my $l = $2;
2146d752fcc8SJoe Perches		my $no = $o + $offset;
2147d752fcc8SJoe Perches		my $nl = $l + $length;
2148d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2149d752fcc8SJoe Perches	}
2150d752fcc8SJoe Perches}
2151d752fcc8SJoe Perches
2152d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2153d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2154d752fcc8SJoe Perches
2155d752fcc8SJoe Perches	my $range_last_linenr = 0;
2156d752fcc8SJoe Perches	my $delta_offset = 0;
2157d752fcc8SJoe Perches
2158d752fcc8SJoe Perches	my $old_linenr = 0;
2159d752fcc8SJoe Perches	my $new_linenr = 0;
2160d752fcc8SJoe Perches
2161d752fcc8SJoe Perches	my $next_insert = 0;
2162d752fcc8SJoe Perches	my $next_delete = 0;
2163d752fcc8SJoe Perches
2164d752fcc8SJoe Perches	my @lines = ();
2165d752fcc8SJoe Perches
2166d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2167d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2168d752fcc8SJoe Perches
2169d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2170d752fcc8SJoe Perches		my $save_line = 1;
2171d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2172323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2173d752fcc8SJoe Perches			$delta_offset = 0;
2174d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2175d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2176d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2177d752fcc8SJoe Perches		}
2178d752fcc8SJoe Perches
2179d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2180d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2181d752fcc8SJoe Perches			$save_line = 0;
2182d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2183d752fcc8SJoe Perches		}
2184d752fcc8SJoe Perches
2185d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2186d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2187d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2188d752fcc8SJoe Perches			$new_linenr++;
2189d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2190d752fcc8SJoe Perches		}
2191d752fcc8SJoe Perches
2192d752fcc8SJoe Perches		if ($save_line) {
2193d752fcc8SJoe Perches			push(@lines, $line);
2194d752fcc8SJoe Perches			$new_linenr++;
2195d752fcc8SJoe Perches		}
2196d752fcc8SJoe Perches
2197d752fcc8SJoe Perches		$old_linenr++;
2198d752fcc8SJoe Perches	}
2199d752fcc8SJoe Perches
2200d752fcc8SJoe Perches	return @lines;
2201d752fcc8SJoe Perches}
2202d752fcc8SJoe Perches
2203f2d7e4d4SJoe Perchessub fix_insert_line {
2204f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2205f2d7e4d4SJoe Perches
2206f2d7e4d4SJoe Perches	my $inserted = {
2207f2d7e4d4SJoe Perches		LINENR => $linenr,
2208f2d7e4d4SJoe Perches		LINE => $line,
2209f2d7e4d4SJoe Perches	};
2210f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2211f2d7e4d4SJoe Perches}
2212f2d7e4d4SJoe Perches
2213f2d7e4d4SJoe Perchessub fix_delete_line {
2214f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2215f2d7e4d4SJoe Perches
2216f2d7e4d4SJoe Perches	my $deleted = {
2217f2d7e4d4SJoe Perches		LINENR => $linenr,
2218f2d7e4d4SJoe Perches		LINE => $line,
2219f2d7e4d4SJoe Perches	};
2220f2d7e4d4SJoe Perches
2221f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2222f2d7e4d4SJoe Perches}
2223f2d7e4d4SJoe Perches
2224de7d4f0eSAndy Whitcroftsub ERROR {
2225cbec18afSJoe Perches	my ($type, $msg) = @_;
2226cbec18afSJoe Perches
2227cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2228de7d4f0eSAndy Whitcroft		our $clean = 0;
22296c72ffaaSAndy Whitcroft		our $cnt_error++;
22303705ce5bSJoe Perches		return 1;
2231de7d4f0eSAndy Whitcroft	}
22323705ce5bSJoe Perches	return 0;
2233773647a0SAndy Whitcroft}
2234de7d4f0eSAndy Whitcroftsub WARN {
2235cbec18afSJoe Perches	my ($type, $msg) = @_;
2236cbec18afSJoe Perches
2237cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2238de7d4f0eSAndy Whitcroft		our $clean = 0;
22396c72ffaaSAndy Whitcroft		our $cnt_warn++;
22403705ce5bSJoe Perches		return 1;
2241de7d4f0eSAndy Whitcroft	}
22423705ce5bSJoe Perches	return 0;
2243773647a0SAndy Whitcroft}
2244de7d4f0eSAndy Whitcroftsub CHK {
2245cbec18afSJoe Perches	my ($type, $msg) = @_;
2246cbec18afSJoe Perches
2247cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2248de7d4f0eSAndy Whitcroft		our $clean = 0;
22496c72ffaaSAndy Whitcroft		our $cnt_chk++;
22503705ce5bSJoe Perches		return 1;
22516c72ffaaSAndy Whitcroft	}
22523705ce5bSJoe Perches	return 0;
2253de7d4f0eSAndy Whitcroft}
2254de7d4f0eSAndy Whitcroft
22556ecd9674SAndy Whitcroftsub check_absolute_file {
22566ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
22576ecd9674SAndy Whitcroft	my $file = $absolute;
22586ecd9674SAndy Whitcroft
22596ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
22606ecd9674SAndy Whitcroft
22616ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
22626ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
22636ecd9674SAndy Whitcroft		if (-f "$root/$file") {
22646ecd9674SAndy Whitcroft			##print "file<$file>\n";
22656ecd9674SAndy Whitcroft			last;
22666ecd9674SAndy Whitcroft		}
22676ecd9674SAndy Whitcroft	}
22686ecd9674SAndy Whitcroft	if (! -f _)  {
22696ecd9674SAndy Whitcroft		return 0;
22706ecd9674SAndy Whitcroft	}
22716ecd9674SAndy Whitcroft
22726ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
22736ecd9674SAndy Whitcroft	my $prefix = $absolute;
22746ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
22756ecd9674SAndy Whitcroft
22766ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
22776ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2278000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2279000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
22806ecd9674SAndy Whitcroft	}
22816ecd9674SAndy Whitcroft}
22826ecd9674SAndy Whitcroft
22833705ce5bSJoe Perchessub trim {
22843705ce5bSJoe Perches	my ($string) = @_;
22853705ce5bSJoe Perches
2286b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2287b34c648bSJoe Perches
2288b34c648bSJoe Perches	return $string;
2289b34c648bSJoe Perches}
2290b34c648bSJoe Perches
2291b34c648bSJoe Perchessub ltrim {
2292b34c648bSJoe Perches	my ($string) = @_;
2293b34c648bSJoe Perches
2294b34c648bSJoe Perches	$string =~ s/^\s+//;
2295b34c648bSJoe Perches
2296b34c648bSJoe Perches	return $string;
2297b34c648bSJoe Perches}
2298b34c648bSJoe Perches
2299b34c648bSJoe Perchessub rtrim {
2300b34c648bSJoe Perches	my ($string) = @_;
2301b34c648bSJoe Perches
2302b34c648bSJoe Perches	$string =~ s/\s+$//;
23033705ce5bSJoe Perches
23043705ce5bSJoe Perches	return $string;
23053705ce5bSJoe Perches}
23063705ce5bSJoe Perches
230752ea8506SJoe Perchessub string_find_replace {
230852ea8506SJoe Perches	my ($string, $find, $replace) = @_;
230952ea8506SJoe Perches
231052ea8506SJoe Perches	$string =~ s/$find/$replace/g;
231152ea8506SJoe Perches
231252ea8506SJoe Perches	return $string;
231352ea8506SJoe Perches}
231452ea8506SJoe Perches
23153705ce5bSJoe Perchessub tabify {
23163705ce5bSJoe Perches	my ($leading) = @_;
23173705ce5bSJoe Perches
2318713a09deSAntonio Borneo	my $source_indent = $tabsize;
23193705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
23203705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
23213705ce5bSJoe Perches
23223705ce5bSJoe Perches	#convert leading spaces to tabs
23233705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
23243705ce5bSJoe Perches	#Remove spaces before a tab
23253705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
23263705ce5bSJoe Perches
23273705ce5bSJoe Perches	return "$leading";
23283705ce5bSJoe Perches}
23293705ce5bSJoe Perches
2330d1fe9c09SJoe Perchessub pos_last_openparen {
2331d1fe9c09SJoe Perches	my ($line) = @_;
2332d1fe9c09SJoe Perches
2333d1fe9c09SJoe Perches	my $pos = 0;
2334d1fe9c09SJoe Perches
2335d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2336d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2337d1fe9c09SJoe Perches
2338d1fe9c09SJoe Perches	my $last_openparen = 0;
2339d1fe9c09SJoe Perches
2340d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2341d1fe9c09SJoe Perches		return -1;
2342d1fe9c09SJoe Perches	}
2343d1fe9c09SJoe Perches
2344d1fe9c09SJoe Perches	my $len = length($line);
2345d1fe9c09SJoe Perches
2346d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2347d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2348d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2349d1fe9c09SJoe Perches			$pos += length($1) - 1;
2350d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2351d1fe9c09SJoe Perches			$last_openparen = $pos;
2352d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2353d1fe9c09SJoe Perches			last;
2354d1fe9c09SJoe Perches		}
2355d1fe9c09SJoe Perches	}
2356d1fe9c09SJoe Perches
235791cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2358d1fe9c09SJoe Perches}
2359d1fe9c09SJoe Perches
2360f36d3eb8SJoe Perchessub get_raw_comment {
2361f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2362f36d3eb8SJoe Perches	my $comment = '';
2363f36d3eb8SJoe Perches
2364f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2365f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2366f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2367f36d3eb8SJoe Perches		}
2368f36d3eb8SJoe Perches	}
2369f36d3eb8SJoe Perches
2370f36d3eb8SJoe Perches	return $comment;
2371f36d3eb8SJoe Perches}
2372f36d3eb8SJoe Perches
23730a920b5bSAndy Whitcroftsub process {
23740a920b5bSAndy Whitcroft	my $filename = shift;
23750a920b5bSAndy Whitcroft
23760a920b5bSAndy Whitcroft	my $linenr=0;
23770a920b5bSAndy Whitcroft	my $prevline="";
2378c2fdda0dSAndy Whitcroft	my $prevrawline="";
23790a920b5bSAndy Whitcroft	my $stashline="";
2380c2fdda0dSAndy Whitcroft	my $stashrawline="";
23810a920b5bSAndy Whitcroft
23824a0df2efSAndy Whitcroft	my $length;
23830a920b5bSAndy Whitcroft	my $indent;
23840a920b5bSAndy Whitcroft	my $previndent=0;
23850a920b5bSAndy Whitcroft	my $stashindent=0;
23860a920b5bSAndy Whitcroft
2387de7d4f0eSAndy Whitcroft	our $clean = 1;
23880a920b5bSAndy Whitcroft	my $signoff = 0;
2389cd261496SGeert Uytterhoeven	my $author = '';
2390cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
239148ca2d8aSDwaipayan Ray	my $author_sob = '';
23920a920b5bSAndy Whitcroft	my $is_patch = 0;
2393133712a2SRob Herring	my $is_binding_patch = -1;
239429ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
239515662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
239644d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2397ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2398490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2399bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
24002a076f40SJoe Perches	my $commit_log_long_line = 0;
2401e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
240213f1937eSJoe Perches	my $reported_maintainer_file = 0;
2403fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2404fa64205dSPasi Savanainen
2405365dd4eaSJoe Perches	my $last_blank_line = 0;
24065e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2407365dd4eaSJoe Perches
240813214adfSAndy Whitcroft	our @report = ();
24096c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
24106c72ffaaSAndy Whitcroft	our $cnt_error = 0;
24116c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
24126c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
24136c72ffaaSAndy Whitcroft
24140a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
24150a920b5bSAndy Whitcroft	my $realfile = '';
24160a920b5bSAndy Whitcroft	my $realline = 0;
24170a920b5bSAndy Whitcroft	my $realcnt = 0;
24180a920b5bSAndy Whitcroft	my $here = '';
241977cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
24200a920b5bSAndy Whitcroft	my $in_comment = 0;
2421c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
24220a920b5bSAndy Whitcroft	my $first_line = 0;
24231e855726SWolfram Sang	my $p1_prefix = '';
24240a920b5bSAndy Whitcroft
242513214adfSAndy Whitcroft	my $prev_values = 'E';
242613214adfSAndy Whitcroft
242713214adfSAndy Whitcroft	# suppression flags
2428773647a0SAndy Whitcroft	my %suppress_ifbraces;
2429170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
24302b474a1aSAndy Whitcroft	my %suppress_export;
24313e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2432653d4876SAndy Whitcroft
24337e51f197SJoe Perches	my %signatures = ();
2434323c1260SJoe Perches
2435c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2436de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2437c2fdda0dSAndy Whitcroft	#
2438de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2439de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2440773647a0SAndy Whitcroft
2441d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2442d8b07710SJoe Perches
24439f3a8992SRob Herring	my $checklicenseline = 1;
24449f3a8992SRob Herring
2445773647a0SAndy Whitcroft	sanitise_line_reset();
2446c2fdda0dSAndy Whitcroft	my $line;
2447c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2448773647a0SAndy Whitcroft		$linenr++;
2449773647a0SAndy Whitcroft		$line = $rawline;
2450c2fdda0dSAndy Whitcroft
24513705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
24523705ce5bSJoe Perches
2453773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2454de7d4f0eSAndy Whitcroft			$setup_docs = 0;
24552581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2456de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2457de7d4f0eSAndy Whitcroft			}
2458773647a0SAndy Whitcroft			#next;
2459de7d4f0eSAndy Whitcroft		}
246074fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2461773647a0SAndy Whitcroft			$realline=$1-1;
2462773647a0SAndy Whitcroft			if (defined $2) {
2463773647a0SAndy Whitcroft				$realcnt=$3+1;
2464773647a0SAndy Whitcroft			} else {
2465773647a0SAndy Whitcroft				$realcnt=1+1;
2466773647a0SAndy Whitcroft			}
2467c45dcabdSAndy Whitcroft			$in_comment = 0;
2468773647a0SAndy Whitcroft
2469773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2470773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2471773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2472773647a0SAndy Whitcroft			# at context start.
2473773647a0SAndy Whitcroft			my $edge;
247401fa9147SAndy Whitcroft			my $cnt = $realcnt;
247501fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
247601fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
247701fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
247801fa9147SAndy Whitcroft				$cnt--;
247901fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2480721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2481fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2482fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2483fae17daeSAndy Whitcroft					($edge) = $1;
2484fae17daeSAndy Whitcroft					last;
2485fae17daeSAndy Whitcroft				}
2486773647a0SAndy Whitcroft			}
2487773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2488773647a0SAndy Whitcroft				$in_comment = 1;
2489773647a0SAndy Whitcroft			}
2490773647a0SAndy Whitcroft
2491773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2492773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2493773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2494773647a0SAndy Whitcroft			if (!defined $edge &&
249583242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2496773647a0SAndy Whitcroft			{
2497773647a0SAndy Whitcroft				$in_comment = 1;
2498773647a0SAndy Whitcroft			}
2499773647a0SAndy Whitcroft
2500773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2501773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2502773647a0SAndy Whitcroft
2503171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2504773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2505171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2506773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2507773647a0SAndy Whitcroft		}
2508773647a0SAndy Whitcroft		push(@lines, $line);
2509773647a0SAndy Whitcroft
2510773647a0SAndy Whitcroft		if ($realcnt > 1) {
2511773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2512773647a0SAndy Whitcroft		} else {
2513773647a0SAndy Whitcroft			$realcnt = 0;
2514773647a0SAndy Whitcroft		}
2515773647a0SAndy Whitcroft
2516773647a0SAndy Whitcroft		#print "==>$rawline\n";
2517773647a0SAndy Whitcroft		#print "-->$line\n";
2518de7d4f0eSAndy Whitcroft
2519de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2520de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2521de7d4f0eSAndy Whitcroft		}
2522de7d4f0eSAndy Whitcroft	}
2523de7d4f0eSAndy Whitcroft
25246c72ffaaSAndy Whitcroft	$prefix = '';
25256c72ffaaSAndy Whitcroft
2526773647a0SAndy Whitcroft	$realcnt = 0;
2527773647a0SAndy Whitcroft	$linenr = 0;
2528194f66fcSJoe Perches	$fixlinenr = -1;
25290a920b5bSAndy Whitcroft	foreach my $line (@lines) {
25300a920b5bSAndy Whitcroft		$linenr++;
2531194f66fcSJoe Perches		$fixlinenr++;
25321b5539b1SJoe Perches		my $sline = $line;	#copy of $line
25331b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
25340a920b5bSAndy Whitcroft
2535c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2536f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
25376c72ffaaSAndy Whitcroft
253812c253abSJoe Perches# check if it's a mode change, rename or start of a patch
253912c253abSJoe Perches		if (!$in_commit_log &&
254012c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
254112c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
254212c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
254312c253abSJoe Perches			$is_patch = 1;
254412c253abSJoe Perches		}
254512c253abSJoe Perches
25460a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2547e518e9a5SJoe Perches		if (!$in_commit_log &&
254874fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
254974fd4f34SJoe Perches			my $context = $4;
25500a920b5bSAndy Whitcroft			$is_patch = 1;
25514a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
25520a920b5bSAndy Whitcroft			$realline=$1-1;
25530a920b5bSAndy Whitcroft			if (defined $2) {
25540a920b5bSAndy Whitcroft				$realcnt=$3+1;
25550a920b5bSAndy Whitcroft			} else {
25560a920b5bSAndy Whitcroft				$realcnt=1+1;
25570a920b5bSAndy Whitcroft			}
2558c2fdda0dSAndy Whitcroft			annotate_reset();
255913214adfSAndy Whitcroft			$prev_values = 'E';
256013214adfSAndy Whitcroft
2561773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2562170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
25632b474a1aSAndy Whitcroft			%suppress_export = ();
25643e469cdcSAndy Whitcroft			$suppress_statement = 0;
256574fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
256674fd4f34SJoe Perches				$context_function = $1;
256774fd4f34SJoe Perches			} else {
256874fd4f34SJoe Perches				undef $context_function;
256974fd4f34SJoe Perches			}
25700a920b5bSAndy Whitcroft			next;
25710a920b5bSAndy Whitcroft
25724a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
25734a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
25744a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2575773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
25760a920b5bSAndy Whitcroft			$realline++;
2577d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
25780a920b5bSAndy Whitcroft
25794a0df2efSAndy Whitcroft			# Measure the line length and indent.
2580c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
25810a920b5bSAndy Whitcroft
25820a920b5bSAndy Whitcroft			# Track the previous line.
25830a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
25840a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2585c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2586c2fdda0dSAndy Whitcroft
2587773647a0SAndy Whitcroft			#warn "line<$line>\n";
25886c72ffaaSAndy Whitcroft
2589d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2590d8aaf121SAndy Whitcroft			$realcnt--;
25910a920b5bSAndy Whitcroft		}
25920a920b5bSAndy Whitcroft
2593cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2594cc77cdcaSAndy Whitcroft
25956c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
25966c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2597773647a0SAndy Whitcroft
25982ac73b4fSJoe Perches		my $found_file = 0;
2599773647a0SAndy Whitcroft		# extract the filename as it passes
26003bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
26013bf9a009SRabin Vincent			$realfile = $1;
26022b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2603270c49a0SJoe Perches			$in_commit_log = 0;
26042ac73b4fSJoe Perches			$found_file = 1;
26053bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2606773647a0SAndy Whitcroft			$realfile = $1;
26072b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2608270c49a0SJoe Perches			$in_commit_log = 0;
26091e855726SWolfram Sang
26101e855726SWolfram Sang			$p1_prefix = $1;
2611e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2612e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2613000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2614000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
26151e855726SWolfram Sang			}
2616773647a0SAndy Whitcroft
2617c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2618000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2619000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2620773647a0SAndy Whitcroft			}
26212ac73b4fSJoe Perches			$found_file = 1;
26222ac73b4fSJoe Perches		}
26232ac73b4fSJoe Perches
262434d8815fSJoe Perches#make up the handle for any error we report on this line
262534d8815fSJoe Perches		if ($showfile) {
262634d8815fSJoe Perches			$prefix = "$realfile:$realline: "
262734d8815fSJoe Perches		} elsif ($emacs) {
26287d3a9f67SJoe Perches			if ($file) {
26297d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
26307d3a9f67SJoe Perches			} else {
263134d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
263234d8815fSJoe Perches			}
26337d3a9f67SJoe Perches		}
263434d8815fSJoe Perches
26352ac73b4fSJoe Perches		if ($found_file) {
263685b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
263785b0ee18SJoe Perches				WARN("OBSOLETE",
263885b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
263985b0ee18SJoe Perches			}
26407bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
26412ac73b4fSJoe Perches				$check = 1;
26422ac73b4fSJoe Perches			} else {
26432ac73b4fSJoe Perches				$check = $check_orig;
26442ac73b4fSJoe Perches			}
26459f3a8992SRob Herring			$checklicenseline = 1;
2646133712a2SRob Herring
2647133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2648133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2649133712a2SRob Herring
2650133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2651133712a2SRob Herring
2652133712a2SRob Herring				if (($last_binding_patch != -1) &&
2653133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2654133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2655858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2656133712a2SRob Herring				}
2657133712a2SRob Herring			}
2658133712a2SRob Herring
2659773647a0SAndy Whitcroft			next;
2660773647a0SAndy Whitcroft		}
2661773647a0SAndy Whitcroft
2662389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
26630a920b5bSAndy Whitcroft
2664c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2665c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2666c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
26670a920b5bSAndy Whitcroft
26686c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
26696c72ffaaSAndy Whitcroft
2670490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2671490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2672490b292cSJoe Perches		if ($in_commit_log) {
2673490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2674490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2675490b292cSJoe Perches			}
2676490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2677490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2678490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2679490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2680490b292cSJoe Perches		}
2681490b292cSJoe Perches
2682e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2683e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
268413e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
268513e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2686e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2687e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2688e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2689e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2690e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2691e518e9a5SJoe Perches		}
2692e518e9a5SJoe Perches
26933bf9a009SRabin Vincent# Check for incorrect file permissions
26943bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
26953bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
269604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
269704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2698000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2699000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
27003bf9a009SRabin Vincent			}
27013bf9a009SRabin Vincent		}
27023bf9a009SRabin Vincent
2703cd261496SGeert Uytterhoeven# Check the patch for a From:
2704cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2705cd261496SGeert Uytterhoeven			$author = $1;
2706e7f929f3SDwaipayan Ray			my $curline = $linenr;
2707e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2708e7f929f3SDwaipayan Ray				$author .= $1;
2709e7f929f3SDwaipayan Ray			}
2710cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2711cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2712dfa05c28SJoe Perches			$author = reformat_email($author);
2713cd261496SGeert Uytterhoeven		}
2714cd261496SGeert Uytterhoeven
271520112475SJoe Perches# Check the patch for a signoff:
2716dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
27174a0df2efSAndy Whitcroft			$signoff++;
271815662b3eSJoe Perches			$in_commit_log = 0;
271948ca2d8aSDwaipayan Ray			if ($author ne ''  && $authorsignoff != 1) {
2720*fccaebf0SDwaipayan Ray				if (same_email_addresses($1, $author)) {
2721cd261496SGeert Uytterhoeven					$authorsignoff = 1;
272248ca2d8aSDwaipayan Ray				} else {
272348ca2d8aSDwaipayan Ray					my $ctx = $1;
272448ca2d8aSDwaipayan Ray					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
272548ca2d8aSDwaipayan Ray					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
272648ca2d8aSDwaipayan Ray
272748ca2d8aSDwaipayan Ray					if ($email_address eq $author_address && $email_name eq $author_name) {
272848ca2d8aSDwaipayan Ray						$author_sob = $ctx;
272948ca2d8aSDwaipayan Ray						$authorsignoff = 2;
273048ca2d8aSDwaipayan Ray					} elsif ($email_address eq $author_address) {
273148ca2d8aSDwaipayan Ray						$author_sob = $ctx;
273248ca2d8aSDwaipayan Ray						$authorsignoff = 3;
273348ca2d8aSDwaipayan Ray					} elsif ($email_name eq $author_name) {
273448ca2d8aSDwaipayan Ray						$author_sob = $ctx;
273548ca2d8aSDwaipayan Ray						$authorsignoff = 4;
273648ca2d8aSDwaipayan Ray
273748ca2d8aSDwaipayan Ray						my $address1 = $email_address;
273848ca2d8aSDwaipayan Ray						my $address2 = $author_address;
273948ca2d8aSDwaipayan Ray
274048ca2d8aSDwaipayan Ray						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
274148ca2d8aSDwaipayan Ray							$address1 = "$1$2";
274248ca2d8aSDwaipayan Ray						}
274348ca2d8aSDwaipayan Ray						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
274448ca2d8aSDwaipayan Ray							$address2 = "$1$2";
274548ca2d8aSDwaipayan Ray						}
274648ca2d8aSDwaipayan Ray						if ($address1 eq $address2) {
274748ca2d8aSDwaipayan Ray							$authorsignoff = 5;
274848ca2d8aSDwaipayan Ray						}
274948ca2d8aSDwaipayan Ray					}
2750cd261496SGeert Uytterhoeven				}
2751cd261496SGeert Uytterhoeven			}
27520a920b5bSAndy Whitcroft		}
275320112475SJoe Perches
275444d303ebSJoe Perches# Check for patch separator
275544d303ebSJoe Perches		if ($line =~ /^---$/) {
275644d303ebSJoe Perches			$has_patch_separator = 1;
275744d303ebSJoe Perches			$in_commit_log = 0;
275844d303ebSJoe Perches		}
275944d303ebSJoe Perches
2760e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2761e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2762e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2763e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2764e0d975b1SJoe Perches		}
2765e0d975b1SJoe Perches
276620112475SJoe Perches# Check signature styles
2767270c49a0SJoe Perches		if (!$in_header_lines &&
2768ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
276920112475SJoe Perches			my $space_before = $1;
277020112475SJoe Perches			my $sign_off = $2;
277120112475SJoe Perches			my $space_after = $3;
277220112475SJoe Perches			my $email = $4;
277320112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
277420112475SJoe Perches
2775ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2776ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2777ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2778ce0338dfSJoe Perches			}
277920112475SJoe Perches			if (defined $space_before && $space_before ne "") {
27803705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27813705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
27823705ce5bSJoe Perches				    $fix) {
2783194f66fcSJoe Perches					$fixed[$fixlinenr] =
27843705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27853705ce5bSJoe Perches				}
278620112475SJoe Perches			}
278720112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
27883705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27893705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
27903705ce5bSJoe Perches				    $fix) {
2791194f66fcSJoe Perches					$fixed[$fixlinenr] =
27923705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27933705ce5bSJoe Perches				}
27943705ce5bSJoe Perches
279520112475SJoe Perches			}
279620112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
27973705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27983705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
27993705ce5bSJoe Perches				    $fix) {
2800194f66fcSJoe Perches					$fixed[$fixlinenr] =
28013705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28023705ce5bSJoe Perches				}
280320112475SJoe Perches			}
280420112475SJoe Perches
2805dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
280648ca2d8aSDwaipayan Ray			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
280720112475SJoe Perches			if ($suggested_email eq "") {
2808000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2809000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
281020112475SJoe Perches			} else {
281120112475SJoe Perches				my $dequoted = $suggested_email;
281220112475SJoe Perches				$dequoted =~ s/^"//;
281320112475SJoe Perches				$dequoted =~ s/" </ </;
281420112475SJoe Perches				# Don't force email to have quotes
281520112475SJoe Perches				# Allow just an angle bracketed address
2816*fccaebf0SDwaipayan Ray				if (!same_email_addresses($email, $suggested_email)) {
2817*fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2818*fccaebf0SDwaipayan Ray						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2819*fccaebf0SDwaipayan Ray					    $fix) {
2820*fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2821*fccaebf0SDwaipayan Ray					}
2822*fccaebf0SDwaipayan Ray				}
2823*fccaebf0SDwaipayan Ray
2824*fccaebf0SDwaipayan Ray				# Address part shouldn't have comments
2825*fccaebf0SDwaipayan Ray				my $stripped_address = $email_address;
2826*fccaebf0SDwaipayan Ray				$stripped_address =~ s/\([^\(\)]*\)//g;
2827*fccaebf0SDwaipayan Ray				if ($email_address ne $stripped_address) {
2828*fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2829*fccaebf0SDwaipayan Ray						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2830*fccaebf0SDwaipayan Ray					    $fix) {
2831*fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2832*fccaebf0SDwaipayan Ray					}
2833*fccaebf0SDwaipayan Ray				}
2834*fccaebf0SDwaipayan Ray
2835*fccaebf0SDwaipayan Ray				# Only one name comment should be allowed
2836*fccaebf0SDwaipayan Ray				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2837*fccaebf0SDwaipayan Ray				if ($comment_count > 1) {
2838000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2839*fccaebf0SDwaipayan Ray					     "Use a single name comment in email: '$email'\n" . $herecurr);
2840*fccaebf0SDwaipayan Ray				}
2841*fccaebf0SDwaipayan Ray
2842*fccaebf0SDwaipayan Ray
2843*fccaebf0SDwaipayan Ray				# [email protected] or [email protected] shouldn't
2844*fccaebf0SDwaipayan Ray				# have an email name. In addition commments should strictly
2845*fccaebf0SDwaipayan Ray				# begin with a #
2846*fccaebf0SDwaipayan Ray				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2847*fccaebf0SDwaipayan Ray					if (($comment ne "" && $comment !~ /^#.+/) ||
2848*fccaebf0SDwaipayan Ray					    ($email_name ne "")) {
2849*fccaebf0SDwaipayan Ray						my $cur_name = $email_name;
2850*fccaebf0SDwaipayan Ray						my $new_comment = $comment;
2851*fccaebf0SDwaipayan Ray						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2852*fccaebf0SDwaipayan Ray
2853*fccaebf0SDwaipayan Ray						# Remove brackets enclosing comment text
2854*fccaebf0SDwaipayan Ray						# and # from start of comments to get comment text
2855*fccaebf0SDwaipayan Ray						$new_comment =~ s/^\((.*)\)$/$1/;
2856*fccaebf0SDwaipayan Ray						$new_comment =~ s/^\[(.*)\]$/$1/;
2857*fccaebf0SDwaipayan Ray						$new_comment =~ s/^[\s\#]+|\s+$//g;
2858*fccaebf0SDwaipayan Ray
2859*fccaebf0SDwaipayan Ray						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2860*fccaebf0SDwaipayan Ray						$new_comment = " # $new_comment" if ($new_comment ne "");
2861*fccaebf0SDwaipayan Ray						my $new_email = "$email_address$new_comment";
2862*fccaebf0SDwaipayan Ray
2863*fccaebf0SDwaipayan Ray						if (WARN("BAD_STABLE_ADDRESS_STYLE",
2864*fccaebf0SDwaipayan Ray							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2865*fccaebf0SDwaipayan Ray						    $fix) {
2866*fccaebf0SDwaipayan Ray							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2867*fccaebf0SDwaipayan Ray						}
2868*fccaebf0SDwaipayan Ray					}
2869*fccaebf0SDwaipayan Ray				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2870*fccaebf0SDwaipayan Ray					my $new_comment = $comment;
2871*fccaebf0SDwaipayan Ray
2872*fccaebf0SDwaipayan Ray					# Extract comment text from within brackets or
2873*fccaebf0SDwaipayan Ray					# c89 style /*...*/ comments
2874*fccaebf0SDwaipayan Ray					$new_comment =~ s/^\[(.*)\]$/$1/;
2875*fccaebf0SDwaipayan Ray					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2876*fccaebf0SDwaipayan Ray
2877*fccaebf0SDwaipayan Ray					$new_comment = trim($new_comment);
2878*fccaebf0SDwaipayan Ray					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2879*fccaebf0SDwaipayan Ray					$new_comment = "($new_comment)" if ($new_comment ne "");
2880*fccaebf0SDwaipayan Ray					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2881*fccaebf0SDwaipayan Ray
2882*fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2883*fccaebf0SDwaipayan Ray						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2884*fccaebf0SDwaipayan Ray					    $fix) {
2885*fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2886*fccaebf0SDwaipayan Ray					}
288720112475SJoe Perches				}
28880a920b5bSAndy Whitcroft			}
28897e51f197SJoe Perches
28907e51f197SJoe Perches# Check for duplicate signatures
28917e51f197SJoe Perches			my $sig_nospace = $line;
28927e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
28937e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
28947e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
28957e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
28967e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
28977e51f197SJoe Perches			} else {
28987e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
28997e51f197SJoe Perches			}
29006c5d24eeSSean Christopherson
29016c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
29026c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
29036c5d24eeSSean Christopherson				if ($email eq $author) {
29046c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29056c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
29066c5d24eeSSean Christopherson				}
29076c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
29086c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29096c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
29106c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
29116c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29126c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29136c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
29146c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29156c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29166c5d24eeSSean Christopherson				}
29176c5d24eeSSean Christopherson			}
29180a920b5bSAndy Whitcroft		}
29190a920b5bSAndy Whitcroft
2920a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2921a2fe16b9SJoe Perches		if ($in_header_lines &&
2922a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2923a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2924a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2925a2fe16b9SJoe Perches		}
2926a2fe16b9SJoe Perches
292744d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
292844d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
29297580c5b9SAditya Srivastava			if (ERROR("GERRIT_CHANGE_ID",
29307580c5b9SAditya Srivastava			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
29317580c5b9SAditya Srivastava			    $fix) {
29327580c5b9SAditya Srivastava                                fix_delete_line($fixlinenr, $rawline);
29337580c5b9SAditya Srivastava                        }
29347ebd05efSChristopher Covington		}
29357ebd05efSChristopher Covington
2936369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2937369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2938369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2939369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2940369c8dd3SJoe Perches					# timestamp
2941634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2942634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2943634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2944634cffccSJoe Perches					# stack dump address styles
2945369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2946369c8dd3SJoe Perches		}
2947369c8dd3SJoe Perches
29482a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
29492a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2950bf4daf12SJoe Perches		    length($line) > 75 &&
2951bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2952bf4daf12SJoe Perches					# file delta changes
2953bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2954bf4daf12SJoe Perches					# filename then :
2955bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2956bf4daf12SJoe Perches					# A Fixes: or Link: line
2957bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
29582a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
29592a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
29602a076f40SJoe Perches			$commit_log_long_line = 1;
29612a076f40SJoe Perches		}
29622a076f40SJoe Perches
2963bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2964bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2965bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2966bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2967bf4daf12SJoe Perches		}
2968bf4daf12SJoe Perches
29690d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2970369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2971a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
2972e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2973fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2974aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2975369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2976bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2977fe043ea1SJoe Perches			my $init_char = "c";
2978fe043ea1SJoe Perches			my $orig_commit = "";
29790d7835fcSJoe Perches			my $short = 1;
29800d7835fcSJoe Perches			my $long = 0;
29810d7835fcSJoe Perches			my $case = 1;
29820d7835fcSJoe Perches			my $space = 1;
29830d7835fcSJoe Perches			my $hasdesc = 0;
298419c146a6SJoe Perches			my $hasparens = 0;
29850d7835fcSJoe Perches			my $id = '0123456789ab';
29860d7835fcSJoe Perches			my $orig_desc = "commit description";
29870d7835fcSJoe Perches			my $description = "";
29880d7835fcSJoe Perches
2989fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2990fe043ea1SJoe Perches				$init_char = $1;
2991fe043ea1SJoe Perches				$orig_commit = lc($2);
2992fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2993fe043ea1SJoe Perches				$orig_commit = lc($1);
2994fe043ea1SJoe Perches			}
2995fe043ea1SJoe Perches
29960d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
29970d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
29980d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
29990d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
30000d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
30010d7835fcSJoe Perches				$orig_desc = $1;
300219c146a6SJoe Perches				$hasparens = 1;
30030d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
30040d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
30050d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
30060d7835fcSJoe Perches				$orig_desc = $1;
300719c146a6SJoe Perches				$hasparens = 1;
3008b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3009b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
3010b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3011b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3012b671fde0SJoe Perches				$orig_desc = $1;
3013b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3014b671fde0SJoe Perches				$orig_desc .= " " . $1;
301519c146a6SJoe Perches				$hasparens = 1;
30160d7835fcSJoe Perches			}
30170d7835fcSJoe Perches
30180d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
30190d7835fcSJoe Perches							      $id, $orig_desc);
30200d7835fcSJoe Perches
3021948b133aSHeinrich Schuchardt			if (defined($id) &&
3022948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
3023d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
30240d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
30250d7835fcSJoe Perches			}
3026d311cd44SJoe Perches		}
3027d311cd44SJoe Perches
302813f1937eSJoe Perches# Check for added, moved or deleted files
302913f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
303013f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
303113f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
303213f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
303313f1937eSJoe Perches		      (defined($1) || defined($2))))) {
3034a82603a8SAndrew Jeffery			$is_patch = 1;
303513f1937eSJoe Perches			$reported_maintainer_file = 1;
303613f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
303713f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
303813f1937eSJoe Perches		}
303913f1937eSJoe Perches
3040e400edb1SRob Herring# Check for adding new DT bindings not in schema format
3041e400edb1SRob Herring		if (!$in_commit_log &&
3042e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3043e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3044e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
3045e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3046e400edb1SRob Herring		}
3047e400edb1SRob Herring
304800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
30498905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3050000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
3051000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
30526c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
3053de7d4f0eSAndy Whitcroft		}
3054de7d4f0eSAndy Whitcroft
3055de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3056de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3057171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
3058171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3059171ae1a4SAndy Whitcroft
3060171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
3061171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3062171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
3063171ae1a4SAndy Whitcroft
306434d99219SJoe Perches			CHK("INVALID_UTF8",
3065000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
306600df344fSAndy Whitcroft		}
30670a920b5bSAndy Whitcroft
306815662b3eSJoe Perches# Check if it's the start of a commit log
306915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
307015662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
3071eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
3072eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
307315662b3eSJoe Perches			$in_header_lines = 0;
307415662b3eSJoe Perches			$in_commit_log = 1;
3075ed43c4e5SAllen Hubbe			$has_commit_log = 1;
307615662b3eSJoe Perches		}
307715662b3eSJoe Perches
3078fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
3079fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
3080fa64205dSPasi Savanainen		if ($in_header_lines &&
3081fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3082fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
3083fa64205dSPasi Savanainen			$non_utf8_charset = 1;
3084fa64205dSPasi Savanainen		}
3085fa64205dSPasi Savanainen
3086fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
308715662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
3088fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
308915662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
309015662b3eSJoe Perches		}
309115662b3eSJoe Perches
3092d6430f71SJoe Perches# Check for absolute kernel paths in commit message
3093d6430f71SJoe Perches		if ($tree && $in_commit_log) {
3094d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
3095d6430f71SJoe Perches				my $file = $1;
3096d6430f71SJoe Perches
3097d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3098d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
3099d6430f71SJoe Perches					#
3100d6430f71SJoe Perches				} else {
3101d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
3102d6430f71SJoe Perches				}
3103d6430f71SJoe Perches			}
3104d6430f71SJoe Perches		}
3105d6430f71SJoe Perches
310666b47b4aSKees Cook# Check for various typo / spelling mistakes
310766d7a382SJoe Perches		if (defined($misspellings) &&
310866d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3109ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
311066b47b4aSKees Cook				my $typo = $1;
311166b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
311266b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
311366b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
31140675a8fbSJean Delvare				my $msg_level = \&WARN;
31150675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
31160675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
311766b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
311866b47b4aSKees Cook				    $fix) {
311966b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
312066b47b4aSKees Cook				}
312166b47b4aSKees Cook			}
312266b47b4aSKees Cook		}
312366b47b4aSKees Cook
3124a8dd86bfSMatteo Croce# check for invalid commit id
3125a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3126a8dd86bfSMatteo Croce			my $id;
3127a8dd86bfSMatteo Croce			my $description;
3128a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
3129a8dd86bfSMatteo Croce			if (!defined($id)) {
3130a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
3131a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3132a8dd86bfSMatteo Croce			}
3133a8dd86bfSMatteo Croce		}
3134a8dd86bfSMatteo Croce
3135310cd06bSJoe Perches# check for repeated words separated by a single space
31368d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
31378d0325ccSAditya Srivastava		if (($rawline =~ /^\+/ || $in_commit_log) &&
31388d0325ccSAditya Srivastava		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
31391db81a68SDwaipayan Ray			pos($rawline) = 1 if (!$in_commit_log);
3140310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3141310cd06bSJoe Perches
3142310cd06bSJoe Perches				my $first = $1;
3143310cd06bSJoe Perches				my $second = $2;
31441db81a68SDwaipayan Ray				my $start_pos = $-[1];
31451db81a68SDwaipayan Ray				my $end_pos = $+[2];
3146310cd06bSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3147310cd06bSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3148310cd06bSJoe Perches					next;
3149310cd06bSJoe Perches				}
3150310cd06bSJoe Perches
31511db81a68SDwaipayan Ray				next if (lc($first) ne lc($second));
3152310cd06bSJoe Perches				next if ($first eq 'long');
3153310cd06bSJoe Perches
31541db81a68SDwaipayan Ray				# check for character before and after the word matches
31551db81a68SDwaipayan Ray				my $start_char = '';
31561db81a68SDwaipayan Ray				my $end_char = '';
31571db81a68SDwaipayan Ray				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
31581db81a68SDwaipayan Ray				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
31591db81a68SDwaipayan Ray
31601db81a68SDwaipayan Ray				next if ($start_char =~ /^\S$/);
31611db81a68SDwaipayan Ray				next if (index(" \t.,;?!", $end_char) == -1);
31621db81a68SDwaipayan Ray
31638d0325ccSAditya Srivastava                                # avoid repeating hex occurrences like 'ff ff fe 09 ...'
31648d0325ccSAditya Srivastava                                if ($first =~ /\b[0-9a-f]{2,}\b/i) {
31658d0325ccSAditya Srivastava                                        next if (!exists($allow_repeated_words{lc($first)}));
31668d0325ccSAditya Srivastava                                }
31678d0325ccSAditya Srivastava
3168310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3169310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3170310cd06bSJoe Perches				    $fix) {
3171310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3172310cd06bSJoe Perches				}
3173310cd06bSJoe Perches			}
3174310cd06bSJoe Perches
3175310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3176310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3177310cd06bSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3178310cd06bSJoe Perches				my $last_word = $1;
3179310cd06bSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3180310cd06bSJoe Perches					if (WARN("REPEATED_WORD",
3181310cd06bSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3182310cd06bSJoe Perches					    $fix) {
3183310cd06bSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3184310cd06bSJoe Perches					}
3185310cd06bSJoe Perches				}
3186310cd06bSJoe Perches			}
3187310cd06bSJoe Perches		}
3188310cd06bSJoe Perches
318930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
319030670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
319100df344fSAndy Whitcroft
31920a920b5bSAndy Whitcroft#trailing whitespace
31939c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
3194c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3195d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3196d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
3197d5e616fcSJoe Perches			    $fix) {
3198194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3199d5e616fcSJoe Perches			}
3200c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3201c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32023705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
32033705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
32043705ce5bSJoe Perches			    $fix) {
3205194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
32063705ce5bSJoe Perches			}
32073705ce5bSJoe Perches
3208d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32090a920b5bSAndy Whitcroft		}
32105368df20SAndy Whitcroft
32114783f894SJosh Triplett# Check for FSF mailing addresses.
3212109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
32131bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
32143e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
32153e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
32164783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32170675a8fbSJean Delvare			my $msg_level = \&ERROR;
32180675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
32190675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
32204783f894SJosh 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)
32214783f894SJosh Triplett		}
32224783f894SJosh Triplett
32233354957aSAndi Kleen# check for Kconfig help text having a real description
32249fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
32259fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
32263354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3227678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3228678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3229678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3230678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
32313354957aSAndi Kleen			my $length = 0;
32329fe287d7SAndy Whitcroft			my $cnt = $realcnt;
32339fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
32349fe287d7SAndy Whitcroft			my $f;
3235a1385803SAndy Whitcroft			my $is_start = 0;
32369fe287d7SAndy Whitcroft			my $is_end = 0;
3237a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
32389fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
32399fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
32409fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
32419fe287d7SAndy Whitcroft
32429fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
32438d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3244a1385803SAndy Whitcroft
324586adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3246a1385803SAndy Whitcroft					$is_start = 1;
324722a4ac02SMasahiro Yamada				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
3248a1385803SAndy Whitcroft					$length = -1;
3249a1385803SAndy Whitcroft				}
3250a1385803SAndy Whitcroft
32519fe287d7SAndy Whitcroft				$f =~ s/^.//;
32523354957aSAndi Kleen				$f =~ s/#.*//;
32533354957aSAndi Kleen				$f =~ s/^\s+//;
32543354957aSAndi Kleen				next if ($f =~ /^$/);
3255678ae162SUlf Magnusson
3256678ae162SUlf Magnusson				# This only checks context lines in the patch
3257678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3258678ae162SUlf Magnusson				# positives, even though some of these are
3259678ae162SUlf Magnusson				# common words in help texts
3260678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3261678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
32629fe287d7SAndy Whitcroft					$is_end = 1;
32639fe287d7SAndy Whitcroft					last;
32649fe287d7SAndy Whitcroft				}
32653354957aSAndi Kleen				$length++;
32663354957aSAndi Kleen			}
326756193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3268000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
326956193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
327056193274SVadim Bendebury			}
3271a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
32723354957aSAndi Kleen		}
32733354957aSAndi Kleen
32747ccf41a8SJoe Perches# check MAINTAINERS entries
32757ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
32767ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
32777ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3278628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3279628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3280628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3281628f91a2SJoe Perches				    $fix) {
3282628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3283628f91a2SJoe Perches				}
3284628f91a2SJoe Perches			}
32857ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
32867ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
32877ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
32887ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
32897ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
32907ccf41a8SJoe Perches				my $cur = $1;
32917ccf41a8SJoe Perches				my $curval = $2;
32927ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
32937ccf41a8SJoe Perches				my $prev = $1;
32947ccf41a8SJoe Perches				my $prevval = $2;
32957ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
32967ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
32977ccf41a8SJoe Perches				if ($curindex < 0) {
32987ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
32997ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
33007ccf41a8SJoe Perches				} else {
33017ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
33027ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33037ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
33047ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
33057ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
33067ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
33077ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33087ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
33097ccf41a8SJoe Perches					}
33107ccf41a8SJoe Perches				}
33117ccf41a8SJoe Perches			}
33127ccf41a8SJoe Perches		}
3313628f91a2SJoe Perches
3314327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3315327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3316327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3317327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3318327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3319327953e9SChristoph Jaeger		}
3320327953e9SChristoph Jaeger
3321c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3322c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3323c68e5878SArnaud Lacombe			my $flag = $1;
3324c68e5878SArnaud Lacombe			my $replacement = {
3325c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3326c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3327c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3328c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3329c68e5878SArnaud Lacombe			};
3330c68e5878SArnaud Lacombe
3331c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3332c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3333c68e5878SArnaud Lacombe		}
3334c68e5878SArnaud Lacombe
3335bff5da43SRob Herring# check for DT compatible documentation
33367dd05b38SFlorian Vaussard		if (defined $root &&
33377dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
33387dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
33397dd05b38SFlorian Vaussard
3340bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3341bff5da43SRob Herring
3342cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3343852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3344cc93319bSFlorian Vaussard
3345bff5da43SRob Herring			foreach my $compat (@compats) {
3346bff5da43SRob Herring				my $compat2 = $compat;
3347185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3348185d566bSRob Herring				my $compat3 = $compat;
3349185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3350185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3351bff5da43SRob Herring				if ( $? >> 8 ) {
3352bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3353bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3354bff5da43SRob Herring				}
3355bff5da43SRob Herring
33564fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
33574fbf32a6SFlorian Vaussard				my $vendor = $1;
3358852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3359bff5da43SRob Herring				if ( $? >> 8 ) {
3360bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3361cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3362bff5da43SRob Herring				}
3363bff5da43SRob Herring			}
3364bff5da43SRob Herring		}
3365bff5da43SRob Herring
33669f3a8992SRob Herring# check for using SPDX license tag at beginning of files
33679f3a8992SRob Herring		if ($realline == $checklicenseline) {
33689f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
33699f3a8992SRob Herring				$checklicenseline = 2;
33709f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
33719f3a8992SRob Herring				my $comment = "";
33729f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
33739f3a8992SRob Herring					$comment = '/*';
33749f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
33759f3a8992SRob Herring					$comment = '//';
3376c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
33779f3a8992SRob Herring					$comment = '#';
33789f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
33799f3a8992SRob Herring					$comment = '..';
33809f3a8992SRob Herring				}
33819f3a8992SRob Herring
3382fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3383fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3384fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3385ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3386fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3387fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3388fdf13693SJoe Perches				}
3389fdf13693SJoe Perches
33909f3a8992SRob Herring				if ($comment !~ /^$/ &&
3391ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
33929f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
33939f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
33943b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
33953b6e8ac9SJoe Perches					my $spdx_license = $1;
33963b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
33973b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
33983b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
33993b6e8ac9SJoe Perches					}
340050c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
340150c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
340250c92900SLubomir Rintel						my $msg_level = \&WARN;
340350c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
340450c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
340550c92900SLubomir Rintel
340650c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
340750c92900SLubomir Rintel						    $fix) {
340850c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
340950c92900SLubomir Rintel						}
341050c92900SLubomir Rintel					}
34119f3a8992SRob Herring				}
34129f3a8992SRob Herring			}
34139f3a8992SRob Herring		}
34149f3a8992SRob Herring
3415a0154cdbSJoe Perches# check for embedded filenames
3416a0154cdbSJoe Perches		if ($rawline =~ /^\+.*\Q$realfile\E/) {
3417a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3418a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3419a0154cdbSJoe Perches		}
3420a0154cdbSJoe Perches
34215368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3422d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
34235368df20SAndy Whitcroft
3424a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3425a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3426a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3427a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3428a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3429a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3430a8da38a9SJoe Perches		}
3431a8da38a9SJoe Perches
343247e0c88bSJoe Perches# line length limit (with some exclusions)
343347e0c88bSJoe Perches#
343447e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
343547e0c88bSJoe Perches#	logging functions like pr_info that end in a string
343647e0c88bSJoe Perches#	lines with a single string
343747e0c88bSJoe Perches#	#defines that are a single string
34382e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
343947e0c88bSJoe Perches#
344047e0c88bSJoe Perches# There are 3 different line length message types:
3441ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
344247e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
344347e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
344447e0c88bSJoe Perches#
344547e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
344647e0c88bSJoe Perches#
344747e0c88bSJoe Perches
3448b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
344947e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
345047e0c88bSJoe Perches
345147e0c88bSJoe Perches			# Check the allowed long line types first
345247e0c88bSJoe Perches
345347e0c88bSJoe Perches			# logging functions that end in a string that starts
345447e0c88bSJoe Perches			# before $max_line_length
345547e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
345647e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
345747e0c88bSJoe Perches				$msg_type = "";
345847e0c88bSJoe Perches
345947e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
346047e0c88bSJoe Perches			# #defines with only strings
346147e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
346247e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
346347e0c88bSJoe Perches				$msg_type = "";
346447e0c88bSJoe Perches
3465cc147506SJoe Perches			# More special cases
3466cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3467cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3468d560a5f8SJoe Perches				$msg_type = "";
3469d560a5f8SJoe Perches
34702e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
34712e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
34722e4bbbc5SAndreas Brauchli				$msg_type = "";
34732e4bbbc5SAndreas Brauchli
347447e0c88bSJoe Perches			# Otherwise set the alternate message types
347547e0c88bSJoe Perches
347647e0c88bSJoe Perches			# a comment starts before $max_line_length
347747e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
347847e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
347947e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
348047e0c88bSJoe Perches
348147e0c88bSJoe Perches			# a quoted string starts before $max_line_length
348247e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
348347e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
348447e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
348547e0c88bSJoe Perches			}
348647e0c88bSJoe Perches
348747e0c88bSJoe Perches			if ($msg_type ne "" &&
348847e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3489bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3490bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3491bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3492bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
34930a920b5bSAndy Whitcroft			}
349447e0c88bSJoe Perches		}
34950a920b5bSAndy Whitcroft
34968905a67cSAndy Whitcroft# check for adding lines without a newline.
34978905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
349847ca69b8STom Rix			if (WARN("MISSING_EOF_NEWLINE",
349947ca69b8STom Rix			         "adding a line without newline at end of file\n" . $herecurr) &&
350047ca69b8STom Rix			    $fix) {
350147ca69b8STom Rix				fix_delete_line($fixlinenr+1, "No newline at end of file");
350247ca69b8STom Rix			}
35038905a67cSAndy Whitcroft		}
35048905a67cSAndy Whitcroft
3505b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3506de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
35070a920b5bSAndy Whitcroft
35080a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3509713a09deSAntonio Borneo# more than $tabsize must use tabs.
3510c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3511c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3512c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3513d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
35143705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
35153705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
35163705ce5bSJoe Perches			    $fix) {
3517194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
35183705ce5bSJoe Perches			}
35190a920b5bSAndy Whitcroft		}
35200a920b5bSAndy Whitcroft
352108e44365SAlberto Panizzo# check for space before tabs.
352208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
352308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35243705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
35253705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
35263705ce5bSJoe Perches			    $fix) {
3527194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3528713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3529194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3530c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
35313705ce5bSJoe Perches			}
353208e44365SAlberto Panizzo		}
353308e44365SAlberto Panizzo
35346a487211SJoe Perches# check for assignments on the start of a line
35356a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
35366a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
35376a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
35386a487211SJoe Perches		}
35396a487211SJoe Perches
3540d1fe9c09SJoe Perches# check for && or || at the start of a line
3541d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3542d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3543d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3544d1fe9c09SJoe Perches		}
3545d1fe9c09SJoe Perches
3546a91e8994SJoe Perches# check indentation starts on a tab stop
35475b57980dSJoe Perches		if ($perl_version_ok &&
3548bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3549a91e8994SJoe Perches			my $indent = length($1);
3550713a09deSAntonio Borneo			if ($indent % $tabsize) {
3551a91e8994SJoe Perches				if (WARN("TABSTOP",
3552a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3553a91e8994SJoe Perches				    $fix) {
3554713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3555a91e8994SJoe Perches				}
3556a91e8994SJoe Perches			}
3557a91e8994SJoe Perches		}
3558a91e8994SJoe Perches
3559d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
35605b57980dSJoe Perches		if ($perl_version_ok &&
3561fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3562d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3563d1fe9c09SJoe Perches			my $oldindent = $1;
3564d1fe9c09SJoe Perches			my $rest = $2;
3565d1fe9c09SJoe Perches
3566d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3567d1fe9c09SJoe Perches			if ($pos >= 0) {
3568b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3569b34a26f3SJoe Perches				my $newindent = $2;
3570d1fe9c09SJoe Perches
3571d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3572713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3573713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3574d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3575d1fe9c09SJoe Perches
3576d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3577d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
35783705ce5bSJoe Perches
35793705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
35803705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
35813705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3582194f66fcSJoe Perches						$fixed[$fixlinenr] =~
35833705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
35843705ce5bSJoe Perches					}
3585d1fe9c09SJoe Perches				}
3586d1fe9c09SJoe Perches			}
3587d1fe9c09SJoe Perches		}
3588d1fe9c09SJoe Perches
35896ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
35906ab3a970SJoe Perches# avoid checking a few false positives:
35916ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
35926ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
35936ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
35946ab3a970SJoe Perches#   multiline macros that define functions
35956ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
35966ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
35976ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
35983705ce5bSJoe Perches			if (CHK("SPACING",
3599f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
36003705ce5bSJoe Perches			    $fix) {
3601194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3602f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
36033705ce5bSJoe Perches			}
3604aad4f614SJoe Perches		}
3605aad4f614SJoe Perches
360686406b1cSJoe Perches# Block comment styles
360786406b1cSJoe Perches# Networking with an initial /*
360805880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3609fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
361085ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
3611c70735c2SŁukasz Stelmach		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
361205880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
361305880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
361405880600SJoe Perches		}
361505880600SJoe Perches
361686406b1cSJoe Perches# Block comments use * on subsequent lines
361786406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
361886406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3619a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
362061135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3621a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
362286406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
362386406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3624a605e32eSJoe Perches		}
3625a605e32eSJoe Perches
362686406b1cSJoe Perches# Block comments use */ on trailing lines
362786406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3628c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3629c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3630c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
363186406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
363286406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
363305880600SJoe Perches		}
363405880600SJoe Perches
363508eb9b80SJoe Perches# Block comment * alignment
363608eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3637af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3638af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3639af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
364008eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3641af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3642af207524SJoe Perches			my $oldindent;
364308eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3644af207524SJoe Perches			if (defined($1)) {
3645af207524SJoe Perches				$oldindent = expand_tabs($1);
3646af207524SJoe Perches			} else {
3647af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3648af207524SJoe Perches				$oldindent = expand_tabs($1);
3649af207524SJoe Perches			}
365008eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
365108eb9b80SJoe Perches			my $newindent = $1;
365208eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3653af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
365408eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
365508eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
365608eb9b80SJoe Perches			}
365708eb9b80SJoe Perches		}
365808eb9b80SJoe Perches
36597f619191SJoe Perches# check for missing blank lines after struct/union declarations
36607f619191SJoe Perches# with exceptions for various attributes and macros
36617f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
36627f619191SJoe Perches		    $line =~ /^\+/ &&
36637f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
36647f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
36657f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
36667f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
36677f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
36687f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
36697f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
36700bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
36717f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3672d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3673d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3674d752fcc8SJoe Perches			    $fix) {
3675f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3676d752fcc8SJoe Perches			}
36777f619191SJoe Perches		}
36787f619191SJoe Perches
3679365dd4eaSJoe Perches# check for multiple consecutive blank lines
3680365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3681365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3682365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3683d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3684d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3685d752fcc8SJoe Perches			    $fix) {
3686f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3687d752fcc8SJoe Perches			}
3688d752fcc8SJoe Perches
3689365dd4eaSJoe Perches			$last_blank_line = $linenr;
3690365dd4eaSJoe Perches		}
3691365dd4eaSJoe Perches
36923b617e3bSJoe Perches# check for missing blank lines after declarations
36933f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
36943f7bac03SJoe Perches			# actual declarations
36953f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
36965a4e1fd3SJoe Perches			# function pointer declarations
36975a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
36983f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
36993f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
37003f7bac03SJoe Perches			# known declaration macros
37013f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
37023f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
37033f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
37043f7bac03SJoe Perches			# other possible extensions of declaration lines
37053f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
37063f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
37073f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
37083f7bac03SJoe Perches			# looks like a declaration
37093f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
37105a4e1fd3SJoe Perches			# function pointer declarations
37115a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
37123f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
37133f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
37143f7bac03SJoe Perches			# known declaration macros
37153f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
37163f7bac03SJoe Perches			# start of struct or union or enum
3717328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
37183f7bac03SJoe Perches			# start or end of block or continuation of declaration
37193f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
37203f7bac03SJoe Perches			# bitfield continuation
37213f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
37223f7bac03SJoe Perches			# other possible extensions of declaration lines
37233f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
37243f7bac03SJoe Perches			# indentation of previous and current line are the same
37253f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3726d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3727d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3728d752fcc8SJoe Perches			    $fix) {
3729f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3730d752fcc8SJoe Perches			}
37313b617e3bSJoe Perches		}
37323b617e3bSJoe Perches
37335f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
37346b4c5bebSAndy Whitcroft# Exceptions:
37356b4c5bebSAndy Whitcroft#  1) within comments
37366b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
37376b4c5bebSAndy Whitcroft#  3) hanging labels
37383705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
37395f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
37403705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
37413705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
37423705ce5bSJoe Perches			    $fix) {
3743194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
37443705ce5bSJoe Perches			}
37455f7ddae6SRaffaele Recalcati		}
37465f7ddae6SRaffaele Recalcati
3747b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3748b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3749b9ea10d6SAndy Whitcroft
37505751a24eSJoe Perches# check for unusual line ending [ or (
37515751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
37525751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
37535751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
37545751a24eSJoe Perches		}
37555751a24eSJoe Perches
37564dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
37574dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
37584dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
37594dbed76fSJoe Perches			$context_function = $1;
37604dbed76fSJoe Perches		}
37614dbed76fSJoe Perches
37624dbed76fSJoe Perches# check if this appears to be the end of function declaration
37634dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
37644dbed76fSJoe Perches			undef $context_function;
37654dbed76fSJoe Perches		}
37664dbed76fSJoe Perches
3767032a4c0fSJoe Perches# check indentation of any line with a bare else
3768840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3769032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3770032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3771032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3772840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3773840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3774840080a0SJoe Perches			     defined $lines[$linenr] &&
3775840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3776032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3777032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3778032a4c0fSJoe Perches			}
3779032a4c0fSJoe Perches		}
3780032a4c0fSJoe Perches
3781c00df19aSJoe Perches# check indentation of a line with a break;
3782dc58bc55SJoe Perches# if the previous line is a goto, return or break
3783dc58bc55SJoe Perches# and is indented the same # of tabs
3784c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3785c00df19aSJoe Perches			my $tabs = $1;
3786dc58bc55SJoe Perches			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3787dc58bc55SJoe Perches				if (WARN("UNNECESSARY_BREAK",
3788dc58bc55SJoe Perches					 "break is not useful after a $1\n" . $hereprev) &&
3789dc58bc55SJoe Perches				    $fix) {
3790dc58bc55SJoe Perches					fix_delete_line($fixlinenr, $rawline);
3791dc58bc55SJoe Perches				}
3792c00df19aSJoe Perches			}
3793c00df19aSJoe Perches		}
3794c00df19aSJoe Perches
3795c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3796cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3797000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3798000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3799c2fdda0dSAndy Whitcroft		}
380022f2a2efSAndy Whitcroft
380156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
380256e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
380356e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
380456e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
380556e77d70SJoe Perches		}
380656e77d70SJoe Perches
38079c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
38082b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
38092b474a1aSAndy Whitcroft		    $realline_next);
38103e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3811ca819864SJoe Perches		if ($linenr > $suppress_statement &&
38121b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3813170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3814f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3815171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3816171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3817171ae1a4SAndy Whitcroft
38183e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
38193e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
38203e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
38213e469cdcSAndy Whitcroft			# until we hit end of it.
38223e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
38233e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
38243e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
38253e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
38263e469cdcSAndy Whitcroft			}
3827f74bd194SAndy Whitcroft
38282b474a1aSAndy Whitcroft			# Find the real next line.
38292b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
38302b474a1aSAndy Whitcroft			if (defined $realline_next &&
38312b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
38322b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
38332b474a1aSAndy Whitcroft				$realline_next++;
38342b474a1aSAndy Whitcroft			}
38352b474a1aSAndy Whitcroft
3836171ae1a4SAndy Whitcroft			my $s = $stat;
3837171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3838cf655043SAndy Whitcroft
3839c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3840171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3841c2fdda0dSAndy Whitcroft
3842c2fdda0dSAndy Whitcroft			# Ignore functions being called
3843171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3844c2fdda0dSAndy Whitcroft
3845463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3846463f2864SAndy Whitcroft
3847c45dcabdSAndy Whitcroft			# declarations always start with types
3848d2506586SAndy 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) {
3849c45dcabdSAndy Whitcroft				my $type = $1;
3850c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3851c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3852c45dcabdSAndy Whitcroft
38536c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3854a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3855c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3856c2fdda0dSAndy Whitcroft			}
38578905a67cSAndy Whitcroft
38586c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
385965863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3860c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
38619c0ca6f9SAndy Whitcroft			}
38628905a67cSAndy Whitcroft
38638905a67cSAndy Whitcroft			# Check for any sort of function declaration.
38648905a67cSAndy Whitcroft			# int foo(something bar, other baz);
38658905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3866171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
38678905a67cSAndy Whitcroft				my ($name_len) = length($1);
38688905a67cSAndy Whitcroft
3869cf655043SAndy Whitcroft				my $ctx = $s;
3870773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
38718905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3872cf655043SAndy Whitcroft
38738905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3874c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
38758905a67cSAndy Whitcroft
3876c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
38778905a67cSAndy Whitcroft					}
38788905a67cSAndy Whitcroft				}
38798905a67cSAndy Whitcroft			}
38808905a67cSAndy Whitcroft
38819c0ca6f9SAndy Whitcroft		}
38829c0ca6f9SAndy Whitcroft
388300df344fSAndy Whitcroft#
388400df344fSAndy Whitcroft# Checks which may be anchored in the context.
388500df344fSAndy Whitcroft#
388600df344fSAndy Whitcroft
388700df344fSAndy Whitcroft# Check for switch () and associated case and default
388800df344fSAndy Whitcroft# statements should be at the same indent.
388900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
389000df344fSAndy Whitcroft			my $err = '';
389100df344fSAndy Whitcroft			my $sep = '';
389200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
389300df344fSAndy Whitcroft			shift(@ctx);
389400df344fSAndy Whitcroft			for my $ctx (@ctx) {
389500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
389600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
389700df344fSAndy Whitcroft							$indent != $cindent) {
389800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
389900df344fSAndy Whitcroft					$sep = '';
390000df344fSAndy Whitcroft				} else {
390100df344fSAndy Whitcroft					$sep = "[...]\n";
390200df344fSAndy Whitcroft				}
390300df344fSAndy Whitcroft			}
390400df344fSAndy Whitcroft			if ($err ne '') {
3905000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3906000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3907de7d4f0eSAndy Whitcroft			}
3908de7d4f0eSAndy Whitcroft		}
3909de7d4f0eSAndy Whitcroft
3910de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3911de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
39120fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3913773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3914773647a0SAndy Whitcroft
39159c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
39168eef05ddSJoe Perches
39178eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
39188eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
39198eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
39208eef05ddSJoe Perches			}
39218eef05ddSJoe Perches
3922de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3923de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3924de7d4f0eSAndy Whitcroft
3925548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3926548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3927de7d4f0eSAndy Whitcroft
3928548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3929548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3930548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3931548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3932548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3933773647a0SAndy Whitcroft				$ctx_ln++;
3934773647a0SAndy Whitcroft			}
3935548596d5SAndy Whitcroft
393653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
393753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3938773647a0SAndy Whitcroft
3939773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3940000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3941000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
394201464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
394300df344fSAndy Whitcroft			}
3944773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3945773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3946773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3947773647a0SAndy Whitcroft			{
39489c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
39499c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3950000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3951000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
395201464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
39539c0ca6f9SAndy Whitcroft				}
39549c0ca6f9SAndy Whitcroft			}
395500df344fSAndy Whitcroft		}
395600df344fSAndy Whitcroft
39574d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3958f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
39593e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
39603e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
39613e469cdcSAndy Whitcroft					if (!defined $stat);
39624d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
39634d001e4dSAndy Whitcroft
39644d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
39654d001e4dSAndy Whitcroft
39669f5af480SJoe Perches			# remove inline comments
39679f5af480SJoe Perches			$s =~ s/$;/ /g;
39689f5af480SJoe Perches			$c =~ s/$;/ /g;
39694d001e4dSAndy Whitcroft
39704d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
39716f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
39726f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
39734d001e4dSAndy Whitcroft
39749f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
39759f5af480SJoe Perches			# none on the first line, and are going to readd them
39769f5af480SJoe Perches			# where necessary.
39779f5af480SJoe Perches			$s =~ s/\n./\n/gs;
39789f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
39799f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
39809f5af480SJoe Perches			}
39819f5af480SJoe Perches
39824d001e4dSAndy Whitcroft			# We want to check the first line inside the block
39834d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
39844d001e4dSAndy Whitcroft			#  1) any blank line termination
39854d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
39864d001e4dSAndy Whitcroft			#  3) any do (...) {
39874d001e4dSAndy Whitcroft			my $continuation = 0;
39884d001e4dSAndy Whitcroft			my $check = 0;
39894d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
39904d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
39914d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
39924d001e4dSAndy Whitcroft				$continuation = 1;
39934d001e4dSAndy Whitcroft			}
39949bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
39954d001e4dSAndy Whitcroft				$check = 1;
39964d001e4dSAndy Whitcroft				$cond_lines++;
39974d001e4dSAndy Whitcroft			}
39984d001e4dSAndy Whitcroft
39994d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
40004d001e4dSAndy Whitcroft			# preprocessor statement.
40014d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
40024d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
40034d001e4dSAndy Whitcroft				$check = 0;
40044d001e4dSAndy Whitcroft			}
40054d001e4dSAndy Whitcroft
40069bd49efeSAndy Whitcroft			my $cond_ptr = -1;
4007740504c6SAndy Whitcroft			$continuation = 0;
40089bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
40099bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
40104d001e4dSAndy Whitcroft
4011f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
4012f16fa28fSAndy Whitcroft				# is not linear.
4013f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4014f16fa28fSAndy Whitcroft					$check = 0;
4015f16fa28fSAndy Whitcroft				}
4016f16fa28fSAndy Whitcroft
40179bd49efeSAndy Whitcroft				# Ignore:
40189bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
40199bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
40209bd49efeSAndy Whitcroft				#  3) labels.
4021740504c6SAndy Whitcroft				if ($continuation ||
4022740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
40239bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
40249bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
4025740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
402630dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
40279bd49efeSAndy Whitcroft						$cond_lines++;
40289bd49efeSAndy Whitcroft					}
40294d001e4dSAndy Whitcroft				}
403030dad6ebSAndy Whitcroft			}
40314d001e4dSAndy Whitcroft
40324d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
40334d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
40344d001e4dSAndy Whitcroft
40354d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
40364d001e4dSAndy Whitcroft			# this is not this patch's fault.
40374d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
40384d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
40394d001e4dSAndy Whitcroft				$check = 0;
40404d001e4dSAndy Whitcroft			}
40414d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
40424d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
40434d001e4dSAndy Whitcroft			}
40444d001e4dSAndy Whitcroft
40459bd49efeSAndy 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";
40464d001e4dSAndy Whitcroft
40479f5af480SJoe Perches			if ($check && $s ne '' &&
4048713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
40499f5af480SJoe Perches			     ($sindent < $indent) ||
4050f6950a73SJoe Perches			     ($sindent == $indent &&
4051f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4052713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
4053000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
4054000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
40554d001e4dSAndy Whitcroft			}
40564d001e4dSAndy Whitcroft		}
40574d001e4dSAndy Whitcroft
40586c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
40596c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
40601f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
40611f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
40626c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
4063c2fdda0dSAndy Whitcroft		if ($dbg_values) {
4064c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
4065cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
4066cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
40671f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
4068c2fdda0dSAndy Whitcroft		}
40696c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
40706c72ffaaSAndy Whitcroft
407100df344fSAndy Whitcroft#ignore lines not being added
40723705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
407300df344fSAndy Whitcroft
407499ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
407599ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
407699ca38c2SJoe Perches#	struct foo bar = *(&(bar));
407799ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
407899ca38c2SJoe Perches			my $var = $1;
407999ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
408099ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
408199ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
408299ca38c2SJoe Perches			}
408399ca38c2SJoe Perches		}
408499ca38c2SJoe Perches
408511ca40a0SJoe Perches# check for dereferences that span multiple lines
408611ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
408711ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
408811ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
408911ca40a0SJoe Perches			my $ref = $1;
409011ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
409111ca40a0SJoe Perches			$ref .= $1;
409211ca40a0SJoe Perches			$ref =~ s/\s//g;
409311ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
409411ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
409511ca40a0SJoe Perches		}
409611ca40a0SJoe Perches
4097a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
4098c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4099a1ce18e4SJoe Perches			my $type = $1;
4100a1ce18e4SJoe Perches			my $var = $2;
4101207a8e84SJoe Perches			$var = "" if (!defined $var);
4102207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4103a1ce18e4SJoe Perches				my $sign = $1;
4104a1ce18e4SJoe Perches				my $pointer = $2;
4105a1ce18e4SJoe Perches
4106a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
4107a1ce18e4SJoe Perches
4108a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
4109a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4110a1ce18e4SJoe Perches				    $fix) {
4111a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
4112207a8e84SJoe Perches					my $comp_pointer = $pointer;
4113207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
4114207a8e84SJoe Perches					$decl .= $comp_pointer;
4115207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
4116207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4117a1ce18e4SJoe Perches				}
4118a1ce18e4SJoe Perches			}
4119a1ce18e4SJoe Perches		}
4120a1ce18e4SJoe Perches
4121653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
41227429c690SAndy Whitcroft		if ($dbg_type) {
41237429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
4124000d1cc1SJoe Perches				ERROR("TEST_TYPE",
4125000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
41267429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4127000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
4128000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
41297429c690SAndy Whitcroft			}
4130653d4876SAndy Whitcroft			next;
4131653d4876SAndy Whitcroft		}
4132a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
4133a1ef277eSAndy Whitcroft		if ($dbg_attr) {
41349360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
4135000d1cc1SJoe Perches				ERROR("TEST_ATTR",
4136000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
41379360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4138000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
4139000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
4140a1ef277eSAndy Whitcroft			}
4141a1ef277eSAndy Whitcroft			next;
4142a1ef277eSAndy Whitcroft		}
4143653d4876SAndy Whitcroft
4144f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
414599423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
414699423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
4147d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4148d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4149f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4150f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4151f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4152d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4153d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4154f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4155d752fcc8SJoe Perches				$fixedline = $line;
41568d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4157f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4158d752fcc8SJoe Perches			}
4159f0a594c1SAndy Whitcroft		}
4160f0a594c1SAndy Whitcroft
416100df344fSAndy Whitcroft#
416200df344fSAndy Whitcroft# Checks which are anchored on the added line.
416300df344fSAndy Whitcroft#
416400df344fSAndy Whitcroft
4165653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4166c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4167653d4876SAndy Whitcroft			my $path = $1;
4168653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4169000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4170495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4171495e9d84SJoe Perches			}
4172495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4173495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4174495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4175653d4876SAndy Whitcroft			}
4176653d4876SAndy Whitcroft		}
4177653d4876SAndy Whitcroft
417800df344fSAndy Whitcroft# no C99 // comments
417900df344fSAndy Whitcroft		if ($line =~ m{//}) {
41803705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
41813705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
41823705ce5bSJoe Perches			    $fix) {
4183194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
41843705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
41853705ce5bSJoe Perches					my $comment = trim($1);
4186194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
41873705ce5bSJoe Perches				}
41883705ce5bSJoe Perches			}
418900df344fSAndy Whitcroft		}
419000df344fSAndy Whitcroft		# Remove C99 comments.
41910a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
41926c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
41930a920b5bSAndy Whitcroft
41942b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
41952b474a1aSAndy Whitcroft# the whole statement.
41962b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
41972b474a1aSAndy Whitcroft		if (defined $realline_next &&
41982b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
41992b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
42002b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
42012b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
42023cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
42033cbf62dfSAndy Whitcroft			# a prefix:
42043cbf62dfSAndy Whitcroft			#   XXX(foo);
42053cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4206653d4876SAndy Whitcroft			my $name = $1;
420787a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
42083cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
42093cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
42103cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
42113cbf62dfSAndy Whitcroft
42123cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
42132b474a1aSAndy Whitcroft				\n.}\s*$|
421448012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
421548012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
421648012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
42172b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
42182b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
421948012058SAndy Whitcroft			    )/x) {
42202b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
42212b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
42222b474a1aSAndy Whitcroft			} else {
42232b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
42240a920b5bSAndy Whitcroft			}
42250a920b5bSAndy Whitcroft		}
42262b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
42272b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
42282b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
42292b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
42302b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
42312b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
42322b474a1aSAndy Whitcroft		}
42332b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
42342b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4235000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4236000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
42372b474a1aSAndy Whitcroft		}
42380a920b5bSAndy Whitcroft
42395150bda4SJoe Eloff# check for global initialisers.
42406d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
4241d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
42426d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4243d5e616fcSJoe Perches			    $fix) {
42446d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4245d5e616fcSJoe Perches			}
4246f0a594c1SAndy Whitcroft		}
42470a920b5bSAndy Whitcroft# check for static initialisers.
42486d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4249d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
42506d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4251d5e616fcSJoe Perches				      $herecurr) &&
4252d5e616fcSJoe Perches			    $fix) {
42536d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4254d5e616fcSJoe Perches			}
42550a920b5bSAndy Whitcroft		}
42560a920b5bSAndy Whitcroft
42571813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
42581813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
42591813087dSJoe Perches			my $tmp = trim($1);
42601813087dSJoe Perches			WARN("MISORDERED_TYPE",
42611813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
42621813087dSJoe Perches		}
42631813087dSJoe Perches
4264809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4265809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4266809e082eSJoe Perches			my $type = trim($1);
4267809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4268809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4269809e082eSJoe Perches			my $new_type = $type;
4270809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4271809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4272809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4273809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4274809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4275809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4276809e082eSJoe Perches			$new_type = trim($new_type);
4277809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4278809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4279809e082eSJoe Perches			    $fix) {
4280809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4281809e082eSJoe Perches			}
4282809e082eSJoe Perches		}
4283809e082eSJoe Perches
4284cb710ecaSJoe Perches# check for static const char * arrays.
4285cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4286000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4287000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4288cb710ecaSJoe Perches				$herecurr);
4289cb710ecaSJoe Perches		}
4290cb710ecaSJoe Perches
429177b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
429277b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
429377b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
429477b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
429577b8c0a8SJoe Perches			    $fix) {
429677b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
429777b8c0a8SJoe Perches			}
429877b8c0a8SJoe Perches		}
429977b8c0a8SJoe Perches
4300cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4301cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4302000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4303000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4304cb710ecaSJoe Perches				$herecurr);
4305cb710ecaSJoe Perches		}
4306cb710ecaSJoe Perches
4307ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4308ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4309ab7e23f3SJoe Perches			my $found = $1;
4310ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4311ab7e23f3SJoe Perches				WARN("CONST_CONST",
4312ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4313ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4314ab7e23f3SJoe Perches				WARN("CONST_CONST",
4315ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4316ab7e23f3SJoe Perches			}
4317ab7e23f3SJoe Perches		}
4318ab7e23f3SJoe Perches
431973169765SJoe Perches# check for const static or static <non ptr type> const declarations
432073169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
432173169765SJoe Perches		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
432273169765SJoe Perches		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
432373169765SJoe Perches			if (WARN("STATIC_CONST",
432473169765SJoe Perches				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
432573169765SJoe Perches			    $fix) {
432673169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
432773169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
432873169765SJoe Perches			}
432973169765SJoe Perches		}
433073169765SJoe Perches
43319b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
43329b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
43339b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
43349b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
43359b0fa60dSJoe Perches				$herecurr);
43369b0fa60dSJoe Perches               }
43379b0fa60dSJoe Perches
4338b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4339b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4340b598b670SJoe Perches			my $array = $1;
4341b598b670SJoe 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*\))@) {
4342b598b670SJoe Perches				my $array_div = $1;
4343b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4344b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4345b598b670SJoe Perches				    $fix) {
4346b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4347b598b670SJoe Perches				}
4348b598b670SJoe Perches			}
4349b598b670SJoe Perches		}
4350b598b670SJoe Perches
4351b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
435216b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4353b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4354b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4355b36190c5SJoe Perches			    $fix) {
4356194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4357b36190c5SJoe Perches			}
4358b36190c5SJoe Perches		}
4359b36190c5SJoe Perches
4360653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4361653d4876SAndy Whitcroft# make sense.
4362653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
43638054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4364c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
43658ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
436646d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4367000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4368000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
43690a920b5bSAndy Whitcroft		}
43700a920b5bSAndy Whitcroft
43710a920b5bSAndy Whitcroft# * goes on variable not on type
437265863862SAndy Whitcroft		# (char*[ const])
4373bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4374bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
43753705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4376d8aaf121SAndy Whitcroft
437765863862SAndy Whitcroft			# Should start with a space.
437865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
437965863862SAndy Whitcroft			# Should not end with a space.
438065863862SAndy Whitcroft			$to =~ s/\s+$//;
438165863862SAndy Whitcroft			# '*'s should not have spaces between.
4382f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
438365863862SAndy Whitcroft			}
4384d8aaf121SAndy Whitcroft
43853705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
438665863862SAndy Whitcroft			if ($from ne $to) {
43873705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
43883705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
43893705ce5bSJoe Perches				    $fix) {
43903705ce5bSJoe Perches					my $sub_from = $ident;
43913705ce5bSJoe Perches					my $sub_to = $ident;
43923705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4393194f66fcSJoe Perches					$fixed[$fixlinenr] =~
43943705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
43953705ce5bSJoe Perches				}
439665863862SAndy Whitcroft			}
4397bfcb2cc7SAndy Whitcroft		}
4398bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4399bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
44003705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4401d8aaf121SAndy Whitcroft
440265863862SAndy Whitcroft			# Should start with a space.
440365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
440465863862SAndy Whitcroft			# Should not end with a space.
440565863862SAndy Whitcroft			$to =~ s/\s+$//;
440665863862SAndy Whitcroft			# '*'s should not have spaces between.
4407f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
440865863862SAndy Whitcroft			}
440965863862SAndy Whitcroft			# Modifiers should have spaces.
441065863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
441165863862SAndy Whitcroft
44123705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4413667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
44143705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
44153705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
44163705ce5bSJoe Perches				    $fix) {
44173705ce5bSJoe Perches
44183705ce5bSJoe Perches					my $sub_from = $match;
44193705ce5bSJoe Perches					my $sub_to = $match;
44203705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4421194f66fcSJoe Perches					$fixed[$fixlinenr] =~
44223705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
44233705ce5bSJoe Perches				}
442465863862SAndy Whitcroft			}
44250a920b5bSAndy Whitcroft		}
44260a920b5bSAndy Whitcroft
44279d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
44289d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
44290675a8fbSJean Delvare			my $msg_level = \&WARN;
44300675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
44310675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
44329d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
44339d3e3c70SJoe Perches		}
44340a920b5bSAndy Whitcroft
44359d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
44368905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4437000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4438000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
44398905a67cSAndy Whitcroft		}
44408905a67cSAndy Whitcroft
444117441227SJoe Perches# check for uses of printk_ratelimit
444217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4443000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4444000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
444517441227SJoe Perches		}
444617441227SJoe Perches
4447eeef5733SJoe Perches# printk should use KERN_* levels
4448eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4449000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4450eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
445100df344fSAndy Whitcroft		}
44520a920b5bSAndy Whitcroft
4453243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4454243f3803SJoe Perches			my $orig = $1;
4455243f3803SJoe Perches			my $level = lc($orig);
4456243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
44578f26b837SJoe Perches			my $level2 = $level;
44588f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4459243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4460daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4461243f3803SJoe Perches		}
4462243f3803SJoe Perches
4463dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4464dc139313SJoe Perches			my $orig = $1;
4465dc139313SJoe Perches			my $level = lc($orig);
4466dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4467dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4468dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4469dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4470dc139313SJoe Perches		}
4471dc139313SJoe Perches
44728020b253SNicolas Boichat# trace_printk should not be used in production code.
44738020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
44748020b253SNicolas Boichat			WARN("TRACE_PRINTK",
44758020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
44768020b253SNicolas Boichat		}
44778020b253SNicolas Boichat
447891c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
447991c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
448091c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
448191c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
448291c9afafSAndy Lutomirski			WARN("ENOSYS",
448391c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
448491c9afafSAndy Lutomirski		}
448591c9afafSAndy Lutomirski
44866b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
44876b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
44886b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
44896b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
44906b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
44916b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
44926b9ea5ffSJakub Kicinski			    $fix) {
44936b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
44946b9ea5ffSJakub Kicinski			}
44956b9ea5ffSJakub Kicinski		}
44966b9ea5ffSJakub Kicinski
4497653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4498653d4876SAndy Whitcroft# or if closed on same line
44995b57980dSJoe Perches		if ($perl_version_ok &&
45002d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
45012d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
45022d453e3bSJoe Perches		    $sline !~ /}/) {
45038d182478SJoe Perches			if (ERROR("OPEN_BRACE",
45042d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
45058d182478SJoe Perches			    $fix) {
45068d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45078d182478SJoe Perches				my $fixed_line = $rawline;
45088d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
45098d182478SJoe Perches				my $line1 = $1;
45108d182478SJoe Perches				my $line2 = $2;
45118d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
45128d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
45138d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
45148d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
45158d182478SJoe Perches				}
45168d182478SJoe Perches			}
45170a920b5bSAndy Whitcroft		}
4518653d4876SAndy Whitcroft
45198905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
45208905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
45218905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
45228d182478SJoe Perches			if (ERROR("OPEN_BRACE",
45238d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
45248d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
45258d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
45268d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45278d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
45288d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
45298d182478SJoe Perches				$fixedline = $rawline;
45308d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
45318d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
45328d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
45338d182478SJoe Perches				}
45348d182478SJoe Perches			}
45358905a67cSAndy Whitcroft		}
45368905a67cSAndy Whitcroft
45370c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
45383705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
45393705ce5bSJoe Perches			if (WARN("SPACING",
45403705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
45413705ce5bSJoe Perches			    $fix) {
4542194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45433705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
45443705ce5bSJoe Perches			}
45450c73b4ebSAndy Whitcroft		}
45460c73b4ebSAndy Whitcroft
454731070b5dSJoe Perches# Function pointer declarations
454831070b5dSJoe Perches# check spacing between type, funcptr, and args
454931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
455091f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
455131070b5dSJoe Perches			my $declare = $1;
455231070b5dSJoe Perches			my $pre_pointer_space = $2;
455331070b5dSJoe Perches			my $post_pointer_space = $3;
455431070b5dSJoe Perches			my $funcname = $4;
455531070b5dSJoe Perches			my $post_funcname_space = $5;
455631070b5dSJoe Perches			my $pre_args_space = $6;
455731070b5dSJoe Perches
455891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
455991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
456091f72e9cSJoe Perches# don't need a space so don't warn for those.
456191f72e9cSJoe Perches			my $post_declare_space = "";
456291f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
456391f72e9cSJoe Perches				$post_declare_space = $1;
456491f72e9cSJoe Perches				$declare = rtrim($declare);
456591f72e9cSJoe Perches			}
456691f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
456731070b5dSJoe Perches				WARN("SPACING",
456831070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
456991f72e9cSJoe Perches				$post_declare_space = " ";
457031070b5dSJoe Perches			}
457131070b5dSJoe Perches
457231070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
457391f72e9cSJoe Perches# This test is not currently implemented because these declarations are
457491f72e9cSJoe Perches# equivalent to
457591f72e9cSJoe Perches#	int  foo(int bar, ...)
457691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
457791f72e9cSJoe Perches#
457891f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
457991f72e9cSJoe Perches#				WARN("SPACING",
458091f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
458191f72e9cSJoe Perches#			}
458231070b5dSJoe Perches
458331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
458431070b5dSJoe Perches			if (defined $pre_pointer_space &&
458531070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
458631070b5dSJoe Perches				WARN("SPACING",
458731070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
458831070b5dSJoe Perches			}
458931070b5dSJoe Perches
459031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
459131070b5dSJoe Perches			if (defined $post_pointer_space &&
459231070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
459331070b5dSJoe Perches				WARN("SPACING",
459431070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
459531070b5dSJoe Perches			}
459631070b5dSJoe Perches
459731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
459831070b5dSJoe Perches			if (defined $post_funcname_space &&
459931070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
460031070b5dSJoe Perches				WARN("SPACING",
460131070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
460231070b5dSJoe Perches			}
460331070b5dSJoe Perches
460431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
460531070b5dSJoe Perches			if (defined $pre_args_space &&
460631070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
460731070b5dSJoe Perches				WARN("SPACING",
460831070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
460931070b5dSJoe Perches			}
461031070b5dSJoe Perches
461131070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4612194f66fcSJoe Perches				$fixed[$fixlinenr] =~
461391f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
461431070b5dSJoe Perches			}
461531070b5dSJoe Perches		}
461631070b5dSJoe Perches
46178d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
46188d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4619fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4620fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
46218d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
46228d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
46238d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4624fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
462538dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
46263705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
46273705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
46283705ce5bSJoe Perches				    $fix) {
4629194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
46303705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
46313705ce5bSJoe Perches				}
46328d31cfceSAndy Whitcroft			}
46338d31cfceSAndy Whitcroft		}
46348d31cfceSAndy Whitcroft
4635f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
46366c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4637c2fdda0dSAndy Whitcroft			my $name = $1;
4638773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4639773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4640c2fdda0dSAndy Whitcroft
4641c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4642773647a0SAndy Whitcroft			if ($name =~ /^(?:
4643773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4644773647a0SAndy Whitcroft				volatile|__volatile__|
4645773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4646773647a0SAndy Whitcroft				asm|__asm__)$/x)
4647773647a0SAndy Whitcroft			{
4648c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4649c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4650c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4651c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4652773647a0SAndy Whitcroft
4653773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4654c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4655c2fdda0dSAndy Whitcroft
4656c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4657c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4658773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4659c2fdda0dSAndy Whitcroft
4660c2fdda0dSAndy Whitcroft			} else {
46613705ce5bSJoe Perches				if (WARN("SPACING",
46623705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
46633705ce5bSJoe Perches					     $fix) {
4664194f66fcSJoe Perches					$fixed[$fixlinenr] =~
46653705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
46663705ce5bSJoe Perches				}
4667f0a594c1SAndy Whitcroft			}
46686c72ffaaSAndy Whitcroft		}
46699a4cad4eSEric Nelson
4670653d4876SAndy Whitcroft# Check operator spacing.
46710a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
46723705ce5bSJoe Perches			my $fixed_line = "";
46733705ce5bSJoe Perches			my $line_fixed = 0;
46743705ce5bSJoe Perches
46759c0ca6f9SAndy Whitcroft			my $ops = qr{
46769c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
46779c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
46789c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
46791f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
468084731623SJoe Perches				\?:|\?|:
46819c0ca6f9SAndy Whitcroft			}x;
4682cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
46833705ce5bSJoe Perches
46843705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
46853705ce5bSJoe Perches##			foreach my $el (@elements) {
46863705ce5bSJoe Perches##				print("el: <$el>\n");
46873705ce5bSJoe Perches##			}
46883705ce5bSJoe Perches
46893705ce5bSJoe Perches			my @fix_elements = ();
469000df344fSAndy Whitcroft			my $off = 0;
46916c72ffaaSAndy Whitcroft
46923705ce5bSJoe Perches			foreach my $el (@elements) {
46933705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
46943705ce5bSJoe Perches				$off += length($el);
46953705ce5bSJoe Perches			}
46963705ce5bSJoe Perches
46973705ce5bSJoe Perches			$off = 0;
46983705ce5bSJoe Perches
46996c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4700b34c648bSJoe Perches			my $last_after = -1;
47016c72ffaaSAndy Whitcroft
47020a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
47033705ce5bSJoe Perches
47043705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
47053705ce5bSJoe Perches
47063705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
47073705ce5bSJoe Perches
47084a0df2efSAndy Whitcroft				$off += length($elements[$n]);
47094a0df2efSAndy Whitcroft
471025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4711773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4712773647a0SAndy Whitcroft				my $cc = '';
4713773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4714773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4715773647a0SAndy Whitcroft				}
4716773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4717773647a0SAndy Whitcroft
47184a0df2efSAndy Whitcroft				my $a = '';
47194a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
47204a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4721cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
47224a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
47234a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4724773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
47254a0df2efSAndy Whitcroft
47260a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
47274a0df2efSAndy Whitcroft
47284a0df2efSAndy Whitcroft				my $c = '';
47290a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
47304a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
47314a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4732cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
47334a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
47344a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
47358b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
47364a0df2efSAndy Whitcroft				} else {
47374a0df2efSAndy Whitcroft					$c = 'E';
47380a920b5bSAndy Whitcroft				}
47390a920b5bSAndy Whitcroft
47404a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
47414a0df2efSAndy Whitcroft
47424a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
47434a0df2efSAndy Whitcroft
47446c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4745de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
47460a920b5bSAndy Whitcroft
474774048ed8SAndy Whitcroft				# Pull out the value of this operator.
47486c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
47490a920b5bSAndy Whitcroft
47501f65f947SAndy Whitcroft				# Get the full operator variant.
47511f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
47521f65f947SAndy Whitcroft
475313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
475413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4755d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
475613214adfSAndy Whitcroft
4757cf655043SAndy Whitcroft#				# Ignore comments
4758cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
475913214adfSAndy Whitcroft
4760d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
476113214adfSAndy Whitcroft				} elsif ($op eq ';') {
4762cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4763cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
47643705ce5bSJoe Perches						if (ERROR("SPACING",
47653705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4766b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
47673705ce5bSJoe Perches							$line_fixed = 1;
47683705ce5bSJoe Perches						}
4769d8aaf121SAndy Whitcroft					}
4770d8aaf121SAndy Whitcroft
4771d8aaf121SAndy Whitcroft				# // is a comment
4772d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
47730a920b5bSAndy Whitcroft
4774b00e4814SJoe Perches				#   :   when part of a bitfield
4775b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4776b00e4814SJoe Perches					# skip the bitfield test for now
4777b00e4814SJoe Perches
47781f65f947SAndy Whitcroft				# No spaces for:
47791f65f947SAndy Whitcroft				#   ->
4780b00e4814SJoe Perches				} elsif ($op eq '->') {
47814a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
47823705ce5bSJoe Perches						if (ERROR("SPACING",
47833705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4784b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
47853705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
47863705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
47873705ce5bSJoe Perches							}
4788b34c648bSJoe Perches							$line_fixed = 1;
47893705ce5bSJoe Perches						}
47900a920b5bSAndy Whitcroft					}
47910a920b5bSAndy Whitcroft
47922381097bSJoe Perches				# , must not have a space before and must have a space on the right.
47930a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
47942381097bSJoe Perches					my $rtrim_before = 0;
47952381097bSJoe Perches					my $space_after = 0;
47962381097bSJoe Perches					if ($ctx =~ /Wx./) {
47972381097bSJoe Perches						if (ERROR("SPACING",
47982381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
47992381097bSJoe Perches							$line_fixed = 1;
48002381097bSJoe Perches							$rtrim_before = 1;
48012381097bSJoe Perches						}
48022381097bSJoe Perches					}
4803cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
48043705ce5bSJoe Perches						if (ERROR("SPACING",
48053705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
48063705ce5bSJoe Perches							$line_fixed = 1;
4807b34c648bSJoe Perches							$last_after = $n;
48082381097bSJoe Perches							$space_after = 1;
48092381097bSJoe Perches						}
48102381097bSJoe Perches					}
48112381097bSJoe Perches					if ($rtrim_before || $space_after) {
48122381097bSJoe Perches						if ($rtrim_before) {
48132381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
48142381097bSJoe Perches						} else {
48152381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
48162381097bSJoe Perches						}
48172381097bSJoe Perches						if ($space_after) {
48182381097bSJoe Perches							$good .= " ";
48193705ce5bSJoe Perches						}
48200a920b5bSAndy Whitcroft					}
48210a920b5bSAndy Whitcroft
48229c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
482374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
48249c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
48259c0ca6f9SAndy Whitcroft
48269c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
48279c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
48289c0ca6f9SAndy Whitcroft				# unary operator, or a cast
48299c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
483074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
48310d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4832cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
48333705ce5bSJoe Perches						if (ERROR("SPACING",
48343705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4835b34c648bSJoe Perches							if ($n != $last_after + 2) {
4836b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
48373705ce5bSJoe Perches								$line_fixed = 1;
48383705ce5bSJoe Perches							}
48390a920b5bSAndy Whitcroft						}
4840b34c648bSJoe Perches					}
4841a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4842171ae1a4SAndy Whitcroft						# A unary '*' may be const
4843171ae1a4SAndy Whitcroft
4844171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
48453705ce5bSJoe Perches						if (ERROR("SPACING",
48463705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4847b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
48483705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
48493705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
48503705ce5bSJoe Perches							}
4851b34c648bSJoe Perches							$line_fixed = 1;
48523705ce5bSJoe Perches						}
48530a920b5bSAndy Whitcroft					}
48540a920b5bSAndy Whitcroft
48550a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
48560a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4857773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
48583705ce5bSJoe Perches						if (ERROR("SPACING",
48593705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4860b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
48613705ce5bSJoe Perches							$line_fixed = 1;
48623705ce5bSJoe Perches						}
48630a920b5bSAndy Whitcroft					}
4864773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4865773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
48663705ce5bSJoe Perches						if (ERROR("SPACING",
48673705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4868b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
48693705ce5bSJoe Perches							$line_fixed = 1;
48703705ce5bSJoe Perches						}
4871653d4876SAndy Whitcroft					}
4872773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
48733705ce5bSJoe Perches						if (ERROR("SPACING",
48743705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4875b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
48763705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
48773705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4878773647a0SAndy Whitcroft							}
4879b34c648bSJoe Perches							$line_fixed = 1;
48803705ce5bSJoe Perches						}
48813705ce5bSJoe Perches					}
48820a920b5bSAndy Whitcroft
48830a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
48849c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
48859c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
48869c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4887c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4888c2fdda0dSAndy Whitcroft					 $op eq '%')
48890a920b5bSAndy Whitcroft				{
4890d2e025f3SJoe Perches					if ($check) {
4891d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4892d2e025f3SJoe Perches							if (CHK("SPACING",
4893d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4894d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4895d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4896d2e025f3SJoe Perches								$line_fixed = 1;
4897d2e025f3SJoe Perches							}
4898d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4899d2e025f3SJoe Perches							if (CHK("SPACING",
4900d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4901d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4902d2e025f3SJoe Perches								$line_fixed = 1;
4903d2e025f3SJoe Perches							}
4904d2e025f3SJoe Perches						}
4905d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
49063705ce5bSJoe Perches						if (ERROR("SPACING",
49073705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4908b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4909b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4910b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4911b34c648bSJoe Perches							}
49123705ce5bSJoe Perches							$line_fixed = 1;
49133705ce5bSJoe Perches						}
49140a920b5bSAndy Whitcroft					}
49150a920b5bSAndy Whitcroft
49161f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
49171f65f947SAndy Whitcroft				# terminating a case value or a label.
49181f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
49191f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
49203705ce5bSJoe Perches						if (ERROR("SPACING",
49213705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4922b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
49233705ce5bSJoe Perches							$line_fixed = 1;
49243705ce5bSJoe Perches						}
49251f65f947SAndy Whitcroft					}
49261f65f947SAndy Whitcroft
49270a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4928cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
49291f65f947SAndy Whitcroft					my $ok = 0;
49301f65f947SAndy Whitcroft
493122f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
49321f65f947SAndy Whitcroft					if (($op eq '<' &&
49331f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
49341f65f947SAndy Whitcroft					    ($op eq '>' &&
49351f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
49361f65f947SAndy Whitcroft					{
49371f65f947SAndy Whitcroft						$ok = 1;
49381f65f947SAndy Whitcroft					}
49391f65f947SAndy Whitcroft
4940e0df7e1fSJoe Perches					# for asm volatile statements
4941e0df7e1fSJoe Perches					# ignore a colon with another
4942e0df7e1fSJoe Perches					# colon immediately before or after
4943e0df7e1fSJoe Perches					if (($op eq ':') &&
4944e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4945e0df7e1fSJoe Perches						$ok = 1;
4946e0df7e1fSJoe Perches					}
4947e0df7e1fSJoe Perches
494884731623SJoe Perches					# messages are ERROR, but ?: are CHK
49491f65f947SAndy Whitcroft					if ($ok == 0) {
49500675a8fbSJean Delvare						my $msg_level = \&ERROR;
49510675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
495284731623SJoe Perches
49530675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
49543705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4955b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4956b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4957b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4958b34c648bSJoe Perches							}
49593705ce5bSJoe Perches							$line_fixed = 1;
49603705ce5bSJoe Perches						}
49610a920b5bSAndy Whitcroft					}
496222f2a2efSAndy Whitcroft				}
49634a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
49643705ce5bSJoe Perches
49653705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
49663705ce5bSJoe Perches
49673705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
49680a920b5bSAndy Whitcroft			}
49693705ce5bSJoe Perches
49703705ce5bSJoe Perches			if (($#elements % 2) == 0) {
49713705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
49723705ce5bSJoe Perches			}
49733705ce5bSJoe Perches
4974194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4975194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
49763705ce5bSJoe Perches			}
49773705ce5bSJoe Perches
49783705ce5bSJoe Perches
49790a920b5bSAndy Whitcroft		}
49800a920b5bSAndy Whitcroft
4981786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4982d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4983786b6326SJoe Perches			if (WARN("SPACING",
4984786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4985786b6326SJoe Perches			    $fix) {
4986194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4987786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4988786b6326SJoe Perches			}
4989786b6326SJoe Perches		}
4990786b6326SJoe Perches
4991f0a594c1SAndy Whitcroft# check for multiple assignments
4992f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4993000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4994000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4995f0a594c1SAndy Whitcroft		}
4996f0a594c1SAndy Whitcroft
499722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
499822f2a2efSAndy Whitcroft## # continuation.
499922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
500022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
500122f2a2efSAndy Whitcroft##
500222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
500322f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
500422f2a2efSAndy Whitcroft## 			my $ln = $line;
500522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
500622f2a2efSAndy Whitcroft## 			}
500722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
5008000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
5009000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
501022f2a2efSAndy Whitcroft## 			}
501122f2a2efSAndy Whitcroft## 		}
5012f0a594c1SAndy Whitcroft
50130a920b5bSAndy Whitcroft#need space before brace following if, while, etc
50146b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
50156ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
50163705ce5bSJoe Perches			if (ERROR("SPACING",
50173705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
50183705ce5bSJoe Perches			    $fix) {
50196ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
50203705ce5bSJoe Perches			}
5021de7d4f0eSAndy Whitcroft		}
5022de7d4f0eSAndy Whitcroft
5023c4a62ef9SJoe Perches## # check for blank lines before declarations
5024c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5025c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
5026c4a62ef9SJoe Perches##			WARN("SPACING",
5027c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
5028c4a62ef9SJoe Perches##		}
5029c4a62ef9SJoe Perches##
5030c4a62ef9SJoe Perches
5031de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
5032de7d4f0eSAndy Whitcroft# on the line
503394fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5034d5e616fcSJoe Perches			if (ERROR("SPACING",
5035d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
5036d5e616fcSJoe Perches			    $fix) {
5037194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5038d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
5039d5e616fcSJoe Perches			}
50400a920b5bSAndy Whitcroft		}
50410a920b5bSAndy Whitcroft
504222f2a2efSAndy Whitcroft# check spacing on square brackets
504322f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
50443705ce5bSJoe Perches			if (ERROR("SPACING",
50453705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
50463705ce5bSJoe Perches			    $fix) {
5047194f66fcSJoe Perches				$fixed[$fixlinenr] =~
50483705ce5bSJoe Perches				    s/\[\s+/\[/;
50493705ce5bSJoe Perches			}
505022f2a2efSAndy Whitcroft		}
505122f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
50523705ce5bSJoe Perches			if (ERROR("SPACING",
50533705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
50543705ce5bSJoe Perches			    $fix) {
5055194f66fcSJoe Perches				$fixed[$fixlinenr] =~
50563705ce5bSJoe Perches				    s/\s+\]/\]/;
50573705ce5bSJoe Perches			}
505822f2a2efSAndy Whitcroft		}
505922f2a2efSAndy Whitcroft
5060c45dcabdSAndy Whitcroft# check spacing on parentheses
50619c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
50629c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
50633705ce5bSJoe Perches			if (ERROR("SPACING",
50643705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
50653705ce5bSJoe Perches			    $fix) {
5066194f66fcSJoe Perches				$fixed[$fixlinenr] =~
50673705ce5bSJoe Perches				    s/\(\s+/\(/;
50683705ce5bSJoe Perches			}
506922f2a2efSAndy Whitcroft		}
507013214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5071c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
5072c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
50733705ce5bSJoe Perches			if (ERROR("SPACING",
50743705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
50753705ce5bSJoe Perches			    $fix) {
5076194f66fcSJoe Perches				$fixed[$fixlinenr] =~
50773705ce5bSJoe Perches				    s/\s+\)/\)/;
50783705ce5bSJoe Perches			}
507922f2a2efSAndy Whitcroft		}
508022f2a2efSAndy Whitcroft
5081e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
5082e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5083e2826fd0SJoe Perches
5084e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5085ea4acbb1SJoe Perches			my $var = $1;
5086ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5087ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
5088ea4acbb1SJoe Perches			    $fix) {
5089ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5090ea4acbb1SJoe Perches			}
5091ea4acbb1SJoe Perches		}
5092ea4acbb1SJoe Perches
5093ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
5094ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
5095ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
5096ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5097ea4acbb1SJoe Perches			my $var = $2;
5098ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5099ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5100ea4acbb1SJoe Perches			    $fix) {
5101ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
5102ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
5103ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5104ea4acbb1SJoe Perches			}
5105e2826fd0SJoe Perches		}
5106e2826fd0SJoe Perches
510763b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
5108a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
5109a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
51105b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
511163b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
511263b7c73eSJoe Perches			my $if_stat = $1;
511363b7c73eSJoe Perches			my $test = substr($2, 1, -1);
511463b7c73eSJoe Perches			my $herectx;
511563b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
511663b7c73eSJoe Perches				my $match = $1;
511763b7c73eSJoe Perches				# avoid parentheses around potential macro args
511863b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
511963b7c73eSJoe Perches				if (!defined($herectx)) {
512063b7c73eSJoe Perches					$herectx = $here . "\n";
512163b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
512263b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
512363b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
512463b7c73eSJoe Perches						$herectx .=  $rl . "\n";
512563b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
512663b7c73eSJoe Perches					}
512763b7c73eSJoe Perches				}
512863b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
512963b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
513063b7c73eSJoe Perches			}
513163b7c73eSJoe Perches		}
513263b7c73eSJoe Perches
51330a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
51344a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
51350a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
51363705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
51373705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
51383705ce5bSJoe Perches			    $fix) {
5139194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51403705ce5bSJoe Perches				    s/^(.)\s+/$1/;
51413705ce5bSJoe Perches			}
51420a920b5bSAndy Whitcroft		}
51430a920b5bSAndy Whitcroft
514440873abaSJoe Perches# check if a statement with a comma should be two statements like:
514540873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
514640873abaSJoe Perches#	bar = baz();
514740873abaSJoe Perches		if (defined($stat) &&
514840873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
514940873abaSJoe Perches			my $cnt = statement_rawlines($stat);
515040873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
515140873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
515240873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
515340873abaSJoe Perches		}
515440873abaSJoe Perches
51555b9553abSJoe Perches# return is not a function
5156507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5157c45dcabdSAndy Whitcroft			my $spacing = $1;
51585b57980dSJoe Perches			if ($perl_version_ok &&
51595b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
51605b9553abSJoe Perches				my $value = $1;
51615b9553abSJoe Perches				$value = deparenthesize($value);
51625b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5163000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5164000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
51655b9553abSJoe Perches				}
5166c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5167000d1cc1SJoe Perches				ERROR("SPACING",
5168000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5169c45dcabdSAndy Whitcroft			}
5170c45dcabdSAndy Whitcroft		}
5171507e5141SJoe Perches
5172b43ae21bSJoe Perches# unnecessary return in a void function
5173b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5174b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5175b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5176b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5177b43ae21bSJoe Perches		    $linenr >= 3 &&
5178b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5179b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
51809819cf25SJoe Perches			WARN("RETURN_VOID",
5181b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
51829819cf25SJoe Perches               }
51839819cf25SJoe Perches
5184189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
51855b57980dSJoe Perches		if ($perl_version_ok &&
5186189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5187189248d8SJoe Perches			my $openparens = $1;
5188189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5189189248d8SJoe Perches			my $msg = "";
5190189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5191189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5192189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5193189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5194189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5195189248d8SJoe Perches			}
5196189248d8SJoe Perches		}
5197189248d8SJoe Perches
5198c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5199c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5200c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5201c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
52025b57980dSJoe Perches		if ($perl_version_ok &&
5203c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5204c5595fa2SJoe Perches			my $lead = $1;
5205c5595fa2SJoe Perches			my $const = $2;
5206c5595fa2SJoe Perches			my $comp = $3;
5207c5595fa2SJoe Perches			my $to = $4;
5208c5595fa2SJoe Perches			my $newcomp = $comp;
5209f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5210c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5211c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5212c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5213c5595fa2SJoe Perches			    $fix) {
5214c5595fa2SJoe Perches				if ($comp eq "<") {
5215c5595fa2SJoe Perches					$newcomp = ">";
5216c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5217c5595fa2SJoe Perches					$newcomp = ">=";
5218c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5219c5595fa2SJoe Perches					$newcomp = "<";
5220c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5221c5595fa2SJoe Perches					$newcomp = "<=";
5222c5595fa2SJoe Perches				}
5223c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5224c5595fa2SJoe Perches			}
5225c5595fa2SJoe Perches		}
5226c5595fa2SJoe Perches
5227f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5228f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
522953a3c448SAndy Whitcroft			my $name = $1;
523053a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
5231000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5232f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
523353a3c448SAndy Whitcroft			}
523453a3c448SAndy Whitcroft		}
5235c45dcabdSAndy Whitcroft
52360a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
52374a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
52383705ce5bSJoe Perches			if (ERROR("SPACING",
52393705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
52403705ce5bSJoe Perches			    $fix) {
5241194f66fcSJoe Perches				$fixed[$fixlinenr] =~
52423705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
52433705ce5bSJoe Perches			}
52440a920b5bSAndy Whitcroft		}
52450a920b5bSAndy Whitcroft
5246f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5247f5fe35ddSAndy Whitcroft# statements after the conditional.
5248170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
52493e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
52503e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
52513e469cdcSAndy Whitcroft					if (!defined $stat);
5252170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5253170d3a22SAndy Whitcroft						$remain_next, $off_next);
5254170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5255170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5256170d3a22SAndy Whitcroft
5257170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5258170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5259170d3a22SAndy Whitcroft				# then count those as offsets.
5260170d3a22SAndy Whitcroft				my ($whitespace) =
5261170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5262170d3a22SAndy Whitcroft				my $offset =
5263170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5264170d3a22SAndy Whitcroft
5265170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5266170d3a22SAndy Whitcroft								$offset} = 1;
5267170d3a22SAndy Whitcroft			}
5268170d3a22SAndy Whitcroft		}
5269170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5270c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5271170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5272171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
52738905a67cSAndy Whitcroft
5274b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
527565b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
527665b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
527765b64b3bSJoe Perches				    $fix && $perl_version_ok) {
527865b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
527965b64b3bSJoe Perches						my $space = $1;
528065b64b3bSJoe Perches						my $not = $2;
528165b64b3bSJoe Perches						my $statement = $3;
528265b64b3bSJoe Perches						my $assigned = $4;
528365b64b3bSJoe Perches						my $test = $8;
528465b64b3bSJoe Perches						my $against = $9;
528565b64b3bSJoe Perches						my $brace = $15;
528665b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
528765b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
528865b64b3bSJoe Perches						my $newline = "${space}if (";
528965b64b3bSJoe Perches						$newline .= '!' if defined($not);
529065b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
529165b64b3bSJoe Perches						$newline .= "$assigned";
529265b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
529365b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
529465b64b3bSJoe Perches						$newline .= ')';
529565b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
529665b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
529765b64b3bSJoe Perches					}
529865b64b3bSJoe Perches				}
52998905a67cSAndy Whitcroft			}
53008905a67cSAndy Whitcroft
53018905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
53028905a67cSAndy Whitcroft			# conditional.
5303773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
53048905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
530513214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
530653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
530753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5308773647a0SAndy Whitcroft			{
5309bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5310bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5311bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
531242bdf74cSHidetoshi Seto				my $stat_real = '';
5313bb44ad39SAndy Whitcroft
531442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
531542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5316bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5317bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5318bb44ad39SAndy Whitcroft				}
5319bb44ad39SAndy Whitcroft
5320000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5321000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
53228905a67cSAndy Whitcroft			}
53238905a67cSAndy Whitcroft		}
53248905a67cSAndy Whitcroft
532513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
532613214adfSAndy Whitcroft		if ($line =~ /
532713214adfSAndy Whitcroft			(?:
532813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
532913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
533013214adfSAndy Whitcroft				(?:\&\&|\|\|)
533113214adfSAndy Whitcroft			|
533213214adfSAndy Whitcroft				(?:\&\&|\|\|)
533313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
533413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
533513214adfSAndy Whitcroft			)/x)
533613214adfSAndy Whitcroft		{
5337000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5338000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
533913214adfSAndy Whitcroft		}
534013214adfSAndy Whitcroft
53418905a67cSAndy Whitcroft# if and else should not have general statements after it
534213214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
534313214adfSAndy Whitcroft			my $s = $1;
534413214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
534513214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5346000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5347000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
53480a920b5bSAndy Whitcroft			}
534913214adfSAndy Whitcroft		}
535039667782SAndy Whitcroft# if should not continue a brace
535139667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5352000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5353048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
535439667782SAndy Whitcroft				$herecurr);
535539667782SAndy Whitcroft		}
5356a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5357a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5358a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
53593fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5360a1080bf8SAndy Whitcroft			\s*return\s+
5361a1080bf8SAndy Whitcroft		    )/xg)
5362a1080bf8SAndy Whitcroft		{
5363000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5364000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5365a1080bf8SAndy Whitcroft		}
53660a920b5bSAndy Whitcroft
53670a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
53680a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
53698b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
53700a920b5bSAndy Whitcroft		    $previndent == $indent) {
53718b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
53728b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
53738b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
53748b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
53758b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
53768b8856f4SJoe Perches				my $fixedline = $prevrawline;
53778b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
53788b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
53798b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
53808b8856f4SJoe Perches				}
53818b8856f4SJoe Perches				$fixedline = $rawline;
53828b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
53838b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
53848b8856f4SJoe Perches			}
53850a920b5bSAndy Whitcroft		}
53860a920b5bSAndy Whitcroft
53878b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5388c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5389c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5390c2fdda0dSAndy Whitcroft
5391c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5392c2fdda0dSAndy Whitcroft			# conditional.
5393773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5394c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5395c2fdda0dSAndy Whitcroft
5396c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
53978b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
53988b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
53998b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
54008b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
54018b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
54028b8856f4SJoe Perches					my $fixedline = $prevrawline;
54038b8856f4SJoe Perches					my $trailing = $rawline;
54048b8856f4SJoe Perches					$trailing =~ s/^\+//;
54058b8856f4SJoe Perches					$trailing = trim($trailing);
54068b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
54078b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54088b8856f4SJoe Perches				}
5409c2fdda0dSAndy Whitcroft			}
5410c2fdda0dSAndy Whitcroft		}
5411c2fdda0dSAndy Whitcroft
541295e2c602SJoe Perches#Specific variable tests
5413323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5414323c1260SJoe Perches			my $var = $1;
541595e2c602SJoe Perches
541695e2c602SJoe Perches#CamelCase
5417807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5418be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
54194104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values
54204104a206SŁukasz Stelmach			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
542122735ce8SJoe Perches#Ignore Page<foo> variants
5422807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5423d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5424d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5425d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5426f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5427f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
54287e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
54297e781f67SJoe Perches					my $word = $1;
54307e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5431d8b07710SJoe Perches					if ($check) {
5432d8b07710SJoe Perches						seed_camelcase_includes();
5433d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5434d8b07710SJoe Perches							seed_camelcase_file($realfile);
5435d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5436d8b07710SJoe Perches						}
5437d8b07710SJoe Perches					}
54387e781f67SJoe Perches					if (!defined $camelcase{$word}) {
54397e781f67SJoe Perches						$camelcase{$word} = 1;
5440be79794bSJoe Perches						CHK("CAMELCASE",
54417e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
54427e781f67SJoe Perches					}
5443323c1260SJoe Perches				}
5444323c1260SJoe Perches			}
54453445686aSJoe Perches		}
54460a920b5bSAndy Whitcroft
54470a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5448d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5449d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5450d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5451d5e616fcSJoe Perches			    $fix) {
5452194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5453d5e616fcSJoe Perches			}
54540a920b5bSAndy Whitcroft		}
54550a920b5bSAndy Whitcroft
54560e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
54570e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5458c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5459e09dec48SAndy Whitcroft			my $file = "$1.h";
5460e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5461e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5462e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
54637840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5464c45dcabdSAndy Whitcroft			{
54650e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
54660e212e0aSFabian Frederick				if ($asminclude > 0) {
5467e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5468000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5469000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5470e09dec48SAndy Whitcroft					} else {
5471000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5472000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5473e09dec48SAndy Whitcroft					}
54740a920b5bSAndy Whitcroft				}
54750a920b5bSAndy Whitcroft			}
54760e212e0aSFabian Frederick		}
54770a920b5bSAndy Whitcroft
5478653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5479653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5480cf655043SAndy Whitcroft# in a known good container
5481b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5482b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5483d8aaf121SAndy Whitcroft			my $ln = $linenr;
5484d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5485c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5486c45dcabdSAndy Whitcroft			my $ctx = '';
548708a2843eSJoe Perches			my $has_flow_statement = 0;
548808a2843eSJoe Perches			my $has_arg_concat = 0;
5489c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5490f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5491f74bd194SAndy Whitcroft			$ctx = $dstat;
5492c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5493a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5494c45dcabdSAndy Whitcroft
549508a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
549662e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
549708a2843eSJoe Perches
5498f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5499f59b64bfSJoe Perches			my $define_args = $1;
5500f59b64bfSJoe Perches			my $define_stmt = $dstat;
5501f59b64bfSJoe Perches			my @def_args = ();
5502f59b64bfSJoe Perches
5503f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5504f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5505f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
55068c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5507f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5508f59b64bfSJoe Perches			}
5509f59b64bfSJoe Perches
5510292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5511c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5512c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5513c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5514c45dcabdSAndy Whitcroft
5515c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
55162e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
55172e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
55182e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5519bf30d6edSAndy Whitcroft			{
5520c45dcabdSAndy Whitcroft			}
5521c45dcabdSAndy Whitcroft
5522342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
552333acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
552433acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5525e45bab8eSAndy Whitcroft			{
5526e45bab8eSAndy Whitcroft			}
5527e45bab8eSAndy Whitcroft
552842e15293SJoe Perches			# Make asm volatile uses seem like a generic function
552942e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
553042e15293SJoe Perches
5531c45dcabdSAndy Whitcroft			my $exceptions = qr{
5532c45dcabdSAndy Whitcroft				$Declare|
5533c45dcabdSAndy Whitcroft				module_param_named|
5534a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5535c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5536c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5537383099fdSAndy Whitcroft				__typeof__\(|
553822fd2d3eSStefani Seibold				union|
553922fd2d3eSStefani Seibold				struct|
5540ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
55416b10df42SVladimir Zapolskiy				^\"|\"$|
55426b10df42SVladimir Zapolskiy				^\[
5543c45dcabdSAndy Whitcroft			}x;
55445eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5545f59b64bfSJoe Perches
5546f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5547f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5548e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5549f59b64bfSJoe Perches
5550f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5551f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5552f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
55533cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5554356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5555f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5556f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5557e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
555872f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
55592e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5560f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5561f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5562f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
55634e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5564f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5565c45dcabdSAndy Whitcroft			{
5566e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5567e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5568e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5569e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5570f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5571f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5572f74bd194SAndy Whitcroft				} else {
5573000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5574388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5575d8aaf121SAndy Whitcroft				}
5576f59b64bfSJoe Perches
5577f59b64bfSJoe Perches			}
55785207649bSJoe Perches
55795207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
55805207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
55815207649bSJoe Perches			my $first = 1;
55825207649bSJoe Perches			$define_stmt = "";
55835207649bSJoe Perches			foreach my $l (@stmt_array) {
55845207649bSJoe Perches				$l =~ s/\\$//;
55855207649bSJoe Perches				if ($first) {
55865207649bSJoe Perches					$define_stmt = $l;
55875207649bSJoe Perches					$first = 0;
55885207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
55895207649bSJoe Perches					$define_stmt .= substr($l, 1);
55905207649bSJoe Perches				}
55915207649bSJoe Perches			}
55925207649bSJoe Perches			$define_stmt =~ s/$;//g;
55935207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
55945207649bSJoe Perches			$define_stmt = trim($define_stmt);
55955207649bSJoe Perches
5596f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5597f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5598f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
55999192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
56007fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
56016dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
56027fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
56037fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5604d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5605f59b64bfSJoe Perches				if ($use_cnt > 1) {
5606f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5607f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5608f59b64bfSJoe Perches				    }
56099192d41aSJoe Perches# check if any macro arguments may have other precedence issues
56107fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
56119192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
56129192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
56139192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
56149192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
56159192d41aSJoe Perches				}
56160a920b5bSAndy Whitcroft			}
56175023d347SJoe Perches
561808a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
561908a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
562008a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
562108a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5622e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
562308a2843eSJoe Perches
562408a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
562508a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
562608a2843eSJoe Perches			}
562708a2843eSJoe Perches
5628481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
56295023d347SJoe Perches
56305023d347SJoe Perches		} else {
56315023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5632481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5633481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
56345023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
56355023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
56365023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
56375023d347SJoe Perches			}
5638653d4876SAndy Whitcroft		}
56390a920b5bSAndy Whitcroft
5640b13edf7fSJoe Perches# do {} while (0) macro tests:
5641b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5642b13edf7fSJoe Perches# macro should not end with a semicolon
56435b57980dSJoe Perches		if ($perl_version_ok &&
5644b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5645b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5646b13edf7fSJoe Perches			my $ln = $linenr;
5647b13edf7fSJoe Perches			my $cnt = $realcnt;
5648b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5649b13edf7fSJoe Perches			my $ctx = '';
5650b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5651b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5652b13edf7fSJoe Perches			$ctx = $dstat;
5653b13edf7fSJoe Perches
5654b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
56551b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5656b13edf7fSJoe Perches
5657b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5658b13edf7fSJoe Perches				my $stmts = $2;
5659b13edf7fSJoe Perches				my $semis = $3;
5660b13edf7fSJoe Perches
5661b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5662b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5663e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5664b13edf7fSJoe Perches
5665ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5666ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5667b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5668b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5669b13edf7fSJoe Perches				}
5670b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5671b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5672b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5673b13edf7fSJoe Perches				}
5674f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5675f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5676f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5677e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5678f5ef95b1SJoe Perches
5679f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5680f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5681b13edf7fSJoe Perches			}
5682b13edf7fSJoe Perches		}
5683b13edf7fSJoe Perches
5684f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
568513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
568613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5687cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
568813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5689cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5690cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5691aad4f614SJoe Perches				my @allowed = ();
5692aad4f614SJoe Perches				my $allow = 0;
569313214adfSAndy Whitcroft				my $seen = 0;
5694773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5695cf655043SAndy Whitcroft				my $ln = $linenr - 1;
569613214adfSAndy Whitcroft				for my $chunk (@chunks) {
569713214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
569813214adfSAndy Whitcroft
5699773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5700773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5701773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5702773647a0SAndy Whitcroft
5703aad4f614SJoe Perches					$allowed[$allow] = 0;
5704773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5705773647a0SAndy Whitcroft
5706773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5707773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5708773647a0SAndy Whitcroft
5709773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5710cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5711cf655043SAndy Whitcroft
5712773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
571313214adfSAndy Whitcroft
571413214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
571513214adfSAndy Whitcroft
5716aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5717cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5718cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5719aad4f614SJoe Perches						$allowed[$allow] = 1;
572013214adfSAndy Whitcroft					}
572113214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5722cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5723aad4f614SJoe Perches						$allowed[$allow] = 1;
572413214adfSAndy Whitcroft					}
5725cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5726cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5727aad4f614SJoe Perches						$allowed[$allow] = 1;
572813214adfSAndy Whitcroft					}
5729aad4f614SJoe Perches					$allow++;
573013214adfSAndy Whitcroft				}
5731aad4f614SJoe Perches				if ($seen) {
5732aad4f614SJoe Perches					my $sum_allowed = 0;
5733aad4f614SJoe Perches					foreach (@allowed) {
5734aad4f614SJoe Perches						$sum_allowed += $_;
5735aad4f614SJoe Perches					}
5736aad4f614SJoe Perches					if ($sum_allowed == 0) {
5737000d1cc1SJoe Perches						WARN("BRACES",
5738000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5739aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5740aad4f614SJoe Perches						 $seen != $allow) {
5741aad4f614SJoe Perches						CHK("BRACES",
5742aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5743aad4f614SJoe Perches					}
574413214adfSAndy Whitcroft				}
574513214adfSAndy Whitcroft			}
574613214adfSAndy Whitcroft		}
5747773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
574813214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5749cf655043SAndy Whitcroft			my $allowed = 0;
5750f0a594c1SAndy Whitcroft
5751cf655043SAndy Whitcroft			# Check the pre-context.
5752cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5753cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5754cf655043SAndy Whitcroft				$allowed = 1;
5755f0a594c1SAndy Whitcroft			}
5756773647a0SAndy Whitcroft
5757773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5758773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5759773647a0SAndy Whitcroft
5760cf655043SAndy Whitcroft			# Check the condition.
5761cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5762773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5763cf655043SAndy Whitcroft			if (defined $cond) {
5764773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5765cf655043SAndy Whitcroft			}
5766cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5767cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5768cf655043SAndy Whitcroft				$allowed = 1;
5769cf655043SAndy Whitcroft			}
5770cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5771cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5772cf655043SAndy Whitcroft				$allowed = 1;
5773cf655043SAndy Whitcroft			}
5774cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5775cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5776cf655043SAndy Whitcroft				$allowed = 1;
5777cf655043SAndy Whitcroft			}
5778cf655043SAndy Whitcroft			# Check the post-context.
5779cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5780cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5781cf655043SAndy Whitcroft				if (defined $cond) {
5782773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5783cf655043SAndy Whitcroft				}
5784cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5785cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5786cf655043SAndy Whitcroft					$allowed = 1;
5787cf655043SAndy Whitcroft				}
5788cf655043SAndy Whitcroft			}
5789cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5790f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5791e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5792cf655043SAndy Whitcroft
5793000d1cc1SJoe Perches				WARN("BRACES",
5794000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5795f0a594c1SAndy Whitcroft			}
5796f0a594c1SAndy Whitcroft		}
5797f0a594c1SAndy Whitcroft
5798e4c5babdSJoe Perches# check for single line unbalanced braces
579995330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
580095330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5801e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5802e4c5babdSJoe Perches		}
5803e4c5babdSJoe Perches
58040979ae66SJoe Perches# check for unnecessary blank lines around braces
580577b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5806f8e58219SJoe Perches			if (CHK("BRACES",
5807f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5808f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5809f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5810f8e58219SJoe Perches			}
58110979ae66SJoe Perches		}
581277b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5813f8e58219SJoe Perches			if (CHK("BRACES",
5814f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5815f8e58219SJoe Perches			    $fix) {
5816f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5817f8e58219SJoe Perches			}
58180979ae66SJoe Perches		}
58190979ae66SJoe Perches
58204a0df2efSAndy Whitcroft# no volatiles please
58216c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
58226c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5823000d1cc1SJoe Perches			WARN("VOLATILE",
58248c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
58254a0df2efSAndy Whitcroft		}
58264a0df2efSAndy Whitcroft
58275e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
58285e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
58295e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
58305e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
583133acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
58325e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
58335e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
58345e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
58355e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
58365e4f6ba5SJoe Perches				     $fix &&
58375e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
58385e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
58395e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
58405e4f6ba5SJoe Perches				my $comma_close = "";
58415e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
58425e4f6ba5SJoe Perches					$comma_close = $1;
58435e4f6ba5SJoe Perches				}
58445e4f6ba5SJoe Perches
58455e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
58465e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
58475e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
58485e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
58495e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
58505e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
58515e4f6ba5SJoe Perches				$fixedline = $rawline;
58525e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
58535e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
58545e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
58555e4f6ba5SJoe Perches				}
58565e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
58575e4f6ba5SJoe Perches			}
58585e4f6ba5SJoe Perches		}
58595e4f6ba5SJoe Perches
58605e4f6ba5SJoe Perches# check for missing a space in a string concatenation
58615e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
58625e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
58635e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
58645e4f6ba5SJoe Perches		}
58655e4f6ba5SJoe Perches
586677cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5867e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5868e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5869e4b7d309SJoe Perches# function declarations
587077cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
587177cb8546SJoe Perches		    defined($context_function) &&
5872e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5873e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
587477cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5875e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
587677cb8546SJoe Perches		}
587777cb8546SJoe Perches
58785e4f6ba5SJoe Perches# check for spaces before a quoted newline
58795e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
58805e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
58815e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
58825e4f6ba5SJoe Perches			    $fix) {
58835e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
58845e4f6ba5SJoe Perches			}
58855e4f6ba5SJoe Perches
58865e4f6ba5SJoe Perches		}
58875e4f6ba5SJoe Perches
5888f17dba4fSJoe Perches# concatenated string without spaces between elements
588979682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
589079682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
589179682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
589279682c0cSJoe Perches			    $fix) {
589379682c0cSJoe Perches				while ($line =~ /($String)/g) {
589479682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
589579682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
589679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
589779682c0cSJoe Perches				}
589879682c0cSJoe Perches			}
5899f17dba4fSJoe Perches		}
5900f17dba4fSJoe Perches
590190ad30e5SJoe Perches# uncoalesced string fragments
590233acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
590379682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
590479682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
590579682c0cSJoe Perches			    $fix) {
590679682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
590779682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
590879682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
590979682c0cSJoe Perches				}
591079682c0cSJoe Perches			}
591190ad30e5SJoe Perches		}
591290ad30e5SJoe Perches
5913522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5914522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5915522b837cSAlexey Dobriyan		my $show_Z = 1;
59165e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5917522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
59185e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5919522b837cSAlexey Dobriyan			# check for %L
5920522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
59215e4f6ba5SJoe Perches				WARN("PRINTF_L",
5922522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5923522b837cSAlexey Dobriyan				$show_L = 0;
59245e4f6ba5SJoe Perches			}
5925522b837cSAlexey Dobriyan			# check for %Z
5926522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5927522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5928522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5929522b837cSAlexey Dobriyan				$show_Z = 0;
5930522b837cSAlexey Dobriyan			}
5931522b837cSAlexey Dobriyan			# check for 0x<decimal>
5932522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5933522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
59346e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
59356e300757SJoe Perches			}
59365e4f6ba5SJoe Perches		}
59375e4f6ba5SJoe Perches
59385e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
59393f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
59405e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
59415e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
59425e4f6ba5SJoe Perches		}
59435e4f6ba5SJoe Perches
594400df344fSAndy Whitcroft# warn about #if 0
5945c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
594660f89010SPrakruthi Deepak Heragu			WARN("IF_0",
594760f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
594860f89010SPrakruthi Deepak Heragu		}
594960f89010SPrakruthi Deepak Heragu
595060f89010SPrakruthi Deepak Heragu# warn about #if 1
595160f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
595260f89010SPrakruthi Deepak Heragu			WARN("IF_1",
595360f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
59544a0df2efSAndy Whitcroft		}
59554a0df2efSAndy Whitcroft
595603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
595703df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5958100425deSJoe Perches			my $tested = quotemeta($1);
5959100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5960100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5961100425deSJoe Perches				my $func = $1;
5962100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5963100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5964100425deSJoe Perches				    $fix) {
5965100425deSJoe Perches					my $do_fix = 1;
5966100425deSJoe Perches					my $leading_tabs = "";
5967100425deSJoe Perches					my $new_leading_tabs = "";
5968100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5969100425deSJoe Perches						$leading_tabs = $1;
5970100425deSJoe Perches					} else {
5971100425deSJoe Perches						$do_fix = 0;
5972100425deSJoe Perches					}
5973100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5974100425deSJoe Perches						$new_leading_tabs = $1;
5975100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5976100425deSJoe Perches							$do_fix = 0;
5977100425deSJoe Perches						}
5978100425deSJoe Perches					} else {
5979100425deSJoe Perches						$do_fix = 0;
5980100425deSJoe Perches					}
5981100425deSJoe Perches					if ($do_fix) {
5982100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5983100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5984100425deSJoe Perches					}
5985100425deSJoe Perches				}
59864c432a8fSGreg Kroah-Hartman			}
59874c432a8fSGreg Kroah-Hartman		}
5988f0a594c1SAndy Whitcroft
5989ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5990ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5991ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5992ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5993ebfdc409SJoe Perches		    $linenr > 3) {
5994ebfdc409SJoe Perches			my $testval = $2;
5995ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5996ebfdc409SJoe Perches
5997ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5998ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5999ebfdc409SJoe Perches
6000e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6001e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
6002ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
6003ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6004ebfdc409SJoe Perches			}
6005ebfdc409SJoe Perches		}
6006ebfdc409SJoe Perches
6007f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
6008dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6009f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6010f78d98f6SJoe Perches			my $level = $1;
6011f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
6012f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
6013f78d98f6SJoe Perches			    $fix) {
6014f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6015f78d98f6SJoe Perches			}
6016f78d98f6SJoe Perches		}
6017f78d98f6SJoe Perches
601845c55e92SJoe Perches# check for logging continuations
601945c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
602045c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
602145c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
602245c55e92SJoe Perches		}
602345c55e92SJoe Perches
6024abb08a53SJoe Perches# check for mask then right shift without a parentheses
60255b57980dSJoe Perches		if ($perl_version_ok &&
6026abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6027abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6028abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
6029abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6030abb08a53SJoe Perches		}
6031abb08a53SJoe Perches
6032b75ac618SJoe Perches# check for pointer comparisons to NULL
60335b57980dSJoe Perches		if ($perl_version_ok) {
6034b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6035b75ac618SJoe Perches				my $val = $1;
6036b75ac618SJoe Perches				my $equal = "!";
6037b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
6038b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
6039b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6040b75ac618SJoe Perches					    $fix) {
6041b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6042b75ac618SJoe Perches				}
6043b75ac618SJoe Perches			}
6044b75ac618SJoe Perches		}
6045b75ac618SJoe Perches
60468716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
60478716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
60488716de38SJoe Perches			my $attr = $1;
60498716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
60508716de38SJoe Perches				my $ptr = $1;
60518716de38SJoe Perches				my $var = $2;
60528716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
60538716de38SJoe Perches				      ERROR("MISPLACED_INIT",
60548716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
60558716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
60568716de38SJoe Perches				      WARN("MISPLACED_INIT",
60578716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
60588716de38SJoe Perches				    $fix) {
6059194f66fcSJoe 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;
60608716de38SJoe Perches				}
60618716de38SJoe Perches			}
60628716de38SJoe Perches		}
60638716de38SJoe Perches
6064e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
6065e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6066e970b884SJoe Perches			my $attr = $1;
6067e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
6068e970b884SJoe Perches			my $attr_prefix = $1;
6069e970b884SJoe Perches			my $attr_type = $2;
6070e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6071e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6072e970b884SJoe Perches			    $fix) {
6073194f66fcSJoe Perches				$fixed[$fixlinenr] =~
6074e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
6075e970b884SJoe Perches			}
6076e970b884SJoe Perches		}
6077e970b884SJoe Perches
6078e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
6079e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6080e970b884SJoe Perches			my $attr = $1;
6081e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6082e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6083e970b884SJoe Perches			    $fix) {
6084194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
6085e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
6086e970b884SJoe Perches				$lead = rtrim($1);
6087e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
6088e970b884SJoe Perches				$lead = "${lead}const ";
6089194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6090e970b884SJoe Perches			}
6091e970b884SJoe Perches		}
6092e970b884SJoe Perches
6093c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
6094c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
6095c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6096c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
6097c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6098c17893c7SJoe Perches			    $fix) {
6099c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6100c17893c7SJoe Perches			}
6101c17893c7SJoe Perches		}
6102c17893c7SJoe Perches
6103fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
6104fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
6105fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6106fbdb8138SJoe Perches			my $constant_func = $1;
6107fbdb8138SJoe Perches			my $func = $constant_func;
6108fbdb8138SJoe Perches			$func =~ s/^__constant_//;
6109fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
6110fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
6111fbdb8138SJoe Perches			    $fix) {
6112194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6113fbdb8138SJoe Perches			}
6114fbdb8138SJoe Perches		}
6115fbdb8138SJoe Perches
61161a15a250SPatrick Pannuto# prefer usleep_range over udelay
611737581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
611843c1d77cSJoe Perches			my $delay = $1;
61191a15a250SPatrick Pannuto			# ignore udelay's < 10, however
612043c1d77cSJoe Perches			if (! ($delay < 10) ) {
6121000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
6122458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
612343c1d77cSJoe Perches			}
612443c1d77cSJoe Perches			if ($delay > 2000) {
612543c1d77cSJoe Perches				WARN("LONG_UDELAY",
612643c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
61271a15a250SPatrick Pannuto			}
61281a15a250SPatrick Pannuto		}
61291a15a250SPatrick Pannuto
613009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
613109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
613209ef8725SPatrick Pannuto			if ($1 < 20) {
6133000d1cc1SJoe Perches				WARN("MSLEEP",
6134458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
613509ef8725SPatrick Pannuto			}
613609ef8725SPatrick Pannuto		}
613709ef8725SPatrick Pannuto
613836ec1939SJoe Perches# check for comparisons of jiffies
613936ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
614036ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
614136ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
614236ec1939SJoe Perches		}
614336ec1939SJoe Perches
61449d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
61459d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
61469d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
61479d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
61489d7a34a5SJoe Perches		}
61499d7a34a5SJoe Perches
615000df344fSAndy Whitcroft# warn about #ifdefs in C files
6151c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
615200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
615300df344fSAndy Whitcroft#			print "$herecurr";
615400df344fSAndy Whitcroft#			$clean = 0;
615500df344fSAndy Whitcroft#		}
615600df344fSAndy Whitcroft
615722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
6158c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
61593705ce5bSJoe Perches			if (ERROR("SPACING",
61603705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
61613705ce5bSJoe Perches			    $fix) {
6162194f66fcSJoe Perches				$fixed[$fixlinenr] =~
61633705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
61643705ce5bSJoe Perches			}
61653705ce5bSJoe Perches
616622f2a2efSAndy Whitcroft		}
616722f2a2efSAndy Whitcroft
61684a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6169171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6170171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
61714a0df2efSAndy Whitcroft			my $which = $1;
61724a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6173000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6174000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
61754a0df2efSAndy Whitcroft			}
61764a0df2efSAndy Whitcroft		}
61774a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6178402c2553SMichael S. Tsirkin
6179402c2553SMichael S. Tsirkin		my $barriers = qr{
6180402c2553SMichael S. Tsirkin			mb|
6181402c2553SMichael S. Tsirkin			rmb|
6182ad83ec6cSWill Deacon			wmb
6183402c2553SMichael S. Tsirkin		}x;
6184402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6185402c2553SMichael S. Tsirkin			mb__before_atomic|
6186402c2553SMichael S. Tsirkin			mb__after_atomic|
6187402c2553SMichael S. Tsirkin			store_release|
6188402c2553SMichael S. Tsirkin			load_acquire|
6189402c2553SMichael S. Tsirkin			store_mb|
6190402c2553SMichael S. Tsirkin			(?:$barriers)
6191402c2553SMichael S. Tsirkin		}x;
6192402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6193402c2553SMichael S. Tsirkin			(?:$barriers)|
619443e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
619543e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6196402c2553SMichael S. Tsirkin		}x;
6197402c2553SMichael S. Tsirkin
6198402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
61994a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6200c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6201000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
62024a0df2efSAndy Whitcroft			}
62034a0df2efSAndy Whitcroft		}
62043ad81779SPaul E. McKenney
6205f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6206f4073b0fSMichael S. Tsirkin
6207f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6208f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6209f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6210f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6211f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6212f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6213f4073b0fSMichael S. Tsirkin		}
6214f4073b0fSMichael S. Tsirkin
6215cb426e99SJoe Perches# check for waitqueue_active without a comment.
6216cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6217cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6218cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6219cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6220cb426e99SJoe Perches			}
6221cb426e99SJoe Perches		}
62223ad81779SPaul E. McKenney
62235099a722SMarco Elver# check for data_race without a comment.
62245099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
62255099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
62265099a722SMarco Elver				WARN("DATA_RACE",
62275099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
62285099a722SMarco Elver			}
62295099a722SMarco Elver		}
62305099a722SMarco Elver
62314a0df2efSAndy Whitcroft# check of hardware specific defines
6232c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6233000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6234000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
62350a920b5bSAndy Whitcroft		}
6236653d4876SAndy Whitcroft
6237596ed45bSJoe Perches# check that the storage class is not after a type
6238596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6239000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6240596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6241596ed45bSJoe Perches		}
6242596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6243596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6244596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6245596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6246596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6247596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6248596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6249d4977c78STobias Klauser		}
6250d4977c78STobias Klauser
6251de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6252de7d4f0eSAndy Whitcroft# storage class and type.
62539c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
62549c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6255000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6256000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6257de7d4f0eSAndy Whitcroft		}
6258de7d4f0eSAndy Whitcroft
62598905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
62602b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
62612b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6262d5e616fcSJoe Perches			if (WARN("INLINE",
6263d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6264d5e616fcSJoe Perches			    $fix) {
6265194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6266d5e616fcSJoe Perches
6267d5e616fcSJoe Perches			}
62688905a67cSAndy Whitcroft		}
62698905a67cSAndy Whitcroft
62707ebe1d17SDwaipayan Ray# Check for compiler attributes
62712b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
62727ebe1d17SDwaipayan Ray		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
62737ebe1d17SDwaipayan Ray			my $attr = $1;
62747ebe1d17SDwaipayan Ray			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
62757ebe1d17SDwaipayan Ray
62767ebe1d17SDwaipayan Ray			my %attr_list = (
62770830aab0SJoe Perches				"alias"				=> "__alias",
62787ebe1d17SDwaipayan Ray				"aligned"			=> "__aligned",
62797ebe1d17SDwaipayan Ray				"always_inline"			=> "__always_inline",
62807ebe1d17SDwaipayan Ray				"assume_aligned"		=> "__assume_aligned",
62817ebe1d17SDwaipayan Ray				"cold"				=> "__cold",
62827ebe1d17SDwaipayan Ray				"const"				=> "__attribute_const__",
62837ebe1d17SDwaipayan Ray				"copy"				=> "__copy",
62847ebe1d17SDwaipayan Ray				"designated_init"		=> "__designated_init",
62857ebe1d17SDwaipayan Ray				"externally_visible"		=> "__visible",
62867ebe1d17SDwaipayan Ray				"format"			=> "printf|scanf",
62877ebe1d17SDwaipayan Ray				"gnu_inline"			=> "__gnu_inline",
62887ebe1d17SDwaipayan Ray				"malloc"			=> "__malloc",
62897ebe1d17SDwaipayan Ray				"mode"				=> "__mode",
62907ebe1d17SDwaipayan Ray				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
62917ebe1d17SDwaipayan Ray				"noclone"			=> "__noclone",
62927ebe1d17SDwaipayan Ray				"noinline"			=> "noinline",
62937ebe1d17SDwaipayan Ray				"nonstring"			=> "__nonstring",
62947ebe1d17SDwaipayan Ray				"noreturn"			=> "__noreturn",
62957ebe1d17SDwaipayan Ray				"packed"			=> "__packed",
62967ebe1d17SDwaipayan Ray				"pure"				=> "__pure",
6297339f29d9SJoe Perches				"section"			=> "__section",
62980830aab0SJoe Perches				"used"				=> "__used",
62990830aab0SJoe Perches				"weak"				=> "__weak"
63007ebe1d17SDwaipayan Ray			);
63017ebe1d17SDwaipayan Ray
63027ebe1d17SDwaipayan Ray			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6303339f29d9SJoe Perches				my $orig_attr = $1;
63047ebe1d17SDwaipayan Ray				my $params = '';
63057ebe1d17SDwaipayan Ray				$params = $2 if defined($2);
6306339f29d9SJoe Perches				my $curr_attr = $orig_attr;
63077ebe1d17SDwaipayan Ray				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
63087ebe1d17SDwaipayan Ray				if (exists($attr_list{$curr_attr})) {
6309339f29d9SJoe Perches					my $new = $attr_list{$curr_attr};
63107ebe1d17SDwaipayan Ray					if ($curr_attr eq "format" && $params) {
63117ebe1d17SDwaipayan Ray						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6312339f29d9SJoe Perches						$new = "__$1\($2";
63137ebe1d17SDwaipayan Ray					} else {
6314339f29d9SJoe Perches						$new = "$new$params";
63157ebe1d17SDwaipayan Ray					}
63167ebe1d17SDwaipayan Ray					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6317339f29d9SJoe Perches						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
63187ebe1d17SDwaipayan Ray					    $fix) {
6319339f29d9SJoe Perches						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6320339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/$remove//;
6321339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6322339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6323339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
63247ebe1d17SDwaipayan Ray					}
632539b7e287SJoe Perches				}
6326462811d9SJoe Perches			}
6327462811d9SJoe Perches
63287ebe1d17SDwaipayan Ray			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
63297ebe1d17SDwaipayan Ray			if ($attr =~ /^_*unused/) {
63307ebe1d17SDwaipayan Ray				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
63317ebe1d17SDwaipayan Ray				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6332d5e616fcSJoe Perches			}
63336061d949SJoe Perches		}
63346061d949SJoe Perches
6335619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
63365b57980dSJoe Perches		if ($perl_version_ok &&
6337619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6338619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6339619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6340619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6341619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6342619a908aSJoe Perches		}
6343619a908aSJoe Perches
6344fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6345e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6346fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6347e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6348e6176fa4SJoe Perches			my $type = $1;
6349e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6350e6176fa4SJoe Perches				$type = $1;
6351e6176fa4SJoe Perches				my $kernel_type = 'u';
6352e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6353e6176fa4SJoe Perches				$type =~ /(\d+)/;
6354e6176fa4SJoe Perches				$kernel_type .= $1;
6355e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6356e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6357e6176fa4SJoe Perches				    $fix) {
6358e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6359e6176fa4SJoe Perches				}
6360e6176fa4SJoe Perches			}
6361e6176fa4SJoe Perches		}
6362e6176fa4SJoe Perches
6363938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6364938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6365938224b5SJoe Perches			my $cast = $1;
6366938224b5SJoe Perches			my $const = $2;
6367938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6368938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6369938224b5SJoe Perches			    $fix) {
6370938224b5SJoe Perches				my $suffix = "";
6371938224b5SJoe Perches				my $newconst = $const;
6372938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6373938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6374938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6375938224b5SJoe Perches					$suffix .= 'LL';
6376938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6377938224b5SJoe Perches					$suffix .= 'L';
6378938224b5SJoe Perches				}
6379938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6380938224b5SJoe Perches			}
6381938224b5SJoe Perches		}
6382938224b5SJoe Perches
63838f53a9b8SJoe Perches# check for sizeof(&)
63848f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6385000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6386000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
63878f53a9b8SJoe Perches		}
63888f53a9b8SJoe Perches
638966c80b60SJoe Perches# check for sizeof without parenthesis
639066c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6391d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6392d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6393d5e616fcSJoe Perches			    $fix) {
6394194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6395d5e616fcSJoe Perches			}
639666c80b60SJoe Perches		}
639766c80b60SJoe Perches
639888982feaSJoe Perches# check for struct spinlock declarations
639988982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
640088982feaSJoe Perches			WARN("USE_SPINLOCK_T",
640188982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
640288982feaSJoe Perches		}
640388982feaSJoe Perches
6404a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
640506668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6406a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6407caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6408caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6409d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6410d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6411d5e616fcSJoe Perches				    $fix) {
6412194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6413d5e616fcSJoe Perches				}
6414a6962d72SJoe Perches			}
6415a6962d72SJoe Perches		}
6416a6962d72SJoe Perches
64170b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
64185b57980dSJoe Perches		if ($perl_version_ok &&
64190b523769SJoe Perches		    defined $stat &&
64200b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
64210b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6422e3c6bc95STobin C. Harding			my $stat_real;
6423e3c6bc95STobin C. Harding
64240b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
64250b523769SJoe Perches			$lc = $lc + $linenr;
64260b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6427ffe07513SJoe Perches				my $specifier;
6428ffe07513SJoe Perches				my $extension;
64293bd32d6aSSakari Ailus				my $qualifier;
6430ffe07513SJoe Perches				my $bad_specifier = "";
64310b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
64320b523769SJoe Perches				$fmt =~ s/%%//g;
6433e3c6bc95STobin C. Harding
64343bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6435e3c6bc95STobin C. Harding					$specifier = $1;
6436e3c6bc95STobin C. Harding					$extension = $2;
64373bd32d6aSSakari Ailus					$qualifier = $3;
6438361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
64393bd32d6aSSakari Ailus					    ($extension eq "f" &&
64403bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6441e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
64420b523769SJoe Perches						last;
64430b523769SJoe Perches					}
6444e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6445e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6446e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
64470b523769SJoe Perches						}
6448e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6449e3c6bc95STobin 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");
6450e3c6bc95STobin C. Harding					}
6451e3c6bc95STobin C. Harding				}
6452e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
64532a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
64541df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
64551df7338aSSergey Senozhatsky					my $use = "";
6456e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
64571df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6458e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
64591df7338aSSergey Senozhatsky					}
64602a9f9d85STobin C. Harding
64610b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6462e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6463e3c6bc95STobin C. Harding				}
64640b523769SJoe Perches			}
64650b523769SJoe Perches		}
64660b523769SJoe Perches
6467554e165cSAndy Whitcroft# Check for misused memsets
64685b57980dSJoe Perches		if ($perl_version_ok &&
6469d1fe9c09SJoe Perches		    defined $stat &&
64709e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6471554e165cSAndy Whitcroft
6472d7c76ba7SJoe Perches			my $ms_addr = $2;
6473d1fe9c09SJoe Perches			my $ms_val = $7;
6474d1fe9c09SJoe Perches			my $ms_size = $12;
6475d7c76ba7SJoe Perches
6476554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6477554e165cSAndy Whitcroft				ERROR("MEMSET",
6478d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6479554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6480554e165cSAndy Whitcroft				WARN("MEMSET",
6481d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6482d7c76ba7SJoe Perches			}
6483d7c76ba7SJoe Perches		}
6484d7c76ba7SJoe Perches
648598a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
64865b57980dSJoe Perches#		if ($perl_version_ok &&
6487f333195dSJoe Perches#		    defined $stat &&
6488f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6489f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6490f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6491f333195dSJoe Perches#			    $fix) {
6492f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6493f333195dSJoe Perches#			}
6494f333195dSJoe Perches#		}
649598a9bba5SJoe Perches
6496b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
64975b57980dSJoe Perches#		if ($perl_version_ok &&
6498f333195dSJoe Perches#		    defined $stat &&
6499f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6500f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6501f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6502f333195dSJoe Perches#		}
6503b6117d17SMateusz Kulikowski
65048617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
65058617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
65065b57980dSJoe Perches#		if ($perl_version_ok &&
6507f333195dSJoe Perches#		    defined $stat &&
6508f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6509f333195dSJoe Perches#
6510f333195dSJoe Perches#			my $ms_val = $7;
6511f333195dSJoe Perches#
6512f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6513f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6514f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6515f333195dSJoe Perches#				    $fix) {
6516f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6517f333195dSJoe Perches#				}
6518f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6519f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6520f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6521f333195dSJoe Perches#				    $fix) {
6522f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6523f333195dSJoe Perches#				}
6524f333195dSJoe Perches#			}
6525f333195dSJoe Perches#		}
65268617cd09SMateusz Kulikowski
6527d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
65285b57980dSJoe Perches		if ($perl_version_ok &&
6529d1fe9c09SJoe Perches		    defined $stat &&
6530d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6531d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6532d7c76ba7SJoe Perches				my $call = $1;
6533d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6534d7c76ba7SJoe Perches				my $arg1 = $3;
6535d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6536d1fe9c09SJoe Perches				my $arg2 = $8;
6537d7c76ba7SJoe Perches				my $cast;
6538d7c76ba7SJoe Perches
6539d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6540d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6541d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6542d7c76ba7SJoe Perches					$cast = $cast1;
6543d7c76ba7SJoe Perches				} else {
6544d7c76ba7SJoe Perches					$cast = $cast2;
6545d7c76ba7SJoe Perches				}
6546d7c76ba7SJoe Perches				WARN("MINMAX",
6547d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6548554e165cSAndy Whitcroft			}
6549554e165cSAndy Whitcroft		}
6550554e165cSAndy Whitcroft
65514a273195SJoe Perches# check usleep_range arguments
65525b57980dSJoe Perches		if ($perl_version_ok &&
65534a273195SJoe Perches		    defined $stat &&
65544a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
65554a273195SJoe Perches			my $min = $1;
65564a273195SJoe Perches			my $max = $7;
65574a273195SJoe Perches			if ($min eq $max) {
65584a273195SJoe Perches				WARN("USLEEP_RANGE",
6559458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
65604a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
65614a273195SJoe Perches				 $min > $max) {
65624a273195SJoe Perches				WARN("USLEEP_RANGE",
6563458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
65644a273195SJoe Perches			}
65654a273195SJoe Perches		}
65664a273195SJoe Perches
6567823b794cSJoe Perches# check for naked sscanf
65685b57980dSJoe Perches		if ($perl_version_ok &&
6569823b794cSJoe Perches		    defined $stat &&
65706c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6571823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6572823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6573823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6574823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6575823b794cSJoe Perches			$lc = $lc + $linenr;
65762a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6577823b794cSJoe Perches			WARN("NAKED_SSCANF",
6578823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6579823b794cSJoe Perches		}
6580823b794cSJoe Perches
6581afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
65825b57980dSJoe Perches		if ($perl_version_ok &&
6583afc819abSJoe Perches		    defined $stat &&
6584afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6585afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6586afc819abSJoe Perches			$lc = $lc + $linenr;
65872a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6588afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6589afc819abSJoe Perches				my $format = $6;
6590afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6591afc819abSJoe Perches				if ($count == 1 &&
6592afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6593afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6594afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6595afc819abSJoe Perches				}
6596afc819abSJoe Perches			}
6597afc819abSJoe Perches		}
6598afc819abSJoe Perches
659970dc8a48SJoe Perches# check for new externs in .h files.
660070dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
660170dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6602d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
660370dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
660470dc8a48SJoe Perches			    $fix) {
6605194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
660670dc8a48SJoe Perches			}
660770dc8a48SJoe Perches		}
660870dc8a48SJoe Perches
6609de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6610171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6611c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6612171ae1a4SAndy Whitcroft		{
6613c45dcabdSAndy Whitcroft			my $function_name = $1;
6614c45dcabdSAndy Whitcroft			my $paren_space = $2;
6615171ae1a4SAndy Whitcroft
6616171ae1a4SAndy Whitcroft			my $s = $stat;
6617171ae1a4SAndy Whitcroft			if (defined $cond) {
6618171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6619171ae1a4SAndy Whitcroft			}
6620d8b44b58SKees Cook			if ($s =~ /^\s*;/)
6621c45dcabdSAndy Whitcroft			{
6622000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6623000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6624de7d4f0eSAndy Whitcroft			}
6625de7d4f0eSAndy Whitcroft
6626171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6627000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6628000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6629171ae1a4SAndy Whitcroft			}
66309c9ba34eSAndy Whitcroft
66319c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
66329c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
66339c9ba34eSAndy Whitcroft		{
6634000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6635000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6636171ae1a4SAndy Whitcroft		}
6637171ae1a4SAndy Whitcroft
6638a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6639a0ad7596SJoe Perches		if (defined $stat &&
6640d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6641d8b44b58SKees Cook		    $1 ne "void") {
6642d8b44b58SKees Cook			my $args = trim($1);
6643ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6644ca0d8929SJoe Perches				my $arg = trim($1);
6645d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6646ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6647ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6648ca0d8929SJoe Perches				}
6649ca0d8929SJoe Perches			}
6650ca0d8929SJoe Perches		}
6651ca0d8929SJoe Perches
6652a0ad7596SJoe Perches# check for function definitions
66535b57980dSJoe Perches		if ($perl_version_ok &&
6654a0ad7596SJoe Perches		    defined $stat &&
6655a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6656a0ad7596SJoe Perches			$context_function = $1;
6657a0ad7596SJoe Perches
6658a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6659a0ad7596SJoe Perches			my $ok = 0;
6660a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6661a0ad7596SJoe Perches			my $herectx = $here . "\n";
6662a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6663a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6664a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6665a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6666a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6667a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6668a0ad7596SJoe Perches			}
6669a0ad7596SJoe Perches			if (!$ok) {
6670a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6671a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6672a0ad7596SJoe Perches			}
6673a0ad7596SJoe Perches		}
6674a0ad7596SJoe Perches
6675de7d4f0eSAndy Whitcroft# checks for new __setup's
6676de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6677de7d4f0eSAndy Whitcroft			my $name = $1;
6678de7d4f0eSAndy Whitcroft
6679de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6680000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
66812581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6682de7d4f0eSAndy Whitcroft			}
6683653d4876SAndy Whitcroft		}
66849c0ca6f9SAndy Whitcroft
6685e29a70f1SJoe Perches# check for pointless casting of alloc functions
6686e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6687000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6688000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
66899c0ca6f9SAndy Whitcroft		}
669013214adfSAndy Whitcroft
6691a640d25cSJoe Perches# alloc style
6692a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
66935b57980dSJoe Perches		if ($perl_version_ok &&
6694e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6695a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6696a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6697a640d25cSJoe Perches		}
6698a640d25cSJoe Perches
669960a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
67005b57980dSJoe Perches		if ($perl_version_ok &&
67011b4a2ed4SJoe Perches		    defined $stat &&
67021b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
670360a55369SJoe Perches			my $oldfunc = $3;
670460a55369SJoe Perches			my $a1 = $4;
670560a55369SJoe Perches			my $a2 = $10;
670660a55369SJoe Perches			my $newfunc = "kmalloc_array";
670760a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
670860a55369SJoe Perches			my $r1 = $a1;
670960a55369SJoe Perches			my $r2 = $a2;
671060a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
671160a55369SJoe Perches				$r1 = $a2;
671260a55369SJoe Perches				$r2 = $a1;
671360a55369SJoe Perches			}
6714e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6715e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
67161b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6717e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6718e3d95a2aSTobin C. Harding
6719e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
67201b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
67211b4a2ed4SJoe Perches				    $cnt == 1 &&
6722e367455aSJoe Perches				    $fix) {
6723194f66fcSJoe 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;
672460a55369SJoe Perches				}
672560a55369SJoe Perches			}
672660a55369SJoe Perches		}
672760a55369SJoe Perches
6728972fdea2SJoe Perches# check for krealloc arg reuse
67295b57980dSJoe Perches		if ($perl_version_ok &&
67304cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
67314cab63ceSJoe Perches		    $1 eq $3) {
6732972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6733972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6734972fdea2SJoe Perches		}
6735972fdea2SJoe Perches
67365ce59ae0SJoe Perches# check for alloc argument mismatch
67375ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
67385ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
67395ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
67405ce59ae0SJoe Perches		}
67415ce59ae0SJoe Perches
6742caf2a54fSJoe Perches# check for multiple semicolons
6743caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6744d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6745d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6746d5e616fcSJoe Perches			    $fix) {
6747194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6748d5e616fcSJoe Perches			}
6749d1e2ad07SJoe Perches		}
6750d1e2ad07SJoe Perches
6751cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6752cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6753cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
67540ab90191SJoe Perches			my $ull = "";
67550ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
67560ab90191SJoe Perches			if (CHK("BIT_MACRO",
67570ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
67580ab90191SJoe Perches			    $fix) {
67590ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
67600ab90191SJoe Perches			}
67610ab90191SJoe Perches		}
67620ab90191SJoe Perches
676350161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
67643e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
676550161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
67663e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
676750161266SJoe Perches		}
676850161266SJoe Perches
67692d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
67703e89ad85SJerome 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*$/) {
67712d632745SJoe Perches			my $config = $1;
67722d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
67733e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
67742d632745SJoe Perches			    $fix) {
67752d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
67762d632745SJoe Perches			}
67772d632745SJoe Perches		}
67782d632745SJoe Perches
6779f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6780f36d3eb8SJoe Perches		my @fallthroughs = (
6781f36d3eb8SJoe Perches			'fallthrough',
6782f36d3eb8SJoe Perches			'@fallthrough@',
6783f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6784f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6785f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6786f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6787f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6788f36d3eb8SJoe Perches		    );
6789f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6790f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6791f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6792f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6793f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6794f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6795f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6796f36d3eb8SJoe Perches					last;
6797f36d3eb8SJoe Perches				}
6798f36d3eb8SJoe Perches			}
6799f36d3eb8SJoe Perches		}
6800f36d3eb8SJoe Perches
6801d1e2ad07SJoe Perches# check for switch/default statements without a break;
68025b57980dSJoe Perches		if ($perl_version_ok &&
6803d1e2ad07SJoe Perches		    defined $stat &&
6804d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6805d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6806e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6807e3d95a2aSTobin C. Harding
6808d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6809d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6810caf2a54fSJoe Perches		}
6811caf2a54fSJoe Perches
681213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6813d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6814d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6815d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6816d5e616fcSJoe Perches			    $fix) {
6817194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6818d5e616fcSJoe Perches			}
681913214adfSAndy Whitcroft		}
6820773647a0SAndy Whitcroft
682162ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
682262ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
682362ec818fSJoe Perches			ERROR("DATE_TIME",
682462ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
682562ec818fSJoe Perches		}
682662ec818fSJoe Perches
68272c92488aSJoe Perches# check for use of yield()
68282c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
68292c92488aSJoe Perches			WARN("YIELD",
68302c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
68312c92488aSJoe Perches		}
68322c92488aSJoe Perches
6833179f8f40SJoe Perches# check for comparisons against true and false
6834179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6835179f8f40SJoe Perches			my $lead = $1;
6836179f8f40SJoe Perches			my $arg = $2;
6837179f8f40SJoe Perches			my $test = $3;
6838179f8f40SJoe Perches			my $otype = $4;
6839179f8f40SJoe Perches			my $trail = $5;
6840179f8f40SJoe Perches			my $op = "!";
6841179f8f40SJoe Perches
6842179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6843179f8f40SJoe Perches
6844179f8f40SJoe Perches			my $type = lc($otype);
6845179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6846179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6847179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6848179f8f40SJoe Perches					$op = "";
6849179f8f40SJoe Perches				}
6850179f8f40SJoe Perches
6851179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6852179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6853179f8f40SJoe Perches
6854179f8f40SJoe Perches## maybe suggesting a correct construct would better
6855179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6856179f8f40SJoe Perches
6857179f8f40SJoe Perches			}
6858179f8f40SJoe Perches		}
6859179f8f40SJoe Perches
68604882720bSThomas Gleixner# check for semaphores initialized locked
68614882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6862000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6863000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6864773647a0SAndy Whitcroft		}
68656712d858SJoe Perches
686667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
686767d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6868000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
686967d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6870773647a0SAndy Whitcroft		}
68716712d858SJoe Perches
6872ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6873f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6874000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6875ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6876f3db6639SMichael Ellerman		}
68776712d858SJoe Perches
68783d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
68793d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
68803d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
68813d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
68823d709ab5SPaul E. McKenney		}
68833d709ab5SPaul E. McKenney
68849189c7e7SJoe Perches# check for deprecated apis
68859189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
68869189c7e7SJoe Perches			my $deprecated_api = $1;
68879189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
68889189c7e7SJoe Perches			WARN("DEPRECATED_API",
68899189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
68909189c7e7SJoe Perches		}
68919189c7e7SJoe Perches
68920f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6893d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
6894ced69da1SQuentin Monnet		if (defined($const_structs) &&
6895ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
6896d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6897000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6898d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
68992b6db5cbSAndy Whitcroft		}
6900773647a0SAndy Whitcroft
6901773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6902773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6903773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6904c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6905c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6906171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6907171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6908171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6909773647a0SAndy Whitcroft		{
6910000d1cc1SJoe Perches			WARN("NR_CPUS",
6911000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6912773647a0SAndy Whitcroft		}
69139c9ba34eSAndy Whitcroft
691452ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
691552ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
691652ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
691752ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
691852ea8506SJoe Perches		}
691952ea8506SJoe Perches
6920acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
69215b57980dSJoe Perches		if ($perl_version_ok &&
6922acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6923acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6924acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6925acd9362cSJoe Perches		}
6926acd9362cSJoe Perches
6927de3f186fSDenis Efremov# nested likely/unlikely calls
6928de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6929de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6930de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6931de3f186fSDenis Efremov		}
6932de3f186fSDenis Efremov
6933691d77b6SAndy Whitcroft# whine mightly about in_atomic
6934691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6935691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6936000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6937000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6938f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6939000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6940000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6941691d77b6SAndy Whitcroft			}
6942691d77b6SAndy Whitcroft		}
69431704f47bSPeter Zijlstra
69440f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
69450f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
69460f5225b0SPeter Zijlstra			ERROR("LOCKING",
69470f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
69480f5225b0SPeter Zijlstra		}
69490f5225b0SPeter Zijlstra
69501704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
69511704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
69521704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
69531704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
69541704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
69551704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6956000d1cc1SJoe Perches				ERROR("LOCKDEP",
6957000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
69581704f47bSPeter Zijlstra			}
69591704f47bSPeter Zijlstra		}
696088f8831cSDave Jones
6961b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6962b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6963000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6964000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
696588f8831cSDave Jones		}
69662435880fSJoe Perches
696700180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
696800180468SJoe Perches# and whether or not function naming is typical and if
696900180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
69705b57980dSJoe Perches		if ($perl_version_ok &&
697100180468SJoe Perches		    defined $stat &&
697200180468SJoe 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*\)/) {
697300180468SJoe Perches			my $var = $1;
697400180468SJoe Perches			my $perms = $2;
697500180468SJoe Perches			my $show = $3;
697600180468SJoe Perches			my $store = $4;
697700180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
697800180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
697900180468SJoe Perches			    $store =~ /^${var}_store$/ &&
698000180468SJoe Perches			    $octal_perms eq "0644") {
698100180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
698200180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
698300180468SJoe Perches				    $fix) {
698400180468SJoe 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})/;
698500180468SJoe Perches				}
698600180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
698700180468SJoe Perches				 $store =~ /^NULL$/ &&
698800180468SJoe Perches				 $octal_perms eq "0444") {
698900180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
699000180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
699100180468SJoe Perches				    $fix) {
699200180468SJoe 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})/;
699300180468SJoe Perches				}
699400180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
699500180468SJoe Perches				 $store =~ /^${var}_store$/ &&
699600180468SJoe Perches				 $octal_perms eq "0200") {
699700180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
699800180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
699900180468SJoe Perches				    $fix) {
700000180468SJoe 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})/;
700100180468SJoe Perches				}
700200180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
700300180468SJoe Perches				 $octal_perms eq "0444" ||
700400180468SJoe Perches				 $octal_perms eq "0200") {
700500180468SJoe Perches				my $newshow = "$show";
700600180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
700700180468SJoe Perches				my $newstore = $store;
700800180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
700900180468SJoe Perches				my $rename = "";
701000180468SJoe Perches				if ($show ne $newshow) {
701100180468SJoe Perches					$rename .= " '$show' to '$newshow'";
701200180468SJoe Perches				}
701300180468SJoe Perches				if ($store ne $newstore) {
701400180468SJoe Perches					$rename .= " '$store' to '$newstore'";
701500180468SJoe Perches				}
701600180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
701700180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
701800180468SJoe Perches			} else {
701900180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
702000180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
702100180468SJoe Perches			}
702200180468SJoe Perches		}
702300180468SJoe Perches
7024515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
7025515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
702673121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
702773121534SJoe Perches#   specific definition of not visible in sysfs.
702873121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
702973121534SJoe Perches#   use the default permissions
70305b57980dSJoe Perches		if ($perl_version_ok &&
7031459cf0aeSJoe Perches		    defined $stat &&
7032515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
70332435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
70342435880fSJoe Perches				my $func = $entry->[0];
70352435880fSJoe Perches				my $arg_pos = $entry->[1];
70362435880fSJoe Perches
7037459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
7038459cf0aeSJoe Perches				$lc = $lc + $linenr;
70392a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
7040459cf0aeSJoe Perches
70412435880fSJoe Perches				my $skip_args = "";
70422435880fSJoe Perches				if ($arg_pos > 1) {
70432435880fSJoe Perches					$arg_pos--;
70442435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
70452435880fSJoe Perches				}
7046f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7047459cf0aeSJoe Perches				if ($stat =~ /$test/) {
70482435880fSJoe Perches					my $val = $1;
70492435880fSJoe Perches					$val = $6 if ($skip_args ne "");
705073121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
705173121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
705273121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
70532435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
7054459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7055f90774e1SJoe Perches					}
7056f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7057c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
7058459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
70592435880fSJoe Perches					}
7060459cf0aeSJoe Perches				}
7061459cf0aeSJoe Perches			}
7062459cf0aeSJoe Perches		}
7063459cf0aeSJoe Perches
7064459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
7065bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
706600180468SJoe Perches			my $oval = $1;
706700180468SJoe Perches			my $octal = perms_to_octal($oval);
7068f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
7069459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7070f90774e1SJoe Perches			    $fix) {
707100180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
70722435880fSJoe Perches			}
707313214adfSAndy Whitcroft		}
70745a6d20ceSBjorn Andersson
70755a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
70765a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
70775a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
70785a6d20ceSBjorn Andersson			my $valid_licenses = qr{
70795a6d20ceSBjorn Andersson						GPL|
70805a6d20ceSBjorn Andersson						GPL\ v2|
70815a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
70825a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
70835a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
70845a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
70855a6d20ceSBjorn Andersson						Proprietary
70865a6d20ceSBjorn Andersson					}x;
70875a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
70885a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
70895a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
70905a6d20ceSBjorn Andersson			}
70915a6d20ceSBjorn Andersson		}
70926a8d76cbSMatteo Croce
70936a8d76cbSMatteo Croce# check for sysctl duplicate constants
70946a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
70956a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
70966a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
70976a8d76cbSMatteo Croce		}
7098515a235eSJoe Perches	}
709913214adfSAndy Whitcroft
710013214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
710113214adfSAndy Whitcroft	# so just keep quiet.
710213214adfSAndy Whitcroft	if ($#rawlines == -1) {
710313214adfSAndy Whitcroft		exit(0);
71040a920b5bSAndy Whitcroft	}
71050a920b5bSAndy Whitcroft
71068905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
71078905a67cSAndy Whitcroft	# things that appear to be patches.
71088905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
71098905a67cSAndy Whitcroft		exit(0);
71108905a67cSAndy Whitcroft	}
71118905a67cSAndy Whitcroft
71128905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
71138905a67cSAndy Whitcroft	# just keep quiet.
71148905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
71158905a67cSAndy Whitcroft		exit(0);
71168905a67cSAndy Whitcroft	}
71178905a67cSAndy Whitcroft
7118a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7119000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
7120000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
71210a920b5bSAndy Whitcroft	}
7122cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
7123cd261496SGeert Uytterhoeven		if ($signoff == 0) {
7124000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
7125000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
712648ca2d8aSDwaipayan Ray		} elsif ($authorsignoff != 1) {
712748ca2d8aSDwaipayan Ray			# authorsignoff values:
712848ca2d8aSDwaipayan Ray			# 0 -> missing sign off
712948ca2d8aSDwaipayan Ray			# 1 -> sign off identical
713048ca2d8aSDwaipayan Ray			# 2 -> names and addresses match, comments mismatch
713148ca2d8aSDwaipayan Ray			# 3 -> addresses match, names different
713248ca2d8aSDwaipayan Ray			# 4 -> names match, addresses different
713348ca2d8aSDwaipayan Ray			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
713448ca2d8aSDwaipayan Ray
713548ca2d8aSDwaipayan Ray			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
713648ca2d8aSDwaipayan Ray
713748ca2d8aSDwaipayan Ray			if ($authorsignoff == 0) {
713848ca2d8aSDwaipayan Ray				ERROR("NO_AUTHOR_SIGN_OFF",
7139cd261496SGeert Uytterhoeven				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
714048ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 2) {
714148ca2d8aSDwaipayan Ray				CHK("FROM_SIGN_OFF_MISMATCH",
714248ca2d8aSDwaipayan Ray				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
714348ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 3) {
714448ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
714548ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
714648ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 4) {
714748ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
714848ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
714948ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 5) {
715048ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
715148ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
715248ca2d8aSDwaipayan Ray			}
7153cd261496SGeert Uytterhoeven		}
71540a920b5bSAndy Whitcroft	}
71550a920b5bSAndy Whitcroft
7156f0a594c1SAndy Whitcroft	print report_dump();
715713214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
715813214adfSAndy Whitcroft		print "$filename " if ($summary_file);
71596c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
71606c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
71616c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
71626c72ffaaSAndy Whitcroft	}
71638905a67cSAndy Whitcroft
7164d2c0a235SAndy Whitcroft	if ($quiet == 0) {
7165ef212196SJoe Perches		# If there were any defects found and not already fixing them
7166ef212196SJoe Perches		if (!$clean and !$fix) {
7167ef212196SJoe Perches			print << "EOM"
7168ef212196SJoe Perches
7169ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
7170ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
7171ef212196SJoe PerchesEOM
7172ef212196SJoe Perches		}
7173d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
7174d2c0a235SAndy Whitcroft		# then suggest that.
7175d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
7176b0781216SMike Frysinger			$rpt_cleaners = 0;
7177d8469f16SJoe Perches			print << "EOM"
7178d8469f16SJoe Perches
7179d8469f16SJoe PerchesNOTE: Whitespace errors detected.
7180d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
7181d8469f16SJoe PerchesEOM
7182d2c0a235SAndy Whitcroft		}
7183d2c0a235SAndy Whitcroft	}
7184d2c0a235SAndy Whitcroft
7185d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
7186d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
7187d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
71889624b8d6SJoe Perches		my $newfile = $filename;
71899624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
71903705ce5bSJoe Perches		my $linecount = 0;
71913705ce5bSJoe Perches		my $f;
71923705ce5bSJoe Perches
7193d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7194d752fcc8SJoe Perches
71953705ce5bSJoe Perches		open($f, '>', $newfile)
71963705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
71973705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
71983705ce5bSJoe Perches			$linecount++;
71993705ce5bSJoe Perches			if ($file) {
72003705ce5bSJoe Perches				if ($linecount > 3) {
72013705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
72023705ce5bSJoe Perches					print $f $fixed_line . "\n";
72033705ce5bSJoe Perches				}
72043705ce5bSJoe Perches			} else {
72053705ce5bSJoe Perches				print $f $fixed_line . "\n";
72063705ce5bSJoe Perches			}
72073705ce5bSJoe Perches		}
72083705ce5bSJoe Perches		close($f);
72093705ce5bSJoe Perches
72103705ce5bSJoe Perches		if (!$quiet) {
72113705ce5bSJoe Perches			print << "EOM";
7212d8469f16SJoe Perches
72133705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
72143705ce5bSJoe Perches
72153705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
72163705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
72173705ce5bSJoe Perches
72183705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
72193705ce5bSJoe PerchesNo warranties, expressed or implied...
72203705ce5bSJoe PerchesEOM
72213705ce5bSJoe Perches		}
72223705ce5bSJoe Perches	}
72233705ce5bSJoe Perches
7224d8469f16SJoe Perches	if ($quiet == 0) {
7225d8469f16SJoe Perches		print "\n";
7226d8469f16SJoe Perches		if ($clean == 1) {
7227d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7228d8469f16SJoe Perches		} else {
7229d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
72300a920b5bSAndy Whitcroft		}
72310a920b5bSAndy Whitcroft	}
72320a920b5bSAndy Whitcroft	return $clean;
72330a920b5bSAndy Whitcroft}
7234