xref: /linux-6.15/scripts/checkpatch.pl (revision 70eb2275)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0
3882ea1d6SJoe Perches#
4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]>
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftuse strict;
11cb77f0d6SKamil Rytarowskiuse warnings;
12c707a81dSJoe Perchesuse POSIX;
1336061e38SJoe Perchesuse File::Basename;
1436061e38SJoe Perchesuse Cwd 'abs_path';
1557230297SJoe Perchesuse Term::ANSIColor qw(:constants);
16cd261496SGeert Uytterhoevenuse Encode qw(decode encode);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $P = $0;
1936061e38SJoe Perchesmy $D = dirname(abs_path($P));
200a920b5bSAndy Whitcroft
21000d1cc1SJoe Perchesmy $V = '0.32';
220a920b5bSAndy Whitcroft
230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
240a920b5bSAndy Whitcroft
250a920b5bSAndy Whitcroftmy $quiet = 0;
260a920b5bSAndy Whitcroftmy $tree = 1;
270a920b5bSAndy Whitcroftmy $chk_signoff = 1;
280a920b5bSAndy Whitcroftmy $chk_patch = 1;
29773647a0SAndy Whitcroftmy $tst_only;
306c72ffaaSAndy Whitcroftmy $emacs = 0;
318905a67cSAndy Whitcroftmy $terse = 0;
3234d8815fSJoe Perchesmy $showfile = 0;
336c72ffaaSAndy Whitcroftmy $file = 0;
344a593c34SDu, Changbinmy $git = 0;
350dea9f1eSJoe Perchesmy %git_commits = ();
366c72ffaaSAndy Whitcroftmy $check = 0;
372ac73b4fSJoe Perchesmy $check_orig = 0;
388905a67cSAndy Whitcroftmy $summary = 1;
398905a67cSAndy Whitcroftmy $mailback = 0;
4013214adfSAndy Whitcroftmy $summary_file = 0;
41000d1cc1SJoe Perchesmy $show_types = 0;
423beb42ecSJoe Perchesmy $list_types = 0;
433705ce5bSJoe Perchesmy $fix = 0;
449624b8d6SJoe Perchesmy $fix_inplace = 0;
456c72ffaaSAndy Whitcroftmy $root;
460f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'};
470f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot);
48c2fdda0dSAndy Whitcroftmy %debug;
493445686aSJoe Perchesmy %camelcase = ();
5091bfe484SJoe Perchesmy %use_type = ();
5191bfe484SJoe Perchesmy @use = ();
5291bfe484SJoe Perchesmy %ignore_type = ();
53000d1cc1SJoe Perchesmy @ignore = ();
5477f5b10aSHannes Edermy $help = 0;
55000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
56bdc48fa1SJoe Perchesmy $max_line_length = 100;
57d62a201fSDave Hansenmy $ignore_perl_version = 0;
58d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5956193274SVadim Bendeburymy $min_conf_desc_length = 4;
6066b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
61ebfd7d62SJoe Perchesmy $codespell = 0;
62f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
63bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
64ced69da1SQuentin Monnetmy $typedefsfile;
65737c0767SJohn Brooksmy $color = "auto";
6698005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
67dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
68dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
69713a09deSAntonio Borneomy $tabsize = 8;
703e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_";
7177f5b10aSHannes Eder
7277f5b10aSHannes Edersub help {
7377f5b10aSHannes Eder	my ($exitcode) = @_;
7477f5b10aSHannes Eder
7577f5b10aSHannes Eder	print << "EOM";
7677f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7777f5b10aSHannes EderVersion: $V
7877f5b10aSHannes Eder
7977f5b10aSHannes EderOptions:
8077f5b10aSHannes Eder  -q, --quiet                quiet
8177f5b10aSHannes Eder  --no-tree                  run without a kernel tree
8277f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
8377f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
8477f5b10aSHannes Eder  --emacs                    emacs compile window format
8577f5b10aSHannes Eder  --terse                    one line per report
8634d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
874a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
884a593c34SDu, Changbin                             single git commit with:
894a593c34SDu, Changbin                               <rev>
904a593c34SDu, Changbin                               <rev>^
914a593c34SDu, Changbin                               <rev>~n
924a593c34SDu, Changbin                             multiple git commits with:
934a593c34SDu, Changbin                               <rev1>..<rev2>
944a593c34SDu, Changbin                               <rev1>...<rev2>
954a593c34SDu, Changbin                               <rev>-<count>
964a593c34SDu, Changbin                             git merges are ignored
9777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
993beb42ecSJoe Perches  --list-types               list the possible message types
10091bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
101000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
1023beb42ecSJoe Perches  --show-types               show the specific message type in the output
103bdc48fa1SJoe Perches  --max-line-length=n        set the maximum line length, (default $max_line_length)
104bdc48fa1SJoe Perches                             if exceeded, warn on patches
105bdc48fa1SJoe Perches                             requires --strict for use with --file
10656193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
107bdc48fa1SJoe Perches  --tab-size=n               set the number of spaces for tab (default $tabsize)
10877f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10977f5b10aSHannes Eder  --no-summary               suppress the per-file summary
11077f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
11177f5b10aSHannes Eder  --summary-file             include the filename in summary
11277f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
11377f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
11477f5b10aSHannes Eder                             is all off)
11577f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
11677f5b10aSHannes Eder                             literally
1173705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1183705ce5bSJoe Perches                             If correctable single-line errors exist, create
1193705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1203705ce5bSJoe Perches                             with potential errors corrected to the preferred
1213705ce5bSJoe Perches                             checkpatch style
1229624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1239624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1249624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
125d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
126d62a201fSDave Hansen                             runtime errors.
127ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
128f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
129ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
13075ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
131737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
132737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
1333e89ad85SJerome Forissier  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
1343e89ad85SJerome Forissier                             ${CONFIG_})
13577f5b10aSHannes Eder  -h, --help, --version      display this help and exit
13677f5b10aSHannes Eder
13777f5b10aSHannes EderWhen FILE is - read standard input.
13877f5b10aSHannes EderEOM
13977f5b10aSHannes Eder
14077f5b10aSHannes Eder	exit($exitcode);
14177f5b10aSHannes Eder}
14277f5b10aSHannes Eder
1433beb42ecSJoe Perchessub uniq {
1443beb42ecSJoe Perches	my %seen;
1453beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1463beb42ecSJoe Perches}
1473beb42ecSJoe Perches
1483beb42ecSJoe Perchessub list_types {
1493beb42ecSJoe Perches	my ($exitcode) = @_;
1503beb42ecSJoe Perches
1513beb42ecSJoe Perches	my $count = 0;
1523beb42ecSJoe Perches
1533beb42ecSJoe Perches	local $/ = undef;
1543beb42ecSJoe Perches
1553beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1563beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1573beb42ecSJoe Perches
1583beb42ecSJoe Perches	my $text = <$script>;
1593beb42ecSJoe Perches	close($script);
1603beb42ecSJoe Perches
1613beb42ecSJoe Perches	my @types = ();
1620547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1630547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1643beb42ecSJoe Perches		push (@types, $_);
1653beb42ecSJoe Perches	}
1663beb42ecSJoe Perches	@types = sort(uniq(@types));
1673beb42ecSJoe Perches	print("#\tMessage type\n\n");
1683beb42ecSJoe Perches	foreach my $type (@types) {
1693beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1703beb42ecSJoe Perches	}
1713beb42ecSJoe Perches
1723beb42ecSJoe Perches	exit($exitcode);
1733beb42ecSJoe Perches}
1743beb42ecSJoe Perches
175000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
176000d1cc1SJoe Perchesif (-f $conf) {
177000d1cc1SJoe Perches	my @conf_args;
178000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
179000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
180000d1cc1SJoe Perches
181000d1cc1SJoe Perches	while (<$conffile>) {
182000d1cc1SJoe Perches		my $line = $_;
183000d1cc1SJoe Perches
184000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
185000d1cc1SJoe Perches		$line =~ s/^\s*//g;
186000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
187000d1cc1SJoe Perches
188000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
189000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
190000d1cc1SJoe Perches
191000d1cc1SJoe Perches		my @words = split(" ", $line);
192000d1cc1SJoe Perches		foreach my $word (@words) {
193000d1cc1SJoe Perches			last if ($word =~ m/^#/);
194000d1cc1SJoe Perches			push (@conf_args, $word);
195000d1cc1SJoe Perches		}
196000d1cc1SJoe Perches	}
197000d1cc1SJoe Perches	close($conffile);
198000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
199000d1cc1SJoe Perches}
200000d1cc1SJoe Perches
201737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
202737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
203737c0767SJohn Brooksforeach (@ARGV) {
204737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
205737c0767SJohn Brooks		$_ = "--color=$color";
206737c0767SJohn Brooks	}
207737c0767SJohn Brooks}
208737c0767SJohn Brooks
2090a920b5bSAndy WhitcroftGetOptions(
2106c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2110a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2120a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2130a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2146c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2158905a67cSAndy Whitcroft	'terse!'	=> \$terse,
21634d8815fSJoe Perches	'showfile!'	=> \$showfile,
21777f5b10aSHannes Eder	'f|file!'	=> \$file,
2184a593c34SDu, Changbin	'g|git!'	=> \$git,
2196c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2206c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
221000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
22291bfe484SJoe Perches	'types=s'	=> \@use,
223000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2243beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2256cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
22656193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
227713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
2286c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2298905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2308905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
23113214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2323705ce5bSJoe Perches	'fix!'		=> \$fix,
2339624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
234d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
235c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
236773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
237ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
238ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
23975ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
240737c0767SJohn Brooks	'color=s'	=> \$color,
241737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
242737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
2433e89ad85SJerome Forissier	'kconfig-prefix=s'	=> \${CONFIG_},
24477f5b10aSHannes Eder	'h|help'	=> \$help,
24577f5b10aSHannes Eder	'version'	=> \$help
24677f5b10aSHannes Eder) or help(1);
24777f5b10aSHannes Eder
24877f5b10aSHannes Ederhelp(0) if ($help);
2490a920b5bSAndy Whitcroft
2503beb42ecSJoe Percheslist_types(0) if ($list_types);
2513beb42ecSJoe Perches
2529624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2532ac73b4fSJoe Perches$check_orig = $check;
2549624b8d6SJoe Perches
25532f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
25632f30ca9SJoe Perches
2570a920b5bSAndy Whitcroftmy $exit = 0;
2580a920b5bSAndy Whitcroft
2595b57980dSJoe Perchesmy $perl_version_ok = 1;
260d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2615b57980dSJoe Perches	$perl_version_ok = 0;
262d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2635b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
264d62a201fSDave Hansen}
265d62a201fSDave Hansen
26645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2670a920b5bSAndy Whitcroftif ($#ARGV < 0) {
26845107ff6SAllen Hubbe	push(@ARGV, '-');
2690a920b5bSAndy Whitcroft}
2700a920b5bSAndy Whitcroft
271737c0767SJohn Brooksif ($color =~ /^[01]$/) {
272737c0767SJohn Brooks	$color = !$color;
273737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
274737c0767SJohn Brooks	$color = 1;
275737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
276737c0767SJohn Brooks	$color = 0;
277737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
278737c0767SJohn Brooks	$color = (-t STDOUT);
279737c0767SJohn Brooks} else {
28032f30ca9SJoe Perches	die "$P: Invalid color mode: $color\n";
281737c0767SJohn Brooks}
282737c0767SJohn Brooks
283713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
28432f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
285713a09deSAntonio Borneo
28691bfe484SJoe Perchessub hash_save_array_words {
28791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
28891bfe484SJoe Perches
28991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
29091bfe484SJoe Perches	foreach my $word (@array) {
291000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
292000d1cc1SJoe Perches		$word =~ s/^\s*//g;
293000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
294000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
295000d1cc1SJoe Perches
296000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
297000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
298000d1cc1SJoe Perches
29991bfe484SJoe Perches		$hashRef->{$word}++;
300000d1cc1SJoe Perches	}
30191bfe484SJoe Perches}
30291bfe484SJoe Perches
30391bfe484SJoe Perchessub hash_show_words {
30491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
30591bfe484SJoe Perches
3063c816e49SJoe Perches	if (keys %$hashRef) {
307d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
30858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
30991bfe484SJoe Perches			print " $word";
31091bfe484SJoe Perches		}
311d8469f16SJoe Perches		print "\n";
31291bfe484SJoe Perches	}
31391bfe484SJoe Perches}
31491bfe484SJoe Perches
31591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
31691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
317000d1cc1SJoe Perches
318c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
319c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3207429c690SAndy Whitcroftmy $dbg_type = 0;
321a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
322c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
32321caa13cSAndy Whitcroft	## no critic
32421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
32521caa13cSAndy Whitcroft	die "$@" if ($@);
326c2fdda0dSAndy Whitcroft}
327c2fdda0dSAndy Whitcroft
328d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
329d2c0a235SAndy Whitcroft
3308905a67cSAndy Whitcroftif ($terse) {
3318905a67cSAndy Whitcroft	$emacs = 1;
3328905a67cSAndy Whitcroft	$quiet++;
3338905a67cSAndy Whitcroft}
3348905a67cSAndy Whitcroft
3356c72ffaaSAndy Whitcroftif ($tree) {
3366c72ffaaSAndy Whitcroft	if (defined $root) {
3376c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3386c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3396c72ffaaSAndy Whitcroft		}
3406c72ffaaSAndy Whitcroft	} else {
3416c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3426c72ffaaSAndy Whitcroft			$root = '.';
3436c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3446c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3456c72ffaaSAndy Whitcroft			$root = $1;
3466c72ffaaSAndy Whitcroft		}
3476c72ffaaSAndy Whitcroft	}
3486c72ffaaSAndy Whitcroft
3496c72ffaaSAndy Whitcroft	if (!defined $root) {
3500a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3510a920b5bSAndy Whitcroft		exit(2);
3520a920b5bSAndy Whitcroft	}
3536c72ffaaSAndy Whitcroft}
3546c72ffaaSAndy Whitcroft
3556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3566c72ffaaSAndy Whitcroft
3572ceb532bSAndy Whitcroftour $Ident	= qr{
3582ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3592ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3602ceb532bSAndy Whitcroft		}x;
3616c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3626c72ffaaSAndy Whitcroftour $Sparse	= qr{
3636c72ffaaSAndy Whitcroft			__user|
3646c72ffaaSAndy Whitcroft			__kernel|
3656c72ffaaSAndy Whitcroft			__force|
3666c72ffaaSAndy Whitcroft			__iomem|
3676c72ffaaSAndy Whitcroft			__must_check|
368417495edSAndy Whitcroft			__kprobes|
369165e72a6SSven Eckelmann			__ref|
37033aa4597SGeert Uytterhoeven			__refconst|
37133aa4597SGeert Uytterhoeven			__refdata|
372ad315455SBoqun Feng			__rcu|
373ad315455SBoqun Feng			__private
3746c72ffaaSAndy Whitcroft		}x;
375e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3808716de38SJoe Perches
38152131292SWolfram Sang# Notes to $Attribute:
38252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3836c72ffaaSAndy Whitcroftour $Attribute	= qr{
3846c72ffaaSAndy Whitcroft			const|
38503f1df7dSJoe Perches			__percpu|
38603f1df7dSJoe Perches			__nocast|
38703f1df7dSJoe Perches			__safe|
38846d832f5SMichael S. Tsirkin			__bitwise|
38903f1df7dSJoe Perches			__packed__|
39003f1df7dSJoe Perches			__packed2__|
39103f1df7dSJoe Perches			__naked|
39203f1df7dSJoe Perches			__maybe_unused|
39303f1df7dSJoe Perches			__always_unused|
39403f1df7dSJoe Perches			__noreturn|
39503f1df7dSJoe Perches			__used|
39603f1df7dSJoe Perches			__cold|
397e23ef1f3SJoe Perches			__pure|
39803f1df7dSJoe Perches			__noclone|
39903f1df7dSJoe Perches			__deprecated|
4006c72ffaaSAndy Whitcroft			__read_mostly|
401c5967e98SJoe Perches			__ro_after_init|
4026c72ffaaSAndy Whitcroft			__kprobes|
4038716de38SJoe Perches			$InitAttribute|
40424e1d81aSAndy Whitcroft			____cacheline_aligned|
40524e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
4065fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
4075fe3af11SAndy Whitcroft			__weak
4086c72ffaaSAndy Whitcroft		  }x;
409c45dcabdSAndy Whitcroftour $Modifier;
41091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
4116c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
4126c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
4136c72ffaaSAndy Whitcroft
41495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
41595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
41695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
41795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4182435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
419c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
420326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
421326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
422326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
42374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4242435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
425326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
426447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
42723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4286c72ffaaSAndy Whitcroftour $Operators	= qr{
4296c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4306c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
43123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4326c72ffaaSAndy Whitcroft		  }x;
4336c72ffaaSAndy Whitcroft
43491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
43591cb5195SJoe Perches
436ab7e23f3SJoe Perchesour $BasicType;
4378905a67cSAndy Whitcroftour $NonptrType;
4381813087dSJoe Perchesour $NonptrTypeMisordered;
4398716de38SJoe Perchesour $NonptrTypeWithAttr;
4408905a67cSAndy Whitcroftour $Type;
4411813087dSJoe Perchesour $TypeMisordered;
4428905a67cSAndy Whitcroftour $Declare;
4431813087dSJoe Perchesour $DeclareMisordered;
4448905a67cSAndy Whitcroft
44515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
44615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
447171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
448171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
449171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
450171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
451171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
452171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
453171ae1a4SAndy Whitcroft}x;
454171ae1a4SAndy Whitcroft
45515662b3eSJoe Perchesour $UTF8	= qr{
45615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
45715662b3eSJoe Perches	| $NON_ASCII_UTF8
45815662b3eSJoe Perches}x;
45915662b3eSJoe Perches
460e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
461021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
462021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
463021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
464021158b4SJoe Perches)};
465e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
466fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4678ed22cadSAndy Whitcroft	atomic_t
4688ed22cadSAndy Whitcroft)};
469e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
470e6176fa4SJoe Perches	$typeC99Typedefs\b|
471e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
472e6176fa4SJoe Perches	$typeKernelTypedefs\b
473e6176fa4SJoe Perches)};
4748ed22cadSAndy Whitcroft
4756d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4766d32f7a3SJoe Perches
477691e669bSJoe Perchesour $logFunctions = qr{(?x:
478758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4797d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
48087bd499aSJoe Perches	TP_printk|
4816e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
482b0531722SJoe Perches	panic|
48306668727SJoe Perches	MODULE_[A-Z_]+|
48406668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
485691e669bSJoe Perches)};
486691e669bSJoe Perches
487e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
488e29a70f1SJoe Perches	(?:(?:devm_)?
489e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
490e29a70f1SJoe Perches		kstrdup(?:_const)? |
491e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
492461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
493e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
494e29a70f1SJoe Perches	dma_alloc_coherent
495e29a70f1SJoe Perches)};
496e29a70f1SJoe Perches
49720112475SJoe Perchesour $signature_tags = qr{(?xi:
49820112475SJoe Perches	Signed-off-by:|
499d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
50020112475SJoe Perches	Acked-by:|
50120112475SJoe Perches	Tested-by:|
50220112475SJoe Perches	Reviewed-by:|
50320112475SJoe Perches	Reported-by:|
5048543ae12SMugunthan V N	Suggested-by:|
50520112475SJoe Perches	To:|
50620112475SJoe Perches	Cc:
50720112475SJoe Perches)};
50820112475SJoe Perches
509831242abSAditya Srivastavasub edit_distance_min {
510831242abSAditya Srivastava	my (@arr) = @_;
511831242abSAditya Srivastava	my $len = scalar @arr;
512831242abSAditya Srivastava	if ((scalar @arr) < 1) {
513831242abSAditya Srivastava		# if underflow, return
514831242abSAditya Srivastava		return;
515831242abSAditya Srivastava	}
516831242abSAditya Srivastava	my $min = $arr[0];
517831242abSAditya Srivastava	for my $i (0 .. ($len-1)) {
518831242abSAditya Srivastava		if ($arr[$i] < $min) {
519831242abSAditya Srivastava			$min = $arr[$i];
520831242abSAditya Srivastava		}
521831242abSAditya Srivastava	}
522831242abSAditya Srivastava	return $min;
523831242abSAditya Srivastava}
524831242abSAditya Srivastava
525831242abSAditya Srivastavasub get_edit_distance {
526831242abSAditya Srivastava	my ($str1, $str2) = @_;
527831242abSAditya Srivastava	$str1 = lc($str1);
528831242abSAditya Srivastava	$str2 = lc($str2);
529831242abSAditya Srivastava	$str1 =~ s/-//g;
530831242abSAditya Srivastava	$str2 =~ s/-//g;
531831242abSAditya Srivastava	my $len1 = length($str1);
532831242abSAditya Srivastava	my $len2 = length($str2);
533831242abSAditya Srivastava	# two dimensional array storing minimum edit distance
534831242abSAditya Srivastava	my @distance;
535831242abSAditya Srivastava	for my $i (0 .. $len1) {
536831242abSAditya Srivastava		for my $j (0 .. $len2) {
537831242abSAditya Srivastava			if ($i == 0) {
538831242abSAditya Srivastava				$distance[$i][$j] = $j;
539831242abSAditya Srivastava			} elsif ($j == 0) {
540831242abSAditya Srivastava				$distance[$i][$j] = $i;
541831242abSAditya Srivastava			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
542831242abSAditya Srivastava				$distance[$i][$j] = $distance[$i - 1][$j - 1];
543831242abSAditya Srivastava			} else {
544831242abSAditya Srivastava				my $dist1 = $distance[$i][$j - 1]; #insert distance
545831242abSAditya Srivastava				my $dist2 = $distance[$i - 1][$j]; # remove
546831242abSAditya Srivastava				my $dist3 = $distance[$i - 1][$j - 1]; #replace
547831242abSAditya Srivastava				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
548831242abSAditya Srivastava			}
549831242abSAditya Srivastava		}
550831242abSAditya Srivastava	}
551831242abSAditya Srivastava	return $distance[$len1][$len2];
552831242abSAditya Srivastava}
553831242abSAditya Srivastava
554831242abSAditya Srivastavasub find_standard_signature {
555831242abSAditya Srivastava	my ($sign_off) = @_;
556831242abSAditya Srivastava	my @standard_signature_tags = (
557831242abSAditya Srivastava		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
558831242abSAditya Srivastava		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
559831242abSAditya Srivastava	);
560831242abSAditya Srivastava	foreach my $signature (@standard_signature_tags) {
561831242abSAditya Srivastava		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
562831242abSAditya Srivastava	}
563831242abSAditya Srivastava
564831242abSAditya Srivastava	return "";
565831242abSAditya Srivastava}
566831242abSAditya Srivastava
5671813087dSJoe Perchesour @typeListMisordered = (
5681813087dSJoe Perches	qr{char\s+(?:un)?signed},
5691813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
5701813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
5711813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
5721813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
5731813087dSJoe Perches	qr{short\s+(?:un)?signed},
5741813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5751813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5761813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5771813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5781813087dSJoe Perches	qr{int\s+(?:un)?signed},
5791813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5801813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5811813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5821813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5831813087dSJoe Perches	qr{long\s+(?:un)?signed},
5841813087dSJoe Perches);
5851813087dSJoe Perches
5868905a67cSAndy Whitcroftour @typeList = (
5878905a67cSAndy Whitcroft	qr{void},
5880c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5890c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5900c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5910c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5920c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5930c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5940c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5950c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5960c773d9dSJoe Perches	qr{(?:un)?signed},
5978905a67cSAndy Whitcroft	qr{float},
5988905a67cSAndy Whitcroft	qr{double},
5998905a67cSAndy Whitcroft	qr{bool},
6008905a67cSAndy Whitcroft	qr{struct\s+$Ident},
6018905a67cSAndy Whitcroft	qr{union\s+$Ident},
6028905a67cSAndy Whitcroft	qr{enum\s+$Ident},
6038905a67cSAndy Whitcroft	qr{${Ident}_t},
6048905a67cSAndy Whitcroft	qr{${Ident}_handler},
6058905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
6061813087dSJoe Perches	@typeListMisordered,
6078905a67cSAndy Whitcroft);
608938224b5SJoe Perches
609938224b5SJoe Perchesour $C90_int_types = qr{(?x:
610938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
611938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
612938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
613938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
614938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
615938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
616938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
617938224b5SJoe Perches
618938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
619938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
620938224b5SJoe Perches	long\s+(?:un)?signed|
621938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
622938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
623938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
624938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
625938224b5SJoe Perches
626938224b5SJoe Perches	int\s+(?:un)?signed|
627938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
628938224b5SJoe Perches)};
629938224b5SJoe Perches
630485ff23eSAlex Dowadour @typeListFile = ();
6318716de38SJoe Perchesour @typeListWithAttr = (
6328716de38SJoe Perches	@typeList,
6338716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
6348716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
6358716de38SJoe Perches);
6368716de38SJoe Perches
637c45dcabdSAndy Whitcroftour @modifierList = (
638c45dcabdSAndy Whitcroft	qr{fastcall},
639c45dcabdSAndy Whitcroft);
640485ff23eSAlex Dowadour @modifierListFile = ();
6418905a67cSAndy Whitcroft
6422435880fSJoe Perchesour @mode_permission_funcs = (
6432435880fSJoe Perches	["module_param", 3],
6442435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
6452435880fSJoe Perches	["module_param_array_named", 5],
6462435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
6472435880fSJoe Perches	["proc_create(?:_data|)", 2],
648459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
649459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
650459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
651459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
652459cf0aeSJoe Perches	["__ATTR", 2],
6532435880fSJoe Perches);
6542435880fSJoe Perches
6551a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
6561a3dcf2eSJoe Perches
657515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
658515a235eSJoe Perchesour $mode_perms_search = "";
659515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
660515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
661515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
662515a235eSJoe Perches}
66300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
664515a235eSJoe Perches
6659189c7e7SJoe Perchesour %deprecated_apis = (
6669189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
6679189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
6689189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
6699189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
6709189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
6719189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
6729189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
6739189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
6749189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
6759189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6769189c7e7SJoe Perches);
6779189c7e7SJoe Perches
6789189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6799189c7e7SJoe Perchesour $deprecated_apis_search = "";
6809189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6819189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6829189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6839189c7e7SJoe Perches}
6849189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6859189c7e7SJoe Perches
686b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
687b392c64fSJoe Perches	S_IWUGO		|
688b392c64fSJoe Perches	S_IWOTH		|
689b392c64fSJoe Perches	S_IRWXUGO	|
690b392c64fSJoe Perches	S_IALLUGO	|
691b392c64fSJoe Perches	0[0-7][0-7][2367]
692b392c64fSJoe Perches}x;
693b392c64fSJoe Perches
694f90774e1SJoe Perchesour %mode_permission_string_types = (
695f90774e1SJoe Perches	"S_IRWXU" => 0700,
696f90774e1SJoe Perches	"S_IRUSR" => 0400,
697f90774e1SJoe Perches	"S_IWUSR" => 0200,
698f90774e1SJoe Perches	"S_IXUSR" => 0100,
699f90774e1SJoe Perches	"S_IRWXG" => 0070,
700f90774e1SJoe Perches	"S_IRGRP" => 0040,
701f90774e1SJoe Perches	"S_IWGRP" => 0020,
702f90774e1SJoe Perches	"S_IXGRP" => 0010,
703f90774e1SJoe Perches	"S_IRWXO" => 0007,
704f90774e1SJoe Perches	"S_IROTH" => 0004,
705f90774e1SJoe Perches	"S_IWOTH" => 0002,
706f90774e1SJoe Perches	"S_IXOTH" => 0001,
707f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
708f90774e1SJoe Perches	"S_IRUGO" => 0444,
709f90774e1SJoe Perches	"S_IWUGO" => 0222,
710f90774e1SJoe Perches	"S_IXUGO" => 0111,
711f90774e1SJoe Perches);
712f90774e1SJoe Perches
713f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
714f90774e1SJoe Perchesour $mode_perms_string_search = "";
715f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
716f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
717f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
718f90774e1SJoe Perches}
71900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
72000180468SJoe Perchesour $multi_mode_perms_string_search = qr{
72100180468SJoe Perches	${single_mode_perms_string_search}
72200180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
72300180468SJoe Perches}x;
72400180468SJoe Perches
72500180468SJoe Perchessub perms_to_octal {
72600180468SJoe Perches	my ($string) = @_;
72700180468SJoe Perches
72800180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
72900180468SJoe Perches
73000180468SJoe Perches	my $val = "";
73100180468SJoe Perches	my $oval = "";
73200180468SJoe Perches	my $to = 0;
73300180468SJoe Perches	my $curpos = 0;
73400180468SJoe Perches	my $lastpos = 0;
73500180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
73600180468SJoe Perches		$curpos = pos($string);
73700180468SJoe Perches		my $match = $2;
73800180468SJoe Perches		my $omatch = $1;
73900180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
74000180468SJoe Perches		$lastpos = $curpos;
74100180468SJoe Perches		$to |= $mode_permission_string_types{$match};
74200180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
74300180468SJoe Perches		$val .= $match;
74400180468SJoe Perches		$oval .= $omatch;
74500180468SJoe Perches	}
74600180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
74700180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
74800180468SJoe Perches	return sprintf("%04o", $to);
74900180468SJoe Perches}
750f90774e1SJoe Perches
7517840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
7527840a94cSWolfram Sang	irq|
753cdcee686SSergey Ryazanov	memory|
754cdcee686SSergey Ryazanov	time|
755cdcee686SSergey Ryazanov	reboot
7567840a94cSWolfram Sang)};
7577840a94cSWolfram Sang# memory.h: ARM has a custom one
7587840a94cSWolfram Sang
75966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
76066b47b4aSKees Cookmy $misspellings;
76166b47b4aSKees Cookmy %spelling_fix;
76236061e38SJoe Perches
76336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
76466b47b4aSKees Cook	while (<$spelling>) {
76566b47b4aSKees Cook		my $line = $_;
76666b47b4aSKees Cook
76766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
76866b47b4aSKees Cook		$line =~ s/^\s*//g;
76966b47b4aSKees Cook
77066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
77166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
77266b47b4aSKees Cook
77366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
77466b47b4aSKees Cook
77566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
77666b47b4aSKees Cook	}
77766b47b4aSKees Cook	close($spelling);
77836061e38SJoe Perches} else {
77936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
78036061e38SJoe Perches}
78166b47b4aSKees Cook
782ebfd7d62SJoe Perchesif ($codespell) {
783ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
784ebfd7d62SJoe Perches		while (<$spelling>) {
785ebfd7d62SJoe Perches			my $line = $_;
786ebfd7d62SJoe Perches
787ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
788ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
789ebfd7d62SJoe Perches
790ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
791ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
792ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
793ebfd7d62SJoe Perches
794ebfd7d62SJoe Perches			$line =~ s/,.*$//;
795ebfd7d62SJoe Perches
796ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
797ebfd7d62SJoe Perches
798ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
799ebfd7d62SJoe Perches		}
800ebfd7d62SJoe Perches		close($spelling);
801ebfd7d62SJoe Perches	} else {
802ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
803ebfd7d62SJoe Perches	}
804ebfd7d62SJoe Perches}
805ebfd7d62SJoe Perches
806ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
807ebfd7d62SJoe Perches
80875ad8c57SJerome Forissiersub read_words {
80975ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
81075ad8c57SJerome Forissier
81175ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
81275ad8c57SJerome Forissier		while (<$words>) {
813bf1fa1daSJoe Perches			my $line = $_;
814bf1fa1daSJoe Perches
815bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
816bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
817bf1fa1daSJoe Perches
818bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
819bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
820bf1fa1daSJoe Perches			if ($line =~ /\s/) {
82175ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
822bf1fa1daSJoe Perches				next;
823bf1fa1daSJoe Perches			}
824bf1fa1daSJoe Perches
825ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
82675ad8c57SJerome Forissier			$$wordsRef .= $line;
827bf1fa1daSJoe Perches		}
82875ad8c57SJerome Forissier		close($file);
82975ad8c57SJerome Forissier		return 1;
830bf1fa1daSJoe Perches	}
831bf1fa1daSJoe Perches
83275ad8c57SJerome Forissier	return 0;
83375ad8c57SJerome Forissier}
83475ad8c57SJerome Forissier
835ced69da1SQuentin Monnetmy $const_structs;
836ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
83775ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
83875ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
839ced69da1SQuentin Monnet}
84075ad8c57SJerome Forissier
841ced69da1SQuentin Monnetif (defined($typedefsfile)) {
842ced69da1SQuentin Monnet	my $typeOtherTypedefs;
84375ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
84475ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
845ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
84675ad8c57SJerome Forissier}
84775ad8c57SJerome Forissier
8488905a67cSAndy Whitcroftsub build_types {
849485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
850485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
8511813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
8528716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
853c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
854ab7e23f3SJoe Perches	$BasicType	= qr{
855ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
856ab7e23f3SJoe Perches				(?:${all}\b)
857ab7e23f3SJoe Perches		}x;
8588905a67cSAndy Whitcroft	$NonptrType	= qr{
859d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
860cf655043SAndy Whitcroft			(?:
8616b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
8628ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
863c45dcabdSAndy Whitcroft				(?:${all}\b)
864cf655043SAndy Whitcroft			)
865c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
8668905a67cSAndy Whitcroft		  }x;
8671813087dSJoe Perches	$NonptrTypeMisordered	= qr{
8681813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
8691813087dSJoe Perches			(?:
8701813087dSJoe Perches				(?:${Misordered}\b)
8711813087dSJoe Perches			)
8721813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
8731813087dSJoe Perches		  }x;
8748716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
8758716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
8768716de38SJoe Perches			(?:
8778716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8788716de38SJoe Perches				(?:$typeTypedefs\b)|
8798716de38SJoe Perches				(?:${allWithAttr}\b)
8808716de38SJoe Perches			)
8818716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8828716de38SJoe Perches		  }x;
8838905a67cSAndy Whitcroft	$Type	= qr{
884c45dcabdSAndy Whitcroft			$NonptrType
8857b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
886c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8878905a67cSAndy Whitcroft		  }x;
8881813087dSJoe Perches	$TypeMisordered	= qr{
8891813087dSJoe Perches			$NonptrTypeMisordered
8907b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
8911813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8921813087dSJoe Perches		  }x;
89391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8941813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8958905a67cSAndy Whitcroft}
8968905a67cSAndy Whitcroftbuild_types();
8976c72ffaaSAndy Whitcroft
8987d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
899d1fe9c09SJoe Perches
900d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
901d1fe9c09SJoe Perches# requires at least perl version v5.10.0
902d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
903d1fe9c09SJoe Perches
904d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
9052435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
906c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
9077d2367afSJoe Perches
908f8422308SJoe Perchesour $declaration_macros = qr{(?x:
9093e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
910fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
9113d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
912f8422308SJoe Perches)};
913f8422308SJoe Perches
9148d0325ccSAditya Srivastavaour %allow_repeated_words = (
9158d0325ccSAditya Srivastava	add => '',
9168d0325ccSAditya Srivastava	added => '',
9178d0325ccSAditya Srivastava	bad => '',
9188d0325ccSAditya Srivastava	be => '',
9198d0325ccSAditya Srivastava);
9208d0325ccSAditya Srivastava
9217d2367afSJoe Perchessub deparenthesize {
9227d2367afSJoe Perches	my ($string) = @_;
9237d2367afSJoe Perches	return "" if (!defined($string));
9245b9553abSJoe Perches
9255b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
9265b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
9275b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
9285b9553abSJoe Perches	}
9295b9553abSJoe Perches
9307d2367afSJoe Perches	$string =~ s@\s+@ @g;
9315b9553abSJoe Perches
9327d2367afSJoe Perches	return $string;
9337d2367afSJoe Perches}
9347d2367afSJoe Perches
9353445686aSJoe Perchessub seed_camelcase_file {
9363445686aSJoe Perches	my ($file) = @_;
9373445686aSJoe Perches
9383445686aSJoe Perches	return if (!(-f $file));
9393445686aSJoe Perches
9403445686aSJoe Perches	local $/;
9413445686aSJoe Perches
9423445686aSJoe Perches	open(my $include_file, '<', "$file")
9433445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
9443445686aSJoe Perches	my $text = <$include_file>;
9453445686aSJoe Perches	close($include_file);
9463445686aSJoe Perches
9473445686aSJoe Perches	my @lines = split('\n', $text);
9483445686aSJoe Perches
9493445686aSJoe Perches	foreach my $line (@lines) {
9503445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
9513445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
9523445686aSJoe Perches			$camelcase{$1} = 1;
95311ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
95411ea516aSJoe Perches			$camelcase{$1} = 1;
95511ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
9563445686aSJoe Perches			$camelcase{$1} = 1;
9573445686aSJoe Perches		}
9583445686aSJoe Perches	}
9593445686aSJoe Perches}
9603445686aSJoe Perches
961cd28b119SJoe Perchesour %maintained_status = ();
962cd28b119SJoe Perches
96385b0ee18SJoe Perchessub is_maintained_obsolete {
96485b0ee18SJoe Perches	my ($filename) = @_;
96585b0ee18SJoe Perches
966f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
96785b0ee18SJoe Perches
968cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
969cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
970cd28b119SJoe Perches	}
97185b0ee18SJoe Perches
972cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
97385b0ee18SJoe Perches}
97485b0ee18SJoe Perches
9753b6e8ac9SJoe Perchessub is_SPDX_License_valid {
9763b6e8ac9SJoe Perches	my ($license) = @_;
9773b6e8ac9SJoe Perches
9780f7f635bSJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
9793b6e8ac9SJoe Perches
98056294112SJoe Perches	my $root_path = abs_path($root);
98156294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
9823b6e8ac9SJoe Perches	return 0 if ($status ne "");
9833b6e8ac9SJoe Perches	return 1;
9843b6e8ac9SJoe Perches}
9853b6e8ac9SJoe Perches
9863445686aSJoe Perchesmy $camelcase_seeded = 0;
9873445686aSJoe Perchessub seed_camelcase_includes {
9883445686aSJoe Perches	return if ($camelcase_seeded);
9893445686aSJoe Perches
9903445686aSJoe Perches	my $files;
991c707a81dSJoe Perches	my $camelcase_cache = "";
992c707a81dSJoe Perches	my @include_files = ();
993c707a81dSJoe Perches
994c707a81dSJoe Perches	$camelcase_seeded = 1;
995351b2a1fSJoe Perches
9960f7f635bSJoe Perches	if (-e "$gitroot") {
997dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
998351b2a1fSJoe Perches		chomp $git_last_include_commit;
999c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1000c707a81dSJoe Perches	} else {
1001c707a81dSJoe Perches		my $last_mod_date = 0;
1002c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
1003c707a81dSJoe Perches		@include_files = split('\n', $files);
1004c707a81dSJoe Perches		foreach my $file (@include_files) {
1005c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
1006c707a81dSJoe Perches						   localtime((stat $file)[9]));
1007c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
1008c707a81dSJoe Perches		}
1009c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1010c707a81dSJoe Perches	}
1011c707a81dSJoe Perches
1012c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
1013c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
1014c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
1015351b2a1fSJoe Perches		while (<$camelcase_file>) {
1016351b2a1fSJoe Perches			chomp;
1017351b2a1fSJoe Perches			$camelcase{$_} = 1;
1018351b2a1fSJoe Perches		}
1019351b2a1fSJoe Perches		close($camelcase_file);
1020351b2a1fSJoe Perches
1021351b2a1fSJoe Perches		return;
1022351b2a1fSJoe Perches	}
1023c707a81dSJoe Perches
10240f7f635bSJoe Perches	if (-e "$gitroot") {
1025dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
1026c707a81dSJoe Perches		@include_files = split('\n', $files);
10273445686aSJoe Perches	}
1028c707a81dSJoe Perches
10293445686aSJoe Perches	foreach my $file (@include_files) {
10303445686aSJoe Perches		seed_camelcase_file($file);
10313445686aSJoe Perches	}
1032351b2a1fSJoe Perches
1033c707a81dSJoe Perches	if ($camelcase_cache ne "") {
1034351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
1035c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
1036c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
1037351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1038351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
1039351b2a1fSJoe Perches		}
1040351b2a1fSJoe Perches		close($camelcase_file);
1041351b2a1fSJoe Perches	}
10423445686aSJoe Perches}
10433445686aSJoe Perches
1044f5f61325SJoe Perchessub git_is_single_file {
1045f5f61325SJoe Perches	my ($filename) = @_;
1046f5f61325SJoe Perches
1047f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1048f5f61325SJoe Perches
1049f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1050f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
1051f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
1052f5f61325SJoe Perches}
1053f5f61325SJoe Perches
1054d311cd44SJoe Perchessub git_commit_info {
1055d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
1056d311cd44SJoe Perches
10570f7f635bSJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1058d311cd44SJoe Perches
1059dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1060d311cd44SJoe Perches	$output =~ s/^\s*//gm;
1061d311cd44SJoe Perches	my @lines = split("\n", $output);
1062d311cd44SJoe Perches
10630d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
10640d7835fcSJoe Perches
10655a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1066d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1067d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1068d311cd44SJoe Perches#
1069d311cd44SJoe Perches#		echo "checking commits $1..."
1070d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1071d311cd44SJoe Perches#		while read line ; do
1072d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1073d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1074d311cd44SJoe Perches#		done
1075d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
1076948b133aSHeinrich Schuchardt		$id = undef;
1077d311cd44SJoe Perches	} else {
1078d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1079d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1080d311cd44SJoe Perches	}
1081d311cd44SJoe Perches
1082d311cd44SJoe Perches	return ($id, $desc);
1083d311cd44SJoe Perches}
1084d311cd44SJoe Perches
10856c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
10860a920b5bSAndy Whitcroft
108700df344fSAndy Whitcroftmy @rawlines = ();
1088c2fdda0dSAndy Whitcroftmy @lines = ();
10893705ce5bSJoe Perchesmy @fixed = ();
1090d752fcc8SJoe Perchesmy @fixed_inserted = ();
1091d752fcc8SJoe Perchesmy @fixed_deleted = ();
1092194f66fcSJoe Perchesmy $fixlinenr = -1;
1093194f66fcSJoe Perches
10944a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10954a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10960f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
10974a593c34SDu, Changbin
10984a593c34SDu, Changbinif ($git) {
10994a593c34SDu, Changbin	my @commits = ();
11000dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
11014a593c34SDu, Changbin		my $git_range;
110228898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
110328898fd1SJoe Perches			$git_range = "-$2 $1";
11044a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
11054a593c34SDu, Changbin			$git_range = "$commit_expr";
11064a593c34SDu, Changbin		} else {
11070dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
11080dea9f1eSJoe Perches		}
1109dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
11100dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
111128898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
111228898fd1SJoe Perches			next if (!defined($1) || !defined($2));
11130dea9f1eSJoe Perches			my $sha1 = $1;
11140dea9f1eSJoe Perches			my $subject = $2;
11150dea9f1eSJoe Perches			unshift(@commits, $sha1);
11160dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
11174a593c34SDu, Changbin		}
11184a593c34SDu, Changbin	}
11194a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
11204a593c34SDu, Changbin	@ARGV = @commits;
11214a593c34SDu, Changbin}
11224a593c34SDu, Changbin
1123c2fdda0dSAndy Whitcroftmy $vname;
112498005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
11256c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
112621caa13cSAndy Whitcroft	my $FILE;
1127f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1128f5f61325SJoe Perches	my $oldfile = $file;
1129f5f61325SJoe Perches	$file = 1 if ($is_git_file);
11304a593c34SDu, Changbin	if ($git) {
11314a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
11324a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
11334a593c34SDu, Changbin	} elsif ($file) {
113421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
11356c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
113621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
113721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
11386c72ffaaSAndy Whitcroft	} else {
113921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
11406c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
11416c72ffaaSAndy Whitcroft	}
1142c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1143c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
11444a593c34SDu, Changbin	} elsif ($git) {
11450dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1146c2fdda0dSAndy Whitcroft	} else {
1147c2fdda0dSAndy Whitcroft		$vname = $filename;
1148c2fdda0dSAndy Whitcroft	}
114921caa13cSAndy Whitcroft	while (<$FILE>) {
11500a920b5bSAndy Whitcroft		chomp;
115100df344fSAndy Whitcroft		push(@rawlines, $_);
1152c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
11536c72ffaaSAndy Whitcroft	}
115421caa13cSAndy Whitcroft	close($FILE);
1155d8469f16SJoe Perches
1156d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1157d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1158d8469f16SJoe Perches		print "$vname\n";
1159d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1160d8469f16SJoe Perches	}
1161d8469f16SJoe Perches
1162c2fdda0dSAndy Whitcroft	if (!process($filename)) {
11630a920b5bSAndy Whitcroft		$exit = 1;
11640a920b5bSAndy Whitcroft	}
116500df344fSAndy Whitcroft	@rawlines = ();
116613214adfSAndy Whitcroft	@lines = ();
11673705ce5bSJoe Perches	@fixed = ();
1168d752fcc8SJoe Perches	@fixed_inserted = ();
1169d752fcc8SJoe Perches	@fixed_deleted = ();
1170194f66fcSJoe Perches	$fixlinenr = -1;
1171485ff23eSAlex Dowad	@modifierListFile = ();
1172485ff23eSAlex Dowad	@typeListFile = ();
1173485ff23eSAlex Dowad	build_types();
1174f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
11750a920b5bSAndy Whitcroft}
11760a920b5bSAndy Whitcroft
1177d8469f16SJoe Perchesif (!$quiet) {
11783c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
11793c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
11803c816e49SJoe Perches
11815b57980dSJoe Perches	if (!$perl_version_ok) {
1182d8469f16SJoe Perches		print << "EOM"
1183d8469f16SJoe Perches
1184d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
11855b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1186d8469f16SJoe PerchesEOM
1187d8469f16SJoe Perches	}
1188d8469f16SJoe Perches	if ($exit) {
1189d8469f16SJoe Perches		print << "EOM"
1190d8469f16SJoe Perches
1191d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1192d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1193d8469f16SJoe PerchesEOM
1194d8469f16SJoe Perches	}
1195d8469f16SJoe Perches}
1196d8469f16SJoe Perches
11970a920b5bSAndy Whitcroftexit($exit);
11980a920b5bSAndy Whitcroft
11990a920b5bSAndy Whitcroftsub top_of_kernel_tree {
12006c72ffaaSAndy Whitcroft	my ($root) = @_;
12016c72ffaaSAndy Whitcroft
12026c72ffaaSAndy Whitcroft	my @tree_check = (
12036c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
12046c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
12056c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
12066c72ffaaSAndy Whitcroft	);
12076c72ffaaSAndy Whitcroft
12086c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
12096c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
12100a920b5bSAndy Whitcroft			return 0;
12110a920b5bSAndy Whitcroft		}
12126c72ffaaSAndy Whitcroft	}
12136c72ffaaSAndy Whitcroft	return 1;
12146c72ffaaSAndy Whitcroft}
12150a920b5bSAndy Whitcroft
121620112475SJoe Perchessub parse_email {
121720112475SJoe Perches	my ($formatted_email) = @_;
121820112475SJoe Perches
121920112475SJoe Perches	my $name = "";
1220fccaebf0SDwaipayan Ray	my $quoted = "";
1221dfa05c28SJoe Perches	my $name_comment = "";
122220112475SJoe Perches	my $address = "";
122320112475SJoe Perches	my $comment = "";
122420112475SJoe Perches
122520112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
122620112475SJoe Perches		$name = $1;
122720112475SJoe Perches		$address = $2;
122820112475SJoe Perches		$comment = $3 if defined $3;
122920112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
123020112475SJoe Perches		$address = $1;
123120112475SJoe Perches		$comment = $2 if defined $2;
123220112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
123320112475SJoe Perches		$address = $1;
123420112475SJoe Perches		$comment = $2 if defined $2;
123585e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
123620112475SJoe Perches		$name = $formatted_email;
12373705ce5bSJoe Perches		$name = trim($name);
123820112475SJoe Perches		$name =~ s/^\"|\"$//g;
123920112475SJoe Perches		# If there's a name left after stripping spaces and
124020112475SJoe Perches		# leading quotes, and the address doesn't have both
124120112475SJoe Perches		# leading and trailing angle brackets, the address
124220112475SJoe Perches		# is invalid. ie:
124320112475SJoe Perches		#   "joe smith [email protected]" bad
124420112475SJoe Perches		#   "joe smith <[email protected]" bad
124520112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
124620112475SJoe Perches			$name = "";
124720112475SJoe Perches			$address = "";
124820112475SJoe Perches			$comment = "";
124920112475SJoe Perches		}
125020112475SJoe Perches	}
125120112475SJoe Perches
1252fccaebf0SDwaipayan Ray	# Extract comments from names excluding quoted parts
1253fccaebf0SDwaipayan Ray	# "John D. (Doe)" - Do not extract
1254fccaebf0SDwaipayan Ray	if ($name =~ s/\"(.+)\"//) {
1255fccaebf0SDwaipayan Ray		$quoted = $1;
1256dfa05c28SJoe Perches	}
1257fccaebf0SDwaipayan Ray	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1258fccaebf0SDwaipayan Ray		$name_comment .= trim($1);
1259fccaebf0SDwaipayan Ray	}
1260fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
1261fccaebf0SDwaipayan Ray	$name = trim("$quoted $name");
1262fccaebf0SDwaipayan Ray
12633705ce5bSJoe Perches	$address = trim($address);
126420112475SJoe Perches	$address =~ s/^\<|\>$//g;
1265fccaebf0SDwaipayan Ray	$comment = trim($comment);
126620112475SJoe Perches
126720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
126820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
126920112475SJoe Perches		$name = "\"$name\"";
127020112475SJoe Perches	}
127120112475SJoe Perches
1272dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
127320112475SJoe Perches}
127420112475SJoe Perches
127520112475SJoe Perchessub format_email {
127648ca2d8aSDwaipayan Ray	my ($name, $name_comment, $address, $comment) = @_;
127720112475SJoe Perches
127820112475SJoe Perches	my $formatted_email;
127920112475SJoe Perches
1280fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
12813705ce5bSJoe Perches	$address = trim($address);
1282fccaebf0SDwaipayan Ray	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
128320112475SJoe Perches
128420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
128520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
128620112475SJoe Perches		$name = "\"$name\"";
128720112475SJoe Perches	}
128820112475SJoe Perches
1289fccaebf0SDwaipayan Ray	$name_comment = trim($name_comment);
1290fccaebf0SDwaipayan Ray	$name_comment = " $name_comment" if ($name_comment ne "");
1291fccaebf0SDwaipayan Ray	$comment = trim($comment);
1292fccaebf0SDwaipayan Ray	$comment = " $comment" if ($comment ne "");
1293fccaebf0SDwaipayan Ray
129420112475SJoe Perches	if ("$name" eq "") {
129520112475SJoe Perches		$formatted_email = "$address";
129620112475SJoe Perches	} else {
129748ca2d8aSDwaipayan Ray		$formatted_email = "$name$name_comment <$address>";
129820112475SJoe Perches	}
129948ca2d8aSDwaipayan Ray	$formatted_email .= "$comment";
130020112475SJoe Perches	return $formatted_email;
130120112475SJoe Perches}
130220112475SJoe Perches
1303dfa05c28SJoe Perchessub reformat_email {
1304dfa05c28SJoe Perches	my ($email) = @_;
1305dfa05c28SJoe Perches
1306dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
130748ca2d8aSDwaipayan Ray	return format_email($email_name, $name_comment, $email_address, $comment);
1308dfa05c28SJoe Perches}
1309dfa05c28SJoe Perches
1310dfa05c28SJoe Perchessub same_email_addresses {
1311fccaebf0SDwaipayan Ray	my ($email1, $email2) = @_;
1312dfa05c28SJoe Perches
1313dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1314dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1315dfa05c28SJoe Perches
131648ca2d8aSDwaipayan Ray	return $email1_name eq $email2_name &&
131748ca2d8aSDwaipayan Ray	       $email1_address eq $email2_address &&
131848ca2d8aSDwaipayan Ray	       $name1_comment eq $name2_comment &&
131948ca2d8aSDwaipayan Ray	       $comment1 eq $comment2;
132048ca2d8aSDwaipayan Ray}
1321dfa05c28SJoe Perches
1322d311cd44SJoe Perchessub which {
1323d311cd44SJoe Perches	my ($bin) = @_;
1324d311cd44SJoe Perches
1325d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1326d311cd44SJoe Perches		if (-e "$path/$bin") {
1327d311cd44SJoe Perches			return "$path/$bin";
1328d311cd44SJoe Perches		}
1329d311cd44SJoe Perches	}
1330d311cd44SJoe Perches
1331d311cd44SJoe Perches	return "";
1332d311cd44SJoe Perches}
1333d311cd44SJoe Perches
1334000d1cc1SJoe Perchessub which_conf {
1335000d1cc1SJoe Perches	my ($conf) = @_;
1336000d1cc1SJoe Perches
1337000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1338000d1cc1SJoe Perches		if (-e "$path/$conf") {
1339000d1cc1SJoe Perches			return "$path/$conf";
1340000d1cc1SJoe Perches		}
1341000d1cc1SJoe Perches	}
1342000d1cc1SJoe Perches
1343000d1cc1SJoe Perches	return "";
1344000d1cc1SJoe Perches}
1345000d1cc1SJoe Perches
13460a920b5bSAndy Whitcroftsub expand_tabs {
13470a920b5bSAndy Whitcroft	my ($str) = @_;
13480a920b5bSAndy Whitcroft
13490a920b5bSAndy Whitcroft	my $res = '';
13500a920b5bSAndy Whitcroft	my $n = 0;
13510a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
13520a920b5bSAndy Whitcroft		if ($c eq "\t") {
13530a920b5bSAndy Whitcroft			$res .= ' ';
13540a920b5bSAndy Whitcroft			$n++;
1355713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
13560a920b5bSAndy Whitcroft				$res .= ' ';
13570a920b5bSAndy Whitcroft			}
13580a920b5bSAndy Whitcroft			next;
13590a920b5bSAndy Whitcroft		}
13600a920b5bSAndy Whitcroft		$res .= $c;
13610a920b5bSAndy Whitcroft		$n++;
13620a920b5bSAndy Whitcroft	}
13630a920b5bSAndy Whitcroft
13640a920b5bSAndy Whitcroft	return $res;
13650a920b5bSAndy Whitcroft}
13666c72ffaaSAndy Whitcroftsub copy_spacing {
1367773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
13686c72ffaaSAndy Whitcroft	return $res;
13696c72ffaaSAndy Whitcroft}
13700a920b5bSAndy Whitcroft
13714a0df2efSAndy Whitcroftsub line_stats {
13724a0df2efSAndy Whitcroft	my ($line) = @_;
13734a0df2efSAndy Whitcroft
13744a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
13754a0df2efSAndy Whitcroft	$line =~ s/^.//;
13764a0df2efSAndy Whitcroft	$line = expand_tabs($line);
13774a0df2efSAndy Whitcroft
13784a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
13794a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
13804a0df2efSAndy Whitcroft
13814a0df2efSAndy Whitcroft	return (length($line), length($white));
13824a0df2efSAndy Whitcroft}
13834a0df2efSAndy Whitcroft
1384773647a0SAndy Whitcroftmy $sanitise_quote = '';
1385773647a0SAndy Whitcroft
1386773647a0SAndy Whitcroftsub sanitise_line_reset {
1387773647a0SAndy Whitcroft	my ($in_comment) = @_;
1388773647a0SAndy Whitcroft
1389773647a0SAndy Whitcroft	if ($in_comment) {
1390773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1391773647a0SAndy Whitcroft	} else {
1392773647a0SAndy Whitcroft		$sanitise_quote = '';
1393773647a0SAndy Whitcroft	}
1394773647a0SAndy Whitcroft}
139500df344fSAndy Whitcroftsub sanitise_line {
139600df344fSAndy Whitcroft	my ($line) = @_;
139700df344fSAndy Whitcroft
139800df344fSAndy Whitcroft	my $res = '';
139900df344fSAndy Whitcroft	my $l = '';
140000df344fSAndy Whitcroft
1401c2fdda0dSAndy Whitcroft	my $qlen = 0;
1402773647a0SAndy Whitcroft	my $off = 0;
1403773647a0SAndy Whitcroft	my $c;
140400df344fSAndy Whitcroft
1405773647a0SAndy Whitcroft	# Always copy over the diff marker.
1406773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1407773647a0SAndy Whitcroft
1408773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1409773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1410773647a0SAndy Whitcroft
14118d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1412773647a0SAndy Whitcroft		# and end, all to $;.
1413773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1414773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1415773647a0SAndy Whitcroft
1416773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1417773647a0SAndy Whitcroft			$off++;
141800df344fSAndy Whitcroft			next;
1419773647a0SAndy Whitcroft		}
142081bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1421773647a0SAndy Whitcroft			$sanitise_quote = '';
1422773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1423773647a0SAndy Whitcroft			$off++;
1424773647a0SAndy Whitcroft			next;
1425773647a0SAndy Whitcroft		}
1426113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1427113f04a8SDaniel Walker			$sanitise_quote = '//';
1428113f04a8SDaniel Walker
1429113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1430113f04a8SDaniel Walker			$off++;
1431113f04a8SDaniel Walker			next;
1432113f04a8SDaniel Walker		}
1433773647a0SAndy Whitcroft
1434773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1435773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1436773647a0SAndy Whitcroft		    $c eq "\\") {
1437773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1438773647a0SAndy Whitcroft			$off++;
1439773647a0SAndy Whitcroft			next;
1440773647a0SAndy Whitcroft		}
1441773647a0SAndy Whitcroft		# Regular quotes.
1442773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1443773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1444773647a0SAndy Whitcroft				$sanitise_quote = $c;
1445773647a0SAndy Whitcroft
1446773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1447773647a0SAndy Whitcroft				next;
1448773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1449773647a0SAndy Whitcroft				$sanitise_quote = '';
145000df344fSAndy Whitcroft			}
145100df344fSAndy Whitcroft		}
1452773647a0SAndy Whitcroft
1453fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1454773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1455773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1456113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1457113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1458773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1459773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
146000df344fSAndy Whitcroft		} else {
1461773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
146200df344fSAndy Whitcroft		}
1463c2fdda0dSAndy Whitcroft	}
1464c2fdda0dSAndy Whitcroft
1465113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1466113f04a8SDaniel Walker		$sanitise_quote = '';
1467113f04a8SDaniel Walker	}
1468113f04a8SDaniel Walker
1469c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1470c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1471c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1472c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1473c2fdda0dSAndy Whitcroft
1474c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1475c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1476c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1477c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1478c2fdda0dSAndy Whitcroft	}
1479c2fdda0dSAndy Whitcroft
1480dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1481dadf680dSJoe Perches		my $match = $1;
1482dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1483dadf680dSJoe Perches	}
1484dadf680dSJoe Perches
148500df344fSAndy Whitcroft	return $res;
148600df344fSAndy Whitcroft}
148700df344fSAndy Whitcroft
1488a6962d72SJoe Perchessub get_quoted_string {
1489a6962d72SJoe Perches	my ($line, $rawline) = @_;
1490a6962d72SJoe Perches
1491478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
149233acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1493a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1494a6962d72SJoe Perches}
1495a6962d72SJoe Perches
14968905a67cSAndy Whitcroftsub ctx_statement_block {
14978905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
14988905a67cSAndy Whitcroft	my $line = $linenr - 1;
14998905a67cSAndy Whitcroft	my $blk = '';
15008905a67cSAndy Whitcroft	my $soff = $off;
15018905a67cSAndy Whitcroft	my $coff = $off - 1;
1502773647a0SAndy Whitcroft	my $coff_set = 0;
15038905a67cSAndy Whitcroft
150413214adfSAndy Whitcroft	my $loff = 0;
150513214adfSAndy Whitcroft
15068905a67cSAndy Whitcroft	my $type = '';
15078905a67cSAndy Whitcroft	my $level = 0;
1508a2750645SAndy Whitcroft	my @stack = ();
1509cf655043SAndy Whitcroft	my $p;
15108905a67cSAndy Whitcroft	my $c;
15118905a67cSAndy Whitcroft	my $len = 0;
151213214adfSAndy Whitcroft
151313214adfSAndy Whitcroft	my $remainder;
15148905a67cSAndy Whitcroft	while (1) {
1515a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1516a2750645SAndy Whitcroft
1517773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
15188905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
15198905a67cSAndy Whitcroft		# context.
15208905a67cSAndy Whitcroft		if ($off >= $len) {
15218905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1522dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1523c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
15248905a67cSAndy Whitcroft				$remain--;
152513214adfSAndy Whitcroft				$loff = $len;
1526c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
15278905a67cSAndy Whitcroft				$len = length($blk);
15288905a67cSAndy Whitcroft				$line++;
15298905a67cSAndy Whitcroft				last;
15308905a67cSAndy Whitcroft			}
15318905a67cSAndy Whitcroft			# Bail if there is no further context.
15328905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
153313214adfSAndy Whitcroft			if ($off >= $len) {
15348905a67cSAndy Whitcroft				last;
15358905a67cSAndy Whitcroft			}
1536f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1537f74bd194SAndy Whitcroft				$level++;
1538f74bd194SAndy Whitcroft				$type = '#';
1539f74bd194SAndy Whitcroft			}
15408905a67cSAndy Whitcroft		}
1541cf655043SAndy Whitcroft		$p = $c;
15428905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
154313214adfSAndy Whitcroft		$remainder = substr($blk, $off);
15448905a67cSAndy Whitcroft
1545773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
15464635f4fbSAndy Whitcroft
15474635f4fbSAndy Whitcroft		# Handle nested #if/#else.
15484635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
15494635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
15504635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
15514635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
15524635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
15534635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
15544635f4fbSAndy Whitcroft		}
15554635f4fbSAndy Whitcroft
15568905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
15578905a67cSAndy Whitcroft		# outermost level.
15588905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
15598905a67cSAndy Whitcroft			last;
15608905a67cSAndy Whitcroft		}
15618905a67cSAndy Whitcroft
156213214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1563773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1564773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1565773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1566773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1567773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1568773647a0SAndy Whitcroft			$coff_set = 1;
1569773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1570773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
157113214adfSAndy Whitcroft		}
157213214adfSAndy Whitcroft
15738905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
15748905a67cSAndy Whitcroft			$level++;
15758905a67cSAndy Whitcroft			$type = '(';
15768905a67cSAndy Whitcroft		}
15778905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
15788905a67cSAndy Whitcroft			$level--;
15798905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
15808905a67cSAndy Whitcroft
15818905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
15828905a67cSAndy Whitcroft				$coff = $off;
1583773647a0SAndy Whitcroft				$coff_set = 1;
1584773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
15858905a67cSAndy Whitcroft			}
15868905a67cSAndy Whitcroft		}
15878905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
15888905a67cSAndy Whitcroft			$level++;
15898905a67cSAndy Whitcroft			$type = '{';
15908905a67cSAndy Whitcroft		}
15918905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
15928905a67cSAndy Whitcroft			$level--;
15938905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
15948905a67cSAndy Whitcroft
15958905a67cSAndy Whitcroft			if ($level == 0) {
1596b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1597b998e001SPatrick Pannuto					$off++;
1598b998e001SPatrick Pannuto				}
15998905a67cSAndy Whitcroft				last;
16008905a67cSAndy Whitcroft			}
16018905a67cSAndy Whitcroft		}
1602f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1603f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1604f74bd194SAndy Whitcroft			$level--;
1605f74bd194SAndy Whitcroft			$type = '';
1606f74bd194SAndy Whitcroft			$off++;
1607f74bd194SAndy Whitcroft			last;
1608f74bd194SAndy Whitcroft		}
16098905a67cSAndy Whitcroft		$off++;
16108905a67cSAndy Whitcroft	}
1611a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
161213214adfSAndy Whitcroft	if ($off == $len) {
1613a3bb97a7SAndy Whitcroft		$loff = $len + 1;
161413214adfSAndy Whitcroft		$line++;
161513214adfSAndy Whitcroft		$remain--;
161613214adfSAndy Whitcroft	}
16178905a67cSAndy Whitcroft
16188905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
16198905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
16208905a67cSAndy Whitcroft
16218905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
16228905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
16238905a67cSAndy Whitcroft
1624773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
162513214adfSAndy Whitcroft
162613214adfSAndy Whitcroft	return ($statement, $condition,
162713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
162813214adfSAndy Whitcroft}
162913214adfSAndy Whitcroft
1630cf655043SAndy Whitcroftsub statement_lines {
1631cf655043SAndy Whitcroft	my ($stmt) = @_;
1632cf655043SAndy Whitcroft
1633cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1634cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1635cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1636cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1637cf655043SAndy Whitcroft
1638cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1639cf655043SAndy Whitcroft
1640cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1641cf655043SAndy Whitcroft}
1642cf655043SAndy Whitcroft
1643cf655043SAndy Whitcroftsub statement_rawlines {
1644cf655043SAndy Whitcroft	my ($stmt) = @_;
1645cf655043SAndy Whitcroft
1646cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1647cf655043SAndy Whitcroft
1648cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1649cf655043SAndy Whitcroft}
1650cf655043SAndy Whitcroft
1651cf655043SAndy Whitcroftsub statement_block_size {
1652cf655043SAndy Whitcroft	my ($stmt) = @_;
1653cf655043SAndy Whitcroft
1654cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1655cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1656cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1657cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1658cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1659cf655043SAndy Whitcroft
1660cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1661cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1662cf655043SAndy Whitcroft
1663cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1664cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1665cf655043SAndy Whitcroft
1666cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1667cf655043SAndy Whitcroft		return $stmt_lines;
1668cf655043SAndy Whitcroft	} else {
1669cf655043SAndy Whitcroft		return $stmt_statements;
1670cf655043SAndy Whitcroft	}
1671cf655043SAndy Whitcroft}
1672cf655043SAndy Whitcroft
167313214adfSAndy Whitcroftsub ctx_statement_full {
167413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
167513214adfSAndy Whitcroft	my ($statement, $condition, $level);
167613214adfSAndy Whitcroft
167713214adfSAndy Whitcroft	my (@chunks);
167813214adfSAndy Whitcroft
1679cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
168013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
168113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1682773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
168313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1684cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1685cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1686cf655043SAndy Whitcroft	}
1687cf655043SAndy Whitcroft
1688cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1689cf655043SAndy Whitcroft	# could continue the statement.
1690cf655043SAndy Whitcroft	for (;;) {
169113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
169213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1693cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1694773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1695cf655043SAndy Whitcroft		#print "C: push\n";
1696cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
169713214adfSAndy Whitcroft	}
169813214adfSAndy Whitcroft
169913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
17008905a67cSAndy Whitcroft}
17018905a67cSAndy Whitcroft
17024a0df2efSAndy Whitcroftsub ctx_block_get {
1703f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
17044a0df2efSAndy Whitcroft	my $line;
17054a0df2efSAndy Whitcroft	my $start = $linenr - 1;
17064a0df2efSAndy Whitcroft	my $blk = '';
17074a0df2efSAndy Whitcroft	my @o;
17084a0df2efSAndy Whitcroft	my @c;
17094a0df2efSAndy Whitcroft	my @res = ();
17104a0df2efSAndy Whitcroft
1711f0a594c1SAndy Whitcroft	my $level = 0;
17124635f4fbSAndy Whitcroft	my @stack = ($level);
171300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
171400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
171500df344fSAndy Whitcroft		$remain--;
171600df344fSAndy Whitcroft
171700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
17184635f4fbSAndy Whitcroft
17194635f4fbSAndy Whitcroft		# Handle nested #if/#else.
172001464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
17214635f4fbSAndy Whitcroft			push(@stack, $level);
172201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
17234635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
172401464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
17254635f4fbSAndy Whitcroft			$level = pop(@stack);
17264635f4fbSAndy Whitcroft		}
17274635f4fbSAndy Whitcroft
172801464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1729f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1730f0a594c1SAndy Whitcroft			if ($off > 0) {
1731f0a594c1SAndy Whitcroft				$off--;
1732f0a594c1SAndy Whitcroft				next;
1733f0a594c1SAndy Whitcroft			}
17344a0df2efSAndy Whitcroft
1735f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1736f0a594c1SAndy Whitcroft				$level--;
1737f0a594c1SAndy Whitcroft				last if ($level == 0);
1738f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1739f0a594c1SAndy Whitcroft				$level++;
1740f0a594c1SAndy Whitcroft			}
1741f0a594c1SAndy Whitcroft		}
17424a0df2efSAndy Whitcroft
1743f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
174400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
17454a0df2efSAndy Whitcroft		}
17464a0df2efSAndy Whitcroft
1747f0a594c1SAndy Whitcroft		last if ($level == 0);
17484a0df2efSAndy Whitcroft	}
17494a0df2efSAndy Whitcroft
1750f0a594c1SAndy Whitcroft	return ($level, @res);
17514a0df2efSAndy Whitcroft}
17524a0df2efSAndy Whitcroftsub ctx_block_outer {
17534a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
17544a0df2efSAndy Whitcroft
1755f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1756f0a594c1SAndy Whitcroft	return @r;
17574a0df2efSAndy Whitcroft}
17584a0df2efSAndy Whitcroftsub ctx_block {
17594a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
17604a0df2efSAndy Whitcroft
1761f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1762f0a594c1SAndy Whitcroft	return @r;
1763653d4876SAndy Whitcroft}
1764653d4876SAndy Whitcroftsub ctx_statement {
1765f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1766f0a594c1SAndy Whitcroft
1767f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1768f0a594c1SAndy Whitcroft	return @r;
1769f0a594c1SAndy Whitcroft}
1770f0a594c1SAndy Whitcroftsub ctx_block_level {
1771653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1772653d4876SAndy Whitcroft
1773f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
17744a0df2efSAndy Whitcroft}
17759c0ca6f9SAndy Whitcroftsub ctx_statement_level {
17769c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
17779c0ca6f9SAndy Whitcroft
17789c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
17799c0ca6f9SAndy Whitcroft}
17804a0df2efSAndy Whitcroft
17814a0df2efSAndy Whitcroftsub ctx_locate_comment {
17824a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17834a0df2efSAndy Whitcroft
1784a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1785a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1786a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1787a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1788a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1789a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1790a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1791a55ee0ccSJoe Perches
17924a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1793a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
17944a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
17954a0df2efSAndy Whitcroft
17964a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
17974a0df2efSAndy Whitcroft	# comment.
17984a0df2efSAndy Whitcroft	my $in_comment = 0;
17994a0df2efSAndy Whitcroft	$current_comment = '';
18004a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
180100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
180200df344fSAndy Whitcroft		#warn "           $line\n";
18034a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
18044a0df2efSAndy Whitcroft			$in_comment = 1;
18054a0df2efSAndy Whitcroft		}
18064a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
18074a0df2efSAndy Whitcroft			$in_comment = 1;
18084a0df2efSAndy Whitcroft		}
18094a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
18104a0df2efSAndy Whitcroft			$current_comment = '';
18114a0df2efSAndy Whitcroft		}
18124a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
18134a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
18144a0df2efSAndy Whitcroft			$in_comment = 0;
18154a0df2efSAndy Whitcroft		}
18164a0df2efSAndy Whitcroft	}
18174a0df2efSAndy Whitcroft
18184a0df2efSAndy Whitcroft	chomp($current_comment);
18194a0df2efSAndy Whitcroft	return($current_comment);
18204a0df2efSAndy Whitcroft}
18214a0df2efSAndy Whitcroftsub ctx_has_comment {
18224a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
18234a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
18244a0df2efSAndy Whitcroft
182500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
18264a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
18274a0df2efSAndy Whitcroft
18284a0df2efSAndy Whitcroft	return ($cmt ne '');
18294a0df2efSAndy Whitcroft}
18304a0df2efSAndy Whitcroft
18314d001e4dSAndy Whitcroftsub raw_line {
18324d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
18334d001e4dSAndy Whitcroft
18344d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
18354d001e4dSAndy Whitcroft	$cnt++;
18364d001e4dSAndy Whitcroft
18374d001e4dSAndy Whitcroft	my $line;
18384d001e4dSAndy Whitcroft	while ($cnt) {
18394d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
18404d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
18414d001e4dSAndy Whitcroft		$cnt--;
18424d001e4dSAndy Whitcroft	}
18434d001e4dSAndy Whitcroft
18444d001e4dSAndy Whitcroft	return $line;
18454d001e4dSAndy Whitcroft}
18464d001e4dSAndy Whitcroft
18472a9f9d85STobin C. Hardingsub get_stat_real {
18482a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
18492a9f9d85STobin C. Harding
18502a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
18512a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
18522a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
18532a9f9d85STobin C. Harding	}
18542a9f9d85STobin C. Harding
18552a9f9d85STobin C. Harding	return $stat_real;
18562a9f9d85STobin C. Harding}
18572a9f9d85STobin C. Harding
1858e3d95a2aSTobin C. Hardingsub get_stat_here {
1859e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1860e3d95a2aSTobin C. Harding
1861e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1862e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1863e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1864e3d95a2aSTobin C. Harding	}
1865e3d95a2aSTobin C. Harding
1866e3d95a2aSTobin C. Harding	return $herectx;
1867e3d95a2aSTobin C. Harding}
1868e3d95a2aSTobin C. Harding
18690a920b5bSAndy Whitcroftsub cat_vet {
18700a920b5bSAndy Whitcroft	my ($vet) = @_;
18719c0ca6f9SAndy Whitcroft	my ($res, $coded);
18720a920b5bSAndy Whitcroft
18739c0ca6f9SAndy Whitcroft	$res = '';
18746c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
18756c72ffaaSAndy Whitcroft		$res .= $1;
18766c72ffaaSAndy Whitcroft		if ($2 ne '') {
18779c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
18786c72ffaaSAndy Whitcroft			$res .= $coded;
18796c72ffaaSAndy Whitcroft		}
18809c0ca6f9SAndy Whitcroft	}
18819c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
18820a920b5bSAndy Whitcroft
18839c0ca6f9SAndy Whitcroft	return $res;
18840a920b5bSAndy Whitcroft}
18850a920b5bSAndy Whitcroft
1886c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1887cf655043SAndy Whitcroftmy $av_pending;
1888c2fdda0dSAndy Whitcroftmy @av_paren_type;
18891f65f947SAndy Whitcroftmy $av_pend_colon;
1890c2fdda0dSAndy Whitcroft
1891c2fdda0dSAndy Whitcroftsub annotate_reset {
1892c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1893cf655043SAndy Whitcroft	$av_pending = '_';
1894cf655043SAndy Whitcroft	@av_paren_type = ('E');
18951f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1896c2fdda0dSAndy Whitcroft}
1897c2fdda0dSAndy Whitcroft
18986c72ffaaSAndy Whitcroftsub annotate_values {
18996c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
19006c72ffaaSAndy Whitcroft
19016c72ffaaSAndy Whitcroft	my $res;
19021f65f947SAndy Whitcroft	my $var = '_' x length($stream);
19036c72ffaaSAndy Whitcroft	my $cur = $stream;
19046c72ffaaSAndy Whitcroft
1905c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
19066c72ffaaSAndy Whitcroft
19076c72ffaaSAndy Whitcroft	while (length($cur)) {
1908773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1909cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1910171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
19116c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1912c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1913c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1914cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1915c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
19166c72ffaaSAndy Whitcroft			}
19176c72ffaaSAndy Whitcroft
1918c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
19199446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
19209446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1921addcdceaSAndy Whitcroft			$type = 'c';
19229446ef56SAndy Whitcroft
1923e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1924c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
19256c72ffaaSAndy Whitcroft			$type = 'T';
19266c72ffaaSAndy Whitcroft
1927389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1928389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1929389a2fe5SAndy Whitcroft			$type = 'T';
1930389a2fe5SAndy Whitcroft
1931c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1932171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1933c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1934171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1935171ae1a4SAndy Whitcroft			if ($2 ne '') {
1936cf655043SAndy Whitcroft				$av_pending = 'N';
1937171ae1a4SAndy Whitcroft			}
1938171ae1a4SAndy Whitcroft			$type = 'E';
1939171ae1a4SAndy Whitcroft
1940c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1941171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1942171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1943171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
19446c72ffaaSAndy Whitcroft
1945c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1946cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1947c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1948cf655043SAndy Whitcroft
1949cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1950cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1951171ae1a4SAndy Whitcroft			$type = 'E';
1952cf655043SAndy Whitcroft
1953c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1954cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1955cf655043SAndy Whitcroft			$av_preprocessor = 1;
1956cf655043SAndy Whitcroft
1957cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1958cf655043SAndy Whitcroft
1959171ae1a4SAndy Whitcroft			$type = 'E';
1960cf655043SAndy Whitcroft
1961c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1962cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1963cf655043SAndy Whitcroft
1964cf655043SAndy Whitcroft			$av_preprocessor = 1;
1965cf655043SAndy Whitcroft
1966cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1967cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1968cf655043SAndy Whitcroft			pop(@av_paren_type);
1969cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1970171ae1a4SAndy Whitcroft			$type = 'E';
19716c72ffaaSAndy Whitcroft
19726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1973c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
19746c72ffaaSAndy Whitcroft
1975171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1976171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1977171ae1a4SAndy Whitcroft			$av_pending = $type;
1978171ae1a4SAndy Whitcroft			$type = 'N';
1979171ae1a4SAndy Whitcroft
19806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1981c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
19826c72ffaaSAndy Whitcroft			if (defined $2) {
1983cf655043SAndy Whitcroft				$av_pending = 'V';
19846c72ffaaSAndy Whitcroft			}
19856c72ffaaSAndy Whitcroft			$type = 'N';
19866c72ffaaSAndy Whitcroft
198714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1988c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
198914b111c1SAndy Whitcroft			$av_pending = 'E';
19906c72ffaaSAndy Whitcroft			$type = 'N';
19916c72ffaaSAndy Whitcroft
19921f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
19931f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
19941f65f947SAndy Whitcroft			$av_pend_colon = 'C';
19951f65f947SAndy Whitcroft			$type = 'N';
19961f65f947SAndy Whitcroft
199714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1998c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
19996c72ffaaSAndy Whitcroft			$type = 'N';
20006c72ffaaSAndy Whitcroft
20016c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
2002c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
2003cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
2004cf655043SAndy Whitcroft			$av_pending = '_';
20056c72ffaaSAndy Whitcroft			$type = 'N';
20066c72ffaaSAndy Whitcroft
20076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
2008cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
2009cf655043SAndy Whitcroft			if ($new_type ne '_') {
2010cf655043SAndy Whitcroft				$type = $new_type;
2011c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
2012c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
20136c72ffaaSAndy Whitcroft			} else {
2014c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
20156c72ffaaSAndy Whitcroft			}
20166c72ffaaSAndy Whitcroft
2017c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
2018c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
2019c8cb2ca3SAndy Whitcroft			$type = 'V';
2020cf655043SAndy Whitcroft			$av_pending = 'V';
20216c72ffaaSAndy Whitcroft
20228e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
20238e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
20241f65f947SAndy Whitcroft				$av_pend_colon = 'B';
20258e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
20268e761b04SAndy Whitcroft				$av_pend_colon = 'L';
20271f65f947SAndy Whitcroft			}
20281f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
20291f65f947SAndy Whitcroft			$type = 'V';
20301f65f947SAndy Whitcroft
20316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
2032c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
20336c72ffaaSAndy Whitcroft			$type = 'V';
20346c72ffaaSAndy Whitcroft
20356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
2036c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
20376c72ffaaSAndy Whitcroft			$type = 'N';
20386c72ffaaSAndy Whitcroft
2039cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
2040c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
204113214adfSAndy Whitcroft			$type = 'E';
20421f65f947SAndy Whitcroft			$av_pend_colon = 'O';
204313214adfSAndy Whitcroft
20448e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
20458e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
20468e761b04SAndy Whitcroft			$type = 'C';
20478e761b04SAndy Whitcroft
20481f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
20491f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
20501f65f947SAndy Whitcroft			$type = 'N';
20511f65f947SAndy Whitcroft
20521f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
20531f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
20541f65f947SAndy Whitcroft
20551f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
20561f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
20571f65f947SAndy Whitcroft				$type = 'E';
20581f65f947SAndy Whitcroft			} else {
20591f65f947SAndy Whitcroft				$type = 'N';
20601f65f947SAndy Whitcroft			}
20611f65f947SAndy Whitcroft			$av_pend_colon = 'O';
20621f65f947SAndy Whitcroft
20638e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
206413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
20656c72ffaaSAndy Whitcroft			$type = 'N';
20666c72ffaaSAndy Whitcroft
20670d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
206874048ed8SAndy Whitcroft			my $variant;
206974048ed8SAndy Whitcroft
207074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
207174048ed8SAndy Whitcroft			if ($type eq 'V') {
207274048ed8SAndy Whitcroft				$variant = 'B';
207374048ed8SAndy Whitcroft			} else {
207474048ed8SAndy Whitcroft				$variant = 'U';
207574048ed8SAndy Whitcroft			}
207674048ed8SAndy Whitcroft
207774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
207874048ed8SAndy Whitcroft			$type = 'N';
207974048ed8SAndy Whitcroft
20806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2081c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
20826c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
20836c72ffaaSAndy Whitcroft				$type = 'N';
20846c72ffaaSAndy Whitcroft			}
20856c72ffaaSAndy Whitcroft
20866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2087c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
20886c72ffaaSAndy Whitcroft		}
20896c72ffaaSAndy Whitcroft		if (defined $1) {
20906c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
20916c72ffaaSAndy Whitcroft			$res .= $type x length($1);
20926c72ffaaSAndy Whitcroft		}
20936c72ffaaSAndy Whitcroft	}
20946c72ffaaSAndy Whitcroft
20951f65f947SAndy Whitcroft	return ($res, $var);
20966c72ffaaSAndy Whitcroft}
20976c72ffaaSAndy Whitcroft
20988905a67cSAndy Whitcroftsub possible {
209913214adfSAndy Whitcroft	my ($possible, $line) = @_;
21009a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
21010776e594SAndy Whitcroft		^(?:
21020776e594SAndy Whitcroft			$Modifier|
21030776e594SAndy Whitcroft			$Storage|
21040776e594SAndy Whitcroft			$Type|
21059a974fdbSAndy Whitcroft			DEFINE_\S+
21069a974fdbSAndy Whitcroft		)$|
21079a974fdbSAndy Whitcroft		^(?:
21080776e594SAndy Whitcroft			goto|
21090776e594SAndy Whitcroft			return|
21100776e594SAndy Whitcroft			case|
21110776e594SAndy Whitcroft			else|
21120776e594SAndy Whitcroft			asm|__asm__|
211389a88353SAndy Whitcroft			do|
211489a88353SAndy Whitcroft			\#|
211589a88353SAndy Whitcroft			\#\#|
21169a974fdbSAndy Whitcroft		)(?:\s|$)|
21170776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
21189a974fdbSAndy Whitcroft	    )}x;
21199a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
21209a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2121c45dcabdSAndy Whitcroft		# Check for modifiers.
2122c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2123c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2124c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2125c45dcabdSAndy Whitcroft
2126c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2127c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2128d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
21299a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2130d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2131485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2132d2506586SAndy Whitcroft				}
21339a974fdbSAndy Whitcroft			}
2134c45dcabdSAndy Whitcroft
2135c45dcabdSAndy Whitcroft		} else {
213613214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2137485ff23eSAlex Dowad			push(@typeListFile, $possible);
2138c45dcabdSAndy Whitcroft		}
21398905a67cSAndy Whitcroft		build_types();
21400776e594SAndy Whitcroft	} else {
21410776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
21428905a67cSAndy Whitcroft	}
21438905a67cSAndy Whitcroft}
21448905a67cSAndy Whitcroft
21456c72ffaaSAndy Whitcroftmy $prefix = '';
21466c72ffaaSAndy Whitcroft
2147000d1cc1SJoe Perchessub show_type {
2148cbec18afSJoe Perches	my ($type) = @_;
214991bfe484SJoe Perches
2150522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2151522b837cSAlexey Dobriyan
2152cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2153cbec18afSJoe Perches
2154cbec18afSJoe Perches	return !defined $ignore_type{$type};
2155000d1cc1SJoe Perches}
2156000d1cc1SJoe Perches
2157f0a594c1SAndy Whitcroftsub report {
2158cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2159cbec18afSJoe Perches
2160cbec18afSJoe Perches	if (!show_type($type) ||
2161cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2162773647a0SAndy Whitcroft		return 0;
2163773647a0SAndy Whitcroft	}
216457230297SJoe Perches	my $output = '';
2165737c0767SJohn Brooks	if ($color) {
216657230297SJoe Perches		if ($level eq 'ERROR') {
216757230297SJoe Perches			$output .= RED;
216857230297SJoe Perches		} elsif ($level eq 'WARNING') {
216957230297SJoe Perches			$output .= YELLOW;
2170000d1cc1SJoe Perches		} else {
217157230297SJoe Perches			$output .= GREEN;
2172000d1cc1SJoe Perches		}
217357230297SJoe Perches	}
217457230297SJoe Perches	$output .= $prefix . $level . ':';
217557230297SJoe Perches	if ($show_types) {
2176737c0767SJohn Brooks		$output .= BLUE if ($color);
217757230297SJoe Perches		$output .= "$type:";
217857230297SJoe Perches	}
2179737c0767SJohn Brooks	$output .= RESET if ($color);
218057230297SJoe Perches	$output .= ' ' . $msg . "\n";
218134d8815fSJoe Perches
218234d8815fSJoe Perches	if ($showfile) {
218334d8815fSJoe Perches		my @lines = split("\n", $output, -1);
218434d8815fSJoe Perches		splice(@lines, 1, 1);
218534d8815fSJoe Perches		$output = join("\n", @lines);
218634d8815fSJoe Perches	}
218757230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
21888905a67cSAndy Whitcroft
218957230297SJoe Perches	push(our @report, $output);
2190773647a0SAndy Whitcroft
2191773647a0SAndy Whitcroft	return 1;
2192f0a594c1SAndy Whitcroft}
2193cbec18afSJoe Perches
2194f0a594c1SAndy Whitcroftsub report_dump {
219513214adfSAndy Whitcroft	our @report;
2196f0a594c1SAndy Whitcroft}
2197000d1cc1SJoe Perches
2198d752fcc8SJoe Perchessub fixup_current_range {
2199d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2200d752fcc8SJoe Perches
2201d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2202d752fcc8SJoe Perches		my $o = $1;
2203d752fcc8SJoe Perches		my $l = $2;
2204d752fcc8SJoe Perches		my $no = $o + $offset;
2205d752fcc8SJoe Perches		my $nl = $l + $length;
2206d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2207d752fcc8SJoe Perches	}
2208d752fcc8SJoe Perches}
2209d752fcc8SJoe Perches
2210d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2211d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2212d752fcc8SJoe Perches
2213d752fcc8SJoe Perches	my $range_last_linenr = 0;
2214d752fcc8SJoe Perches	my $delta_offset = 0;
2215d752fcc8SJoe Perches
2216d752fcc8SJoe Perches	my $old_linenr = 0;
2217d752fcc8SJoe Perches	my $new_linenr = 0;
2218d752fcc8SJoe Perches
2219d752fcc8SJoe Perches	my $next_insert = 0;
2220d752fcc8SJoe Perches	my $next_delete = 0;
2221d752fcc8SJoe Perches
2222d752fcc8SJoe Perches	my @lines = ();
2223d752fcc8SJoe Perches
2224d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2225d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2226d752fcc8SJoe Perches
2227d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2228d752fcc8SJoe Perches		my $save_line = 1;
2229d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2230323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2231d752fcc8SJoe Perches			$delta_offset = 0;
2232d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2233d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2234d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2235d752fcc8SJoe Perches		}
2236d752fcc8SJoe Perches
2237d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2238d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2239d752fcc8SJoe Perches			$save_line = 0;
2240d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2241d752fcc8SJoe Perches		}
2242d752fcc8SJoe Perches
2243d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2244d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2245d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2246d752fcc8SJoe Perches			$new_linenr++;
2247d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2248d752fcc8SJoe Perches		}
2249d752fcc8SJoe Perches
2250d752fcc8SJoe Perches		if ($save_line) {
2251d752fcc8SJoe Perches			push(@lines, $line);
2252d752fcc8SJoe Perches			$new_linenr++;
2253d752fcc8SJoe Perches		}
2254d752fcc8SJoe Perches
2255d752fcc8SJoe Perches		$old_linenr++;
2256d752fcc8SJoe Perches	}
2257d752fcc8SJoe Perches
2258d752fcc8SJoe Perches	return @lines;
2259d752fcc8SJoe Perches}
2260d752fcc8SJoe Perches
2261f2d7e4d4SJoe Perchessub fix_insert_line {
2262f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2263f2d7e4d4SJoe Perches
2264f2d7e4d4SJoe Perches	my $inserted = {
2265f2d7e4d4SJoe Perches		LINENR => $linenr,
2266f2d7e4d4SJoe Perches		LINE => $line,
2267f2d7e4d4SJoe Perches	};
2268f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2269f2d7e4d4SJoe Perches}
2270f2d7e4d4SJoe Perches
2271f2d7e4d4SJoe Perchessub fix_delete_line {
2272f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2273f2d7e4d4SJoe Perches
2274f2d7e4d4SJoe Perches	my $deleted = {
2275f2d7e4d4SJoe Perches		LINENR => $linenr,
2276f2d7e4d4SJoe Perches		LINE => $line,
2277f2d7e4d4SJoe Perches	};
2278f2d7e4d4SJoe Perches
2279f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2280f2d7e4d4SJoe Perches}
2281f2d7e4d4SJoe Perches
2282de7d4f0eSAndy Whitcroftsub ERROR {
2283cbec18afSJoe Perches	my ($type, $msg) = @_;
2284cbec18afSJoe Perches
2285cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2286de7d4f0eSAndy Whitcroft		our $clean = 0;
22876c72ffaaSAndy Whitcroft		our $cnt_error++;
22883705ce5bSJoe Perches		return 1;
2289de7d4f0eSAndy Whitcroft	}
22903705ce5bSJoe Perches	return 0;
2291773647a0SAndy Whitcroft}
2292de7d4f0eSAndy Whitcroftsub WARN {
2293cbec18afSJoe Perches	my ($type, $msg) = @_;
2294cbec18afSJoe Perches
2295cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2296de7d4f0eSAndy Whitcroft		our $clean = 0;
22976c72ffaaSAndy Whitcroft		our $cnt_warn++;
22983705ce5bSJoe Perches		return 1;
2299de7d4f0eSAndy Whitcroft	}
23003705ce5bSJoe Perches	return 0;
2301773647a0SAndy Whitcroft}
2302de7d4f0eSAndy Whitcroftsub CHK {
2303cbec18afSJoe Perches	my ($type, $msg) = @_;
2304cbec18afSJoe Perches
2305cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2306de7d4f0eSAndy Whitcroft		our $clean = 0;
23076c72ffaaSAndy Whitcroft		our $cnt_chk++;
23083705ce5bSJoe Perches		return 1;
23096c72ffaaSAndy Whitcroft	}
23103705ce5bSJoe Perches	return 0;
2311de7d4f0eSAndy Whitcroft}
2312de7d4f0eSAndy Whitcroft
23136ecd9674SAndy Whitcroftsub check_absolute_file {
23146ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
23156ecd9674SAndy Whitcroft	my $file = $absolute;
23166ecd9674SAndy Whitcroft
23176ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
23186ecd9674SAndy Whitcroft
23196ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
23206ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
23216ecd9674SAndy Whitcroft		if (-f "$root/$file") {
23226ecd9674SAndy Whitcroft			##print "file<$file>\n";
23236ecd9674SAndy Whitcroft			last;
23246ecd9674SAndy Whitcroft		}
23256ecd9674SAndy Whitcroft	}
23266ecd9674SAndy Whitcroft	if (! -f _)  {
23276ecd9674SAndy Whitcroft		return 0;
23286ecd9674SAndy Whitcroft	}
23296ecd9674SAndy Whitcroft
23306ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
23316ecd9674SAndy Whitcroft	my $prefix = $absolute;
23326ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
23336ecd9674SAndy Whitcroft
23346ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
23356ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2336000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2337000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
23386ecd9674SAndy Whitcroft	}
23396ecd9674SAndy Whitcroft}
23406ecd9674SAndy Whitcroft
23413705ce5bSJoe Perchessub trim {
23423705ce5bSJoe Perches	my ($string) = @_;
23433705ce5bSJoe Perches
2344b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2345b34c648bSJoe Perches
2346b34c648bSJoe Perches	return $string;
2347b34c648bSJoe Perches}
2348b34c648bSJoe Perches
2349b34c648bSJoe Perchessub ltrim {
2350b34c648bSJoe Perches	my ($string) = @_;
2351b34c648bSJoe Perches
2352b34c648bSJoe Perches	$string =~ s/^\s+//;
2353b34c648bSJoe Perches
2354b34c648bSJoe Perches	return $string;
2355b34c648bSJoe Perches}
2356b34c648bSJoe Perches
2357b34c648bSJoe Perchessub rtrim {
2358b34c648bSJoe Perches	my ($string) = @_;
2359b34c648bSJoe Perches
2360b34c648bSJoe Perches	$string =~ s/\s+$//;
23613705ce5bSJoe Perches
23623705ce5bSJoe Perches	return $string;
23633705ce5bSJoe Perches}
23643705ce5bSJoe Perches
236552ea8506SJoe Perchessub string_find_replace {
236652ea8506SJoe Perches	my ($string, $find, $replace) = @_;
236752ea8506SJoe Perches
236852ea8506SJoe Perches	$string =~ s/$find/$replace/g;
236952ea8506SJoe Perches
237052ea8506SJoe Perches	return $string;
237152ea8506SJoe Perches}
237252ea8506SJoe Perches
23733705ce5bSJoe Perchessub tabify {
23743705ce5bSJoe Perches	my ($leading) = @_;
23753705ce5bSJoe Perches
2376713a09deSAntonio Borneo	my $source_indent = $tabsize;
23773705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
23783705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
23793705ce5bSJoe Perches
23803705ce5bSJoe Perches	#convert leading spaces to tabs
23813705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
23823705ce5bSJoe Perches	#Remove spaces before a tab
23833705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
23843705ce5bSJoe Perches
23853705ce5bSJoe Perches	return "$leading";
23863705ce5bSJoe Perches}
23873705ce5bSJoe Perches
2388d1fe9c09SJoe Perchessub pos_last_openparen {
2389d1fe9c09SJoe Perches	my ($line) = @_;
2390d1fe9c09SJoe Perches
2391d1fe9c09SJoe Perches	my $pos = 0;
2392d1fe9c09SJoe Perches
2393d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2394d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2395d1fe9c09SJoe Perches
2396d1fe9c09SJoe Perches	my $last_openparen = 0;
2397d1fe9c09SJoe Perches
2398d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2399d1fe9c09SJoe Perches		return -1;
2400d1fe9c09SJoe Perches	}
2401d1fe9c09SJoe Perches
2402d1fe9c09SJoe Perches	my $len = length($line);
2403d1fe9c09SJoe Perches
2404d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2405d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2406d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2407d1fe9c09SJoe Perches			$pos += length($1) - 1;
2408d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2409d1fe9c09SJoe Perches			$last_openparen = $pos;
2410d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2411d1fe9c09SJoe Perches			last;
2412d1fe9c09SJoe Perches		}
2413d1fe9c09SJoe Perches	}
2414d1fe9c09SJoe Perches
241591cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2416d1fe9c09SJoe Perches}
2417d1fe9c09SJoe Perches
2418f36d3eb8SJoe Perchessub get_raw_comment {
2419f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2420f36d3eb8SJoe Perches	my $comment = '';
2421f36d3eb8SJoe Perches
2422f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2423f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2424f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2425f36d3eb8SJoe Perches		}
2426f36d3eb8SJoe Perches	}
2427f36d3eb8SJoe Perches
2428f36d3eb8SJoe Perches	return $comment;
2429f36d3eb8SJoe Perches}
2430f36d3eb8SJoe Perches
24310a920b5bSAndy Whitcroftsub process {
24320a920b5bSAndy Whitcroft	my $filename = shift;
24330a920b5bSAndy Whitcroft
24340a920b5bSAndy Whitcroft	my $linenr=0;
24350a920b5bSAndy Whitcroft	my $prevline="";
2436c2fdda0dSAndy Whitcroft	my $prevrawline="";
24370a920b5bSAndy Whitcroft	my $stashline="";
2438c2fdda0dSAndy Whitcroft	my $stashrawline="";
24390a920b5bSAndy Whitcroft
24404a0df2efSAndy Whitcroft	my $length;
24410a920b5bSAndy Whitcroft	my $indent;
24420a920b5bSAndy Whitcroft	my $previndent=0;
24430a920b5bSAndy Whitcroft	my $stashindent=0;
24440a920b5bSAndy Whitcroft
2445de7d4f0eSAndy Whitcroft	our $clean = 1;
24460a920b5bSAndy Whitcroft	my $signoff = 0;
2447cd261496SGeert Uytterhoeven	my $author = '';
2448cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
244948ca2d8aSDwaipayan Ray	my $author_sob = '';
24500a920b5bSAndy Whitcroft	my $is_patch = 0;
2451133712a2SRob Herring	my $is_binding_patch = -1;
245229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
245315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
245444d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2455ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2456490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2457bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
24582a076f40SJoe Perches	my $commit_log_long_line = 0;
2459e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
246013f1937eSJoe Perches	my $reported_maintainer_file = 0;
2461fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2462fa64205dSPasi Savanainen
2463365dd4eaSJoe Perches	my $last_blank_line = 0;
24645e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2465365dd4eaSJoe Perches
246613214adfSAndy Whitcroft	our @report = ();
24676c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
24686c72ffaaSAndy Whitcroft	our $cnt_error = 0;
24696c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
24706c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
24716c72ffaaSAndy Whitcroft
24720a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
24730a920b5bSAndy Whitcroft	my $realfile = '';
24740a920b5bSAndy Whitcroft	my $realline = 0;
24750a920b5bSAndy Whitcroft	my $realcnt = 0;
24760a920b5bSAndy Whitcroft	my $here = '';
247777cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
24780a920b5bSAndy Whitcroft	my $in_comment = 0;
2479c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
24800a920b5bSAndy Whitcroft	my $first_line = 0;
24811e855726SWolfram Sang	my $p1_prefix = '';
24820a920b5bSAndy Whitcroft
248313214adfSAndy Whitcroft	my $prev_values = 'E';
248413214adfSAndy Whitcroft
248513214adfSAndy Whitcroft	# suppression flags
2486773647a0SAndy Whitcroft	my %suppress_ifbraces;
2487170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
24882b474a1aSAndy Whitcroft	my %suppress_export;
24893e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2490653d4876SAndy Whitcroft
24917e51f197SJoe Perches	my %signatures = ();
2492323c1260SJoe Perches
2493c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2494de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2495c2fdda0dSAndy Whitcroft	#
2496de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2497de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2498773647a0SAndy Whitcroft
2499d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2500d8b07710SJoe Perches
25019f3a8992SRob Herring	my $checklicenseline = 1;
25029f3a8992SRob Herring
2503773647a0SAndy Whitcroft	sanitise_line_reset();
2504c2fdda0dSAndy Whitcroft	my $line;
2505c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2506773647a0SAndy Whitcroft		$linenr++;
2507773647a0SAndy Whitcroft		$line = $rawline;
2508c2fdda0dSAndy Whitcroft
25093705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
25103705ce5bSJoe Perches
2511773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2512de7d4f0eSAndy Whitcroft			$setup_docs = 0;
25132581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2514de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2515de7d4f0eSAndy Whitcroft			}
2516773647a0SAndy Whitcroft			#next;
2517de7d4f0eSAndy Whitcroft		}
251874fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2519773647a0SAndy Whitcroft			$realline=$1-1;
2520773647a0SAndy Whitcroft			if (defined $2) {
2521773647a0SAndy Whitcroft				$realcnt=$3+1;
2522773647a0SAndy Whitcroft			} else {
2523773647a0SAndy Whitcroft				$realcnt=1+1;
2524773647a0SAndy Whitcroft			}
2525c45dcabdSAndy Whitcroft			$in_comment = 0;
2526773647a0SAndy Whitcroft
2527773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2528773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2529773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2530773647a0SAndy Whitcroft			# at context start.
2531773647a0SAndy Whitcroft			my $edge;
253201fa9147SAndy Whitcroft			my $cnt = $realcnt;
253301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
253401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
253501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
253601fa9147SAndy Whitcroft				$cnt--;
253701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2538721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2539fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2540fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2541fae17daeSAndy Whitcroft					($edge) = $1;
2542fae17daeSAndy Whitcroft					last;
2543fae17daeSAndy Whitcroft				}
2544773647a0SAndy Whitcroft			}
2545773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2546773647a0SAndy Whitcroft				$in_comment = 1;
2547773647a0SAndy Whitcroft			}
2548773647a0SAndy Whitcroft
2549773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2550773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2551773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2552773647a0SAndy Whitcroft			if (!defined $edge &&
255383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2554773647a0SAndy Whitcroft			{
2555773647a0SAndy Whitcroft				$in_comment = 1;
2556773647a0SAndy Whitcroft			}
2557773647a0SAndy Whitcroft
2558773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2559773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2560773647a0SAndy Whitcroft
2561171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2562773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2563171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2564773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2565773647a0SAndy Whitcroft		}
2566773647a0SAndy Whitcroft		push(@lines, $line);
2567773647a0SAndy Whitcroft
2568773647a0SAndy Whitcroft		if ($realcnt > 1) {
2569773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2570773647a0SAndy Whitcroft		} else {
2571773647a0SAndy Whitcroft			$realcnt = 0;
2572773647a0SAndy Whitcroft		}
2573773647a0SAndy Whitcroft
2574773647a0SAndy Whitcroft		#print "==>$rawline\n";
2575773647a0SAndy Whitcroft		#print "-->$line\n";
2576de7d4f0eSAndy Whitcroft
2577de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2578de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2579de7d4f0eSAndy Whitcroft		}
2580de7d4f0eSAndy Whitcroft	}
2581de7d4f0eSAndy Whitcroft
25826c72ffaaSAndy Whitcroft	$prefix = '';
25836c72ffaaSAndy Whitcroft
2584773647a0SAndy Whitcroft	$realcnt = 0;
2585773647a0SAndy Whitcroft	$linenr = 0;
2586194f66fcSJoe Perches	$fixlinenr = -1;
25870a920b5bSAndy Whitcroft	foreach my $line (@lines) {
25880a920b5bSAndy Whitcroft		$linenr++;
2589194f66fcSJoe Perches		$fixlinenr++;
25901b5539b1SJoe Perches		my $sline = $line;	#copy of $line
25911b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
25920a920b5bSAndy Whitcroft
2593c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2594f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
25956c72ffaaSAndy Whitcroft
259612c253abSJoe Perches# check if it's a mode change, rename or start of a patch
259712c253abSJoe Perches		if (!$in_commit_log &&
259812c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
259912c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
260012c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
260112c253abSJoe Perches			$is_patch = 1;
260212c253abSJoe Perches		}
260312c253abSJoe Perches
26040a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2605e518e9a5SJoe Perches		if (!$in_commit_log &&
260674fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
260774fd4f34SJoe Perches			my $context = $4;
26080a920b5bSAndy Whitcroft			$is_patch = 1;
26094a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
26100a920b5bSAndy Whitcroft			$realline=$1-1;
26110a920b5bSAndy Whitcroft			if (defined $2) {
26120a920b5bSAndy Whitcroft				$realcnt=$3+1;
26130a920b5bSAndy Whitcroft			} else {
26140a920b5bSAndy Whitcroft				$realcnt=1+1;
26150a920b5bSAndy Whitcroft			}
2616c2fdda0dSAndy Whitcroft			annotate_reset();
261713214adfSAndy Whitcroft			$prev_values = 'E';
261813214adfSAndy Whitcroft
2619773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2620170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
26212b474a1aSAndy Whitcroft			%suppress_export = ();
26223e469cdcSAndy Whitcroft			$suppress_statement = 0;
262374fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
262474fd4f34SJoe Perches				$context_function = $1;
262574fd4f34SJoe Perches			} else {
262674fd4f34SJoe Perches				undef $context_function;
262774fd4f34SJoe Perches			}
26280a920b5bSAndy Whitcroft			next;
26290a920b5bSAndy Whitcroft
26304a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
26314a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
26324a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2633773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
26340a920b5bSAndy Whitcroft			$realline++;
2635d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
26360a920b5bSAndy Whitcroft
26374a0df2efSAndy Whitcroft			# Measure the line length and indent.
2638c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
26390a920b5bSAndy Whitcroft
26400a920b5bSAndy Whitcroft			# Track the previous line.
26410a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
26420a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2643c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2644c2fdda0dSAndy Whitcroft
2645773647a0SAndy Whitcroft			#warn "line<$line>\n";
26466c72ffaaSAndy Whitcroft
2647d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2648d8aaf121SAndy Whitcroft			$realcnt--;
26490a920b5bSAndy Whitcroft		}
26500a920b5bSAndy Whitcroft
2651cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2652cc77cdcaSAndy Whitcroft
26536c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
26546c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2655773647a0SAndy Whitcroft
26562ac73b4fSJoe Perches		my $found_file = 0;
2657773647a0SAndy Whitcroft		# extract the filename as it passes
26583bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
26593bf9a009SRabin Vincent			$realfile = $1;
26602b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2661270c49a0SJoe Perches			$in_commit_log = 0;
26622ac73b4fSJoe Perches			$found_file = 1;
26633bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2664773647a0SAndy Whitcroft			$realfile = $1;
26652b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2666270c49a0SJoe Perches			$in_commit_log = 0;
26671e855726SWolfram Sang
26681e855726SWolfram Sang			$p1_prefix = $1;
2669e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2670e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2671000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2672000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
26731e855726SWolfram Sang			}
2674773647a0SAndy Whitcroft
2675c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2676000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2677000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2678773647a0SAndy Whitcroft			}
26792ac73b4fSJoe Perches			$found_file = 1;
26802ac73b4fSJoe Perches		}
26812ac73b4fSJoe Perches
268234d8815fSJoe Perches#make up the handle for any error we report on this line
268334d8815fSJoe Perches		if ($showfile) {
268434d8815fSJoe Perches			$prefix = "$realfile:$realline: "
268534d8815fSJoe Perches		} elsif ($emacs) {
26867d3a9f67SJoe Perches			if ($file) {
26877d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
26887d3a9f67SJoe Perches			} else {
268934d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
269034d8815fSJoe Perches			}
26917d3a9f67SJoe Perches		}
269234d8815fSJoe Perches
26932ac73b4fSJoe Perches		if ($found_file) {
269485b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
269585b0ee18SJoe Perches				WARN("OBSOLETE",
269685b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
269785b0ee18SJoe Perches			}
26987bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
26992ac73b4fSJoe Perches				$check = 1;
27002ac73b4fSJoe Perches			} else {
27012ac73b4fSJoe Perches				$check = $check_orig;
27022ac73b4fSJoe Perches			}
27039f3a8992SRob Herring			$checklicenseline = 1;
2704133712a2SRob Herring
2705133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2706133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2707133712a2SRob Herring
2708133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2709133712a2SRob Herring
2710133712a2SRob Herring				if (($last_binding_patch != -1) &&
2711133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2712133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2713858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2714133712a2SRob Herring				}
2715133712a2SRob Herring			}
2716133712a2SRob Herring
2717773647a0SAndy Whitcroft			next;
2718773647a0SAndy Whitcroft		}
2719773647a0SAndy Whitcroft
2720389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
27210a920b5bSAndy Whitcroft
2722c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2723c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2724c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
27250a920b5bSAndy Whitcroft
27266c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
27276c72ffaaSAndy Whitcroft
2728490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2729490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2730490b292cSJoe Perches		if ($in_commit_log) {
2731490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2732490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2733490b292cSJoe Perches			}
2734490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2735490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2736490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2737490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2738490b292cSJoe Perches		}
2739490b292cSJoe Perches
2740e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2741e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
274213e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
274313e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2744e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2745e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2746e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2747e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2748e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2749e518e9a5SJoe Perches		}
2750e518e9a5SJoe Perches
27513bf9a009SRabin Vincent# Check for incorrect file permissions
27523bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
27533bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
275404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
275504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2756000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2757000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
27583bf9a009SRabin Vincent			}
27593bf9a009SRabin Vincent		}
27603bf9a009SRabin Vincent
2761cd261496SGeert Uytterhoeven# Check the patch for a From:
2762cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2763cd261496SGeert Uytterhoeven			$author = $1;
2764e7f929f3SDwaipayan Ray			my $curline = $linenr;
2765e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2766e7f929f3SDwaipayan Ray				$author .= $1;
2767e7f929f3SDwaipayan Ray			}
2768cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2769cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2770dfa05c28SJoe Perches			$author = reformat_email($author);
2771cd261496SGeert Uytterhoeven		}
2772cd261496SGeert Uytterhoeven
277320112475SJoe Perches# Check the patch for a signoff:
2774dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
27754a0df2efSAndy Whitcroft			$signoff++;
277615662b3eSJoe Perches			$in_commit_log = 0;
277748ca2d8aSDwaipayan Ray			if ($author ne ''  && $authorsignoff != 1) {
2778fccaebf0SDwaipayan Ray				if (same_email_addresses($1, $author)) {
2779cd261496SGeert Uytterhoeven					$authorsignoff = 1;
278048ca2d8aSDwaipayan Ray				} else {
278148ca2d8aSDwaipayan Ray					my $ctx = $1;
278248ca2d8aSDwaipayan Ray					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
278348ca2d8aSDwaipayan Ray					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
278448ca2d8aSDwaipayan Ray
278548ca2d8aSDwaipayan Ray					if ($email_address eq $author_address && $email_name eq $author_name) {
278648ca2d8aSDwaipayan Ray						$author_sob = $ctx;
278748ca2d8aSDwaipayan Ray						$authorsignoff = 2;
278848ca2d8aSDwaipayan Ray					} elsif ($email_address eq $author_address) {
278948ca2d8aSDwaipayan Ray						$author_sob = $ctx;
279048ca2d8aSDwaipayan Ray						$authorsignoff = 3;
279148ca2d8aSDwaipayan Ray					} elsif ($email_name eq $author_name) {
279248ca2d8aSDwaipayan Ray						$author_sob = $ctx;
279348ca2d8aSDwaipayan Ray						$authorsignoff = 4;
279448ca2d8aSDwaipayan Ray
279548ca2d8aSDwaipayan Ray						my $address1 = $email_address;
279648ca2d8aSDwaipayan Ray						my $address2 = $author_address;
279748ca2d8aSDwaipayan Ray
279848ca2d8aSDwaipayan Ray						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
279948ca2d8aSDwaipayan Ray							$address1 = "$1$2";
280048ca2d8aSDwaipayan Ray						}
280148ca2d8aSDwaipayan Ray						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
280248ca2d8aSDwaipayan Ray							$address2 = "$1$2";
280348ca2d8aSDwaipayan Ray						}
280448ca2d8aSDwaipayan Ray						if ($address1 eq $address2) {
280548ca2d8aSDwaipayan Ray							$authorsignoff = 5;
280648ca2d8aSDwaipayan Ray						}
280748ca2d8aSDwaipayan Ray					}
2808cd261496SGeert Uytterhoeven				}
2809cd261496SGeert Uytterhoeven			}
28100a920b5bSAndy Whitcroft		}
281120112475SJoe Perches
281244d303ebSJoe Perches# Check for patch separator
281344d303ebSJoe Perches		if ($line =~ /^---$/) {
281444d303ebSJoe Perches			$has_patch_separator = 1;
281544d303ebSJoe Perches			$in_commit_log = 0;
281644d303ebSJoe Perches		}
281744d303ebSJoe Perches
2818e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2819e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2820e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2821e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2822e0d975b1SJoe Perches		}
2823e0d975b1SJoe Perches
282420112475SJoe Perches# Check signature styles
2825270c49a0SJoe Perches		if (!$in_header_lines &&
2826ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
282720112475SJoe Perches			my $space_before = $1;
282820112475SJoe Perches			my $sign_off = $2;
282920112475SJoe Perches			my $space_after = $3;
283020112475SJoe Perches			my $email = $4;
283120112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
283220112475SJoe Perches
2833ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2834831242abSAditya Srivastava				my $suggested_signature = find_standard_signature($sign_off);
2835831242abSAditya Srivastava				if ($suggested_signature eq "") {
2836ce0338dfSJoe Perches					WARN("BAD_SIGN_OFF",
2837ce0338dfSJoe Perches					     "Non-standard signature: $sign_off\n" . $herecurr);
2838831242abSAditya Srivastava				} else {
2839831242abSAditya Srivastava					if (WARN("BAD_SIGN_OFF",
2840831242abSAditya Srivastava						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2841831242abSAditya Srivastava					    $fix) {
2842831242abSAditya Srivastava						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2843831242abSAditya Srivastava					}
2844831242abSAditya Srivastava				}
2845ce0338dfSJoe Perches			}
284620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
28473705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28483705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
28493705ce5bSJoe Perches				    $fix) {
2850194f66fcSJoe Perches					$fixed[$fixlinenr] =
28513705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28523705ce5bSJoe Perches				}
285320112475SJoe Perches			}
285420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
28553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28563705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
28573705ce5bSJoe Perches				    $fix) {
2858194f66fcSJoe Perches					$fixed[$fixlinenr] =
28593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28603705ce5bSJoe Perches				}
28613705ce5bSJoe Perches
286220112475SJoe Perches			}
286320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
28643705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
28653705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
28663705ce5bSJoe Perches				    $fix) {
2867194f66fcSJoe Perches					$fixed[$fixlinenr] =
28683705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
28693705ce5bSJoe Perches				}
287020112475SJoe Perches			}
287120112475SJoe Perches
2872dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
287348ca2d8aSDwaipayan Ray			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
287420112475SJoe Perches			if ($suggested_email eq "") {
2875000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2876000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
287720112475SJoe Perches			} else {
287820112475SJoe Perches				my $dequoted = $suggested_email;
287920112475SJoe Perches				$dequoted =~ s/^"//;
288020112475SJoe Perches				$dequoted =~ s/" </ </;
288120112475SJoe Perches				# Don't force email to have quotes
288220112475SJoe Perches				# Allow just an angle bracketed address
2883fccaebf0SDwaipayan Ray				if (!same_email_addresses($email, $suggested_email)) {
2884fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2885fccaebf0SDwaipayan Ray						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2886fccaebf0SDwaipayan Ray					    $fix) {
2887fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2888fccaebf0SDwaipayan Ray					}
2889fccaebf0SDwaipayan Ray				}
2890fccaebf0SDwaipayan Ray
2891fccaebf0SDwaipayan Ray				# Address part shouldn't have comments
2892fccaebf0SDwaipayan Ray				my $stripped_address = $email_address;
2893fccaebf0SDwaipayan Ray				$stripped_address =~ s/\([^\(\)]*\)//g;
2894fccaebf0SDwaipayan Ray				if ($email_address ne $stripped_address) {
2895fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2896fccaebf0SDwaipayan Ray						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2897fccaebf0SDwaipayan Ray					    $fix) {
2898fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2899fccaebf0SDwaipayan Ray					}
2900fccaebf0SDwaipayan Ray				}
2901fccaebf0SDwaipayan Ray
2902fccaebf0SDwaipayan Ray				# Only one name comment should be allowed
2903fccaebf0SDwaipayan Ray				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2904fccaebf0SDwaipayan Ray				if ($comment_count > 1) {
2905000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2906fccaebf0SDwaipayan Ray					     "Use a single name comment in email: '$email'\n" . $herecurr);
2907fccaebf0SDwaipayan Ray				}
2908fccaebf0SDwaipayan Ray
2909fccaebf0SDwaipayan Ray
2910fccaebf0SDwaipayan Ray				# [email protected] or [email protected] shouldn't
2911e73d2715SDwaipayan Ray				# have an email name. In addition comments should strictly
2912fccaebf0SDwaipayan Ray				# begin with a #
2913fccaebf0SDwaipayan Ray				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2914fccaebf0SDwaipayan Ray					if (($comment ne "" && $comment !~ /^#.+/) ||
2915fccaebf0SDwaipayan Ray					    ($email_name ne "")) {
2916fccaebf0SDwaipayan Ray						my $cur_name = $email_name;
2917fccaebf0SDwaipayan Ray						my $new_comment = $comment;
2918fccaebf0SDwaipayan Ray						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2919fccaebf0SDwaipayan Ray
2920fccaebf0SDwaipayan Ray						# Remove brackets enclosing comment text
2921fccaebf0SDwaipayan Ray						# and # from start of comments to get comment text
2922fccaebf0SDwaipayan Ray						$new_comment =~ s/^\((.*)\)$/$1/;
2923fccaebf0SDwaipayan Ray						$new_comment =~ s/^\[(.*)\]$/$1/;
2924fccaebf0SDwaipayan Ray						$new_comment =~ s/^[\s\#]+|\s+$//g;
2925fccaebf0SDwaipayan Ray
2926fccaebf0SDwaipayan Ray						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2927fccaebf0SDwaipayan Ray						$new_comment = " # $new_comment" if ($new_comment ne "");
2928fccaebf0SDwaipayan Ray						my $new_email = "$email_address$new_comment";
2929fccaebf0SDwaipayan Ray
2930fccaebf0SDwaipayan Ray						if (WARN("BAD_STABLE_ADDRESS_STYLE",
2931fccaebf0SDwaipayan Ray							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2932fccaebf0SDwaipayan Ray						    $fix) {
2933fccaebf0SDwaipayan Ray							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2934fccaebf0SDwaipayan Ray						}
2935fccaebf0SDwaipayan Ray					}
2936fccaebf0SDwaipayan Ray				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2937fccaebf0SDwaipayan Ray					my $new_comment = $comment;
2938fccaebf0SDwaipayan Ray
2939fccaebf0SDwaipayan Ray					# Extract comment text from within brackets or
2940fccaebf0SDwaipayan Ray					# c89 style /*...*/ comments
2941fccaebf0SDwaipayan Ray					$new_comment =~ s/^\[(.*)\]$/$1/;
2942fccaebf0SDwaipayan Ray					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2943fccaebf0SDwaipayan Ray
2944fccaebf0SDwaipayan Ray					$new_comment = trim($new_comment);
2945fccaebf0SDwaipayan Ray					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2946fccaebf0SDwaipayan Ray					$new_comment = "($new_comment)" if ($new_comment ne "");
2947fccaebf0SDwaipayan Ray					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2948fccaebf0SDwaipayan Ray
2949fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
2950fccaebf0SDwaipayan Ray						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2951fccaebf0SDwaipayan Ray					    $fix) {
2952fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2953fccaebf0SDwaipayan Ray					}
295420112475SJoe Perches				}
29550a920b5bSAndy Whitcroft			}
29567e51f197SJoe Perches
29577e51f197SJoe Perches# Check for duplicate signatures
29587e51f197SJoe Perches			my $sig_nospace = $line;
29597e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
29607e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
29617e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
29627e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
29637e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
29647e51f197SJoe Perches			} else {
29657e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
29667e51f197SJoe Perches			}
29676c5d24eeSSean Christopherson
29686c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
29696c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
29706c5d24eeSSean Christopherson				if ($email eq $author) {
29716c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29726c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
29736c5d24eeSSean Christopherson				}
29746c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
29756c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29766c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
29776c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
29786c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29796c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29806c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
29816c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
29826c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
29836c5d24eeSSean Christopherson				}
29846c5d24eeSSean Christopherson			}
29850a920b5bSAndy Whitcroft		}
29860a920b5bSAndy Whitcroft
2987a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2988a2fe16b9SJoe Perches		if ($in_header_lines &&
2989a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2990a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2991a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2992a2fe16b9SJoe Perches		}
2993a2fe16b9SJoe Perches
299444d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
299544d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
29967580c5b9SAditya Srivastava			if (ERROR("GERRIT_CHANGE_ID",
29977580c5b9SAditya Srivastava			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
29987580c5b9SAditya Srivastava			    $fix) {
29997580c5b9SAditya Srivastava                                fix_delete_line($fixlinenr, $rawline);
30007580c5b9SAditya Srivastava                        }
30017ebd05efSChristopher Covington		}
30027ebd05efSChristopher Covington
3003369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
3004369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3005369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3006369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3007369c8dd3SJoe Perches					# timestamp
3008634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3009634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3010634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3011634cffccSJoe Perches					# stack dump address styles
3012369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
3013369c8dd3SJoe Perches		}
3014369c8dd3SJoe Perches
30152a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
30162a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
3017bf4daf12SJoe Perches		    length($line) > 75 &&
3018bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3019bf4daf12SJoe Perches					# file delta changes
3020bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3021bf4daf12SJoe Perches					# filename then :
302227b379afSAditya Srivastava		      $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
302327b379afSAditya Srivastava					# A Fixes: or Link: line or signature tag line
3024bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
30252a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
30262a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
30272a076f40SJoe Perches			$commit_log_long_line = 1;
30282a076f40SJoe Perches		}
30292a076f40SJoe Perches
3030bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
3031bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
3032bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
3033bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
3034bf4daf12SJoe Perches		}
3035bf4daf12SJoe Perches
30360d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
3037369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3038a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3039e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
3040fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3041aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3042369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3043bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3044fe043ea1SJoe Perches			my $init_char = "c";
3045fe043ea1SJoe Perches			my $orig_commit = "";
30460d7835fcSJoe Perches			my $short = 1;
30470d7835fcSJoe Perches			my $long = 0;
30480d7835fcSJoe Perches			my $case = 1;
30490d7835fcSJoe Perches			my $space = 1;
30500d7835fcSJoe Perches			my $hasdesc = 0;
305119c146a6SJoe Perches			my $hasparens = 0;
30520d7835fcSJoe Perches			my $id = '0123456789ab';
30530d7835fcSJoe Perches			my $orig_desc = "commit description";
30540d7835fcSJoe Perches			my $description = "";
30550d7835fcSJoe Perches
3056fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3057fe043ea1SJoe Perches				$init_char = $1;
3058fe043ea1SJoe Perches				$orig_commit = lc($2);
3059fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3060fe043ea1SJoe Perches				$orig_commit = lc($1);
3061fe043ea1SJoe Perches			}
3062fe043ea1SJoe Perches
30630d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
30640d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
30650d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
30660d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
30670d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
30680d7835fcSJoe Perches				$orig_desc = $1;
306919c146a6SJoe Perches				$hasparens = 1;
30700d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
30710d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
30720d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
30730d7835fcSJoe Perches				$orig_desc = $1;
307419c146a6SJoe Perches				$hasparens = 1;
3075b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3076b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
3077b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3078b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3079b671fde0SJoe Perches				$orig_desc = $1;
3080b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3081b671fde0SJoe Perches				$orig_desc .= " " . $1;
308219c146a6SJoe Perches				$hasparens = 1;
30830d7835fcSJoe Perches			}
30840d7835fcSJoe Perches
30850d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
30860d7835fcSJoe Perches							      $id, $orig_desc);
30870d7835fcSJoe Perches
3088948b133aSHeinrich Schuchardt			if (defined($id) &&
3089948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
3090d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
30910d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
30920d7835fcSJoe Perches			}
3093d311cd44SJoe Perches		}
3094d311cd44SJoe Perches
309513f1937eSJoe Perches# Check for added, moved or deleted files
309613f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
309713f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
309813f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
309913f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
310013f1937eSJoe Perches		      (defined($1) || defined($2))))) {
3101a82603a8SAndrew Jeffery			$is_patch = 1;
310213f1937eSJoe Perches			$reported_maintainer_file = 1;
310313f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
310413f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
310513f1937eSJoe Perches		}
310613f1937eSJoe Perches
3107e400edb1SRob Herring# Check for adding new DT bindings not in schema format
3108e400edb1SRob Herring		if (!$in_commit_log &&
3109e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3110e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3111e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
3112e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3113e400edb1SRob Herring		}
3114e400edb1SRob Herring
311500df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
31168905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3117000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
3118000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
31196c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
3120de7d4f0eSAndy Whitcroft		}
3121de7d4f0eSAndy Whitcroft
3122de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3123de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3124171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
3125171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3126171ae1a4SAndy Whitcroft
3127171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
3128171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3129171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
3130171ae1a4SAndy Whitcroft
313134d99219SJoe Perches			CHK("INVALID_UTF8",
3132000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
313300df344fSAndy Whitcroft		}
31340a920b5bSAndy Whitcroft
313515662b3eSJoe Perches# Check if it's the start of a commit log
313615662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
313715662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
3138eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
3139eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
314015662b3eSJoe Perches			$in_header_lines = 0;
314115662b3eSJoe Perches			$in_commit_log = 1;
3142ed43c4e5SAllen Hubbe			$has_commit_log = 1;
314315662b3eSJoe Perches		}
314415662b3eSJoe Perches
3145fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
3146fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
3147fa64205dSPasi Savanainen		if ($in_header_lines &&
3148fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3149fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
3150fa64205dSPasi Savanainen			$non_utf8_charset = 1;
3151fa64205dSPasi Savanainen		}
3152fa64205dSPasi Savanainen
3153fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
315415662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
3155fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
315615662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
315715662b3eSJoe Perches		}
315815662b3eSJoe Perches
3159d6430f71SJoe Perches# Check for absolute kernel paths in commit message
3160d6430f71SJoe Perches		if ($tree && $in_commit_log) {
3161d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
3162d6430f71SJoe Perches				my $file = $1;
3163d6430f71SJoe Perches
3164d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3165d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
3166d6430f71SJoe Perches					#
3167d6430f71SJoe Perches				} else {
3168d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
3169d6430f71SJoe Perches				}
3170d6430f71SJoe Perches			}
3171d6430f71SJoe Perches		}
3172d6430f71SJoe Perches
317366b47b4aSKees Cook# Check for various typo / spelling mistakes
317466d7a382SJoe Perches		if (defined($misspellings) &&
317566d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3176ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
317766b47b4aSKees Cook				my $typo = $1;
317866b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
317966b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
318066b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
31810675a8fbSJean Delvare				my $msg_level = \&WARN;
31820675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
31830675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
318466b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
318566b47b4aSKees Cook				    $fix) {
318666b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
318766b47b4aSKees Cook				}
318866b47b4aSKees Cook			}
318966b47b4aSKees Cook		}
319066b47b4aSKees Cook
3191a8dd86bfSMatteo Croce# check for invalid commit id
3192a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3193a8dd86bfSMatteo Croce			my $id;
3194a8dd86bfSMatteo Croce			my $description;
3195a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
3196a8dd86bfSMatteo Croce			if (!defined($id)) {
3197a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
3198a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3199a8dd86bfSMatteo Croce			}
3200a8dd86bfSMatteo Croce		}
3201a8dd86bfSMatteo Croce
3202310cd06bSJoe Perches# check for repeated words separated by a single space
32038d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
32048d0325ccSAditya Srivastava		if (($rawline =~ /^\+/ || $in_commit_log) &&
32058d0325ccSAditya Srivastava		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
32061db81a68SDwaipayan Ray			pos($rawline) = 1 if (!$in_commit_log);
3207310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3208310cd06bSJoe Perches
3209310cd06bSJoe Perches				my $first = $1;
3210310cd06bSJoe Perches				my $second = $2;
32111db81a68SDwaipayan Ray				my $start_pos = $-[1];
32121db81a68SDwaipayan Ray				my $end_pos = $+[2];
3213310cd06bSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3214310cd06bSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3215310cd06bSJoe Perches					next;
3216310cd06bSJoe Perches				}
3217310cd06bSJoe Perches
32181db81a68SDwaipayan Ray				next if (lc($first) ne lc($second));
3219310cd06bSJoe Perches				next if ($first eq 'long');
3220310cd06bSJoe Perches
32211db81a68SDwaipayan Ray				# check for character before and after the word matches
32221db81a68SDwaipayan Ray				my $start_char = '';
32231db81a68SDwaipayan Ray				my $end_char = '';
32241db81a68SDwaipayan Ray				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
32251db81a68SDwaipayan Ray				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
32261db81a68SDwaipayan Ray
32271db81a68SDwaipayan Ray				next if ($start_char =~ /^\S$/);
32281db81a68SDwaipayan Ray				next if (index(" \t.,;?!", $end_char) == -1);
32291db81a68SDwaipayan Ray
32308d0325ccSAditya Srivastava                                # avoid repeating hex occurrences like 'ff ff fe 09 ...'
32318d0325ccSAditya Srivastava                                if ($first =~ /\b[0-9a-f]{2,}\b/i) {
32328d0325ccSAditya Srivastava                                        next if (!exists($allow_repeated_words{lc($first)}));
32338d0325ccSAditya Srivastava                                }
32348d0325ccSAditya Srivastava
3235310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3236310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3237310cd06bSJoe Perches				    $fix) {
3238310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3239310cd06bSJoe Perches				}
3240310cd06bSJoe Perches			}
3241310cd06bSJoe Perches
3242310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3243310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3244310cd06bSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3245310cd06bSJoe Perches				my $last_word = $1;
3246310cd06bSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3247310cd06bSJoe Perches					if (WARN("REPEATED_WORD",
3248310cd06bSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3249310cd06bSJoe Perches					    $fix) {
3250310cd06bSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3251310cd06bSJoe Perches					}
3252310cd06bSJoe Perches				}
3253310cd06bSJoe Perches			}
3254310cd06bSJoe Perches		}
3255310cd06bSJoe Perches
325630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
325730670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
325800df344fSAndy Whitcroft
32590a920b5bSAndy Whitcroft#trailing whitespace
32609c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
3261c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3262d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3263d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
3264d5e616fcSJoe Perches			    $fix) {
3265194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3266d5e616fcSJoe Perches			}
3267c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3268c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32693705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
32703705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
32713705ce5bSJoe Perches			    $fix) {
3272194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
32733705ce5bSJoe Perches			}
32743705ce5bSJoe Perches
3275d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32760a920b5bSAndy Whitcroft		}
32775368df20SAndy Whitcroft
32784783f894SJosh Triplett# Check for FSF mailing addresses.
3279109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
32801bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
32813e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
32823e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
32834783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32840675a8fbSJean Delvare			my $msg_level = \&ERROR;
32850675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
32860675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
32874783f894SJosh 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)
32884783f894SJosh Triplett		}
32894783f894SJosh Triplett
32903354957aSAndi Kleen# check for Kconfig help text having a real description
32919fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
32929fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
32933354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3294678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3295678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3296678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3297678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
32983354957aSAndi Kleen			my $length = 0;
32999fe287d7SAndy Whitcroft			my $cnt = $realcnt;
33009fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
33019fe287d7SAndy Whitcroft			my $f;
3302a1385803SAndy Whitcroft			my $is_start = 0;
33039fe287d7SAndy Whitcroft			my $is_end = 0;
3304a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
33059fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
33069fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
33079fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
33089fe287d7SAndy Whitcroft
33099fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
33108d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3311a1385803SAndy Whitcroft
331286adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3313a1385803SAndy Whitcroft					$is_start = 1;
331422a4ac02SMasahiro Yamada				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
3315a1385803SAndy Whitcroft					$length = -1;
3316a1385803SAndy Whitcroft				}
3317a1385803SAndy Whitcroft
33189fe287d7SAndy Whitcroft				$f =~ s/^.//;
33193354957aSAndi Kleen				$f =~ s/#.*//;
33203354957aSAndi Kleen				$f =~ s/^\s+//;
33213354957aSAndi Kleen				next if ($f =~ /^$/);
3322678ae162SUlf Magnusson
3323678ae162SUlf Magnusson				# This only checks context lines in the patch
3324678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3325678ae162SUlf Magnusson				# positives, even though some of these are
3326678ae162SUlf Magnusson				# common words in help texts
3327678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3328678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
33299fe287d7SAndy Whitcroft					$is_end = 1;
33309fe287d7SAndy Whitcroft					last;
33319fe287d7SAndy Whitcroft				}
33323354957aSAndi Kleen				$length++;
33333354957aSAndi Kleen			}
333456193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3335000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
333656193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
333756193274SVadim Bendebury			}
3338a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
33393354957aSAndi Kleen		}
33403354957aSAndi Kleen
33417ccf41a8SJoe Perches# check MAINTAINERS entries
33427ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
33437ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
33447ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3345628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3346628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3347628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3348628f91a2SJoe Perches				    $fix) {
3349628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3350628f91a2SJoe Perches				}
3351628f91a2SJoe Perches			}
33527ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
33537ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
33547ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
33557ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
33567ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
33577ccf41a8SJoe Perches				my $cur = $1;
33587ccf41a8SJoe Perches				my $curval = $2;
33597ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
33607ccf41a8SJoe Perches				my $prev = $1;
33617ccf41a8SJoe Perches				my $prevval = $2;
33627ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
33637ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
33647ccf41a8SJoe Perches				if ($curindex < 0) {
33657ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
33667ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
33677ccf41a8SJoe Perches				} else {
33687ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
33697ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33707ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
33717ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
33727ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
33737ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
33747ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
33757ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
33767ccf41a8SJoe Perches					}
33777ccf41a8SJoe Perches				}
33787ccf41a8SJoe Perches			}
33797ccf41a8SJoe Perches		}
3380628f91a2SJoe Perches
3381327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3382327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3383327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3384327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3385327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3386327953e9SChristoph Jaeger		}
3387327953e9SChristoph Jaeger
3388c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3389c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3390c68e5878SArnaud Lacombe			my $flag = $1;
3391c68e5878SArnaud Lacombe			my $replacement = {
3392c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3393c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3394c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3395c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3396c68e5878SArnaud Lacombe			};
3397c68e5878SArnaud Lacombe
3398c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3399c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3400c68e5878SArnaud Lacombe		}
3401c68e5878SArnaud Lacombe
3402bff5da43SRob Herring# check for DT compatible documentation
34037dd05b38SFlorian Vaussard		if (defined $root &&
34047dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
34057dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
34067dd05b38SFlorian Vaussard
3407bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3408bff5da43SRob Herring
3409cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3410852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3411cc93319bSFlorian Vaussard
3412bff5da43SRob Herring			foreach my $compat (@compats) {
3413bff5da43SRob Herring				my $compat2 = $compat;
3414185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3415185d566bSRob Herring				my $compat3 = $compat;
3416185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3417185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3418bff5da43SRob Herring				if ( $? >> 8 ) {
3419bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3420bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3421bff5da43SRob Herring				}
3422bff5da43SRob Herring
34234fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
34244fbf32a6SFlorian Vaussard				my $vendor = $1;
3425852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3426bff5da43SRob Herring				if ( $? >> 8 ) {
3427bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3428cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3429bff5da43SRob Herring				}
3430bff5da43SRob Herring			}
3431bff5da43SRob Herring		}
3432bff5da43SRob Herring
34339f3a8992SRob Herring# check for using SPDX license tag at beginning of files
34349f3a8992SRob Herring		if ($realline == $checklicenseline) {
34359f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
34369f3a8992SRob Herring				$checklicenseline = 2;
34379f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
34389f3a8992SRob Herring				my $comment = "";
34399f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
34409f3a8992SRob Herring					$comment = '/*';
34419f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
34429f3a8992SRob Herring					$comment = '//';
3443c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
34449f3a8992SRob Herring					$comment = '#';
34459f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
34469f3a8992SRob Herring					$comment = '..';
34479f3a8992SRob Herring				}
34489f3a8992SRob Herring
3449fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3450fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3451fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3452ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3453fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3454fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3455fdf13693SJoe Perches				}
3456fdf13693SJoe Perches
34579f3a8992SRob Herring				if ($comment !~ /^$/ &&
3458ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
34599f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
34609f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
34613b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
34623b6e8ac9SJoe Perches					my $spdx_license = $1;
34633b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
34643b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
34653b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
34663b6e8ac9SJoe Perches					}
346750c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
346850c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
346950c92900SLubomir Rintel						my $msg_level = \&WARN;
347050c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
347150c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
347250c92900SLubomir Rintel
347350c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
347450c92900SLubomir Rintel						    $fix) {
347550c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
347650c92900SLubomir Rintel						}
347750c92900SLubomir Rintel					}
34789f3a8992SRob Herring				}
34799f3a8992SRob Herring			}
34809f3a8992SRob Herring		}
34819f3a8992SRob Herring
3482a0154cdbSJoe Perches# check for embedded filenames
3483a0154cdbSJoe Perches		if ($rawline =~ /^\+.*\Q$realfile\E/) {
3484a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3485a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3486a0154cdbSJoe Perches		}
3487a0154cdbSJoe Perches
34885368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3489d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
34905368df20SAndy Whitcroft
3491a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3492a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3493a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3494a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3495a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3496a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3497a8da38a9SJoe Perches		}
3498a8da38a9SJoe Perches
349947e0c88bSJoe Perches# line length limit (with some exclusions)
350047e0c88bSJoe Perches#
350147e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
350247e0c88bSJoe Perches#	logging functions like pr_info that end in a string
350347e0c88bSJoe Perches#	lines with a single string
350447e0c88bSJoe Perches#	#defines that are a single string
35052e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
350647e0c88bSJoe Perches#
350747e0c88bSJoe Perches# There are 3 different line length message types:
3508ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
350947e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
351047e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
351147e0c88bSJoe Perches#
351247e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
351347e0c88bSJoe Perches#
351447e0c88bSJoe Perches
3515b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
351647e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
351747e0c88bSJoe Perches
351847e0c88bSJoe Perches			# Check the allowed long line types first
351947e0c88bSJoe Perches
352047e0c88bSJoe Perches			# logging functions that end in a string that starts
352147e0c88bSJoe Perches			# before $max_line_length
352247e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
352347e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
352447e0c88bSJoe Perches				$msg_type = "";
352547e0c88bSJoe Perches
352647e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
352747e0c88bSJoe Perches			# #defines with only strings
352847e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
352947e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
353047e0c88bSJoe Perches				$msg_type = "";
353147e0c88bSJoe Perches
3532cc147506SJoe Perches			# More special cases
3533cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3534cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3535d560a5f8SJoe Perches				$msg_type = "";
3536d560a5f8SJoe Perches
35372e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
35382e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
35392e4bbbc5SAndreas Brauchli				$msg_type = "";
35402e4bbbc5SAndreas Brauchli
354147e0c88bSJoe Perches			# Otherwise set the alternate message types
354247e0c88bSJoe Perches
354347e0c88bSJoe Perches			# a comment starts before $max_line_length
354447e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
354547e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
354647e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
354747e0c88bSJoe Perches
354847e0c88bSJoe Perches			# a quoted string starts before $max_line_length
354947e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
355047e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
355147e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
355247e0c88bSJoe Perches			}
355347e0c88bSJoe Perches
355447e0c88bSJoe Perches			if ($msg_type ne "" &&
355547e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3556bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3557bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3558bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3559bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
35600a920b5bSAndy Whitcroft			}
356147e0c88bSJoe Perches		}
35620a920b5bSAndy Whitcroft
35638905a67cSAndy Whitcroft# check for adding lines without a newline.
35648905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
356547ca69b8STom Rix			if (WARN("MISSING_EOF_NEWLINE",
356647ca69b8STom Rix			         "adding a line without newline at end of file\n" . $herecurr) &&
356747ca69b8STom Rix			    $fix) {
356847ca69b8STom Rix				fix_delete_line($fixlinenr+1, "No newline at end of file");
356947ca69b8STom Rix			}
35708905a67cSAndy Whitcroft		}
35718905a67cSAndy Whitcroft
3572b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3573de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
35740a920b5bSAndy Whitcroft
35750a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3576713a09deSAntonio Borneo# more than $tabsize must use tabs.
3577c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3578c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3579c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3580d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
35813705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
35823705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
35833705ce5bSJoe Perches			    $fix) {
3584194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
35853705ce5bSJoe Perches			}
35860a920b5bSAndy Whitcroft		}
35870a920b5bSAndy Whitcroft
358808e44365SAlberto Panizzo# check for space before tabs.
358908e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
359008e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35913705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
35923705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
35933705ce5bSJoe Perches			    $fix) {
3594194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3595713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3596194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3597c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
35983705ce5bSJoe Perches			}
359908e44365SAlberto Panizzo		}
360008e44365SAlberto Panizzo
36016a487211SJoe Perches# check for assignments on the start of a line
36026a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3603da7355abSAditya Srivastava			my $operator = $1;
3604da7355abSAditya Srivastava			if (CHK("ASSIGNMENT_CONTINUATIONS",
3605da7355abSAditya Srivastava				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3606da7355abSAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
3607da7355abSAditya Srivastava				# add assignment operator to the previous line, remove from current line
3608da7355abSAditya Srivastava				$fixed[$fixlinenr - 1] .= " $operator";
3609da7355abSAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3610da7355abSAditya Srivastava			}
36116a487211SJoe Perches		}
36126a487211SJoe Perches
3613d1fe9c09SJoe Perches# check for && or || at the start of a line
3614d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
36158e08f076SAditya Srivastava			my $operator = $1;
36168e08f076SAditya Srivastava			if (CHK("LOGICAL_CONTINUATIONS",
36178e08f076SAditya Srivastava				"Logical continuations should be on the previous line\n" . $hereprev) &&
36188e08f076SAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
36198e08f076SAditya Srivastava				# insert logical operator at last non-comment, non-whitepsace char on previous line
36208e08f076SAditya Srivastava				$prevline =~ /[\s$;]*$/;
36218e08f076SAditya Srivastava				my $line_end = substr($prevrawline, $-[0]);
36228e08f076SAditya Srivastava				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
36238e08f076SAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
36248e08f076SAditya Srivastava			}
3625d1fe9c09SJoe Perches		}
3626d1fe9c09SJoe Perches
3627a91e8994SJoe Perches# check indentation starts on a tab stop
36285b57980dSJoe Perches		if ($perl_version_ok &&
3629bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3630a91e8994SJoe Perches			my $indent = length($1);
3631713a09deSAntonio Borneo			if ($indent % $tabsize) {
3632a91e8994SJoe Perches				if (WARN("TABSTOP",
3633a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3634a91e8994SJoe Perches				    $fix) {
3635713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3636a91e8994SJoe Perches				}
3637a91e8994SJoe Perches			}
3638a91e8994SJoe Perches		}
3639a91e8994SJoe Perches
3640d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
36415b57980dSJoe Perches		if ($perl_version_ok &&
3642fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3643d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3644d1fe9c09SJoe Perches			my $oldindent = $1;
3645d1fe9c09SJoe Perches			my $rest = $2;
3646d1fe9c09SJoe Perches
3647d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3648d1fe9c09SJoe Perches			if ($pos >= 0) {
3649b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3650b34a26f3SJoe Perches				my $newindent = $2;
3651d1fe9c09SJoe Perches
3652d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3653713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3654713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3655d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3656d1fe9c09SJoe Perches
3657d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3658d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
36593705ce5bSJoe Perches
36603705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
36613705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
36623705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3663194f66fcSJoe Perches						$fixed[$fixlinenr] =~
36643705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
36653705ce5bSJoe Perches					}
3666d1fe9c09SJoe Perches				}
3667d1fe9c09SJoe Perches			}
3668d1fe9c09SJoe Perches		}
3669d1fe9c09SJoe Perches
36706ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
36716ab3a970SJoe Perches# avoid checking a few false positives:
36726ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
36736ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
36746ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
36756ab3a970SJoe Perches#   multiline macros that define functions
36766ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
36776ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
36786ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
36793705ce5bSJoe Perches			if (CHK("SPACING",
3680f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
36813705ce5bSJoe Perches			    $fix) {
3682194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3683f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
36843705ce5bSJoe Perches			}
3685aad4f614SJoe Perches		}
3686aad4f614SJoe Perches
368786406b1cSJoe Perches# Block comment styles
368886406b1cSJoe Perches# Networking with an initial /*
368905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3690fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
369185ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
3692c70735c2SŁukasz Stelmach		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
369305880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
369405880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
369505880600SJoe Perches		}
369605880600SJoe Perches
369786406b1cSJoe Perches# Block comments use * on subsequent lines
369886406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
369986406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3700a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
370161135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3702a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
370386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
370486406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3705a605e32eSJoe Perches		}
3706a605e32eSJoe Perches
370786406b1cSJoe Perches# Block comments use */ on trailing lines
370886406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3709c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3710c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3711c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
371286406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
371386406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
371405880600SJoe Perches		}
371505880600SJoe Perches
371608eb9b80SJoe Perches# Block comment * alignment
371708eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3718af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3719af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3720af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
372108eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3722af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3723af207524SJoe Perches			my $oldindent;
372408eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3725af207524SJoe Perches			if (defined($1)) {
3726af207524SJoe Perches				$oldindent = expand_tabs($1);
3727af207524SJoe Perches			} else {
3728af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3729af207524SJoe Perches				$oldindent = expand_tabs($1);
3730af207524SJoe Perches			}
373108eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
373208eb9b80SJoe Perches			my $newindent = $1;
373308eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3734af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
373508eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
373608eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
373708eb9b80SJoe Perches			}
373808eb9b80SJoe Perches		}
373908eb9b80SJoe Perches
37407f619191SJoe Perches# check for missing blank lines after struct/union declarations
37417f619191SJoe Perches# with exceptions for various attributes and macros
37427f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
37437f619191SJoe Perches		    $line =~ /^\+/ &&
37447f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
37457f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
37467f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
37477f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
37487f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
37497f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
37507f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
37510bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
37527f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3753d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3754d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3755d752fcc8SJoe Perches			    $fix) {
3756f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3757d752fcc8SJoe Perches			}
37587f619191SJoe Perches		}
37597f619191SJoe Perches
3760365dd4eaSJoe Perches# check for multiple consecutive blank lines
3761365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3762365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3763365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3764d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3765d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3766d752fcc8SJoe Perches			    $fix) {
3767f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3768d752fcc8SJoe Perches			}
3769d752fcc8SJoe Perches
3770365dd4eaSJoe Perches			$last_blank_line = $linenr;
3771365dd4eaSJoe Perches		}
3772365dd4eaSJoe Perches
37733b617e3bSJoe Perches# check for missing blank lines after declarations
37743f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
37753f7bac03SJoe Perches			# actual declarations
37763f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
37775a4e1fd3SJoe Perches			# function pointer declarations
37785a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
37793f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
37803f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
37813f7bac03SJoe Perches			# known declaration macros
37823f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
37833f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
37843f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
37853f7bac03SJoe Perches			# other possible extensions of declaration lines
37863f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
37873f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
37883f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
37893f7bac03SJoe Perches			# looks like a declaration
37903f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
37915a4e1fd3SJoe Perches			# function pointer declarations
37925a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
37933f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
37943f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
37953f7bac03SJoe Perches			# known declaration macros
37963f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
37973f7bac03SJoe Perches			# start of struct or union or enum
3798328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
37993f7bac03SJoe Perches			# start or end of block or continuation of declaration
38003f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
38013f7bac03SJoe Perches			# bitfield continuation
38023f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
38033f7bac03SJoe Perches			# other possible extensions of declaration lines
38043f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
38053f7bac03SJoe Perches			# indentation of previous and current line are the same
38063f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3807d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3808d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3809d752fcc8SJoe Perches			    $fix) {
3810f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3811d752fcc8SJoe Perches			}
38123b617e3bSJoe Perches		}
38133b617e3bSJoe Perches
38145f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
38156b4c5bebSAndy Whitcroft# Exceptions:
38166b4c5bebSAndy Whitcroft#  1) within comments
38176b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
38186b4c5bebSAndy Whitcroft#  3) hanging labels
38193705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
38205f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
38213705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
38223705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
38233705ce5bSJoe Perches			    $fix) {
3824194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
38253705ce5bSJoe Perches			}
38265f7ddae6SRaffaele Recalcati		}
38275f7ddae6SRaffaele Recalcati
3828b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3829b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3830b9ea10d6SAndy Whitcroft
38315751a24eSJoe Perches# check for unusual line ending [ or (
38325751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
38335751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
38345751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
38355751a24eSJoe Perches		}
38365751a24eSJoe Perches
38374dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
38384dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
38394dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
38404dbed76fSJoe Perches			$context_function = $1;
38414dbed76fSJoe Perches		}
38424dbed76fSJoe Perches
38434dbed76fSJoe Perches# check if this appears to be the end of function declaration
38444dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
38454dbed76fSJoe Perches			undef $context_function;
38464dbed76fSJoe Perches		}
38474dbed76fSJoe Perches
3848032a4c0fSJoe Perches# check indentation of any line with a bare else
3849840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3850032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3851032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3852032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3853840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3854840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3855840080a0SJoe Perches			     defined $lines[$linenr] &&
3856840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3857032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3858032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3859032a4c0fSJoe Perches			}
3860032a4c0fSJoe Perches		}
3861032a4c0fSJoe Perches
3862c00df19aSJoe Perches# check indentation of a line with a break;
3863dc58bc55SJoe Perches# if the previous line is a goto, return or break
3864dc58bc55SJoe Perches# and is indented the same # of tabs
3865c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3866c00df19aSJoe Perches			my $tabs = $1;
3867dc58bc55SJoe Perches			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3868dc58bc55SJoe Perches				if (WARN("UNNECESSARY_BREAK",
3869dc58bc55SJoe Perches					 "break is not useful after a $1\n" . $hereprev) &&
3870dc58bc55SJoe Perches				    $fix) {
3871dc58bc55SJoe Perches					fix_delete_line($fixlinenr, $rawline);
3872dc58bc55SJoe Perches				}
3873c00df19aSJoe Perches			}
3874c00df19aSJoe Perches		}
3875c00df19aSJoe Perches
3876c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3877cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3878000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3879000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3880c2fdda0dSAndy Whitcroft		}
388122f2a2efSAndy Whitcroft
388256e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
388356e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
388456e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
388556e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
388656e77d70SJoe Perches		}
388756e77d70SJoe Perches
38889c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
38892b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
38902b474a1aSAndy Whitcroft		    $realline_next);
38913e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3892ca819864SJoe Perches		if ($linenr > $suppress_statement &&
38931b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3894170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3895f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3896171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3897171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3898171ae1a4SAndy Whitcroft
38993e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
39003e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
39013e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
39023e469cdcSAndy Whitcroft			# until we hit end of it.
39033e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
39043e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
39053e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
39063e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
39073e469cdcSAndy Whitcroft			}
3908f74bd194SAndy Whitcroft
39092b474a1aSAndy Whitcroft			# Find the real next line.
39102b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
39112b474a1aSAndy Whitcroft			if (defined $realline_next &&
39122b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
39132b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
39142b474a1aSAndy Whitcroft				$realline_next++;
39152b474a1aSAndy Whitcroft			}
39162b474a1aSAndy Whitcroft
3917171ae1a4SAndy Whitcroft			my $s = $stat;
3918171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3919cf655043SAndy Whitcroft
3920c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3921171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3922c2fdda0dSAndy Whitcroft
3923c2fdda0dSAndy Whitcroft			# Ignore functions being called
3924171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3925c2fdda0dSAndy Whitcroft
3926463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3927463f2864SAndy Whitcroft
3928c45dcabdSAndy Whitcroft			# declarations always start with types
3929d2506586SAndy 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) {
3930c45dcabdSAndy Whitcroft				my $type = $1;
3931c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3932c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3933c45dcabdSAndy Whitcroft
39346c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3935a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3936c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3937c2fdda0dSAndy Whitcroft			}
39388905a67cSAndy Whitcroft
39396c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
394065863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3941c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
39429c0ca6f9SAndy Whitcroft			}
39438905a67cSAndy Whitcroft
39448905a67cSAndy Whitcroft			# Check for any sort of function declaration.
39458905a67cSAndy Whitcroft			# int foo(something bar, other baz);
39468905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3947171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
39488905a67cSAndy Whitcroft				my ($name_len) = length($1);
39498905a67cSAndy Whitcroft
3950cf655043SAndy Whitcroft				my $ctx = $s;
3951773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
39528905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3953cf655043SAndy Whitcroft
39548905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3955c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
39568905a67cSAndy Whitcroft
3957c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
39588905a67cSAndy Whitcroft					}
39598905a67cSAndy Whitcroft				}
39608905a67cSAndy Whitcroft			}
39618905a67cSAndy Whitcroft
39629c0ca6f9SAndy Whitcroft		}
39639c0ca6f9SAndy Whitcroft
396400df344fSAndy Whitcroft#
396500df344fSAndy Whitcroft# Checks which may be anchored in the context.
396600df344fSAndy Whitcroft#
396700df344fSAndy Whitcroft
396800df344fSAndy Whitcroft# Check for switch () and associated case and default
396900df344fSAndy Whitcroft# statements should be at the same indent.
397000df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
397100df344fSAndy Whitcroft			my $err = '';
397200df344fSAndy Whitcroft			my $sep = '';
397300df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
397400df344fSAndy Whitcroft			shift(@ctx);
397500df344fSAndy Whitcroft			for my $ctx (@ctx) {
397600df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
397700df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
397800df344fSAndy Whitcroft							$indent != $cindent) {
397900df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
398000df344fSAndy Whitcroft					$sep = '';
398100df344fSAndy Whitcroft				} else {
398200df344fSAndy Whitcroft					$sep = "[...]\n";
398300df344fSAndy Whitcroft				}
398400df344fSAndy Whitcroft			}
398500df344fSAndy Whitcroft			if ($err ne '') {
3986000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3987000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3988de7d4f0eSAndy Whitcroft			}
3989de7d4f0eSAndy Whitcroft		}
3990de7d4f0eSAndy Whitcroft
3991de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3992de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
39930fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3994773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3995773647a0SAndy Whitcroft
39969c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
39978eef05ddSJoe Perches
39988eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
39998eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
40008eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
40018eef05ddSJoe Perches			}
40028eef05ddSJoe Perches
4003de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
4004de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
4005de7d4f0eSAndy Whitcroft
4006548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
4007548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
4008de7d4f0eSAndy Whitcroft
4009548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4010548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
4011548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
4012548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4013548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4014773647a0SAndy Whitcroft				$ctx_ln++;
4015773647a0SAndy Whitcroft			}
4016548596d5SAndy Whitcroft
401753210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
401853210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4019773647a0SAndy Whitcroft
4020773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4021000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
4022000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
402301464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
402400df344fSAndy Whitcroft			}
4025773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4026773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
4027773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
4028773647a0SAndy Whitcroft			{
40299c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
40309c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
4031000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
4032000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
403301464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
40349c0ca6f9SAndy Whitcroft				}
40359c0ca6f9SAndy Whitcroft			}
403600df344fSAndy Whitcroft		}
403700df344fSAndy Whitcroft
40384d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
4039f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
40403e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
40413e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
40423e469cdcSAndy Whitcroft					if (!defined $stat);
40434d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
40444d001e4dSAndy Whitcroft
40454d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
40464d001e4dSAndy Whitcroft
40479f5af480SJoe Perches			# remove inline comments
40489f5af480SJoe Perches			$s =~ s/$;/ /g;
40499f5af480SJoe Perches			$c =~ s/$;/ /g;
40504d001e4dSAndy Whitcroft
40514d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
40526f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
40536f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
40544d001e4dSAndy Whitcroft
40559f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
40569f5af480SJoe Perches			# none on the first line, and are going to readd them
40579f5af480SJoe Perches			# where necessary.
40589f5af480SJoe Perches			$s =~ s/\n./\n/gs;
40599f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
40609f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
40619f5af480SJoe Perches			}
40629f5af480SJoe Perches
40634d001e4dSAndy Whitcroft			# We want to check the first line inside the block
40644d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
40654d001e4dSAndy Whitcroft			#  1) any blank line termination
40664d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
40674d001e4dSAndy Whitcroft			#  3) any do (...) {
40684d001e4dSAndy Whitcroft			my $continuation = 0;
40694d001e4dSAndy Whitcroft			my $check = 0;
40704d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
40714d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
40724d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
40734d001e4dSAndy Whitcroft				$continuation = 1;
40744d001e4dSAndy Whitcroft			}
40759bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
40764d001e4dSAndy Whitcroft				$check = 1;
40774d001e4dSAndy Whitcroft				$cond_lines++;
40784d001e4dSAndy Whitcroft			}
40794d001e4dSAndy Whitcroft
40804d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
40814d001e4dSAndy Whitcroft			# preprocessor statement.
40824d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
40834d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
40844d001e4dSAndy Whitcroft				$check = 0;
40854d001e4dSAndy Whitcroft			}
40864d001e4dSAndy Whitcroft
40879bd49efeSAndy Whitcroft			my $cond_ptr = -1;
4088740504c6SAndy Whitcroft			$continuation = 0;
40899bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
40909bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
40914d001e4dSAndy Whitcroft
4092f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
4093f16fa28fSAndy Whitcroft				# is not linear.
4094f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4095f16fa28fSAndy Whitcroft					$check = 0;
4096f16fa28fSAndy Whitcroft				}
4097f16fa28fSAndy Whitcroft
40989bd49efeSAndy Whitcroft				# Ignore:
40999bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
41009bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
41019bd49efeSAndy Whitcroft				#  3) labels.
4102740504c6SAndy Whitcroft				if ($continuation ||
4103740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
41049bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
41059bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
4106740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
410730dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
41089bd49efeSAndy Whitcroft						$cond_lines++;
41099bd49efeSAndy Whitcroft					}
41104d001e4dSAndy Whitcroft				}
411130dad6ebSAndy Whitcroft			}
41124d001e4dSAndy Whitcroft
41134d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
41144d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
41154d001e4dSAndy Whitcroft
41164d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
41174d001e4dSAndy Whitcroft			# this is not this patch's fault.
41184d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
41194d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
41204d001e4dSAndy Whitcroft				$check = 0;
41214d001e4dSAndy Whitcroft			}
41224d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
41234d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
41244d001e4dSAndy Whitcroft			}
41254d001e4dSAndy Whitcroft
41269bd49efeSAndy 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";
41274d001e4dSAndy Whitcroft
41289f5af480SJoe Perches			if ($check && $s ne '' &&
4129713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
41309f5af480SJoe Perches			     ($sindent < $indent) ||
4131f6950a73SJoe Perches			     ($sindent == $indent &&
4132f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4133713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
4134000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
4135000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
41364d001e4dSAndy Whitcroft			}
41374d001e4dSAndy Whitcroft		}
41384d001e4dSAndy Whitcroft
41396c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
41406c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
41411f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
41421f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
41436c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
4144c2fdda0dSAndy Whitcroft		if ($dbg_values) {
4145c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
4146cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
4147cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
41481f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
4149c2fdda0dSAndy Whitcroft		}
41506c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
41516c72ffaaSAndy Whitcroft
415200df344fSAndy Whitcroft#ignore lines not being added
41533705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
415400df344fSAndy Whitcroft
415599ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
415699ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
415799ca38c2SJoe Perches#	struct foo bar = *(&(bar));
415899ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
415999ca38c2SJoe Perches			my $var = $1;
416099ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
416199ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
416299ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
416399ca38c2SJoe Perches			}
416499ca38c2SJoe Perches		}
416599ca38c2SJoe Perches
416611ca40a0SJoe Perches# check for dereferences that span multiple lines
416711ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
416811ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
416911ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
417011ca40a0SJoe Perches			my $ref = $1;
417111ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
417211ca40a0SJoe Perches			$ref .= $1;
417311ca40a0SJoe Perches			$ref =~ s/\s//g;
417411ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
417511ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
417611ca40a0SJoe Perches		}
417711ca40a0SJoe Perches
4178a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
4179c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4180a1ce18e4SJoe Perches			my $type = $1;
4181a1ce18e4SJoe Perches			my $var = $2;
4182207a8e84SJoe Perches			$var = "" if (!defined $var);
4183207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4184a1ce18e4SJoe Perches				my $sign = $1;
4185a1ce18e4SJoe Perches				my $pointer = $2;
4186a1ce18e4SJoe Perches
4187a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
4188a1ce18e4SJoe Perches
4189a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
4190a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4191a1ce18e4SJoe Perches				    $fix) {
4192a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
4193207a8e84SJoe Perches					my $comp_pointer = $pointer;
4194207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
4195207a8e84SJoe Perches					$decl .= $comp_pointer;
4196207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
4197207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4198a1ce18e4SJoe Perches				}
4199a1ce18e4SJoe Perches			}
4200a1ce18e4SJoe Perches		}
4201a1ce18e4SJoe Perches
4202653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
42037429c690SAndy Whitcroft		if ($dbg_type) {
42047429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
4205000d1cc1SJoe Perches				ERROR("TEST_TYPE",
4206000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
42077429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4208000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
4209000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
42107429c690SAndy Whitcroft			}
4211653d4876SAndy Whitcroft			next;
4212653d4876SAndy Whitcroft		}
4213a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
4214a1ef277eSAndy Whitcroft		if ($dbg_attr) {
42159360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
4216000d1cc1SJoe Perches				ERROR("TEST_ATTR",
4217000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
42189360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4219000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
4220000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
4221a1ef277eSAndy Whitcroft			}
4222a1ef277eSAndy Whitcroft			next;
4223a1ef277eSAndy Whitcroft		}
4224653d4876SAndy Whitcroft
4225f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
422699423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
422799423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
4228d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4229d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4230f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4231f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4232f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4233d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4234d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4235f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4236d752fcc8SJoe Perches				$fixedline = $line;
42378d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4238f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4239d752fcc8SJoe Perches			}
4240f0a594c1SAndy Whitcroft		}
4241f0a594c1SAndy Whitcroft
424200df344fSAndy Whitcroft#
424300df344fSAndy Whitcroft# Checks which are anchored on the added line.
424400df344fSAndy Whitcroft#
424500df344fSAndy Whitcroft
4246653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4247c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4248653d4876SAndy Whitcroft			my $path = $1;
4249653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4250000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4251495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4252495e9d84SJoe Perches			}
4253495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4254495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4255495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4256653d4876SAndy Whitcroft			}
4257653d4876SAndy Whitcroft		}
4258653d4876SAndy Whitcroft
425900df344fSAndy Whitcroft# no C99 // comments
426000df344fSAndy Whitcroft		if ($line =~ m{//}) {
42613705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
42623705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
42633705ce5bSJoe Perches			    $fix) {
4264194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
42653705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
42663705ce5bSJoe Perches					my $comment = trim($1);
4267194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
42683705ce5bSJoe Perches				}
42693705ce5bSJoe Perches			}
427000df344fSAndy Whitcroft		}
427100df344fSAndy Whitcroft		# Remove C99 comments.
42720a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
42736c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
42740a920b5bSAndy Whitcroft
42752b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
42762b474a1aSAndy Whitcroft# the whole statement.
42772b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
42782b474a1aSAndy Whitcroft		if (defined $realline_next &&
42792b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
42802b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
42812b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
42822b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
42833cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
42843cbf62dfSAndy Whitcroft			# a prefix:
42853cbf62dfSAndy Whitcroft			#   XXX(foo);
42863cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4287653d4876SAndy Whitcroft			my $name = $1;
428887a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
42893cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
42903cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
42913cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
42923cbf62dfSAndy Whitcroft
42933cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
42942b474a1aSAndy Whitcroft				\n.}\s*$|
429548012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
429648012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
429748012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
42982b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
42992b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
430048012058SAndy Whitcroft			    )/x) {
43012b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
43022b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
43032b474a1aSAndy Whitcroft			} else {
43042b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
43050a920b5bSAndy Whitcroft			}
43060a920b5bSAndy Whitcroft		}
43072b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
43082b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
43092b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
43102b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
43112b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
43122b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
43132b474a1aSAndy Whitcroft		}
43142b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
43152b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4316000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4317000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
43182b474a1aSAndy Whitcroft		}
43190a920b5bSAndy Whitcroft
43205150bda4SJoe Eloff# check for global initialisers.
43216d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
4322d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
43236d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4324d5e616fcSJoe Perches			    $fix) {
43256d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4326d5e616fcSJoe Perches			}
4327f0a594c1SAndy Whitcroft		}
43280a920b5bSAndy Whitcroft# check for static initialisers.
43296d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4330d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
43316d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4332d5e616fcSJoe Perches				      $herecurr) &&
4333d5e616fcSJoe Perches			    $fix) {
43346d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4335d5e616fcSJoe Perches			}
43360a920b5bSAndy Whitcroft		}
43370a920b5bSAndy Whitcroft
43381813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
43391813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
43401813087dSJoe Perches			my $tmp = trim($1);
43411813087dSJoe Perches			WARN("MISORDERED_TYPE",
43421813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
43431813087dSJoe Perches		}
43441813087dSJoe Perches
4345809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4346809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4347809e082eSJoe Perches			my $type = trim($1);
4348809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4349809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4350809e082eSJoe Perches			my $new_type = $type;
4351809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4352809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4353809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4354809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4355809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4356809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4357809e082eSJoe Perches			$new_type = trim($new_type);
4358809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4359809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4360809e082eSJoe Perches			    $fix) {
4361809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4362809e082eSJoe Perches			}
4363809e082eSJoe Perches		}
4364809e082eSJoe Perches
4365cb710ecaSJoe Perches# check for static const char * arrays.
4366cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4367000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4368000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4369cb710ecaSJoe Perches				$herecurr);
4370cb710ecaSJoe Perches		}
4371cb710ecaSJoe Perches
437277b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
437377b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
437477b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
437577b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
437677b8c0a8SJoe Perches			    $fix) {
437777b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
437877b8c0a8SJoe Perches			}
437977b8c0a8SJoe Perches		}
438077b8c0a8SJoe Perches
4381cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4382cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4383000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4384000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4385cb710ecaSJoe Perches				$herecurr);
4386cb710ecaSJoe Perches		}
4387cb710ecaSJoe Perches
4388ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4389ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4390ab7e23f3SJoe Perches			my $found = $1;
4391ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4392ab7e23f3SJoe Perches				WARN("CONST_CONST",
4393ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4394ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4395ab7e23f3SJoe Perches				WARN("CONST_CONST",
4396ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4397ab7e23f3SJoe Perches			}
4398ab7e23f3SJoe Perches		}
4399ab7e23f3SJoe Perches
440073169765SJoe Perches# check for const static or static <non ptr type> const declarations
440173169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
440273169765SJoe Perches		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
440373169765SJoe Perches		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
440473169765SJoe Perches			if (WARN("STATIC_CONST",
440573169765SJoe Perches				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
440673169765SJoe Perches			    $fix) {
440773169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
440873169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
440973169765SJoe Perches			}
441073169765SJoe Perches		}
441173169765SJoe Perches
44129b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
44139b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
44149b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
44159b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
44169b0fa60dSJoe Perches				$herecurr);
44179b0fa60dSJoe Perches               }
44189b0fa60dSJoe Perches
4419b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4420b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4421b598b670SJoe Perches			my $array = $1;
4422b598b670SJoe 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*\))@) {
4423b598b670SJoe Perches				my $array_div = $1;
4424b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4425b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4426b598b670SJoe Perches				    $fix) {
4427b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4428b598b670SJoe Perches				}
4429b598b670SJoe Perches			}
4430b598b670SJoe Perches		}
4431b598b670SJoe Perches
4432b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
443316b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4434b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4435b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4436b36190c5SJoe Perches			    $fix) {
4437194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4438b36190c5SJoe Perches			}
4439b36190c5SJoe Perches		}
4440b36190c5SJoe Perches
4441653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4442653d4876SAndy Whitcroft# make sense.
4443653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
44448054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4445c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
44468ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
444746d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4448000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4449000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
44500a920b5bSAndy Whitcroft		}
44510a920b5bSAndy Whitcroft
44520a920b5bSAndy Whitcroft# * goes on variable not on type
445365863862SAndy Whitcroft		# (char*[ const])
4454bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4455bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
44563705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4457d8aaf121SAndy Whitcroft
445865863862SAndy Whitcroft			# Should start with a space.
445965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
446065863862SAndy Whitcroft			# Should not end with a space.
446165863862SAndy Whitcroft			$to =~ s/\s+$//;
446265863862SAndy Whitcroft			# '*'s should not have spaces between.
4463f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
446465863862SAndy Whitcroft			}
4465d8aaf121SAndy Whitcroft
44663705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
446765863862SAndy Whitcroft			if ($from ne $to) {
44683705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
44693705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
44703705ce5bSJoe Perches				    $fix) {
44713705ce5bSJoe Perches					my $sub_from = $ident;
44723705ce5bSJoe Perches					my $sub_to = $ident;
44733705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4474194f66fcSJoe Perches					$fixed[$fixlinenr] =~
44753705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
44763705ce5bSJoe Perches				}
447765863862SAndy Whitcroft			}
4478bfcb2cc7SAndy Whitcroft		}
4479bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4480bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
44813705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4482d8aaf121SAndy Whitcroft
448365863862SAndy Whitcroft			# Should start with a space.
448465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
448565863862SAndy Whitcroft			# Should not end with a space.
448665863862SAndy Whitcroft			$to =~ s/\s+$//;
448765863862SAndy Whitcroft			# '*'s should not have spaces between.
4488f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
448965863862SAndy Whitcroft			}
449065863862SAndy Whitcroft			# Modifiers should have spaces.
449165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
449265863862SAndy Whitcroft
44933705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4494667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
44953705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
44963705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
44973705ce5bSJoe Perches				    $fix) {
44983705ce5bSJoe Perches
44993705ce5bSJoe Perches					my $sub_from = $match;
45003705ce5bSJoe Perches					my $sub_to = $match;
45013705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4502194f66fcSJoe Perches					$fixed[$fixlinenr] =~
45033705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
45043705ce5bSJoe Perches				}
450565863862SAndy Whitcroft			}
45060a920b5bSAndy Whitcroft		}
45070a920b5bSAndy Whitcroft
45089d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
45099d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
45100675a8fbSJean Delvare			my $msg_level = \&WARN;
45110675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
45120675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
45139d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
45149d3e3c70SJoe Perches		}
45150a920b5bSAndy Whitcroft
45169d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
45178905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4518000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4519000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
45208905a67cSAndy Whitcroft		}
45218905a67cSAndy Whitcroft
452217441227SJoe Perches# check for uses of printk_ratelimit
452317441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4524000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4525000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
452617441227SJoe Perches		}
452717441227SJoe Perches
4528eeef5733SJoe Perches# printk should use KERN_* levels
4529eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4530000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4531eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
453200df344fSAndy Whitcroft		}
45330a920b5bSAndy Whitcroft
4534243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4535243f3803SJoe Perches			my $orig = $1;
4536243f3803SJoe Perches			my $level = lc($orig);
4537243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
45388f26b837SJoe Perches			my $level2 = $level;
45398f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4540243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4541daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4542243f3803SJoe Perches		}
4543243f3803SJoe Perches
4544dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4545dc139313SJoe Perches			my $orig = $1;
4546dc139313SJoe Perches			my $level = lc($orig);
4547dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4548dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4549dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4550dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4551dc139313SJoe Perches		}
4552dc139313SJoe Perches
45538020b253SNicolas Boichat# trace_printk should not be used in production code.
45548020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
45558020b253SNicolas Boichat			WARN("TRACE_PRINTK",
45568020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
45578020b253SNicolas Boichat		}
45588020b253SNicolas Boichat
455991c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
456091c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
456191c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
456291c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
456391c9afafSAndy Lutomirski			WARN("ENOSYS",
456491c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
456591c9afafSAndy Lutomirski		}
456691c9afafSAndy Lutomirski
45676b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
45686b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
45696b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
45706b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
45716b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
45726b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
45736b9ea5ffSJakub Kicinski			    $fix) {
45746b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
45756b9ea5ffSJakub Kicinski			}
45766b9ea5ffSJakub Kicinski		}
45776b9ea5ffSJakub Kicinski
4578653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4579653d4876SAndy Whitcroft# or if closed on same line
45805b57980dSJoe Perches		if ($perl_version_ok &&
45812d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
45822d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
45832d453e3bSJoe Perches		    $sline !~ /}/) {
45848d182478SJoe Perches			if (ERROR("OPEN_BRACE",
45852d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
45868d182478SJoe Perches			    $fix) {
45878d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45888d182478SJoe Perches				my $fixed_line = $rawline;
458903f49351SDwaipayan Ray				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
45908d182478SJoe Perches				my $line1 = $1;
45918d182478SJoe Perches				my $line2 = $2;
45928d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
45938d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
45948d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
45958d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
45968d182478SJoe Perches				}
45978d182478SJoe Perches			}
45980a920b5bSAndy Whitcroft		}
4599653d4876SAndy Whitcroft
46008905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
46018905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
46028905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
46038d182478SJoe Perches			if (ERROR("OPEN_BRACE",
46048d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
46058d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
46068d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
46078d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
46088d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
46098d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
46108d182478SJoe Perches				$fixedline = $rawline;
46118d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
46128d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
46138d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46148d182478SJoe Perches				}
46158d182478SJoe Perches			}
46168905a67cSAndy Whitcroft		}
46178905a67cSAndy Whitcroft
46180c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
46193705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
46203705ce5bSJoe Perches			if (WARN("SPACING",
46213705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
46223705ce5bSJoe Perches			    $fix) {
4623194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46243705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
46253705ce5bSJoe Perches			}
46260c73b4ebSAndy Whitcroft		}
46270c73b4ebSAndy Whitcroft
462831070b5dSJoe Perches# Function pointer declarations
462931070b5dSJoe Perches# check spacing between type, funcptr, and args
463031070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
463191f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
463231070b5dSJoe Perches			my $declare = $1;
463331070b5dSJoe Perches			my $pre_pointer_space = $2;
463431070b5dSJoe Perches			my $post_pointer_space = $3;
463531070b5dSJoe Perches			my $funcname = $4;
463631070b5dSJoe Perches			my $post_funcname_space = $5;
463731070b5dSJoe Perches			my $pre_args_space = $6;
463831070b5dSJoe Perches
463991f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
464091f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
464191f72e9cSJoe Perches# don't need a space so don't warn for those.
464291f72e9cSJoe Perches			my $post_declare_space = "";
464391f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
464491f72e9cSJoe Perches				$post_declare_space = $1;
464591f72e9cSJoe Perches				$declare = rtrim($declare);
464691f72e9cSJoe Perches			}
464791f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
464831070b5dSJoe Perches				WARN("SPACING",
464931070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
465091f72e9cSJoe Perches				$post_declare_space = " ";
465131070b5dSJoe Perches			}
465231070b5dSJoe Perches
465331070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
465491f72e9cSJoe Perches# This test is not currently implemented because these declarations are
465591f72e9cSJoe Perches# equivalent to
465691f72e9cSJoe Perches#	int  foo(int bar, ...)
465791f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
465891f72e9cSJoe Perches#
465991f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
466091f72e9cSJoe Perches#				WARN("SPACING",
466191f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
466291f72e9cSJoe Perches#			}
466331070b5dSJoe Perches
466431070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
466531070b5dSJoe Perches			if (defined $pre_pointer_space &&
466631070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
466731070b5dSJoe Perches				WARN("SPACING",
466831070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
466931070b5dSJoe Perches			}
467031070b5dSJoe Perches
467131070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
467231070b5dSJoe Perches			if (defined $post_pointer_space &&
467331070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
467431070b5dSJoe Perches				WARN("SPACING",
467531070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
467631070b5dSJoe Perches			}
467731070b5dSJoe Perches
467831070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
467931070b5dSJoe Perches			if (defined $post_funcname_space &&
468031070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
468131070b5dSJoe Perches				WARN("SPACING",
468231070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
468331070b5dSJoe Perches			}
468431070b5dSJoe Perches
468531070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
468631070b5dSJoe Perches			if (defined $pre_args_space &&
468731070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
468831070b5dSJoe Perches				WARN("SPACING",
468931070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
469031070b5dSJoe Perches			}
469131070b5dSJoe Perches
469231070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4693194f66fcSJoe Perches				$fixed[$fixlinenr] =~
469491f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
469531070b5dSJoe Perches			}
469631070b5dSJoe Perches		}
469731070b5dSJoe Perches
46988d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
46998d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4700fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4701fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
47028d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
47038d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
47048d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4705fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
470638dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
47073705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
47083705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
47093705ce5bSJoe Perches				    $fix) {
4710194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
47113705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
47123705ce5bSJoe Perches				}
47138d31cfceSAndy Whitcroft			}
47148d31cfceSAndy Whitcroft		}
47158d31cfceSAndy Whitcroft
4716f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
47176c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4718c2fdda0dSAndy Whitcroft			my $name = $1;
4719773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4720773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4721c2fdda0dSAndy Whitcroft
4722c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4723773647a0SAndy Whitcroft			if ($name =~ /^(?:
4724773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4725773647a0SAndy Whitcroft				volatile|__volatile__|
4726773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4727773647a0SAndy Whitcroft				asm|__asm__)$/x)
4728773647a0SAndy Whitcroft			{
4729c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4730c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4731c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4732c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4733773647a0SAndy Whitcroft
4734773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4735c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4736c2fdda0dSAndy Whitcroft
4737c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4738c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4739773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4740c2fdda0dSAndy Whitcroft
4741c2fdda0dSAndy Whitcroft			} else {
47423705ce5bSJoe Perches				if (WARN("SPACING",
47433705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
47443705ce5bSJoe Perches					     $fix) {
4745194f66fcSJoe Perches					$fixed[$fixlinenr] =~
47463705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
47473705ce5bSJoe Perches				}
4748f0a594c1SAndy Whitcroft			}
47496c72ffaaSAndy Whitcroft		}
47509a4cad4eSEric Nelson
4751653d4876SAndy Whitcroft# Check operator spacing.
47520a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
47533705ce5bSJoe Perches			my $fixed_line = "";
47543705ce5bSJoe Perches			my $line_fixed = 0;
47553705ce5bSJoe Perches
47569c0ca6f9SAndy Whitcroft			my $ops = qr{
47579c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
47589c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
47599c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
47601f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
476184731623SJoe Perches				\?:|\?|:
47629c0ca6f9SAndy Whitcroft			}x;
4763cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
47643705ce5bSJoe Perches
47653705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
47663705ce5bSJoe Perches##			foreach my $el (@elements) {
47673705ce5bSJoe Perches##				print("el: <$el>\n");
47683705ce5bSJoe Perches##			}
47693705ce5bSJoe Perches
47703705ce5bSJoe Perches			my @fix_elements = ();
477100df344fSAndy Whitcroft			my $off = 0;
47726c72ffaaSAndy Whitcroft
47733705ce5bSJoe Perches			foreach my $el (@elements) {
47743705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
47753705ce5bSJoe Perches				$off += length($el);
47763705ce5bSJoe Perches			}
47773705ce5bSJoe Perches
47783705ce5bSJoe Perches			$off = 0;
47793705ce5bSJoe Perches
47806c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4781b34c648bSJoe Perches			my $last_after = -1;
47826c72ffaaSAndy Whitcroft
47830a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
47843705ce5bSJoe Perches
47853705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
47863705ce5bSJoe Perches
47873705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
47883705ce5bSJoe Perches
47894a0df2efSAndy Whitcroft				$off += length($elements[$n]);
47904a0df2efSAndy Whitcroft
479125985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4792773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4793773647a0SAndy Whitcroft				my $cc = '';
4794773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4795773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4796773647a0SAndy Whitcroft				}
4797773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4798773647a0SAndy Whitcroft
47994a0df2efSAndy Whitcroft				my $a = '';
48004a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
48014a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4802cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
48034a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
48044a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4805773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
48064a0df2efSAndy Whitcroft
48070a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
48084a0df2efSAndy Whitcroft
48094a0df2efSAndy Whitcroft				my $c = '';
48100a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
48114a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
48124a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4813cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
48144a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
48154a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
48168b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
48174a0df2efSAndy Whitcroft				} else {
48184a0df2efSAndy Whitcroft					$c = 'E';
48190a920b5bSAndy Whitcroft				}
48200a920b5bSAndy Whitcroft
48214a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
48224a0df2efSAndy Whitcroft
48234a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
48244a0df2efSAndy Whitcroft
48256c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4826de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
48270a920b5bSAndy Whitcroft
482874048ed8SAndy Whitcroft				# Pull out the value of this operator.
48296c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
48300a920b5bSAndy Whitcroft
48311f65f947SAndy Whitcroft				# Get the full operator variant.
48321f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
48331f65f947SAndy Whitcroft
483413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
483513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4836d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
483713214adfSAndy Whitcroft
4838cf655043SAndy Whitcroft#				# Ignore comments
4839cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
484013214adfSAndy Whitcroft
4841d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
484213214adfSAndy Whitcroft				} elsif ($op eq ';') {
4843cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4844cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
48453705ce5bSJoe Perches						if (ERROR("SPACING",
48463705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4847b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
48483705ce5bSJoe Perches							$line_fixed = 1;
48493705ce5bSJoe Perches						}
4850d8aaf121SAndy Whitcroft					}
4851d8aaf121SAndy Whitcroft
4852d8aaf121SAndy Whitcroft				# // is a comment
4853d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
48540a920b5bSAndy Whitcroft
4855b00e4814SJoe Perches				#   :   when part of a bitfield
4856b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4857b00e4814SJoe Perches					# skip the bitfield test for now
4858b00e4814SJoe Perches
48591f65f947SAndy Whitcroft				# No spaces for:
48601f65f947SAndy Whitcroft				#   ->
4861b00e4814SJoe Perches				} elsif ($op eq '->') {
48624a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
48633705ce5bSJoe Perches						if (ERROR("SPACING",
48643705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4865b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
48663705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
48673705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
48683705ce5bSJoe Perches							}
4869b34c648bSJoe Perches							$line_fixed = 1;
48703705ce5bSJoe Perches						}
48710a920b5bSAndy Whitcroft					}
48720a920b5bSAndy Whitcroft
48732381097bSJoe Perches				# , must not have a space before and must have a space on the right.
48740a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
48752381097bSJoe Perches					my $rtrim_before = 0;
48762381097bSJoe Perches					my $space_after = 0;
48772381097bSJoe Perches					if ($ctx =~ /Wx./) {
48782381097bSJoe Perches						if (ERROR("SPACING",
48792381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
48802381097bSJoe Perches							$line_fixed = 1;
48812381097bSJoe Perches							$rtrim_before = 1;
48822381097bSJoe Perches						}
48832381097bSJoe Perches					}
4884cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
48853705ce5bSJoe Perches						if (ERROR("SPACING",
48863705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
48873705ce5bSJoe Perches							$line_fixed = 1;
4888b34c648bSJoe Perches							$last_after = $n;
48892381097bSJoe Perches							$space_after = 1;
48902381097bSJoe Perches						}
48912381097bSJoe Perches					}
48922381097bSJoe Perches					if ($rtrim_before || $space_after) {
48932381097bSJoe Perches						if ($rtrim_before) {
48942381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
48952381097bSJoe Perches						} else {
48962381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
48972381097bSJoe Perches						}
48982381097bSJoe Perches						if ($space_after) {
48992381097bSJoe Perches							$good .= " ";
49003705ce5bSJoe Perches						}
49010a920b5bSAndy Whitcroft					}
49020a920b5bSAndy Whitcroft
49039c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
490474048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
49059c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
49069c0ca6f9SAndy Whitcroft
49079c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
49089c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
49099c0ca6f9SAndy Whitcroft				# unary operator, or a cast
49109c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
491174048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
49120d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4913cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
49143705ce5bSJoe Perches						if (ERROR("SPACING",
49153705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4916b34c648bSJoe Perches							if ($n != $last_after + 2) {
4917b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
49183705ce5bSJoe Perches								$line_fixed = 1;
49193705ce5bSJoe Perches							}
49200a920b5bSAndy Whitcroft						}
4921b34c648bSJoe Perches					}
4922a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4923171ae1a4SAndy Whitcroft						# A unary '*' may be const
4924171ae1a4SAndy Whitcroft
4925171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
49263705ce5bSJoe Perches						if (ERROR("SPACING",
49273705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4928b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
49293705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
49303705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
49313705ce5bSJoe Perches							}
4932b34c648bSJoe Perches							$line_fixed = 1;
49333705ce5bSJoe Perches						}
49340a920b5bSAndy Whitcroft					}
49350a920b5bSAndy Whitcroft
49360a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
49370a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4938773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
49393705ce5bSJoe Perches						if (ERROR("SPACING",
49403705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4941b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
49423705ce5bSJoe Perches							$line_fixed = 1;
49433705ce5bSJoe Perches						}
49440a920b5bSAndy Whitcroft					}
4945773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4946773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
49473705ce5bSJoe Perches						if (ERROR("SPACING",
49483705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4949b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
49503705ce5bSJoe Perches							$line_fixed = 1;
49513705ce5bSJoe Perches						}
4952653d4876SAndy Whitcroft					}
4953773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
49543705ce5bSJoe Perches						if (ERROR("SPACING",
49553705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4956b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
49573705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
49583705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4959773647a0SAndy Whitcroft							}
4960b34c648bSJoe Perches							$line_fixed = 1;
49613705ce5bSJoe Perches						}
49623705ce5bSJoe Perches					}
49630a920b5bSAndy Whitcroft
49640a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
49659c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
49669c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
49679c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4968c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4969c2fdda0dSAndy Whitcroft					 $op eq '%')
49700a920b5bSAndy Whitcroft				{
4971d2e025f3SJoe Perches					if ($check) {
4972d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4973d2e025f3SJoe Perches							if (CHK("SPACING",
4974d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4975d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4976d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4977d2e025f3SJoe Perches								$line_fixed = 1;
4978d2e025f3SJoe Perches							}
4979d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4980d2e025f3SJoe Perches							if (CHK("SPACING",
4981d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4982d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4983d2e025f3SJoe Perches								$line_fixed = 1;
4984d2e025f3SJoe Perches							}
4985d2e025f3SJoe Perches						}
4986d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
49873705ce5bSJoe Perches						if (ERROR("SPACING",
49883705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4989b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4990b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4991b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4992b34c648bSJoe Perches							}
49933705ce5bSJoe Perches							$line_fixed = 1;
49943705ce5bSJoe Perches						}
49950a920b5bSAndy Whitcroft					}
49960a920b5bSAndy Whitcroft
49971f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
49981f65f947SAndy Whitcroft				# terminating a case value or a label.
49991f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
50001f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
50013705ce5bSJoe Perches						if (ERROR("SPACING",
50023705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5003b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
50043705ce5bSJoe Perches							$line_fixed = 1;
50053705ce5bSJoe Perches						}
50061f65f947SAndy Whitcroft					}
50071f65f947SAndy Whitcroft
50080a920b5bSAndy Whitcroft				# All the others need spaces both sides.
5009cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
50101f65f947SAndy Whitcroft					my $ok = 0;
50111f65f947SAndy Whitcroft
501222f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
50131f65f947SAndy Whitcroft					if (($op eq '<' &&
50141f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
50151f65f947SAndy Whitcroft					    ($op eq '>' &&
50161f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
50171f65f947SAndy Whitcroft					{
50181f65f947SAndy Whitcroft						$ok = 1;
50191f65f947SAndy Whitcroft					}
50201f65f947SAndy Whitcroft
5021e0df7e1fSJoe Perches					# for asm volatile statements
5022e0df7e1fSJoe Perches					# ignore a colon with another
5023e0df7e1fSJoe Perches					# colon immediately before or after
5024e0df7e1fSJoe Perches					if (($op eq ':') &&
5025e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
5026e0df7e1fSJoe Perches						$ok = 1;
5027e0df7e1fSJoe Perches					}
5028e0df7e1fSJoe Perches
502984731623SJoe Perches					# messages are ERROR, but ?: are CHK
50301f65f947SAndy Whitcroft					if ($ok == 0) {
50310675a8fbSJean Delvare						my $msg_level = \&ERROR;
50320675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
503384731623SJoe Perches
50340675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
50353705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
5036b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5037b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5038b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5039b34c648bSJoe Perches							}
50403705ce5bSJoe Perches							$line_fixed = 1;
50413705ce5bSJoe Perches						}
50420a920b5bSAndy Whitcroft					}
504322f2a2efSAndy Whitcroft				}
50444a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
50453705ce5bSJoe Perches
50463705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
50473705ce5bSJoe Perches
50483705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
50490a920b5bSAndy Whitcroft			}
50503705ce5bSJoe Perches
50513705ce5bSJoe Perches			if (($#elements % 2) == 0) {
50523705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
50533705ce5bSJoe Perches			}
50543705ce5bSJoe Perches
5055194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5056194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
50573705ce5bSJoe Perches			}
50583705ce5bSJoe Perches
50593705ce5bSJoe Perches
50600a920b5bSAndy Whitcroft		}
50610a920b5bSAndy Whitcroft
5062786b6326SJoe Perches# check for whitespace before a non-naked semicolon
5063d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
5064786b6326SJoe Perches			if (WARN("SPACING",
5065786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
5066786b6326SJoe Perches			    $fix) {
5067194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
5068786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
5069786b6326SJoe Perches			}
5070786b6326SJoe Perches		}
5071786b6326SJoe Perches
5072f0a594c1SAndy Whitcroft# check for multiple assignments
5073f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5074000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
5075000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
5076f0a594c1SAndy Whitcroft		}
5077f0a594c1SAndy Whitcroft
507822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
507922f2a2efSAndy Whitcroft## # continuation.
508022f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
508122f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
508222f2a2efSAndy Whitcroft##
508322f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
5084e73d2715SDwaipayan Ray## 			# falsely report the parameters of functions.
508522f2a2efSAndy Whitcroft## 			my $ln = $line;
508622f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
508722f2a2efSAndy Whitcroft## 			}
508822f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
5089000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
5090000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
509122f2a2efSAndy Whitcroft## 			}
509222f2a2efSAndy Whitcroft## 		}
5093f0a594c1SAndy Whitcroft
50940a920b5bSAndy Whitcroft#need space before brace following if, while, etc
50956b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
50966ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
50973705ce5bSJoe Perches			if (ERROR("SPACING",
50983705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
50993705ce5bSJoe Perches			    $fix) {
51006ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
51013705ce5bSJoe Perches			}
5102de7d4f0eSAndy Whitcroft		}
5103de7d4f0eSAndy Whitcroft
5104c4a62ef9SJoe Perches## # check for blank lines before declarations
5105c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5106c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
5107c4a62ef9SJoe Perches##			WARN("SPACING",
5108c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
5109c4a62ef9SJoe Perches##		}
5110c4a62ef9SJoe Perches##
5111c4a62ef9SJoe Perches
5112de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
5113de7d4f0eSAndy Whitcroft# on the line
511494fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5115d5e616fcSJoe Perches			if (ERROR("SPACING",
5116d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
5117d5e616fcSJoe Perches			    $fix) {
5118194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5119d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
5120d5e616fcSJoe Perches			}
51210a920b5bSAndy Whitcroft		}
51220a920b5bSAndy Whitcroft
512322f2a2efSAndy Whitcroft# check spacing on square brackets
512422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
51253705ce5bSJoe Perches			if (ERROR("SPACING",
51263705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
51273705ce5bSJoe Perches			    $fix) {
5128194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51293705ce5bSJoe Perches				    s/\[\s+/\[/;
51303705ce5bSJoe Perches			}
513122f2a2efSAndy Whitcroft		}
513222f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
51333705ce5bSJoe Perches			if (ERROR("SPACING",
51343705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
51353705ce5bSJoe Perches			    $fix) {
5136194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51373705ce5bSJoe Perches				    s/\s+\]/\]/;
51383705ce5bSJoe Perches			}
513922f2a2efSAndy Whitcroft		}
514022f2a2efSAndy Whitcroft
5141c45dcabdSAndy Whitcroft# check spacing on parentheses
51429c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
51439c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
51443705ce5bSJoe Perches			if (ERROR("SPACING",
51453705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
51463705ce5bSJoe Perches			    $fix) {
5147194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51483705ce5bSJoe Perches				    s/\(\s+/\(/;
51493705ce5bSJoe Perches			}
515022f2a2efSAndy Whitcroft		}
515113214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5152c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
5153c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
51543705ce5bSJoe Perches			if (ERROR("SPACING",
51553705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
51563705ce5bSJoe Perches			    $fix) {
5157194f66fcSJoe Perches				$fixed[$fixlinenr] =~
51583705ce5bSJoe Perches				    s/\s+\)/\)/;
51593705ce5bSJoe Perches			}
516022f2a2efSAndy Whitcroft		}
516122f2a2efSAndy Whitcroft
5162e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
5163e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5164e2826fd0SJoe Perches
5165e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5166ea4acbb1SJoe Perches			my $var = $1;
5167ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5168ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
5169ea4acbb1SJoe Perches			    $fix) {
5170ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5171ea4acbb1SJoe Perches			}
5172ea4acbb1SJoe Perches		}
5173ea4acbb1SJoe Perches
5174ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
5175ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
5176ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
5177ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5178ea4acbb1SJoe Perches			my $var = $2;
5179ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5180ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5181ea4acbb1SJoe Perches			    $fix) {
5182ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
5183ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
5184ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5185ea4acbb1SJoe Perches			}
5186e2826fd0SJoe Perches		}
5187e2826fd0SJoe Perches
518863b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
5189a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
5190a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
51915b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
519263b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
519363b7c73eSJoe Perches			my $if_stat = $1;
519463b7c73eSJoe Perches			my $test = substr($2, 1, -1);
519563b7c73eSJoe Perches			my $herectx;
519663b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
519763b7c73eSJoe Perches				my $match = $1;
519863b7c73eSJoe Perches				# avoid parentheses around potential macro args
519963b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
520063b7c73eSJoe Perches				if (!defined($herectx)) {
520163b7c73eSJoe Perches					$herectx = $here . "\n";
520263b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
520363b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
520463b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
520563b7c73eSJoe Perches						$herectx .=  $rl . "\n";
520663b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
520763b7c73eSJoe Perches					}
520863b7c73eSJoe Perches				}
520963b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
521063b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
521163b7c73eSJoe Perches			}
521263b7c73eSJoe Perches		}
521363b7c73eSJoe Perches
52140a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
52154a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
52160a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
52173705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
52183705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
52193705ce5bSJoe Perches			    $fix) {
5220194f66fcSJoe Perches				$fixed[$fixlinenr] =~
52213705ce5bSJoe Perches				    s/^(.)\s+/$1/;
52223705ce5bSJoe Perches			}
52230a920b5bSAndy Whitcroft		}
52240a920b5bSAndy Whitcroft
522540873abaSJoe Perches# check if a statement with a comma should be two statements like:
522640873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
522740873abaSJoe Perches#	bar = baz();
522840873abaSJoe Perches		if (defined($stat) &&
522940873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
523040873abaSJoe Perches			my $cnt = statement_rawlines($stat);
523140873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
523240873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
523340873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
523440873abaSJoe Perches		}
523540873abaSJoe Perches
52365b9553abSJoe Perches# return is not a function
5237507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5238c45dcabdSAndy Whitcroft			my $spacing = $1;
52395b57980dSJoe Perches			if ($perl_version_ok &&
52405b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
52415b9553abSJoe Perches				my $value = $1;
52425b9553abSJoe Perches				$value = deparenthesize($value);
52435b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5244000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5245000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
52465b9553abSJoe Perches				}
5247c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5248000d1cc1SJoe Perches				ERROR("SPACING",
5249000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5250c45dcabdSAndy Whitcroft			}
5251c45dcabdSAndy Whitcroft		}
5252507e5141SJoe Perches
5253b43ae21bSJoe Perches# unnecessary return in a void function
5254b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5255b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5256b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5257b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5258b43ae21bSJoe Perches		    $linenr >= 3 &&
5259b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5260b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
52619819cf25SJoe Perches			WARN("RETURN_VOID",
5262b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
52639819cf25SJoe Perches               }
52649819cf25SJoe Perches
5265189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
52665b57980dSJoe Perches		if ($perl_version_ok &&
5267189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5268189248d8SJoe Perches			my $openparens = $1;
5269189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5270189248d8SJoe Perches			my $msg = "";
5271189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5272189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5273189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5274189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5275189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5276189248d8SJoe Perches			}
5277189248d8SJoe Perches		}
5278189248d8SJoe Perches
5279c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5280c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5281c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5282c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
52835b57980dSJoe Perches		if ($perl_version_ok &&
5284c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5285c5595fa2SJoe Perches			my $lead = $1;
5286c5595fa2SJoe Perches			my $const = $2;
5287c5595fa2SJoe Perches			my $comp = $3;
5288c5595fa2SJoe Perches			my $to = $4;
5289c5595fa2SJoe Perches			my $newcomp = $comp;
5290f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5291c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5292c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5293c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5294c5595fa2SJoe Perches			    $fix) {
5295c5595fa2SJoe Perches				if ($comp eq "<") {
5296c5595fa2SJoe Perches					$newcomp = ">";
5297c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5298c5595fa2SJoe Perches					$newcomp = ">=";
5299c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5300c5595fa2SJoe Perches					$newcomp = "<";
5301c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5302c5595fa2SJoe Perches					$newcomp = "<=";
5303c5595fa2SJoe Perches				}
5304c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5305c5595fa2SJoe Perches			}
5306c5595fa2SJoe Perches		}
5307c5595fa2SJoe Perches
5308f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5309f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
531053a3c448SAndy Whitcroft			my $name = $1;
531153a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
5312000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5313f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
531453a3c448SAndy Whitcroft			}
531553a3c448SAndy Whitcroft		}
5316c45dcabdSAndy Whitcroft
53170a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
53184a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
53193705ce5bSJoe Perches			if (ERROR("SPACING",
53203705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
53213705ce5bSJoe Perches			    $fix) {
5322194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53233705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
53243705ce5bSJoe Perches			}
53250a920b5bSAndy Whitcroft		}
53260a920b5bSAndy Whitcroft
5327f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5328f5fe35ddSAndy Whitcroft# statements after the conditional.
5329170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
53303e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
53313e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
53323e469cdcSAndy Whitcroft					if (!defined $stat);
5333170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5334170d3a22SAndy Whitcroft						$remain_next, $off_next);
5335170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5336170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5337170d3a22SAndy Whitcroft
5338170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5339170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5340170d3a22SAndy Whitcroft				# then count those as offsets.
5341170d3a22SAndy Whitcroft				my ($whitespace) =
5342170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5343170d3a22SAndy Whitcroft				my $offset =
5344170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5345170d3a22SAndy Whitcroft
5346170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5347170d3a22SAndy Whitcroft								$offset} = 1;
5348170d3a22SAndy Whitcroft			}
5349170d3a22SAndy Whitcroft		}
5350170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5351c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5352170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5353171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
53548905a67cSAndy Whitcroft
5355b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
535665b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
535765b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
535865b64b3bSJoe Perches				    $fix && $perl_version_ok) {
535965b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
536065b64b3bSJoe Perches						my $space = $1;
536165b64b3bSJoe Perches						my $not = $2;
536265b64b3bSJoe Perches						my $statement = $3;
536365b64b3bSJoe Perches						my $assigned = $4;
536465b64b3bSJoe Perches						my $test = $8;
536565b64b3bSJoe Perches						my $against = $9;
536665b64b3bSJoe Perches						my $brace = $15;
536765b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
536865b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
536965b64b3bSJoe Perches						my $newline = "${space}if (";
537065b64b3bSJoe Perches						$newline .= '!' if defined($not);
537165b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
537265b64b3bSJoe Perches						$newline .= "$assigned";
537365b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
537465b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
537565b64b3bSJoe Perches						$newline .= ')';
537665b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
537765b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
537865b64b3bSJoe Perches					}
537965b64b3bSJoe Perches				}
53808905a67cSAndy Whitcroft			}
53818905a67cSAndy Whitcroft
53828905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
53838905a67cSAndy Whitcroft			# conditional.
5384773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
53858905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
538613214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
538753210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
538853210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5389773647a0SAndy Whitcroft			{
5390bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5391bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5392bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
539342bdf74cSHidetoshi Seto				my $stat_real = '';
5394bb44ad39SAndy Whitcroft
539542bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
539642bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5397bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5398bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5399bb44ad39SAndy Whitcroft				}
5400bb44ad39SAndy Whitcroft
5401000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5402000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
54038905a67cSAndy Whitcroft			}
54048905a67cSAndy Whitcroft		}
54058905a67cSAndy Whitcroft
540613214adfSAndy Whitcroft# Check for bitwise tests written as boolean
540713214adfSAndy Whitcroft		if ($line =~ /
540813214adfSAndy Whitcroft			(?:
540913214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
541013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
541113214adfSAndy Whitcroft				(?:\&\&|\|\|)
541213214adfSAndy Whitcroft			|
541313214adfSAndy Whitcroft				(?:\&\&|\|\|)
541413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
541513214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
541613214adfSAndy Whitcroft			)/x)
541713214adfSAndy Whitcroft		{
5418000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5419000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
542013214adfSAndy Whitcroft		}
542113214adfSAndy Whitcroft
54228905a67cSAndy Whitcroft# if and else should not have general statements after it
542313214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
542413214adfSAndy Whitcroft			my $s = $1;
542513214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
542613214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5427000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5428000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
54290a920b5bSAndy Whitcroft			}
543013214adfSAndy Whitcroft		}
543139667782SAndy Whitcroft# if should not continue a brace
543239667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5433000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5434048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
543539667782SAndy Whitcroft				$herecurr);
543639667782SAndy Whitcroft		}
5437a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5438a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5439a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
54403fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5441a1080bf8SAndy Whitcroft			\s*return\s+
5442a1080bf8SAndy Whitcroft		    )/xg)
5443a1080bf8SAndy Whitcroft		{
5444000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5445000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5446a1080bf8SAndy Whitcroft		}
54470a920b5bSAndy Whitcroft
54480a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
54490a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
54508b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
54510a920b5bSAndy Whitcroft		    $previndent == $indent) {
54528b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
54538b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
54548b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
54558b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
54568b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
54578b8856f4SJoe Perches				my $fixedline = $prevrawline;
54588b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
54598b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
54608b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54618b8856f4SJoe Perches				}
54628b8856f4SJoe Perches				$fixedline = $rawline;
54638b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
54648b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
54658b8856f4SJoe Perches			}
54660a920b5bSAndy Whitcroft		}
54670a920b5bSAndy Whitcroft
54688b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5469c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5470c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5471c2fdda0dSAndy Whitcroft
5472c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5473c2fdda0dSAndy Whitcroft			# conditional.
5474773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5475c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5476c2fdda0dSAndy Whitcroft
5477c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
54788b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
54798b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
54808b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
54818b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
54828b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
54838b8856f4SJoe Perches					my $fixedline = $prevrawline;
54848b8856f4SJoe Perches					my $trailing = $rawline;
54858b8856f4SJoe Perches					$trailing =~ s/^\+//;
54868b8856f4SJoe Perches					$trailing = trim($trailing);
54878b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
54888b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54898b8856f4SJoe Perches				}
5490c2fdda0dSAndy Whitcroft			}
5491c2fdda0dSAndy Whitcroft		}
5492c2fdda0dSAndy Whitcroft
549395e2c602SJoe Perches#Specific variable tests
5494323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5495323c1260SJoe Perches			my $var = $1;
549695e2c602SJoe Perches
549795e2c602SJoe Perches#CamelCase
5498807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5499be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
55004104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values
55014104a206SŁukasz Stelmach			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
550222735ce8SJoe Perches#Ignore Page<foo> variants
5503807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5504d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5505d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5506d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5507f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5508f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
55097e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
55107e781f67SJoe Perches					my $word = $1;
55117e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5512d8b07710SJoe Perches					if ($check) {
5513d8b07710SJoe Perches						seed_camelcase_includes();
5514d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5515d8b07710SJoe Perches							seed_camelcase_file($realfile);
5516d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5517d8b07710SJoe Perches						}
5518d8b07710SJoe Perches					}
55197e781f67SJoe Perches					if (!defined $camelcase{$word}) {
55207e781f67SJoe Perches						$camelcase{$word} = 1;
5521be79794bSJoe Perches						CHK("CAMELCASE",
55227e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
55237e781f67SJoe Perches					}
5524323c1260SJoe Perches				}
5525323c1260SJoe Perches			}
55263445686aSJoe Perches		}
55270a920b5bSAndy Whitcroft
55280a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5529d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5530d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5531d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5532d5e616fcSJoe Perches			    $fix) {
5533194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5534d5e616fcSJoe Perches			}
55350a920b5bSAndy Whitcroft		}
55360a920b5bSAndy Whitcroft
55370e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
55380e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5539c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5540e09dec48SAndy Whitcroft			my $file = "$1.h";
5541e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5542e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5543e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
55447840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5545c45dcabdSAndy Whitcroft			{
55460e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
55470e212e0aSFabian Frederick				if ($asminclude > 0) {
5548e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5549000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5550000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5551e09dec48SAndy Whitcroft					} else {
5552000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5553000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5554e09dec48SAndy Whitcroft					}
55550a920b5bSAndy Whitcroft				}
55560a920b5bSAndy Whitcroft			}
55570e212e0aSFabian Frederick		}
55580a920b5bSAndy Whitcroft
5559653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5560653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5561cf655043SAndy Whitcroft# in a known good container
5562b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5563b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5564d8aaf121SAndy Whitcroft			my $ln = $linenr;
5565d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5566c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5567c45dcabdSAndy Whitcroft			my $ctx = '';
556808a2843eSJoe Perches			my $has_flow_statement = 0;
556908a2843eSJoe Perches			my $has_arg_concat = 0;
5570c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5571f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5572f74bd194SAndy Whitcroft			$ctx = $dstat;
5573c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5574a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5575c45dcabdSAndy Whitcroft
557608a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
557762e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
557808a2843eSJoe Perches
5579f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5580f59b64bfSJoe Perches			my $define_args = $1;
5581f59b64bfSJoe Perches			my $define_stmt = $dstat;
5582f59b64bfSJoe Perches			my @def_args = ();
5583f59b64bfSJoe Perches
5584f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5585f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5586f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
55878c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5588f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5589f59b64bfSJoe Perches			}
5590f59b64bfSJoe Perches
5591292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5592c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5593c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5594c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5595c45dcabdSAndy Whitcroft
5596c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
55972e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
55982e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
55992e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5600bf30d6edSAndy Whitcroft			{
5601c45dcabdSAndy Whitcroft			}
5602c45dcabdSAndy Whitcroft
5603342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
560433acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
560533acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5606e45bab8eSAndy Whitcroft			{
5607e45bab8eSAndy Whitcroft			}
5608e45bab8eSAndy Whitcroft
560942e15293SJoe Perches			# Make asm volatile uses seem like a generic function
561042e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
561142e15293SJoe Perches
5612c45dcabdSAndy Whitcroft			my $exceptions = qr{
5613c45dcabdSAndy Whitcroft				$Declare|
5614c45dcabdSAndy Whitcroft				module_param_named|
5615a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5616c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5617c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5618383099fdSAndy Whitcroft				__typeof__\(|
561922fd2d3eSStefani Seibold				union|
562022fd2d3eSStefani Seibold				struct|
5621ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
56226b10df42SVladimir Zapolskiy				^\"|\"$|
56236b10df42SVladimir Zapolskiy				^\[
5624c45dcabdSAndy Whitcroft			}x;
56255eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5626f59b64bfSJoe Perches
5627f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5628f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5629e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5630f59b64bfSJoe Perches
5631f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5632f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5633f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
56343cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5635356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5636f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5637f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5638e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
563972f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
56402e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5641f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5642f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5643f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
56444e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5645f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5646c45dcabdSAndy Whitcroft			{
5647e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5648e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5649e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5650e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5651f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5652f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5653f74bd194SAndy Whitcroft				} else {
5654000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5655388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5656d8aaf121SAndy Whitcroft				}
5657f59b64bfSJoe Perches
5658f59b64bfSJoe Perches			}
56595207649bSJoe Perches
56605207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
56615207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
56625207649bSJoe Perches			my $first = 1;
56635207649bSJoe Perches			$define_stmt = "";
56645207649bSJoe Perches			foreach my $l (@stmt_array) {
56655207649bSJoe Perches				$l =~ s/\\$//;
56665207649bSJoe Perches				if ($first) {
56675207649bSJoe Perches					$define_stmt = $l;
56685207649bSJoe Perches					$first = 0;
56695207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
56705207649bSJoe Perches					$define_stmt .= substr($l, 1);
56715207649bSJoe Perches				}
56725207649bSJoe Perches			}
56735207649bSJoe Perches			$define_stmt =~ s/$;//g;
56745207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
56755207649bSJoe Perches			$define_stmt = trim($define_stmt);
56765207649bSJoe Perches
5677f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5678f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5679f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
56809192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
56817fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
56826dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
56837fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
56847fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5685d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5686f59b64bfSJoe Perches				if ($use_cnt > 1) {
5687f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5688f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5689f59b64bfSJoe Perches				    }
56909192d41aSJoe Perches# check if any macro arguments may have other precedence issues
56917fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
56929192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
56939192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
56949192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
56959192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
56969192d41aSJoe Perches				}
56970a920b5bSAndy Whitcroft			}
56985023d347SJoe Perches
569908a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
570008a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
570108a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
570208a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5703e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
570408a2843eSJoe Perches
570508a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
570608a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
570708a2843eSJoe Perches			}
570808a2843eSJoe Perches
5709481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
57105023d347SJoe Perches
57115023d347SJoe Perches		} else {
57125023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5713481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5714481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
57155023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
57165023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
57175023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
57185023d347SJoe Perches			}
5719653d4876SAndy Whitcroft		}
57200a920b5bSAndy Whitcroft
5721b13edf7fSJoe Perches# do {} while (0) macro tests:
5722b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5723b13edf7fSJoe Perches# macro should not end with a semicolon
57245b57980dSJoe Perches		if ($perl_version_ok &&
5725b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5726b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5727b13edf7fSJoe Perches			my $ln = $linenr;
5728b13edf7fSJoe Perches			my $cnt = $realcnt;
5729b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5730b13edf7fSJoe Perches			my $ctx = '';
5731b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5732b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5733b13edf7fSJoe Perches			$ctx = $dstat;
5734b13edf7fSJoe Perches
5735b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
57361b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5737b13edf7fSJoe Perches
5738b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5739b13edf7fSJoe Perches				my $stmts = $2;
5740b13edf7fSJoe Perches				my $semis = $3;
5741b13edf7fSJoe Perches
5742b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5743b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5744e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5745b13edf7fSJoe Perches
5746ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5747ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5748b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5749b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5750b13edf7fSJoe Perches				}
5751b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5752b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5753b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5754b13edf7fSJoe Perches				}
5755f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5756f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5757f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5758e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5759f5ef95b1SJoe Perches
5760f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5761f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5762b13edf7fSJoe Perches			}
5763b13edf7fSJoe Perches		}
5764b13edf7fSJoe Perches
5765f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
576613214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
576713214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5768cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
576913214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5770cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5771cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5772aad4f614SJoe Perches				my @allowed = ();
5773aad4f614SJoe Perches				my $allow = 0;
577413214adfSAndy Whitcroft				my $seen = 0;
5775773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5776cf655043SAndy Whitcroft				my $ln = $linenr - 1;
577713214adfSAndy Whitcroft				for my $chunk (@chunks) {
577813214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
577913214adfSAndy Whitcroft
5780773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5781773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5782773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5783773647a0SAndy Whitcroft
5784aad4f614SJoe Perches					$allowed[$allow] = 0;
5785773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5786773647a0SAndy Whitcroft
5787773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5788773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5789773647a0SAndy Whitcroft
5790773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5791cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5792cf655043SAndy Whitcroft
5793773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
579413214adfSAndy Whitcroft
579513214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
579613214adfSAndy Whitcroft
5797aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5798cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5799cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5800aad4f614SJoe Perches						$allowed[$allow] = 1;
580113214adfSAndy Whitcroft					}
580213214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5803cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5804aad4f614SJoe Perches						$allowed[$allow] = 1;
580513214adfSAndy Whitcroft					}
5806cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5807cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5808aad4f614SJoe Perches						$allowed[$allow] = 1;
580913214adfSAndy Whitcroft					}
5810aad4f614SJoe Perches					$allow++;
581113214adfSAndy Whitcroft				}
5812aad4f614SJoe Perches				if ($seen) {
5813aad4f614SJoe Perches					my $sum_allowed = 0;
5814aad4f614SJoe Perches					foreach (@allowed) {
5815aad4f614SJoe Perches						$sum_allowed += $_;
5816aad4f614SJoe Perches					}
5817aad4f614SJoe Perches					if ($sum_allowed == 0) {
5818000d1cc1SJoe Perches						WARN("BRACES",
5819000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5820aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5821aad4f614SJoe Perches						 $seen != $allow) {
5822aad4f614SJoe Perches						CHK("BRACES",
5823aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5824aad4f614SJoe Perches					}
582513214adfSAndy Whitcroft				}
582613214adfSAndy Whitcroft			}
582713214adfSAndy Whitcroft		}
5828773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
582913214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5830cf655043SAndy Whitcroft			my $allowed = 0;
5831f0a594c1SAndy Whitcroft
5832cf655043SAndy Whitcroft			# Check the pre-context.
5833cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5834cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5835cf655043SAndy Whitcroft				$allowed = 1;
5836f0a594c1SAndy Whitcroft			}
5837773647a0SAndy Whitcroft
5838773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5839773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5840773647a0SAndy Whitcroft
5841cf655043SAndy Whitcroft			# Check the condition.
5842cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5843773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5844cf655043SAndy Whitcroft			if (defined $cond) {
5845773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5846cf655043SAndy Whitcroft			}
5847cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5848cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5849cf655043SAndy Whitcroft				$allowed = 1;
5850cf655043SAndy Whitcroft			}
5851cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5852cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5853cf655043SAndy Whitcroft				$allowed = 1;
5854cf655043SAndy Whitcroft			}
5855cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5856cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5857cf655043SAndy Whitcroft				$allowed = 1;
5858cf655043SAndy Whitcroft			}
5859cf655043SAndy Whitcroft			# Check the post-context.
5860cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5861cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5862cf655043SAndy Whitcroft				if (defined $cond) {
5863773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5864cf655043SAndy Whitcroft				}
5865cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5866cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5867cf655043SAndy Whitcroft					$allowed = 1;
5868cf655043SAndy Whitcroft				}
5869cf655043SAndy Whitcroft			}
5870cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5871f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5872e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5873cf655043SAndy Whitcroft
5874000d1cc1SJoe Perches				WARN("BRACES",
5875000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5876f0a594c1SAndy Whitcroft			}
5877f0a594c1SAndy Whitcroft		}
5878f0a594c1SAndy Whitcroft
5879e4c5babdSJoe Perches# check for single line unbalanced braces
588095330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
588195330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5882e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5883e4c5babdSJoe Perches		}
5884e4c5babdSJoe Perches
58850979ae66SJoe Perches# check for unnecessary blank lines around braces
588677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5887f8e58219SJoe Perches			if (CHK("BRACES",
5888f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5889f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5890f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5891f8e58219SJoe Perches			}
58920979ae66SJoe Perches		}
589377b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5894f8e58219SJoe Perches			if (CHK("BRACES",
5895f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5896f8e58219SJoe Perches			    $fix) {
5897f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5898f8e58219SJoe Perches			}
58990979ae66SJoe Perches		}
59000979ae66SJoe Perches
59014a0df2efSAndy Whitcroft# no volatiles please
59026c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
59036c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5904000d1cc1SJoe Perches			WARN("VOLATILE",
59058c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
59064a0df2efSAndy Whitcroft		}
59074a0df2efSAndy Whitcroft
59085e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
59095e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
59105e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
59115e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
591233acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
59135e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
59145e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
59155e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
59165e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
59175e4f6ba5SJoe Perches				     $fix &&
59185e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
59195e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
59205e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
59215e4f6ba5SJoe Perches				my $comma_close = "";
59225e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
59235e4f6ba5SJoe Perches					$comma_close = $1;
59245e4f6ba5SJoe Perches				}
59255e4f6ba5SJoe Perches
59265e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
59275e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
59285e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
59295e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
59305e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
59315e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
59325e4f6ba5SJoe Perches				$fixedline = $rawline;
59335e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
59345e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
59355e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
59365e4f6ba5SJoe Perches				}
59375e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
59385e4f6ba5SJoe Perches			}
59395e4f6ba5SJoe Perches		}
59405e4f6ba5SJoe Perches
59415e4f6ba5SJoe Perches# check for missing a space in a string concatenation
59425e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
59435e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
59445e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
59455e4f6ba5SJoe Perches		}
59465e4f6ba5SJoe Perches
594777cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5948e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5949e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5950e4b7d309SJoe Perches# function declarations
595177cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
595277cb8546SJoe Perches		    defined($context_function) &&
5953e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5954e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
595577cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5956e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
595777cb8546SJoe Perches		}
595877cb8546SJoe Perches
59595e4f6ba5SJoe Perches# check for spaces before a quoted newline
59605e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
59615e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
59625e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
59635e4f6ba5SJoe Perches			    $fix) {
59645e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
59655e4f6ba5SJoe Perches			}
59665e4f6ba5SJoe Perches
59675e4f6ba5SJoe Perches		}
59685e4f6ba5SJoe Perches
5969f17dba4fSJoe Perches# concatenated string without spaces between elements
597079682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
597179682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
597279682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
597379682c0cSJoe Perches			    $fix) {
597479682c0cSJoe Perches				while ($line =~ /($String)/g) {
597579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
597679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
597779682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
597879682c0cSJoe Perches				}
597979682c0cSJoe Perches			}
5980f17dba4fSJoe Perches		}
5981f17dba4fSJoe Perches
598290ad30e5SJoe Perches# uncoalesced string fragments
598333acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
598479682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
598579682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
598679682c0cSJoe Perches			    $fix) {
598779682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
598879682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
598979682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
599079682c0cSJoe Perches				}
599179682c0cSJoe Perches			}
599290ad30e5SJoe Perches		}
599390ad30e5SJoe Perches
5994522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5995522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5996522b837cSAlexey Dobriyan		my $show_Z = 1;
59975e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5998522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
59995e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
6000522b837cSAlexey Dobriyan			# check for %L
6001522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
60025e4f6ba5SJoe Perches				WARN("PRINTF_L",
6003522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6004522b837cSAlexey Dobriyan				$show_L = 0;
60055e4f6ba5SJoe Perches			}
6006522b837cSAlexey Dobriyan			# check for %Z
6007522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6008522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
6009522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6010522b837cSAlexey Dobriyan				$show_Z = 0;
6011522b837cSAlexey Dobriyan			}
6012522b837cSAlexey Dobriyan			# check for 0x<decimal>
6013522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6014522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
60156e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
60166e300757SJoe Perches			}
60175e4f6ba5SJoe Perches		}
60185e4f6ba5SJoe Perches
60195e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
60203f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
60215e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
60225e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
60235e4f6ba5SJoe Perches		}
60245e4f6ba5SJoe Perches
602500df344fSAndy Whitcroft# warn about #if 0
6026c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
602760f89010SPrakruthi Deepak Heragu			WARN("IF_0",
602860f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
602960f89010SPrakruthi Deepak Heragu		}
603060f89010SPrakruthi Deepak Heragu
603160f89010SPrakruthi Deepak Heragu# warn about #if 1
603260f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
603360f89010SPrakruthi Deepak Heragu			WARN("IF_1",
603460f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
60354a0df2efSAndy Whitcroft		}
60364a0df2efSAndy Whitcroft
603703df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
603803df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6039100425deSJoe Perches			my $tested = quotemeta($1);
6040100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6041100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6042100425deSJoe Perches				my $func = $1;
6043100425deSJoe Perches				if (WARN('NEEDLESS_IF',
6044100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6045100425deSJoe Perches				    $fix) {
6046100425deSJoe Perches					my $do_fix = 1;
6047100425deSJoe Perches					my $leading_tabs = "";
6048100425deSJoe Perches					my $new_leading_tabs = "";
6049100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6050100425deSJoe Perches						$leading_tabs = $1;
6051100425deSJoe Perches					} else {
6052100425deSJoe Perches						$do_fix = 0;
6053100425deSJoe Perches					}
6054100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6055100425deSJoe Perches						$new_leading_tabs = $1;
6056100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6057100425deSJoe Perches							$do_fix = 0;
6058100425deSJoe Perches						}
6059100425deSJoe Perches					} else {
6060100425deSJoe Perches						$do_fix = 0;
6061100425deSJoe Perches					}
6062100425deSJoe Perches					if ($do_fix) {
6063100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
6064100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6065100425deSJoe Perches					}
6066100425deSJoe Perches				}
60674c432a8fSGreg Kroah-Hartman			}
60684c432a8fSGreg Kroah-Hartman		}
6069f0a594c1SAndy Whitcroft
6070ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
6071ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6072ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6073ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
6074ebfdc409SJoe Perches		    $linenr > 3) {
6075ebfdc409SJoe Perches			my $testval = $2;
6076ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
6077ebfdc409SJoe Perches
6078ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6079ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6080ebfdc409SJoe Perches
6081e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6082e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
6083ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
6084ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6085ebfdc409SJoe Perches			}
6086ebfdc409SJoe Perches		}
6087ebfdc409SJoe Perches
6088f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
6089dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6090f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6091f78d98f6SJoe Perches			my $level = $1;
6092f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
6093f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
6094f78d98f6SJoe Perches			    $fix) {
6095f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6096f78d98f6SJoe Perches			}
6097f78d98f6SJoe Perches		}
6098f78d98f6SJoe Perches
609945c55e92SJoe Perches# check for logging continuations
610045c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
610145c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
610245c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
610345c55e92SJoe Perches		}
610445c55e92SJoe Perches
6105*70eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6106*70eb2275SDwaipayan Ray		if (defined $stat &&
6107*70eb2275SDwaipayan Ray		    $line =~ /\b$logFunctions\s*\(/ &&
6108*70eb2275SDwaipayan Ray		    index($stat, '"') >= 0) {
6109*70eb2275SDwaipayan Ray			my $lc = $stat =~ tr@\n@@;
6110*70eb2275SDwaipayan Ray			$lc = $lc + $linenr;
6111*70eb2275SDwaipayan Ray			my $stat_real = get_stat_real($linenr, $lc);
6112*70eb2275SDwaipayan Ray			pos($stat_real) = index($stat_real, '"');
6113*70eb2275SDwaipayan Ray			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6114*70eb2275SDwaipayan Ray				my $pspec = $1;
6115*70eb2275SDwaipayan Ray				my $h = $2;
6116*70eb2275SDwaipayan Ray				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6117*70eb2275SDwaipayan Ray				if (WARN("UNNECESSARY_MODIFIER",
6118*70eb2275SDwaipayan Ray					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6119*70eb2275SDwaipayan Ray				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6120*70eb2275SDwaipayan Ray					my $nspec = $pspec;
6121*70eb2275SDwaipayan Ray					$nspec =~ s/h//g;
6122*70eb2275SDwaipayan Ray					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6123*70eb2275SDwaipayan Ray				}
6124*70eb2275SDwaipayan Ray			}
6125*70eb2275SDwaipayan Ray		}
6126*70eb2275SDwaipayan Ray
6127abb08a53SJoe Perches# check for mask then right shift without a parentheses
61285b57980dSJoe Perches		if ($perl_version_ok &&
6129abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6130abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6131abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
6132abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6133abb08a53SJoe Perches		}
6134abb08a53SJoe Perches
6135b75ac618SJoe Perches# check for pointer comparisons to NULL
61365b57980dSJoe Perches		if ($perl_version_ok) {
6137b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6138b75ac618SJoe Perches				my $val = $1;
6139b75ac618SJoe Perches				my $equal = "!";
6140b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
6141b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
6142b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6143b75ac618SJoe Perches					    $fix) {
6144b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6145b75ac618SJoe Perches				}
6146b75ac618SJoe Perches			}
6147b75ac618SJoe Perches		}
6148b75ac618SJoe Perches
61498716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
61508716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
61518716de38SJoe Perches			my $attr = $1;
61528716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
61538716de38SJoe Perches				my $ptr = $1;
61548716de38SJoe Perches				my $var = $2;
61558716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
61568716de38SJoe Perches				      ERROR("MISPLACED_INIT",
61578716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
61588716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
61598716de38SJoe Perches				      WARN("MISPLACED_INIT",
61608716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
61618716de38SJoe Perches				    $fix) {
6162194f66fcSJoe 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;
61638716de38SJoe Perches				}
61648716de38SJoe Perches			}
61658716de38SJoe Perches		}
61668716de38SJoe Perches
6167e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
6168e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6169e970b884SJoe Perches			my $attr = $1;
6170e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
6171e970b884SJoe Perches			my $attr_prefix = $1;
6172e970b884SJoe Perches			my $attr_type = $2;
6173e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6174e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6175e970b884SJoe Perches			    $fix) {
6176194f66fcSJoe Perches				$fixed[$fixlinenr] =~
6177e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
6178e970b884SJoe Perches			}
6179e970b884SJoe Perches		}
6180e970b884SJoe Perches
6181e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
6182e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6183e970b884SJoe Perches			my $attr = $1;
6184e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6185e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6186e970b884SJoe Perches			    $fix) {
6187194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
6188e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
6189e970b884SJoe Perches				$lead = rtrim($1);
6190e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
6191e970b884SJoe Perches				$lead = "${lead}const ";
6192194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6193e970b884SJoe Perches			}
6194e970b884SJoe Perches		}
6195e970b884SJoe Perches
6196c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
6197c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
6198c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6199c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
6200c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6201c17893c7SJoe Perches			    $fix) {
6202c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6203c17893c7SJoe Perches			}
6204c17893c7SJoe Perches		}
6205c17893c7SJoe Perches
6206fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
6207fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
6208fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6209fbdb8138SJoe Perches			my $constant_func = $1;
6210fbdb8138SJoe Perches			my $func = $constant_func;
6211fbdb8138SJoe Perches			$func =~ s/^__constant_//;
6212fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
6213fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
6214fbdb8138SJoe Perches			    $fix) {
6215194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6216fbdb8138SJoe Perches			}
6217fbdb8138SJoe Perches		}
6218fbdb8138SJoe Perches
62191a15a250SPatrick Pannuto# prefer usleep_range over udelay
622037581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
622143c1d77cSJoe Perches			my $delay = $1;
62221a15a250SPatrick Pannuto			# ignore udelay's < 10, however
622343c1d77cSJoe Perches			if (! ($delay < 10) ) {
6224000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
6225458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
622643c1d77cSJoe Perches			}
622743c1d77cSJoe Perches			if ($delay > 2000) {
622843c1d77cSJoe Perches				WARN("LONG_UDELAY",
622943c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
62301a15a250SPatrick Pannuto			}
62311a15a250SPatrick Pannuto		}
62321a15a250SPatrick Pannuto
623309ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
623409ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
623509ef8725SPatrick Pannuto			if ($1 < 20) {
6236000d1cc1SJoe Perches				WARN("MSLEEP",
6237458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
623809ef8725SPatrick Pannuto			}
623909ef8725SPatrick Pannuto		}
624009ef8725SPatrick Pannuto
624136ec1939SJoe Perches# check for comparisons of jiffies
624236ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
624336ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
624436ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
624536ec1939SJoe Perches		}
624636ec1939SJoe Perches
62479d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
62489d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
62499d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
62509d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
62519d7a34a5SJoe Perches		}
62529d7a34a5SJoe Perches
625300df344fSAndy Whitcroft# warn about #ifdefs in C files
6254c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
625500df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
625600df344fSAndy Whitcroft#			print "$herecurr";
625700df344fSAndy Whitcroft#			$clean = 0;
625800df344fSAndy Whitcroft#		}
625900df344fSAndy Whitcroft
626022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
6261c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
62623705ce5bSJoe Perches			if (ERROR("SPACING",
62633705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
62643705ce5bSJoe Perches			    $fix) {
6265194f66fcSJoe Perches				$fixed[$fixlinenr] =~
62663705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
62673705ce5bSJoe Perches			}
62683705ce5bSJoe Perches
626922f2a2efSAndy Whitcroft		}
627022f2a2efSAndy Whitcroft
62714a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6272171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6273171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
62744a0df2efSAndy Whitcroft			my $which = $1;
62754a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6276000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6277000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
62784a0df2efSAndy Whitcroft			}
62794a0df2efSAndy Whitcroft		}
62804a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6281402c2553SMichael S. Tsirkin
6282402c2553SMichael S. Tsirkin		my $barriers = qr{
6283402c2553SMichael S. Tsirkin			mb|
6284402c2553SMichael S. Tsirkin			rmb|
6285ad83ec6cSWill Deacon			wmb
6286402c2553SMichael S. Tsirkin		}x;
6287402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6288402c2553SMichael S. Tsirkin			mb__before_atomic|
6289402c2553SMichael S. Tsirkin			mb__after_atomic|
6290402c2553SMichael S. Tsirkin			store_release|
6291402c2553SMichael S. Tsirkin			load_acquire|
6292402c2553SMichael S. Tsirkin			store_mb|
6293402c2553SMichael S. Tsirkin			(?:$barriers)
6294402c2553SMichael S. Tsirkin		}x;
6295402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6296402c2553SMichael S. Tsirkin			(?:$barriers)|
629743e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
629843e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6299402c2553SMichael S. Tsirkin		}x;
6300402c2553SMichael S. Tsirkin
6301402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
63024a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6303c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6304000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
63054a0df2efSAndy Whitcroft			}
63064a0df2efSAndy Whitcroft		}
63073ad81779SPaul E. McKenney
6308f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6309f4073b0fSMichael S. Tsirkin
6310f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6311f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6312f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6313f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6314f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6315f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6316f4073b0fSMichael S. Tsirkin		}
6317f4073b0fSMichael S. Tsirkin
6318cb426e99SJoe Perches# check for waitqueue_active without a comment.
6319cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6320cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6321cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6322cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6323cb426e99SJoe Perches			}
6324cb426e99SJoe Perches		}
63253ad81779SPaul E. McKenney
63265099a722SMarco Elver# check for data_race without a comment.
63275099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
63285099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
63295099a722SMarco Elver				WARN("DATA_RACE",
63305099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
63315099a722SMarco Elver			}
63325099a722SMarco Elver		}
63335099a722SMarco Elver
63344a0df2efSAndy Whitcroft# check of hardware specific defines
6335c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6336000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6337000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
63380a920b5bSAndy Whitcroft		}
6339653d4876SAndy Whitcroft
6340596ed45bSJoe Perches# check that the storage class is not after a type
6341596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6342000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6343596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6344596ed45bSJoe Perches		}
6345596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6346596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6347596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6348596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6349596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6350596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6351596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6352d4977c78STobias Klauser		}
6353d4977c78STobias Klauser
6354de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6355de7d4f0eSAndy Whitcroft# storage class and type.
63569c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
63579c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6358000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6359000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6360de7d4f0eSAndy Whitcroft		}
6361de7d4f0eSAndy Whitcroft
63628905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
63632b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
63642b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6365d5e616fcSJoe Perches			if (WARN("INLINE",
6366d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6367d5e616fcSJoe Perches			    $fix) {
6368194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6369d5e616fcSJoe Perches
6370d5e616fcSJoe Perches			}
63718905a67cSAndy Whitcroft		}
63728905a67cSAndy Whitcroft
63737ebe1d17SDwaipayan Ray# Check for compiler attributes
63742b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
63757ebe1d17SDwaipayan Ray		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
63767ebe1d17SDwaipayan Ray			my $attr = $1;
63777ebe1d17SDwaipayan Ray			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
63787ebe1d17SDwaipayan Ray
63797ebe1d17SDwaipayan Ray			my %attr_list = (
63800830aab0SJoe Perches				"alias"				=> "__alias",
63817ebe1d17SDwaipayan Ray				"aligned"			=> "__aligned",
63827ebe1d17SDwaipayan Ray				"always_inline"			=> "__always_inline",
63837ebe1d17SDwaipayan Ray				"assume_aligned"		=> "__assume_aligned",
63847ebe1d17SDwaipayan Ray				"cold"				=> "__cold",
63857ebe1d17SDwaipayan Ray				"const"				=> "__attribute_const__",
63867ebe1d17SDwaipayan Ray				"copy"				=> "__copy",
63877ebe1d17SDwaipayan Ray				"designated_init"		=> "__designated_init",
63887ebe1d17SDwaipayan Ray				"externally_visible"		=> "__visible",
63897ebe1d17SDwaipayan Ray				"format"			=> "printf|scanf",
63907ebe1d17SDwaipayan Ray				"gnu_inline"			=> "__gnu_inline",
63917ebe1d17SDwaipayan Ray				"malloc"			=> "__malloc",
63927ebe1d17SDwaipayan Ray				"mode"				=> "__mode",
63937ebe1d17SDwaipayan Ray				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
63947ebe1d17SDwaipayan Ray				"noclone"			=> "__noclone",
63957ebe1d17SDwaipayan Ray				"noinline"			=> "noinline",
63967ebe1d17SDwaipayan Ray				"nonstring"			=> "__nonstring",
63977ebe1d17SDwaipayan Ray				"noreturn"			=> "__noreturn",
63987ebe1d17SDwaipayan Ray				"packed"			=> "__packed",
63997ebe1d17SDwaipayan Ray				"pure"				=> "__pure",
6400339f29d9SJoe Perches				"section"			=> "__section",
64010830aab0SJoe Perches				"used"				=> "__used",
64020830aab0SJoe Perches				"weak"				=> "__weak"
64037ebe1d17SDwaipayan Ray			);
64047ebe1d17SDwaipayan Ray
64057ebe1d17SDwaipayan Ray			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6406339f29d9SJoe Perches				my $orig_attr = $1;
64077ebe1d17SDwaipayan Ray				my $params = '';
64087ebe1d17SDwaipayan Ray				$params = $2 if defined($2);
6409339f29d9SJoe Perches				my $curr_attr = $orig_attr;
64107ebe1d17SDwaipayan Ray				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
64117ebe1d17SDwaipayan Ray				if (exists($attr_list{$curr_attr})) {
6412339f29d9SJoe Perches					my $new = $attr_list{$curr_attr};
64137ebe1d17SDwaipayan Ray					if ($curr_attr eq "format" && $params) {
64147ebe1d17SDwaipayan Ray						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6415339f29d9SJoe Perches						$new = "__$1\($2";
64167ebe1d17SDwaipayan Ray					} else {
6417339f29d9SJoe Perches						$new = "$new$params";
64187ebe1d17SDwaipayan Ray					}
64197ebe1d17SDwaipayan Ray					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6420339f29d9SJoe Perches						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
64217ebe1d17SDwaipayan Ray					    $fix) {
6422339f29d9SJoe Perches						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6423339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/$remove//;
6424339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6425339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6426339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
64277ebe1d17SDwaipayan Ray					}
642839b7e287SJoe Perches				}
6429462811d9SJoe Perches			}
6430462811d9SJoe Perches
64317ebe1d17SDwaipayan Ray			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
64327ebe1d17SDwaipayan Ray			if ($attr =~ /^_*unused/) {
64337ebe1d17SDwaipayan Ray				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
64347ebe1d17SDwaipayan Ray				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6435d5e616fcSJoe Perches			}
64366061d949SJoe Perches		}
64376061d949SJoe Perches
6438619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
64395b57980dSJoe Perches		if ($perl_version_ok &&
6440619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6441619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6442619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6443619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6444619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6445619a908aSJoe Perches		}
6446619a908aSJoe Perches
6447fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6448e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6449fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6450e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6451e6176fa4SJoe Perches			my $type = $1;
6452e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6453e6176fa4SJoe Perches				$type = $1;
6454e6176fa4SJoe Perches				my $kernel_type = 'u';
6455e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6456e6176fa4SJoe Perches				$type =~ /(\d+)/;
6457e6176fa4SJoe Perches				$kernel_type .= $1;
6458e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6459e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6460e6176fa4SJoe Perches				    $fix) {
6461e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6462e6176fa4SJoe Perches				}
6463e6176fa4SJoe Perches			}
6464e6176fa4SJoe Perches		}
6465e6176fa4SJoe Perches
6466938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6467938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6468938224b5SJoe Perches			my $cast = $1;
6469938224b5SJoe Perches			my $const = $2;
6470938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6471938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6472938224b5SJoe Perches			    $fix) {
6473938224b5SJoe Perches				my $suffix = "";
6474938224b5SJoe Perches				my $newconst = $const;
6475938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6476938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6477938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6478938224b5SJoe Perches					$suffix .= 'LL';
6479938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6480938224b5SJoe Perches					$suffix .= 'L';
6481938224b5SJoe Perches				}
6482938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6483938224b5SJoe Perches			}
6484938224b5SJoe Perches		}
6485938224b5SJoe Perches
64868f53a9b8SJoe Perches# check for sizeof(&)
64878f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6488000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6489000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
64908f53a9b8SJoe Perches		}
64918f53a9b8SJoe Perches
649266c80b60SJoe Perches# check for sizeof without parenthesis
649366c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6494d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6495d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6496d5e616fcSJoe Perches			    $fix) {
6497194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6498d5e616fcSJoe Perches			}
649966c80b60SJoe Perches		}
650066c80b60SJoe Perches
650188982feaSJoe Perches# check for struct spinlock declarations
650288982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
650388982feaSJoe Perches			WARN("USE_SPINLOCK_T",
650488982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
650588982feaSJoe Perches		}
650688982feaSJoe Perches
6507a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
650806668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6509a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6510caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6511caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6512d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6513d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6514d5e616fcSJoe Perches				    $fix) {
6515194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6516d5e616fcSJoe Perches				}
6517a6962d72SJoe Perches			}
6518a6962d72SJoe Perches		}
6519a6962d72SJoe Perches
65200b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
65215b57980dSJoe Perches		if ($perl_version_ok &&
65220b523769SJoe Perches		    defined $stat &&
65230b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
65240b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6525e3c6bc95STobin C. Harding			my $stat_real;
6526e3c6bc95STobin C. Harding
65270b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
65280b523769SJoe Perches			$lc = $lc + $linenr;
65290b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6530ffe07513SJoe Perches				my $specifier;
6531ffe07513SJoe Perches				my $extension;
65323bd32d6aSSakari Ailus				my $qualifier;
6533ffe07513SJoe Perches				my $bad_specifier = "";
65340b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
65350b523769SJoe Perches				$fmt =~ s/%%//g;
6536e3c6bc95STobin C. Harding
65373bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6538e3c6bc95STobin C. Harding					$specifier = $1;
6539e3c6bc95STobin C. Harding					$extension = $2;
65403bd32d6aSSakari Ailus					$qualifier = $3;
6541361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
65423bd32d6aSSakari Ailus					    ($extension eq "f" &&
65433bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6544e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
65450b523769SJoe Perches						last;
65460b523769SJoe Perches					}
6547e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6548e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6549e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
65500b523769SJoe Perches						}
6551e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6552e3c6bc95STobin 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");
6553e3c6bc95STobin C. Harding					}
6554e3c6bc95STobin C. Harding				}
6555e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
65562a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
65571df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
65581df7338aSSergey Senozhatsky					my $use = "";
6559e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
65601df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6561e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
65621df7338aSSergey Senozhatsky					}
65632a9f9d85STobin C. Harding
65640b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6565e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6566e3c6bc95STobin C. Harding				}
65670b523769SJoe Perches			}
65680b523769SJoe Perches		}
65690b523769SJoe Perches
6570554e165cSAndy Whitcroft# Check for misused memsets
65715b57980dSJoe Perches		if ($perl_version_ok &&
6572d1fe9c09SJoe Perches		    defined $stat &&
65739e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6574554e165cSAndy Whitcroft
6575d7c76ba7SJoe Perches			my $ms_addr = $2;
6576d1fe9c09SJoe Perches			my $ms_val = $7;
6577d1fe9c09SJoe Perches			my $ms_size = $12;
6578d7c76ba7SJoe Perches
6579554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6580554e165cSAndy Whitcroft				ERROR("MEMSET",
6581d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6582554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6583554e165cSAndy Whitcroft				WARN("MEMSET",
6584d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6585d7c76ba7SJoe Perches			}
6586d7c76ba7SJoe Perches		}
6587d7c76ba7SJoe Perches
658898a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
65895b57980dSJoe Perches#		if ($perl_version_ok &&
6590f333195dSJoe Perches#		    defined $stat &&
6591f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6592f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6593f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6594f333195dSJoe Perches#			    $fix) {
6595f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6596f333195dSJoe Perches#			}
6597f333195dSJoe Perches#		}
659898a9bba5SJoe Perches
6599b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
66005b57980dSJoe Perches#		if ($perl_version_ok &&
6601f333195dSJoe Perches#		    defined $stat &&
6602f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6603f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6604f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6605f333195dSJoe Perches#		}
6606b6117d17SMateusz Kulikowski
66078617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
66088617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
66095b57980dSJoe Perches#		if ($perl_version_ok &&
6610f333195dSJoe Perches#		    defined $stat &&
6611f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6612f333195dSJoe Perches#
6613f333195dSJoe Perches#			my $ms_val = $7;
6614f333195dSJoe Perches#
6615f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6616f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6617f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6618f333195dSJoe Perches#				    $fix) {
6619f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6620f333195dSJoe Perches#				}
6621f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6622f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6623f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6624f333195dSJoe Perches#				    $fix) {
6625f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6626f333195dSJoe Perches#				}
6627f333195dSJoe Perches#			}
6628f333195dSJoe Perches#		}
66298617cd09SMateusz Kulikowski
6630d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
66315b57980dSJoe Perches		if ($perl_version_ok &&
6632d1fe9c09SJoe Perches		    defined $stat &&
6633d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6634d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6635d7c76ba7SJoe Perches				my $call = $1;
6636d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6637d7c76ba7SJoe Perches				my $arg1 = $3;
6638d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6639d1fe9c09SJoe Perches				my $arg2 = $8;
6640d7c76ba7SJoe Perches				my $cast;
6641d7c76ba7SJoe Perches
6642d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6643d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6644d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6645d7c76ba7SJoe Perches					$cast = $cast1;
6646d7c76ba7SJoe Perches				} else {
6647d7c76ba7SJoe Perches					$cast = $cast2;
6648d7c76ba7SJoe Perches				}
6649d7c76ba7SJoe Perches				WARN("MINMAX",
6650d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6651554e165cSAndy Whitcroft			}
6652554e165cSAndy Whitcroft		}
6653554e165cSAndy Whitcroft
66544a273195SJoe Perches# check usleep_range arguments
66555b57980dSJoe Perches		if ($perl_version_ok &&
66564a273195SJoe Perches		    defined $stat &&
66574a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
66584a273195SJoe Perches			my $min = $1;
66594a273195SJoe Perches			my $max = $7;
66604a273195SJoe Perches			if ($min eq $max) {
66614a273195SJoe Perches				WARN("USLEEP_RANGE",
6662458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
66634a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
66644a273195SJoe Perches				 $min > $max) {
66654a273195SJoe Perches				WARN("USLEEP_RANGE",
6666458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
66674a273195SJoe Perches			}
66684a273195SJoe Perches		}
66694a273195SJoe Perches
6670823b794cSJoe Perches# check for naked sscanf
66715b57980dSJoe Perches		if ($perl_version_ok &&
6672823b794cSJoe Perches		    defined $stat &&
66736c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6674823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6675823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6676823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6677823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6678823b794cSJoe Perches			$lc = $lc + $linenr;
66792a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6680823b794cSJoe Perches			WARN("NAKED_SSCANF",
6681823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6682823b794cSJoe Perches		}
6683823b794cSJoe Perches
6684afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
66855b57980dSJoe Perches		if ($perl_version_ok &&
6686afc819abSJoe Perches		    defined $stat &&
6687afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6688afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6689afc819abSJoe Perches			$lc = $lc + $linenr;
66902a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6691afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6692afc819abSJoe Perches				my $format = $6;
6693afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6694afc819abSJoe Perches				if ($count == 1 &&
6695afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6696afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6697afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6698afc819abSJoe Perches				}
6699afc819abSJoe Perches			}
6700afc819abSJoe Perches		}
6701afc819abSJoe Perches
670270dc8a48SJoe Perches# check for new externs in .h files.
670370dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
670470dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6705d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
670670dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
670770dc8a48SJoe Perches			    $fix) {
6708194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
670970dc8a48SJoe Perches			}
671070dc8a48SJoe Perches		}
671170dc8a48SJoe Perches
6712de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6713171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6714c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6715171ae1a4SAndy Whitcroft		{
6716c45dcabdSAndy Whitcroft			my $function_name = $1;
6717c45dcabdSAndy Whitcroft			my $paren_space = $2;
6718171ae1a4SAndy Whitcroft
6719171ae1a4SAndy Whitcroft			my $s = $stat;
6720171ae1a4SAndy Whitcroft			if (defined $cond) {
6721171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6722171ae1a4SAndy Whitcroft			}
6723d8b44b58SKees Cook			if ($s =~ /^\s*;/)
6724c45dcabdSAndy Whitcroft			{
6725000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6726000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6727de7d4f0eSAndy Whitcroft			}
6728de7d4f0eSAndy Whitcroft
6729171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6730000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6731000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6732171ae1a4SAndy Whitcroft			}
67339c9ba34eSAndy Whitcroft
67349c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
67359c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
67369c9ba34eSAndy Whitcroft		{
6737000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6738000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6739171ae1a4SAndy Whitcroft		}
6740171ae1a4SAndy Whitcroft
6741a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6742a0ad7596SJoe Perches		if (defined $stat &&
6743d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6744d8b44b58SKees Cook		    $1 ne "void") {
6745d8b44b58SKees Cook			my $args = trim($1);
6746ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6747ca0d8929SJoe Perches				my $arg = trim($1);
6748d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6749ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6750ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6751ca0d8929SJoe Perches				}
6752ca0d8929SJoe Perches			}
6753ca0d8929SJoe Perches		}
6754ca0d8929SJoe Perches
6755a0ad7596SJoe Perches# check for function definitions
67565b57980dSJoe Perches		if ($perl_version_ok &&
6757a0ad7596SJoe Perches		    defined $stat &&
6758a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6759a0ad7596SJoe Perches			$context_function = $1;
6760a0ad7596SJoe Perches
6761a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6762a0ad7596SJoe Perches			my $ok = 0;
6763a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6764a0ad7596SJoe Perches			my $herectx = $here . "\n";
6765a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6766a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6767a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6768a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6769a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6770a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6771a0ad7596SJoe Perches			}
6772a0ad7596SJoe Perches			if (!$ok) {
6773a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6774a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6775a0ad7596SJoe Perches			}
6776a0ad7596SJoe Perches		}
6777a0ad7596SJoe Perches
6778de7d4f0eSAndy Whitcroft# checks for new __setup's
6779de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6780de7d4f0eSAndy Whitcroft			my $name = $1;
6781de7d4f0eSAndy Whitcroft
6782de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6783000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
67842581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6785de7d4f0eSAndy Whitcroft			}
6786653d4876SAndy Whitcroft		}
67879c0ca6f9SAndy Whitcroft
6788e29a70f1SJoe Perches# check for pointless casting of alloc functions
6789e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6790000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6791000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
67929c0ca6f9SAndy Whitcroft		}
679313214adfSAndy Whitcroft
6794a640d25cSJoe Perches# alloc style
6795a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
67965b57980dSJoe Perches		if ($perl_version_ok &&
6797e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6798a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6799a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6800a640d25cSJoe Perches		}
6801a640d25cSJoe Perches
680260a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
68035b57980dSJoe Perches		if ($perl_version_ok &&
68041b4a2ed4SJoe Perches		    defined $stat &&
68051b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
680660a55369SJoe Perches			my $oldfunc = $3;
680760a55369SJoe Perches			my $a1 = $4;
680860a55369SJoe Perches			my $a2 = $10;
680960a55369SJoe Perches			my $newfunc = "kmalloc_array";
681060a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
681160a55369SJoe Perches			my $r1 = $a1;
681260a55369SJoe Perches			my $r2 = $a2;
681360a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
681460a55369SJoe Perches				$r1 = $a2;
681560a55369SJoe Perches				$r2 = $a1;
681660a55369SJoe Perches			}
6817e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6818e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
68191b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6820e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6821e3d95a2aSTobin C. Harding
6822e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
68231b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
68241b4a2ed4SJoe Perches				    $cnt == 1 &&
6825e367455aSJoe Perches				    $fix) {
6826194f66fcSJoe 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;
682760a55369SJoe Perches				}
682860a55369SJoe Perches			}
682960a55369SJoe Perches		}
683060a55369SJoe Perches
6831972fdea2SJoe Perches# check for krealloc arg reuse
68325b57980dSJoe Perches		if ($perl_version_ok &&
68334cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
68344cab63ceSJoe Perches		    $1 eq $3) {
6835972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6836972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6837972fdea2SJoe Perches		}
6838972fdea2SJoe Perches
68395ce59ae0SJoe Perches# check for alloc argument mismatch
68405ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
68415ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
68425ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
68435ce59ae0SJoe Perches		}
68445ce59ae0SJoe Perches
6845caf2a54fSJoe Perches# check for multiple semicolons
6846caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6847d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6848d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6849d5e616fcSJoe Perches			    $fix) {
6850194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6851d5e616fcSJoe Perches			}
6852d1e2ad07SJoe Perches		}
6853d1e2ad07SJoe Perches
6854cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6855cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6856cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
68570ab90191SJoe Perches			my $ull = "";
68580ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
68590ab90191SJoe Perches			if (CHK("BIT_MACRO",
68600ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
68610ab90191SJoe Perches			    $fix) {
68620ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
68630ab90191SJoe Perches			}
68640ab90191SJoe Perches		}
68650ab90191SJoe Perches
686650161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
68673e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
686850161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
68693e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
687050161266SJoe Perches		}
687150161266SJoe Perches
68722d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
68733e89ad85SJerome 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*$/) {
68742d632745SJoe Perches			my $config = $1;
68752d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
68763e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
68772d632745SJoe Perches			    $fix) {
68782d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
68792d632745SJoe Perches			}
68802d632745SJoe Perches		}
68812d632745SJoe Perches
6882f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6883f36d3eb8SJoe Perches		my @fallthroughs = (
6884f36d3eb8SJoe Perches			'fallthrough',
6885f36d3eb8SJoe Perches			'@fallthrough@',
6886f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6887f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6888f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6889f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6890f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6891f36d3eb8SJoe Perches		    );
6892f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6893f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6894f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6895f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6896f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6897f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6898f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6899f36d3eb8SJoe Perches					last;
6900f36d3eb8SJoe Perches				}
6901f36d3eb8SJoe Perches			}
6902f36d3eb8SJoe Perches		}
6903f36d3eb8SJoe Perches
6904d1e2ad07SJoe Perches# check for switch/default statements without a break;
69055b57980dSJoe Perches		if ($perl_version_ok &&
6906d1e2ad07SJoe Perches		    defined $stat &&
6907d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6908d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6909e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6910e3d95a2aSTobin C. Harding
6911d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6912d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6913caf2a54fSJoe Perches		}
6914caf2a54fSJoe Perches
691513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6916d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6917d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6918d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6919d5e616fcSJoe Perches			    $fix) {
6920194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6921d5e616fcSJoe Perches			}
692213214adfSAndy Whitcroft		}
6923773647a0SAndy Whitcroft
692462ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
692562ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
692662ec818fSJoe Perches			ERROR("DATE_TIME",
692762ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
692862ec818fSJoe Perches		}
692962ec818fSJoe Perches
69302c92488aSJoe Perches# check for use of yield()
69312c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
69322c92488aSJoe Perches			WARN("YIELD",
69332c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
69342c92488aSJoe Perches		}
69352c92488aSJoe Perches
6936179f8f40SJoe Perches# check for comparisons against true and false
6937179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6938179f8f40SJoe Perches			my $lead = $1;
6939179f8f40SJoe Perches			my $arg = $2;
6940179f8f40SJoe Perches			my $test = $3;
6941179f8f40SJoe Perches			my $otype = $4;
6942179f8f40SJoe Perches			my $trail = $5;
6943179f8f40SJoe Perches			my $op = "!";
6944179f8f40SJoe Perches
6945179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6946179f8f40SJoe Perches
6947179f8f40SJoe Perches			my $type = lc($otype);
6948179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6949179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6950179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6951179f8f40SJoe Perches					$op = "";
6952179f8f40SJoe Perches				}
6953179f8f40SJoe Perches
6954179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6955179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6956179f8f40SJoe Perches
6957179f8f40SJoe Perches## maybe suggesting a correct construct would better
6958179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6959179f8f40SJoe Perches
6960179f8f40SJoe Perches			}
6961179f8f40SJoe Perches		}
6962179f8f40SJoe Perches
69634882720bSThomas Gleixner# check for semaphores initialized locked
69644882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6965000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6966000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6967773647a0SAndy Whitcroft		}
69686712d858SJoe Perches
696967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
697067d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6971000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
697267d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6973773647a0SAndy Whitcroft		}
69746712d858SJoe Perches
6975ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6976f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6977000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6978ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6979f3db6639SMichael Ellerman		}
69806712d858SJoe Perches
69813d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
69823d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
69833d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
69843d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
69853d709ab5SPaul E. McKenney		}
69863d709ab5SPaul E. McKenney
69879189c7e7SJoe Perches# check for deprecated apis
69889189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
69899189c7e7SJoe Perches			my $deprecated_api = $1;
69909189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
69919189c7e7SJoe Perches			WARN("DEPRECATED_API",
69929189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
69939189c7e7SJoe Perches		}
69949189c7e7SJoe Perches
69950f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6996d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
6997ced69da1SQuentin Monnet		if (defined($const_structs) &&
6998ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
6999d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7000000d1cc1SJoe Perches			WARN("CONST_STRUCT",
7001d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
70022b6db5cbSAndy Whitcroft		}
7003773647a0SAndy Whitcroft
7004773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
7005773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
7006773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
7007c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7008c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7009171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7010171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7011171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
7012773647a0SAndy Whitcroft		{
7013000d1cc1SJoe Perches			WARN("NR_CPUS",
7014000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7015773647a0SAndy Whitcroft		}
70169c9ba34eSAndy Whitcroft
701752ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
701852ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
701952ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
702052ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
702152ea8506SJoe Perches		}
702252ea8506SJoe Perches
7023acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
70245b57980dSJoe Perches		if ($perl_version_ok &&
7025acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7026acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
7027acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7028acd9362cSJoe Perches		}
7029acd9362cSJoe Perches
7030de3f186fSDenis Efremov# nested likely/unlikely calls
7031de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7032de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
7033de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7034de3f186fSDenis Efremov		}
7035de3f186fSDenis Efremov
7036691d77b6SAndy Whitcroft# whine mightly about in_atomic
7037691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
7038691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
7039000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
7040000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
7041f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
7042000d1cc1SJoe Perches				WARN("IN_ATOMIC",
7043000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7044691d77b6SAndy Whitcroft			}
7045691d77b6SAndy Whitcroft		}
70461704f47bSPeter Zijlstra
70470f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
70480f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
70490f5225b0SPeter Zijlstra			ERROR("LOCKING",
70500f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
70510f5225b0SPeter Zijlstra		}
70520f5225b0SPeter Zijlstra
70531704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
70541704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
70551704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
70561704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
70571704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
70581704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
7059000d1cc1SJoe Perches				ERROR("LOCKDEP",
7060000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
70611704f47bSPeter Zijlstra			}
70621704f47bSPeter Zijlstra		}
706388f8831cSDave Jones
7064b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7065b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7066000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
7067000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
706888f8831cSDave Jones		}
70692435880fSJoe Perches
707000180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
707100180468SJoe Perches# and whether or not function naming is typical and if
707200180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
70735b57980dSJoe Perches		if ($perl_version_ok &&
707400180468SJoe Perches		    defined $stat &&
707500180468SJoe 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*\)/) {
707600180468SJoe Perches			my $var = $1;
707700180468SJoe Perches			my $perms = $2;
707800180468SJoe Perches			my $show = $3;
707900180468SJoe Perches			my $store = $4;
708000180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
708100180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
708200180468SJoe Perches			    $store =~ /^${var}_store$/ &&
708300180468SJoe Perches			    $octal_perms eq "0644") {
708400180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
708500180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
708600180468SJoe Perches				    $fix) {
708700180468SJoe 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})/;
708800180468SJoe Perches				}
708900180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
709000180468SJoe Perches				 $store =~ /^NULL$/ &&
709100180468SJoe Perches				 $octal_perms eq "0444") {
709200180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
709300180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
709400180468SJoe Perches				    $fix) {
709500180468SJoe 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})/;
709600180468SJoe Perches				}
709700180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
709800180468SJoe Perches				 $store =~ /^${var}_store$/ &&
709900180468SJoe Perches				 $octal_perms eq "0200") {
710000180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
710100180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
710200180468SJoe Perches				    $fix) {
710300180468SJoe 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})/;
710400180468SJoe Perches				}
710500180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
710600180468SJoe Perches				 $octal_perms eq "0444" ||
710700180468SJoe Perches				 $octal_perms eq "0200") {
710800180468SJoe Perches				my $newshow = "$show";
710900180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
711000180468SJoe Perches				my $newstore = $store;
711100180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
711200180468SJoe Perches				my $rename = "";
711300180468SJoe Perches				if ($show ne $newshow) {
711400180468SJoe Perches					$rename .= " '$show' to '$newshow'";
711500180468SJoe Perches				}
711600180468SJoe Perches				if ($store ne $newstore) {
711700180468SJoe Perches					$rename .= " '$store' to '$newstore'";
711800180468SJoe Perches				}
711900180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
712000180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
712100180468SJoe Perches			} else {
712200180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
712300180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
712400180468SJoe Perches			}
712500180468SJoe Perches		}
712600180468SJoe Perches
7127515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
7128515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
712973121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
713073121534SJoe Perches#   specific definition of not visible in sysfs.
713173121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
713273121534SJoe Perches#   use the default permissions
71335b57980dSJoe Perches		if ($perl_version_ok &&
7134459cf0aeSJoe Perches		    defined $stat &&
7135515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
71362435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
71372435880fSJoe Perches				my $func = $entry->[0];
71382435880fSJoe Perches				my $arg_pos = $entry->[1];
71392435880fSJoe Perches
7140459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
7141459cf0aeSJoe Perches				$lc = $lc + $linenr;
71422a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
7143459cf0aeSJoe Perches
71442435880fSJoe Perches				my $skip_args = "";
71452435880fSJoe Perches				if ($arg_pos > 1) {
71462435880fSJoe Perches					$arg_pos--;
71472435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
71482435880fSJoe Perches				}
7149f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7150459cf0aeSJoe Perches				if ($stat =~ /$test/) {
71512435880fSJoe Perches					my $val = $1;
71522435880fSJoe Perches					$val = $6 if ($skip_args ne "");
715373121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
715473121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
715573121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
71562435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
7157459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7158f90774e1SJoe Perches					}
7159f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7160c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
7161459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
71622435880fSJoe Perches					}
7163459cf0aeSJoe Perches				}
7164459cf0aeSJoe Perches			}
7165459cf0aeSJoe Perches		}
7166459cf0aeSJoe Perches
7167459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
7168bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
716900180468SJoe Perches			my $oval = $1;
717000180468SJoe Perches			my $octal = perms_to_octal($oval);
7171f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
7172459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7173f90774e1SJoe Perches			    $fix) {
717400180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
71752435880fSJoe Perches			}
717613214adfSAndy Whitcroft		}
71775a6d20ceSBjorn Andersson
71785a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
71795a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
71805a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
71815a6d20ceSBjorn Andersson			my $valid_licenses = qr{
71825a6d20ceSBjorn Andersson						GPL|
71835a6d20ceSBjorn Andersson						GPL\ v2|
71845a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
71855a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
71865a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
71875a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
71885a6d20ceSBjorn Andersson						Proprietary
71895a6d20ceSBjorn Andersson					}x;
71905a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
71915a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
71925a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
71935a6d20ceSBjorn Andersson			}
71945a6d20ceSBjorn Andersson		}
71956a8d76cbSMatteo Croce
71966a8d76cbSMatteo Croce# check for sysctl duplicate constants
71976a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
71986a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
71996a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
72006a8d76cbSMatteo Croce		}
7201515a235eSJoe Perches	}
720213214adfSAndy Whitcroft
720313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
720413214adfSAndy Whitcroft	# so just keep quiet.
720513214adfSAndy Whitcroft	if ($#rawlines == -1) {
720613214adfSAndy Whitcroft		exit(0);
72070a920b5bSAndy Whitcroft	}
72080a920b5bSAndy Whitcroft
72098905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
72108905a67cSAndy Whitcroft	# things that appear to be patches.
72118905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
72128905a67cSAndy Whitcroft		exit(0);
72138905a67cSAndy Whitcroft	}
72148905a67cSAndy Whitcroft
7215e73d2715SDwaipayan Ray	# This is not a patch, and we are in 'no-patch' mode so
72168905a67cSAndy Whitcroft	# just keep quiet.
72178905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
72188905a67cSAndy Whitcroft		exit(0);
72198905a67cSAndy Whitcroft	}
72208905a67cSAndy Whitcroft
7221a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7222000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
7223000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
72240a920b5bSAndy Whitcroft	}
7225cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
7226cd261496SGeert Uytterhoeven		if ($signoff == 0) {
7227000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
7228000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
722948ca2d8aSDwaipayan Ray		} elsif ($authorsignoff != 1) {
723048ca2d8aSDwaipayan Ray			# authorsignoff values:
723148ca2d8aSDwaipayan Ray			# 0 -> missing sign off
723248ca2d8aSDwaipayan Ray			# 1 -> sign off identical
723348ca2d8aSDwaipayan Ray			# 2 -> names and addresses match, comments mismatch
723448ca2d8aSDwaipayan Ray			# 3 -> addresses match, names different
723548ca2d8aSDwaipayan Ray			# 4 -> names match, addresses different
723648ca2d8aSDwaipayan Ray			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
723748ca2d8aSDwaipayan Ray
723848ca2d8aSDwaipayan Ray			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
723948ca2d8aSDwaipayan Ray
724048ca2d8aSDwaipayan Ray			if ($authorsignoff == 0) {
724148ca2d8aSDwaipayan Ray				ERROR("NO_AUTHOR_SIGN_OFF",
7242cd261496SGeert Uytterhoeven				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
724348ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 2) {
724448ca2d8aSDwaipayan Ray				CHK("FROM_SIGN_OFF_MISMATCH",
724548ca2d8aSDwaipayan Ray				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
724648ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 3) {
724748ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
724848ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
724948ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 4) {
725048ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
725148ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
725248ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 5) {
725348ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
725448ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
725548ca2d8aSDwaipayan Ray			}
7256cd261496SGeert Uytterhoeven		}
72570a920b5bSAndy Whitcroft	}
72580a920b5bSAndy Whitcroft
7259f0a594c1SAndy Whitcroft	print report_dump();
726013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
726113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
72626c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
72636c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
72646c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
72656c72ffaaSAndy Whitcroft	}
72668905a67cSAndy Whitcroft
7267d2c0a235SAndy Whitcroft	if ($quiet == 0) {
7268ef212196SJoe Perches		# If there were any defects found and not already fixing them
7269ef212196SJoe Perches		if (!$clean and !$fix) {
7270ef212196SJoe Perches			print << "EOM"
7271ef212196SJoe Perches
7272ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
7273ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
7274ef212196SJoe PerchesEOM
7275ef212196SJoe Perches		}
7276d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
7277d2c0a235SAndy Whitcroft		# then suggest that.
7278d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
7279b0781216SMike Frysinger			$rpt_cleaners = 0;
7280d8469f16SJoe Perches			print << "EOM"
7281d8469f16SJoe Perches
7282d8469f16SJoe PerchesNOTE: Whitespace errors detected.
7283d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
7284d8469f16SJoe PerchesEOM
7285d2c0a235SAndy Whitcroft		}
7286d2c0a235SAndy Whitcroft	}
7287d2c0a235SAndy Whitcroft
7288d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
7289d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
7290d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
72919624b8d6SJoe Perches		my $newfile = $filename;
72929624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
72933705ce5bSJoe Perches		my $linecount = 0;
72943705ce5bSJoe Perches		my $f;
72953705ce5bSJoe Perches
7296d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7297d752fcc8SJoe Perches
72983705ce5bSJoe Perches		open($f, '>', $newfile)
72993705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
73003705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
73013705ce5bSJoe Perches			$linecount++;
73023705ce5bSJoe Perches			if ($file) {
73033705ce5bSJoe Perches				if ($linecount > 3) {
73043705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
73053705ce5bSJoe Perches					print $f $fixed_line . "\n";
73063705ce5bSJoe Perches				}
73073705ce5bSJoe Perches			} else {
73083705ce5bSJoe Perches				print $f $fixed_line . "\n";
73093705ce5bSJoe Perches			}
73103705ce5bSJoe Perches		}
73113705ce5bSJoe Perches		close($f);
73123705ce5bSJoe Perches
73133705ce5bSJoe Perches		if (!$quiet) {
73143705ce5bSJoe Perches			print << "EOM";
7315d8469f16SJoe Perches
73163705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
73173705ce5bSJoe Perches
73183705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
73193705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
73203705ce5bSJoe Perches
73213705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
73223705ce5bSJoe PerchesNo warranties, expressed or implied...
73233705ce5bSJoe PerchesEOM
73243705ce5bSJoe Perches		}
73253705ce5bSJoe Perches	}
73263705ce5bSJoe Perches
7327d8469f16SJoe Perches	if ($quiet == 0) {
7328d8469f16SJoe Perches		print "\n";
7329d8469f16SJoe Perches		if ($clean == 1) {
7330d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7331d8469f16SJoe Perches		} else {
7332d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
73330a920b5bSAndy Whitcroft		}
73340a920b5bSAndy Whitcroft	}
73350a920b5bSAndy Whitcroft	return $clean;
73360a920b5bSAndy Whitcroft}
7337