xref: /linux-6.15/scripts/checkpatch.pl (revision 2e44e803)
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;
46c2fdda0dSAndy Whitcroftmy %debug;
473445686aSJoe Perchesmy %camelcase = ();
4891bfe484SJoe Perchesmy %use_type = ();
4991bfe484SJoe Perchesmy @use = ();
5091bfe484SJoe Perchesmy %ignore_type = ();
51000d1cc1SJoe Perchesmy @ignore = ();
5277f5b10aSHannes Edermy $help = 0;
53000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
54bdc48fa1SJoe Perchesmy $max_line_length = 100;
55d62a201fSDave Hansenmy $ignore_perl_version = 0;
56d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5756193274SVadim Bendeburymy $min_conf_desc_length = 4;
5866b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
59ebfd7d62SJoe Perchesmy $codespell = 0;
60f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
61bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
62ced69da1SQuentin Monnetmy $typedefsfile;
63737c0767SJohn Brooksmy $color = "auto";
6498005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
65dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
66dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
67713a09deSAntonio Borneomy $tabsize = 8;
683e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_";
6977f5b10aSHannes Eder
7077f5b10aSHannes Edersub help {
7177f5b10aSHannes Eder	my ($exitcode) = @_;
7277f5b10aSHannes Eder
7377f5b10aSHannes Eder	print << "EOM";
7477f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7577f5b10aSHannes EderVersion: $V
7677f5b10aSHannes Eder
7777f5b10aSHannes EderOptions:
7877f5b10aSHannes Eder  -q, --quiet                quiet
7977f5b10aSHannes Eder  --no-tree                  run without a kernel tree
8077f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
8177f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
8277f5b10aSHannes Eder  --emacs                    emacs compile window format
8377f5b10aSHannes Eder  --terse                    one line per report
8434d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
854a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
864a593c34SDu, Changbin                             single git commit with:
874a593c34SDu, Changbin                               <rev>
884a593c34SDu, Changbin                               <rev>^
894a593c34SDu, Changbin                               <rev>~n
904a593c34SDu, Changbin                             multiple git commits with:
914a593c34SDu, Changbin                               <rev1>..<rev2>
924a593c34SDu, Changbin                               <rev1>...<rev2>
934a593c34SDu, Changbin                               <rev>-<count>
944a593c34SDu, Changbin                             git merges are ignored
9577f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9677f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
973beb42ecSJoe Perches  --list-types               list the possible message types
9891bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
99000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
1003beb42ecSJoe Perches  --show-types               show the specific message type in the output
101bdc48fa1SJoe Perches  --max-line-length=n        set the maximum line length, (default $max_line_length)
102bdc48fa1SJoe Perches                             if exceeded, warn on patches
103bdc48fa1SJoe Perches                             requires --strict for use with --file
10456193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
105bdc48fa1SJoe Perches  --tab-size=n               set the number of spaces for tab (default $tabsize)
10677f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10777f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10877f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10977f5b10aSHannes Eder  --summary-file             include the filename in summary
11077f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
11177f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
11277f5b10aSHannes Eder                             is all off)
11377f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
11477f5b10aSHannes Eder                             literally
1153705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1163705ce5bSJoe Perches                             If correctable single-line errors exist, create
1173705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1183705ce5bSJoe Perches                             with potential errors corrected to the preferred
1193705ce5bSJoe Perches                             checkpatch style
1209624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1219624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1229624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
123d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
124d62a201fSDave Hansen                             runtime errors.
125ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
126f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
127ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12875ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
129737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
130737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
1313e89ad85SJerome Forissier  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
1323e89ad85SJerome Forissier                             ${CONFIG_})
13377f5b10aSHannes Eder  -h, --help, --version      display this help and exit
13477f5b10aSHannes Eder
13577f5b10aSHannes EderWhen FILE is - read standard input.
13677f5b10aSHannes EderEOM
13777f5b10aSHannes Eder
13877f5b10aSHannes Eder	exit($exitcode);
13977f5b10aSHannes Eder}
14077f5b10aSHannes Eder
1413beb42ecSJoe Perchessub uniq {
1423beb42ecSJoe Perches	my %seen;
1433beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1443beb42ecSJoe Perches}
1453beb42ecSJoe Perches
1463beb42ecSJoe Perchessub list_types {
1473beb42ecSJoe Perches	my ($exitcode) = @_;
1483beb42ecSJoe Perches
1493beb42ecSJoe Perches	my $count = 0;
1503beb42ecSJoe Perches
1513beb42ecSJoe Perches	local $/ = undef;
1523beb42ecSJoe Perches
1533beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1543beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1553beb42ecSJoe Perches
1563beb42ecSJoe Perches	my $text = <$script>;
1573beb42ecSJoe Perches	close($script);
1583beb42ecSJoe Perches
1593beb42ecSJoe Perches	my @types = ();
1600547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1610547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1623beb42ecSJoe Perches		push (@types, $_);
1633beb42ecSJoe Perches	}
1643beb42ecSJoe Perches	@types = sort(uniq(@types));
1653beb42ecSJoe Perches	print("#\tMessage type\n\n");
1663beb42ecSJoe Perches	foreach my $type (@types) {
1673beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1683beb42ecSJoe Perches	}
1693beb42ecSJoe Perches
1703beb42ecSJoe Perches	exit($exitcode);
1713beb42ecSJoe Perches}
1723beb42ecSJoe Perches
173000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
174000d1cc1SJoe Perchesif (-f $conf) {
175000d1cc1SJoe Perches	my @conf_args;
176000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
177000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
178000d1cc1SJoe Perches
179000d1cc1SJoe Perches	while (<$conffile>) {
180000d1cc1SJoe Perches		my $line = $_;
181000d1cc1SJoe Perches
182000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
183000d1cc1SJoe Perches		$line =~ s/^\s*//g;
184000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
185000d1cc1SJoe Perches
186000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
187000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
188000d1cc1SJoe Perches
189000d1cc1SJoe Perches		my @words = split(" ", $line);
190000d1cc1SJoe Perches		foreach my $word (@words) {
191000d1cc1SJoe Perches			last if ($word =~ m/^#/);
192000d1cc1SJoe Perches			push (@conf_args, $word);
193000d1cc1SJoe Perches		}
194000d1cc1SJoe Perches	}
195000d1cc1SJoe Perches	close($conffile);
196000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
197000d1cc1SJoe Perches}
198000d1cc1SJoe Perches
199737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
200737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
201737c0767SJohn Brooksforeach (@ARGV) {
202737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
203737c0767SJohn Brooks		$_ = "--color=$color";
204737c0767SJohn Brooks	}
205737c0767SJohn Brooks}
206737c0767SJohn Brooks
2070a920b5bSAndy WhitcroftGetOptions(
2086c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2090a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2100a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2110a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2126c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2138905a67cSAndy Whitcroft	'terse!'	=> \$terse,
21434d8815fSJoe Perches	'showfile!'	=> \$showfile,
21577f5b10aSHannes Eder	'f|file!'	=> \$file,
2164a593c34SDu, Changbin	'g|git!'	=> \$git,
2176c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2186c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
219000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
22091bfe484SJoe Perches	'types=s'	=> \@use,
221000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2223beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2236cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
22456193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
225713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
2266c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2278905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2288905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
22913214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2303705ce5bSJoe Perches	'fix!'		=> \$fix,
2319624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
232d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
233c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
234773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
235ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
236ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
23775ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
238737c0767SJohn Brooks	'color=s'	=> \$color,
239737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
240737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
2413e89ad85SJerome Forissier	'kconfig-prefix=s'	=> \${CONFIG_},
24277f5b10aSHannes Eder	'h|help'	=> \$help,
24377f5b10aSHannes Eder	'version'	=> \$help
24477f5b10aSHannes Eder) or help(1);
24577f5b10aSHannes Eder
24677f5b10aSHannes Ederhelp(0) if ($help);
2470a920b5bSAndy Whitcroft
2483beb42ecSJoe Percheslist_types(0) if ($list_types);
2493beb42ecSJoe Perches
2509624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2512ac73b4fSJoe Perches$check_orig = $check;
2529624b8d6SJoe Perches
25332f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
25432f30ca9SJoe Perches
2550a920b5bSAndy Whitcroftmy $exit = 0;
2560a920b5bSAndy Whitcroft
2575b57980dSJoe Perchesmy $perl_version_ok = 1;
258d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2595b57980dSJoe Perches	$perl_version_ok = 0;
260d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2615b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
262d62a201fSDave Hansen}
263d62a201fSDave Hansen
26445107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2650a920b5bSAndy Whitcroftif ($#ARGV < 0) {
26645107ff6SAllen Hubbe	push(@ARGV, '-');
2670a920b5bSAndy Whitcroft}
2680a920b5bSAndy Whitcroft
269737c0767SJohn Brooksif ($color =~ /^[01]$/) {
270737c0767SJohn Brooks	$color = !$color;
271737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
272737c0767SJohn Brooks	$color = 1;
273737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
274737c0767SJohn Brooks	$color = 0;
275737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
276737c0767SJohn Brooks	$color = (-t STDOUT);
277737c0767SJohn Brooks} else {
27832f30ca9SJoe Perches	die "$P: Invalid color mode: $color\n";
279737c0767SJohn Brooks}
280737c0767SJohn Brooks
281713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
28232f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
283713a09deSAntonio Borneo
28491bfe484SJoe Perchessub hash_save_array_words {
28591bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
28691bfe484SJoe Perches
28791bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
28891bfe484SJoe Perches	foreach my $word (@array) {
289000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
290000d1cc1SJoe Perches		$word =~ s/^\s*//g;
291000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
292000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
293000d1cc1SJoe Perches
294000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
295000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
296000d1cc1SJoe Perches
29791bfe484SJoe Perches		$hashRef->{$word}++;
298000d1cc1SJoe Perches	}
29991bfe484SJoe Perches}
30091bfe484SJoe Perches
30191bfe484SJoe Perchessub hash_show_words {
30291bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
30391bfe484SJoe Perches
3043c816e49SJoe Perches	if (keys %$hashRef) {
305d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
30658cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
30791bfe484SJoe Perches			print " $word";
30891bfe484SJoe Perches		}
309d8469f16SJoe Perches		print "\n";
31091bfe484SJoe Perches	}
31191bfe484SJoe Perches}
31291bfe484SJoe Perches
31391bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
31491bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
315000d1cc1SJoe Perches
316c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
317c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3187429c690SAndy Whitcroftmy $dbg_type = 0;
319a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
320c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
32121caa13cSAndy Whitcroft	## no critic
32221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
32321caa13cSAndy Whitcroft	die "$@" if ($@);
324c2fdda0dSAndy Whitcroft}
325c2fdda0dSAndy Whitcroft
326d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
327d2c0a235SAndy Whitcroft
3288905a67cSAndy Whitcroftif ($terse) {
3298905a67cSAndy Whitcroft	$emacs = 1;
3308905a67cSAndy Whitcroft	$quiet++;
3318905a67cSAndy Whitcroft}
3328905a67cSAndy Whitcroft
3336c72ffaaSAndy Whitcroftif ($tree) {
3346c72ffaaSAndy Whitcroft	if (defined $root) {
3356c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3366c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3376c72ffaaSAndy Whitcroft		}
3386c72ffaaSAndy Whitcroft	} else {
3396c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3406c72ffaaSAndy Whitcroft			$root = '.';
3416c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3426c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3436c72ffaaSAndy Whitcroft			$root = $1;
3446c72ffaaSAndy Whitcroft		}
3456c72ffaaSAndy Whitcroft	}
3466c72ffaaSAndy Whitcroft
3476c72ffaaSAndy Whitcroft	if (!defined $root) {
3480a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3490a920b5bSAndy Whitcroft		exit(2);
3500a920b5bSAndy Whitcroft	}
3516c72ffaaSAndy Whitcroft}
3526c72ffaaSAndy Whitcroft
3536c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3546c72ffaaSAndy Whitcroft
3552ceb532bSAndy Whitcroftour $Ident	= qr{
3562ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3572ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3582ceb532bSAndy Whitcroft		}x;
3596c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3606c72ffaaSAndy Whitcroftour $Sparse	= qr{
3616c72ffaaSAndy Whitcroft			__user|
3626c72ffaaSAndy Whitcroft			__kernel|
3636c72ffaaSAndy Whitcroft			__force|
3646c72ffaaSAndy Whitcroft			__iomem|
3656c72ffaaSAndy Whitcroft			__must_check|
366417495edSAndy Whitcroft			__kprobes|
367165e72a6SSven Eckelmann			__ref|
36833aa4597SGeert Uytterhoeven			__refconst|
36933aa4597SGeert Uytterhoeven			__refdata|
370ad315455SBoqun Feng			__rcu|
371ad315455SBoqun Feng			__private
3726c72ffaaSAndy Whitcroft		}x;
373e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
374e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
375e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
376e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
377e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3788716de38SJoe Perches
37952131292SWolfram Sang# Notes to $Attribute:
38052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3816c72ffaaSAndy Whitcroftour $Attribute	= qr{
3826c72ffaaSAndy Whitcroft			const|
38303f1df7dSJoe Perches			__percpu|
38403f1df7dSJoe Perches			__nocast|
38503f1df7dSJoe Perches			__safe|
38646d832f5SMichael S. Tsirkin			__bitwise|
38703f1df7dSJoe Perches			__packed__|
38803f1df7dSJoe Perches			__packed2__|
38903f1df7dSJoe Perches			__naked|
39003f1df7dSJoe Perches			__maybe_unused|
39103f1df7dSJoe Perches			__always_unused|
39203f1df7dSJoe Perches			__noreturn|
39303f1df7dSJoe Perches			__used|
39403f1df7dSJoe Perches			__cold|
395e23ef1f3SJoe Perches			__pure|
39603f1df7dSJoe Perches			__noclone|
39703f1df7dSJoe Perches			__deprecated|
3986c72ffaaSAndy Whitcroft			__read_mostly|
399c5967e98SJoe Perches			__ro_after_init|
4006c72ffaaSAndy Whitcroft			__kprobes|
4018716de38SJoe Perches			$InitAttribute|
40224e1d81aSAndy Whitcroft			____cacheline_aligned|
40324e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
4045fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
4055fe3af11SAndy Whitcroft			__weak
4066c72ffaaSAndy Whitcroft		  }x;
407c45dcabdSAndy Whitcroftour $Modifier;
40891cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
4096c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
4106c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
4116c72ffaaSAndy Whitcroft
41295e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
41395e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
41495e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
41595e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4162435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
417c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
418326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
419326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
420326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
42174349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4222435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
423326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
424447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
42523f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4266c72ffaaSAndy Whitcroftour $Operators	= qr{
4276c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4286c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
42923f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4306c72ffaaSAndy Whitcroft		  }x;
4316c72ffaaSAndy Whitcroft
43291cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
43391cb5195SJoe Perches
434ab7e23f3SJoe Perchesour $BasicType;
4358905a67cSAndy Whitcroftour $NonptrType;
4361813087dSJoe Perchesour $NonptrTypeMisordered;
4378716de38SJoe Perchesour $NonptrTypeWithAttr;
4388905a67cSAndy Whitcroftour $Type;
4391813087dSJoe Perchesour $TypeMisordered;
4408905a67cSAndy Whitcroftour $Declare;
4411813087dSJoe Perchesour $DeclareMisordered;
4428905a67cSAndy Whitcroft
44315662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
44415662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
445171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
446171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
447171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
448171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
449171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
450171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
451171ae1a4SAndy Whitcroft}x;
452171ae1a4SAndy Whitcroft
45315662b3eSJoe Perchesour $UTF8	= qr{
45415662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
45515662b3eSJoe Perches	| $NON_ASCII_UTF8
45615662b3eSJoe Perches}x;
45715662b3eSJoe Perches
458e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
459021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
460021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
461021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
462021158b4SJoe Perches)};
463e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
464fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4658ed22cadSAndy Whitcroft	atomic_t
4668ed22cadSAndy Whitcroft)};
467e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
468e6176fa4SJoe Perches	$typeC99Typedefs\b|
469e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
470e6176fa4SJoe Perches	$typeKernelTypedefs\b
471e6176fa4SJoe Perches)};
4728ed22cadSAndy Whitcroft
4736d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4746d32f7a3SJoe Perches
475691e669bSJoe Perchesour $logFunctions = qr{(?x:
476758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4777d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
47887bd499aSJoe Perches	TP_printk|
4796e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
480b0531722SJoe Perches	panic|
48106668727SJoe Perches	MODULE_[A-Z_]+|
48206668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
483691e669bSJoe Perches)};
484691e669bSJoe Perches
485e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
486e29a70f1SJoe Perches	(?:(?:devm_)?
487e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
488e29a70f1SJoe Perches		kstrdup(?:_const)? |
489e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
490461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
491e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
492e29a70f1SJoe Perches	dma_alloc_coherent
493e29a70f1SJoe Perches)};
494e29a70f1SJoe Perches
49520112475SJoe Perchesour $signature_tags = qr{(?xi:
49620112475SJoe Perches	Signed-off-by:|
497d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
49820112475SJoe Perches	Acked-by:|
49920112475SJoe Perches	Tested-by:|
50020112475SJoe Perches	Reviewed-by:|
50120112475SJoe Perches	Reported-by:|
5028543ae12SMugunthan V N	Suggested-by:|
50320112475SJoe Perches	To:|
50420112475SJoe Perches	Cc:
50520112475SJoe Perches)};
50620112475SJoe Perches
5071813087dSJoe Perchesour @typeListMisordered = (
5081813087dSJoe Perches	qr{char\s+(?:un)?signed},
5091813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
5101813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
5111813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
5121813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
5131813087dSJoe Perches	qr{short\s+(?:un)?signed},
5141813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5151813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5161813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5171813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5181813087dSJoe Perches	qr{int\s+(?:un)?signed},
5191813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5201813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5211813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5221813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5231813087dSJoe Perches	qr{long\s+(?:un)?signed},
5241813087dSJoe Perches);
5251813087dSJoe Perches
5268905a67cSAndy Whitcroftour @typeList = (
5278905a67cSAndy Whitcroft	qr{void},
5280c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5290c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5300c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5310c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5320c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5330c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5340c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5350c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5360c773d9dSJoe Perches	qr{(?:un)?signed},
5378905a67cSAndy Whitcroft	qr{float},
5388905a67cSAndy Whitcroft	qr{double},
5398905a67cSAndy Whitcroft	qr{bool},
5408905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5418905a67cSAndy Whitcroft	qr{union\s+$Ident},
5428905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5438905a67cSAndy Whitcroft	qr{${Ident}_t},
5448905a67cSAndy Whitcroft	qr{${Ident}_handler},
5458905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5461813087dSJoe Perches	@typeListMisordered,
5478905a67cSAndy Whitcroft);
548938224b5SJoe Perches
549938224b5SJoe Perchesour $C90_int_types = qr{(?x:
550938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
551938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
552938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
553938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
554938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
555938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
556938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
557938224b5SJoe Perches
558938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
559938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
560938224b5SJoe Perches	long\s+(?:un)?signed|
561938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
562938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
563938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
564938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
565938224b5SJoe Perches
566938224b5SJoe Perches	int\s+(?:un)?signed|
567938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
568938224b5SJoe Perches)};
569938224b5SJoe Perches
570485ff23eSAlex Dowadour @typeListFile = ();
5718716de38SJoe Perchesour @typeListWithAttr = (
5728716de38SJoe Perches	@typeList,
5738716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5748716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5758716de38SJoe Perches);
5768716de38SJoe Perches
577c45dcabdSAndy Whitcroftour @modifierList = (
578c45dcabdSAndy Whitcroft	qr{fastcall},
579c45dcabdSAndy Whitcroft);
580485ff23eSAlex Dowadour @modifierListFile = ();
5818905a67cSAndy Whitcroft
5822435880fSJoe Perchesour @mode_permission_funcs = (
5832435880fSJoe Perches	["module_param", 3],
5842435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5852435880fSJoe Perches	["module_param_array_named", 5],
5862435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5872435880fSJoe Perches	["proc_create(?:_data|)", 2],
588459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
589459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
590459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
591459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
592459cf0aeSJoe Perches	["__ATTR", 2],
5932435880fSJoe Perches);
5942435880fSJoe Perches
5951a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
5961a3dcf2eSJoe Perches
597515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
598515a235eSJoe Perchesour $mode_perms_search = "";
599515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
600515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
601515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
602515a235eSJoe Perches}
60300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
604515a235eSJoe Perches
6059189c7e7SJoe Perchesour %deprecated_apis = (
6069189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
6079189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
6089189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
6099189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
6109189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
6119189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
6129189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
6139189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
6149189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
6159189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6169189c7e7SJoe Perches);
6179189c7e7SJoe Perches
6189189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6199189c7e7SJoe Perchesour $deprecated_apis_search = "";
6209189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6219189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6229189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6239189c7e7SJoe Perches}
6249189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6259189c7e7SJoe Perches
626b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
627b392c64fSJoe Perches	S_IWUGO		|
628b392c64fSJoe Perches	S_IWOTH		|
629b392c64fSJoe Perches	S_IRWXUGO	|
630b392c64fSJoe Perches	S_IALLUGO	|
631b392c64fSJoe Perches	0[0-7][0-7][2367]
632b392c64fSJoe Perches}x;
633b392c64fSJoe Perches
634f90774e1SJoe Perchesour %mode_permission_string_types = (
635f90774e1SJoe Perches	"S_IRWXU" => 0700,
636f90774e1SJoe Perches	"S_IRUSR" => 0400,
637f90774e1SJoe Perches	"S_IWUSR" => 0200,
638f90774e1SJoe Perches	"S_IXUSR" => 0100,
639f90774e1SJoe Perches	"S_IRWXG" => 0070,
640f90774e1SJoe Perches	"S_IRGRP" => 0040,
641f90774e1SJoe Perches	"S_IWGRP" => 0020,
642f90774e1SJoe Perches	"S_IXGRP" => 0010,
643f90774e1SJoe Perches	"S_IRWXO" => 0007,
644f90774e1SJoe Perches	"S_IROTH" => 0004,
645f90774e1SJoe Perches	"S_IWOTH" => 0002,
646f90774e1SJoe Perches	"S_IXOTH" => 0001,
647f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
648f90774e1SJoe Perches	"S_IRUGO" => 0444,
649f90774e1SJoe Perches	"S_IWUGO" => 0222,
650f90774e1SJoe Perches	"S_IXUGO" => 0111,
651f90774e1SJoe Perches);
652f90774e1SJoe Perches
653f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
654f90774e1SJoe Perchesour $mode_perms_string_search = "";
655f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
656f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
657f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
658f90774e1SJoe Perches}
65900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
66000180468SJoe Perchesour $multi_mode_perms_string_search = qr{
66100180468SJoe Perches	${single_mode_perms_string_search}
66200180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
66300180468SJoe Perches}x;
66400180468SJoe Perches
66500180468SJoe Perchessub perms_to_octal {
66600180468SJoe Perches	my ($string) = @_;
66700180468SJoe Perches
66800180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
66900180468SJoe Perches
67000180468SJoe Perches	my $val = "";
67100180468SJoe Perches	my $oval = "";
67200180468SJoe Perches	my $to = 0;
67300180468SJoe Perches	my $curpos = 0;
67400180468SJoe Perches	my $lastpos = 0;
67500180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
67600180468SJoe Perches		$curpos = pos($string);
67700180468SJoe Perches		my $match = $2;
67800180468SJoe Perches		my $omatch = $1;
67900180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
68000180468SJoe Perches		$lastpos = $curpos;
68100180468SJoe Perches		$to |= $mode_permission_string_types{$match};
68200180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
68300180468SJoe Perches		$val .= $match;
68400180468SJoe Perches		$oval .= $omatch;
68500180468SJoe Perches	}
68600180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
68700180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
68800180468SJoe Perches	return sprintf("%04o", $to);
68900180468SJoe Perches}
690f90774e1SJoe Perches
6917840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6927840a94cSWolfram Sang	irq|
693cdcee686SSergey Ryazanov	memory|
694cdcee686SSergey Ryazanov	time|
695cdcee686SSergey Ryazanov	reboot
6967840a94cSWolfram Sang)};
6977840a94cSWolfram Sang# memory.h: ARM has a custom one
6987840a94cSWolfram Sang
69966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
70066b47b4aSKees Cookmy $misspellings;
70166b47b4aSKees Cookmy %spelling_fix;
70236061e38SJoe Perches
70336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
70466b47b4aSKees Cook	while (<$spelling>) {
70566b47b4aSKees Cook		my $line = $_;
70666b47b4aSKees Cook
70766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
70866b47b4aSKees Cook		$line =~ s/^\s*//g;
70966b47b4aSKees Cook
71066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
71166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
71266b47b4aSKees Cook
71366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
71466b47b4aSKees Cook
71566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
71666b47b4aSKees Cook	}
71766b47b4aSKees Cook	close($spelling);
71836061e38SJoe Perches} else {
71936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
72036061e38SJoe Perches}
72166b47b4aSKees Cook
722ebfd7d62SJoe Perchesif ($codespell) {
723ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
724ebfd7d62SJoe Perches		while (<$spelling>) {
725ebfd7d62SJoe Perches			my $line = $_;
726ebfd7d62SJoe Perches
727ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
728ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
729ebfd7d62SJoe Perches
730ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
731ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
732ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
733ebfd7d62SJoe Perches
734ebfd7d62SJoe Perches			$line =~ s/,.*$//;
735ebfd7d62SJoe Perches
736ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
737ebfd7d62SJoe Perches
738ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
739ebfd7d62SJoe Perches		}
740ebfd7d62SJoe Perches		close($spelling);
741ebfd7d62SJoe Perches	} else {
742ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
743ebfd7d62SJoe Perches	}
744ebfd7d62SJoe Perches}
745ebfd7d62SJoe Perches
746ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
747ebfd7d62SJoe Perches
74875ad8c57SJerome Forissiersub read_words {
74975ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
75075ad8c57SJerome Forissier
75175ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
75275ad8c57SJerome Forissier		while (<$words>) {
753bf1fa1daSJoe Perches			my $line = $_;
754bf1fa1daSJoe Perches
755bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
756bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
757bf1fa1daSJoe Perches
758bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
759bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
760bf1fa1daSJoe Perches			if ($line =~ /\s/) {
76175ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
762bf1fa1daSJoe Perches				next;
763bf1fa1daSJoe Perches			}
764bf1fa1daSJoe Perches
765ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
76675ad8c57SJerome Forissier			$$wordsRef .= $line;
767bf1fa1daSJoe Perches		}
76875ad8c57SJerome Forissier		close($file);
76975ad8c57SJerome Forissier		return 1;
770bf1fa1daSJoe Perches	}
771bf1fa1daSJoe Perches
77275ad8c57SJerome Forissier	return 0;
77375ad8c57SJerome Forissier}
77475ad8c57SJerome Forissier
775ced69da1SQuentin Monnetmy $const_structs;
776ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
77775ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
77875ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
779ced69da1SQuentin Monnet}
78075ad8c57SJerome Forissier
781ced69da1SQuentin Monnetif (defined($typedefsfile)) {
782ced69da1SQuentin Monnet	my $typeOtherTypedefs;
78375ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
78475ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
785ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
78675ad8c57SJerome Forissier}
78775ad8c57SJerome Forissier
7888905a67cSAndy Whitcroftsub build_types {
789485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
790485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7911813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7928716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
793c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
794ab7e23f3SJoe Perches	$BasicType	= qr{
795ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
796ab7e23f3SJoe Perches				(?:${all}\b)
797ab7e23f3SJoe Perches		}x;
7988905a67cSAndy Whitcroft	$NonptrType	= qr{
799d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
800cf655043SAndy Whitcroft			(?:
8016b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
8028ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
803c45dcabdSAndy Whitcroft				(?:${all}\b)
804cf655043SAndy Whitcroft			)
805c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
8068905a67cSAndy Whitcroft		  }x;
8071813087dSJoe Perches	$NonptrTypeMisordered	= qr{
8081813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
8091813087dSJoe Perches			(?:
8101813087dSJoe Perches				(?:${Misordered}\b)
8111813087dSJoe Perches			)
8121813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
8131813087dSJoe Perches		  }x;
8148716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
8158716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
8168716de38SJoe Perches			(?:
8178716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8188716de38SJoe Perches				(?:$typeTypedefs\b)|
8198716de38SJoe Perches				(?:${allWithAttr}\b)
8208716de38SJoe Perches			)
8218716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8228716de38SJoe Perches		  }x;
8238905a67cSAndy Whitcroft	$Type	= qr{
824c45dcabdSAndy Whitcroft			$NonptrType
8257b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
826c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8278905a67cSAndy Whitcroft		  }x;
8281813087dSJoe Perches	$TypeMisordered	= qr{
8291813087dSJoe Perches			$NonptrTypeMisordered
8307b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
8311813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8321813087dSJoe Perches		  }x;
83391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8341813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8358905a67cSAndy Whitcroft}
8368905a67cSAndy Whitcroftbuild_types();
8376c72ffaaSAndy Whitcroft
8387d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
839d1fe9c09SJoe Perches
840d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
841d1fe9c09SJoe Perches# requires at least perl version v5.10.0
842d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
843d1fe9c09SJoe Perches
844d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8452435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
846c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8477d2367afSJoe Perches
848f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8493e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
850fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8513d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
852f8422308SJoe Perches)};
853f8422308SJoe Perches
8547d2367afSJoe Perchessub deparenthesize {
8557d2367afSJoe Perches	my ($string) = @_;
8567d2367afSJoe Perches	return "" if (!defined($string));
8575b9553abSJoe Perches
8585b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8595b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8605b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8615b9553abSJoe Perches	}
8625b9553abSJoe Perches
8637d2367afSJoe Perches	$string =~ s@\s+@ @g;
8645b9553abSJoe Perches
8657d2367afSJoe Perches	return $string;
8667d2367afSJoe Perches}
8677d2367afSJoe Perches
8683445686aSJoe Perchessub seed_camelcase_file {
8693445686aSJoe Perches	my ($file) = @_;
8703445686aSJoe Perches
8713445686aSJoe Perches	return if (!(-f $file));
8723445686aSJoe Perches
8733445686aSJoe Perches	local $/;
8743445686aSJoe Perches
8753445686aSJoe Perches	open(my $include_file, '<', "$file")
8763445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8773445686aSJoe Perches	my $text = <$include_file>;
8783445686aSJoe Perches	close($include_file);
8793445686aSJoe Perches
8803445686aSJoe Perches	my @lines = split('\n', $text);
8813445686aSJoe Perches
8823445686aSJoe Perches	foreach my $line (@lines) {
8833445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8843445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8853445686aSJoe Perches			$camelcase{$1} = 1;
88611ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
88711ea516aSJoe Perches			$camelcase{$1} = 1;
88811ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8893445686aSJoe Perches			$camelcase{$1} = 1;
8903445686aSJoe Perches		}
8913445686aSJoe Perches	}
8923445686aSJoe Perches}
8933445686aSJoe Perches
894cd28b119SJoe Perchesour %maintained_status = ();
895cd28b119SJoe Perches
89685b0ee18SJoe Perchessub is_maintained_obsolete {
89785b0ee18SJoe Perches	my ($filename) = @_;
89885b0ee18SJoe Perches
899f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
90085b0ee18SJoe Perches
901cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
902cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
903cd28b119SJoe Perches	}
90485b0ee18SJoe Perches
905cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
90685b0ee18SJoe Perches}
90785b0ee18SJoe Perches
9083b6e8ac9SJoe Perchessub is_SPDX_License_valid {
9093b6e8ac9SJoe Perches	my ($license) = @_;
9103b6e8ac9SJoe Perches
91156294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
9123b6e8ac9SJoe Perches
91356294112SJoe Perches	my $root_path = abs_path($root);
91456294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
9153b6e8ac9SJoe Perches	return 0 if ($status ne "");
9163b6e8ac9SJoe Perches	return 1;
9173b6e8ac9SJoe Perches}
9183b6e8ac9SJoe Perches
9193445686aSJoe Perchesmy $camelcase_seeded = 0;
9203445686aSJoe Perchessub seed_camelcase_includes {
9213445686aSJoe Perches	return if ($camelcase_seeded);
9223445686aSJoe Perches
9233445686aSJoe Perches	my $files;
924c707a81dSJoe Perches	my $camelcase_cache = "";
925c707a81dSJoe Perches	my @include_files = ();
926c707a81dSJoe Perches
927c707a81dSJoe Perches	$camelcase_seeded = 1;
928351b2a1fSJoe Perches
9293645e328SRichard Genoud	if (-e ".git") {
930dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
931351b2a1fSJoe Perches		chomp $git_last_include_commit;
932c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
933c707a81dSJoe Perches	} else {
934c707a81dSJoe Perches		my $last_mod_date = 0;
935c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
936c707a81dSJoe Perches		@include_files = split('\n', $files);
937c707a81dSJoe Perches		foreach my $file (@include_files) {
938c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
939c707a81dSJoe Perches						   localtime((stat $file)[9]));
940c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
941c707a81dSJoe Perches		}
942c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
943c707a81dSJoe Perches	}
944c707a81dSJoe Perches
945c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
946c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
947c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
948351b2a1fSJoe Perches		while (<$camelcase_file>) {
949351b2a1fSJoe Perches			chomp;
950351b2a1fSJoe Perches			$camelcase{$_} = 1;
951351b2a1fSJoe Perches		}
952351b2a1fSJoe Perches		close($camelcase_file);
953351b2a1fSJoe Perches
954351b2a1fSJoe Perches		return;
955351b2a1fSJoe Perches	}
956c707a81dSJoe Perches
9573645e328SRichard Genoud	if (-e ".git") {
958dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
959c707a81dSJoe Perches		@include_files = split('\n', $files);
9603445686aSJoe Perches	}
961c707a81dSJoe Perches
9623445686aSJoe Perches	foreach my $file (@include_files) {
9633445686aSJoe Perches		seed_camelcase_file($file);
9643445686aSJoe Perches	}
965351b2a1fSJoe Perches
966c707a81dSJoe Perches	if ($camelcase_cache ne "") {
967351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
968c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
969c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
970351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
971351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
972351b2a1fSJoe Perches		}
973351b2a1fSJoe Perches		close($camelcase_file);
974351b2a1fSJoe Perches	}
9753445686aSJoe Perches}
9763445686aSJoe Perches
977f5f61325SJoe Perchessub git_is_single_file {
978f5f61325SJoe Perches	my ($filename) = @_;
979f5f61325SJoe Perches
980f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
981f5f61325SJoe Perches
982f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
983f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
984f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
985f5f61325SJoe Perches}
986f5f61325SJoe Perches
987d311cd44SJoe Perchessub git_commit_info {
988d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
989d311cd44SJoe Perches
990d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
991d311cd44SJoe Perches
992dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
993d311cd44SJoe Perches	$output =~ s/^\s*//gm;
994d311cd44SJoe Perches	my @lines = split("\n", $output);
995d311cd44SJoe Perches
9960d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9970d7835fcSJoe Perches
9985a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
999d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1000d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1001d311cd44SJoe Perches#
1002d311cd44SJoe Perches#		echo "checking commits $1..."
1003d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1004d311cd44SJoe Perches#		while read line ; do
1005d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1006d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1007d311cd44SJoe Perches#		done
1008d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
1009948b133aSHeinrich Schuchardt		$id = undef;
1010d311cd44SJoe Perches	} else {
1011d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1012d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1013d311cd44SJoe Perches	}
1014d311cd44SJoe Perches
1015d311cd44SJoe Perches	return ($id, $desc);
1016d311cd44SJoe Perches}
1017d311cd44SJoe Perches
10186c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
10190a920b5bSAndy Whitcroft
102000df344fSAndy Whitcroftmy @rawlines = ();
1021c2fdda0dSAndy Whitcroftmy @lines = ();
10223705ce5bSJoe Perchesmy @fixed = ();
1023d752fcc8SJoe Perchesmy @fixed_inserted = ();
1024d752fcc8SJoe Perchesmy @fixed_deleted = ();
1025194f66fcSJoe Perchesmy $fixlinenr = -1;
1026194f66fcSJoe Perches
10274a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10284a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10294a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
10304a593c34SDu, Changbin
10314a593c34SDu, Changbinif ($git) {
10324a593c34SDu, Changbin	my @commits = ();
10330dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
10344a593c34SDu, Changbin		my $git_range;
103528898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
103628898fd1SJoe Perches			$git_range = "-$2 $1";
10374a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
10384a593c34SDu, Changbin			$git_range = "$commit_expr";
10394a593c34SDu, Changbin		} else {
10400dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
10410dea9f1eSJoe Perches		}
1042dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10430dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
104428898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
104528898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10460dea9f1eSJoe Perches			my $sha1 = $1;
10470dea9f1eSJoe Perches			my $subject = $2;
10480dea9f1eSJoe Perches			unshift(@commits, $sha1);
10490dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10504a593c34SDu, Changbin		}
10514a593c34SDu, Changbin	}
10524a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10534a593c34SDu, Changbin	@ARGV = @commits;
10544a593c34SDu, Changbin}
10554a593c34SDu, Changbin
1056c2fdda0dSAndy Whitcroftmy $vname;
105798005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
10586c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
105921caa13cSAndy Whitcroft	my $FILE;
1060f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1061f5f61325SJoe Perches	my $oldfile = $file;
1062f5f61325SJoe Perches	$file = 1 if ($is_git_file);
10634a593c34SDu, Changbin	if ($git) {
10644a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10654a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10664a593c34SDu, Changbin	} elsif ($file) {
106721caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10686c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
106921caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
107021caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10716c72ffaaSAndy Whitcroft	} else {
107221caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10736c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10746c72ffaaSAndy Whitcroft	}
1075c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1076c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10774a593c34SDu, Changbin	} elsif ($git) {
10780dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1079c2fdda0dSAndy Whitcroft	} else {
1080c2fdda0dSAndy Whitcroft		$vname = $filename;
1081c2fdda0dSAndy Whitcroft	}
108221caa13cSAndy Whitcroft	while (<$FILE>) {
10830a920b5bSAndy Whitcroft		chomp;
108400df344fSAndy Whitcroft		push(@rawlines, $_);
1085c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
10866c72ffaaSAndy Whitcroft	}
108721caa13cSAndy Whitcroft	close($FILE);
1088d8469f16SJoe Perches
1089d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1090d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1091d8469f16SJoe Perches		print "$vname\n";
1092d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1093d8469f16SJoe Perches	}
1094d8469f16SJoe Perches
1095c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10960a920b5bSAndy Whitcroft		$exit = 1;
10970a920b5bSAndy Whitcroft	}
109800df344fSAndy Whitcroft	@rawlines = ();
109913214adfSAndy Whitcroft	@lines = ();
11003705ce5bSJoe Perches	@fixed = ();
1101d752fcc8SJoe Perches	@fixed_inserted = ();
1102d752fcc8SJoe Perches	@fixed_deleted = ();
1103194f66fcSJoe Perches	$fixlinenr = -1;
1104485ff23eSAlex Dowad	@modifierListFile = ();
1105485ff23eSAlex Dowad	@typeListFile = ();
1106485ff23eSAlex Dowad	build_types();
1107f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
11080a920b5bSAndy Whitcroft}
11090a920b5bSAndy Whitcroft
1110d8469f16SJoe Perchesif (!$quiet) {
11113c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
11123c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
11133c816e49SJoe Perches
11145b57980dSJoe Perches	if (!$perl_version_ok) {
1115d8469f16SJoe Perches		print << "EOM"
1116d8469f16SJoe Perches
1117d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
11185b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1119d8469f16SJoe PerchesEOM
1120d8469f16SJoe Perches	}
1121d8469f16SJoe Perches	if ($exit) {
1122d8469f16SJoe Perches		print << "EOM"
1123d8469f16SJoe Perches
1124d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1125d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1126d8469f16SJoe PerchesEOM
1127d8469f16SJoe Perches	}
1128d8469f16SJoe Perches}
1129d8469f16SJoe Perches
11300a920b5bSAndy Whitcroftexit($exit);
11310a920b5bSAndy Whitcroft
11320a920b5bSAndy Whitcroftsub top_of_kernel_tree {
11336c72ffaaSAndy Whitcroft	my ($root) = @_;
11346c72ffaaSAndy Whitcroft
11356c72ffaaSAndy Whitcroft	my @tree_check = (
11366c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
11376c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
11386c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
11396c72ffaaSAndy Whitcroft	);
11406c72ffaaSAndy Whitcroft
11416c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
11426c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
11430a920b5bSAndy Whitcroft			return 0;
11440a920b5bSAndy Whitcroft		}
11456c72ffaaSAndy Whitcroft	}
11466c72ffaaSAndy Whitcroft	return 1;
11476c72ffaaSAndy Whitcroft}
11480a920b5bSAndy Whitcroft
114920112475SJoe Perchessub parse_email {
115020112475SJoe Perches	my ($formatted_email) = @_;
115120112475SJoe Perches
115220112475SJoe Perches	my $name = "";
1153dfa05c28SJoe Perches	my $name_comment = "";
115420112475SJoe Perches	my $address = "";
115520112475SJoe Perches	my $comment = "";
115620112475SJoe Perches
115720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
115820112475SJoe Perches		$name = $1;
115920112475SJoe Perches		$address = $2;
116020112475SJoe Perches		$comment = $3 if defined $3;
116120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
116220112475SJoe Perches		$address = $1;
116320112475SJoe Perches		$comment = $2 if defined $2;
116420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
116520112475SJoe Perches		$address = $1;
116620112475SJoe Perches		$comment = $2 if defined $2;
116785e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
116820112475SJoe Perches		$name = $formatted_email;
11693705ce5bSJoe Perches		$name = trim($name);
117020112475SJoe Perches		$name =~ s/^\"|\"$//g;
117120112475SJoe Perches		# If there's a name left after stripping spaces and
117220112475SJoe Perches		# leading quotes, and the address doesn't have both
117320112475SJoe Perches		# leading and trailing angle brackets, the address
117420112475SJoe Perches		# is invalid. ie:
117520112475SJoe Perches		#   "joe smith [email protected]" bad
117620112475SJoe Perches		#   "joe smith <[email protected]" bad
117720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
117820112475SJoe Perches			$name = "";
117920112475SJoe Perches			$address = "";
118020112475SJoe Perches			$comment = "";
118120112475SJoe Perches		}
118220112475SJoe Perches	}
118320112475SJoe Perches
11843705ce5bSJoe Perches	$name = trim($name);
118520112475SJoe Perches	$name =~ s/^\"|\"$//g;
1186dfa05c28SJoe Perches	$name =~ s/(\s*\([^\)]+\))\s*//;
1187dfa05c28SJoe Perches	if (defined($1)) {
1188dfa05c28SJoe Perches		$name_comment = trim($1);
1189dfa05c28SJoe Perches	}
11903705ce5bSJoe Perches	$address = trim($address);
119120112475SJoe Perches	$address =~ s/^\<|\>$//g;
119220112475SJoe Perches
119320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
119420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
119520112475SJoe Perches		$name = "\"$name\"";
119620112475SJoe Perches	}
119720112475SJoe Perches
1198dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
119920112475SJoe Perches}
120020112475SJoe Perches
120120112475SJoe Perchessub format_email {
120220112475SJoe Perches	my ($name, $address) = @_;
120320112475SJoe Perches
120420112475SJoe Perches	my $formatted_email;
120520112475SJoe Perches
12063705ce5bSJoe Perches	$name = trim($name);
120720112475SJoe Perches	$name =~ s/^\"|\"$//g;
12083705ce5bSJoe Perches	$address = trim($address);
120920112475SJoe Perches
121020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
121120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
121220112475SJoe Perches		$name = "\"$name\"";
121320112475SJoe Perches	}
121420112475SJoe Perches
121520112475SJoe Perches	if ("$name" eq "") {
121620112475SJoe Perches		$formatted_email = "$address";
121720112475SJoe Perches	} else {
121820112475SJoe Perches		$formatted_email = "$name <$address>";
121920112475SJoe Perches	}
122020112475SJoe Perches
122120112475SJoe Perches	return $formatted_email;
122220112475SJoe Perches}
122320112475SJoe Perches
1224dfa05c28SJoe Perchessub reformat_email {
1225dfa05c28SJoe Perches	my ($email) = @_;
1226dfa05c28SJoe Perches
1227dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1228dfa05c28SJoe Perches	return format_email($email_name, $email_address);
1229dfa05c28SJoe Perches}
1230dfa05c28SJoe Perches
1231dfa05c28SJoe Perchessub same_email_addresses {
1232dfa05c28SJoe Perches	my ($email1, $email2) = @_;
1233dfa05c28SJoe Perches
1234dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1235dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1236dfa05c28SJoe Perches
1237dfa05c28SJoe Perches	return $email1_name eq $email2_name &&
1238dfa05c28SJoe Perches	       $email1_address eq $email2_address;
1239dfa05c28SJoe Perches}
1240dfa05c28SJoe Perches
1241d311cd44SJoe Perchessub which {
1242d311cd44SJoe Perches	my ($bin) = @_;
1243d311cd44SJoe Perches
1244d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1245d311cd44SJoe Perches		if (-e "$path/$bin") {
1246d311cd44SJoe Perches			return "$path/$bin";
1247d311cd44SJoe Perches		}
1248d311cd44SJoe Perches	}
1249d311cd44SJoe Perches
1250d311cd44SJoe Perches	return "";
1251d311cd44SJoe Perches}
1252d311cd44SJoe Perches
1253000d1cc1SJoe Perchessub which_conf {
1254000d1cc1SJoe Perches	my ($conf) = @_;
1255000d1cc1SJoe Perches
1256000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1257000d1cc1SJoe Perches		if (-e "$path/$conf") {
1258000d1cc1SJoe Perches			return "$path/$conf";
1259000d1cc1SJoe Perches		}
1260000d1cc1SJoe Perches	}
1261000d1cc1SJoe Perches
1262000d1cc1SJoe Perches	return "";
1263000d1cc1SJoe Perches}
1264000d1cc1SJoe Perches
12650a920b5bSAndy Whitcroftsub expand_tabs {
12660a920b5bSAndy Whitcroft	my ($str) = @_;
12670a920b5bSAndy Whitcroft
12680a920b5bSAndy Whitcroft	my $res = '';
12690a920b5bSAndy Whitcroft	my $n = 0;
12700a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12710a920b5bSAndy Whitcroft		if ($c eq "\t") {
12720a920b5bSAndy Whitcroft			$res .= ' ';
12730a920b5bSAndy Whitcroft			$n++;
1274713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
12750a920b5bSAndy Whitcroft				$res .= ' ';
12760a920b5bSAndy Whitcroft			}
12770a920b5bSAndy Whitcroft			next;
12780a920b5bSAndy Whitcroft		}
12790a920b5bSAndy Whitcroft		$res .= $c;
12800a920b5bSAndy Whitcroft		$n++;
12810a920b5bSAndy Whitcroft	}
12820a920b5bSAndy Whitcroft
12830a920b5bSAndy Whitcroft	return $res;
12840a920b5bSAndy Whitcroft}
12856c72ffaaSAndy Whitcroftsub copy_spacing {
1286773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
12876c72ffaaSAndy Whitcroft	return $res;
12886c72ffaaSAndy Whitcroft}
12890a920b5bSAndy Whitcroft
12904a0df2efSAndy Whitcroftsub line_stats {
12914a0df2efSAndy Whitcroft	my ($line) = @_;
12924a0df2efSAndy Whitcroft
12934a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12944a0df2efSAndy Whitcroft	$line =~ s/^.//;
12954a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12964a0df2efSAndy Whitcroft
12974a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12984a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12994a0df2efSAndy Whitcroft
13004a0df2efSAndy Whitcroft	return (length($line), length($white));
13014a0df2efSAndy Whitcroft}
13024a0df2efSAndy Whitcroft
1303773647a0SAndy Whitcroftmy $sanitise_quote = '';
1304773647a0SAndy Whitcroft
1305773647a0SAndy Whitcroftsub sanitise_line_reset {
1306773647a0SAndy Whitcroft	my ($in_comment) = @_;
1307773647a0SAndy Whitcroft
1308773647a0SAndy Whitcroft	if ($in_comment) {
1309773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1310773647a0SAndy Whitcroft	} else {
1311773647a0SAndy Whitcroft		$sanitise_quote = '';
1312773647a0SAndy Whitcroft	}
1313773647a0SAndy Whitcroft}
131400df344fSAndy Whitcroftsub sanitise_line {
131500df344fSAndy Whitcroft	my ($line) = @_;
131600df344fSAndy Whitcroft
131700df344fSAndy Whitcroft	my $res = '';
131800df344fSAndy Whitcroft	my $l = '';
131900df344fSAndy Whitcroft
1320c2fdda0dSAndy Whitcroft	my $qlen = 0;
1321773647a0SAndy Whitcroft	my $off = 0;
1322773647a0SAndy Whitcroft	my $c;
132300df344fSAndy Whitcroft
1324773647a0SAndy Whitcroft	# Always copy over the diff marker.
1325773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1326773647a0SAndy Whitcroft
1327773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1328773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1329773647a0SAndy Whitcroft
13308d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1331773647a0SAndy Whitcroft		# and end, all to $;.
1332773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1333773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1334773647a0SAndy Whitcroft
1335773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1336773647a0SAndy Whitcroft			$off++;
133700df344fSAndy Whitcroft			next;
1338773647a0SAndy Whitcroft		}
133981bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1340773647a0SAndy Whitcroft			$sanitise_quote = '';
1341773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1342773647a0SAndy Whitcroft			$off++;
1343773647a0SAndy Whitcroft			next;
1344773647a0SAndy Whitcroft		}
1345113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1346113f04a8SDaniel Walker			$sanitise_quote = '//';
1347113f04a8SDaniel Walker
1348113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1349113f04a8SDaniel Walker			$off++;
1350113f04a8SDaniel Walker			next;
1351113f04a8SDaniel Walker		}
1352773647a0SAndy Whitcroft
1353773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1354773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1355773647a0SAndy Whitcroft		    $c eq "\\") {
1356773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1357773647a0SAndy Whitcroft			$off++;
1358773647a0SAndy Whitcroft			next;
1359773647a0SAndy Whitcroft		}
1360773647a0SAndy Whitcroft		# Regular quotes.
1361773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1362773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1363773647a0SAndy Whitcroft				$sanitise_quote = $c;
1364773647a0SAndy Whitcroft
1365773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1366773647a0SAndy Whitcroft				next;
1367773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1368773647a0SAndy Whitcroft				$sanitise_quote = '';
136900df344fSAndy Whitcroft			}
137000df344fSAndy Whitcroft		}
1371773647a0SAndy Whitcroft
1372fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1373773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1374773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1375113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1376113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1377773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1378773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
137900df344fSAndy Whitcroft		} else {
1380773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
138100df344fSAndy Whitcroft		}
1382c2fdda0dSAndy Whitcroft	}
1383c2fdda0dSAndy Whitcroft
1384113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1385113f04a8SDaniel Walker		$sanitise_quote = '';
1386113f04a8SDaniel Walker	}
1387113f04a8SDaniel Walker
1388c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1389c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1390c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1391c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1392c2fdda0dSAndy Whitcroft
1393c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1394c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1395c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1396c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1397c2fdda0dSAndy Whitcroft	}
1398c2fdda0dSAndy Whitcroft
1399dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1400dadf680dSJoe Perches		my $match = $1;
1401dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1402dadf680dSJoe Perches	}
1403dadf680dSJoe Perches
140400df344fSAndy Whitcroft	return $res;
140500df344fSAndy Whitcroft}
140600df344fSAndy Whitcroft
1407a6962d72SJoe Perchessub get_quoted_string {
1408a6962d72SJoe Perches	my ($line, $rawline) = @_;
1409a6962d72SJoe Perches
1410478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
141133acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1412a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1413a6962d72SJoe Perches}
1414a6962d72SJoe Perches
14158905a67cSAndy Whitcroftsub ctx_statement_block {
14168905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
14178905a67cSAndy Whitcroft	my $line = $linenr - 1;
14188905a67cSAndy Whitcroft	my $blk = '';
14198905a67cSAndy Whitcroft	my $soff = $off;
14208905a67cSAndy Whitcroft	my $coff = $off - 1;
1421773647a0SAndy Whitcroft	my $coff_set = 0;
14228905a67cSAndy Whitcroft
142313214adfSAndy Whitcroft	my $loff = 0;
142413214adfSAndy Whitcroft
14258905a67cSAndy Whitcroft	my $type = '';
14268905a67cSAndy Whitcroft	my $level = 0;
1427a2750645SAndy Whitcroft	my @stack = ();
1428cf655043SAndy Whitcroft	my $p;
14298905a67cSAndy Whitcroft	my $c;
14308905a67cSAndy Whitcroft	my $len = 0;
143113214adfSAndy Whitcroft
143213214adfSAndy Whitcroft	my $remainder;
14338905a67cSAndy Whitcroft	while (1) {
1434a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1435a2750645SAndy Whitcroft
1436773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
14378905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
14388905a67cSAndy Whitcroft		# context.
14398905a67cSAndy Whitcroft		if ($off >= $len) {
14408905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1441dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1442c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
14438905a67cSAndy Whitcroft				$remain--;
144413214adfSAndy Whitcroft				$loff = $len;
1445c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
14468905a67cSAndy Whitcroft				$len = length($blk);
14478905a67cSAndy Whitcroft				$line++;
14488905a67cSAndy Whitcroft				last;
14498905a67cSAndy Whitcroft			}
14508905a67cSAndy Whitcroft			# Bail if there is no further context.
14518905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
145213214adfSAndy Whitcroft			if ($off >= $len) {
14538905a67cSAndy Whitcroft				last;
14548905a67cSAndy Whitcroft			}
1455f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1456f74bd194SAndy Whitcroft				$level++;
1457f74bd194SAndy Whitcroft				$type = '#';
1458f74bd194SAndy Whitcroft			}
14598905a67cSAndy Whitcroft		}
1460cf655043SAndy Whitcroft		$p = $c;
14618905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
146213214adfSAndy Whitcroft		$remainder = substr($blk, $off);
14638905a67cSAndy Whitcroft
1464773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
14654635f4fbSAndy Whitcroft
14664635f4fbSAndy Whitcroft		# Handle nested #if/#else.
14674635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
14684635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
14694635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
14704635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14714635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14724635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14734635f4fbSAndy Whitcroft		}
14744635f4fbSAndy Whitcroft
14758905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14768905a67cSAndy Whitcroft		# outermost level.
14778905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
14788905a67cSAndy Whitcroft			last;
14798905a67cSAndy Whitcroft		}
14808905a67cSAndy Whitcroft
148113214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1482773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1483773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1484773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1485773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1486773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1487773647a0SAndy Whitcroft			$coff_set = 1;
1488773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1489773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
149013214adfSAndy Whitcroft		}
149113214adfSAndy Whitcroft
14928905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14938905a67cSAndy Whitcroft			$level++;
14948905a67cSAndy Whitcroft			$type = '(';
14958905a67cSAndy Whitcroft		}
14968905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14978905a67cSAndy Whitcroft			$level--;
14988905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14998905a67cSAndy Whitcroft
15008905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
15018905a67cSAndy Whitcroft				$coff = $off;
1502773647a0SAndy Whitcroft				$coff_set = 1;
1503773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
15048905a67cSAndy Whitcroft			}
15058905a67cSAndy Whitcroft		}
15068905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
15078905a67cSAndy Whitcroft			$level++;
15088905a67cSAndy Whitcroft			$type = '{';
15098905a67cSAndy Whitcroft		}
15108905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
15118905a67cSAndy Whitcroft			$level--;
15128905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
15138905a67cSAndy Whitcroft
15148905a67cSAndy Whitcroft			if ($level == 0) {
1515b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1516b998e001SPatrick Pannuto					$off++;
1517b998e001SPatrick Pannuto				}
15188905a67cSAndy Whitcroft				last;
15198905a67cSAndy Whitcroft			}
15208905a67cSAndy Whitcroft		}
1521f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1522f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1523f74bd194SAndy Whitcroft			$level--;
1524f74bd194SAndy Whitcroft			$type = '';
1525f74bd194SAndy Whitcroft			$off++;
1526f74bd194SAndy Whitcroft			last;
1527f74bd194SAndy Whitcroft		}
15288905a67cSAndy Whitcroft		$off++;
15298905a67cSAndy Whitcroft	}
1530a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
153113214adfSAndy Whitcroft	if ($off == $len) {
1532a3bb97a7SAndy Whitcroft		$loff = $len + 1;
153313214adfSAndy Whitcroft		$line++;
153413214adfSAndy Whitcroft		$remain--;
153513214adfSAndy Whitcroft	}
15368905a67cSAndy Whitcroft
15378905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
15388905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
15398905a67cSAndy Whitcroft
15408905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
15418905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
15428905a67cSAndy Whitcroft
1543773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
154413214adfSAndy Whitcroft
154513214adfSAndy Whitcroft	return ($statement, $condition,
154613214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
154713214adfSAndy Whitcroft}
154813214adfSAndy Whitcroft
1549cf655043SAndy Whitcroftsub statement_lines {
1550cf655043SAndy Whitcroft	my ($stmt) = @_;
1551cf655043SAndy Whitcroft
1552cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1553cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1554cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1555cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1556cf655043SAndy Whitcroft
1557cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1558cf655043SAndy Whitcroft
1559cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1560cf655043SAndy Whitcroft}
1561cf655043SAndy Whitcroft
1562cf655043SAndy Whitcroftsub statement_rawlines {
1563cf655043SAndy Whitcroft	my ($stmt) = @_;
1564cf655043SAndy Whitcroft
1565cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1566cf655043SAndy Whitcroft
1567cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1568cf655043SAndy Whitcroft}
1569cf655043SAndy Whitcroft
1570cf655043SAndy Whitcroftsub statement_block_size {
1571cf655043SAndy Whitcroft	my ($stmt) = @_;
1572cf655043SAndy Whitcroft
1573cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1574cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1575cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1576cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1577cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1578cf655043SAndy Whitcroft
1579cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1580cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1581cf655043SAndy Whitcroft
1582cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1583cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1584cf655043SAndy Whitcroft
1585cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1586cf655043SAndy Whitcroft		return $stmt_lines;
1587cf655043SAndy Whitcroft	} else {
1588cf655043SAndy Whitcroft		return $stmt_statements;
1589cf655043SAndy Whitcroft	}
1590cf655043SAndy Whitcroft}
1591cf655043SAndy Whitcroft
159213214adfSAndy Whitcroftsub ctx_statement_full {
159313214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
159413214adfSAndy Whitcroft	my ($statement, $condition, $level);
159513214adfSAndy Whitcroft
159613214adfSAndy Whitcroft	my (@chunks);
159713214adfSAndy Whitcroft
1598cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
159913214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
160013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1601773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
160213214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1603cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1604cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1605cf655043SAndy Whitcroft	}
1606cf655043SAndy Whitcroft
1607cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1608cf655043SAndy Whitcroft	# could continue the statement.
1609cf655043SAndy Whitcroft	for (;;) {
161013214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
161113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1612cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1613773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1614cf655043SAndy Whitcroft		#print "C: push\n";
1615cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
161613214adfSAndy Whitcroft	}
161713214adfSAndy Whitcroft
161813214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
16198905a67cSAndy Whitcroft}
16208905a67cSAndy Whitcroft
16214a0df2efSAndy Whitcroftsub ctx_block_get {
1622f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
16234a0df2efSAndy Whitcroft	my $line;
16244a0df2efSAndy Whitcroft	my $start = $linenr - 1;
16254a0df2efSAndy Whitcroft	my $blk = '';
16264a0df2efSAndy Whitcroft	my @o;
16274a0df2efSAndy Whitcroft	my @c;
16284a0df2efSAndy Whitcroft	my @res = ();
16294a0df2efSAndy Whitcroft
1630f0a594c1SAndy Whitcroft	my $level = 0;
16314635f4fbSAndy Whitcroft	my @stack = ($level);
163200df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
163300df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
163400df344fSAndy Whitcroft		$remain--;
163500df344fSAndy Whitcroft
163600df344fSAndy Whitcroft		$blk .= $rawlines[$line];
16374635f4fbSAndy Whitcroft
16384635f4fbSAndy Whitcroft		# Handle nested #if/#else.
163901464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
16404635f4fbSAndy Whitcroft			push(@stack, $level);
164101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
16424635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
164301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
16444635f4fbSAndy Whitcroft			$level = pop(@stack);
16454635f4fbSAndy Whitcroft		}
16464635f4fbSAndy Whitcroft
164701464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1648f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1649f0a594c1SAndy Whitcroft			if ($off > 0) {
1650f0a594c1SAndy Whitcroft				$off--;
1651f0a594c1SAndy Whitcroft				next;
1652f0a594c1SAndy Whitcroft			}
16534a0df2efSAndy Whitcroft
1654f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1655f0a594c1SAndy Whitcroft				$level--;
1656f0a594c1SAndy Whitcroft				last if ($level == 0);
1657f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1658f0a594c1SAndy Whitcroft				$level++;
1659f0a594c1SAndy Whitcroft			}
1660f0a594c1SAndy Whitcroft		}
16614a0df2efSAndy Whitcroft
1662f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
166300df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
16644a0df2efSAndy Whitcroft		}
16654a0df2efSAndy Whitcroft
1666f0a594c1SAndy Whitcroft		last if ($level == 0);
16674a0df2efSAndy Whitcroft	}
16684a0df2efSAndy Whitcroft
1669f0a594c1SAndy Whitcroft	return ($level, @res);
16704a0df2efSAndy Whitcroft}
16714a0df2efSAndy Whitcroftsub ctx_block_outer {
16724a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16734a0df2efSAndy Whitcroft
1674f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1675f0a594c1SAndy Whitcroft	return @r;
16764a0df2efSAndy Whitcroft}
16774a0df2efSAndy Whitcroftsub ctx_block {
16784a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16794a0df2efSAndy Whitcroft
1680f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1681f0a594c1SAndy Whitcroft	return @r;
1682653d4876SAndy Whitcroft}
1683653d4876SAndy Whitcroftsub ctx_statement {
1684f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1685f0a594c1SAndy Whitcroft
1686f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1687f0a594c1SAndy Whitcroft	return @r;
1688f0a594c1SAndy Whitcroft}
1689f0a594c1SAndy Whitcroftsub ctx_block_level {
1690653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1691653d4876SAndy Whitcroft
1692f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16934a0df2efSAndy Whitcroft}
16949c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16959c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16969c0ca6f9SAndy Whitcroft
16979c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16989c0ca6f9SAndy Whitcroft}
16994a0df2efSAndy Whitcroft
17004a0df2efSAndy Whitcroftsub ctx_locate_comment {
17014a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17024a0df2efSAndy Whitcroft
1703a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1704a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1705a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1706a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1707a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1708a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1709a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1710a55ee0ccSJoe Perches
17114a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1712a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
17134a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
17144a0df2efSAndy Whitcroft
17154a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
17164a0df2efSAndy Whitcroft	# comment.
17174a0df2efSAndy Whitcroft	my $in_comment = 0;
17184a0df2efSAndy Whitcroft	$current_comment = '';
17194a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
172000df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
172100df344fSAndy Whitcroft		#warn "           $line\n";
17224a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
17234a0df2efSAndy Whitcroft			$in_comment = 1;
17244a0df2efSAndy Whitcroft		}
17254a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
17264a0df2efSAndy Whitcroft			$in_comment = 1;
17274a0df2efSAndy Whitcroft		}
17284a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
17294a0df2efSAndy Whitcroft			$current_comment = '';
17304a0df2efSAndy Whitcroft		}
17314a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
17324a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
17334a0df2efSAndy Whitcroft			$in_comment = 0;
17344a0df2efSAndy Whitcroft		}
17354a0df2efSAndy Whitcroft	}
17364a0df2efSAndy Whitcroft
17374a0df2efSAndy Whitcroft	chomp($current_comment);
17384a0df2efSAndy Whitcroft	return($current_comment);
17394a0df2efSAndy Whitcroft}
17404a0df2efSAndy Whitcroftsub ctx_has_comment {
17414a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17424a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
17434a0df2efSAndy Whitcroft
174400df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
17454a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
17464a0df2efSAndy Whitcroft
17474a0df2efSAndy Whitcroft	return ($cmt ne '');
17484a0df2efSAndy Whitcroft}
17494a0df2efSAndy Whitcroft
17504d001e4dSAndy Whitcroftsub raw_line {
17514d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
17524d001e4dSAndy Whitcroft
17534d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
17544d001e4dSAndy Whitcroft	$cnt++;
17554d001e4dSAndy Whitcroft
17564d001e4dSAndy Whitcroft	my $line;
17574d001e4dSAndy Whitcroft	while ($cnt) {
17584d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
17594d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
17604d001e4dSAndy Whitcroft		$cnt--;
17614d001e4dSAndy Whitcroft	}
17624d001e4dSAndy Whitcroft
17634d001e4dSAndy Whitcroft	return $line;
17644d001e4dSAndy Whitcroft}
17654d001e4dSAndy Whitcroft
17662a9f9d85STobin C. Hardingsub get_stat_real {
17672a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
17682a9f9d85STobin C. Harding
17692a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
17702a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
17712a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
17722a9f9d85STobin C. Harding	}
17732a9f9d85STobin C. Harding
17742a9f9d85STobin C. Harding	return $stat_real;
17752a9f9d85STobin C. Harding}
17762a9f9d85STobin C. Harding
1777e3d95a2aSTobin C. Hardingsub get_stat_here {
1778e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1779e3d95a2aSTobin C. Harding
1780e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1781e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1782e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1783e3d95a2aSTobin C. Harding	}
1784e3d95a2aSTobin C. Harding
1785e3d95a2aSTobin C. Harding	return $herectx;
1786e3d95a2aSTobin C. Harding}
1787e3d95a2aSTobin C. Harding
17880a920b5bSAndy Whitcroftsub cat_vet {
17890a920b5bSAndy Whitcroft	my ($vet) = @_;
17909c0ca6f9SAndy Whitcroft	my ($res, $coded);
17910a920b5bSAndy Whitcroft
17929c0ca6f9SAndy Whitcroft	$res = '';
17936c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
17946c72ffaaSAndy Whitcroft		$res .= $1;
17956c72ffaaSAndy Whitcroft		if ($2 ne '') {
17969c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
17976c72ffaaSAndy Whitcroft			$res .= $coded;
17986c72ffaaSAndy Whitcroft		}
17999c0ca6f9SAndy Whitcroft	}
18009c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
18010a920b5bSAndy Whitcroft
18029c0ca6f9SAndy Whitcroft	return $res;
18030a920b5bSAndy Whitcroft}
18040a920b5bSAndy Whitcroft
1805c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1806cf655043SAndy Whitcroftmy $av_pending;
1807c2fdda0dSAndy Whitcroftmy @av_paren_type;
18081f65f947SAndy Whitcroftmy $av_pend_colon;
1809c2fdda0dSAndy Whitcroft
1810c2fdda0dSAndy Whitcroftsub annotate_reset {
1811c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1812cf655043SAndy Whitcroft	$av_pending = '_';
1813cf655043SAndy Whitcroft	@av_paren_type = ('E');
18141f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1815c2fdda0dSAndy Whitcroft}
1816c2fdda0dSAndy Whitcroft
18176c72ffaaSAndy Whitcroftsub annotate_values {
18186c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
18196c72ffaaSAndy Whitcroft
18206c72ffaaSAndy Whitcroft	my $res;
18211f65f947SAndy Whitcroft	my $var = '_' x length($stream);
18226c72ffaaSAndy Whitcroft	my $cur = $stream;
18236c72ffaaSAndy Whitcroft
1824c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
18256c72ffaaSAndy Whitcroft
18266c72ffaaSAndy Whitcroft	while (length($cur)) {
1827773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1828cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1829171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
18306c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1831c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1832c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1833cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1834c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
18356c72ffaaSAndy Whitcroft			}
18366c72ffaaSAndy Whitcroft
1837c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
18389446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
18399446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1840addcdceaSAndy Whitcroft			$type = 'c';
18419446ef56SAndy Whitcroft
1842e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1843c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
18446c72ffaaSAndy Whitcroft			$type = 'T';
18456c72ffaaSAndy Whitcroft
1846389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1847389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1848389a2fe5SAndy Whitcroft			$type = 'T';
1849389a2fe5SAndy Whitcroft
1850c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1851171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1852c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1853171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1854171ae1a4SAndy Whitcroft			if ($2 ne '') {
1855cf655043SAndy Whitcroft				$av_pending = 'N';
1856171ae1a4SAndy Whitcroft			}
1857171ae1a4SAndy Whitcroft			$type = 'E';
1858171ae1a4SAndy Whitcroft
1859c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1860171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1861171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1862171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
18636c72ffaaSAndy Whitcroft
1864c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1865cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1866c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1867cf655043SAndy Whitcroft
1868cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1869cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1870171ae1a4SAndy Whitcroft			$type = 'E';
1871cf655043SAndy Whitcroft
1872c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1873cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1874cf655043SAndy Whitcroft			$av_preprocessor = 1;
1875cf655043SAndy Whitcroft
1876cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1877cf655043SAndy Whitcroft
1878171ae1a4SAndy Whitcroft			$type = 'E';
1879cf655043SAndy Whitcroft
1880c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1881cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1882cf655043SAndy Whitcroft
1883cf655043SAndy Whitcroft			$av_preprocessor = 1;
1884cf655043SAndy Whitcroft
1885cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1886cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1887cf655043SAndy Whitcroft			pop(@av_paren_type);
1888cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1889171ae1a4SAndy Whitcroft			$type = 'E';
18906c72ffaaSAndy Whitcroft
18916c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1892c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
18936c72ffaaSAndy Whitcroft
1894171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1895171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1896171ae1a4SAndy Whitcroft			$av_pending = $type;
1897171ae1a4SAndy Whitcroft			$type = 'N';
1898171ae1a4SAndy Whitcroft
18996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1900c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
19016c72ffaaSAndy Whitcroft			if (defined $2) {
1902cf655043SAndy Whitcroft				$av_pending = 'V';
19036c72ffaaSAndy Whitcroft			}
19046c72ffaaSAndy Whitcroft			$type = 'N';
19056c72ffaaSAndy Whitcroft
190614b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1907c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
190814b111c1SAndy Whitcroft			$av_pending = 'E';
19096c72ffaaSAndy Whitcroft			$type = 'N';
19106c72ffaaSAndy Whitcroft
19111f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
19121f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
19131f65f947SAndy Whitcroft			$av_pend_colon = 'C';
19141f65f947SAndy Whitcroft			$type = 'N';
19151f65f947SAndy Whitcroft
191614b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1917c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
19186c72ffaaSAndy Whitcroft			$type = 'N';
19196c72ffaaSAndy Whitcroft
19206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1921c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1922cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1923cf655043SAndy Whitcroft			$av_pending = '_';
19246c72ffaaSAndy Whitcroft			$type = 'N';
19256c72ffaaSAndy Whitcroft
19266c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1927cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1928cf655043SAndy Whitcroft			if ($new_type ne '_') {
1929cf655043SAndy Whitcroft				$type = $new_type;
1930c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1931c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
19326c72ffaaSAndy Whitcroft			} else {
1933c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
19346c72ffaaSAndy Whitcroft			}
19356c72ffaaSAndy Whitcroft
1936c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1937c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1938c8cb2ca3SAndy Whitcroft			$type = 'V';
1939cf655043SAndy Whitcroft			$av_pending = 'V';
19406c72ffaaSAndy Whitcroft
19418e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
19428e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
19431f65f947SAndy Whitcroft				$av_pend_colon = 'B';
19448e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
19458e761b04SAndy Whitcroft				$av_pend_colon = 'L';
19461f65f947SAndy Whitcroft			}
19471f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
19481f65f947SAndy Whitcroft			$type = 'V';
19491f65f947SAndy Whitcroft
19506c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1951c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
19526c72ffaaSAndy Whitcroft			$type = 'V';
19536c72ffaaSAndy Whitcroft
19546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1955c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
19566c72ffaaSAndy Whitcroft			$type = 'N';
19576c72ffaaSAndy Whitcroft
1958cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1959c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
196013214adfSAndy Whitcroft			$type = 'E';
19611f65f947SAndy Whitcroft			$av_pend_colon = 'O';
196213214adfSAndy Whitcroft
19638e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
19648e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
19658e761b04SAndy Whitcroft			$type = 'C';
19668e761b04SAndy Whitcroft
19671f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
19681f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
19691f65f947SAndy Whitcroft			$type = 'N';
19701f65f947SAndy Whitcroft
19711f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
19721f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
19731f65f947SAndy Whitcroft
19741f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
19751f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
19761f65f947SAndy Whitcroft				$type = 'E';
19771f65f947SAndy Whitcroft			} else {
19781f65f947SAndy Whitcroft				$type = 'N';
19791f65f947SAndy Whitcroft			}
19801f65f947SAndy Whitcroft			$av_pend_colon = 'O';
19811f65f947SAndy Whitcroft
19828e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
198313214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
19846c72ffaaSAndy Whitcroft			$type = 'N';
19856c72ffaaSAndy Whitcroft
19860d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
198774048ed8SAndy Whitcroft			my $variant;
198874048ed8SAndy Whitcroft
198974048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
199074048ed8SAndy Whitcroft			if ($type eq 'V') {
199174048ed8SAndy Whitcroft				$variant = 'B';
199274048ed8SAndy Whitcroft			} else {
199374048ed8SAndy Whitcroft				$variant = 'U';
199474048ed8SAndy Whitcroft			}
199574048ed8SAndy Whitcroft
199674048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
199774048ed8SAndy Whitcroft			$type = 'N';
199874048ed8SAndy Whitcroft
19996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2000c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
20016c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
20026c72ffaaSAndy Whitcroft				$type = 'N';
20036c72ffaaSAndy Whitcroft			}
20046c72ffaaSAndy Whitcroft
20056c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2006c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
20076c72ffaaSAndy Whitcroft		}
20086c72ffaaSAndy Whitcroft		if (defined $1) {
20096c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
20106c72ffaaSAndy Whitcroft			$res .= $type x length($1);
20116c72ffaaSAndy Whitcroft		}
20126c72ffaaSAndy Whitcroft	}
20136c72ffaaSAndy Whitcroft
20141f65f947SAndy Whitcroft	return ($res, $var);
20156c72ffaaSAndy Whitcroft}
20166c72ffaaSAndy Whitcroft
20178905a67cSAndy Whitcroftsub possible {
201813214adfSAndy Whitcroft	my ($possible, $line) = @_;
20199a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
20200776e594SAndy Whitcroft		^(?:
20210776e594SAndy Whitcroft			$Modifier|
20220776e594SAndy Whitcroft			$Storage|
20230776e594SAndy Whitcroft			$Type|
20249a974fdbSAndy Whitcroft			DEFINE_\S+
20259a974fdbSAndy Whitcroft		)$|
20269a974fdbSAndy Whitcroft		^(?:
20270776e594SAndy Whitcroft			goto|
20280776e594SAndy Whitcroft			return|
20290776e594SAndy Whitcroft			case|
20300776e594SAndy Whitcroft			else|
20310776e594SAndy Whitcroft			asm|__asm__|
203289a88353SAndy Whitcroft			do|
203389a88353SAndy Whitcroft			\#|
203489a88353SAndy Whitcroft			\#\#|
20359a974fdbSAndy Whitcroft		)(?:\s|$)|
20360776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
20379a974fdbSAndy Whitcroft	    )}x;
20389a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
20399a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2040c45dcabdSAndy Whitcroft		# Check for modifiers.
2041c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2042c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2043c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2044c45dcabdSAndy Whitcroft
2045c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2046c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2047d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
20489a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2049d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2050485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2051d2506586SAndy Whitcroft				}
20529a974fdbSAndy Whitcroft			}
2053c45dcabdSAndy Whitcroft
2054c45dcabdSAndy Whitcroft		} else {
205513214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2056485ff23eSAlex Dowad			push(@typeListFile, $possible);
2057c45dcabdSAndy Whitcroft		}
20588905a67cSAndy Whitcroft		build_types();
20590776e594SAndy Whitcroft	} else {
20600776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
20618905a67cSAndy Whitcroft	}
20628905a67cSAndy Whitcroft}
20638905a67cSAndy Whitcroft
20646c72ffaaSAndy Whitcroftmy $prefix = '';
20656c72ffaaSAndy Whitcroft
2066000d1cc1SJoe Perchessub show_type {
2067cbec18afSJoe Perches	my ($type) = @_;
206891bfe484SJoe Perches
2069522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2070522b837cSAlexey Dobriyan
2071cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2072cbec18afSJoe Perches
2073cbec18afSJoe Perches	return !defined $ignore_type{$type};
2074000d1cc1SJoe Perches}
2075000d1cc1SJoe Perches
2076f0a594c1SAndy Whitcroftsub report {
2077cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2078cbec18afSJoe Perches
2079cbec18afSJoe Perches	if (!show_type($type) ||
2080cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2081773647a0SAndy Whitcroft		return 0;
2082773647a0SAndy Whitcroft	}
208357230297SJoe Perches	my $output = '';
2084737c0767SJohn Brooks	if ($color) {
208557230297SJoe Perches		if ($level eq 'ERROR') {
208657230297SJoe Perches			$output .= RED;
208757230297SJoe Perches		} elsif ($level eq 'WARNING') {
208857230297SJoe Perches			$output .= YELLOW;
2089000d1cc1SJoe Perches		} else {
209057230297SJoe Perches			$output .= GREEN;
2091000d1cc1SJoe Perches		}
209257230297SJoe Perches	}
209357230297SJoe Perches	$output .= $prefix . $level . ':';
209457230297SJoe Perches	if ($show_types) {
2095737c0767SJohn Brooks		$output .= BLUE if ($color);
209657230297SJoe Perches		$output .= "$type:";
209757230297SJoe Perches	}
2098737c0767SJohn Brooks	$output .= RESET if ($color);
209957230297SJoe Perches	$output .= ' ' . $msg . "\n";
210034d8815fSJoe Perches
210134d8815fSJoe Perches	if ($showfile) {
210234d8815fSJoe Perches		my @lines = split("\n", $output, -1);
210334d8815fSJoe Perches		splice(@lines, 1, 1);
210434d8815fSJoe Perches		$output = join("\n", @lines);
210534d8815fSJoe Perches	}
210657230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
21078905a67cSAndy Whitcroft
210857230297SJoe Perches	push(our @report, $output);
2109773647a0SAndy Whitcroft
2110773647a0SAndy Whitcroft	return 1;
2111f0a594c1SAndy Whitcroft}
2112cbec18afSJoe Perches
2113f0a594c1SAndy Whitcroftsub report_dump {
211413214adfSAndy Whitcroft	our @report;
2115f0a594c1SAndy Whitcroft}
2116000d1cc1SJoe Perches
2117d752fcc8SJoe Perchessub fixup_current_range {
2118d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2119d752fcc8SJoe Perches
2120d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2121d752fcc8SJoe Perches		my $o = $1;
2122d752fcc8SJoe Perches		my $l = $2;
2123d752fcc8SJoe Perches		my $no = $o + $offset;
2124d752fcc8SJoe Perches		my $nl = $l + $length;
2125d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2126d752fcc8SJoe Perches	}
2127d752fcc8SJoe Perches}
2128d752fcc8SJoe Perches
2129d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2130d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2131d752fcc8SJoe Perches
2132d752fcc8SJoe Perches	my $range_last_linenr = 0;
2133d752fcc8SJoe Perches	my $delta_offset = 0;
2134d752fcc8SJoe Perches
2135d752fcc8SJoe Perches	my $old_linenr = 0;
2136d752fcc8SJoe Perches	my $new_linenr = 0;
2137d752fcc8SJoe Perches
2138d752fcc8SJoe Perches	my $next_insert = 0;
2139d752fcc8SJoe Perches	my $next_delete = 0;
2140d752fcc8SJoe Perches
2141d752fcc8SJoe Perches	my @lines = ();
2142d752fcc8SJoe Perches
2143d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2144d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2145d752fcc8SJoe Perches
2146d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2147d752fcc8SJoe Perches		my $save_line = 1;
2148d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2149323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2150d752fcc8SJoe Perches			$delta_offset = 0;
2151d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2152d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2153d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2154d752fcc8SJoe Perches		}
2155d752fcc8SJoe Perches
2156d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2157d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2158d752fcc8SJoe Perches			$save_line = 0;
2159d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2160d752fcc8SJoe Perches		}
2161d752fcc8SJoe Perches
2162d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2163d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2164d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2165d752fcc8SJoe Perches			$new_linenr++;
2166d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2167d752fcc8SJoe Perches		}
2168d752fcc8SJoe Perches
2169d752fcc8SJoe Perches		if ($save_line) {
2170d752fcc8SJoe Perches			push(@lines, $line);
2171d752fcc8SJoe Perches			$new_linenr++;
2172d752fcc8SJoe Perches		}
2173d752fcc8SJoe Perches
2174d752fcc8SJoe Perches		$old_linenr++;
2175d752fcc8SJoe Perches	}
2176d752fcc8SJoe Perches
2177d752fcc8SJoe Perches	return @lines;
2178d752fcc8SJoe Perches}
2179d752fcc8SJoe Perches
2180f2d7e4d4SJoe Perchessub fix_insert_line {
2181f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2182f2d7e4d4SJoe Perches
2183f2d7e4d4SJoe Perches	my $inserted = {
2184f2d7e4d4SJoe Perches		LINENR => $linenr,
2185f2d7e4d4SJoe Perches		LINE => $line,
2186f2d7e4d4SJoe Perches	};
2187f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2188f2d7e4d4SJoe Perches}
2189f2d7e4d4SJoe Perches
2190f2d7e4d4SJoe Perchessub fix_delete_line {
2191f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2192f2d7e4d4SJoe Perches
2193f2d7e4d4SJoe Perches	my $deleted = {
2194f2d7e4d4SJoe Perches		LINENR => $linenr,
2195f2d7e4d4SJoe Perches		LINE => $line,
2196f2d7e4d4SJoe Perches	};
2197f2d7e4d4SJoe Perches
2198f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2199f2d7e4d4SJoe Perches}
2200f2d7e4d4SJoe Perches
2201de7d4f0eSAndy Whitcroftsub ERROR {
2202cbec18afSJoe Perches	my ($type, $msg) = @_;
2203cbec18afSJoe Perches
2204cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2205de7d4f0eSAndy Whitcroft		our $clean = 0;
22066c72ffaaSAndy Whitcroft		our $cnt_error++;
22073705ce5bSJoe Perches		return 1;
2208de7d4f0eSAndy Whitcroft	}
22093705ce5bSJoe Perches	return 0;
2210773647a0SAndy Whitcroft}
2211de7d4f0eSAndy Whitcroftsub WARN {
2212cbec18afSJoe Perches	my ($type, $msg) = @_;
2213cbec18afSJoe Perches
2214cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2215de7d4f0eSAndy Whitcroft		our $clean = 0;
22166c72ffaaSAndy Whitcroft		our $cnt_warn++;
22173705ce5bSJoe Perches		return 1;
2218de7d4f0eSAndy Whitcroft	}
22193705ce5bSJoe Perches	return 0;
2220773647a0SAndy Whitcroft}
2221de7d4f0eSAndy Whitcroftsub CHK {
2222cbec18afSJoe Perches	my ($type, $msg) = @_;
2223cbec18afSJoe Perches
2224cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2225de7d4f0eSAndy Whitcroft		our $clean = 0;
22266c72ffaaSAndy Whitcroft		our $cnt_chk++;
22273705ce5bSJoe Perches		return 1;
22286c72ffaaSAndy Whitcroft	}
22293705ce5bSJoe Perches	return 0;
2230de7d4f0eSAndy Whitcroft}
2231de7d4f0eSAndy Whitcroft
22326ecd9674SAndy Whitcroftsub check_absolute_file {
22336ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
22346ecd9674SAndy Whitcroft	my $file = $absolute;
22356ecd9674SAndy Whitcroft
22366ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
22376ecd9674SAndy Whitcroft
22386ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
22396ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
22406ecd9674SAndy Whitcroft		if (-f "$root/$file") {
22416ecd9674SAndy Whitcroft			##print "file<$file>\n";
22426ecd9674SAndy Whitcroft			last;
22436ecd9674SAndy Whitcroft		}
22446ecd9674SAndy Whitcroft	}
22456ecd9674SAndy Whitcroft	if (! -f _)  {
22466ecd9674SAndy Whitcroft		return 0;
22476ecd9674SAndy Whitcroft	}
22486ecd9674SAndy Whitcroft
22496ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
22506ecd9674SAndy Whitcroft	my $prefix = $absolute;
22516ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
22526ecd9674SAndy Whitcroft
22536ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
22546ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2255000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2256000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
22576ecd9674SAndy Whitcroft	}
22586ecd9674SAndy Whitcroft}
22596ecd9674SAndy Whitcroft
22603705ce5bSJoe Perchessub trim {
22613705ce5bSJoe Perches	my ($string) = @_;
22623705ce5bSJoe Perches
2263b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2264b34c648bSJoe Perches
2265b34c648bSJoe Perches	return $string;
2266b34c648bSJoe Perches}
2267b34c648bSJoe Perches
2268b34c648bSJoe Perchessub ltrim {
2269b34c648bSJoe Perches	my ($string) = @_;
2270b34c648bSJoe Perches
2271b34c648bSJoe Perches	$string =~ s/^\s+//;
2272b34c648bSJoe Perches
2273b34c648bSJoe Perches	return $string;
2274b34c648bSJoe Perches}
2275b34c648bSJoe Perches
2276b34c648bSJoe Perchessub rtrim {
2277b34c648bSJoe Perches	my ($string) = @_;
2278b34c648bSJoe Perches
2279b34c648bSJoe Perches	$string =~ s/\s+$//;
22803705ce5bSJoe Perches
22813705ce5bSJoe Perches	return $string;
22823705ce5bSJoe Perches}
22833705ce5bSJoe Perches
228452ea8506SJoe Perchessub string_find_replace {
228552ea8506SJoe Perches	my ($string, $find, $replace) = @_;
228652ea8506SJoe Perches
228752ea8506SJoe Perches	$string =~ s/$find/$replace/g;
228852ea8506SJoe Perches
228952ea8506SJoe Perches	return $string;
229052ea8506SJoe Perches}
229152ea8506SJoe Perches
22923705ce5bSJoe Perchessub tabify {
22933705ce5bSJoe Perches	my ($leading) = @_;
22943705ce5bSJoe Perches
2295713a09deSAntonio Borneo	my $source_indent = $tabsize;
22963705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
22973705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
22983705ce5bSJoe Perches
22993705ce5bSJoe Perches	#convert leading spaces to tabs
23003705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
23013705ce5bSJoe Perches	#Remove spaces before a tab
23023705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
23033705ce5bSJoe Perches
23043705ce5bSJoe Perches	return "$leading";
23053705ce5bSJoe Perches}
23063705ce5bSJoe Perches
2307d1fe9c09SJoe Perchessub pos_last_openparen {
2308d1fe9c09SJoe Perches	my ($line) = @_;
2309d1fe9c09SJoe Perches
2310d1fe9c09SJoe Perches	my $pos = 0;
2311d1fe9c09SJoe Perches
2312d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2313d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2314d1fe9c09SJoe Perches
2315d1fe9c09SJoe Perches	my $last_openparen = 0;
2316d1fe9c09SJoe Perches
2317d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2318d1fe9c09SJoe Perches		return -1;
2319d1fe9c09SJoe Perches	}
2320d1fe9c09SJoe Perches
2321d1fe9c09SJoe Perches	my $len = length($line);
2322d1fe9c09SJoe Perches
2323d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2324d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2325d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2326d1fe9c09SJoe Perches			$pos += length($1) - 1;
2327d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2328d1fe9c09SJoe Perches			$last_openparen = $pos;
2329d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2330d1fe9c09SJoe Perches			last;
2331d1fe9c09SJoe Perches		}
2332d1fe9c09SJoe Perches	}
2333d1fe9c09SJoe Perches
233491cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2335d1fe9c09SJoe Perches}
2336d1fe9c09SJoe Perches
2337f36d3eb8SJoe Perchessub get_raw_comment {
2338f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2339f36d3eb8SJoe Perches	my $comment = '';
2340f36d3eb8SJoe Perches
2341f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2342f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2343f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2344f36d3eb8SJoe Perches		}
2345f36d3eb8SJoe Perches	}
2346f36d3eb8SJoe Perches
2347f36d3eb8SJoe Perches	return $comment;
2348f36d3eb8SJoe Perches}
2349f36d3eb8SJoe Perches
23500a920b5bSAndy Whitcroftsub process {
23510a920b5bSAndy Whitcroft	my $filename = shift;
23520a920b5bSAndy Whitcroft
23530a920b5bSAndy Whitcroft	my $linenr=0;
23540a920b5bSAndy Whitcroft	my $prevline="";
2355c2fdda0dSAndy Whitcroft	my $prevrawline="";
23560a920b5bSAndy Whitcroft	my $stashline="";
2357c2fdda0dSAndy Whitcroft	my $stashrawline="";
23580a920b5bSAndy Whitcroft
23594a0df2efSAndy Whitcroft	my $length;
23600a920b5bSAndy Whitcroft	my $indent;
23610a920b5bSAndy Whitcroft	my $previndent=0;
23620a920b5bSAndy Whitcroft	my $stashindent=0;
23630a920b5bSAndy Whitcroft
2364de7d4f0eSAndy Whitcroft	our $clean = 1;
23650a920b5bSAndy Whitcroft	my $signoff = 0;
2366cd261496SGeert Uytterhoeven	my $author = '';
2367cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
23680a920b5bSAndy Whitcroft	my $is_patch = 0;
2369133712a2SRob Herring	my $is_binding_patch = -1;
237029ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
237115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
237244d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2373ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2374490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2375bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
23762a076f40SJoe Perches	my $commit_log_long_line = 0;
2377e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
237813f1937eSJoe Perches	my $reported_maintainer_file = 0;
2379fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2380fa64205dSPasi Savanainen
2381365dd4eaSJoe Perches	my $last_blank_line = 0;
23825e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2383365dd4eaSJoe Perches
238413214adfSAndy Whitcroft	our @report = ();
23856c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
23866c72ffaaSAndy Whitcroft	our $cnt_error = 0;
23876c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
23886c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
23896c72ffaaSAndy Whitcroft
23900a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
23910a920b5bSAndy Whitcroft	my $realfile = '';
23920a920b5bSAndy Whitcroft	my $realline = 0;
23930a920b5bSAndy Whitcroft	my $realcnt = 0;
23940a920b5bSAndy Whitcroft	my $here = '';
239577cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23960a920b5bSAndy Whitcroft	my $in_comment = 0;
2397c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23980a920b5bSAndy Whitcroft	my $first_line = 0;
23991e855726SWolfram Sang	my $p1_prefix = '';
24000a920b5bSAndy Whitcroft
240113214adfSAndy Whitcroft	my $prev_values = 'E';
240213214adfSAndy Whitcroft
240313214adfSAndy Whitcroft	# suppression flags
2404773647a0SAndy Whitcroft	my %suppress_ifbraces;
2405170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
24062b474a1aSAndy Whitcroft	my %suppress_export;
24073e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2408653d4876SAndy Whitcroft
24097e51f197SJoe Perches	my %signatures = ();
2410323c1260SJoe Perches
2411c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2412de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2413c2fdda0dSAndy Whitcroft	#
2414de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2415de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2416773647a0SAndy Whitcroft
2417d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2418d8b07710SJoe Perches
24199f3a8992SRob Herring	my $checklicenseline = 1;
24209f3a8992SRob Herring
2421773647a0SAndy Whitcroft	sanitise_line_reset();
2422c2fdda0dSAndy Whitcroft	my $line;
2423c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2424773647a0SAndy Whitcroft		$linenr++;
2425773647a0SAndy Whitcroft		$line = $rawline;
2426c2fdda0dSAndy Whitcroft
24273705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
24283705ce5bSJoe Perches
2429773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2430de7d4f0eSAndy Whitcroft			$setup_docs = 0;
24312581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2432de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2433de7d4f0eSAndy Whitcroft			}
2434773647a0SAndy Whitcroft			#next;
2435de7d4f0eSAndy Whitcroft		}
243674fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2437773647a0SAndy Whitcroft			$realline=$1-1;
2438773647a0SAndy Whitcroft			if (defined $2) {
2439773647a0SAndy Whitcroft				$realcnt=$3+1;
2440773647a0SAndy Whitcroft			} else {
2441773647a0SAndy Whitcroft				$realcnt=1+1;
2442773647a0SAndy Whitcroft			}
2443c45dcabdSAndy Whitcroft			$in_comment = 0;
2444773647a0SAndy Whitcroft
2445773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2446773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2447773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2448773647a0SAndy Whitcroft			# at context start.
2449773647a0SAndy Whitcroft			my $edge;
245001fa9147SAndy Whitcroft			my $cnt = $realcnt;
245101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
245201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
245301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
245401fa9147SAndy Whitcroft				$cnt--;
245501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2456721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2457fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2458fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2459fae17daeSAndy Whitcroft					($edge) = $1;
2460fae17daeSAndy Whitcroft					last;
2461fae17daeSAndy Whitcroft				}
2462773647a0SAndy Whitcroft			}
2463773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2464773647a0SAndy Whitcroft				$in_comment = 1;
2465773647a0SAndy Whitcroft			}
2466773647a0SAndy Whitcroft
2467773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2468773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2469773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2470773647a0SAndy Whitcroft			if (!defined $edge &&
247183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2472773647a0SAndy Whitcroft			{
2473773647a0SAndy Whitcroft				$in_comment = 1;
2474773647a0SAndy Whitcroft			}
2475773647a0SAndy Whitcroft
2476773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2477773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2478773647a0SAndy Whitcroft
2479171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2480773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2481171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2482773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2483773647a0SAndy Whitcroft		}
2484773647a0SAndy Whitcroft		push(@lines, $line);
2485773647a0SAndy Whitcroft
2486773647a0SAndy Whitcroft		if ($realcnt > 1) {
2487773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2488773647a0SAndy Whitcroft		} else {
2489773647a0SAndy Whitcroft			$realcnt = 0;
2490773647a0SAndy Whitcroft		}
2491773647a0SAndy Whitcroft
2492773647a0SAndy Whitcroft		#print "==>$rawline\n";
2493773647a0SAndy Whitcroft		#print "-->$line\n";
2494de7d4f0eSAndy Whitcroft
2495de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2496de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2497de7d4f0eSAndy Whitcroft		}
2498de7d4f0eSAndy Whitcroft	}
2499de7d4f0eSAndy Whitcroft
25006c72ffaaSAndy Whitcroft	$prefix = '';
25016c72ffaaSAndy Whitcroft
2502773647a0SAndy Whitcroft	$realcnt = 0;
2503773647a0SAndy Whitcroft	$linenr = 0;
2504194f66fcSJoe Perches	$fixlinenr = -1;
25050a920b5bSAndy Whitcroft	foreach my $line (@lines) {
25060a920b5bSAndy Whitcroft		$linenr++;
2507194f66fcSJoe Perches		$fixlinenr++;
25081b5539b1SJoe Perches		my $sline = $line;	#copy of $line
25091b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
25100a920b5bSAndy Whitcroft
2511c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2512f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
25136c72ffaaSAndy Whitcroft
251412c253abSJoe Perches# check if it's a mode change, rename or start of a patch
251512c253abSJoe Perches		if (!$in_commit_log &&
251612c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
251712c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
251812c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
251912c253abSJoe Perches			$is_patch = 1;
252012c253abSJoe Perches		}
252112c253abSJoe Perches
25220a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2523e518e9a5SJoe Perches		if (!$in_commit_log &&
252474fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
252574fd4f34SJoe Perches			my $context = $4;
25260a920b5bSAndy Whitcroft			$is_patch = 1;
25274a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
25280a920b5bSAndy Whitcroft			$realline=$1-1;
25290a920b5bSAndy Whitcroft			if (defined $2) {
25300a920b5bSAndy Whitcroft				$realcnt=$3+1;
25310a920b5bSAndy Whitcroft			} else {
25320a920b5bSAndy Whitcroft				$realcnt=1+1;
25330a920b5bSAndy Whitcroft			}
2534c2fdda0dSAndy Whitcroft			annotate_reset();
253513214adfSAndy Whitcroft			$prev_values = 'E';
253613214adfSAndy Whitcroft
2537773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2538170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
25392b474a1aSAndy Whitcroft			%suppress_export = ();
25403e469cdcSAndy Whitcroft			$suppress_statement = 0;
254174fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
254274fd4f34SJoe Perches				$context_function = $1;
254374fd4f34SJoe Perches			} else {
254474fd4f34SJoe Perches				undef $context_function;
254574fd4f34SJoe Perches			}
25460a920b5bSAndy Whitcroft			next;
25470a920b5bSAndy Whitcroft
25484a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
25494a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
25504a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2551773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
25520a920b5bSAndy Whitcroft			$realline++;
2553d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
25540a920b5bSAndy Whitcroft
25554a0df2efSAndy Whitcroft			# Measure the line length and indent.
2556c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
25570a920b5bSAndy Whitcroft
25580a920b5bSAndy Whitcroft			# Track the previous line.
25590a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
25600a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2561c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2562c2fdda0dSAndy Whitcroft
2563773647a0SAndy Whitcroft			#warn "line<$line>\n";
25646c72ffaaSAndy Whitcroft
2565d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2566d8aaf121SAndy Whitcroft			$realcnt--;
25670a920b5bSAndy Whitcroft		}
25680a920b5bSAndy Whitcroft
2569cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2570cc77cdcaSAndy Whitcroft
25716c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
25726c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2573773647a0SAndy Whitcroft
25742ac73b4fSJoe Perches		my $found_file = 0;
2575773647a0SAndy Whitcroft		# extract the filename as it passes
25763bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
25773bf9a009SRabin Vincent			$realfile = $1;
25782b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2579270c49a0SJoe Perches			$in_commit_log = 0;
25802ac73b4fSJoe Perches			$found_file = 1;
25813bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2582773647a0SAndy Whitcroft			$realfile = $1;
25832b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2584270c49a0SJoe Perches			$in_commit_log = 0;
25851e855726SWolfram Sang
25861e855726SWolfram Sang			$p1_prefix = $1;
2587e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2588e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2589000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2590000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
25911e855726SWolfram Sang			}
2592773647a0SAndy Whitcroft
2593c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2594000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2595000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2596773647a0SAndy Whitcroft			}
25972ac73b4fSJoe Perches			$found_file = 1;
25982ac73b4fSJoe Perches		}
25992ac73b4fSJoe Perches
260034d8815fSJoe Perches#make up the handle for any error we report on this line
260134d8815fSJoe Perches		if ($showfile) {
260234d8815fSJoe Perches			$prefix = "$realfile:$realline: "
260334d8815fSJoe Perches		} elsif ($emacs) {
26047d3a9f67SJoe Perches			if ($file) {
26057d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
26067d3a9f67SJoe Perches			} else {
260734d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
260834d8815fSJoe Perches			}
26097d3a9f67SJoe Perches		}
261034d8815fSJoe Perches
26112ac73b4fSJoe Perches		if ($found_file) {
261285b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
261385b0ee18SJoe Perches				WARN("OBSOLETE",
261485b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
261585b0ee18SJoe Perches			}
26167bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
26172ac73b4fSJoe Perches				$check = 1;
26182ac73b4fSJoe Perches			} else {
26192ac73b4fSJoe Perches				$check = $check_orig;
26202ac73b4fSJoe Perches			}
26219f3a8992SRob Herring			$checklicenseline = 1;
2622133712a2SRob Herring
2623133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2624133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2625133712a2SRob Herring
2626133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2627133712a2SRob Herring
2628133712a2SRob Herring				if (($last_binding_patch != -1) &&
2629133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2630133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2631858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2632133712a2SRob Herring				}
2633133712a2SRob Herring			}
2634133712a2SRob Herring
2635773647a0SAndy Whitcroft			next;
2636773647a0SAndy Whitcroft		}
2637773647a0SAndy Whitcroft
2638389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
26390a920b5bSAndy Whitcroft
2640c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2641c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2642c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
26430a920b5bSAndy Whitcroft
26446c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
26456c72ffaaSAndy Whitcroft
2646490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2647490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2648490b292cSJoe Perches		if ($in_commit_log) {
2649490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2650490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2651490b292cSJoe Perches			}
2652490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2653490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2654490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2655490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2656490b292cSJoe Perches		}
2657490b292cSJoe Perches
2658e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2659e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
266013e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
266113e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2662e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2663e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2664e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2665e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2666e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2667e518e9a5SJoe Perches		}
2668e518e9a5SJoe Perches
26693bf9a009SRabin Vincent# Check for incorrect file permissions
26703bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
26713bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
267204db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
267304db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2674000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2675000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
26763bf9a009SRabin Vincent			}
26773bf9a009SRabin Vincent		}
26783bf9a009SRabin Vincent
2679cd261496SGeert Uytterhoeven# Check the patch for a From:
2680cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2681cd261496SGeert Uytterhoeven			$author = $1;
2682e7f929f3SDwaipayan Ray			my $curline = $linenr;
2683e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2684e7f929f3SDwaipayan Ray				$author .= $1;
2685e7f929f3SDwaipayan Ray			}
2686cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2687cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2688dfa05c28SJoe Perches			$author = reformat_email($author);
2689cd261496SGeert Uytterhoeven		}
2690cd261496SGeert Uytterhoeven
269120112475SJoe Perches# Check the patch for a signoff:
2692dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
26934a0df2efSAndy Whitcroft			$signoff++;
269415662b3eSJoe Perches			$in_commit_log = 0;
2695cd261496SGeert Uytterhoeven			if ($author ne '') {
2696dfa05c28SJoe Perches				if (same_email_addresses($1, $author)) {
2697cd261496SGeert Uytterhoeven					$authorsignoff = 1;
2698cd261496SGeert Uytterhoeven				}
2699cd261496SGeert Uytterhoeven			}
27000a920b5bSAndy Whitcroft		}
270120112475SJoe Perches
270244d303ebSJoe Perches# Check for patch separator
270344d303ebSJoe Perches		if ($line =~ /^---$/) {
270444d303ebSJoe Perches			$has_patch_separator = 1;
270544d303ebSJoe Perches			$in_commit_log = 0;
270644d303ebSJoe Perches		}
270744d303ebSJoe Perches
2708e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2709e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2710e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2711e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2712e0d975b1SJoe Perches		}
2713e0d975b1SJoe Perches
271420112475SJoe Perches# Check signature styles
2715270c49a0SJoe Perches		if (!$in_header_lines &&
2716ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
271720112475SJoe Perches			my $space_before = $1;
271820112475SJoe Perches			my $sign_off = $2;
271920112475SJoe Perches			my $space_after = $3;
272020112475SJoe Perches			my $email = $4;
272120112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
272220112475SJoe Perches
2723ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2724ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2725ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2726ce0338dfSJoe Perches			}
272720112475SJoe Perches			if (defined $space_before && $space_before ne "") {
27283705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27293705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
27303705ce5bSJoe Perches				    $fix) {
2731194f66fcSJoe Perches					$fixed[$fixlinenr] =
27323705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27333705ce5bSJoe Perches				}
273420112475SJoe Perches			}
273520112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
27363705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27373705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
27383705ce5bSJoe Perches				    $fix) {
2739194f66fcSJoe Perches					$fixed[$fixlinenr] =
27403705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27413705ce5bSJoe Perches				}
27423705ce5bSJoe Perches
274320112475SJoe Perches			}
274420112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
27453705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27463705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
27473705ce5bSJoe Perches				    $fix) {
2748194f66fcSJoe Perches					$fixed[$fixlinenr] =
27493705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27503705ce5bSJoe Perches				}
275120112475SJoe Perches			}
275220112475SJoe Perches
2753dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
275420112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
275520112475SJoe Perches			if ($suggested_email eq "") {
2756000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2757000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
275820112475SJoe Perches			} else {
275920112475SJoe Perches				my $dequoted = $suggested_email;
276020112475SJoe Perches				$dequoted =~ s/^"//;
276120112475SJoe Perches				$dequoted =~ s/" </ </;
276220112475SJoe Perches				# Don't force email to have quotes
276320112475SJoe Perches				# Allow just an angle bracketed address
2764dfa05c28SJoe Perches				if (!same_email_addresses($email, $suggested_email)) {
2765000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2766000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
276720112475SJoe Perches				}
27680a920b5bSAndy Whitcroft			}
27697e51f197SJoe Perches
27707e51f197SJoe Perches# Check for duplicate signatures
27717e51f197SJoe Perches			my $sig_nospace = $line;
27727e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
27737e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
27747e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
27757e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
27767e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
27777e51f197SJoe Perches			} else {
27787e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
27797e51f197SJoe Perches			}
27806c5d24eeSSean Christopherson
27816c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
27826c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
27836c5d24eeSSean Christopherson				if ($email eq $author) {
27846c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27856c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
27866c5d24eeSSean Christopherson				}
27876c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
27886c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27896c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
27906c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
27916c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27926c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27936c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
27946c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27956c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27966c5d24eeSSean Christopherson				}
27976c5d24eeSSean Christopherson			}
27980a920b5bSAndy Whitcroft		}
27990a920b5bSAndy Whitcroft
2800a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2801a2fe16b9SJoe Perches		if ($in_header_lines &&
2802a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2803a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2804a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2805a2fe16b9SJoe Perches		}
2806a2fe16b9SJoe Perches
280744d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
280844d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
28097ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
281044d303ebSJoe Perches			      "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
28117ebd05efSChristopher Covington		}
28127ebd05efSChristopher Covington
2813369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2814369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2815369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2816369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2817369c8dd3SJoe Perches					# timestamp
2818634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2819634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2820634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2821634cffccSJoe Perches					# stack dump address styles
2822369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2823369c8dd3SJoe Perches		}
2824369c8dd3SJoe Perches
28252a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
28262a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2827bf4daf12SJoe Perches		    length($line) > 75 &&
2828bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2829bf4daf12SJoe Perches					# file delta changes
2830bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2831bf4daf12SJoe Perches					# filename then :
2832bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2833bf4daf12SJoe Perches					# A Fixes: or Link: line
2834bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
28352a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
28362a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
28372a076f40SJoe Perches			$commit_log_long_line = 1;
28382a076f40SJoe Perches		}
28392a076f40SJoe Perches
2840bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2841bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2842bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2843bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2844bf4daf12SJoe Perches		}
2845bf4daf12SJoe Perches
28460d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2847369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2848a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
2849e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2850fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2851aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2852369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2853bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2854fe043ea1SJoe Perches			my $init_char = "c";
2855fe043ea1SJoe Perches			my $orig_commit = "";
28560d7835fcSJoe Perches			my $short = 1;
28570d7835fcSJoe Perches			my $long = 0;
28580d7835fcSJoe Perches			my $case = 1;
28590d7835fcSJoe Perches			my $space = 1;
28600d7835fcSJoe Perches			my $hasdesc = 0;
286119c146a6SJoe Perches			my $hasparens = 0;
28620d7835fcSJoe Perches			my $id = '0123456789ab';
28630d7835fcSJoe Perches			my $orig_desc = "commit description";
28640d7835fcSJoe Perches			my $description = "";
28650d7835fcSJoe Perches
2866fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2867fe043ea1SJoe Perches				$init_char = $1;
2868fe043ea1SJoe Perches				$orig_commit = lc($2);
2869fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2870fe043ea1SJoe Perches				$orig_commit = lc($1);
2871fe043ea1SJoe Perches			}
2872fe043ea1SJoe Perches
28730d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
28740d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
28750d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
28760d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
28770d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
28780d7835fcSJoe Perches				$orig_desc = $1;
287919c146a6SJoe Perches				$hasparens = 1;
28800d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
28810d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
28820d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
28830d7835fcSJoe Perches				$orig_desc = $1;
288419c146a6SJoe Perches				$hasparens = 1;
2885b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2886b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2887b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2888b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2889b671fde0SJoe Perches				$orig_desc = $1;
2890b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2891b671fde0SJoe Perches				$orig_desc .= " " . $1;
289219c146a6SJoe Perches				$hasparens = 1;
28930d7835fcSJoe Perches			}
28940d7835fcSJoe Perches
28950d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
28960d7835fcSJoe Perches							      $id, $orig_desc);
28970d7835fcSJoe Perches
2898948b133aSHeinrich Schuchardt			if (defined($id) &&
2899948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2900d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
29010d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
29020d7835fcSJoe Perches			}
2903d311cd44SJoe Perches		}
2904d311cd44SJoe Perches
290513f1937eSJoe Perches# Check for added, moved or deleted files
290613f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
290713f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
290813f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
290913f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
291013f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2911a82603a8SAndrew Jeffery			$is_patch = 1;
291213f1937eSJoe Perches			$reported_maintainer_file = 1;
291313f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
291413f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
291513f1937eSJoe Perches		}
291613f1937eSJoe Perches
2917e400edb1SRob Herring# Check for adding new DT bindings not in schema format
2918e400edb1SRob Herring		if (!$in_commit_log &&
2919e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
2920e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2921e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
2922e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2923e400edb1SRob Herring		}
2924e400edb1SRob Herring
292500df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
29268905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2927000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2928000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
29296c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2930de7d4f0eSAndy Whitcroft		}
2931de7d4f0eSAndy Whitcroft
2932de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2933de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2934171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2935171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2936171ae1a4SAndy Whitcroft
2937171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2938171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2939171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2940171ae1a4SAndy Whitcroft
294134d99219SJoe Perches			CHK("INVALID_UTF8",
2942000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
294300df344fSAndy Whitcroft		}
29440a920b5bSAndy Whitcroft
294515662b3eSJoe Perches# Check if it's the start of a commit log
294615662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
294715662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2948eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2949eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
295015662b3eSJoe Perches			$in_header_lines = 0;
295115662b3eSJoe Perches			$in_commit_log = 1;
2952ed43c4e5SAllen Hubbe			$has_commit_log = 1;
295315662b3eSJoe Perches		}
295415662b3eSJoe Perches
2955fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2956fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2957fa64205dSPasi Savanainen		if ($in_header_lines &&
2958fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2959fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2960fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2961fa64205dSPasi Savanainen		}
2962fa64205dSPasi Savanainen
2963fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
296415662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2965fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
296615662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
296715662b3eSJoe Perches		}
296815662b3eSJoe Perches
2969d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2970d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2971d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2972d6430f71SJoe Perches				my $file = $1;
2973d6430f71SJoe Perches
2974d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2975d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2976d6430f71SJoe Perches					#
2977d6430f71SJoe Perches				} else {
2978d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2979d6430f71SJoe Perches				}
2980d6430f71SJoe Perches			}
2981d6430f71SJoe Perches		}
2982d6430f71SJoe Perches
298366b47b4aSKees Cook# Check for various typo / spelling mistakes
298466d7a382SJoe Perches		if (defined($misspellings) &&
298566d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2986ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
298766b47b4aSKees Cook				my $typo = $1;
298866b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
298966b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
299066b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
29910675a8fbSJean Delvare				my $msg_level = \&WARN;
29920675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
29930675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
299466b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
299566b47b4aSKees Cook				    $fix) {
299666b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
299766b47b4aSKees Cook				}
299866b47b4aSKees Cook			}
299966b47b4aSKees Cook		}
300066b47b4aSKees Cook
3001a8dd86bfSMatteo Croce# check for invalid commit id
3002a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3003a8dd86bfSMatteo Croce			my $id;
3004a8dd86bfSMatteo Croce			my $description;
3005a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
3006a8dd86bfSMatteo Croce			if (!defined($id)) {
3007a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
3008a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3009a8dd86bfSMatteo Croce			}
3010a8dd86bfSMatteo Croce		}
3011a8dd86bfSMatteo Croce
3012310cd06bSJoe Perches# check for repeated words separated by a single space
3013310cd06bSJoe Perches		if ($rawline =~ /^\+/ || $in_commit_log) {
3014310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3015310cd06bSJoe Perches
3016310cd06bSJoe Perches				my $first = $1;
3017310cd06bSJoe Perches				my $second = $2;
3018310cd06bSJoe Perches
3019310cd06bSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3020310cd06bSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3021310cd06bSJoe Perches					next;
3022310cd06bSJoe Perches				}
3023310cd06bSJoe Perches
3024310cd06bSJoe Perches				next if ($first ne $second);
3025310cd06bSJoe Perches				next if ($first eq 'long');
3026310cd06bSJoe Perches
3027310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3028310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3029310cd06bSJoe Perches				    $fix) {
3030310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3031310cd06bSJoe Perches				}
3032310cd06bSJoe Perches			}
3033310cd06bSJoe Perches
3034310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3035310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3036310cd06bSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3037310cd06bSJoe Perches				my $last_word = $1;
3038310cd06bSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3039310cd06bSJoe Perches					if (WARN("REPEATED_WORD",
3040310cd06bSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3041310cd06bSJoe Perches					    $fix) {
3042310cd06bSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3043310cd06bSJoe Perches					}
3044310cd06bSJoe Perches				}
3045310cd06bSJoe Perches			}
3046310cd06bSJoe Perches		}
3047310cd06bSJoe Perches
304830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
304930670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
305000df344fSAndy Whitcroft
30510a920b5bSAndy Whitcroft#trailing whitespace
30529c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
3053c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3054d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3055d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
3056d5e616fcSJoe Perches			    $fix) {
3057194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3058d5e616fcSJoe Perches			}
3059c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3060c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30613705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
30623705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
30633705ce5bSJoe Perches			    $fix) {
3064194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
30653705ce5bSJoe Perches			}
30663705ce5bSJoe Perches
3067d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
30680a920b5bSAndy Whitcroft		}
30695368df20SAndy Whitcroft
30704783f894SJosh Triplett# Check for FSF mailing addresses.
3071109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
30721bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
30733e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
30743e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
30754783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30760675a8fbSJean Delvare			my $msg_level = \&ERROR;
30770675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
30780675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
30794783f894SJosh 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)
30804783f894SJosh Triplett		}
30814783f894SJosh Triplett
30823354957aSAndi Kleen# check for Kconfig help text having a real description
30839fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
30849fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
30853354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3086678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3087678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3088678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3089678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
30903354957aSAndi Kleen			my $length = 0;
30919fe287d7SAndy Whitcroft			my $cnt = $realcnt;
30929fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
30939fe287d7SAndy Whitcroft			my $f;
3094a1385803SAndy Whitcroft			my $is_start = 0;
30959fe287d7SAndy Whitcroft			my $is_end = 0;
3096a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
30979fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
30989fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
30999fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
31009fe287d7SAndy Whitcroft
31019fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
31028d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3103a1385803SAndy Whitcroft
310486adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3105a1385803SAndy Whitcroft					$is_start = 1;
310622a4ac02SMasahiro Yamada				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
3107a1385803SAndy Whitcroft					$length = -1;
3108a1385803SAndy Whitcroft				}
3109a1385803SAndy Whitcroft
31109fe287d7SAndy Whitcroft				$f =~ s/^.//;
31113354957aSAndi Kleen				$f =~ s/#.*//;
31123354957aSAndi Kleen				$f =~ s/^\s+//;
31133354957aSAndi Kleen				next if ($f =~ /^$/);
3114678ae162SUlf Magnusson
3115678ae162SUlf Magnusson				# This only checks context lines in the patch
3116678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3117678ae162SUlf Magnusson				# positives, even though some of these are
3118678ae162SUlf Magnusson				# common words in help texts
3119678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3120678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
31219fe287d7SAndy Whitcroft					$is_end = 1;
31229fe287d7SAndy Whitcroft					last;
31239fe287d7SAndy Whitcroft				}
31243354957aSAndi Kleen				$length++;
31253354957aSAndi Kleen			}
312656193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3127000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
312856193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
312956193274SVadim Bendebury			}
3130a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
31313354957aSAndi Kleen		}
31323354957aSAndi Kleen
31337ccf41a8SJoe Perches# check MAINTAINERS entries
31347ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
31357ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
31367ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3137628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3138628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3139628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3140628f91a2SJoe Perches				    $fix) {
3141628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3142628f91a2SJoe Perches				}
3143628f91a2SJoe Perches			}
31447ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
31457ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
31467ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
31477ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
31487ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
31497ccf41a8SJoe Perches				my $cur = $1;
31507ccf41a8SJoe Perches				my $curval = $2;
31517ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
31527ccf41a8SJoe Perches				my $prev = $1;
31537ccf41a8SJoe Perches				my $prevval = $2;
31547ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
31557ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
31567ccf41a8SJoe Perches				if ($curindex < 0) {
31577ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
31587ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
31597ccf41a8SJoe Perches				} else {
31607ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
31617ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
31627ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
31637ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
31647ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
31657ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
31667ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
31677ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
31687ccf41a8SJoe Perches					}
31697ccf41a8SJoe Perches				}
31707ccf41a8SJoe Perches			}
31717ccf41a8SJoe Perches		}
3172628f91a2SJoe Perches
3173327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3174327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3175327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3176327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3177327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3178327953e9SChristoph Jaeger		}
3179327953e9SChristoph Jaeger
3180c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3181c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3182c68e5878SArnaud Lacombe			my $flag = $1;
3183c68e5878SArnaud Lacombe			my $replacement = {
3184c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3185c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3186c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3187c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3188c68e5878SArnaud Lacombe			};
3189c68e5878SArnaud Lacombe
3190c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3191c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3192c68e5878SArnaud Lacombe		}
3193c68e5878SArnaud Lacombe
3194bff5da43SRob Herring# check for DT compatible documentation
31957dd05b38SFlorian Vaussard		if (defined $root &&
31967dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
31977dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
31987dd05b38SFlorian Vaussard
3199bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3200bff5da43SRob Herring
3201cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3202852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3203cc93319bSFlorian Vaussard
3204bff5da43SRob Herring			foreach my $compat (@compats) {
3205bff5da43SRob Herring				my $compat2 = $compat;
3206185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3207185d566bSRob Herring				my $compat3 = $compat;
3208185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3209185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3210bff5da43SRob Herring				if ( $? >> 8 ) {
3211bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3212bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3213bff5da43SRob Herring				}
3214bff5da43SRob Herring
32154fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
32164fbf32a6SFlorian Vaussard				my $vendor = $1;
3217852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3218bff5da43SRob Herring				if ( $? >> 8 ) {
3219bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3220cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3221bff5da43SRob Herring				}
3222bff5da43SRob Herring			}
3223bff5da43SRob Herring		}
3224bff5da43SRob Herring
32259f3a8992SRob Herring# check for using SPDX license tag at beginning of files
32269f3a8992SRob Herring		if ($realline == $checklicenseline) {
32279f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
32289f3a8992SRob Herring				$checklicenseline = 2;
32299f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
32309f3a8992SRob Herring				my $comment = "";
32319f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
32329f3a8992SRob Herring					$comment = '/*';
32339f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
32349f3a8992SRob Herring					$comment = '//';
3235c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
32369f3a8992SRob Herring					$comment = '#';
32379f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
32389f3a8992SRob Herring					$comment = '..';
32399f3a8992SRob Herring				}
32409f3a8992SRob Herring
3241fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3242fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3243fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3244ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3245fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3246fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3247fdf13693SJoe Perches				}
3248fdf13693SJoe Perches
32499f3a8992SRob Herring				if ($comment !~ /^$/ &&
3250ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
32519f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
32529f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
32533b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
32543b6e8ac9SJoe Perches					my $spdx_license = $1;
32553b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
32563b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
32573b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
32583b6e8ac9SJoe Perches					}
325950c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
326050c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
326150c92900SLubomir Rintel						my $msg_level = \&WARN;
326250c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
326350c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
326450c92900SLubomir Rintel
326550c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
326650c92900SLubomir Rintel						    $fix) {
326750c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
326850c92900SLubomir Rintel						}
326950c92900SLubomir Rintel					}
32709f3a8992SRob Herring				}
32719f3a8992SRob Herring			}
32729f3a8992SRob Herring		}
32739f3a8992SRob Herring
3274a0154cdbSJoe Perches# check for embedded filenames
3275a0154cdbSJoe Perches		if ($rawline =~ /^\+.*\Q$realfile\E/) {
3276a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3277a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3278a0154cdbSJoe Perches		}
3279a0154cdbSJoe Perches
32805368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3281d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
32825368df20SAndy Whitcroft
3283a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3284a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3285a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3286a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3287a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3288a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3289a8da38a9SJoe Perches		}
3290a8da38a9SJoe Perches
329147e0c88bSJoe Perches# line length limit (with some exclusions)
329247e0c88bSJoe Perches#
329347e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
329447e0c88bSJoe Perches#	logging functions like pr_info that end in a string
329547e0c88bSJoe Perches#	lines with a single string
329647e0c88bSJoe Perches#	#defines that are a single string
32972e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
329847e0c88bSJoe Perches#
329947e0c88bSJoe Perches# There are 3 different line length message types:
3300ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
330147e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
330247e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
330347e0c88bSJoe Perches#
330447e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
330547e0c88bSJoe Perches#
330647e0c88bSJoe Perches
3307b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
330847e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
330947e0c88bSJoe Perches
331047e0c88bSJoe Perches			# Check the allowed long line types first
331147e0c88bSJoe Perches
331247e0c88bSJoe Perches			# logging functions that end in a string that starts
331347e0c88bSJoe Perches			# before $max_line_length
331447e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
331547e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
331647e0c88bSJoe Perches				$msg_type = "";
331747e0c88bSJoe Perches
331847e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
331947e0c88bSJoe Perches			# #defines with only strings
332047e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
332147e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
332247e0c88bSJoe Perches				$msg_type = "";
332347e0c88bSJoe Perches
3324cc147506SJoe Perches			# More special cases
3325cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3326cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3327d560a5f8SJoe Perches				$msg_type = "";
3328d560a5f8SJoe Perches
33292e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
33302e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
33312e4bbbc5SAndreas Brauchli				$msg_type = "";
33322e4bbbc5SAndreas Brauchli
333347e0c88bSJoe Perches			# Otherwise set the alternate message types
333447e0c88bSJoe Perches
333547e0c88bSJoe Perches			# a comment starts before $max_line_length
333647e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
333747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
333847e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
333947e0c88bSJoe Perches
334047e0c88bSJoe Perches			# a quoted string starts before $max_line_length
334147e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
334247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
334347e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
334447e0c88bSJoe Perches			}
334547e0c88bSJoe Perches
334647e0c88bSJoe Perches			if ($msg_type ne "" &&
334747e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3348bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3349bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3350bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3351bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
33520a920b5bSAndy Whitcroft			}
335347e0c88bSJoe Perches		}
33540a920b5bSAndy Whitcroft
33558905a67cSAndy Whitcroft# check for adding lines without a newline.
33568905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3357000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3358000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
33598905a67cSAndy Whitcroft		}
33608905a67cSAndy Whitcroft
3361b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3362de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
33630a920b5bSAndy Whitcroft
33640a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3365713a09deSAntonio Borneo# more than $tabsize must use tabs.
3366c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3367c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3368c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3369d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
33703705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
33713705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
33723705ce5bSJoe Perches			    $fix) {
3373194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
33743705ce5bSJoe Perches			}
33750a920b5bSAndy Whitcroft		}
33760a920b5bSAndy Whitcroft
337708e44365SAlberto Panizzo# check for space before tabs.
337808e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
337908e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
33803705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
33813705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
33823705ce5bSJoe Perches			    $fix) {
3383194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3384713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3385194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3386c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
33873705ce5bSJoe Perches			}
338808e44365SAlberto Panizzo		}
338908e44365SAlberto Panizzo
33906a487211SJoe Perches# check for assignments on the start of a line
33916a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
33926a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
33936a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
33946a487211SJoe Perches		}
33956a487211SJoe Perches
3396d1fe9c09SJoe Perches# check for && or || at the start of a line
3397d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3398d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3399d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3400d1fe9c09SJoe Perches		}
3401d1fe9c09SJoe Perches
3402a91e8994SJoe Perches# check indentation starts on a tab stop
34035b57980dSJoe Perches		if ($perl_version_ok &&
3404bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3405a91e8994SJoe Perches			my $indent = length($1);
3406713a09deSAntonio Borneo			if ($indent % $tabsize) {
3407a91e8994SJoe Perches				if (WARN("TABSTOP",
3408a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3409a91e8994SJoe Perches				    $fix) {
3410713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3411a91e8994SJoe Perches				}
3412a91e8994SJoe Perches			}
3413a91e8994SJoe Perches		}
3414a91e8994SJoe Perches
3415d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
34165b57980dSJoe Perches		if ($perl_version_ok &&
3417fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3418d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3419d1fe9c09SJoe Perches			my $oldindent = $1;
3420d1fe9c09SJoe Perches			my $rest = $2;
3421d1fe9c09SJoe Perches
3422d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3423d1fe9c09SJoe Perches			if ($pos >= 0) {
3424b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3425b34a26f3SJoe Perches				my $newindent = $2;
3426d1fe9c09SJoe Perches
3427d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3428713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3429713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3430d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3431d1fe9c09SJoe Perches
3432d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3433d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
34343705ce5bSJoe Perches
34353705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
34363705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
34373705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3438194f66fcSJoe Perches						$fixed[$fixlinenr] =~
34393705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
34403705ce5bSJoe Perches					}
3441d1fe9c09SJoe Perches				}
3442d1fe9c09SJoe Perches			}
3443d1fe9c09SJoe Perches		}
3444d1fe9c09SJoe Perches
34456ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
34466ab3a970SJoe Perches# avoid checking a few false positives:
34476ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
34486ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
34496ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
34506ab3a970SJoe Perches#   multiline macros that define functions
34516ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
34526ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
34536ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
34543705ce5bSJoe Perches			if (CHK("SPACING",
3455f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
34563705ce5bSJoe Perches			    $fix) {
3457194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3458f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
34593705ce5bSJoe Perches			}
3460aad4f614SJoe Perches		}
3461aad4f614SJoe Perches
346286406b1cSJoe Perches# Block comment styles
346386406b1cSJoe Perches# Networking with an initial /*
346405880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3465fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
346685ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
346785ad978cSJoe Perches		    $realline > 2) {
346805880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
346905880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
347005880600SJoe Perches		}
347105880600SJoe Perches
347286406b1cSJoe Perches# Block comments use * on subsequent lines
347386406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
347486406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3475a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
347661135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3477a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
347886406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
347986406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3480a605e32eSJoe Perches		}
3481a605e32eSJoe Perches
348286406b1cSJoe Perches# Block comments use */ on trailing lines
348386406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3484c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3485c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3486c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
348786406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
348886406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
348905880600SJoe Perches		}
349005880600SJoe Perches
349108eb9b80SJoe Perches# Block comment * alignment
349208eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3493af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3494af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3495af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
349608eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3497af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3498af207524SJoe Perches			my $oldindent;
349908eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3500af207524SJoe Perches			if (defined($1)) {
3501af207524SJoe Perches				$oldindent = expand_tabs($1);
3502af207524SJoe Perches			} else {
3503af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3504af207524SJoe Perches				$oldindent = expand_tabs($1);
3505af207524SJoe Perches			}
350608eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
350708eb9b80SJoe Perches			my $newindent = $1;
350808eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3509af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
351008eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
351108eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
351208eb9b80SJoe Perches			}
351308eb9b80SJoe Perches		}
351408eb9b80SJoe Perches
35157f619191SJoe Perches# check for missing blank lines after struct/union declarations
35167f619191SJoe Perches# with exceptions for various attributes and macros
35177f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
35187f619191SJoe Perches		    $line =~ /^\+/ &&
35197f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
35207f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
35217f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
35227f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
35237f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
35247f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
35257f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
35260bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
35277f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3528d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3529d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3530d752fcc8SJoe Perches			    $fix) {
3531f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3532d752fcc8SJoe Perches			}
35337f619191SJoe Perches		}
35347f619191SJoe Perches
3535365dd4eaSJoe Perches# check for multiple consecutive blank lines
3536365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3537365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3538365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3539d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3540d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3541d752fcc8SJoe Perches			    $fix) {
3542f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3543d752fcc8SJoe Perches			}
3544d752fcc8SJoe Perches
3545365dd4eaSJoe Perches			$last_blank_line = $linenr;
3546365dd4eaSJoe Perches		}
3547365dd4eaSJoe Perches
35483b617e3bSJoe Perches# check for missing blank lines after declarations
35493f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
35503f7bac03SJoe Perches			# actual declarations
35513f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
35525a4e1fd3SJoe Perches			# function pointer declarations
35535a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
35543f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
35553f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
35563f7bac03SJoe Perches			# known declaration macros
35573f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
35583f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
35593f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
35603f7bac03SJoe Perches			# other possible extensions of declaration lines
35613f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
35623f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
35633f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
35643f7bac03SJoe Perches			# looks like a declaration
35653f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
35665a4e1fd3SJoe Perches			# function pointer declarations
35675a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
35683f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
35693f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
35703f7bac03SJoe Perches			# known declaration macros
35713f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
35723f7bac03SJoe Perches			# start of struct or union or enum
3573328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
35743f7bac03SJoe Perches			# start or end of block or continuation of declaration
35753f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
35763f7bac03SJoe Perches			# bitfield continuation
35773f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
35783f7bac03SJoe Perches			# other possible extensions of declaration lines
35793f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
35803f7bac03SJoe Perches			# indentation of previous and current line are the same
35813f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3582d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3583d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3584d752fcc8SJoe Perches			    $fix) {
3585f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3586d752fcc8SJoe Perches			}
35873b617e3bSJoe Perches		}
35883b617e3bSJoe Perches
35895f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
35906b4c5bebSAndy Whitcroft# Exceptions:
35916b4c5bebSAndy Whitcroft#  1) within comments
35926b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
35936b4c5bebSAndy Whitcroft#  3) hanging labels
35943705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
35955f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35963705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
35973705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
35983705ce5bSJoe Perches			    $fix) {
3599194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
36003705ce5bSJoe Perches			}
36015f7ddae6SRaffaele Recalcati		}
36025f7ddae6SRaffaele Recalcati
3603b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3604b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3605b9ea10d6SAndy Whitcroft
36065751a24eSJoe Perches# check for unusual line ending [ or (
36075751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
36085751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
36095751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
36105751a24eSJoe Perches		}
36115751a24eSJoe Perches
36124dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
36134dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
36144dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
36154dbed76fSJoe Perches			$context_function = $1;
36164dbed76fSJoe Perches		}
36174dbed76fSJoe Perches
36184dbed76fSJoe Perches# check if this appears to be the end of function declaration
36194dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
36204dbed76fSJoe Perches			undef $context_function;
36214dbed76fSJoe Perches		}
36224dbed76fSJoe Perches
3623032a4c0fSJoe Perches# check indentation of any line with a bare else
3624840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3625032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3626032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3627032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3628840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3629840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3630840080a0SJoe Perches			     defined $lines[$linenr] &&
3631840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3632032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3633032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3634032a4c0fSJoe Perches			}
3635032a4c0fSJoe Perches		}
3636032a4c0fSJoe Perches
3637c00df19aSJoe Perches# check indentation of a line with a break;
3638c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3639c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3640c00df19aSJoe Perches			my $tabs = $1;
3641c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3642c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3643c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3644c00df19aSJoe Perches			}
3645c00df19aSJoe Perches		}
3646c00df19aSJoe Perches
3647c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3648cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3649000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3650000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3651c2fdda0dSAndy Whitcroft		}
365222f2a2efSAndy Whitcroft
365356e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
365456e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
365556e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
365656e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
365756e77d70SJoe Perches		}
365856e77d70SJoe Perches
36599c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
36602b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
36612b474a1aSAndy Whitcroft		    $realline_next);
36623e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3663ca819864SJoe Perches		if ($linenr > $suppress_statement &&
36641b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3665170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3666f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3667171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3668171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3669171ae1a4SAndy Whitcroft
36703e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
36713e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
36723e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
36733e469cdcSAndy Whitcroft			# until we hit end of it.
36743e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
36753e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
36763e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
36773e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
36783e469cdcSAndy Whitcroft			}
3679f74bd194SAndy Whitcroft
36802b474a1aSAndy Whitcroft			# Find the real next line.
36812b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
36822b474a1aSAndy Whitcroft			if (defined $realline_next &&
36832b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
36842b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
36852b474a1aSAndy Whitcroft				$realline_next++;
36862b474a1aSAndy Whitcroft			}
36872b474a1aSAndy Whitcroft
3688171ae1a4SAndy Whitcroft			my $s = $stat;
3689171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3690cf655043SAndy Whitcroft
3691c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3692171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3693c2fdda0dSAndy Whitcroft
3694c2fdda0dSAndy Whitcroft			# Ignore functions being called
3695171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3696c2fdda0dSAndy Whitcroft
3697463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3698463f2864SAndy Whitcroft
3699c45dcabdSAndy Whitcroft			# declarations always start with types
3700d2506586SAndy 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) {
3701c45dcabdSAndy Whitcroft				my $type = $1;
3702c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3703c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3704c45dcabdSAndy Whitcroft
37056c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3706a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3707c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3708c2fdda0dSAndy Whitcroft			}
37098905a67cSAndy Whitcroft
37106c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
371165863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3712c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
37139c0ca6f9SAndy Whitcroft			}
37148905a67cSAndy Whitcroft
37158905a67cSAndy Whitcroft			# Check for any sort of function declaration.
37168905a67cSAndy Whitcroft			# int foo(something bar, other baz);
37178905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3718171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
37198905a67cSAndy Whitcroft				my ($name_len) = length($1);
37208905a67cSAndy Whitcroft
3721cf655043SAndy Whitcroft				my $ctx = $s;
3722773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
37238905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3724cf655043SAndy Whitcroft
37258905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3726c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
37278905a67cSAndy Whitcroft
3728c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
37298905a67cSAndy Whitcroft					}
37308905a67cSAndy Whitcroft				}
37318905a67cSAndy Whitcroft			}
37328905a67cSAndy Whitcroft
37339c0ca6f9SAndy Whitcroft		}
37349c0ca6f9SAndy Whitcroft
373500df344fSAndy Whitcroft#
373600df344fSAndy Whitcroft# Checks which may be anchored in the context.
373700df344fSAndy Whitcroft#
373800df344fSAndy Whitcroft
373900df344fSAndy Whitcroft# Check for switch () and associated case and default
374000df344fSAndy Whitcroft# statements should be at the same indent.
374100df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
374200df344fSAndy Whitcroft			my $err = '';
374300df344fSAndy Whitcroft			my $sep = '';
374400df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
374500df344fSAndy Whitcroft			shift(@ctx);
374600df344fSAndy Whitcroft			for my $ctx (@ctx) {
374700df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
374800df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
374900df344fSAndy Whitcroft							$indent != $cindent) {
375000df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
375100df344fSAndy Whitcroft					$sep = '';
375200df344fSAndy Whitcroft				} else {
375300df344fSAndy Whitcroft					$sep = "[...]\n";
375400df344fSAndy Whitcroft				}
375500df344fSAndy Whitcroft			}
375600df344fSAndy Whitcroft			if ($err ne '') {
3757000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3758000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3759de7d4f0eSAndy Whitcroft			}
3760de7d4f0eSAndy Whitcroft		}
3761de7d4f0eSAndy Whitcroft
3762de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3763de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
37640fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3765773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3766773647a0SAndy Whitcroft
37679c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
37688eef05ddSJoe Perches
37698eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
37708eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
37718eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
37728eef05ddSJoe Perches			}
37738eef05ddSJoe Perches
3774de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3775de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3776de7d4f0eSAndy Whitcroft
3777548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3778548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3779de7d4f0eSAndy Whitcroft
3780548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3781548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3782548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3783548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3784548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3785773647a0SAndy Whitcroft				$ctx_ln++;
3786773647a0SAndy Whitcroft			}
3787548596d5SAndy Whitcroft
378853210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
378953210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3790773647a0SAndy Whitcroft
3791773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3792000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3793000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
379401464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
379500df344fSAndy Whitcroft			}
3796773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3797773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3798773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3799773647a0SAndy Whitcroft			{
38009c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
38019c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3802000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3803000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
380401464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
38059c0ca6f9SAndy Whitcroft				}
38069c0ca6f9SAndy Whitcroft			}
380700df344fSAndy Whitcroft		}
380800df344fSAndy Whitcroft
38094d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3810f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
38113e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
38123e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
38133e469cdcSAndy Whitcroft					if (!defined $stat);
38144d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
38154d001e4dSAndy Whitcroft
38164d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
38174d001e4dSAndy Whitcroft
38189f5af480SJoe Perches			# remove inline comments
38199f5af480SJoe Perches			$s =~ s/$;/ /g;
38209f5af480SJoe Perches			$c =~ s/$;/ /g;
38214d001e4dSAndy Whitcroft
38224d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
38236f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
38246f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
38254d001e4dSAndy Whitcroft
38269f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
38279f5af480SJoe Perches			# none on the first line, and are going to readd them
38289f5af480SJoe Perches			# where necessary.
38299f5af480SJoe Perches			$s =~ s/\n./\n/gs;
38309f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
38319f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
38329f5af480SJoe Perches			}
38339f5af480SJoe Perches
38344d001e4dSAndy Whitcroft			# We want to check the first line inside the block
38354d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
38364d001e4dSAndy Whitcroft			#  1) any blank line termination
38374d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
38384d001e4dSAndy Whitcroft			#  3) any do (...) {
38394d001e4dSAndy Whitcroft			my $continuation = 0;
38404d001e4dSAndy Whitcroft			my $check = 0;
38414d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
38424d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
38434d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
38444d001e4dSAndy Whitcroft				$continuation = 1;
38454d001e4dSAndy Whitcroft			}
38469bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
38474d001e4dSAndy Whitcroft				$check = 1;
38484d001e4dSAndy Whitcroft				$cond_lines++;
38494d001e4dSAndy Whitcroft			}
38504d001e4dSAndy Whitcroft
38514d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
38524d001e4dSAndy Whitcroft			# preprocessor statement.
38534d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
38544d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
38554d001e4dSAndy Whitcroft				$check = 0;
38564d001e4dSAndy Whitcroft			}
38574d001e4dSAndy Whitcroft
38589bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3859740504c6SAndy Whitcroft			$continuation = 0;
38609bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
38619bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
38624d001e4dSAndy Whitcroft
3863f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3864f16fa28fSAndy Whitcroft				# is not linear.
3865f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3866f16fa28fSAndy Whitcroft					$check = 0;
3867f16fa28fSAndy Whitcroft				}
3868f16fa28fSAndy Whitcroft
38699bd49efeSAndy Whitcroft				# Ignore:
38709bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
38719bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
38729bd49efeSAndy Whitcroft				#  3) labels.
3873740504c6SAndy Whitcroft				if ($continuation ||
3874740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
38759bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
38769bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3877740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
387830dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
38799bd49efeSAndy Whitcroft						$cond_lines++;
38809bd49efeSAndy Whitcroft					}
38814d001e4dSAndy Whitcroft				}
388230dad6ebSAndy Whitcroft			}
38834d001e4dSAndy Whitcroft
38844d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
38854d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
38864d001e4dSAndy Whitcroft
38874d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
38884d001e4dSAndy Whitcroft			# this is not this patch's fault.
38894d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
38904d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
38914d001e4dSAndy Whitcroft				$check = 0;
38924d001e4dSAndy Whitcroft			}
38934d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
38944d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
38954d001e4dSAndy Whitcroft			}
38964d001e4dSAndy Whitcroft
38979bd49efeSAndy 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";
38984d001e4dSAndy Whitcroft
38999f5af480SJoe Perches			if ($check && $s ne '' &&
3900713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
39019f5af480SJoe Perches			     ($sindent < $indent) ||
3902f6950a73SJoe Perches			     ($sindent == $indent &&
3903f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3904713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
3905000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3906000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
39074d001e4dSAndy Whitcroft			}
39084d001e4dSAndy Whitcroft		}
39094d001e4dSAndy Whitcroft
39106c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
39116c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
39121f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
39131f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
39146c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3915c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3916c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3917cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3918cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
39191f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3920c2fdda0dSAndy Whitcroft		}
39216c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
39226c72ffaaSAndy Whitcroft
392300df344fSAndy Whitcroft#ignore lines not being added
39243705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
392500df344fSAndy Whitcroft
392699ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
392799ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
392899ca38c2SJoe Perches#	struct foo bar = *(&(bar));
392999ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
393099ca38c2SJoe Perches			my $var = $1;
393199ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
393299ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
393399ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
393499ca38c2SJoe Perches			}
393599ca38c2SJoe Perches		}
393699ca38c2SJoe Perches
393711ca40a0SJoe Perches# check for dereferences that span multiple lines
393811ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
393911ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
394011ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
394111ca40a0SJoe Perches			my $ref = $1;
394211ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
394311ca40a0SJoe Perches			$ref .= $1;
394411ca40a0SJoe Perches			$ref =~ s/\s//g;
394511ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
394611ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
394711ca40a0SJoe Perches		}
394811ca40a0SJoe Perches
3949a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3950c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3951a1ce18e4SJoe Perches			my $type = $1;
3952a1ce18e4SJoe Perches			my $var = $2;
3953207a8e84SJoe Perches			$var = "" if (!defined $var);
3954207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3955a1ce18e4SJoe Perches				my $sign = $1;
3956a1ce18e4SJoe Perches				my $pointer = $2;
3957a1ce18e4SJoe Perches
3958a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3959a1ce18e4SJoe Perches
3960a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3961a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3962a1ce18e4SJoe Perches				    $fix) {
3963a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3964207a8e84SJoe Perches					my $comp_pointer = $pointer;
3965207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3966207a8e84SJoe Perches					$decl .= $comp_pointer;
3967207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3968207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3969a1ce18e4SJoe Perches				}
3970a1ce18e4SJoe Perches			}
3971a1ce18e4SJoe Perches		}
3972a1ce18e4SJoe Perches
3973653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
39747429c690SAndy Whitcroft		if ($dbg_type) {
39757429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3976000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3977000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
39787429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3979000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3980000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
39817429c690SAndy Whitcroft			}
3982653d4876SAndy Whitcroft			next;
3983653d4876SAndy Whitcroft		}
3984a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3985a1ef277eSAndy Whitcroft		if ($dbg_attr) {
39869360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3987000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3988000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
39899360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3990000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3991000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3992a1ef277eSAndy Whitcroft			}
3993a1ef277eSAndy Whitcroft			next;
3994a1ef277eSAndy Whitcroft		}
3995653d4876SAndy Whitcroft
3996f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
399799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
399899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3999d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4000d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4001f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4002f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4003f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4004d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4005d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4006f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4007d752fcc8SJoe Perches				$fixedline = $line;
40088d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4009f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4010d752fcc8SJoe Perches			}
4011f0a594c1SAndy Whitcroft		}
4012f0a594c1SAndy Whitcroft
401300df344fSAndy Whitcroft#
401400df344fSAndy Whitcroft# Checks which are anchored on the added line.
401500df344fSAndy Whitcroft#
401600df344fSAndy Whitcroft
4017653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4018c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4019653d4876SAndy Whitcroft			my $path = $1;
4020653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4021000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4022495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4023495e9d84SJoe Perches			}
4024495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4025495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4026495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4027653d4876SAndy Whitcroft			}
4028653d4876SAndy Whitcroft		}
4029653d4876SAndy Whitcroft
403000df344fSAndy Whitcroft# no C99 // comments
403100df344fSAndy Whitcroft		if ($line =~ m{//}) {
40323705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
40333705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
40343705ce5bSJoe Perches			    $fix) {
4035194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
40363705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
40373705ce5bSJoe Perches					my $comment = trim($1);
4038194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
40393705ce5bSJoe Perches				}
40403705ce5bSJoe Perches			}
404100df344fSAndy Whitcroft		}
404200df344fSAndy Whitcroft		# Remove C99 comments.
40430a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
40446c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
40450a920b5bSAndy Whitcroft
40462b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
40472b474a1aSAndy Whitcroft# the whole statement.
40482b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
40492b474a1aSAndy Whitcroft		if (defined $realline_next &&
40502b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
40512b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
40522b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
40532b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
40543cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
40553cbf62dfSAndy Whitcroft			# a prefix:
40563cbf62dfSAndy Whitcroft			#   XXX(foo);
40573cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4058653d4876SAndy Whitcroft			my $name = $1;
405987a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
40603cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
40613cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
40623cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
40633cbf62dfSAndy Whitcroft
40643cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
40652b474a1aSAndy Whitcroft				\n.}\s*$|
406648012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
406748012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
406848012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
40692b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
40702b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
407148012058SAndy Whitcroft			    )/x) {
40722b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
40732b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
40742b474a1aSAndy Whitcroft			} else {
40752b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
40760a920b5bSAndy Whitcroft			}
40770a920b5bSAndy Whitcroft		}
40782b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
40792b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
40802b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
40812b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
40822b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
40832b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
40842b474a1aSAndy Whitcroft		}
40852b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
40862b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4087000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4088000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
40892b474a1aSAndy Whitcroft		}
40900a920b5bSAndy Whitcroft
40915150bda4SJoe Eloff# check for global initialisers.
40926d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
4093d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
40946d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4095d5e616fcSJoe Perches			    $fix) {
40966d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4097d5e616fcSJoe Perches			}
4098f0a594c1SAndy Whitcroft		}
40990a920b5bSAndy Whitcroft# check for static initialisers.
41006d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4101d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
41026d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4103d5e616fcSJoe Perches				      $herecurr) &&
4104d5e616fcSJoe Perches			    $fix) {
41056d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4106d5e616fcSJoe Perches			}
41070a920b5bSAndy Whitcroft		}
41080a920b5bSAndy Whitcroft
41091813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
41101813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
41111813087dSJoe Perches			my $tmp = trim($1);
41121813087dSJoe Perches			WARN("MISORDERED_TYPE",
41131813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
41141813087dSJoe Perches		}
41151813087dSJoe Perches
4116809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4117809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4118809e082eSJoe Perches			my $type = trim($1);
4119809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4120809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4121809e082eSJoe Perches			my $new_type = $type;
4122809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4123809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4124809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4125809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4126809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4127809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4128809e082eSJoe Perches			$new_type = trim($new_type);
4129809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4130809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4131809e082eSJoe Perches			    $fix) {
4132809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4133809e082eSJoe Perches			}
4134809e082eSJoe Perches		}
4135809e082eSJoe Perches
4136cb710ecaSJoe Perches# check for static const char * arrays.
4137cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4138000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4139000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4140cb710ecaSJoe Perches				$herecurr);
4141cb710ecaSJoe Perches		}
4142cb710ecaSJoe Perches
414377b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
414477b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
414577b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
414677b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
414777b8c0a8SJoe Perches			    $fix) {
414877b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
414977b8c0a8SJoe Perches			}
415077b8c0a8SJoe Perches		}
415177b8c0a8SJoe Perches
4152cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4153cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4154000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4155000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4156cb710ecaSJoe Perches				$herecurr);
4157cb710ecaSJoe Perches		}
4158cb710ecaSJoe Perches
4159ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4160ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4161ab7e23f3SJoe Perches			my $found = $1;
4162ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4163ab7e23f3SJoe Perches				WARN("CONST_CONST",
4164ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4165ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4166ab7e23f3SJoe Perches				WARN("CONST_CONST",
4167ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4168ab7e23f3SJoe Perches			}
4169ab7e23f3SJoe Perches		}
4170ab7e23f3SJoe Perches
41719b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
41729b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
41739b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
41749b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
41759b0fa60dSJoe Perches				$herecurr);
41769b0fa60dSJoe Perches               }
41779b0fa60dSJoe Perches
4178b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4179b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4180b598b670SJoe Perches			my $array = $1;
4181b598b670SJoe 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*\))@) {
4182b598b670SJoe Perches				my $array_div = $1;
4183b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4184b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4185b598b670SJoe Perches				    $fix) {
4186b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4187b598b670SJoe Perches				}
4188b598b670SJoe Perches			}
4189b598b670SJoe Perches		}
4190b598b670SJoe Perches
4191b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
419216b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4193b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4194b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4195b36190c5SJoe Perches			    $fix) {
4196194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4197b36190c5SJoe Perches			}
4198b36190c5SJoe Perches		}
4199b36190c5SJoe Perches
4200653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4201653d4876SAndy Whitcroft# make sense.
4202653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
42038054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4204c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
42058ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
420646d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4207000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4208000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
42090a920b5bSAndy Whitcroft		}
42100a920b5bSAndy Whitcroft
42110a920b5bSAndy Whitcroft# * goes on variable not on type
421265863862SAndy Whitcroft		# (char*[ const])
4213bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4214bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
42153705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4216d8aaf121SAndy Whitcroft
421765863862SAndy Whitcroft			# Should start with a space.
421865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
421965863862SAndy Whitcroft			# Should not end with a space.
422065863862SAndy Whitcroft			$to =~ s/\s+$//;
422165863862SAndy Whitcroft			# '*'s should not have spaces between.
4222f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
422365863862SAndy Whitcroft			}
4224d8aaf121SAndy Whitcroft
42253705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
422665863862SAndy Whitcroft			if ($from ne $to) {
42273705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
42283705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
42293705ce5bSJoe Perches				    $fix) {
42303705ce5bSJoe Perches					my $sub_from = $ident;
42313705ce5bSJoe Perches					my $sub_to = $ident;
42323705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4233194f66fcSJoe Perches					$fixed[$fixlinenr] =~
42343705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
42353705ce5bSJoe Perches				}
423665863862SAndy Whitcroft			}
4237bfcb2cc7SAndy Whitcroft		}
4238bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4239bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
42403705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4241d8aaf121SAndy Whitcroft
424265863862SAndy Whitcroft			# Should start with a space.
424365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
424465863862SAndy Whitcroft			# Should not end with a space.
424565863862SAndy Whitcroft			$to =~ s/\s+$//;
424665863862SAndy Whitcroft			# '*'s should not have spaces between.
4247f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
424865863862SAndy Whitcroft			}
424965863862SAndy Whitcroft			# Modifiers should have spaces.
425065863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
425165863862SAndy Whitcroft
42523705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4253667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
42543705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
42553705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
42563705ce5bSJoe Perches				    $fix) {
42573705ce5bSJoe Perches
42583705ce5bSJoe Perches					my $sub_from = $match;
42593705ce5bSJoe Perches					my $sub_to = $match;
42603705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4261194f66fcSJoe Perches					$fixed[$fixlinenr] =~
42623705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
42633705ce5bSJoe Perches				}
426465863862SAndy Whitcroft			}
42650a920b5bSAndy Whitcroft		}
42660a920b5bSAndy Whitcroft
42679d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
42689d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
42690675a8fbSJean Delvare			my $msg_level = \&WARN;
42700675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
42710675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
42729d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
42739d3e3c70SJoe Perches		}
42740a920b5bSAndy Whitcroft
42759d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
42768905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4277000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4278000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
42798905a67cSAndy Whitcroft		}
42808905a67cSAndy Whitcroft
428117441227SJoe Perches# check for uses of printk_ratelimit
428217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4283000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4284000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
428517441227SJoe Perches		}
428617441227SJoe Perches
4287eeef5733SJoe Perches# printk should use KERN_* levels
4288eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4289000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4290eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
429100df344fSAndy Whitcroft		}
42920a920b5bSAndy Whitcroft
4293243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4294243f3803SJoe Perches			my $orig = $1;
4295243f3803SJoe Perches			my $level = lc($orig);
4296243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
42978f26b837SJoe Perches			my $level2 = $level;
42988f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4299243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4300daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4301243f3803SJoe Perches		}
4302243f3803SJoe Perches
4303dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4304dc139313SJoe Perches			my $orig = $1;
4305dc139313SJoe Perches			my $level = lc($orig);
4306dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4307dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4308dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4309dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4310dc139313SJoe Perches		}
4311dc139313SJoe Perches
43128020b253SNicolas Boichat# trace_printk should not be used in production code.
43138020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
43148020b253SNicolas Boichat			WARN("TRACE_PRINTK",
43158020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
43168020b253SNicolas Boichat		}
43178020b253SNicolas Boichat
431891c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
431991c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
432091c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
432191c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
432291c9afafSAndy Lutomirski			WARN("ENOSYS",
432391c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
432491c9afafSAndy Lutomirski		}
432591c9afafSAndy Lutomirski
43266b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
43276b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
43286b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
43296b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
43306b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
43316b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
43326b9ea5ffSJakub Kicinski			    $fix) {
43336b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
43346b9ea5ffSJakub Kicinski			}
43356b9ea5ffSJakub Kicinski		}
43366b9ea5ffSJakub Kicinski
4337653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4338653d4876SAndy Whitcroft# or if closed on same line
43395b57980dSJoe Perches		if ($perl_version_ok &&
43402d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
43412d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
43422d453e3bSJoe Perches		    $sline !~ /}/) {
43438d182478SJoe Perches			if (ERROR("OPEN_BRACE",
43442d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
43458d182478SJoe Perches			    $fix) {
43468d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
43478d182478SJoe Perches				my $fixed_line = $rawline;
43488d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
43498d182478SJoe Perches				my $line1 = $1;
43508d182478SJoe Perches				my $line2 = $2;
43518d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
43528d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
43538d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
43548d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
43558d182478SJoe Perches				}
43568d182478SJoe Perches			}
43570a920b5bSAndy Whitcroft		}
4358653d4876SAndy Whitcroft
43598905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
43608905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
43618905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
43628d182478SJoe Perches			if (ERROR("OPEN_BRACE",
43638d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
43648d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
43658d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
43668d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
43678d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
43688d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
43698d182478SJoe Perches				$fixedline = $rawline;
43708d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
43718d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
43728d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
43738d182478SJoe Perches				}
43748d182478SJoe Perches			}
43758905a67cSAndy Whitcroft		}
43768905a67cSAndy Whitcroft
43770c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
43783705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
43793705ce5bSJoe Perches			if (WARN("SPACING",
43803705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
43813705ce5bSJoe Perches			    $fix) {
4382194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43833705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
43843705ce5bSJoe Perches			}
43850c73b4ebSAndy Whitcroft		}
43860c73b4ebSAndy Whitcroft
438731070b5dSJoe Perches# Function pointer declarations
438831070b5dSJoe Perches# check spacing between type, funcptr, and args
438931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
439091f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
439131070b5dSJoe Perches			my $declare = $1;
439231070b5dSJoe Perches			my $pre_pointer_space = $2;
439331070b5dSJoe Perches			my $post_pointer_space = $3;
439431070b5dSJoe Perches			my $funcname = $4;
439531070b5dSJoe Perches			my $post_funcname_space = $5;
439631070b5dSJoe Perches			my $pre_args_space = $6;
439731070b5dSJoe Perches
439891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
439991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
440091f72e9cSJoe Perches# don't need a space so don't warn for those.
440191f72e9cSJoe Perches			my $post_declare_space = "";
440291f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
440391f72e9cSJoe Perches				$post_declare_space = $1;
440491f72e9cSJoe Perches				$declare = rtrim($declare);
440591f72e9cSJoe Perches			}
440691f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
440731070b5dSJoe Perches				WARN("SPACING",
440831070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
440991f72e9cSJoe Perches				$post_declare_space = " ";
441031070b5dSJoe Perches			}
441131070b5dSJoe Perches
441231070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
441391f72e9cSJoe Perches# This test is not currently implemented because these declarations are
441491f72e9cSJoe Perches# equivalent to
441591f72e9cSJoe Perches#	int  foo(int bar, ...)
441691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
441791f72e9cSJoe Perches#
441891f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
441991f72e9cSJoe Perches#				WARN("SPACING",
442091f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
442191f72e9cSJoe Perches#			}
442231070b5dSJoe Perches
442331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
442431070b5dSJoe Perches			if (defined $pre_pointer_space &&
442531070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
442631070b5dSJoe Perches				WARN("SPACING",
442731070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
442831070b5dSJoe Perches			}
442931070b5dSJoe Perches
443031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
443131070b5dSJoe Perches			if (defined $post_pointer_space &&
443231070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
443331070b5dSJoe Perches				WARN("SPACING",
443431070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
443531070b5dSJoe Perches			}
443631070b5dSJoe Perches
443731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
443831070b5dSJoe Perches			if (defined $post_funcname_space &&
443931070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
444031070b5dSJoe Perches				WARN("SPACING",
444131070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
444231070b5dSJoe Perches			}
444331070b5dSJoe Perches
444431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
444531070b5dSJoe Perches			if (defined $pre_args_space &&
444631070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
444731070b5dSJoe Perches				WARN("SPACING",
444831070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
444931070b5dSJoe Perches			}
445031070b5dSJoe Perches
445131070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4452194f66fcSJoe Perches				$fixed[$fixlinenr] =~
445391f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
445431070b5dSJoe Perches			}
445531070b5dSJoe Perches		}
445631070b5dSJoe Perches
44578d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
44588d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4459fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4460fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
44618d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
44628d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
44638d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4464fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
446538dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
44663705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
44673705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
44683705ce5bSJoe Perches				    $fix) {
4469194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
44703705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
44713705ce5bSJoe Perches				}
44728d31cfceSAndy Whitcroft			}
44738d31cfceSAndy Whitcroft		}
44748d31cfceSAndy Whitcroft
4475f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
44766c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4477c2fdda0dSAndy Whitcroft			my $name = $1;
4478773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4479773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4480c2fdda0dSAndy Whitcroft
4481c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4482773647a0SAndy Whitcroft			if ($name =~ /^(?:
4483773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4484773647a0SAndy Whitcroft				volatile|__volatile__|
4485773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4486773647a0SAndy Whitcroft				asm|__asm__)$/x)
4487773647a0SAndy Whitcroft			{
4488c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4489c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4490c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4491c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4492773647a0SAndy Whitcroft
4493773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4494c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4495c2fdda0dSAndy Whitcroft
4496c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4497c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4498773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4499c2fdda0dSAndy Whitcroft
4500c2fdda0dSAndy Whitcroft			} else {
45013705ce5bSJoe Perches				if (WARN("SPACING",
45023705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
45033705ce5bSJoe Perches					     $fix) {
4504194f66fcSJoe Perches					$fixed[$fixlinenr] =~
45053705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
45063705ce5bSJoe Perches				}
4507f0a594c1SAndy Whitcroft			}
45086c72ffaaSAndy Whitcroft		}
45099a4cad4eSEric Nelson
4510653d4876SAndy Whitcroft# Check operator spacing.
45110a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
45123705ce5bSJoe Perches			my $fixed_line = "";
45133705ce5bSJoe Perches			my $line_fixed = 0;
45143705ce5bSJoe Perches
45159c0ca6f9SAndy Whitcroft			my $ops = qr{
45169c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
45179c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
45189c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
45191f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
452084731623SJoe Perches				\?:|\?|:
45219c0ca6f9SAndy Whitcroft			}x;
4522cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
45233705ce5bSJoe Perches
45243705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
45253705ce5bSJoe Perches##			foreach my $el (@elements) {
45263705ce5bSJoe Perches##				print("el: <$el>\n");
45273705ce5bSJoe Perches##			}
45283705ce5bSJoe Perches
45293705ce5bSJoe Perches			my @fix_elements = ();
453000df344fSAndy Whitcroft			my $off = 0;
45316c72ffaaSAndy Whitcroft
45323705ce5bSJoe Perches			foreach my $el (@elements) {
45333705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
45343705ce5bSJoe Perches				$off += length($el);
45353705ce5bSJoe Perches			}
45363705ce5bSJoe Perches
45373705ce5bSJoe Perches			$off = 0;
45383705ce5bSJoe Perches
45396c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4540b34c648bSJoe Perches			my $last_after = -1;
45416c72ffaaSAndy Whitcroft
45420a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
45433705ce5bSJoe Perches
45443705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
45453705ce5bSJoe Perches
45463705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
45473705ce5bSJoe Perches
45484a0df2efSAndy Whitcroft				$off += length($elements[$n]);
45494a0df2efSAndy Whitcroft
455025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4551773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4552773647a0SAndy Whitcroft				my $cc = '';
4553773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4554773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4555773647a0SAndy Whitcroft				}
4556773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4557773647a0SAndy Whitcroft
45584a0df2efSAndy Whitcroft				my $a = '';
45594a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
45604a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4561cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
45624a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
45634a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4564773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
45654a0df2efSAndy Whitcroft
45660a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
45674a0df2efSAndy Whitcroft
45684a0df2efSAndy Whitcroft				my $c = '';
45690a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
45704a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
45714a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4572cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
45734a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
45744a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
45758b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
45764a0df2efSAndy Whitcroft				} else {
45774a0df2efSAndy Whitcroft					$c = 'E';
45780a920b5bSAndy Whitcroft				}
45790a920b5bSAndy Whitcroft
45804a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
45814a0df2efSAndy Whitcroft
45824a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
45834a0df2efSAndy Whitcroft
45846c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4585de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
45860a920b5bSAndy Whitcroft
458774048ed8SAndy Whitcroft				# Pull out the value of this operator.
45886c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
45890a920b5bSAndy Whitcroft
45901f65f947SAndy Whitcroft				# Get the full operator variant.
45911f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
45921f65f947SAndy Whitcroft
459313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
459413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4595d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
459613214adfSAndy Whitcroft
4597cf655043SAndy Whitcroft#				# Ignore comments
4598cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
459913214adfSAndy Whitcroft
4600d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
460113214adfSAndy Whitcroft				} elsif ($op eq ';') {
4602cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4603cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
46043705ce5bSJoe Perches						if (ERROR("SPACING",
46053705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4606b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
46073705ce5bSJoe Perches							$line_fixed = 1;
46083705ce5bSJoe Perches						}
4609d8aaf121SAndy Whitcroft					}
4610d8aaf121SAndy Whitcroft
4611d8aaf121SAndy Whitcroft				# // is a comment
4612d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
46130a920b5bSAndy Whitcroft
4614b00e4814SJoe Perches				#   :   when part of a bitfield
4615b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4616b00e4814SJoe Perches					# skip the bitfield test for now
4617b00e4814SJoe Perches
46181f65f947SAndy Whitcroft				# No spaces for:
46191f65f947SAndy Whitcroft				#   ->
4620b00e4814SJoe Perches				} elsif ($op eq '->') {
46214a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
46223705ce5bSJoe Perches						if (ERROR("SPACING",
46233705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4624b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
46253705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
46263705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
46273705ce5bSJoe Perches							}
4628b34c648bSJoe Perches							$line_fixed = 1;
46293705ce5bSJoe Perches						}
46300a920b5bSAndy Whitcroft					}
46310a920b5bSAndy Whitcroft
46322381097bSJoe Perches				# , must not have a space before and must have a space on the right.
46330a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
46342381097bSJoe Perches					my $rtrim_before = 0;
46352381097bSJoe Perches					my $space_after = 0;
46362381097bSJoe Perches					if ($ctx =~ /Wx./) {
46372381097bSJoe Perches						if (ERROR("SPACING",
46382381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
46392381097bSJoe Perches							$line_fixed = 1;
46402381097bSJoe Perches							$rtrim_before = 1;
46412381097bSJoe Perches						}
46422381097bSJoe Perches					}
4643cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
46443705ce5bSJoe Perches						if (ERROR("SPACING",
46453705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
46463705ce5bSJoe Perches							$line_fixed = 1;
4647b34c648bSJoe Perches							$last_after = $n;
46482381097bSJoe Perches							$space_after = 1;
46492381097bSJoe Perches						}
46502381097bSJoe Perches					}
46512381097bSJoe Perches					if ($rtrim_before || $space_after) {
46522381097bSJoe Perches						if ($rtrim_before) {
46532381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
46542381097bSJoe Perches						} else {
46552381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
46562381097bSJoe Perches						}
46572381097bSJoe Perches						if ($space_after) {
46582381097bSJoe Perches							$good .= " ";
46593705ce5bSJoe Perches						}
46600a920b5bSAndy Whitcroft					}
46610a920b5bSAndy Whitcroft
46629c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
466374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
46649c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
46659c0ca6f9SAndy Whitcroft
46669c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
46679c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
46689c0ca6f9SAndy Whitcroft				# unary operator, or a cast
46699c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
467074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
46710d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4672cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
46733705ce5bSJoe Perches						if (ERROR("SPACING",
46743705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4675b34c648bSJoe Perches							if ($n != $last_after + 2) {
4676b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
46773705ce5bSJoe Perches								$line_fixed = 1;
46783705ce5bSJoe Perches							}
46790a920b5bSAndy Whitcroft						}
4680b34c648bSJoe Perches					}
4681a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4682171ae1a4SAndy Whitcroft						# A unary '*' may be const
4683171ae1a4SAndy Whitcroft
4684171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
46853705ce5bSJoe Perches						if (ERROR("SPACING",
46863705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4687b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
46883705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
46893705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
46903705ce5bSJoe Perches							}
4691b34c648bSJoe Perches							$line_fixed = 1;
46923705ce5bSJoe Perches						}
46930a920b5bSAndy Whitcroft					}
46940a920b5bSAndy Whitcroft
46950a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
46960a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4697773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
46983705ce5bSJoe Perches						if (ERROR("SPACING",
46993705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4700b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
47013705ce5bSJoe Perches							$line_fixed = 1;
47023705ce5bSJoe Perches						}
47030a920b5bSAndy Whitcroft					}
4704773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4705773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
47063705ce5bSJoe Perches						if (ERROR("SPACING",
47073705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4708b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
47093705ce5bSJoe Perches							$line_fixed = 1;
47103705ce5bSJoe Perches						}
4711653d4876SAndy Whitcroft					}
4712773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
47133705ce5bSJoe Perches						if (ERROR("SPACING",
47143705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4715b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
47163705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
47173705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4718773647a0SAndy Whitcroft							}
4719b34c648bSJoe Perches							$line_fixed = 1;
47203705ce5bSJoe Perches						}
47213705ce5bSJoe Perches					}
47220a920b5bSAndy Whitcroft
47230a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
47249c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
47259c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
47269c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4727c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4728c2fdda0dSAndy Whitcroft					 $op eq '%')
47290a920b5bSAndy Whitcroft				{
4730d2e025f3SJoe Perches					if ($check) {
4731d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4732d2e025f3SJoe Perches							if (CHK("SPACING",
4733d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4734d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4735d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4736d2e025f3SJoe Perches								$line_fixed = 1;
4737d2e025f3SJoe Perches							}
4738d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4739d2e025f3SJoe Perches							if (CHK("SPACING",
4740d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4741d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4742d2e025f3SJoe Perches								$line_fixed = 1;
4743d2e025f3SJoe Perches							}
4744d2e025f3SJoe Perches						}
4745d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
47463705ce5bSJoe Perches						if (ERROR("SPACING",
47473705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4748b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4749b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4750b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4751b34c648bSJoe Perches							}
47523705ce5bSJoe Perches							$line_fixed = 1;
47533705ce5bSJoe Perches						}
47540a920b5bSAndy Whitcroft					}
47550a920b5bSAndy Whitcroft
47561f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
47571f65f947SAndy Whitcroft				# terminating a case value or a label.
47581f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
47591f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
47603705ce5bSJoe Perches						if (ERROR("SPACING",
47613705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4762b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
47633705ce5bSJoe Perches							$line_fixed = 1;
47643705ce5bSJoe Perches						}
47651f65f947SAndy Whitcroft					}
47661f65f947SAndy Whitcroft
47670a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4768cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
47691f65f947SAndy Whitcroft					my $ok = 0;
47701f65f947SAndy Whitcroft
477122f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
47721f65f947SAndy Whitcroft					if (($op eq '<' &&
47731f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
47741f65f947SAndy Whitcroft					    ($op eq '>' &&
47751f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
47761f65f947SAndy Whitcroft					{
47771f65f947SAndy Whitcroft						$ok = 1;
47781f65f947SAndy Whitcroft					}
47791f65f947SAndy Whitcroft
4780e0df7e1fSJoe Perches					# for asm volatile statements
4781e0df7e1fSJoe Perches					# ignore a colon with another
4782e0df7e1fSJoe Perches					# colon immediately before or after
4783e0df7e1fSJoe Perches					if (($op eq ':') &&
4784e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4785e0df7e1fSJoe Perches						$ok = 1;
4786e0df7e1fSJoe Perches					}
4787e0df7e1fSJoe Perches
478884731623SJoe Perches					# messages are ERROR, but ?: are CHK
47891f65f947SAndy Whitcroft					if ($ok == 0) {
47900675a8fbSJean Delvare						my $msg_level = \&ERROR;
47910675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
479284731623SJoe Perches
47930675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
47943705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4795b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4796b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4797b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4798b34c648bSJoe Perches							}
47993705ce5bSJoe Perches							$line_fixed = 1;
48003705ce5bSJoe Perches						}
48010a920b5bSAndy Whitcroft					}
480222f2a2efSAndy Whitcroft				}
48034a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
48043705ce5bSJoe Perches
48053705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
48063705ce5bSJoe Perches
48073705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
48080a920b5bSAndy Whitcroft			}
48093705ce5bSJoe Perches
48103705ce5bSJoe Perches			if (($#elements % 2) == 0) {
48113705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
48123705ce5bSJoe Perches			}
48133705ce5bSJoe Perches
4814194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4815194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
48163705ce5bSJoe Perches			}
48173705ce5bSJoe Perches
48183705ce5bSJoe Perches
48190a920b5bSAndy Whitcroft		}
48200a920b5bSAndy Whitcroft
4821786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4822d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4823786b6326SJoe Perches			if (WARN("SPACING",
4824786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4825786b6326SJoe Perches			    $fix) {
4826194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4827786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4828786b6326SJoe Perches			}
4829786b6326SJoe Perches		}
4830786b6326SJoe Perches
4831f0a594c1SAndy Whitcroft# check for multiple assignments
4832f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4833000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4834000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4835f0a594c1SAndy Whitcroft		}
4836f0a594c1SAndy Whitcroft
483722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
483822f2a2efSAndy Whitcroft## # continuation.
483922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
484022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
484122f2a2efSAndy Whitcroft##
484222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
484322f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
484422f2a2efSAndy Whitcroft## 			my $ln = $line;
484522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
484622f2a2efSAndy Whitcroft## 			}
484722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4848000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4849000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
485022f2a2efSAndy Whitcroft## 			}
485122f2a2efSAndy Whitcroft## 		}
4852f0a594c1SAndy Whitcroft
48530a920b5bSAndy Whitcroft#need space before brace following if, while, etc
48546b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
48556ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
48563705ce5bSJoe Perches			if (ERROR("SPACING",
48573705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
48583705ce5bSJoe Perches			    $fix) {
48596ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
48603705ce5bSJoe Perches			}
4861de7d4f0eSAndy Whitcroft		}
4862de7d4f0eSAndy Whitcroft
4863c4a62ef9SJoe Perches## # check for blank lines before declarations
4864c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4865c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4866c4a62ef9SJoe Perches##			WARN("SPACING",
4867c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4868c4a62ef9SJoe Perches##		}
4869c4a62ef9SJoe Perches##
4870c4a62ef9SJoe Perches
4871de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4872de7d4f0eSAndy Whitcroft# on the line
487394fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4874d5e616fcSJoe Perches			if (ERROR("SPACING",
4875d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4876d5e616fcSJoe Perches			    $fix) {
4877194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4878d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4879d5e616fcSJoe Perches			}
48800a920b5bSAndy Whitcroft		}
48810a920b5bSAndy Whitcroft
488222f2a2efSAndy Whitcroft# check spacing on square brackets
488322f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
48843705ce5bSJoe Perches			if (ERROR("SPACING",
48853705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
48863705ce5bSJoe Perches			    $fix) {
4887194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48883705ce5bSJoe Perches				    s/\[\s+/\[/;
48893705ce5bSJoe Perches			}
489022f2a2efSAndy Whitcroft		}
489122f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
48923705ce5bSJoe Perches			if (ERROR("SPACING",
48933705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
48943705ce5bSJoe Perches			    $fix) {
4895194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48963705ce5bSJoe Perches				    s/\s+\]/\]/;
48973705ce5bSJoe Perches			}
489822f2a2efSAndy Whitcroft		}
489922f2a2efSAndy Whitcroft
4900c45dcabdSAndy Whitcroft# check spacing on parentheses
49019c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
49029c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
49033705ce5bSJoe Perches			if (ERROR("SPACING",
49043705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
49053705ce5bSJoe Perches			    $fix) {
4906194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49073705ce5bSJoe Perches				    s/\(\s+/\(/;
49083705ce5bSJoe Perches			}
490922f2a2efSAndy Whitcroft		}
491013214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4911c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4912c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
49133705ce5bSJoe Perches			if (ERROR("SPACING",
49143705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
49153705ce5bSJoe Perches			    $fix) {
4916194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49173705ce5bSJoe Perches				    s/\s+\)/\)/;
49183705ce5bSJoe Perches			}
491922f2a2efSAndy Whitcroft		}
492022f2a2efSAndy Whitcroft
4921e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4922e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4923e2826fd0SJoe Perches
4924e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4925ea4acbb1SJoe Perches			my $var = $1;
4926ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4927ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4928ea4acbb1SJoe Perches			    $fix) {
4929ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4930ea4acbb1SJoe Perches			}
4931ea4acbb1SJoe Perches		}
4932ea4acbb1SJoe Perches
4933ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4934ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4935ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4936ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4937ea4acbb1SJoe Perches			my $var = $2;
4938ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4939ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4940ea4acbb1SJoe Perches			    $fix) {
4941ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4942ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4943ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4944ea4acbb1SJoe Perches			}
4945e2826fd0SJoe Perches		}
4946e2826fd0SJoe Perches
494763b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4948a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4949a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
49505b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
495163b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
495263b7c73eSJoe Perches			my $if_stat = $1;
495363b7c73eSJoe Perches			my $test = substr($2, 1, -1);
495463b7c73eSJoe Perches			my $herectx;
495563b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
495663b7c73eSJoe Perches				my $match = $1;
495763b7c73eSJoe Perches				# avoid parentheses around potential macro args
495863b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
495963b7c73eSJoe Perches				if (!defined($herectx)) {
496063b7c73eSJoe Perches					$herectx = $here . "\n";
496163b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
496263b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
496363b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
496463b7c73eSJoe Perches						$herectx .=  $rl . "\n";
496563b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
496663b7c73eSJoe Perches					}
496763b7c73eSJoe Perches				}
496863b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
496963b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
497063b7c73eSJoe Perches			}
497163b7c73eSJoe Perches		}
497263b7c73eSJoe Perches
49730a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
49744a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
49750a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
49763705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
49773705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
49783705ce5bSJoe Perches			    $fix) {
4979194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49803705ce5bSJoe Perches				    s/^(.)\s+/$1/;
49813705ce5bSJoe Perches			}
49820a920b5bSAndy Whitcroft		}
49830a920b5bSAndy Whitcroft
498440873abaSJoe Perches# check if a statement with a comma should be two statements like:
498540873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
498640873abaSJoe Perches#	bar = baz();
498740873abaSJoe Perches		if (defined($stat) &&
498840873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
498940873abaSJoe Perches			my $cnt = statement_rawlines($stat);
499040873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
499140873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
499240873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
499340873abaSJoe Perches		}
499440873abaSJoe Perches
49955b9553abSJoe Perches# return is not a function
4996507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4997c45dcabdSAndy Whitcroft			my $spacing = $1;
49985b57980dSJoe Perches			if ($perl_version_ok &&
49995b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
50005b9553abSJoe Perches				my $value = $1;
50015b9553abSJoe Perches				$value = deparenthesize($value);
50025b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5003000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5004000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
50055b9553abSJoe Perches				}
5006c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5007000d1cc1SJoe Perches				ERROR("SPACING",
5008000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5009c45dcabdSAndy Whitcroft			}
5010c45dcabdSAndy Whitcroft		}
5011507e5141SJoe Perches
5012b43ae21bSJoe Perches# unnecessary return in a void function
5013b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5014b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5015b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5016b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5017b43ae21bSJoe Perches		    $linenr >= 3 &&
5018b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5019b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
50209819cf25SJoe Perches			WARN("RETURN_VOID",
5021b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
50229819cf25SJoe Perches               }
50239819cf25SJoe Perches
5024189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
50255b57980dSJoe Perches		if ($perl_version_ok &&
5026189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5027189248d8SJoe Perches			my $openparens = $1;
5028189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5029189248d8SJoe Perches			my $msg = "";
5030189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5031189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5032189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5033189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5034189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5035189248d8SJoe Perches			}
5036189248d8SJoe Perches		}
5037189248d8SJoe Perches
5038c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5039c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5040c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5041c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
50425b57980dSJoe Perches		if ($perl_version_ok &&
5043c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5044c5595fa2SJoe Perches			my $lead = $1;
5045c5595fa2SJoe Perches			my $const = $2;
5046c5595fa2SJoe Perches			my $comp = $3;
5047c5595fa2SJoe Perches			my $to = $4;
5048c5595fa2SJoe Perches			my $newcomp = $comp;
5049f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5050c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5051c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5052c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5053c5595fa2SJoe Perches			    $fix) {
5054c5595fa2SJoe Perches				if ($comp eq "<") {
5055c5595fa2SJoe Perches					$newcomp = ">";
5056c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5057c5595fa2SJoe Perches					$newcomp = ">=";
5058c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5059c5595fa2SJoe Perches					$newcomp = "<";
5060c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5061c5595fa2SJoe Perches					$newcomp = "<=";
5062c5595fa2SJoe Perches				}
5063c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5064c5595fa2SJoe Perches			}
5065c5595fa2SJoe Perches		}
5066c5595fa2SJoe Perches
5067f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5068f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
506953a3c448SAndy Whitcroft			my $name = $1;
507053a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
5071000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5072f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
507353a3c448SAndy Whitcroft			}
507453a3c448SAndy Whitcroft		}
5075c45dcabdSAndy Whitcroft
50760a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
50774a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
50783705ce5bSJoe Perches			if (ERROR("SPACING",
50793705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
50803705ce5bSJoe Perches			    $fix) {
5081194f66fcSJoe Perches				$fixed[$fixlinenr] =~
50823705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
50833705ce5bSJoe Perches			}
50840a920b5bSAndy Whitcroft		}
50850a920b5bSAndy Whitcroft
5086f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5087f5fe35ddSAndy Whitcroft# statements after the conditional.
5088170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
50893e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
50903e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
50913e469cdcSAndy Whitcroft					if (!defined $stat);
5092170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5093170d3a22SAndy Whitcroft						$remain_next, $off_next);
5094170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5095170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5096170d3a22SAndy Whitcroft
5097170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5098170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5099170d3a22SAndy Whitcroft				# then count those as offsets.
5100170d3a22SAndy Whitcroft				my ($whitespace) =
5101170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5102170d3a22SAndy Whitcroft				my $offset =
5103170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5104170d3a22SAndy Whitcroft
5105170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5106170d3a22SAndy Whitcroft								$offset} = 1;
5107170d3a22SAndy Whitcroft			}
5108170d3a22SAndy Whitcroft		}
5109170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5110c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5111170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5112171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
51138905a67cSAndy Whitcroft
5114b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
511565b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
511665b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
511765b64b3bSJoe Perches				    $fix && $perl_version_ok) {
511865b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
511965b64b3bSJoe Perches						my $space = $1;
512065b64b3bSJoe Perches						my $not = $2;
512165b64b3bSJoe Perches						my $statement = $3;
512265b64b3bSJoe Perches						my $assigned = $4;
512365b64b3bSJoe Perches						my $test = $8;
512465b64b3bSJoe Perches						my $against = $9;
512565b64b3bSJoe Perches						my $brace = $15;
512665b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
512765b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
512865b64b3bSJoe Perches						my $newline = "${space}if (";
512965b64b3bSJoe Perches						$newline .= '!' if defined($not);
513065b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
513165b64b3bSJoe Perches						$newline .= "$assigned";
513265b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
513365b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
513465b64b3bSJoe Perches						$newline .= ')';
513565b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
513665b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
513765b64b3bSJoe Perches					}
513865b64b3bSJoe Perches				}
51398905a67cSAndy Whitcroft			}
51408905a67cSAndy Whitcroft
51418905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
51428905a67cSAndy Whitcroft			# conditional.
5143773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
51448905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
514513214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
514653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
514753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5148773647a0SAndy Whitcroft			{
5149bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5150bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5151bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
515242bdf74cSHidetoshi Seto				my $stat_real = '';
5153bb44ad39SAndy Whitcroft
515442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
515542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5156bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5157bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5158bb44ad39SAndy Whitcroft				}
5159bb44ad39SAndy Whitcroft
5160000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5161000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
51628905a67cSAndy Whitcroft			}
51638905a67cSAndy Whitcroft		}
51648905a67cSAndy Whitcroft
516513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
516613214adfSAndy Whitcroft		if ($line =~ /
516713214adfSAndy Whitcroft			(?:
516813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
516913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
517013214adfSAndy Whitcroft				(?:\&\&|\|\|)
517113214adfSAndy Whitcroft			|
517213214adfSAndy Whitcroft				(?:\&\&|\|\|)
517313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
517413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
517513214adfSAndy Whitcroft			)/x)
517613214adfSAndy Whitcroft		{
5177000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5178000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
517913214adfSAndy Whitcroft		}
518013214adfSAndy Whitcroft
51818905a67cSAndy Whitcroft# if and else should not have general statements after it
518213214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
518313214adfSAndy Whitcroft			my $s = $1;
518413214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
518513214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5186000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5187000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
51880a920b5bSAndy Whitcroft			}
518913214adfSAndy Whitcroft		}
519039667782SAndy Whitcroft# if should not continue a brace
519139667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5192000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5193048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
519439667782SAndy Whitcroft				$herecurr);
519539667782SAndy Whitcroft		}
5196a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5197a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5198a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
51993fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5200a1080bf8SAndy Whitcroft			\s*return\s+
5201a1080bf8SAndy Whitcroft		    )/xg)
5202a1080bf8SAndy Whitcroft		{
5203000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5204000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5205a1080bf8SAndy Whitcroft		}
52060a920b5bSAndy Whitcroft
52070a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
52080a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
52098b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
52100a920b5bSAndy Whitcroft		    $previndent == $indent) {
52118b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
52128b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
52138b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
52148b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
52158b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
52168b8856f4SJoe Perches				my $fixedline = $prevrawline;
52178b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
52188b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
52198b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
52208b8856f4SJoe Perches				}
52218b8856f4SJoe Perches				$fixedline = $rawline;
52228b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
52238b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
52248b8856f4SJoe Perches			}
52250a920b5bSAndy Whitcroft		}
52260a920b5bSAndy Whitcroft
52278b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5228c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5229c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5230c2fdda0dSAndy Whitcroft
5231c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5232c2fdda0dSAndy Whitcroft			# conditional.
5233773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5234c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5235c2fdda0dSAndy Whitcroft
5236c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
52378b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
52388b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
52398b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
52408b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
52418b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
52428b8856f4SJoe Perches					my $fixedline = $prevrawline;
52438b8856f4SJoe Perches					my $trailing = $rawline;
52448b8856f4SJoe Perches					$trailing =~ s/^\+//;
52458b8856f4SJoe Perches					$trailing = trim($trailing);
52468b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
52478b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
52488b8856f4SJoe Perches				}
5249c2fdda0dSAndy Whitcroft			}
5250c2fdda0dSAndy Whitcroft		}
5251c2fdda0dSAndy Whitcroft
525295e2c602SJoe Perches#Specific variable tests
5253323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5254323c1260SJoe Perches			my $var = $1;
525595e2c602SJoe Perches
525695e2c602SJoe Perches#CamelCase
5257807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5258be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
525922735ce8SJoe Perches#Ignore Page<foo> variants
5260807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5261d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5262d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5263d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5264f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5265f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
52667e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
52677e781f67SJoe Perches					my $word = $1;
52687e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5269d8b07710SJoe Perches					if ($check) {
5270d8b07710SJoe Perches						seed_camelcase_includes();
5271d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5272d8b07710SJoe Perches							seed_camelcase_file($realfile);
5273d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5274d8b07710SJoe Perches						}
5275d8b07710SJoe Perches					}
52767e781f67SJoe Perches					if (!defined $camelcase{$word}) {
52777e781f67SJoe Perches						$camelcase{$word} = 1;
5278be79794bSJoe Perches						CHK("CAMELCASE",
52797e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
52807e781f67SJoe Perches					}
5281323c1260SJoe Perches				}
5282323c1260SJoe Perches			}
52833445686aSJoe Perches		}
52840a920b5bSAndy Whitcroft
52850a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5286d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5287d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5288d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5289d5e616fcSJoe Perches			    $fix) {
5290194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5291d5e616fcSJoe Perches			}
52920a920b5bSAndy Whitcroft		}
52930a920b5bSAndy Whitcroft
52940e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
52950e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5296c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5297e09dec48SAndy Whitcroft			my $file = "$1.h";
5298e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5299e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5300e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
53017840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5302c45dcabdSAndy Whitcroft			{
53030e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
53040e212e0aSFabian Frederick				if ($asminclude > 0) {
5305e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5306000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5307000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5308e09dec48SAndy Whitcroft					} else {
5309000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5310000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5311e09dec48SAndy Whitcroft					}
53120a920b5bSAndy Whitcroft				}
53130a920b5bSAndy Whitcroft			}
53140e212e0aSFabian Frederick		}
53150a920b5bSAndy Whitcroft
5316653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5317653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5318cf655043SAndy Whitcroft# in a known good container
5319b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5320b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5321d8aaf121SAndy Whitcroft			my $ln = $linenr;
5322d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5323c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5324c45dcabdSAndy Whitcroft			my $ctx = '';
532508a2843eSJoe Perches			my $has_flow_statement = 0;
532608a2843eSJoe Perches			my $has_arg_concat = 0;
5327c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5328f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5329f74bd194SAndy Whitcroft			$ctx = $dstat;
5330c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5331a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5332c45dcabdSAndy Whitcroft
533308a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
533462e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
533508a2843eSJoe Perches
5336f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5337f59b64bfSJoe Perches			my $define_args = $1;
5338f59b64bfSJoe Perches			my $define_stmt = $dstat;
5339f59b64bfSJoe Perches			my @def_args = ();
5340f59b64bfSJoe Perches
5341f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5342f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5343f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
53448c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5345f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5346f59b64bfSJoe Perches			}
5347f59b64bfSJoe Perches
5348292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5349c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5350c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5351c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5352c45dcabdSAndy Whitcroft
5353c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5354*2e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5355*2e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5356*2e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5357bf30d6edSAndy Whitcroft			{
5358c45dcabdSAndy Whitcroft			}
5359c45dcabdSAndy Whitcroft
5360342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
536133acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
536233acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5363e45bab8eSAndy Whitcroft			{
5364e45bab8eSAndy Whitcroft			}
5365e45bab8eSAndy Whitcroft
536642e15293SJoe Perches			# Make asm volatile uses seem like a generic function
536742e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
536842e15293SJoe Perches
5369c45dcabdSAndy Whitcroft			my $exceptions = qr{
5370c45dcabdSAndy Whitcroft				$Declare|
5371c45dcabdSAndy Whitcroft				module_param_named|
5372a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5373c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5374c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5375383099fdSAndy Whitcroft				__typeof__\(|
537622fd2d3eSStefani Seibold				union|
537722fd2d3eSStefani Seibold				struct|
5378ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
53796b10df42SVladimir Zapolskiy				^\"|\"$|
53806b10df42SVladimir Zapolskiy				^\[
5381c45dcabdSAndy Whitcroft			}x;
53825eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5383f59b64bfSJoe Perches
5384f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5385f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5386e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5387f59b64bfSJoe Perches
5388f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5389f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5390f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
53913cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5392356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5393f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5394f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5395e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
539672f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5397*2e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5398f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5399f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5400f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
54014e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5402f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5403c45dcabdSAndy Whitcroft			{
5404e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5405e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5406e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5407e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5408f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5409f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5410f74bd194SAndy Whitcroft				} else {
5411000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5412388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5413d8aaf121SAndy Whitcroft				}
5414f59b64bfSJoe Perches
5415f59b64bfSJoe Perches			}
54165207649bSJoe Perches
54175207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
54185207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
54195207649bSJoe Perches			my $first = 1;
54205207649bSJoe Perches			$define_stmt = "";
54215207649bSJoe Perches			foreach my $l (@stmt_array) {
54225207649bSJoe Perches				$l =~ s/\\$//;
54235207649bSJoe Perches				if ($first) {
54245207649bSJoe Perches					$define_stmt = $l;
54255207649bSJoe Perches					$first = 0;
54265207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
54275207649bSJoe Perches					$define_stmt .= substr($l, 1);
54285207649bSJoe Perches				}
54295207649bSJoe Perches			}
54305207649bSJoe Perches			$define_stmt =~ s/$;//g;
54315207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
54325207649bSJoe Perches			$define_stmt = trim($define_stmt);
54335207649bSJoe Perches
5434f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5435f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5436f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
54379192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
54387fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
54396dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
54407fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
54417fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5442d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5443f59b64bfSJoe Perches				if ($use_cnt > 1) {
5444f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5445f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5446f59b64bfSJoe Perches				    }
54479192d41aSJoe Perches# check if any macro arguments may have other precedence issues
54487fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
54499192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
54509192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
54519192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
54529192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
54539192d41aSJoe Perches				}
54540a920b5bSAndy Whitcroft			}
54555023d347SJoe Perches
545608a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
545708a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
545808a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
545908a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5460e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
546108a2843eSJoe Perches
546208a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
546308a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
546408a2843eSJoe Perches			}
546508a2843eSJoe Perches
5466481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
54675023d347SJoe Perches
54685023d347SJoe Perches		} else {
54695023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5470481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5471481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
54725023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
54735023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
54745023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
54755023d347SJoe Perches			}
5476653d4876SAndy Whitcroft		}
54770a920b5bSAndy Whitcroft
5478b13edf7fSJoe Perches# do {} while (0) macro tests:
5479b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5480b13edf7fSJoe Perches# macro should not end with a semicolon
54815b57980dSJoe Perches		if ($perl_version_ok &&
5482b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5483b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5484b13edf7fSJoe Perches			my $ln = $linenr;
5485b13edf7fSJoe Perches			my $cnt = $realcnt;
5486b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5487b13edf7fSJoe Perches			my $ctx = '';
5488b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5489b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5490b13edf7fSJoe Perches			$ctx = $dstat;
5491b13edf7fSJoe Perches
5492b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
54931b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5494b13edf7fSJoe Perches
5495b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5496b13edf7fSJoe Perches				my $stmts = $2;
5497b13edf7fSJoe Perches				my $semis = $3;
5498b13edf7fSJoe Perches
5499b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5500b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5501e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5502b13edf7fSJoe Perches
5503ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5504ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5505b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5506b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5507b13edf7fSJoe Perches				}
5508b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5509b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5510b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5511b13edf7fSJoe Perches				}
5512f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5513f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5514f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5515e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5516f5ef95b1SJoe Perches
5517f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5518f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5519b13edf7fSJoe Perches			}
5520b13edf7fSJoe Perches		}
5521b13edf7fSJoe Perches
5522f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
552313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
552413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5525cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
552613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5527cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5528cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5529aad4f614SJoe Perches				my @allowed = ();
5530aad4f614SJoe Perches				my $allow = 0;
553113214adfSAndy Whitcroft				my $seen = 0;
5532773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5533cf655043SAndy Whitcroft				my $ln = $linenr - 1;
553413214adfSAndy Whitcroft				for my $chunk (@chunks) {
553513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
553613214adfSAndy Whitcroft
5537773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5538773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5539773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5540773647a0SAndy Whitcroft
5541aad4f614SJoe Perches					$allowed[$allow] = 0;
5542773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5543773647a0SAndy Whitcroft
5544773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5545773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5546773647a0SAndy Whitcroft
5547773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5548cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5549cf655043SAndy Whitcroft
5550773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
555113214adfSAndy Whitcroft
555213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
555313214adfSAndy Whitcroft
5554aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5555cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5556cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5557aad4f614SJoe Perches						$allowed[$allow] = 1;
555813214adfSAndy Whitcroft					}
555913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5560cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5561aad4f614SJoe Perches						$allowed[$allow] = 1;
556213214adfSAndy Whitcroft					}
5563cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5564cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5565aad4f614SJoe Perches						$allowed[$allow] = 1;
556613214adfSAndy Whitcroft					}
5567aad4f614SJoe Perches					$allow++;
556813214adfSAndy Whitcroft				}
5569aad4f614SJoe Perches				if ($seen) {
5570aad4f614SJoe Perches					my $sum_allowed = 0;
5571aad4f614SJoe Perches					foreach (@allowed) {
5572aad4f614SJoe Perches						$sum_allowed += $_;
5573aad4f614SJoe Perches					}
5574aad4f614SJoe Perches					if ($sum_allowed == 0) {
5575000d1cc1SJoe Perches						WARN("BRACES",
5576000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5577aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5578aad4f614SJoe Perches						 $seen != $allow) {
5579aad4f614SJoe Perches						CHK("BRACES",
5580aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5581aad4f614SJoe Perches					}
558213214adfSAndy Whitcroft				}
558313214adfSAndy Whitcroft			}
558413214adfSAndy Whitcroft		}
5585773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
558613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5587cf655043SAndy Whitcroft			my $allowed = 0;
5588f0a594c1SAndy Whitcroft
5589cf655043SAndy Whitcroft			# Check the pre-context.
5590cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5591cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5592cf655043SAndy Whitcroft				$allowed = 1;
5593f0a594c1SAndy Whitcroft			}
5594773647a0SAndy Whitcroft
5595773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5596773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5597773647a0SAndy Whitcroft
5598cf655043SAndy Whitcroft			# Check the condition.
5599cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5600773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5601cf655043SAndy Whitcroft			if (defined $cond) {
5602773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5603cf655043SAndy Whitcroft			}
5604cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5605cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5606cf655043SAndy Whitcroft				$allowed = 1;
5607cf655043SAndy Whitcroft			}
5608cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5609cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5610cf655043SAndy Whitcroft				$allowed = 1;
5611cf655043SAndy Whitcroft			}
5612cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5613cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5614cf655043SAndy Whitcroft				$allowed = 1;
5615cf655043SAndy Whitcroft			}
5616cf655043SAndy Whitcroft			# Check the post-context.
5617cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5618cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5619cf655043SAndy Whitcroft				if (defined $cond) {
5620773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5621cf655043SAndy Whitcroft				}
5622cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5623cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5624cf655043SAndy Whitcroft					$allowed = 1;
5625cf655043SAndy Whitcroft				}
5626cf655043SAndy Whitcroft			}
5627cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5628f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5629e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5630cf655043SAndy Whitcroft
5631000d1cc1SJoe Perches				WARN("BRACES",
5632000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5633f0a594c1SAndy Whitcroft			}
5634f0a594c1SAndy Whitcroft		}
5635f0a594c1SAndy Whitcroft
5636e4c5babdSJoe Perches# check for single line unbalanced braces
563795330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
563895330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5639e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5640e4c5babdSJoe Perches		}
5641e4c5babdSJoe Perches
56420979ae66SJoe Perches# check for unnecessary blank lines around braces
564377b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5644f8e58219SJoe Perches			if (CHK("BRACES",
5645f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5646f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5647f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5648f8e58219SJoe Perches			}
56490979ae66SJoe Perches		}
565077b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5651f8e58219SJoe Perches			if (CHK("BRACES",
5652f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5653f8e58219SJoe Perches			    $fix) {
5654f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5655f8e58219SJoe Perches			}
56560979ae66SJoe Perches		}
56570979ae66SJoe Perches
56584a0df2efSAndy Whitcroft# no volatiles please
56596c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
56606c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5661000d1cc1SJoe Perches			WARN("VOLATILE",
56628c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
56634a0df2efSAndy Whitcroft		}
56644a0df2efSAndy Whitcroft
56655e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
56665e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
56675e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
56685e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
566933acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
56705e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
56715e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
56725e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
56735e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
56745e4f6ba5SJoe Perches				     $fix &&
56755e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
56765e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
56775e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
56785e4f6ba5SJoe Perches				my $comma_close = "";
56795e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
56805e4f6ba5SJoe Perches					$comma_close = $1;
56815e4f6ba5SJoe Perches				}
56825e4f6ba5SJoe Perches
56835e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
56845e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
56855e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
56865e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
56875e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
56885e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
56895e4f6ba5SJoe Perches				$fixedline = $rawline;
56905e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
56915e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
56925e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
56935e4f6ba5SJoe Perches				}
56945e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
56955e4f6ba5SJoe Perches			}
56965e4f6ba5SJoe Perches		}
56975e4f6ba5SJoe Perches
56985e4f6ba5SJoe Perches# check for missing a space in a string concatenation
56995e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
57005e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
57015e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
57025e4f6ba5SJoe Perches		}
57035e4f6ba5SJoe Perches
570477cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5705e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5706e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5707e4b7d309SJoe Perches# function declarations
570877cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
570977cb8546SJoe Perches		    defined($context_function) &&
5710e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5711e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
571277cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5713e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
571477cb8546SJoe Perches		}
571577cb8546SJoe Perches
57165e4f6ba5SJoe Perches# check for spaces before a quoted newline
57175e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
57185e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
57195e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
57205e4f6ba5SJoe Perches			    $fix) {
57215e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
57225e4f6ba5SJoe Perches			}
57235e4f6ba5SJoe Perches
57245e4f6ba5SJoe Perches		}
57255e4f6ba5SJoe Perches
5726f17dba4fSJoe Perches# concatenated string without spaces between elements
572779682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
572879682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
572979682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
573079682c0cSJoe Perches			    $fix) {
573179682c0cSJoe Perches				while ($line =~ /($String)/g) {
573279682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
573379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
573479682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
573579682c0cSJoe Perches				}
573679682c0cSJoe Perches			}
5737f17dba4fSJoe Perches		}
5738f17dba4fSJoe Perches
573990ad30e5SJoe Perches# uncoalesced string fragments
574033acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
574179682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
574279682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
574379682c0cSJoe Perches			    $fix) {
574479682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
574579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
574679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
574779682c0cSJoe Perches				}
574879682c0cSJoe Perches			}
574990ad30e5SJoe Perches		}
575090ad30e5SJoe Perches
5751522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5752522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5753522b837cSAlexey Dobriyan		my $show_Z = 1;
57545e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5755522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
57565e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5757522b837cSAlexey Dobriyan			# check for %L
5758522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
57595e4f6ba5SJoe Perches				WARN("PRINTF_L",
5760522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5761522b837cSAlexey Dobriyan				$show_L = 0;
57625e4f6ba5SJoe Perches			}
5763522b837cSAlexey Dobriyan			# check for %Z
5764522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5765522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5766522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5767522b837cSAlexey Dobriyan				$show_Z = 0;
5768522b837cSAlexey Dobriyan			}
5769522b837cSAlexey Dobriyan			# check for 0x<decimal>
5770522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5771522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
57726e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
57736e300757SJoe Perches			}
57745e4f6ba5SJoe Perches		}
57755e4f6ba5SJoe Perches
57765e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
57773f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
57785e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
57795e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
57805e4f6ba5SJoe Perches		}
57815e4f6ba5SJoe Perches
578200df344fSAndy Whitcroft# warn about #if 0
5783c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
578460f89010SPrakruthi Deepak Heragu			WARN("IF_0",
578560f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
578660f89010SPrakruthi Deepak Heragu		}
578760f89010SPrakruthi Deepak Heragu
578860f89010SPrakruthi Deepak Heragu# warn about #if 1
578960f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
579060f89010SPrakruthi Deepak Heragu			WARN("IF_1",
579160f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
57924a0df2efSAndy Whitcroft		}
57934a0df2efSAndy Whitcroft
579403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
579503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5796100425deSJoe Perches			my $tested = quotemeta($1);
5797100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5798100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5799100425deSJoe Perches				my $func = $1;
5800100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5801100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5802100425deSJoe Perches				    $fix) {
5803100425deSJoe Perches					my $do_fix = 1;
5804100425deSJoe Perches					my $leading_tabs = "";
5805100425deSJoe Perches					my $new_leading_tabs = "";
5806100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5807100425deSJoe Perches						$leading_tabs = $1;
5808100425deSJoe Perches					} else {
5809100425deSJoe Perches						$do_fix = 0;
5810100425deSJoe Perches					}
5811100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5812100425deSJoe Perches						$new_leading_tabs = $1;
5813100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5814100425deSJoe Perches							$do_fix = 0;
5815100425deSJoe Perches						}
5816100425deSJoe Perches					} else {
5817100425deSJoe Perches						$do_fix = 0;
5818100425deSJoe Perches					}
5819100425deSJoe Perches					if ($do_fix) {
5820100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5821100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5822100425deSJoe Perches					}
5823100425deSJoe Perches				}
58244c432a8fSGreg Kroah-Hartman			}
58254c432a8fSGreg Kroah-Hartman		}
5826f0a594c1SAndy Whitcroft
5827ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5828ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5829ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5830ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5831ebfdc409SJoe Perches		    $linenr > 3) {
5832ebfdc409SJoe Perches			my $testval = $2;
5833ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5834ebfdc409SJoe Perches
5835ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5836ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5837ebfdc409SJoe Perches
5838e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5839e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5840ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5841ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5842ebfdc409SJoe Perches			}
5843ebfdc409SJoe Perches		}
5844ebfdc409SJoe Perches
5845f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5846dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5847f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5848f78d98f6SJoe Perches			my $level = $1;
5849f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5850f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5851f78d98f6SJoe Perches			    $fix) {
5852f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5853f78d98f6SJoe Perches			}
5854f78d98f6SJoe Perches		}
5855f78d98f6SJoe Perches
585645c55e92SJoe Perches# check for logging continuations
585745c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
585845c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
585945c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
586045c55e92SJoe Perches		}
586145c55e92SJoe Perches
5862abb08a53SJoe Perches# check for mask then right shift without a parentheses
58635b57980dSJoe Perches		if ($perl_version_ok &&
5864abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5865abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5866abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5867abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5868abb08a53SJoe Perches		}
5869abb08a53SJoe Perches
5870b75ac618SJoe Perches# check for pointer comparisons to NULL
58715b57980dSJoe Perches		if ($perl_version_ok) {
5872b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5873b75ac618SJoe Perches				my $val = $1;
5874b75ac618SJoe Perches				my $equal = "!";
5875b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5876b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5877b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5878b75ac618SJoe Perches					    $fix) {
5879b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5880b75ac618SJoe Perches				}
5881b75ac618SJoe Perches			}
5882b75ac618SJoe Perches		}
5883b75ac618SJoe Perches
58848716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
58858716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
58868716de38SJoe Perches			my $attr = $1;
58878716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
58888716de38SJoe Perches				my $ptr = $1;
58898716de38SJoe Perches				my $var = $2;
58908716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
58918716de38SJoe Perches				      ERROR("MISPLACED_INIT",
58928716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
58938716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
58948716de38SJoe Perches				      WARN("MISPLACED_INIT",
58958716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
58968716de38SJoe Perches				    $fix) {
5897194f66fcSJoe 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;
58988716de38SJoe Perches				}
58998716de38SJoe Perches			}
59008716de38SJoe Perches		}
59018716de38SJoe Perches
5902e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5903e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5904e970b884SJoe Perches			my $attr = $1;
5905e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5906e970b884SJoe Perches			my $attr_prefix = $1;
5907e970b884SJoe Perches			my $attr_type = $2;
5908e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5909e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5910e970b884SJoe Perches			    $fix) {
5911194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5912e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5913e970b884SJoe Perches			}
5914e970b884SJoe Perches		}
5915e970b884SJoe Perches
5916e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5917e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5918e970b884SJoe Perches			my $attr = $1;
5919e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5920e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5921e970b884SJoe Perches			    $fix) {
5922194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5923e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5924e970b884SJoe Perches				$lead = rtrim($1);
5925e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5926e970b884SJoe Perches				$lead = "${lead}const ";
5927194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5928e970b884SJoe Perches			}
5929e970b884SJoe Perches		}
5930e970b884SJoe Perches
5931c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5932c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5933c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5934c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5935c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5936c17893c7SJoe Perches			    $fix) {
5937c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5938c17893c7SJoe Perches			}
5939c17893c7SJoe Perches		}
5940c17893c7SJoe Perches
5941fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5942fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5943fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5944fbdb8138SJoe Perches			my $constant_func = $1;
5945fbdb8138SJoe Perches			my $func = $constant_func;
5946fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5947fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5948fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5949fbdb8138SJoe Perches			    $fix) {
5950194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5951fbdb8138SJoe Perches			}
5952fbdb8138SJoe Perches		}
5953fbdb8138SJoe Perches
59541a15a250SPatrick Pannuto# prefer usleep_range over udelay
595537581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
595643c1d77cSJoe Perches			my $delay = $1;
59571a15a250SPatrick Pannuto			# ignore udelay's < 10, however
595843c1d77cSJoe Perches			if (! ($delay < 10) ) {
5959000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5960458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
596143c1d77cSJoe Perches			}
596243c1d77cSJoe Perches			if ($delay > 2000) {
596343c1d77cSJoe Perches				WARN("LONG_UDELAY",
596443c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
59651a15a250SPatrick Pannuto			}
59661a15a250SPatrick Pannuto		}
59671a15a250SPatrick Pannuto
596809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
596909ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
597009ef8725SPatrick Pannuto			if ($1 < 20) {
5971000d1cc1SJoe Perches				WARN("MSLEEP",
5972458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
597309ef8725SPatrick Pannuto			}
597409ef8725SPatrick Pannuto		}
597509ef8725SPatrick Pannuto
597636ec1939SJoe Perches# check for comparisons of jiffies
597736ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
597836ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
597936ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
598036ec1939SJoe Perches		}
598136ec1939SJoe Perches
59829d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
59839d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
59849d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
59859d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
59869d7a34a5SJoe Perches		}
59879d7a34a5SJoe Perches
598800df344fSAndy Whitcroft# warn about #ifdefs in C files
5989c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
599000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
599100df344fSAndy Whitcroft#			print "$herecurr";
599200df344fSAndy Whitcroft#			$clean = 0;
599300df344fSAndy Whitcroft#		}
599400df344fSAndy Whitcroft
599522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5996c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
59973705ce5bSJoe Perches			if (ERROR("SPACING",
59983705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
59993705ce5bSJoe Perches			    $fix) {
6000194f66fcSJoe Perches				$fixed[$fixlinenr] =~
60013705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
60023705ce5bSJoe Perches			}
60033705ce5bSJoe Perches
600422f2a2efSAndy Whitcroft		}
600522f2a2efSAndy Whitcroft
60064a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6007171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6008171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
60094a0df2efSAndy Whitcroft			my $which = $1;
60104a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6011000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6012000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
60134a0df2efSAndy Whitcroft			}
60144a0df2efSAndy Whitcroft		}
60154a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6016402c2553SMichael S. Tsirkin
6017402c2553SMichael S. Tsirkin		my $barriers = qr{
6018402c2553SMichael S. Tsirkin			mb|
6019402c2553SMichael S. Tsirkin			rmb|
6020ad83ec6cSWill Deacon			wmb
6021402c2553SMichael S. Tsirkin		}x;
6022402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6023402c2553SMichael S. Tsirkin			mb__before_atomic|
6024402c2553SMichael S. Tsirkin			mb__after_atomic|
6025402c2553SMichael S. Tsirkin			store_release|
6026402c2553SMichael S. Tsirkin			load_acquire|
6027402c2553SMichael S. Tsirkin			store_mb|
6028402c2553SMichael S. Tsirkin			(?:$barriers)
6029402c2553SMichael S. Tsirkin		}x;
6030402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6031402c2553SMichael S. Tsirkin			(?:$barriers)|
603243e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
603343e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6034402c2553SMichael S. Tsirkin		}x;
6035402c2553SMichael S. Tsirkin
6036402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
60374a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6038c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6039000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
60404a0df2efSAndy Whitcroft			}
60414a0df2efSAndy Whitcroft		}
60423ad81779SPaul E. McKenney
6043f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6044f4073b0fSMichael S. Tsirkin
6045f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6046f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6047f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6048f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6049f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6050f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6051f4073b0fSMichael S. Tsirkin		}
6052f4073b0fSMichael S. Tsirkin
6053cb426e99SJoe Perches# check for waitqueue_active without a comment.
6054cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6055cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6056cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6057cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6058cb426e99SJoe Perches			}
6059cb426e99SJoe Perches		}
60603ad81779SPaul E. McKenney
60615099a722SMarco Elver# check for data_race without a comment.
60625099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
60635099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
60645099a722SMarco Elver				WARN("DATA_RACE",
60655099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
60665099a722SMarco Elver			}
60675099a722SMarco Elver		}
60685099a722SMarco Elver
60694a0df2efSAndy Whitcroft# check of hardware specific defines
6070c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6071000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6072000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
60730a920b5bSAndy Whitcroft		}
6074653d4876SAndy Whitcroft
6075596ed45bSJoe Perches# check that the storage class is not after a type
6076596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6077000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6078596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6079596ed45bSJoe Perches		}
6080596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6081596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6082596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6083596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6084596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6085596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6086596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6087d4977c78STobias Klauser		}
6088d4977c78STobias Klauser
6089de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6090de7d4f0eSAndy Whitcroft# storage class and type.
60919c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
60929c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6093000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6094000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6095de7d4f0eSAndy Whitcroft		}
6096de7d4f0eSAndy Whitcroft
60978905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
60982b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
60992b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6100d5e616fcSJoe Perches			if (WARN("INLINE",
6101d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6102d5e616fcSJoe Perches			    $fix) {
6103194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6104d5e616fcSJoe Perches
6105d5e616fcSJoe Perches			}
61068905a67cSAndy Whitcroft		}
61078905a67cSAndy Whitcroft
61083d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
61092b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
61102b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
6111000d1cc1SJoe Perches			WARN("PREFER_PACKED",
6112000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
61133d130fd0SJoe Perches		}
61143d130fd0SJoe Perches
611539b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
61162b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
61172b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
6118000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
6119000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
612039b7e287SJoe Perches		}
612139b7e287SJoe Perches
6122462811d9SJoe Perches# Check for __attribute__ section, prefer __section
6123462811d9SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6124462811d9SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6125462811d9SJoe Perches			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6126462811d9SJoe Perches			my $new = substr($old, 1, -1);
6127462811d9SJoe Perches			if (WARN("PREFER_SECTION",
6128462811d9SJoe Perches				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6129462811d9SJoe Perches			    $fix) {
6130462811d9SJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6131462811d9SJoe Perches			}
6132462811d9SJoe Perches		}
6133462811d9SJoe Perches
61345f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
61352b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
61362b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
6137d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
6138d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6139d5e616fcSJoe Perches			    $fix) {
6140194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
6141d5e616fcSJoe Perches
6142d5e616fcSJoe Perches			}
61435f14d3bdSJoe Perches		}
61445f14d3bdSJoe Perches
61456061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
61462b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
61472b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
6148d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
6149d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6150d5e616fcSJoe Perches			    $fix) {
6151194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
6152d5e616fcSJoe Perches			}
61536061d949SJoe Perches		}
61546061d949SJoe Perches
6155619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
61565b57980dSJoe Perches		if ($perl_version_ok &&
6157619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6158619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6159619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6160619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6161619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6162619a908aSJoe Perches		}
6163619a908aSJoe Perches
6164fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6165e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6166fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6167e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6168e6176fa4SJoe Perches			my $type = $1;
6169e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6170e6176fa4SJoe Perches				$type = $1;
6171e6176fa4SJoe Perches				my $kernel_type = 'u';
6172e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6173e6176fa4SJoe Perches				$type =~ /(\d+)/;
6174e6176fa4SJoe Perches				$kernel_type .= $1;
6175e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6176e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6177e6176fa4SJoe Perches				    $fix) {
6178e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6179e6176fa4SJoe Perches				}
6180e6176fa4SJoe Perches			}
6181e6176fa4SJoe Perches		}
6182e6176fa4SJoe Perches
6183938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6184938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6185938224b5SJoe Perches			my $cast = $1;
6186938224b5SJoe Perches			my $const = $2;
6187938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6188938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6189938224b5SJoe Perches			    $fix) {
6190938224b5SJoe Perches				my $suffix = "";
6191938224b5SJoe Perches				my $newconst = $const;
6192938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6193938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6194938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6195938224b5SJoe Perches					$suffix .= 'LL';
6196938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6197938224b5SJoe Perches					$suffix .= 'L';
6198938224b5SJoe Perches				}
6199938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6200938224b5SJoe Perches			}
6201938224b5SJoe Perches		}
6202938224b5SJoe Perches
62038f53a9b8SJoe Perches# check for sizeof(&)
62048f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6205000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6206000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
62078f53a9b8SJoe Perches		}
62088f53a9b8SJoe Perches
620966c80b60SJoe Perches# check for sizeof without parenthesis
621066c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6211d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6212d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6213d5e616fcSJoe Perches			    $fix) {
6214194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6215d5e616fcSJoe Perches			}
621666c80b60SJoe Perches		}
621766c80b60SJoe Perches
621888982feaSJoe Perches# check for struct spinlock declarations
621988982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
622088982feaSJoe Perches			WARN("USE_SPINLOCK_T",
622188982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
622288982feaSJoe Perches		}
622388982feaSJoe Perches
6224a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
622506668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6226a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6227caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6228caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6229d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6230d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6231d5e616fcSJoe Perches				    $fix) {
6232194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6233d5e616fcSJoe Perches				}
6234a6962d72SJoe Perches			}
6235a6962d72SJoe Perches		}
6236a6962d72SJoe Perches
62370b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
62385b57980dSJoe Perches		if ($perl_version_ok &&
62390b523769SJoe Perches		    defined $stat &&
62400b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
62410b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6242e3c6bc95STobin C. Harding			my $stat_real;
6243e3c6bc95STobin C. Harding
62440b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
62450b523769SJoe Perches			$lc = $lc + $linenr;
62460b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6247ffe07513SJoe Perches				my $specifier;
6248ffe07513SJoe Perches				my $extension;
62493bd32d6aSSakari Ailus				my $qualifier;
6250ffe07513SJoe Perches				my $bad_specifier = "";
62510b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
62520b523769SJoe Perches				$fmt =~ s/%%//g;
6253e3c6bc95STobin C. Harding
62543bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6255e3c6bc95STobin C. Harding					$specifier = $1;
6256e3c6bc95STobin C. Harding					$extension = $2;
62573bd32d6aSSakari Ailus					$qualifier = $3;
6258361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
62593bd32d6aSSakari Ailus					    ($extension eq "f" &&
62603bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6261e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
62620b523769SJoe Perches						last;
62630b523769SJoe Perches					}
6264e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6265e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6266e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
62670b523769SJoe Perches						}
6268e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6269e3c6bc95STobin 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");
6270e3c6bc95STobin C. Harding					}
6271e3c6bc95STobin C. Harding				}
6272e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
62732a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
62741df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
62751df7338aSSergey Senozhatsky					my $use = "";
6276e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
62771df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6278e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
62791df7338aSSergey Senozhatsky					}
62802a9f9d85STobin C. Harding
62810b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6282e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6283e3c6bc95STobin C. Harding				}
62840b523769SJoe Perches			}
62850b523769SJoe Perches		}
62860b523769SJoe Perches
6287554e165cSAndy Whitcroft# Check for misused memsets
62885b57980dSJoe Perches		if ($perl_version_ok &&
6289d1fe9c09SJoe Perches		    defined $stat &&
62909e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6291554e165cSAndy Whitcroft
6292d7c76ba7SJoe Perches			my $ms_addr = $2;
6293d1fe9c09SJoe Perches			my $ms_val = $7;
6294d1fe9c09SJoe Perches			my $ms_size = $12;
6295d7c76ba7SJoe Perches
6296554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6297554e165cSAndy Whitcroft				ERROR("MEMSET",
6298d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6299554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6300554e165cSAndy Whitcroft				WARN("MEMSET",
6301d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6302d7c76ba7SJoe Perches			}
6303d7c76ba7SJoe Perches		}
6304d7c76ba7SJoe Perches
630598a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
63065b57980dSJoe Perches#		if ($perl_version_ok &&
6307f333195dSJoe Perches#		    defined $stat &&
6308f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6309f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6310f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6311f333195dSJoe Perches#			    $fix) {
6312f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6313f333195dSJoe Perches#			}
6314f333195dSJoe Perches#		}
631598a9bba5SJoe Perches
6316b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
63175b57980dSJoe Perches#		if ($perl_version_ok &&
6318f333195dSJoe Perches#		    defined $stat &&
6319f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6320f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6321f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6322f333195dSJoe Perches#		}
6323b6117d17SMateusz Kulikowski
63248617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
63258617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
63265b57980dSJoe Perches#		if ($perl_version_ok &&
6327f333195dSJoe Perches#		    defined $stat &&
6328f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6329f333195dSJoe Perches#
6330f333195dSJoe Perches#			my $ms_val = $7;
6331f333195dSJoe Perches#
6332f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6333f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6334f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6335f333195dSJoe Perches#				    $fix) {
6336f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6337f333195dSJoe Perches#				}
6338f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6339f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6340f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6341f333195dSJoe Perches#				    $fix) {
6342f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6343f333195dSJoe Perches#				}
6344f333195dSJoe Perches#			}
6345f333195dSJoe Perches#		}
63468617cd09SMateusz Kulikowski
6347d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
63485b57980dSJoe Perches		if ($perl_version_ok &&
6349d1fe9c09SJoe Perches		    defined $stat &&
6350d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6351d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6352d7c76ba7SJoe Perches				my $call = $1;
6353d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6354d7c76ba7SJoe Perches				my $arg1 = $3;
6355d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6356d1fe9c09SJoe Perches				my $arg2 = $8;
6357d7c76ba7SJoe Perches				my $cast;
6358d7c76ba7SJoe Perches
6359d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6360d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6361d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6362d7c76ba7SJoe Perches					$cast = $cast1;
6363d7c76ba7SJoe Perches				} else {
6364d7c76ba7SJoe Perches					$cast = $cast2;
6365d7c76ba7SJoe Perches				}
6366d7c76ba7SJoe Perches				WARN("MINMAX",
6367d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6368554e165cSAndy Whitcroft			}
6369554e165cSAndy Whitcroft		}
6370554e165cSAndy Whitcroft
63714a273195SJoe Perches# check usleep_range arguments
63725b57980dSJoe Perches		if ($perl_version_ok &&
63734a273195SJoe Perches		    defined $stat &&
63744a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
63754a273195SJoe Perches			my $min = $1;
63764a273195SJoe Perches			my $max = $7;
63774a273195SJoe Perches			if ($min eq $max) {
63784a273195SJoe Perches				WARN("USLEEP_RANGE",
6379458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
63804a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
63814a273195SJoe Perches				 $min > $max) {
63824a273195SJoe Perches				WARN("USLEEP_RANGE",
6383458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
63844a273195SJoe Perches			}
63854a273195SJoe Perches		}
63864a273195SJoe Perches
6387823b794cSJoe Perches# check for naked sscanf
63885b57980dSJoe Perches		if ($perl_version_ok &&
6389823b794cSJoe Perches		    defined $stat &&
63906c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6391823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6392823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6393823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6394823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6395823b794cSJoe Perches			$lc = $lc + $linenr;
63962a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6397823b794cSJoe Perches			WARN("NAKED_SSCANF",
6398823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6399823b794cSJoe Perches		}
6400823b794cSJoe Perches
6401afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
64025b57980dSJoe Perches		if ($perl_version_ok &&
6403afc819abSJoe Perches		    defined $stat &&
6404afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6405afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6406afc819abSJoe Perches			$lc = $lc + $linenr;
64072a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6408afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6409afc819abSJoe Perches				my $format = $6;
6410afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6411afc819abSJoe Perches				if ($count == 1 &&
6412afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6413afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6414afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6415afc819abSJoe Perches				}
6416afc819abSJoe Perches			}
6417afc819abSJoe Perches		}
6418afc819abSJoe Perches
641970dc8a48SJoe Perches# check for new externs in .h files.
642070dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
642170dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6422d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
642370dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
642470dc8a48SJoe Perches			    $fix) {
6425194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
642670dc8a48SJoe Perches			}
642770dc8a48SJoe Perches		}
642870dc8a48SJoe Perches
6429de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6430171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6431c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6432171ae1a4SAndy Whitcroft		{
6433c45dcabdSAndy Whitcroft			my $function_name = $1;
6434c45dcabdSAndy Whitcroft			my $paren_space = $2;
6435171ae1a4SAndy Whitcroft
6436171ae1a4SAndy Whitcroft			my $s = $stat;
6437171ae1a4SAndy Whitcroft			if (defined $cond) {
6438171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6439171ae1a4SAndy Whitcroft			}
6440d8b44b58SKees Cook			if ($s =~ /^\s*;/)
6441c45dcabdSAndy Whitcroft			{
6442000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6443000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6444de7d4f0eSAndy Whitcroft			}
6445de7d4f0eSAndy Whitcroft
6446171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6447000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6448000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6449171ae1a4SAndy Whitcroft			}
64509c9ba34eSAndy Whitcroft
64519c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
64529c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
64539c9ba34eSAndy Whitcroft		{
6454000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6455000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6456171ae1a4SAndy Whitcroft		}
6457171ae1a4SAndy Whitcroft
6458a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6459a0ad7596SJoe Perches		if (defined $stat &&
6460d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6461d8b44b58SKees Cook		    $1 ne "void") {
6462d8b44b58SKees Cook			my $args = trim($1);
6463ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6464ca0d8929SJoe Perches				my $arg = trim($1);
6465d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6466ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6467ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6468ca0d8929SJoe Perches				}
6469ca0d8929SJoe Perches			}
6470ca0d8929SJoe Perches		}
6471ca0d8929SJoe Perches
6472a0ad7596SJoe Perches# check for function definitions
64735b57980dSJoe Perches		if ($perl_version_ok &&
6474a0ad7596SJoe Perches		    defined $stat &&
6475a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6476a0ad7596SJoe Perches			$context_function = $1;
6477a0ad7596SJoe Perches
6478a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6479a0ad7596SJoe Perches			my $ok = 0;
6480a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6481a0ad7596SJoe Perches			my $herectx = $here . "\n";
6482a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6483a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6484a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6485a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6486a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6487a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6488a0ad7596SJoe Perches			}
6489a0ad7596SJoe Perches			if (!$ok) {
6490a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6491a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6492a0ad7596SJoe Perches			}
6493a0ad7596SJoe Perches		}
6494a0ad7596SJoe Perches
6495de7d4f0eSAndy Whitcroft# checks for new __setup's
6496de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6497de7d4f0eSAndy Whitcroft			my $name = $1;
6498de7d4f0eSAndy Whitcroft
6499de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6500000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
65012581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6502de7d4f0eSAndy Whitcroft			}
6503653d4876SAndy Whitcroft		}
65049c0ca6f9SAndy Whitcroft
6505e29a70f1SJoe Perches# check for pointless casting of alloc functions
6506e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6507000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6508000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
65099c0ca6f9SAndy Whitcroft		}
651013214adfSAndy Whitcroft
6511a640d25cSJoe Perches# alloc style
6512a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
65135b57980dSJoe Perches		if ($perl_version_ok &&
6514e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6515a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6516a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6517a640d25cSJoe Perches		}
6518a640d25cSJoe Perches
651960a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
65205b57980dSJoe Perches		if ($perl_version_ok &&
65211b4a2ed4SJoe Perches		    defined $stat &&
65221b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
652360a55369SJoe Perches			my $oldfunc = $3;
652460a55369SJoe Perches			my $a1 = $4;
652560a55369SJoe Perches			my $a2 = $10;
652660a55369SJoe Perches			my $newfunc = "kmalloc_array";
652760a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
652860a55369SJoe Perches			my $r1 = $a1;
652960a55369SJoe Perches			my $r2 = $a2;
653060a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
653160a55369SJoe Perches				$r1 = $a2;
653260a55369SJoe Perches				$r2 = $a1;
653360a55369SJoe Perches			}
6534e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6535e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
65361b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6537e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6538e3d95a2aSTobin C. Harding
6539e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
65401b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
65411b4a2ed4SJoe Perches				    $cnt == 1 &&
6542e367455aSJoe Perches				    $fix) {
6543194f66fcSJoe 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;
654460a55369SJoe Perches				}
654560a55369SJoe Perches			}
654660a55369SJoe Perches		}
654760a55369SJoe Perches
6548972fdea2SJoe Perches# check for krealloc arg reuse
65495b57980dSJoe Perches		if ($perl_version_ok &&
65504cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
65514cab63ceSJoe Perches		    $1 eq $3) {
6552972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6553972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6554972fdea2SJoe Perches		}
6555972fdea2SJoe Perches
65565ce59ae0SJoe Perches# check for alloc argument mismatch
65575ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
65585ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
65595ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
65605ce59ae0SJoe Perches		}
65615ce59ae0SJoe Perches
6562caf2a54fSJoe Perches# check for multiple semicolons
6563caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6564d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6565d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6566d5e616fcSJoe Perches			    $fix) {
6567194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6568d5e616fcSJoe Perches			}
6569d1e2ad07SJoe Perches		}
6570d1e2ad07SJoe Perches
6571cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6572cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6573cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
65740ab90191SJoe Perches			my $ull = "";
65750ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
65760ab90191SJoe Perches			if (CHK("BIT_MACRO",
65770ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
65780ab90191SJoe Perches			    $fix) {
65790ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
65800ab90191SJoe Perches			}
65810ab90191SJoe Perches		}
65820ab90191SJoe Perches
658350161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
65843e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
658550161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
65863e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
658750161266SJoe Perches		}
658850161266SJoe Perches
65892d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
65903e89ad85SJerome 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*$/) {
65912d632745SJoe Perches			my $config = $1;
65922d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
65933e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
65942d632745SJoe Perches			    $fix) {
65952d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
65962d632745SJoe Perches			}
65972d632745SJoe Perches		}
65982d632745SJoe Perches
6599f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6600f36d3eb8SJoe Perches		my @fallthroughs = (
6601f36d3eb8SJoe Perches			'fallthrough',
6602f36d3eb8SJoe Perches			'@fallthrough@',
6603f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6604f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6605f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6606f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6607f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6608f36d3eb8SJoe Perches		    );
6609f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6610f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6611f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6612f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6613f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6614f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6615f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6616f36d3eb8SJoe Perches					last;
6617f36d3eb8SJoe Perches				}
6618f36d3eb8SJoe Perches			}
6619f36d3eb8SJoe Perches		}
6620f36d3eb8SJoe Perches
6621d1e2ad07SJoe Perches# check for switch/default statements without a break;
66225b57980dSJoe Perches		if ($perl_version_ok &&
6623d1e2ad07SJoe Perches		    defined $stat &&
6624d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6625d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6626e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6627e3d95a2aSTobin C. Harding
6628d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6629d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6630caf2a54fSJoe Perches		}
6631caf2a54fSJoe Perches
663213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6633d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6634d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6635d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6636d5e616fcSJoe Perches			    $fix) {
6637194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6638d5e616fcSJoe Perches			}
663913214adfSAndy Whitcroft		}
6640773647a0SAndy Whitcroft
664162ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
664262ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
664362ec818fSJoe Perches			ERROR("DATE_TIME",
664462ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
664562ec818fSJoe Perches		}
664662ec818fSJoe Perches
66472c92488aSJoe Perches# check for use of yield()
66482c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
66492c92488aSJoe Perches			WARN("YIELD",
66502c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
66512c92488aSJoe Perches		}
66522c92488aSJoe Perches
6653179f8f40SJoe Perches# check for comparisons against true and false
6654179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6655179f8f40SJoe Perches			my $lead = $1;
6656179f8f40SJoe Perches			my $arg = $2;
6657179f8f40SJoe Perches			my $test = $3;
6658179f8f40SJoe Perches			my $otype = $4;
6659179f8f40SJoe Perches			my $trail = $5;
6660179f8f40SJoe Perches			my $op = "!";
6661179f8f40SJoe Perches
6662179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6663179f8f40SJoe Perches
6664179f8f40SJoe Perches			my $type = lc($otype);
6665179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6666179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6667179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6668179f8f40SJoe Perches					$op = "";
6669179f8f40SJoe Perches				}
6670179f8f40SJoe Perches
6671179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6672179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6673179f8f40SJoe Perches
6674179f8f40SJoe Perches## maybe suggesting a correct construct would better
6675179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6676179f8f40SJoe Perches
6677179f8f40SJoe Perches			}
6678179f8f40SJoe Perches		}
6679179f8f40SJoe Perches
66804882720bSThomas Gleixner# check for semaphores initialized locked
66814882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6682000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6683000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6684773647a0SAndy Whitcroft		}
66856712d858SJoe Perches
668667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
668767d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6688000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
668967d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6690773647a0SAndy Whitcroft		}
66916712d858SJoe Perches
6692ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6693f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6694000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6695ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6696f3db6639SMichael Ellerman		}
66976712d858SJoe Perches
66983d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
66993d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
67003d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
67013d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
67023d709ab5SPaul E. McKenney		}
67033d709ab5SPaul E. McKenney
67049189c7e7SJoe Perches# check for deprecated apis
67059189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
67069189c7e7SJoe Perches			my $deprecated_api = $1;
67079189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
67089189c7e7SJoe Perches			WARN("DEPRECATED_API",
67099189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
67109189c7e7SJoe Perches		}
67119189c7e7SJoe Perches
67120f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6713d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
6714ced69da1SQuentin Monnet		if (defined($const_structs) &&
6715ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
6716d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6717000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6718d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
67192b6db5cbSAndy Whitcroft		}
6720773647a0SAndy Whitcroft
6721773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6722773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6723773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6724c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6725c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6726171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6727171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6728171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6729773647a0SAndy Whitcroft		{
6730000d1cc1SJoe Perches			WARN("NR_CPUS",
6731000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6732773647a0SAndy Whitcroft		}
67339c9ba34eSAndy Whitcroft
673452ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
673552ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
673652ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
673752ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
673852ea8506SJoe Perches		}
673952ea8506SJoe Perches
6740acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
67415b57980dSJoe Perches		if ($perl_version_ok &&
6742acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6743acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6744acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6745acd9362cSJoe Perches		}
6746acd9362cSJoe Perches
6747de3f186fSDenis Efremov# nested likely/unlikely calls
6748de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6749de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6750de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6751de3f186fSDenis Efremov		}
6752de3f186fSDenis Efremov
6753691d77b6SAndy Whitcroft# whine mightly about in_atomic
6754691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6755691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6756000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6757000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6758f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6759000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6760000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6761691d77b6SAndy Whitcroft			}
6762691d77b6SAndy Whitcroft		}
67631704f47bSPeter Zijlstra
67640f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
67650f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
67660f5225b0SPeter Zijlstra			ERROR("LOCKING",
67670f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
67680f5225b0SPeter Zijlstra		}
67690f5225b0SPeter Zijlstra
67701704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
67711704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
67721704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
67731704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
67741704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
67751704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6776000d1cc1SJoe Perches				ERROR("LOCKDEP",
6777000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
67781704f47bSPeter Zijlstra			}
67791704f47bSPeter Zijlstra		}
678088f8831cSDave Jones
6781b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6782b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6783000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6784000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
678588f8831cSDave Jones		}
67862435880fSJoe Perches
678700180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
678800180468SJoe Perches# and whether or not function naming is typical and if
678900180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
67905b57980dSJoe Perches		if ($perl_version_ok &&
679100180468SJoe Perches		    defined $stat &&
679200180468SJoe 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*\)/) {
679300180468SJoe Perches			my $var = $1;
679400180468SJoe Perches			my $perms = $2;
679500180468SJoe Perches			my $show = $3;
679600180468SJoe Perches			my $store = $4;
679700180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
679800180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
679900180468SJoe Perches			    $store =~ /^${var}_store$/ &&
680000180468SJoe Perches			    $octal_perms eq "0644") {
680100180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
680200180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
680300180468SJoe Perches				    $fix) {
680400180468SJoe 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})/;
680500180468SJoe Perches				}
680600180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
680700180468SJoe Perches				 $store =~ /^NULL$/ &&
680800180468SJoe Perches				 $octal_perms eq "0444") {
680900180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
681000180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
681100180468SJoe Perches				    $fix) {
681200180468SJoe 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})/;
681300180468SJoe Perches				}
681400180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
681500180468SJoe Perches				 $store =~ /^${var}_store$/ &&
681600180468SJoe Perches				 $octal_perms eq "0200") {
681700180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
681800180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
681900180468SJoe Perches				    $fix) {
682000180468SJoe 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})/;
682100180468SJoe Perches				}
682200180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
682300180468SJoe Perches				 $octal_perms eq "0444" ||
682400180468SJoe Perches				 $octal_perms eq "0200") {
682500180468SJoe Perches				my $newshow = "$show";
682600180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
682700180468SJoe Perches				my $newstore = $store;
682800180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
682900180468SJoe Perches				my $rename = "";
683000180468SJoe Perches				if ($show ne $newshow) {
683100180468SJoe Perches					$rename .= " '$show' to '$newshow'";
683200180468SJoe Perches				}
683300180468SJoe Perches				if ($store ne $newstore) {
683400180468SJoe Perches					$rename .= " '$store' to '$newstore'";
683500180468SJoe Perches				}
683600180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
683700180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
683800180468SJoe Perches			} else {
683900180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
684000180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
684100180468SJoe Perches			}
684200180468SJoe Perches		}
684300180468SJoe Perches
6844515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6845515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
684673121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
684773121534SJoe Perches#   specific definition of not visible in sysfs.
684873121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
684973121534SJoe Perches#   use the default permissions
68505b57980dSJoe Perches		if ($perl_version_ok &&
6851459cf0aeSJoe Perches		    defined $stat &&
6852515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
68532435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
68542435880fSJoe Perches				my $func = $entry->[0];
68552435880fSJoe Perches				my $arg_pos = $entry->[1];
68562435880fSJoe Perches
6857459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6858459cf0aeSJoe Perches				$lc = $lc + $linenr;
68592a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6860459cf0aeSJoe Perches
68612435880fSJoe Perches				my $skip_args = "";
68622435880fSJoe Perches				if ($arg_pos > 1) {
68632435880fSJoe Perches					$arg_pos--;
68642435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
68652435880fSJoe Perches				}
6866f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6867459cf0aeSJoe Perches				if ($stat =~ /$test/) {
68682435880fSJoe Perches					my $val = $1;
68692435880fSJoe Perches					$val = $6 if ($skip_args ne "");
687073121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
687173121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
687273121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
68732435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6874459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6875f90774e1SJoe Perches					}
6876f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6877c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6878459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
68792435880fSJoe Perches					}
6880459cf0aeSJoe Perches				}
6881459cf0aeSJoe Perches			}
6882459cf0aeSJoe Perches		}
6883459cf0aeSJoe Perches
6884459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6885bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
688600180468SJoe Perches			my $oval = $1;
688700180468SJoe Perches			my $octal = perms_to_octal($oval);
6888f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6889459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6890f90774e1SJoe Perches			    $fix) {
689100180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
68922435880fSJoe Perches			}
689313214adfSAndy Whitcroft		}
68945a6d20ceSBjorn Andersson
68955a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
68965a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
68975a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
68985a6d20ceSBjorn Andersson			my $valid_licenses = qr{
68995a6d20ceSBjorn Andersson						GPL|
69005a6d20ceSBjorn Andersson						GPL\ v2|
69015a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
69025a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
69035a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
69045a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
69055a6d20ceSBjorn Andersson						Proprietary
69065a6d20ceSBjorn Andersson					}x;
69075a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
69085a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
69095a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
69105a6d20ceSBjorn Andersson			}
69115a6d20ceSBjorn Andersson		}
69126a8d76cbSMatteo Croce
69136a8d76cbSMatteo Croce# check for sysctl duplicate constants
69146a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
69156a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
69166a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
69176a8d76cbSMatteo Croce		}
6918515a235eSJoe Perches	}
691913214adfSAndy Whitcroft
692013214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
692113214adfSAndy Whitcroft	# so just keep quiet.
692213214adfSAndy Whitcroft	if ($#rawlines == -1) {
692313214adfSAndy Whitcroft		exit(0);
69240a920b5bSAndy Whitcroft	}
69250a920b5bSAndy Whitcroft
69268905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
69278905a67cSAndy Whitcroft	# things that appear to be patches.
69288905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
69298905a67cSAndy Whitcroft		exit(0);
69308905a67cSAndy Whitcroft	}
69318905a67cSAndy Whitcroft
69328905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
69338905a67cSAndy Whitcroft	# just keep quiet.
69348905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
69358905a67cSAndy Whitcroft		exit(0);
69368905a67cSAndy Whitcroft	}
69378905a67cSAndy Whitcroft
6938a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6939000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6940000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
69410a920b5bSAndy Whitcroft	}
6942cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6943cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6944000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6945000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6946cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6947cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6948cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6949cd261496SGeert Uytterhoeven		}
69500a920b5bSAndy Whitcroft	}
69510a920b5bSAndy Whitcroft
6952f0a594c1SAndy Whitcroft	print report_dump();
695313214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
695413214adfSAndy Whitcroft		print "$filename " if ($summary_file);
69556c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
69566c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
69576c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
69586c72ffaaSAndy Whitcroft	}
69598905a67cSAndy Whitcroft
6960d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6961ef212196SJoe Perches		# If there were any defects found and not already fixing them
6962ef212196SJoe Perches		if (!$clean and !$fix) {
6963ef212196SJoe Perches			print << "EOM"
6964ef212196SJoe Perches
6965ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6966ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6967ef212196SJoe PerchesEOM
6968ef212196SJoe Perches		}
6969d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6970d2c0a235SAndy Whitcroft		# then suggest that.
6971d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6972b0781216SMike Frysinger			$rpt_cleaners = 0;
6973d8469f16SJoe Perches			print << "EOM"
6974d8469f16SJoe Perches
6975d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6976d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6977d8469f16SJoe PerchesEOM
6978d2c0a235SAndy Whitcroft		}
6979d2c0a235SAndy Whitcroft	}
6980d2c0a235SAndy Whitcroft
6981d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6982d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6983d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
69849624b8d6SJoe Perches		my $newfile = $filename;
69859624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
69863705ce5bSJoe Perches		my $linecount = 0;
69873705ce5bSJoe Perches		my $f;
69883705ce5bSJoe Perches
6989d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6990d752fcc8SJoe Perches
69913705ce5bSJoe Perches		open($f, '>', $newfile)
69923705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
69933705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
69943705ce5bSJoe Perches			$linecount++;
69953705ce5bSJoe Perches			if ($file) {
69963705ce5bSJoe Perches				if ($linecount > 3) {
69973705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
69983705ce5bSJoe Perches					print $f $fixed_line . "\n";
69993705ce5bSJoe Perches				}
70003705ce5bSJoe Perches			} else {
70013705ce5bSJoe Perches				print $f $fixed_line . "\n";
70023705ce5bSJoe Perches			}
70033705ce5bSJoe Perches		}
70043705ce5bSJoe Perches		close($f);
70053705ce5bSJoe Perches
70063705ce5bSJoe Perches		if (!$quiet) {
70073705ce5bSJoe Perches			print << "EOM";
7008d8469f16SJoe Perches
70093705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
70103705ce5bSJoe Perches
70113705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
70123705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
70133705ce5bSJoe Perches
70143705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
70153705ce5bSJoe PerchesNo warranties, expressed or implied...
70163705ce5bSJoe PerchesEOM
70173705ce5bSJoe Perches		}
70183705ce5bSJoe Perches	}
70193705ce5bSJoe Perches
7020d8469f16SJoe Perches	if ($quiet == 0) {
7021d8469f16SJoe Perches		print "\n";
7022d8469f16SJoe Perches		if ($clean == 1) {
7023d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7024d8469f16SJoe Perches		} else {
7025d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
70260a920b5bSAndy Whitcroft		}
70270a920b5bSAndy Whitcroft	}
70280a920b5bSAndy Whitcroft	return $clean;
70290a920b5bSAndy Whitcroft}
7030