xref: /linux-6.15/scripts/checkpatch.pl (revision 1a3dcf2e)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0
3882ea1d6SJoe Perches#
4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]>
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftuse strict;
11cb77f0d6SKamil Rytarowskiuse warnings;
12c707a81dSJoe Perchesuse POSIX;
1336061e38SJoe Perchesuse File::Basename;
1436061e38SJoe Perchesuse Cwd 'abs_path';
1557230297SJoe Perchesuse Term::ANSIColor qw(:constants);
16cd261496SGeert Uytterhoevenuse Encode qw(decode encode);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $P = $0;
1936061e38SJoe Perchesmy $D = dirname(abs_path($P));
200a920b5bSAndy Whitcroft
21000d1cc1SJoe Perchesmy $V = '0.32';
220a920b5bSAndy Whitcroft
230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
240a920b5bSAndy Whitcroft
250a920b5bSAndy Whitcroftmy $quiet = 0;
260a920b5bSAndy Whitcroftmy $tree = 1;
270a920b5bSAndy Whitcroftmy $chk_signoff = 1;
280a920b5bSAndy Whitcroftmy $chk_patch = 1;
29773647a0SAndy Whitcroftmy $tst_only;
306c72ffaaSAndy Whitcroftmy $emacs = 0;
318905a67cSAndy Whitcroftmy $terse = 0;
3234d8815fSJoe Perchesmy $showfile = 0;
336c72ffaaSAndy Whitcroftmy $file = 0;
344a593c34SDu, Changbinmy $git = 0;
350dea9f1eSJoe Perchesmy %git_commits = ();
366c72ffaaSAndy Whitcroftmy $check = 0;
372ac73b4fSJoe Perchesmy $check_orig = 0;
388905a67cSAndy Whitcroftmy $summary = 1;
398905a67cSAndy Whitcroftmy $mailback = 0;
4013214adfSAndy Whitcroftmy $summary_file = 0;
41000d1cc1SJoe Perchesmy $show_types = 0;
423beb42ecSJoe Perchesmy $list_types = 0;
433705ce5bSJoe Perchesmy $fix = 0;
449624b8d6SJoe Perchesmy $fix_inplace = 0;
456c72ffaaSAndy Whitcroftmy $root;
46c2fdda0dSAndy Whitcroftmy %debug;
473445686aSJoe Perchesmy %camelcase = ();
4891bfe484SJoe Perchesmy %use_type = ();
4991bfe484SJoe Perchesmy @use = ();
5091bfe484SJoe Perchesmy %ignore_type = ();
51000d1cc1SJoe Perchesmy @ignore = ();
5277f5b10aSHannes Edermy $help = 0;
53000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
54bdc48fa1SJoe Perchesmy $max_line_length = 100;
55d62a201fSDave Hansenmy $ignore_perl_version = 0;
56d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5756193274SVadim Bendeburymy $min_conf_desc_length = 4;
5866b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
59ebfd7d62SJoe Perchesmy $codespell = 0;
60f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
61bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
62ced69da1SQuentin Monnetmy $typedefsfile;
63737c0767SJohn Brooksmy $color = "auto";
6498005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
65dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
66dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
67713a09deSAntonio Borneomy $tabsize = 8;
6877f5b10aSHannes Eder
6977f5b10aSHannes Edersub help {
7077f5b10aSHannes Eder	my ($exitcode) = @_;
7177f5b10aSHannes Eder
7277f5b10aSHannes Eder	print << "EOM";
7377f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7477f5b10aSHannes EderVersion: $V
7577f5b10aSHannes Eder
7677f5b10aSHannes EderOptions:
7777f5b10aSHannes Eder  -q, --quiet                quiet
7877f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7977f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
8077f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
8177f5b10aSHannes Eder  --emacs                    emacs compile window format
8277f5b10aSHannes Eder  --terse                    one line per report
8334d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
844a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
854a593c34SDu, Changbin                             single git commit with:
864a593c34SDu, Changbin                               <rev>
874a593c34SDu, Changbin                               <rev>^
884a593c34SDu, Changbin                               <rev>~n
894a593c34SDu, Changbin                             multiple git commits with:
904a593c34SDu, Changbin                               <rev1>..<rev2>
914a593c34SDu, Changbin                               <rev1>...<rev2>
924a593c34SDu, Changbin                               <rev>-<count>
934a593c34SDu, Changbin                             git merges are ignored
9477f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9577f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
963beb42ecSJoe Perches  --list-types               list the possible message types
9791bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
98000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
993beb42ecSJoe Perches  --show-types               show the specific message type in the output
100bdc48fa1SJoe Perches  --max-line-length=n        set the maximum line length, (default $max_line_length)
101bdc48fa1SJoe Perches                             if exceeded, warn on patches
102bdc48fa1SJoe Perches                             requires --strict for use with --file
10356193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
104bdc48fa1SJoe Perches  --tab-size=n               set the number of spaces for tab (default $tabsize)
10577f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10677f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10777f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10877f5b10aSHannes Eder  --summary-file             include the filename in summary
10977f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
11077f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
11177f5b10aSHannes Eder                             is all off)
11277f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
11377f5b10aSHannes Eder                             literally
1143705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1153705ce5bSJoe Perches                             If correctable single-line errors exist, create
1163705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1173705ce5bSJoe Perches                             with potential errors corrected to the preferred
1183705ce5bSJoe Perches                             checkpatch style
1199624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1209624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1219624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
122d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
123d62a201fSDave Hansen                             runtime errors.
124ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
125f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
126ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12775ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
128737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
129737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
13077f5b10aSHannes Eder  -h, --help, --version      display this help and exit
13177f5b10aSHannes Eder
13277f5b10aSHannes EderWhen FILE is - read standard input.
13377f5b10aSHannes EderEOM
13477f5b10aSHannes Eder
13577f5b10aSHannes Eder	exit($exitcode);
13677f5b10aSHannes Eder}
13777f5b10aSHannes Eder
1383beb42ecSJoe Perchessub uniq {
1393beb42ecSJoe Perches	my %seen;
1403beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1413beb42ecSJoe Perches}
1423beb42ecSJoe Perches
1433beb42ecSJoe Perchessub list_types {
1443beb42ecSJoe Perches	my ($exitcode) = @_;
1453beb42ecSJoe Perches
1463beb42ecSJoe Perches	my $count = 0;
1473beb42ecSJoe Perches
1483beb42ecSJoe Perches	local $/ = undef;
1493beb42ecSJoe Perches
1503beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1513beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1523beb42ecSJoe Perches
1533beb42ecSJoe Perches	my $text = <$script>;
1543beb42ecSJoe Perches	close($script);
1553beb42ecSJoe Perches
1563beb42ecSJoe Perches	my @types = ();
1570547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1580547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1593beb42ecSJoe Perches		push (@types, $_);
1603beb42ecSJoe Perches	}
1613beb42ecSJoe Perches	@types = sort(uniq(@types));
1623beb42ecSJoe Perches	print("#\tMessage type\n\n");
1633beb42ecSJoe Perches	foreach my $type (@types) {
1643beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1653beb42ecSJoe Perches	}
1663beb42ecSJoe Perches
1673beb42ecSJoe Perches	exit($exitcode);
1683beb42ecSJoe Perches}
1693beb42ecSJoe Perches
170000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
171000d1cc1SJoe Perchesif (-f $conf) {
172000d1cc1SJoe Perches	my @conf_args;
173000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
174000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
175000d1cc1SJoe Perches
176000d1cc1SJoe Perches	while (<$conffile>) {
177000d1cc1SJoe Perches		my $line = $_;
178000d1cc1SJoe Perches
179000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
180000d1cc1SJoe Perches		$line =~ s/^\s*//g;
181000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
182000d1cc1SJoe Perches
183000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
184000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
185000d1cc1SJoe Perches
186000d1cc1SJoe Perches		my @words = split(" ", $line);
187000d1cc1SJoe Perches		foreach my $word (@words) {
188000d1cc1SJoe Perches			last if ($word =~ m/^#/);
189000d1cc1SJoe Perches			push (@conf_args, $word);
190000d1cc1SJoe Perches		}
191000d1cc1SJoe Perches	}
192000d1cc1SJoe Perches	close($conffile);
193000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
194000d1cc1SJoe Perches}
195000d1cc1SJoe Perches
196737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
197737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
198737c0767SJohn Brooksforeach (@ARGV) {
199737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
200737c0767SJohn Brooks		$_ = "--color=$color";
201737c0767SJohn Brooks	}
202737c0767SJohn Brooks}
203737c0767SJohn Brooks
2040a920b5bSAndy WhitcroftGetOptions(
2056c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2060a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2070a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2080a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2096c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2108905a67cSAndy Whitcroft	'terse!'	=> \$terse,
21134d8815fSJoe Perches	'showfile!'	=> \$showfile,
21277f5b10aSHannes Eder	'f|file!'	=> \$file,
2134a593c34SDu, Changbin	'g|git!'	=> \$git,
2146c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2156c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
216000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
21791bfe484SJoe Perches	'types=s'	=> \@use,
218000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2193beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2206cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
22156193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
222713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
2236c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2248905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2258905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
22613214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2273705ce5bSJoe Perches	'fix!'		=> \$fix,
2289624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
229d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
230c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
231773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
232ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
233ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
23475ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
235737c0767SJohn Brooks	'color=s'	=> \$color,
236737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
237737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
23877f5b10aSHannes Eder	'h|help'	=> \$help,
23977f5b10aSHannes Eder	'version'	=> \$help
24077f5b10aSHannes Eder) or help(1);
24177f5b10aSHannes Eder
24277f5b10aSHannes Ederhelp(0) if ($help);
2430a920b5bSAndy Whitcroft
2443beb42ecSJoe Percheslist_types(0) if ($list_types);
2453beb42ecSJoe Perches
2469624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2472ac73b4fSJoe Perches$check_orig = $check;
2489624b8d6SJoe Perches
24932f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
25032f30ca9SJoe Perches
2510a920b5bSAndy Whitcroftmy $exit = 0;
2520a920b5bSAndy Whitcroft
2535b57980dSJoe Perchesmy $perl_version_ok = 1;
254d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2555b57980dSJoe Perches	$perl_version_ok = 0;
256d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2575b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
258d62a201fSDave Hansen}
259d62a201fSDave Hansen
26045107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2610a920b5bSAndy Whitcroftif ($#ARGV < 0) {
26245107ff6SAllen Hubbe	push(@ARGV, '-');
2630a920b5bSAndy Whitcroft}
2640a920b5bSAndy Whitcroft
265737c0767SJohn Brooksif ($color =~ /^[01]$/) {
266737c0767SJohn Brooks	$color = !$color;
267737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
268737c0767SJohn Brooks	$color = 1;
269737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
270737c0767SJohn Brooks	$color = 0;
271737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
272737c0767SJohn Brooks	$color = (-t STDOUT);
273737c0767SJohn Brooks} else {
27432f30ca9SJoe Perches	die "$P: Invalid color mode: $color\n";
275737c0767SJohn Brooks}
276737c0767SJohn Brooks
277713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
27832f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
279713a09deSAntonio Borneo
28091bfe484SJoe Perchessub hash_save_array_words {
28191bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
28291bfe484SJoe Perches
28391bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
28491bfe484SJoe Perches	foreach my $word (@array) {
285000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
286000d1cc1SJoe Perches		$word =~ s/^\s*//g;
287000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
288000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
289000d1cc1SJoe Perches
290000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
291000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
292000d1cc1SJoe Perches
29391bfe484SJoe Perches		$hashRef->{$word}++;
294000d1cc1SJoe Perches	}
29591bfe484SJoe Perches}
29691bfe484SJoe Perches
29791bfe484SJoe Perchessub hash_show_words {
29891bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
29991bfe484SJoe Perches
3003c816e49SJoe Perches	if (keys %$hashRef) {
301d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
30258cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
30391bfe484SJoe Perches			print " $word";
30491bfe484SJoe Perches		}
305d8469f16SJoe Perches		print "\n";
30691bfe484SJoe Perches	}
30791bfe484SJoe Perches}
30891bfe484SJoe Perches
30991bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
31091bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
311000d1cc1SJoe Perches
312c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
313c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3147429c690SAndy Whitcroftmy $dbg_type = 0;
315a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
316c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
31721caa13cSAndy Whitcroft	## no critic
31821caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
31921caa13cSAndy Whitcroft	die "$@" if ($@);
320c2fdda0dSAndy Whitcroft}
321c2fdda0dSAndy Whitcroft
322d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
323d2c0a235SAndy Whitcroft
3248905a67cSAndy Whitcroftif ($terse) {
3258905a67cSAndy Whitcroft	$emacs = 1;
3268905a67cSAndy Whitcroft	$quiet++;
3278905a67cSAndy Whitcroft}
3288905a67cSAndy Whitcroft
3296c72ffaaSAndy Whitcroftif ($tree) {
3306c72ffaaSAndy Whitcroft	if (defined $root) {
3316c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3326c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3336c72ffaaSAndy Whitcroft		}
3346c72ffaaSAndy Whitcroft	} else {
3356c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3366c72ffaaSAndy Whitcroft			$root = '.';
3376c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3386c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3396c72ffaaSAndy Whitcroft			$root = $1;
3406c72ffaaSAndy Whitcroft		}
3416c72ffaaSAndy Whitcroft	}
3426c72ffaaSAndy Whitcroft
3436c72ffaaSAndy Whitcroft	if (!defined $root) {
3440a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3450a920b5bSAndy Whitcroft		exit(2);
3460a920b5bSAndy Whitcroft	}
3476c72ffaaSAndy Whitcroft}
3486c72ffaaSAndy Whitcroft
3496c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3506c72ffaaSAndy Whitcroft
3512ceb532bSAndy Whitcroftour $Ident	= qr{
3522ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3532ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3542ceb532bSAndy Whitcroft		}x;
3556c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3566c72ffaaSAndy Whitcroftour $Sparse	= qr{
3576c72ffaaSAndy Whitcroft			__user|
3586c72ffaaSAndy Whitcroft			__kernel|
3596c72ffaaSAndy Whitcroft			__force|
3606c72ffaaSAndy Whitcroft			__iomem|
3616c72ffaaSAndy Whitcroft			__must_check|
362417495edSAndy Whitcroft			__kprobes|
363165e72a6SSven Eckelmann			__ref|
36433aa4597SGeert Uytterhoeven			__refconst|
36533aa4597SGeert Uytterhoeven			__refdata|
366ad315455SBoqun Feng			__rcu|
367ad315455SBoqun Feng			__private
3686c72ffaaSAndy Whitcroft		}x;
369e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
370e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
371e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
372e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
373e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3748716de38SJoe Perches
37552131292SWolfram Sang# Notes to $Attribute:
37652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3776c72ffaaSAndy Whitcroftour $Attribute	= qr{
3786c72ffaaSAndy Whitcroft			const|
37903f1df7dSJoe Perches			__percpu|
38003f1df7dSJoe Perches			__nocast|
38103f1df7dSJoe Perches			__safe|
38246d832f5SMichael S. Tsirkin			__bitwise|
38303f1df7dSJoe Perches			__packed__|
38403f1df7dSJoe Perches			__packed2__|
38503f1df7dSJoe Perches			__naked|
38603f1df7dSJoe Perches			__maybe_unused|
38703f1df7dSJoe Perches			__always_unused|
38803f1df7dSJoe Perches			__noreturn|
38903f1df7dSJoe Perches			__used|
39003f1df7dSJoe Perches			__cold|
391e23ef1f3SJoe Perches			__pure|
39203f1df7dSJoe Perches			__noclone|
39303f1df7dSJoe Perches			__deprecated|
3946c72ffaaSAndy Whitcroft			__read_mostly|
395c5967e98SJoe Perches			__ro_after_init|
3966c72ffaaSAndy Whitcroft			__kprobes|
3978716de38SJoe Perches			$InitAttribute|
39824e1d81aSAndy Whitcroft			____cacheline_aligned|
39924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
4005fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
4015fe3af11SAndy Whitcroft			__weak
4026c72ffaaSAndy Whitcroft		  }x;
403c45dcabdSAndy Whitcroftour $Modifier;
40491cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
4056c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
4066c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
4076c72ffaaSAndy Whitcroft
40895e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
40995e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
41095e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
41195e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4122435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
413c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
414326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
415326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
416326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
41774349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4182435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
419326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
420447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
42123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4226c72ffaaSAndy Whitcroftour $Operators	= qr{
4236c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4246c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
42523f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4266c72ffaaSAndy Whitcroft		  }x;
4276c72ffaaSAndy Whitcroft
42891cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
42991cb5195SJoe Perches
430ab7e23f3SJoe Perchesour $BasicType;
4318905a67cSAndy Whitcroftour $NonptrType;
4321813087dSJoe Perchesour $NonptrTypeMisordered;
4338716de38SJoe Perchesour $NonptrTypeWithAttr;
4348905a67cSAndy Whitcroftour $Type;
4351813087dSJoe Perchesour $TypeMisordered;
4368905a67cSAndy Whitcroftour $Declare;
4371813087dSJoe Perchesour $DeclareMisordered;
4388905a67cSAndy Whitcroft
43915662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
44015662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
441171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
442171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
443171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
444171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
445171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
446171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
447171ae1a4SAndy Whitcroft}x;
448171ae1a4SAndy Whitcroft
44915662b3eSJoe Perchesour $UTF8	= qr{
45015662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
45115662b3eSJoe Perches	| $NON_ASCII_UTF8
45215662b3eSJoe Perches}x;
45315662b3eSJoe Perches
454e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
455021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
456021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
457021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
458021158b4SJoe Perches)};
459e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
460fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4618ed22cadSAndy Whitcroft	atomic_t
4628ed22cadSAndy Whitcroft)};
463e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
464e6176fa4SJoe Perches	$typeC99Typedefs\b|
465e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
466e6176fa4SJoe Perches	$typeKernelTypedefs\b
467e6176fa4SJoe Perches)};
4688ed22cadSAndy Whitcroft
4696d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4706d32f7a3SJoe Perches
471691e669bSJoe Perchesour $logFunctions = qr{(?x:
472758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4737d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
47487bd499aSJoe Perches	TP_printk|
4756e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
476b0531722SJoe Perches	panic|
47706668727SJoe Perches	MODULE_[A-Z_]+|
47806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
479691e669bSJoe Perches)};
480691e669bSJoe Perches
481e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
482e29a70f1SJoe Perches	(?:(?:devm_)?
483e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
484e29a70f1SJoe Perches		kstrdup(?:_const)? |
485e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
486461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
487e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
488e29a70f1SJoe Perches	dma_alloc_coherent
489e29a70f1SJoe Perches)};
490e29a70f1SJoe Perches
49120112475SJoe Perchesour $signature_tags = qr{(?xi:
49220112475SJoe Perches	Signed-off-by:|
493d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
49420112475SJoe Perches	Acked-by:|
49520112475SJoe Perches	Tested-by:|
49620112475SJoe Perches	Reviewed-by:|
49720112475SJoe Perches	Reported-by:|
4988543ae12SMugunthan V N	Suggested-by:|
49920112475SJoe Perches	To:|
50020112475SJoe Perches	Cc:
50120112475SJoe Perches)};
50220112475SJoe Perches
5031813087dSJoe Perchesour @typeListMisordered = (
5041813087dSJoe Perches	qr{char\s+(?:un)?signed},
5051813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
5061813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
5071813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
5081813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
5091813087dSJoe Perches	qr{short\s+(?:un)?signed},
5101813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5111813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5121813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5131813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5141813087dSJoe Perches	qr{int\s+(?:un)?signed},
5151813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5161813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5171813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5181813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5191813087dSJoe Perches	qr{long\s+(?:un)?signed},
5201813087dSJoe Perches);
5211813087dSJoe Perches
5228905a67cSAndy Whitcroftour @typeList = (
5238905a67cSAndy Whitcroft	qr{void},
5240c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5250c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5260c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5270c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5280c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5290c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5300c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5310c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5320c773d9dSJoe Perches	qr{(?:un)?signed},
5338905a67cSAndy Whitcroft	qr{float},
5348905a67cSAndy Whitcroft	qr{double},
5358905a67cSAndy Whitcroft	qr{bool},
5368905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5378905a67cSAndy Whitcroft	qr{union\s+$Ident},
5388905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5398905a67cSAndy Whitcroft	qr{${Ident}_t},
5408905a67cSAndy Whitcroft	qr{${Ident}_handler},
5418905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5421813087dSJoe Perches	@typeListMisordered,
5438905a67cSAndy Whitcroft);
544938224b5SJoe Perches
545938224b5SJoe Perchesour $C90_int_types = qr{(?x:
546938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
547938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
548938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
549938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
550938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
551938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
552938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
553938224b5SJoe Perches
554938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
555938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
556938224b5SJoe Perches	long\s+(?:un)?signed|
557938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
558938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
559938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
560938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
561938224b5SJoe Perches
562938224b5SJoe Perches	int\s+(?:un)?signed|
563938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
564938224b5SJoe Perches)};
565938224b5SJoe Perches
566485ff23eSAlex Dowadour @typeListFile = ();
5678716de38SJoe Perchesour @typeListWithAttr = (
5688716de38SJoe Perches	@typeList,
5698716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5708716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5718716de38SJoe Perches);
5728716de38SJoe Perches
573c45dcabdSAndy Whitcroftour @modifierList = (
574c45dcabdSAndy Whitcroft	qr{fastcall},
575c45dcabdSAndy Whitcroft);
576485ff23eSAlex Dowadour @modifierListFile = ();
5778905a67cSAndy Whitcroft
5782435880fSJoe Perchesour @mode_permission_funcs = (
5792435880fSJoe Perches	["module_param", 3],
5802435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5812435880fSJoe Perches	["module_param_array_named", 5],
5822435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5832435880fSJoe Perches	["proc_create(?:_data|)", 2],
584459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
585459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
586459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
587459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
588459cf0aeSJoe Perches	["__ATTR", 2],
5892435880fSJoe Perches);
5902435880fSJoe Perches
591*1a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
592*1a3dcf2eSJoe Perches
593515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
594515a235eSJoe Perchesour $mode_perms_search = "";
595515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
596515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
597515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
598515a235eSJoe Perches}
59900180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
600515a235eSJoe Perches
6019189c7e7SJoe Perchesour %deprecated_apis = (
6029189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
6039189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
6049189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
6059189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
6069189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
6079189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
6089189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
6099189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
6109189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
6119189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6129189c7e7SJoe Perches);
6139189c7e7SJoe Perches
6149189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6159189c7e7SJoe Perchesour $deprecated_apis_search = "";
6169189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6179189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6189189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6199189c7e7SJoe Perches}
6209189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6219189c7e7SJoe Perches
622b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
623b392c64fSJoe Perches	S_IWUGO		|
624b392c64fSJoe Perches	S_IWOTH		|
625b392c64fSJoe Perches	S_IRWXUGO	|
626b392c64fSJoe Perches	S_IALLUGO	|
627b392c64fSJoe Perches	0[0-7][0-7][2367]
628b392c64fSJoe Perches}x;
629b392c64fSJoe Perches
630f90774e1SJoe Perchesour %mode_permission_string_types = (
631f90774e1SJoe Perches	"S_IRWXU" => 0700,
632f90774e1SJoe Perches	"S_IRUSR" => 0400,
633f90774e1SJoe Perches	"S_IWUSR" => 0200,
634f90774e1SJoe Perches	"S_IXUSR" => 0100,
635f90774e1SJoe Perches	"S_IRWXG" => 0070,
636f90774e1SJoe Perches	"S_IRGRP" => 0040,
637f90774e1SJoe Perches	"S_IWGRP" => 0020,
638f90774e1SJoe Perches	"S_IXGRP" => 0010,
639f90774e1SJoe Perches	"S_IRWXO" => 0007,
640f90774e1SJoe Perches	"S_IROTH" => 0004,
641f90774e1SJoe Perches	"S_IWOTH" => 0002,
642f90774e1SJoe Perches	"S_IXOTH" => 0001,
643f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
644f90774e1SJoe Perches	"S_IRUGO" => 0444,
645f90774e1SJoe Perches	"S_IWUGO" => 0222,
646f90774e1SJoe Perches	"S_IXUGO" => 0111,
647f90774e1SJoe Perches);
648f90774e1SJoe Perches
649f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
650f90774e1SJoe Perchesour $mode_perms_string_search = "";
651f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
652f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
653f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
654f90774e1SJoe Perches}
65500180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
65600180468SJoe Perchesour $multi_mode_perms_string_search = qr{
65700180468SJoe Perches	${single_mode_perms_string_search}
65800180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
65900180468SJoe Perches}x;
66000180468SJoe Perches
66100180468SJoe Perchessub perms_to_octal {
66200180468SJoe Perches	my ($string) = @_;
66300180468SJoe Perches
66400180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
66500180468SJoe Perches
66600180468SJoe Perches	my $val = "";
66700180468SJoe Perches	my $oval = "";
66800180468SJoe Perches	my $to = 0;
66900180468SJoe Perches	my $curpos = 0;
67000180468SJoe Perches	my $lastpos = 0;
67100180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
67200180468SJoe Perches		$curpos = pos($string);
67300180468SJoe Perches		my $match = $2;
67400180468SJoe Perches		my $omatch = $1;
67500180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
67600180468SJoe Perches		$lastpos = $curpos;
67700180468SJoe Perches		$to |= $mode_permission_string_types{$match};
67800180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
67900180468SJoe Perches		$val .= $match;
68000180468SJoe Perches		$oval .= $omatch;
68100180468SJoe Perches	}
68200180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
68300180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
68400180468SJoe Perches	return sprintf("%04o", $to);
68500180468SJoe Perches}
686f90774e1SJoe Perches
6877840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6887840a94cSWolfram Sang	irq|
689cdcee686SSergey Ryazanov	memory|
690cdcee686SSergey Ryazanov	time|
691cdcee686SSergey Ryazanov	reboot
6927840a94cSWolfram Sang)};
6937840a94cSWolfram Sang# memory.h: ARM has a custom one
6947840a94cSWolfram Sang
69566b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
69666b47b4aSKees Cookmy $misspellings;
69766b47b4aSKees Cookmy %spelling_fix;
69836061e38SJoe Perches
69936061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
70066b47b4aSKees Cook	while (<$spelling>) {
70166b47b4aSKees Cook		my $line = $_;
70266b47b4aSKees Cook
70366b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
70466b47b4aSKees Cook		$line =~ s/^\s*//g;
70566b47b4aSKees Cook
70666b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
70766b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
70866b47b4aSKees Cook
70966b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
71066b47b4aSKees Cook
71166b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
71266b47b4aSKees Cook	}
71366b47b4aSKees Cook	close($spelling);
71436061e38SJoe Perches} else {
71536061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
71636061e38SJoe Perches}
71766b47b4aSKees Cook
718ebfd7d62SJoe Perchesif ($codespell) {
719ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
720ebfd7d62SJoe Perches		while (<$spelling>) {
721ebfd7d62SJoe Perches			my $line = $_;
722ebfd7d62SJoe Perches
723ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
724ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
725ebfd7d62SJoe Perches
726ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
727ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
728ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
729ebfd7d62SJoe Perches
730ebfd7d62SJoe Perches			$line =~ s/,.*$//;
731ebfd7d62SJoe Perches
732ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
733ebfd7d62SJoe Perches
734ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
735ebfd7d62SJoe Perches		}
736ebfd7d62SJoe Perches		close($spelling);
737ebfd7d62SJoe Perches	} else {
738ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
739ebfd7d62SJoe Perches	}
740ebfd7d62SJoe Perches}
741ebfd7d62SJoe Perches
742ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
743ebfd7d62SJoe Perches
74475ad8c57SJerome Forissiersub read_words {
74575ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
74675ad8c57SJerome Forissier
74775ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
74875ad8c57SJerome Forissier		while (<$words>) {
749bf1fa1daSJoe Perches			my $line = $_;
750bf1fa1daSJoe Perches
751bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
752bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
753bf1fa1daSJoe Perches
754bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
755bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
756bf1fa1daSJoe Perches			if ($line =~ /\s/) {
75775ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
758bf1fa1daSJoe Perches				next;
759bf1fa1daSJoe Perches			}
760bf1fa1daSJoe Perches
761ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
76275ad8c57SJerome Forissier			$$wordsRef .= $line;
763bf1fa1daSJoe Perches		}
76475ad8c57SJerome Forissier		close($file);
76575ad8c57SJerome Forissier		return 1;
766bf1fa1daSJoe Perches	}
767bf1fa1daSJoe Perches
76875ad8c57SJerome Forissier	return 0;
76975ad8c57SJerome Forissier}
77075ad8c57SJerome Forissier
771ced69da1SQuentin Monnetmy $const_structs;
772ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
77375ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
77475ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
775ced69da1SQuentin Monnet}
77675ad8c57SJerome Forissier
777ced69da1SQuentin Monnetif (defined($typedefsfile)) {
778ced69da1SQuentin Monnet	my $typeOtherTypedefs;
77975ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
78075ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
781ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
78275ad8c57SJerome Forissier}
78375ad8c57SJerome Forissier
7848905a67cSAndy Whitcroftsub build_types {
785485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
786485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7871813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7888716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
789c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
790ab7e23f3SJoe Perches	$BasicType	= qr{
791ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
792ab7e23f3SJoe Perches				(?:${all}\b)
793ab7e23f3SJoe Perches		}x;
7948905a67cSAndy Whitcroft	$NonptrType	= qr{
795d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
796cf655043SAndy Whitcroft			(?:
7976b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7988ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
799c45dcabdSAndy Whitcroft				(?:${all}\b)
800cf655043SAndy Whitcroft			)
801c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
8028905a67cSAndy Whitcroft		  }x;
8031813087dSJoe Perches	$NonptrTypeMisordered	= qr{
8041813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
8051813087dSJoe Perches			(?:
8061813087dSJoe Perches				(?:${Misordered}\b)
8071813087dSJoe Perches			)
8081813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
8091813087dSJoe Perches		  }x;
8108716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
8118716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
8128716de38SJoe Perches			(?:
8138716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8148716de38SJoe Perches				(?:$typeTypedefs\b)|
8158716de38SJoe Perches				(?:${allWithAttr}\b)
8168716de38SJoe Perches			)
8178716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8188716de38SJoe Perches		  }x;
8198905a67cSAndy Whitcroft	$Type	= qr{
820c45dcabdSAndy Whitcroft			$NonptrType
8217b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
822c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8238905a67cSAndy Whitcroft		  }x;
8241813087dSJoe Perches	$TypeMisordered	= qr{
8251813087dSJoe Perches			$NonptrTypeMisordered
8267b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
8271813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8281813087dSJoe Perches		  }x;
82991cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8301813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8318905a67cSAndy Whitcroft}
8328905a67cSAndy Whitcroftbuild_types();
8336c72ffaaSAndy Whitcroft
8347d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
835d1fe9c09SJoe Perches
836d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
837d1fe9c09SJoe Perches# requires at least perl version v5.10.0
838d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
839d1fe9c09SJoe Perches
840d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8412435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
842c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8437d2367afSJoe Perches
844f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8453e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
846fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8473d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
848f8422308SJoe Perches)};
849f8422308SJoe Perches
8507d2367afSJoe Perchessub deparenthesize {
8517d2367afSJoe Perches	my ($string) = @_;
8527d2367afSJoe Perches	return "" if (!defined($string));
8535b9553abSJoe Perches
8545b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8555b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8565b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8575b9553abSJoe Perches	}
8585b9553abSJoe Perches
8597d2367afSJoe Perches	$string =~ s@\s+@ @g;
8605b9553abSJoe Perches
8617d2367afSJoe Perches	return $string;
8627d2367afSJoe Perches}
8637d2367afSJoe Perches
8643445686aSJoe Perchessub seed_camelcase_file {
8653445686aSJoe Perches	my ($file) = @_;
8663445686aSJoe Perches
8673445686aSJoe Perches	return if (!(-f $file));
8683445686aSJoe Perches
8693445686aSJoe Perches	local $/;
8703445686aSJoe Perches
8713445686aSJoe Perches	open(my $include_file, '<', "$file")
8723445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8733445686aSJoe Perches	my $text = <$include_file>;
8743445686aSJoe Perches	close($include_file);
8753445686aSJoe Perches
8763445686aSJoe Perches	my @lines = split('\n', $text);
8773445686aSJoe Perches
8783445686aSJoe Perches	foreach my $line (@lines) {
8793445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8803445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8813445686aSJoe Perches			$camelcase{$1} = 1;
88211ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
88311ea516aSJoe Perches			$camelcase{$1} = 1;
88411ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8853445686aSJoe Perches			$camelcase{$1} = 1;
8863445686aSJoe Perches		}
8873445686aSJoe Perches	}
8883445686aSJoe Perches}
8893445686aSJoe Perches
890cd28b119SJoe Perchesour %maintained_status = ();
891cd28b119SJoe Perches
89285b0ee18SJoe Perchessub is_maintained_obsolete {
89385b0ee18SJoe Perches	my ($filename) = @_;
89485b0ee18SJoe Perches
895f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
89685b0ee18SJoe Perches
897cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
898cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
899cd28b119SJoe Perches	}
90085b0ee18SJoe Perches
901cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
90285b0ee18SJoe Perches}
90385b0ee18SJoe Perches
9043b6e8ac9SJoe Perchessub is_SPDX_License_valid {
9053b6e8ac9SJoe Perches	my ($license) = @_;
9063b6e8ac9SJoe Perches
90756294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
9083b6e8ac9SJoe Perches
90956294112SJoe Perches	my $root_path = abs_path($root);
91056294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
9113b6e8ac9SJoe Perches	return 0 if ($status ne "");
9123b6e8ac9SJoe Perches	return 1;
9133b6e8ac9SJoe Perches}
9143b6e8ac9SJoe Perches
9153445686aSJoe Perchesmy $camelcase_seeded = 0;
9163445686aSJoe Perchessub seed_camelcase_includes {
9173445686aSJoe Perches	return if ($camelcase_seeded);
9183445686aSJoe Perches
9193445686aSJoe Perches	my $files;
920c707a81dSJoe Perches	my $camelcase_cache = "";
921c707a81dSJoe Perches	my @include_files = ();
922c707a81dSJoe Perches
923c707a81dSJoe Perches	$camelcase_seeded = 1;
924351b2a1fSJoe Perches
9253645e328SRichard Genoud	if (-e ".git") {
926dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
927351b2a1fSJoe Perches		chomp $git_last_include_commit;
928c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
929c707a81dSJoe Perches	} else {
930c707a81dSJoe Perches		my $last_mod_date = 0;
931c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
932c707a81dSJoe Perches		@include_files = split('\n', $files);
933c707a81dSJoe Perches		foreach my $file (@include_files) {
934c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
935c707a81dSJoe Perches						   localtime((stat $file)[9]));
936c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
937c707a81dSJoe Perches		}
938c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
939c707a81dSJoe Perches	}
940c707a81dSJoe Perches
941c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
942c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
943c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
944351b2a1fSJoe Perches		while (<$camelcase_file>) {
945351b2a1fSJoe Perches			chomp;
946351b2a1fSJoe Perches			$camelcase{$_} = 1;
947351b2a1fSJoe Perches		}
948351b2a1fSJoe Perches		close($camelcase_file);
949351b2a1fSJoe Perches
950351b2a1fSJoe Perches		return;
951351b2a1fSJoe Perches	}
952c707a81dSJoe Perches
9533645e328SRichard Genoud	if (-e ".git") {
954dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
955c707a81dSJoe Perches		@include_files = split('\n', $files);
9563445686aSJoe Perches	}
957c707a81dSJoe Perches
9583445686aSJoe Perches	foreach my $file (@include_files) {
9593445686aSJoe Perches		seed_camelcase_file($file);
9603445686aSJoe Perches	}
961351b2a1fSJoe Perches
962c707a81dSJoe Perches	if ($camelcase_cache ne "") {
963351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
964c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
965c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
966351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
967351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
968351b2a1fSJoe Perches		}
969351b2a1fSJoe Perches		close($camelcase_file);
970351b2a1fSJoe Perches	}
9713445686aSJoe Perches}
9723445686aSJoe Perches
973d311cd44SJoe Perchessub git_commit_info {
974d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
975d311cd44SJoe Perches
976d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
977d311cd44SJoe Perches
978dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
979d311cd44SJoe Perches	$output =~ s/^\s*//gm;
980d311cd44SJoe Perches	my @lines = split("\n", $output);
981d311cd44SJoe Perches
9820d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9830d7835fcSJoe Perches
9845a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
985d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
986d311cd44SJoe Perches# all matching commit ids, but it's very slow...
987d311cd44SJoe Perches#
988d311cd44SJoe Perches#		echo "checking commits $1..."
989d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
990d311cd44SJoe Perches#		while read line ; do
991d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
992d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
993d311cd44SJoe Perches#		done
994d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
995948b133aSHeinrich Schuchardt		$id = undef;
996d311cd44SJoe Perches	} else {
997d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
998d311cd44SJoe Perches		$desc = substr($lines[0], 41);
999d311cd44SJoe Perches	}
1000d311cd44SJoe Perches
1001d311cd44SJoe Perches	return ($id, $desc);
1002d311cd44SJoe Perches}
1003d311cd44SJoe Perches
10046c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
10050a920b5bSAndy Whitcroft
100600df344fSAndy Whitcroftmy @rawlines = ();
1007c2fdda0dSAndy Whitcroftmy @lines = ();
10083705ce5bSJoe Perchesmy @fixed = ();
1009d752fcc8SJoe Perchesmy @fixed_inserted = ();
1010d752fcc8SJoe Perchesmy @fixed_deleted = ();
1011194f66fcSJoe Perchesmy $fixlinenr = -1;
1012194f66fcSJoe Perches
10134a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10144a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10154a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
10164a593c34SDu, Changbin
10174a593c34SDu, Changbinif ($git) {
10184a593c34SDu, Changbin	my @commits = ();
10190dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
10204a593c34SDu, Changbin		my $git_range;
102128898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
102228898fd1SJoe Perches			$git_range = "-$2 $1";
10234a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
10244a593c34SDu, Changbin			$git_range = "$commit_expr";
10254a593c34SDu, Changbin		} else {
10260dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
10270dea9f1eSJoe Perches		}
1028dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10290dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
103028898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
103128898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10320dea9f1eSJoe Perches			my $sha1 = $1;
10330dea9f1eSJoe Perches			my $subject = $2;
10340dea9f1eSJoe Perches			unshift(@commits, $sha1);
10350dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10364a593c34SDu, Changbin		}
10374a593c34SDu, Changbin	}
10384a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10394a593c34SDu, Changbin	@ARGV = @commits;
10404a593c34SDu, Changbin}
10414a593c34SDu, Changbin
1042c2fdda0dSAndy Whitcroftmy $vname;
104398005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
10446c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
104521caa13cSAndy Whitcroft	my $FILE;
10464a593c34SDu, Changbin	if ($git) {
10474a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10484a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10494a593c34SDu, Changbin	} elsif ($file) {
105021caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10516c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
105221caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
105321caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10546c72ffaaSAndy Whitcroft	} else {
105521caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10566c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10576c72ffaaSAndy Whitcroft	}
1058c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1059c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10604a593c34SDu, Changbin	} elsif ($git) {
10610dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1062c2fdda0dSAndy Whitcroft	} else {
1063c2fdda0dSAndy Whitcroft		$vname = $filename;
1064c2fdda0dSAndy Whitcroft	}
106521caa13cSAndy Whitcroft	while (<$FILE>) {
10660a920b5bSAndy Whitcroft		chomp;
106700df344fSAndy Whitcroft		push(@rawlines, $_);
1068c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
10696c72ffaaSAndy Whitcroft	}
107021caa13cSAndy Whitcroft	close($FILE);
1071d8469f16SJoe Perches
1072d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1073d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1074d8469f16SJoe Perches		print "$vname\n";
1075d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1076d8469f16SJoe Perches	}
1077d8469f16SJoe Perches
1078c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10790a920b5bSAndy Whitcroft		$exit = 1;
10800a920b5bSAndy Whitcroft	}
108100df344fSAndy Whitcroft	@rawlines = ();
108213214adfSAndy Whitcroft	@lines = ();
10833705ce5bSJoe Perches	@fixed = ();
1084d752fcc8SJoe Perches	@fixed_inserted = ();
1085d752fcc8SJoe Perches	@fixed_deleted = ();
1086194f66fcSJoe Perches	$fixlinenr = -1;
1087485ff23eSAlex Dowad	@modifierListFile = ();
1088485ff23eSAlex Dowad	@typeListFile = ();
1089485ff23eSAlex Dowad	build_types();
10900a920b5bSAndy Whitcroft}
10910a920b5bSAndy Whitcroft
1092d8469f16SJoe Perchesif (!$quiet) {
10933c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10943c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10953c816e49SJoe Perches
10965b57980dSJoe Perches	if (!$perl_version_ok) {
1097d8469f16SJoe Perches		print << "EOM"
1098d8469f16SJoe Perches
1099d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
11005b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1101d8469f16SJoe PerchesEOM
1102d8469f16SJoe Perches	}
1103d8469f16SJoe Perches	if ($exit) {
1104d8469f16SJoe Perches		print << "EOM"
1105d8469f16SJoe Perches
1106d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1107d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1108d8469f16SJoe PerchesEOM
1109d8469f16SJoe Perches	}
1110d8469f16SJoe Perches}
1111d8469f16SJoe Perches
11120a920b5bSAndy Whitcroftexit($exit);
11130a920b5bSAndy Whitcroft
11140a920b5bSAndy Whitcroftsub top_of_kernel_tree {
11156c72ffaaSAndy Whitcroft	my ($root) = @_;
11166c72ffaaSAndy Whitcroft
11176c72ffaaSAndy Whitcroft	my @tree_check = (
11186c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
11196c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
11206c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
11216c72ffaaSAndy Whitcroft	);
11226c72ffaaSAndy Whitcroft
11236c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
11246c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
11250a920b5bSAndy Whitcroft			return 0;
11260a920b5bSAndy Whitcroft		}
11276c72ffaaSAndy Whitcroft	}
11286c72ffaaSAndy Whitcroft	return 1;
11296c72ffaaSAndy Whitcroft}
11300a920b5bSAndy Whitcroft
113120112475SJoe Perchessub parse_email {
113220112475SJoe Perches	my ($formatted_email) = @_;
113320112475SJoe Perches
113420112475SJoe Perches	my $name = "";
1135dfa05c28SJoe Perches	my $name_comment = "";
113620112475SJoe Perches	my $address = "";
113720112475SJoe Perches	my $comment = "";
113820112475SJoe Perches
113920112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
114020112475SJoe Perches		$name = $1;
114120112475SJoe Perches		$address = $2;
114220112475SJoe Perches		$comment = $3 if defined $3;
114320112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
114420112475SJoe Perches		$address = $1;
114520112475SJoe Perches		$comment = $2 if defined $2;
114620112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
114720112475SJoe Perches		$address = $1;
114820112475SJoe Perches		$comment = $2 if defined $2;
114985e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
115020112475SJoe Perches		$name = $formatted_email;
11513705ce5bSJoe Perches		$name = trim($name);
115220112475SJoe Perches		$name =~ s/^\"|\"$//g;
115320112475SJoe Perches		# If there's a name left after stripping spaces and
115420112475SJoe Perches		# leading quotes, and the address doesn't have both
115520112475SJoe Perches		# leading and trailing angle brackets, the address
115620112475SJoe Perches		# is invalid. ie:
115720112475SJoe Perches		#   "joe smith [email protected]" bad
115820112475SJoe Perches		#   "joe smith <[email protected]" bad
115920112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
116020112475SJoe Perches			$name = "";
116120112475SJoe Perches			$address = "";
116220112475SJoe Perches			$comment = "";
116320112475SJoe Perches		}
116420112475SJoe Perches	}
116520112475SJoe Perches
11663705ce5bSJoe Perches	$name = trim($name);
116720112475SJoe Perches	$name =~ s/^\"|\"$//g;
1168dfa05c28SJoe Perches	$name =~ s/(\s*\([^\)]+\))\s*//;
1169dfa05c28SJoe Perches	if (defined($1)) {
1170dfa05c28SJoe Perches		$name_comment = trim($1);
1171dfa05c28SJoe Perches	}
11723705ce5bSJoe Perches	$address = trim($address);
117320112475SJoe Perches	$address =~ s/^\<|\>$//g;
117420112475SJoe Perches
117520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
117620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
117720112475SJoe Perches		$name = "\"$name\"";
117820112475SJoe Perches	}
117920112475SJoe Perches
1180dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
118120112475SJoe Perches}
118220112475SJoe Perches
118320112475SJoe Perchessub format_email {
118420112475SJoe Perches	my ($name, $address) = @_;
118520112475SJoe Perches
118620112475SJoe Perches	my $formatted_email;
118720112475SJoe Perches
11883705ce5bSJoe Perches	$name = trim($name);
118920112475SJoe Perches	$name =~ s/^\"|\"$//g;
11903705ce5bSJoe Perches	$address = trim($address);
119120112475SJoe Perches
119220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
119320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
119420112475SJoe Perches		$name = "\"$name\"";
119520112475SJoe Perches	}
119620112475SJoe Perches
119720112475SJoe Perches	if ("$name" eq "") {
119820112475SJoe Perches		$formatted_email = "$address";
119920112475SJoe Perches	} else {
120020112475SJoe Perches		$formatted_email = "$name <$address>";
120120112475SJoe Perches	}
120220112475SJoe Perches
120320112475SJoe Perches	return $formatted_email;
120420112475SJoe Perches}
120520112475SJoe Perches
1206dfa05c28SJoe Perchessub reformat_email {
1207dfa05c28SJoe Perches	my ($email) = @_;
1208dfa05c28SJoe Perches
1209dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1210dfa05c28SJoe Perches	return format_email($email_name, $email_address);
1211dfa05c28SJoe Perches}
1212dfa05c28SJoe Perches
1213dfa05c28SJoe Perchessub same_email_addresses {
1214dfa05c28SJoe Perches	my ($email1, $email2) = @_;
1215dfa05c28SJoe Perches
1216dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1217dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1218dfa05c28SJoe Perches
1219dfa05c28SJoe Perches	return $email1_name eq $email2_name &&
1220dfa05c28SJoe Perches	       $email1_address eq $email2_address;
1221dfa05c28SJoe Perches}
1222dfa05c28SJoe Perches
1223d311cd44SJoe Perchessub which {
1224d311cd44SJoe Perches	my ($bin) = @_;
1225d311cd44SJoe Perches
1226d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1227d311cd44SJoe Perches		if (-e "$path/$bin") {
1228d311cd44SJoe Perches			return "$path/$bin";
1229d311cd44SJoe Perches		}
1230d311cd44SJoe Perches	}
1231d311cd44SJoe Perches
1232d311cd44SJoe Perches	return "";
1233d311cd44SJoe Perches}
1234d311cd44SJoe Perches
1235000d1cc1SJoe Perchessub which_conf {
1236000d1cc1SJoe Perches	my ($conf) = @_;
1237000d1cc1SJoe Perches
1238000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1239000d1cc1SJoe Perches		if (-e "$path/$conf") {
1240000d1cc1SJoe Perches			return "$path/$conf";
1241000d1cc1SJoe Perches		}
1242000d1cc1SJoe Perches	}
1243000d1cc1SJoe Perches
1244000d1cc1SJoe Perches	return "";
1245000d1cc1SJoe Perches}
1246000d1cc1SJoe Perches
12470a920b5bSAndy Whitcroftsub expand_tabs {
12480a920b5bSAndy Whitcroft	my ($str) = @_;
12490a920b5bSAndy Whitcroft
12500a920b5bSAndy Whitcroft	my $res = '';
12510a920b5bSAndy Whitcroft	my $n = 0;
12520a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12530a920b5bSAndy Whitcroft		if ($c eq "\t") {
12540a920b5bSAndy Whitcroft			$res .= ' ';
12550a920b5bSAndy Whitcroft			$n++;
1256713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
12570a920b5bSAndy Whitcroft				$res .= ' ';
12580a920b5bSAndy Whitcroft			}
12590a920b5bSAndy Whitcroft			next;
12600a920b5bSAndy Whitcroft		}
12610a920b5bSAndy Whitcroft		$res .= $c;
12620a920b5bSAndy Whitcroft		$n++;
12630a920b5bSAndy Whitcroft	}
12640a920b5bSAndy Whitcroft
12650a920b5bSAndy Whitcroft	return $res;
12660a920b5bSAndy Whitcroft}
12676c72ffaaSAndy Whitcroftsub copy_spacing {
1268773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
12696c72ffaaSAndy Whitcroft	return $res;
12706c72ffaaSAndy Whitcroft}
12710a920b5bSAndy Whitcroft
12724a0df2efSAndy Whitcroftsub line_stats {
12734a0df2efSAndy Whitcroft	my ($line) = @_;
12744a0df2efSAndy Whitcroft
12754a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12764a0df2efSAndy Whitcroft	$line =~ s/^.//;
12774a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12784a0df2efSAndy Whitcroft
12794a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12804a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12814a0df2efSAndy Whitcroft
12824a0df2efSAndy Whitcroft	return (length($line), length($white));
12834a0df2efSAndy Whitcroft}
12844a0df2efSAndy Whitcroft
1285773647a0SAndy Whitcroftmy $sanitise_quote = '';
1286773647a0SAndy Whitcroft
1287773647a0SAndy Whitcroftsub sanitise_line_reset {
1288773647a0SAndy Whitcroft	my ($in_comment) = @_;
1289773647a0SAndy Whitcroft
1290773647a0SAndy Whitcroft	if ($in_comment) {
1291773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1292773647a0SAndy Whitcroft	} else {
1293773647a0SAndy Whitcroft		$sanitise_quote = '';
1294773647a0SAndy Whitcroft	}
1295773647a0SAndy Whitcroft}
129600df344fSAndy Whitcroftsub sanitise_line {
129700df344fSAndy Whitcroft	my ($line) = @_;
129800df344fSAndy Whitcroft
129900df344fSAndy Whitcroft	my $res = '';
130000df344fSAndy Whitcroft	my $l = '';
130100df344fSAndy Whitcroft
1302c2fdda0dSAndy Whitcroft	my $qlen = 0;
1303773647a0SAndy Whitcroft	my $off = 0;
1304773647a0SAndy Whitcroft	my $c;
130500df344fSAndy Whitcroft
1306773647a0SAndy Whitcroft	# Always copy over the diff marker.
1307773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1308773647a0SAndy Whitcroft
1309773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1310773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1311773647a0SAndy Whitcroft
13128d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1313773647a0SAndy Whitcroft		# and end, all to $;.
1314773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1315773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1316773647a0SAndy Whitcroft
1317773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1318773647a0SAndy Whitcroft			$off++;
131900df344fSAndy Whitcroft			next;
1320773647a0SAndy Whitcroft		}
132181bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1322773647a0SAndy Whitcroft			$sanitise_quote = '';
1323773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1324773647a0SAndy Whitcroft			$off++;
1325773647a0SAndy Whitcroft			next;
1326773647a0SAndy Whitcroft		}
1327113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1328113f04a8SDaniel Walker			$sanitise_quote = '//';
1329113f04a8SDaniel Walker
1330113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1331113f04a8SDaniel Walker			$off++;
1332113f04a8SDaniel Walker			next;
1333113f04a8SDaniel Walker		}
1334773647a0SAndy Whitcroft
1335773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1336773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1337773647a0SAndy Whitcroft		    $c eq "\\") {
1338773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1339773647a0SAndy Whitcroft			$off++;
1340773647a0SAndy Whitcroft			next;
1341773647a0SAndy Whitcroft		}
1342773647a0SAndy Whitcroft		# Regular quotes.
1343773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1344773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1345773647a0SAndy Whitcroft				$sanitise_quote = $c;
1346773647a0SAndy Whitcroft
1347773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1348773647a0SAndy Whitcroft				next;
1349773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1350773647a0SAndy Whitcroft				$sanitise_quote = '';
135100df344fSAndy Whitcroft			}
135200df344fSAndy Whitcroft		}
1353773647a0SAndy Whitcroft
1354fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1355773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1356773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1357113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1358113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1359773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1360773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
136100df344fSAndy Whitcroft		} else {
1362773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
136300df344fSAndy Whitcroft		}
1364c2fdda0dSAndy Whitcroft	}
1365c2fdda0dSAndy Whitcroft
1366113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1367113f04a8SDaniel Walker		$sanitise_quote = '';
1368113f04a8SDaniel Walker	}
1369113f04a8SDaniel Walker
1370c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1371c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1372c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1373c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1374c2fdda0dSAndy Whitcroft
1375c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1376c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1377c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1378c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1379c2fdda0dSAndy Whitcroft	}
1380c2fdda0dSAndy Whitcroft
1381dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1382dadf680dSJoe Perches		my $match = $1;
1383dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1384dadf680dSJoe Perches	}
1385dadf680dSJoe Perches
138600df344fSAndy Whitcroft	return $res;
138700df344fSAndy Whitcroft}
138800df344fSAndy Whitcroft
1389a6962d72SJoe Perchessub get_quoted_string {
1390a6962d72SJoe Perches	my ($line, $rawline) = @_;
1391a6962d72SJoe Perches
1392478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
139333acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1394a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1395a6962d72SJoe Perches}
1396a6962d72SJoe Perches
13978905a67cSAndy Whitcroftsub ctx_statement_block {
13988905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13998905a67cSAndy Whitcroft	my $line = $linenr - 1;
14008905a67cSAndy Whitcroft	my $blk = '';
14018905a67cSAndy Whitcroft	my $soff = $off;
14028905a67cSAndy Whitcroft	my $coff = $off - 1;
1403773647a0SAndy Whitcroft	my $coff_set = 0;
14048905a67cSAndy Whitcroft
140513214adfSAndy Whitcroft	my $loff = 0;
140613214adfSAndy Whitcroft
14078905a67cSAndy Whitcroft	my $type = '';
14088905a67cSAndy Whitcroft	my $level = 0;
1409a2750645SAndy Whitcroft	my @stack = ();
1410cf655043SAndy Whitcroft	my $p;
14118905a67cSAndy Whitcroft	my $c;
14128905a67cSAndy Whitcroft	my $len = 0;
141313214adfSAndy Whitcroft
141413214adfSAndy Whitcroft	my $remainder;
14158905a67cSAndy Whitcroft	while (1) {
1416a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1417a2750645SAndy Whitcroft
1418773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
14198905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
14208905a67cSAndy Whitcroft		# context.
14218905a67cSAndy Whitcroft		if ($off >= $len) {
14228905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1423dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1424c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
14258905a67cSAndy Whitcroft				$remain--;
142613214adfSAndy Whitcroft				$loff = $len;
1427c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
14288905a67cSAndy Whitcroft				$len = length($blk);
14298905a67cSAndy Whitcroft				$line++;
14308905a67cSAndy Whitcroft				last;
14318905a67cSAndy Whitcroft			}
14328905a67cSAndy Whitcroft			# Bail if there is no further context.
14338905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
143413214adfSAndy Whitcroft			if ($off >= $len) {
14358905a67cSAndy Whitcroft				last;
14368905a67cSAndy Whitcroft			}
1437f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1438f74bd194SAndy Whitcroft				$level++;
1439f74bd194SAndy Whitcroft				$type = '#';
1440f74bd194SAndy Whitcroft			}
14418905a67cSAndy Whitcroft		}
1442cf655043SAndy Whitcroft		$p = $c;
14438905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
144413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
14458905a67cSAndy Whitcroft
1446773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
14474635f4fbSAndy Whitcroft
14484635f4fbSAndy Whitcroft		# Handle nested #if/#else.
14494635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
14504635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
14514635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
14524635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14534635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14544635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14554635f4fbSAndy Whitcroft		}
14564635f4fbSAndy Whitcroft
14578905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14588905a67cSAndy Whitcroft		# outermost level.
14598905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
14608905a67cSAndy Whitcroft			last;
14618905a67cSAndy Whitcroft		}
14628905a67cSAndy Whitcroft
146313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1464773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1465773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1466773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1467773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1468773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1469773647a0SAndy Whitcroft			$coff_set = 1;
1470773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1471773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
147213214adfSAndy Whitcroft		}
147313214adfSAndy Whitcroft
14748905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14758905a67cSAndy Whitcroft			$level++;
14768905a67cSAndy Whitcroft			$type = '(';
14778905a67cSAndy Whitcroft		}
14788905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14798905a67cSAndy Whitcroft			$level--;
14808905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14818905a67cSAndy Whitcroft
14828905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
14838905a67cSAndy Whitcroft				$coff = $off;
1484773647a0SAndy Whitcroft				$coff_set = 1;
1485773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
14868905a67cSAndy Whitcroft			}
14878905a67cSAndy Whitcroft		}
14888905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14898905a67cSAndy Whitcroft			$level++;
14908905a67cSAndy Whitcroft			$type = '{';
14918905a67cSAndy Whitcroft		}
14928905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14938905a67cSAndy Whitcroft			$level--;
14948905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14958905a67cSAndy Whitcroft
14968905a67cSAndy Whitcroft			if ($level == 0) {
1497b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1498b998e001SPatrick Pannuto					$off++;
1499b998e001SPatrick Pannuto				}
15008905a67cSAndy Whitcroft				last;
15018905a67cSAndy Whitcroft			}
15028905a67cSAndy Whitcroft		}
1503f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1504f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1505f74bd194SAndy Whitcroft			$level--;
1506f74bd194SAndy Whitcroft			$type = '';
1507f74bd194SAndy Whitcroft			$off++;
1508f74bd194SAndy Whitcroft			last;
1509f74bd194SAndy Whitcroft		}
15108905a67cSAndy Whitcroft		$off++;
15118905a67cSAndy Whitcroft	}
1512a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
151313214adfSAndy Whitcroft	if ($off == $len) {
1514a3bb97a7SAndy Whitcroft		$loff = $len + 1;
151513214adfSAndy Whitcroft		$line++;
151613214adfSAndy Whitcroft		$remain--;
151713214adfSAndy Whitcroft	}
15188905a67cSAndy Whitcroft
15198905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
15208905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
15218905a67cSAndy Whitcroft
15228905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
15238905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
15248905a67cSAndy Whitcroft
1525773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
152613214adfSAndy Whitcroft
152713214adfSAndy Whitcroft	return ($statement, $condition,
152813214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
152913214adfSAndy Whitcroft}
153013214adfSAndy Whitcroft
1531cf655043SAndy Whitcroftsub statement_lines {
1532cf655043SAndy Whitcroft	my ($stmt) = @_;
1533cf655043SAndy Whitcroft
1534cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1535cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1536cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1537cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1538cf655043SAndy Whitcroft
1539cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1540cf655043SAndy Whitcroft
1541cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1542cf655043SAndy Whitcroft}
1543cf655043SAndy Whitcroft
1544cf655043SAndy Whitcroftsub statement_rawlines {
1545cf655043SAndy Whitcroft	my ($stmt) = @_;
1546cf655043SAndy Whitcroft
1547cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1548cf655043SAndy Whitcroft
1549cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1550cf655043SAndy Whitcroft}
1551cf655043SAndy Whitcroft
1552cf655043SAndy Whitcroftsub statement_block_size {
1553cf655043SAndy Whitcroft	my ($stmt) = @_;
1554cf655043SAndy Whitcroft
1555cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1556cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1557cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1558cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1559cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1560cf655043SAndy Whitcroft
1561cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1562cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1563cf655043SAndy Whitcroft
1564cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1565cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1566cf655043SAndy Whitcroft
1567cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1568cf655043SAndy Whitcroft		return $stmt_lines;
1569cf655043SAndy Whitcroft	} else {
1570cf655043SAndy Whitcroft		return $stmt_statements;
1571cf655043SAndy Whitcroft	}
1572cf655043SAndy Whitcroft}
1573cf655043SAndy Whitcroft
157413214adfSAndy Whitcroftsub ctx_statement_full {
157513214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
157613214adfSAndy Whitcroft	my ($statement, $condition, $level);
157713214adfSAndy Whitcroft
157813214adfSAndy Whitcroft	my (@chunks);
157913214adfSAndy Whitcroft
1580cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
158113214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
158213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1583773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
158413214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1585cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1586cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1587cf655043SAndy Whitcroft	}
1588cf655043SAndy Whitcroft
1589cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1590cf655043SAndy Whitcroft	# could continue the statement.
1591cf655043SAndy Whitcroft	for (;;) {
159213214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
159313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1594cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1595773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1596cf655043SAndy Whitcroft		#print "C: push\n";
1597cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
159813214adfSAndy Whitcroft	}
159913214adfSAndy Whitcroft
160013214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
16018905a67cSAndy Whitcroft}
16028905a67cSAndy Whitcroft
16034a0df2efSAndy Whitcroftsub ctx_block_get {
1604f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
16054a0df2efSAndy Whitcroft	my $line;
16064a0df2efSAndy Whitcroft	my $start = $linenr - 1;
16074a0df2efSAndy Whitcroft	my $blk = '';
16084a0df2efSAndy Whitcroft	my @o;
16094a0df2efSAndy Whitcroft	my @c;
16104a0df2efSAndy Whitcroft	my @res = ();
16114a0df2efSAndy Whitcroft
1612f0a594c1SAndy Whitcroft	my $level = 0;
16134635f4fbSAndy Whitcroft	my @stack = ($level);
161400df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
161500df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
161600df344fSAndy Whitcroft		$remain--;
161700df344fSAndy Whitcroft
161800df344fSAndy Whitcroft		$blk .= $rawlines[$line];
16194635f4fbSAndy Whitcroft
16204635f4fbSAndy Whitcroft		# Handle nested #if/#else.
162101464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
16224635f4fbSAndy Whitcroft			push(@stack, $level);
162301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
16244635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
162501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
16264635f4fbSAndy Whitcroft			$level = pop(@stack);
16274635f4fbSAndy Whitcroft		}
16284635f4fbSAndy Whitcroft
162901464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1630f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1631f0a594c1SAndy Whitcroft			if ($off > 0) {
1632f0a594c1SAndy Whitcroft				$off--;
1633f0a594c1SAndy Whitcroft				next;
1634f0a594c1SAndy Whitcroft			}
16354a0df2efSAndy Whitcroft
1636f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1637f0a594c1SAndy Whitcroft				$level--;
1638f0a594c1SAndy Whitcroft				last if ($level == 0);
1639f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1640f0a594c1SAndy Whitcroft				$level++;
1641f0a594c1SAndy Whitcroft			}
1642f0a594c1SAndy Whitcroft		}
16434a0df2efSAndy Whitcroft
1644f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
164500df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
16464a0df2efSAndy Whitcroft		}
16474a0df2efSAndy Whitcroft
1648f0a594c1SAndy Whitcroft		last if ($level == 0);
16494a0df2efSAndy Whitcroft	}
16504a0df2efSAndy Whitcroft
1651f0a594c1SAndy Whitcroft	return ($level, @res);
16524a0df2efSAndy Whitcroft}
16534a0df2efSAndy Whitcroftsub ctx_block_outer {
16544a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16554a0df2efSAndy Whitcroft
1656f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1657f0a594c1SAndy Whitcroft	return @r;
16584a0df2efSAndy Whitcroft}
16594a0df2efSAndy Whitcroftsub ctx_block {
16604a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16614a0df2efSAndy Whitcroft
1662f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1663f0a594c1SAndy Whitcroft	return @r;
1664653d4876SAndy Whitcroft}
1665653d4876SAndy Whitcroftsub ctx_statement {
1666f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1667f0a594c1SAndy Whitcroft
1668f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1669f0a594c1SAndy Whitcroft	return @r;
1670f0a594c1SAndy Whitcroft}
1671f0a594c1SAndy Whitcroftsub ctx_block_level {
1672653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1673653d4876SAndy Whitcroft
1674f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16754a0df2efSAndy Whitcroft}
16769c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16779c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16789c0ca6f9SAndy Whitcroft
16799c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16809c0ca6f9SAndy Whitcroft}
16814a0df2efSAndy Whitcroft
16824a0df2efSAndy Whitcroftsub ctx_locate_comment {
16834a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16844a0df2efSAndy Whitcroft
1685a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1686a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1687a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1688a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1689a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1690a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1691a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1692a55ee0ccSJoe Perches
16934a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1694a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
16954a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16964a0df2efSAndy Whitcroft
16974a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16984a0df2efSAndy Whitcroft	# comment.
16994a0df2efSAndy Whitcroft	my $in_comment = 0;
17004a0df2efSAndy Whitcroft	$current_comment = '';
17014a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
170200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
170300df344fSAndy Whitcroft		#warn "           $line\n";
17044a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
17054a0df2efSAndy Whitcroft			$in_comment = 1;
17064a0df2efSAndy Whitcroft		}
17074a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
17084a0df2efSAndy Whitcroft			$in_comment = 1;
17094a0df2efSAndy Whitcroft		}
17104a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
17114a0df2efSAndy Whitcroft			$current_comment = '';
17124a0df2efSAndy Whitcroft		}
17134a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
17144a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
17154a0df2efSAndy Whitcroft			$in_comment = 0;
17164a0df2efSAndy Whitcroft		}
17174a0df2efSAndy Whitcroft	}
17184a0df2efSAndy Whitcroft
17194a0df2efSAndy Whitcroft	chomp($current_comment);
17204a0df2efSAndy Whitcroft	return($current_comment);
17214a0df2efSAndy Whitcroft}
17224a0df2efSAndy Whitcroftsub ctx_has_comment {
17234a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17244a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
17254a0df2efSAndy Whitcroft
172600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
17274a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
17284a0df2efSAndy Whitcroft
17294a0df2efSAndy Whitcroft	return ($cmt ne '');
17304a0df2efSAndy Whitcroft}
17314a0df2efSAndy Whitcroft
17324d001e4dSAndy Whitcroftsub raw_line {
17334d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
17344d001e4dSAndy Whitcroft
17354d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
17364d001e4dSAndy Whitcroft	$cnt++;
17374d001e4dSAndy Whitcroft
17384d001e4dSAndy Whitcroft	my $line;
17394d001e4dSAndy Whitcroft	while ($cnt) {
17404d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
17414d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
17424d001e4dSAndy Whitcroft		$cnt--;
17434d001e4dSAndy Whitcroft	}
17444d001e4dSAndy Whitcroft
17454d001e4dSAndy Whitcroft	return $line;
17464d001e4dSAndy Whitcroft}
17474d001e4dSAndy Whitcroft
17482a9f9d85STobin C. Hardingsub get_stat_real {
17492a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
17502a9f9d85STobin C. Harding
17512a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
17522a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
17532a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
17542a9f9d85STobin C. Harding	}
17552a9f9d85STobin C. Harding
17562a9f9d85STobin C. Harding	return $stat_real;
17572a9f9d85STobin C. Harding}
17582a9f9d85STobin C. Harding
1759e3d95a2aSTobin C. Hardingsub get_stat_here {
1760e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1761e3d95a2aSTobin C. Harding
1762e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1763e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1764e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1765e3d95a2aSTobin C. Harding	}
1766e3d95a2aSTobin C. Harding
1767e3d95a2aSTobin C. Harding	return $herectx;
1768e3d95a2aSTobin C. Harding}
1769e3d95a2aSTobin C. Harding
17700a920b5bSAndy Whitcroftsub cat_vet {
17710a920b5bSAndy Whitcroft	my ($vet) = @_;
17729c0ca6f9SAndy Whitcroft	my ($res, $coded);
17730a920b5bSAndy Whitcroft
17749c0ca6f9SAndy Whitcroft	$res = '';
17756c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
17766c72ffaaSAndy Whitcroft		$res .= $1;
17776c72ffaaSAndy Whitcroft		if ($2 ne '') {
17789c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
17796c72ffaaSAndy Whitcroft			$res .= $coded;
17806c72ffaaSAndy Whitcroft		}
17819c0ca6f9SAndy Whitcroft	}
17829c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
17830a920b5bSAndy Whitcroft
17849c0ca6f9SAndy Whitcroft	return $res;
17850a920b5bSAndy Whitcroft}
17860a920b5bSAndy Whitcroft
1787c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1788cf655043SAndy Whitcroftmy $av_pending;
1789c2fdda0dSAndy Whitcroftmy @av_paren_type;
17901f65f947SAndy Whitcroftmy $av_pend_colon;
1791c2fdda0dSAndy Whitcroft
1792c2fdda0dSAndy Whitcroftsub annotate_reset {
1793c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1794cf655043SAndy Whitcroft	$av_pending = '_';
1795cf655043SAndy Whitcroft	@av_paren_type = ('E');
17961f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1797c2fdda0dSAndy Whitcroft}
1798c2fdda0dSAndy Whitcroft
17996c72ffaaSAndy Whitcroftsub annotate_values {
18006c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
18016c72ffaaSAndy Whitcroft
18026c72ffaaSAndy Whitcroft	my $res;
18031f65f947SAndy Whitcroft	my $var = '_' x length($stream);
18046c72ffaaSAndy Whitcroft	my $cur = $stream;
18056c72ffaaSAndy Whitcroft
1806c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
18076c72ffaaSAndy Whitcroft
18086c72ffaaSAndy Whitcroft	while (length($cur)) {
1809773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1810cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1811171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
18126c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1813c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1814c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1815cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1816c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
18176c72ffaaSAndy Whitcroft			}
18186c72ffaaSAndy Whitcroft
1819c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
18209446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
18219446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1822addcdceaSAndy Whitcroft			$type = 'c';
18239446ef56SAndy Whitcroft
1824e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1825c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
18266c72ffaaSAndy Whitcroft			$type = 'T';
18276c72ffaaSAndy Whitcroft
1828389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1829389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1830389a2fe5SAndy Whitcroft			$type = 'T';
1831389a2fe5SAndy Whitcroft
1832c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1833171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1834c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1835171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1836171ae1a4SAndy Whitcroft			if ($2 ne '') {
1837cf655043SAndy Whitcroft				$av_pending = 'N';
1838171ae1a4SAndy Whitcroft			}
1839171ae1a4SAndy Whitcroft			$type = 'E';
1840171ae1a4SAndy Whitcroft
1841c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1842171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1843171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1844171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
18456c72ffaaSAndy Whitcroft
1846c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1847cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1848c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1849cf655043SAndy Whitcroft
1850cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1851cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1852171ae1a4SAndy Whitcroft			$type = 'E';
1853cf655043SAndy Whitcroft
1854c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1855cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1856cf655043SAndy Whitcroft			$av_preprocessor = 1;
1857cf655043SAndy Whitcroft
1858cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1859cf655043SAndy Whitcroft
1860171ae1a4SAndy Whitcroft			$type = 'E';
1861cf655043SAndy Whitcroft
1862c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1863cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1864cf655043SAndy Whitcroft
1865cf655043SAndy Whitcroft			$av_preprocessor = 1;
1866cf655043SAndy Whitcroft
1867cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1868cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1869cf655043SAndy Whitcroft			pop(@av_paren_type);
1870cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1871171ae1a4SAndy Whitcroft			$type = 'E';
18726c72ffaaSAndy Whitcroft
18736c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1874c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
18756c72ffaaSAndy Whitcroft
1876171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1877171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1878171ae1a4SAndy Whitcroft			$av_pending = $type;
1879171ae1a4SAndy Whitcroft			$type = 'N';
1880171ae1a4SAndy Whitcroft
18816c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1882c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
18836c72ffaaSAndy Whitcroft			if (defined $2) {
1884cf655043SAndy Whitcroft				$av_pending = 'V';
18856c72ffaaSAndy Whitcroft			}
18866c72ffaaSAndy Whitcroft			$type = 'N';
18876c72ffaaSAndy Whitcroft
188814b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1889c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
189014b111c1SAndy Whitcroft			$av_pending = 'E';
18916c72ffaaSAndy Whitcroft			$type = 'N';
18926c72ffaaSAndy Whitcroft
18931f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
18941f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
18951f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18961f65f947SAndy Whitcroft			$type = 'N';
18971f65f947SAndy Whitcroft
189814b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1899c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
19006c72ffaaSAndy Whitcroft			$type = 'N';
19016c72ffaaSAndy Whitcroft
19026c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1903c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1904cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1905cf655043SAndy Whitcroft			$av_pending = '_';
19066c72ffaaSAndy Whitcroft			$type = 'N';
19076c72ffaaSAndy Whitcroft
19086c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1909cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1910cf655043SAndy Whitcroft			if ($new_type ne '_') {
1911cf655043SAndy Whitcroft				$type = $new_type;
1912c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1913c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
19146c72ffaaSAndy Whitcroft			} else {
1915c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
19166c72ffaaSAndy Whitcroft			}
19176c72ffaaSAndy Whitcroft
1918c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1919c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1920c8cb2ca3SAndy Whitcroft			$type = 'V';
1921cf655043SAndy Whitcroft			$av_pending = 'V';
19226c72ffaaSAndy Whitcroft
19238e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
19248e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
19251f65f947SAndy Whitcroft				$av_pend_colon = 'B';
19268e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
19278e761b04SAndy Whitcroft				$av_pend_colon = 'L';
19281f65f947SAndy Whitcroft			}
19291f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
19301f65f947SAndy Whitcroft			$type = 'V';
19311f65f947SAndy Whitcroft
19326c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1933c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
19346c72ffaaSAndy Whitcroft			$type = 'V';
19356c72ffaaSAndy Whitcroft
19366c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1937c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
19386c72ffaaSAndy Whitcroft			$type = 'N';
19396c72ffaaSAndy Whitcroft
1940cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1941c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
194213214adfSAndy Whitcroft			$type = 'E';
19431f65f947SAndy Whitcroft			$av_pend_colon = 'O';
194413214adfSAndy Whitcroft
19458e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
19468e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
19478e761b04SAndy Whitcroft			$type = 'C';
19488e761b04SAndy Whitcroft
19491f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
19501f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
19511f65f947SAndy Whitcroft			$type = 'N';
19521f65f947SAndy Whitcroft
19531f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
19541f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
19551f65f947SAndy Whitcroft
19561f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
19571f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
19581f65f947SAndy Whitcroft				$type = 'E';
19591f65f947SAndy Whitcroft			} else {
19601f65f947SAndy Whitcroft				$type = 'N';
19611f65f947SAndy Whitcroft			}
19621f65f947SAndy Whitcroft			$av_pend_colon = 'O';
19631f65f947SAndy Whitcroft
19648e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
196513214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
19666c72ffaaSAndy Whitcroft			$type = 'N';
19676c72ffaaSAndy Whitcroft
19680d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
196974048ed8SAndy Whitcroft			my $variant;
197074048ed8SAndy Whitcroft
197174048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
197274048ed8SAndy Whitcroft			if ($type eq 'V') {
197374048ed8SAndy Whitcroft				$variant = 'B';
197474048ed8SAndy Whitcroft			} else {
197574048ed8SAndy Whitcroft				$variant = 'U';
197674048ed8SAndy Whitcroft			}
197774048ed8SAndy Whitcroft
197874048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
197974048ed8SAndy Whitcroft			$type = 'N';
198074048ed8SAndy Whitcroft
19816c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1982c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
19836c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
19846c72ffaaSAndy Whitcroft				$type = 'N';
19856c72ffaaSAndy Whitcroft			}
19866c72ffaaSAndy Whitcroft
19876c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1988c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
19896c72ffaaSAndy Whitcroft		}
19906c72ffaaSAndy Whitcroft		if (defined $1) {
19916c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
19926c72ffaaSAndy Whitcroft			$res .= $type x length($1);
19936c72ffaaSAndy Whitcroft		}
19946c72ffaaSAndy Whitcroft	}
19956c72ffaaSAndy Whitcroft
19961f65f947SAndy Whitcroft	return ($res, $var);
19976c72ffaaSAndy Whitcroft}
19986c72ffaaSAndy Whitcroft
19998905a67cSAndy Whitcroftsub possible {
200013214adfSAndy Whitcroft	my ($possible, $line) = @_;
20019a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
20020776e594SAndy Whitcroft		^(?:
20030776e594SAndy Whitcroft			$Modifier|
20040776e594SAndy Whitcroft			$Storage|
20050776e594SAndy Whitcroft			$Type|
20069a974fdbSAndy Whitcroft			DEFINE_\S+
20079a974fdbSAndy Whitcroft		)$|
20089a974fdbSAndy Whitcroft		^(?:
20090776e594SAndy Whitcroft			goto|
20100776e594SAndy Whitcroft			return|
20110776e594SAndy Whitcroft			case|
20120776e594SAndy Whitcroft			else|
20130776e594SAndy Whitcroft			asm|__asm__|
201489a88353SAndy Whitcroft			do|
201589a88353SAndy Whitcroft			\#|
201689a88353SAndy Whitcroft			\#\#|
20179a974fdbSAndy Whitcroft		)(?:\s|$)|
20180776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
20199a974fdbSAndy Whitcroft	    )}x;
20209a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
20219a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2022c45dcabdSAndy Whitcroft		# Check for modifiers.
2023c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2024c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2025c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2026c45dcabdSAndy Whitcroft
2027c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2028c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2029d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
20309a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2031d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2032485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2033d2506586SAndy Whitcroft				}
20349a974fdbSAndy Whitcroft			}
2035c45dcabdSAndy Whitcroft
2036c45dcabdSAndy Whitcroft		} else {
203713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2038485ff23eSAlex Dowad			push(@typeListFile, $possible);
2039c45dcabdSAndy Whitcroft		}
20408905a67cSAndy Whitcroft		build_types();
20410776e594SAndy Whitcroft	} else {
20420776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
20438905a67cSAndy Whitcroft	}
20448905a67cSAndy Whitcroft}
20458905a67cSAndy Whitcroft
20466c72ffaaSAndy Whitcroftmy $prefix = '';
20476c72ffaaSAndy Whitcroft
2048000d1cc1SJoe Perchessub show_type {
2049cbec18afSJoe Perches	my ($type) = @_;
205091bfe484SJoe Perches
2051522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2052522b837cSAlexey Dobriyan
2053cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2054cbec18afSJoe Perches
2055cbec18afSJoe Perches	return !defined $ignore_type{$type};
2056000d1cc1SJoe Perches}
2057000d1cc1SJoe Perches
2058f0a594c1SAndy Whitcroftsub report {
2059cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2060cbec18afSJoe Perches
2061cbec18afSJoe Perches	if (!show_type($type) ||
2062cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2063773647a0SAndy Whitcroft		return 0;
2064773647a0SAndy Whitcroft	}
206557230297SJoe Perches	my $output = '';
2066737c0767SJohn Brooks	if ($color) {
206757230297SJoe Perches		if ($level eq 'ERROR') {
206857230297SJoe Perches			$output .= RED;
206957230297SJoe Perches		} elsif ($level eq 'WARNING') {
207057230297SJoe Perches			$output .= YELLOW;
2071000d1cc1SJoe Perches		} else {
207257230297SJoe Perches			$output .= GREEN;
2073000d1cc1SJoe Perches		}
207457230297SJoe Perches	}
207557230297SJoe Perches	$output .= $prefix . $level . ':';
207657230297SJoe Perches	if ($show_types) {
2077737c0767SJohn Brooks		$output .= BLUE if ($color);
207857230297SJoe Perches		$output .= "$type:";
207957230297SJoe Perches	}
2080737c0767SJohn Brooks	$output .= RESET if ($color);
208157230297SJoe Perches	$output .= ' ' . $msg . "\n";
208234d8815fSJoe Perches
208334d8815fSJoe Perches	if ($showfile) {
208434d8815fSJoe Perches		my @lines = split("\n", $output, -1);
208534d8815fSJoe Perches		splice(@lines, 1, 1);
208634d8815fSJoe Perches		$output = join("\n", @lines);
208734d8815fSJoe Perches	}
208857230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
20898905a67cSAndy Whitcroft
209057230297SJoe Perches	push(our @report, $output);
2091773647a0SAndy Whitcroft
2092773647a0SAndy Whitcroft	return 1;
2093f0a594c1SAndy Whitcroft}
2094cbec18afSJoe Perches
2095f0a594c1SAndy Whitcroftsub report_dump {
209613214adfSAndy Whitcroft	our @report;
2097f0a594c1SAndy Whitcroft}
2098000d1cc1SJoe Perches
2099d752fcc8SJoe Perchessub fixup_current_range {
2100d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2101d752fcc8SJoe Perches
2102d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2103d752fcc8SJoe Perches		my $o = $1;
2104d752fcc8SJoe Perches		my $l = $2;
2105d752fcc8SJoe Perches		my $no = $o + $offset;
2106d752fcc8SJoe Perches		my $nl = $l + $length;
2107d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2108d752fcc8SJoe Perches	}
2109d752fcc8SJoe Perches}
2110d752fcc8SJoe Perches
2111d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2112d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2113d752fcc8SJoe Perches
2114d752fcc8SJoe Perches	my $range_last_linenr = 0;
2115d752fcc8SJoe Perches	my $delta_offset = 0;
2116d752fcc8SJoe Perches
2117d752fcc8SJoe Perches	my $old_linenr = 0;
2118d752fcc8SJoe Perches	my $new_linenr = 0;
2119d752fcc8SJoe Perches
2120d752fcc8SJoe Perches	my $next_insert = 0;
2121d752fcc8SJoe Perches	my $next_delete = 0;
2122d752fcc8SJoe Perches
2123d752fcc8SJoe Perches	my @lines = ();
2124d752fcc8SJoe Perches
2125d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2126d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2127d752fcc8SJoe Perches
2128d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2129d752fcc8SJoe Perches		my $save_line = 1;
2130d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2131323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2132d752fcc8SJoe Perches			$delta_offset = 0;
2133d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2134d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2135d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2136d752fcc8SJoe Perches		}
2137d752fcc8SJoe Perches
2138d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2139d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2140d752fcc8SJoe Perches			$save_line = 0;
2141d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2142d752fcc8SJoe Perches		}
2143d752fcc8SJoe Perches
2144d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2145d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2146d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2147d752fcc8SJoe Perches			$new_linenr++;
2148d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2149d752fcc8SJoe Perches		}
2150d752fcc8SJoe Perches
2151d752fcc8SJoe Perches		if ($save_line) {
2152d752fcc8SJoe Perches			push(@lines, $line);
2153d752fcc8SJoe Perches			$new_linenr++;
2154d752fcc8SJoe Perches		}
2155d752fcc8SJoe Perches
2156d752fcc8SJoe Perches		$old_linenr++;
2157d752fcc8SJoe Perches	}
2158d752fcc8SJoe Perches
2159d752fcc8SJoe Perches	return @lines;
2160d752fcc8SJoe Perches}
2161d752fcc8SJoe Perches
2162f2d7e4d4SJoe Perchessub fix_insert_line {
2163f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2164f2d7e4d4SJoe Perches
2165f2d7e4d4SJoe Perches	my $inserted = {
2166f2d7e4d4SJoe Perches		LINENR => $linenr,
2167f2d7e4d4SJoe Perches		LINE => $line,
2168f2d7e4d4SJoe Perches	};
2169f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2170f2d7e4d4SJoe Perches}
2171f2d7e4d4SJoe Perches
2172f2d7e4d4SJoe Perchessub fix_delete_line {
2173f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2174f2d7e4d4SJoe Perches
2175f2d7e4d4SJoe Perches	my $deleted = {
2176f2d7e4d4SJoe Perches		LINENR => $linenr,
2177f2d7e4d4SJoe Perches		LINE => $line,
2178f2d7e4d4SJoe Perches	};
2179f2d7e4d4SJoe Perches
2180f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2181f2d7e4d4SJoe Perches}
2182f2d7e4d4SJoe Perches
2183de7d4f0eSAndy Whitcroftsub ERROR {
2184cbec18afSJoe Perches	my ($type, $msg) = @_;
2185cbec18afSJoe Perches
2186cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2187de7d4f0eSAndy Whitcroft		our $clean = 0;
21886c72ffaaSAndy Whitcroft		our $cnt_error++;
21893705ce5bSJoe Perches		return 1;
2190de7d4f0eSAndy Whitcroft	}
21913705ce5bSJoe Perches	return 0;
2192773647a0SAndy Whitcroft}
2193de7d4f0eSAndy Whitcroftsub WARN {
2194cbec18afSJoe Perches	my ($type, $msg) = @_;
2195cbec18afSJoe Perches
2196cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2197de7d4f0eSAndy Whitcroft		our $clean = 0;
21986c72ffaaSAndy Whitcroft		our $cnt_warn++;
21993705ce5bSJoe Perches		return 1;
2200de7d4f0eSAndy Whitcroft	}
22013705ce5bSJoe Perches	return 0;
2202773647a0SAndy Whitcroft}
2203de7d4f0eSAndy Whitcroftsub CHK {
2204cbec18afSJoe Perches	my ($type, $msg) = @_;
2205cbec18afSJoe Perches
2206cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2207de7d4f0eSAndy Whitcroft		our $clean = 0;
22086c72ffaaSAndy Whitcroft		our $cnt_chk++;
22093705ce5bSJoe Perches		return 1;
22106c72ffaaSAndy Whitcroft	}
22113705ce5bSJoe Perches	return 0;
2212de7d4f0eSAndy Whitcroft}
2213de7d4f0eSAndy Whitcroft
22146ecd9674SAndy Whitcroftsub check_absolute_file {
22156ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
22166ecd9674SAndy Whitcroft	my $file = $absolute;
22176ecd9674SAndy Whitcroft
22186ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
22196ecd9674SAndy Whitcroft
22206ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
22216ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
22226ecd9674SAndy Whitcroft		if (-f "$root/$file") {
22236ecd9674SAndy Whitcroft			##print "file<$file>\n";
22246ecd9674SAndy Whitcroft			last;
22256ecd9674SAndy Whitcroft		}
22266ecd9674SAndy Whitcroft	}
22276ecd9674SAndy Whitcroft	if (! -f _)  {
22286ecd9674SAndy Whitcroft		return 0;
22296ecd9674SAndy Whitcroft	}
22306ecd9674SAndy Whitcroft
22316ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
22326ecd9674SAndy Whitcroft	my $prefix = $absolute;
22336ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
22346ecd9674SAndy Whitcroft
22356ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
22366ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2237000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2238000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
22396ecd9674SAndy Whitcroft	}
22406ecd9674SAndy Whitcroft}
22416ecd9674SAndy Whitcroft
22423705ce5bSJoe Perchessub trim {
22433705ce5bSJoe Perches	my ($string) = @_;
22443705ce5bSJoe Perches
2245b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2246b34c648bSJoe Perches
2247b34c648bSJoe Perches	return $string;
2248b34c648bSJoe Perches}
2249b34c648bSJoe Perches
2250b34c648bSJoe Perchessub ltrim {
2251b34c648bSJoe Perches	my ($string) = @_;
2252b34c648bSJoe Perches
2253b34c648bSJoe Perches	$string =~ s/^\s+//;
2254b34c648bSJoe Perches
2255b34c648bSJoe Perches	return $string;
2256b34c648bSJoe Perches}
2257b34c648bSJoe Perches
2258b34c648bSJoe Perchessub rtrim {
2259b34c648bSJoe Perches	my ($string) = @_;
2260b34c648bSJoe Perches
2261b34c648bSJoe Perches	$string =~ s/\s+$//;
22623705ce5bSJoe Perches
22633705ce5bSJoe Perches	return $string;
22643705ce5bSJoe Perches}
22653705ce5bSJoe Perches
226652ea8506SJoe Perchessub string_find_replace {
226752ea8506SJoe Perches	my ($string, $find, $replace) = @_;
226852ea8506SJoe Perches
226952ea8506SJoe Perches	$string =~ s/$find/$replace/g;
227052ea8506SJoe Perches
227152ea8506SJoe Perches	return $string;
227252ea8506SJoe Perches}
227352ea8506SJoe Perches
22743705ce5bSJoe Perchessub tabify {
22753705ce5bSJoe Perches	my ($leading) = @_;
22763705ce5bSJoe Perches
2277713a09deSAntonio Borneo	my $source_indent = $tabsize;
22783705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
22793705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
22803705ce5bSJoe Perches
22813705ce5bSJoe Perches	#convert leading spaces to tabs
22823705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
22833705ce5bSJoe Perches	#Remove spaces before a tab
22843705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
22853705ce5bSJoe Perches
22863705ce5bSJoe Perches	return "$leading";
22873705ce5bSJoe Perches}
22883705ce5bSJoe Perches
2289d1fe9c09SJoe Perchessub pos_last_openparen {
2290d1fe9c09SJoe Perches	my ($line) = @_;
2291d1fe9c09SJoe Perches
2292d1fe9c09SJoe Perches	my $pos = 0;
2293d1fe9c09SJoe Perches
2294d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2295d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2296d1fe9c09SJoe Perches
2297d1fe9c09SJoe Perches	my $last_openparen = 0;
2298d1fe9c09SJoe Perches
2299d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2300d1fe9c09SJoe Perches		return -1;
2301d1fe9c09SJoe Perches	}
2302d1fe9c09SJoe Perches
2303d1fe9c09SJoe Perches	my $len = length($line);
2304d1fe9c09SJoe Perches
2305d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2306d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2307d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2308d1fe9c09SJoe Perches			$pos += length($1) - 1;
2309d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2310d1fe9c09SJoe Perches			$last_openparen = $pos;
2311d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2312d1fe9c09SJoe Perches			last;
2313d1fe9c09SJoe Perches		}
2314d1fe9c09SJoe Perches	}
2315d1fe9c09SJoe Perches
231691cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2317d1fe9c09SJoe Perches}
2318d1fe9c09SJoe Perches
2319f36d3eb8SJoe Perchessub get_raw_comment {
2320f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2321f36d3eb8SJoe Perches	my $comment = '';
2322f36d3eb8SJoe Perches
2323f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2324f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2325f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2326f36d3eb8SJoe Perches		}
2327f36d3eb8SJoe Perches	}
2328f36d3eb8SJoe Perches
2329f36d3eb8SJoe Perches	return $comment;
2330f36d3eb8SJoe Perches}
2331f36d3eb8SJoe Perches
23320a920b5bSAndy Whitcroftsub process {
23330a920b5bSAndy Whitcroft	my $filename = shift;
23340a920b5bSAndy Whitcroft
23350a920b5bSAndy Whitcroft	my $linenr=0;
23360a920b5bSAndy Whitcroft	my $prevline="";
2337c2fdda0dSAndy Whitcroft	my $prevrawline="";
23380a920b5bSAndy Whitcroft	my $stashline="";
2339c2fdda0dSAndy Whitcroft	my $stashrawline="";
23400a920b5bSAndy Whitcroft
23414a0df2efSAndy Whitcroft	my $length;
23420a920b5bSAndy Whitcroft	my $indent;
23430a920b5bSAndy Whitcroft	my $previndent=0;
23440a920b5bSAndy Whitcroft	my $stashindent=0;
23450a920b5bSAndy Whitcroft
2346de7d4f0eSAndy Whitcroft	our $clean = 1;
23470a920b5bSAndy Whitcroft	my $signoff = 0;
2348cd261496SGeert Uytterhoeven	my $author = '';
2349cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
23500a920b5bSAndy Whitcroft	my $is_patch = 0;
2351133712a2SRob Herring	my $is_binding_patch = -1;
235229ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
235315662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
235444d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2355ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2356490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2357bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
23582a076f40SJoe Perches	my $commit_log_long_line = 0;
2359e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
236013f1937eSJoe Perches	my $reported_maintainer_file = 0;
2361fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2362fa64205dSPasi Savanainen
2363365dd4eaSJoe Perches	my $last_blank_line = 0;
23645e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2365365dd4eaSJoe Perches
236613214adfSAndy Whitcroft	our @report = ();
23676c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
23686c72ffaaSAndy Whitcroft	our $cnt_error = 0;
23696c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
23706c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
23716c72ffaaSAndy Whitcroft
23720a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
23730a920b5bSAndy Whitcroft	my $realfile = '';
23740a920b5bSAndy Whitcroft	my $realline = 0;
23750a920b5bSAndy Whitcroft	my $realcnt = 0;
23760a920b5bSAndy Whitcroft	my $here = '';
237777cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23780a920b5bSAndy Whitcroft	my $in_comment = 0;
2379c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23800a920b5bSAndy Whitcroft	my $first_line = 0;
23811e855726SWolfram Sang	my $p1_prefix = '';
23820a920b5bSAndy Whitcroft
238313214adfSAndy Whitcroft	my $prev_values = 'E';
238413214adfSAndy Whitcroft
238513214adfSAndy Whitcroft	# suppression flags
2386773647a0SAndy Whitcroft	my %suppress_ifbraces;
2387170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
23882b474a1aSAndy Whitcroft	my %suppress_export;
23893e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2390653d4876SAndy Whitcroft
23917e51f197SJoe Perches	my %signatures = ();
2392323c1260SJoe Perches
2393c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2394de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2395c2fdda0dSAndy Whitcroft	#
2396de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2397de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2398773647a0SAndy Whitcroft
2399d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2400d8b07710SJoe Perches
24019f3a8992SRob Herring	my $checklicenseline = 1;
24029f3a8992SRob Herring
2403773647a0SAndy Whitcroft	sanitise_line_reset();
2404c2fdda0dSAndy Whitcroft	my $line;
2405c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2406773647a0SAndy Whitcroft		$linenr++;
2407773647a0SAndy Whitcroft		$line = $rawline;
2408c2fdda0dSAndy Whitcroft
24093705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
24103705ce5bSJoe Perches
2411773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2412de7d4f0eSAndy Whitcroft			$setup_docs = 0;
24132581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2414de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2415de7d4f0eSAndy Whitcroft			}
2416773647a0SAndy Whitcroft			#next;
2417de7d4f0eSAndy Whitcroft		}
241874fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2419773647a0SAndy Whitcroft			$realline=$1-1;
2420773647a0SAndy Whitcroft			if (defined $2) {
2421773647a0SAndy Whitcroft				$realcnt=$3+1;
2422773647a0SAndy Whitcroft			} else {
2423773647a0SAndy Whitcroft				$realcnt=1+1;
2424773647a0SAndy Whitcroft			}
2425c45dcabdSAndy Whitcroft			$in_comment = 0;
2426773647a0SAndy Whitcroft
2427773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2428773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2429773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2430773647a0SAndy Whitcroft			# at context start.
2431773647a0SAndy Whitcroft			my $edge;
243201fa9147SAndy Whitcroft			my $cnt = $realcnt;
243301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
243401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
243501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
243601fa9147SAndy Whitcroft				$cnt--;
243701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2438721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2439fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2440fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2441fae17daeSAndy Whitcroft					($edge) = $1;
2442fae17daeSAndy Whitcroft					last;
2443fae17daeSAndy Whitcroft				}
2444773647a0SAndy Whitcroft			}
2445773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2446773647a0SAndy Whitcroft				$in_comment = 1;
2447773647a0SAndy Whitcroft			}
2448773647a0SAndy Whitcroft
2449773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2450773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2451773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2452773647a0SAndy Whitcroft			if (!defined $edge &&
245383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2454773647a0SAndy Whitcroft			{
2455773647a0SAndy Whitcroft				$in_comment = 1;
2456773647a0SAndy Whitcroft			}
2457773647a0SAndy Whitcroft
2458773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2459773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2460773647a0SAndy Whitcroft
2461171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2462773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2463171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2464773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2465773647a0SAndy Whitcroft		}
2466773647a0SAndy Whitcroft		push(@lines, $line);
2467773647a0SAndy Whitcroft
2468773647a0SAndy Whitcroft		if ($realcnt > 1) {
2469773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2470773647a0SAndy Whitcroft		} else {
2471773647a0SAndy Whitcroft			$realcnt = 0;
2472773647a0SAndy Whitcroft		}
2473773647a0SAndy Whitcroft
2474773647a0SAndy Whitcroft		#print "==>$rawline\n";
2475773647a0SAndy Whitcroft		#print "-->$line\n";
2476de7d4f0eSAndy Whitcroft
2477de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2478de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2479de7d4f0eSAndy Whitcroft		}
2480de7d4f0eSAndy Whitcroft	}
2481de7d4f0eSAndy Whitcroft
24826c72ffaaSAndy Whitcroft	$prefix = '';
24836c72ffaaSAndy Whitcroft
2484773647a0SAndy Whitcroft	$realcnt = 0;
2485773647a0SAndy Whitcroft	$linenr = 0;
2486194f66fcSJoe Perches	$fixlinenr = -1;
24870a920b5bSAndy Whitcroft	foreach my $line (@lines) {
24880a920b5bSAndy Whitcroft		$linenr++;
2489194f66fcSJoe Perches		$fixlinenr++;
24901b5539b1SJoe Perches		my $sline = $line;	#copy of $line
24911b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
24920a920b5bSAndy Whitcroft
2493c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2494f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
24956c72ffaaSAndy Whitcroft
249612c253abSJoe Perches# check if it's a mode change, rename or start of a patch
249712c253abSJoe Perches		if (!$in_commit_log &&
249812c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
249912c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
250012c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
250112c253abSJoe Perches			$is_patch = 1;
250212c253abSJoe Perches		}
250312c253abSJoe Perches
25040a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2505e518e9a5SJoe Perches		if (!$in_commit_log &&
250674fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
250774fd4f34SJoe Perches			my $context = $4;
25080a920b5bSAndy Whitcroft			$is_patch = 1;
25094a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
25100a920b5bSAndy Whitcroft			$realline=$1-1;
25110a920b5bSAndy Whitcroft			if (defined $2) {
25120a920b5bSAndy Whitcroft				$realcnt=$3+1;
25130a920b5bSAndy Whitcroft			} else {
25140a920b5bSAndy Whitcroft				$realcnt=1+1;
25150a920b5bSAndy Whitcroft			}
2516c2fdda0dSAndy Whitcroft			annotate_reset();
251713214adfSAndy Whitcroft			$prev_values = 'E';
251813214adfSAndy Whitcroft
2519773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2520170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
25212b474a1aSAndy Whitcroft			%suppress_export = ();
25223e469cdcSAndy Whitcroft			$suppress_statement = 0;
252374fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
252474fd4f34SJoe Perches				$context_function = $1;
252574fd4f34SJoe Perches			} else {
252674fd4f34SJoe Perches				undef $context_function;
252774fd4f34SJoe Perches			}
25280a920b5bSAndy Whitcroft			next;
25290a920b5bSAndy Whitcroft
25304a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
25314a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
25324a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2533773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
25340a920b5bSAndy Whitcroft			$realline++;
2535d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
25360a920b5bSAndy Whitcroft
25374a0df2efSAndy Whitcroft			# Measure the line length and indent.
2538c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
25390a920b5bSAndy Whitcroft
25400a920b5bSAndy Whitcroft			# Track the previous line.
25410a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
25420a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2543c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2544c2fdda0dSAndy Whitcroft
2545773647a0SAndy Whitcroft			#warn "line<$line>\n";
25466c72ffaaSAndy Whitcroft
2547d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2548d8aaf121SAndy Whitcroft			$realcnt--;
25490a920b5bSAndy Whitcroft		}
25500a920b5bSAndy Whitcroft
2551cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2552cc77cdcaSAndy Whitcroft
25536c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
25546c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2555773647a0SAndy Whitcroft
25562ac73b4fSJoe Perches		my $found_file = 0;
2557773647a0SAndy Whitcroft		# extract the filename as it passes
25583bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
25593bf9a009SRabin Vincent			$realfile = $1;
25602b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2561270c49a0SJoe Perches			$in_commit_log = 0;
25622ac73b4fSJoe Perches			$found_file = 1;
25633bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2564773647a0SAndy Whitcroft			$realfile = $1;
25652b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2566270c49a0SJoe Perches			$in_commit_log = 0;
25671e855726SWolfram Sang
25681e855726SWolfram Sang			$p1_prefix = $1;
2569e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2570e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2571000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2572000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
25731e855726SWolfram Sang			}
2574773647a0SAndy Whitcroft
2575c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2576000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2577000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2578773647a0SAndy Whitcroft			}
25792ac73b4fSJoe Perches			$found_file = 1;
25802ac73b4fSJoe Perches		}
25812ac73b4fSJoe Perches
258234d8815fSJoe Perches#make up the handle for any error we report on this line
258334d8815fSJoe Perches		if ($showfile) {
258434d8815fSJoe Perches			$prefix = "$realfile:$realline: "
258534d8815fSJoe Perches		} elsif ($emacs) {
25867d3a9f67SJoe Perches			if ($file) {
25877d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
25887d3a9f67SJoe Perches			} else {
258934d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
259034d8815fSJoe Perches			}
25917d3a9f67SJoe Perches		}
259234d8815fSJoe Perches
25932ac73b4fSJoe Perches		if ($found_file) {
259485b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
259585b0ee18SJoe Perches				WARN("OBSOLETE",
259685b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
259785b0ee18SJoe Perches			}
25987bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25992ac73b4fSJoe Perches				$check = 1;
26002ac73b4fSJoe Perches			} else {
26012ac73b4fSJoe Perches				$check = $check_orig;
26022ac73b4fSJoe Perches			}
26039f3a8992SRob Herring			$checklicenseline = 1;
2604133712a2SRob Herring
2605133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2606133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2607133712a2SRob Herring
2608133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2609133712a2SRob Herring
2610133712a2SRob Herring				if (($last_binding_patch != -1) &&
2611133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2612133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2613858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2614133712a2SRob Herring				}
2615133712a2SRob Herring			}
2616133712a2SRob Herring
2617773647a0SAndy Whitcroft			next;
2618773647a0SAndy Whitcroft		}
2619773647a0SAndy Whitcroft
2620389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
26210a920b5bSAndy Whitcroft
2622c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2623c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2624c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
26250a920b5bSAndy Whitcroft
26266c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
26276c72ffaaSAndy Whitcroft
2628490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2629490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2630490b292cSJoe Perches		if ($in_commit_log) {
2631490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2632490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2633490b292cSJoe Perches			}
2634490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2635490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2636490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2637490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2638490b292cSJoe Perches		}
2639490b292cSJoe Perches
2640e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2641e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2642e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2643e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2644e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2645e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2646e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2647e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2648e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2649e518e9a5SJoe Perches		}
2650e518e9a5SJoe Perches
26513bf9a009SRabin Vincent# Check for incorrect file permissions
26523bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
26533bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
265404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
265504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2656000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2657000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
26583bf9a009SRabin Vincent			}
26593bf9a009SRabin Vincent		}
26603bf9a009SRabin Vincent
2661cd261496SGeert Uytterhoeven# Check the patch for a From:
2662cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2663cd261496SGeert Uytterhoeven			$author = $1;
2664cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2665cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2666dfa05c28SJoe Perches			$author = reformat_email($author);
2667cd261496SGeert Uytterhoeven		}
2668cd261496SGeert Uytterhoeven
266920112475SJoe Perches# Check the patch for a signoff:
2670dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
26714a0df2efSAndy Whitcroft			$signoff++;
267215662b3eSJoe Perches			$in_commit_log = 0;
2673cd261496SGeert Uytterhoeven			if ($author ne '') {
2674dfa05c28SJoe Perches				if (same_email_addresses($1, $author)) {
2675cd261496SGeert Uytterhoeven					$authorsignoff = 1;
2676cd261496SGeert Uytterhoeven				}
2677cd261496SGeert Uytterhoeven			}
26780a920b5bSAndy Whitcroft		}
267920112475SJoe Perches
268044d303ebSJoe Perches# Check for patch separator
268144d303ebSJoe Perches		if ($line =~ /^---$/) {
268244d303ebSJoe Perches			$has_patch_separator = 1;
268344d303ebSJoe Perches			$in_commit_log = 0;
268444d303ebSJoe Perches		}
268544d303ebSJoe Perches
2686e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2687e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2688e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2689e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2690e0d975b1SJoe Perches		}
2691e0d975b1SJoe Perches
269220112475SJoe Perches# Check signature styles
2693270c49a0SJoe Perches		if (!$in_header_lines &&
2694ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
269520112475SJoe Perches			my $space_before = $1;
269620112475SJoe Perches			my $sign_off = $2;
269720112475SJoe Perches			my $space_after = $3;
269820112475SJoe Perches			my $email = $4;
269920112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
270020112475SJoe Perches
2701ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2702ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2703ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2704ce0338dfSJoe Perches			}
270520112475SJoe Perches			if (defined $space_before && $space_before ne "") {
27063705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27073705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
27083705ce5bSJoe Perches				    $fix) {
2709194f66fcSJoe Perches					$fixed[$fixlinenr] =
27103705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27113705ce5bSJoe Perches				}
271220112475SJoe Perches			}
271320112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
27143705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27153705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
27163705ce5bSJoe Perches				    $fix) {
2717194f66fcSJoe Perches					$fixed[$fixlinenr] =
27183705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27193705ce5bSJoe Perches				}
27203705ce5bSJoe Perches
272120112475SJoe Perches			}
272220112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
27233705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27243705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
27253705ce5bSJoe Perches				    $fix) {
2726194f66fcSJoe Perches					$fixed[$fixlinenr] =
27273705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27283705ce5bSJoe Perches				}
272920112475SJoe Perches			}
273020112475SJoe Perches
2731dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
273220112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
273320112475SJoe Perches			if ($suggested_email eq "") {
2734000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2735000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
273620112475SJoe Perches			} else {
273720112475SJoe Perches				my $dequoted = $suggested_email;
273820112475SJoe Perches				$dequoted =~ s/^"//;
273920112475SJoe Perches				$dequoted =~ s/" </ </;
274020112475SJoe Perches				# Don't force email to have quotes
274120112475SJoe Perches				# Allow just an angle bracketed address
2742dfa05c28SJoe Perches				if (!same_email_addresses($email, $suggested_email)) {
2743000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2744000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
274520112475SJoe Perches				}
27460a920b5bSAndy Whitcroft			}
27477e51f197SJoe Perches
27487e51f197SJoe Perches# Check for duplicate signatures
27497e51f197SJoe Perches			my $sig_nospace = $line;
27507e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
27517e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
27527e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
27537e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
27547e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
27557e51f197SJoe Perches			} else {
27567e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
27577e51f197SJoe Perches			}
27586c5d24eeSSean Christopherson
27596c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
27606c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
27616c5d24eeSSean Christopherson				if ($email eq $author) {
27626c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27636c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
27646c5d24eeSSean Christopherson				}
27656c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
27666c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27676c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
27686c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
27696c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27706c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27716c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
27726c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27736c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27746c5d24eeSSean Christopherson				}
27756c5d24eeSSean Christopherson			}
27760a920b5bSAndy Whitcroft		}
27770a920b5bSAndy Whitcroft
2778a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2779a2fe16b9SJoe Perches		if ($in_header_lines &&
2780a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2781a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2782a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2783a2fe16b9SJoe Perches		}
2784a2fe16b9SJoe Perches
278544d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
278644d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
27877ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
278844d303ebSJoe Perches			      "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
27897ebd05efSChristopher Covington		}
27907ebd05efSChristopher Covington
2791369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2792369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2793369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2794369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2795369c8dd3SJoe Perches					# timestamp
2796634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2797634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2798634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2799634cffccSJoe Perches					# stack dump address styles
2800369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2801369c8dd3SJoe Perches		}
2802369c8dd3SJoe Perches
28032a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
28042a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2805bf4daf12SJoe Perches		    length($line) > 75 &&
2806bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2807bf4daf12SJoe Perches					# file delta changes
2808bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2809bf4daf12SJoe Perches					# filename then :
2810bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2811bf4daf12SJoe Perches					# A Fixes: or Link: line
2812bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
28132a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
28142a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
28152a076f40SJoe Perches			$commit_log_long_line = 1;
28162a076f40SJoe Perches		}
28172a076f40SJoe Perches
2818bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2819bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2820bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2821bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2822bf4daf12SJoe Perches		}
2823bf4daf12SJoe Perches
28240d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2825369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2826a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
2827e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2828fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2829aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2830369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2831bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2832fe043ea1SJoe Perches			my $init_char = "c";
2833fe043ea1SJoe Perches			my $orig_commit = "";
28340d7835fcSJoe Perches			my $short = 1;
28350d7835fcSJoe Perches			my $long = 0;
28360d7835fcSJoe Perches			my $case = 1;
28370d7835fcSJoe Perches			my $space = 1;
28380d7835fcSJoe Perches			my $hasdesc = 0;
283919c146a6SJoe Perches			my $hasparens = 0;
28400d7835fcSJoe Perches			my $id = '0123456789ab';
28410d7835fcSJoe Perches			my $orig_desc = "commit description";
28420d7835fcSJoe Perches			my $description = "";
28430d7835fcSJoe Perches
2844fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2845fe043ea1SJoe Perches				$init_char = $1;
2846fe043ea1SJoe Perches				$orig_commit = lc($2);
2847fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2848fe043ea1SJoe Perches				$orig_commit = lc($1);
2849fe043ea1SJoe Perches			}
2850fe043ea1SJoe Perches
28510d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
28520d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
28530d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
28540d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
28550d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
28560d7835fcSJoe Perches				$orig_desc = $1;
285719c146a6SJoe Perches				$hasparens = 1;
28580d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
28590d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
28600d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
28610d7835fcSJoe Perches				$orig_desc = $1;
286219c146a6SJoe Perches				$hasparens = 1;
2863b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2864b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2865b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2866b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2867b671fde0SJoe Perches				$orig_desc = $1;
2868b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2869b671fde0SJoe Perches				$orig_desc .= " " . $1;
287019c146a6SJoe Perches				$hasparens = 1;
28710d7835fcSJoe Perches			}
28720d7835fcSJoe Perches
28730d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
28740d7835fcSJoe Perches							      $id, $orig_desc);
28750d7835fcSJoe Perches
2876948b133aSHeinrich Schuchardt			if (defined($id) &&
2877948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2878d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
28790d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
28800d7835fcSJoe Perches			}
2881d311cd44SJoe Perches		}
2882d311cd44SJoe Perches
288313f1937eSJoe Perches# Check for added, moved or deleted files
288413f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
288513f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
288613f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
288713f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
288813f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2889a82603a8SAndrew Jeffery			$is_patch = 1;
289013f1937eSJoe Perches			$reported_maintainer_file = 1;
289113f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
289213f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
289313f1937eSJoe Perches		}
289413f1937eSJoe Perches
2895e400edb1SRob Herring# Check for adding new DT bindings not in schema format
2896e400edb1SRob Herring		if (!$in_commit_log &&
2897e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
2898e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2899e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
2900e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2901e400edb1SRob Herring		}
2902e400edb1SRob Herring
290300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
29048905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2905000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2906000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
29076c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2908de7d4f0eSAndy Whitcroft		}
2909de7d4f0eSAndy Whitcroft
2910de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2911de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2912171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2913171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2914171ae1a4SAndy Whitcroft
2915171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2916171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2917171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2918171ae1a4SAndy Whitcroft
291934d99219SJoe Perches			CHK("INVALID_UTF8",
2920000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
292100df344fSAndy Whitcroft		}
29220a920b5bSAndy Whitcroft
292315662b3eSJoe Perches# Check if it's the start of a commit log
292415662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
292515662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2926eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2927eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
292815662b3eSJoe Perches			$in_header_lines = 0;
292915662b3eSJoe Perches			$in_commit_log = 1;
2930ed43c4e5SAllen Hubbe			$has_commit_log = 1;
293115662b3eSJoe Perches		}
293215662b3eSJoe Perches
2933fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2934fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2935fa64205dSPasi Savanainen		if ($in_header_lines &&
2936fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2937fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2938fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2939fa64205dSPasi Savanainen		}
2940fa64205dSPasi Savanainen
2941fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
294215662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2943fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
294415662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
294515662b3eSJoe Perches		}
294615662b3eSJoe Perches
2947d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2948d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2949d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2950d6430f71SJoe Perches				my $file = $1;
2951d6430f71SJoe Perches
2952d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2953d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2954d6430f71SJoe Perches					#
2955d6430f71SJoe Perches				} else {
2956d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2957d6430f71SJoe Perches				}
2958d6430f71SJoe Perches			}
2959d6430f71SJoe Perches		}
2960d6430f71SJoe Perches
296166b47b4aSKees Cook# Check for various typo / spelling mistakes
296266d7a382SJoe Perches		if (defined($misspellings) &&
296366d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2964ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
296566b47b4aSKees Cook				my $typo = $1;
296666b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
296766b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
296866b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
29690675a8fbSJean Delvare				my $msg_level = \&WARN;
29700675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
29710675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
297266b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
297366b47b4aSKees Cook				    $fix) {
297466b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
297566b47b4aSKees Cook				}
297666b47b4aSKees Cook			}
297766b47b4aSKees Cook		}
297866b47b4aSKees Cook
2979a8dd86bfSMatteo Croce# check for invalid commit id
2980a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2981a8dd86bfSMatteo Croce			my $id;
2982a8dd86bfSMatteo Croce			my $description;
2983a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
2984a8dd86bfSMatteo Croce			if (!defined($id)) {
2985a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
2986a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2987a8dd86bfSMatteo Croce			}
2988a8dd86bfSMatteo Croce		}
2989a8dd86bfSMatteo Croce
299030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
299130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
299200df344fSAndy Whitcroft
29930a920b5bSAndy Whitcroft#trailing whitespace
29949c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2995c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2996d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2997d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2998d5e616fcSJoe Perches			    $fix) {
2999194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3000d5e616fcSJoe Perches			}
3001c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3002c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30033705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
30043705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
30053705ce5bSJoe Perches			    $fix) {
3006194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
30073705ce5bSJoe Perches			}
30083705ce5bSJoe Perches
3009d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
30100a920b5bSAndy Whitcroft		}
30115368df20SAndy Whitcroft
30124783f894SJosh Triplett# Check for FSF mailing addresses.
3013109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
30141bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
30153e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
30163e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
30174783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30180675a8fbSJean Delvare			my $msg_level = \&ERROR;
30190675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
30200675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
30214783f894SJosh 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)
30224783f894SJosh Triplett		}
30234783f894SJosh Triplett
30243354957aSAndi Kleen# check for Kconfig help text having a real description
30259fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
30269fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
30273354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3028678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3029678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3030678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3031678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
30323354957aSAndi Kleen			my $length = 0;
30339fe287d7SAndy Whitcroft			my $cnt = $realcnt;
30349fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
30359fe287d7SAndy Whitcroft			my $f;
3036a1385803SAndy Whitcroft			my $is_start = 0;
30379fe287d7SAndy Whitcroft			my $is_end = 0;
3038a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
30399fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
30409fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
30419fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
30429fe287d7SAndy Whitcroft
30439fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
30448d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3045a1385803SAndy Whitcroft
304686adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3047a1385803SAndy Whitcroft					$is_start = 1;
304884af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
304984af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
305084af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
305184af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
305284af7a61SUlf Magnusson					}
3053a1385803SAndy Whitcroft					$length = -1;
3054a1385803SAndy Whitcroft				}
3055a1385803SAndy Whitcroft
30569fe287d7SAndy Whitcroft				$f =~ s/^.//;
30573354957aSAndi Kleen				$f =~ s/#.*//;
30583354957aSAndi Kleen				$f =~ s/^\s+//;
30593354957aSAndi Kleen				next if ($f =~ /^$/);
3060678ae162SUlf Magnusson
3061678ae162SUlf Magnusson				# This only checks context lines in the patch
3062678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3063678ae162SUlf Magnusson				# positives, even though some of these are
3064678ae162SUlf Magnusson				# common words in help texts
3065678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3066678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
30679fe287d7SAndy Whitcroft					$is_end = 1;
30689fe287d7SAndy Whitcroft					last;
30699fe287d7SAndy Whitcroft				}
30703354957aSAndi Kleen				$length++;
30713354957aSAndi Kleen			}
307256193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3073000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
307456193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
307556193274SVadim Bendebury			}
3076a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
30773354957aSAndi Kleen		}
30783354957aSAndi Kleen
30797ccf41a8SJoe Perches# check MAINTAINERS entries
30807ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
30817ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
30827ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
3083628f91a2SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
3084628f91a2SJoe Perches				if (WARN("MAINTAINERS_STYLE",
3085628f91a2SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3086628f91a2SJoe Perches				    $fix) {
3087628f91a2SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3088628f91a2SJoe Perches				}
3089628f91a2SJoe Perches			}
30907ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
30917ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
30927ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
30937ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
30947ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
30957ccf41a8SJoe Perches				my $cur = $1;
30967ccf41a8SJoe Perches				my $curval = $2;
30977ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
30987ccf41a8SJoe Perches				my $prev = $1;
30997ccf41a8SJoe Perches				my $prevval = $2;
31007ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
31017ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
31027ccf41a8SJoe Perches				if ($curindex < 0) {
31037ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
31047ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
31057ccf41a8SJoe Perches				} else {
31067ccf41a8SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
31077ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
31087ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
31097ccf41a8SJoe Perches					} elsif ((($prev eq 'F' && $cur eq 'F') ||
31107ccf41a8SJoe Perches						  ($prev eq 'X' && $cur eq 'X')) &&
31117ccf41a8SJoe Perches						 ($prevval cmp $curval) > 0) {
31127ccf41a8SJoe Perches						WARN("MAINTAINERS_STYLE",
31137ccf41a8SJoe Perches						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
31147ccf41a8SJoe Perches					}
31157ccf41a8SJoe Perches				}
31167ccf41a8SJoe Perches			}
31177ccf41a8SJoe Perches		}
3118628f91a2SJoe Perches
3119327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3120327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3121327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3122327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3123327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3124327953e9SChristoph Jaeger		}
3125327953e9SChristoph Jaeger
3126c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3127c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3128c68e5878SArnaud Lacombe			my $flag = $1;
3129c68e5878SArnaud Lacombe			my $replacement = {
3130c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3131c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3132c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3133c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3134c68e5878SArnaud Lacombe			};
3135c68e5878SArnaud Lacombe
3136c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3137c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3138c68e5878SArnaud Lacombe		}
3139c68e5878SArnaud Lacombe
3140bff5da43SRob Herring# check for DT compatible documentation
31417dd05b38SFlorian Vaussard		if (defined $root &&
31427dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
31437dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
31447dd05b38SFlorian Vaussard
3145bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3146bff5da43SRob Herring
3147cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3148852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3149cc93319bSFlorian Vaussard
3150bff5da43SRob Herring			foreach my $compat (@compats) {
3151bff5da43SRob Herring				my $compat2 = $compat;
3152185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3153185d566bSRob Herring				my $compat3 = $compat;
3154185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3155185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3156bff5da43SRob Herring				if ( $? >> 8 ) {
3157bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3158bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3159bff5da43SRob Herring				}
3160bff5da43SRob Herring
31614fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
31624fbf32a6SFlorian Vaussard				my $vendor = $1;
3163852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3164bff5da43SRob Herring				if ( $? >> 8 ) {
3165bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3166cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3167bff5da43SRob Herring				}
3168bff5da43SRob Herring			}
3169bff5da43SRob Herring		}
3170bff5da43SRob Herring
31719f3a8992SRob Herring# check for using SPDX license tag at beginning of files
31729f3a8992SRob Herring		if ($realline == $checklicenseline) {
31739f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
31749f3a8992SRob Herring				$checklicenseline = 2;
31759f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
31769f3a8992SRob Herring				my $comment = "";
31779f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
31789f3a8992SRob Herring					$comment = '/*';
31799f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
31809f3a8992SRob Herring					$comment = '//';
3181c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
31829f3a8992SRob Herring					$comment = '#';
31839f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
31849f3a8992SRob Herring					$comment = '..';
31859f3a8992SRob Herring				}
31869f3a8992SRob Herring
3187fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3188fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3189fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3190ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3191fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3192fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3193fdf13693SJoe Perches				}
3194fdf13693SJoe Perches
31959f3a8992SRob Herring				if ($comment !~ /^$/ &&
3196ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
31979f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
31989f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
31993b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
32003b6e8ac9SJoe Perches					my $spdx_license = $1;
32013b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
32023b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
32033b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
32043b6e8ac9SJoe Perches					}
320550c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
320650c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
320750c92900SLubomir Rintel						my $msg_level = \&WARN;
320850c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
320950c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
321050c92900SLubomir Rintel
321150c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
321250c92900SLubomir Rintel						    $fix) {
321350c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
321450c92900SLubomir Rintel						}
321550c92900SLubomir Rintel					}
32169f3a8992SRob Herring				}
32179f3a8992SRob Herring			}
32189f3a8992SRob Herring		}
32199f3a8992SRob Herring
32205368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3221d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
32225368df20SAndy Whitcroft
3223a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3224a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3225a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3226a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3227a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3228a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3229a8da38a9SJoe Perches		}
3230a8da38a9SJoe Perches
323147e0c88bSJoe Perches# line length limit (with some exclusions)
323247e0c88bSJoe Perches#
323347e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
323447e0c88bSJoe Perches#	logging functions like pr_info that end in a string
323547e0c88bSJoe Perches#	lines with a single string
323647e0c88bSJoe Perches#	#defines that are a single string
32372e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
323847e0c88bSJoe Perches#
323947e0c88bSJoe Perches# There are 3 different line length message types:
3240ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
324147e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
324247e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
324347e0c88bSJoe Perches#
324447e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
324547e0c88bSJoe Perches#
324647e0c88bSJoe Perches
3247b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
324847e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
324947e0c88bSJoe Perches
325047e0c88bSJoe Perches			# Check the allowed long line types first
325147e0c88bSJoe Perches
325247e0c88bSJoe Perches			# logging functions that end in a string that starts
325347e0c88bSJoe Perches			# before $max_line_length
325447e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
325547e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
325647e0c88bSJoe Perches				$msg_type = "";
325747e0c88bSJoe Perches
325847e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
325947e0c88bSJoe Perches			# #defines with only strings
326047e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
326147e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
326247e0c88bSJoe Perches				$msg_type = "";
326347e0c88bSJoe Perches
3264cc147506SJoe Perches			# More special cases
3265cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3266cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3267d560a5f8SJoe Perches				$msg_type = "";
3268d560a5f8SJoe Perches
32692e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
32702e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
32712e4bbbc5SAndreas Brauchli				$msg_type = "";
32722e4bbbc5SAndreas Brauchli
327347e0c88bSJoe Perches			# Otherwise set the alternate message types
327447e0c88bSJoe Perches
327547e0c88bSJoe Perches			# a comment starts before $max_line_length
327647e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
327747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
327847e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
327947e0c88bSJoe Perches
328047e0c88bSJoe Perches			# a quoted string starts before $max_line_length
328147e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
328247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
328347e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
328447e0c88bSJoe Perches			}
328547e0c88bSJoe Perches
328647e0c88bSJoe Perches			if ($msg_type ne "" &&
328747e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
3288bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3289bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3290bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3291bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
32920a920b5bSAndy Whitcroft			}
329347e0c88bSJoe Perches		}
32940a920b5bSAndy Whitcroft
32958905a67cSAndy Whitcroft# check for adding lines without a newline.
32968905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3297000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3298000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
32998905a67cSAndy Whitcroft		}
33008905a67cSAndy Whitcroft
3301b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3302de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
33030a920b5bSAndy Whitcroft
33040a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3305713a09deSAntonio Borneo# more than $tabsize must use tabs.
3306c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3307c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3308c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3309d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
33103705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
33113705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
33123705ce5bSJoe Perches			    $fix) {
3313194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
33143705ce5bSJoe Perches			}
33150a920b5bSAndy Whitcroft		}
33160a920b5bSAndy Whitcroft
3317*1a3dcf2eSJoe Perches# check for repeated words separated by a single space
3318*1a3dcf2eSJoe Perches		if ($rawline =~ /^\+/) {
3319*1a3dcf2eSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3320*1a3dcf2eSJoe Perches
3321*1a3dcf2eSJoe Perches				my $first = $1;
3322*1a3dcf2eSJoe Perches				my $second = $2;
3323*1a3dcf2eSJoe Perches
3324*1a3dcf2eSJoe Perches				if ($first =~ /(?:struct|union|enum)/) {
3325*1a3dcf2eSJoe Perches					pos($rawline) += length($first) + length($second) + 1;
3326*1a3dcf2eSJoe Perches					next;
3327*1a3dcf2eSJoe Perches				}
3328*1a3dcf2eSJoe Perches
3329*1a3dcf2eSJoe Perches				next if ($first ne $second);
3330*1a3dcf2eSJoe Perches				next if ($first eq 'long');
3331*1a3dcf2eSJoe Perches
3332*1a3dcf2eSJoe Perches				if (WARN("REPEATED_WORD",
3333*1a3dcf2eSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3334*1a3dcf2eSJoe Perches				    $fix) {
3335*1a3dcf2eSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3336*1a3dcf2eSJoe Perches				}
3337*1a3dcf2eSJoe Perches			}
3338*1a3dcf2eSJoe Perches
3339*1a3dcf2eSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3340*1a3dcf2eSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
3341*1a3dcf2eSJoe Perches			    $prevrawline =~ /($word_pattern)\s*$/) {
3342*1a3dcf2eSJoe Perches				my $last_word = $1;
3343*1a3dcf2eSJoe Perches				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3344*1a3dcf2eSJoe Perches					if (WARN("REPEATED_WORD",
3345*1a3dcf2eSJoe Perches						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3346*1a3dcf2eSJoe Perches					    $fix) {
3347*1a3dcf2eSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3348*1a3dcf2eSJoe Perches					}
3349*1a3dcf2eSJoe Perches				}
3350*1a3dcf2eSJoe Perches			}
3351*1a3dcf2eSJoe Perches		}
3352*1a3dcf2eSJoe Perches
335308e44365SAlberto Panizzo# check for space before tabs.
335408e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
335508e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
33563705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
33573705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
33583705ce5bSJoe Perches			    $fix) {
3359194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3360713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3361194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3362c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
33633705ce5bSJoe Perches			}
336408e44365SAlberto Panizzo		}
336508e44365SAlberto Panizzo
33666a487211SJoe Perches# check for assignments on the start of a line
33676a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
33686a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
33696a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
33706a487211SJoe Perches		}
33716a487211SJoe Perches
3372d1fe9c09SJoe Perches# check for && or || at the start of a line
3373d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3374d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3375d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3376d1fe9c09SJoe Perches		}
3377d1fe9c09SJoe Perches
3378a91e8994SJoe Perches# check indentation starts on a tab stop
33795b57980dSJoe Perches		if ($perl_version_ok &&
3380bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3381a91e8994SJoe Perches			my $indent = length($1);
3382713a09deSAntonio Borneo			if ($indent % $tabsize) {
3383a91e8994SJoe Perches				if (WARN("TABSTOP",
3384a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3385a91e8994SJoe Perches				    $fix) {
3386713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3387a91e8994SJoe Perches				}
3388a91e8994SJoe Perches			}
3389a91e8994SJoe Perches		}
3390a91e8994SJoe Perches
3391d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
33925b57980dSJoe Perches		if ($perl_version_ok &&
3393fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3394d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3395d1fe9c09SJoe Perches			my $oldindent = $1;
3396d1fe9c09SJoe Perches			my $rest = $2;
3397d1fe9c09SJoe Perches
3398d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3399d1fe9c09SJoe Perches			if ($pos >= 0) {
3400b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3401b34a26f3SJoe Perches				my $newindent = $2;
3402d1fe9c09SJoe Perches
3403d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3404713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3405713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3406d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3407d1fe9c09SJoe Perches
3408d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3409d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
34103705ce5bSJoe Perches
34113705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
34123705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
34133705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3414194f66fcSJoe Perches						$fixed[$fixlinenr] =~
34153705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
34163705ce5bSJoe Perches					}
3417d1fe9c09SJoe Perches				}
3418d1fe9c09SJoe Perches			}
3419d1fe9c09SJoe Perches		}
3420d1fe9c09SJoe Perches
34216ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
34226ab3a970SJoe Perches# avoid checking a few false positives:
34236ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
34246ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
34256ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
34266ab3a970SJoe Perches#   multiline macros that define functions
34276ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
34286ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
34296ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
34303705ce5bSJoe Perches			if (CHK("SPACING",
3431f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
34323705ce5bSJoe Perches			    $fix) {
3433194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3434f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
34353705ce5bSJoe Perches			}
3436aad4f614SJoe Perches		}
3437aad4f614SJoe Perches
343886406b1cSJoe Perches# Block comment styles
343986406b1cSJoe Perches# Networking with an initial /*
344005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3441fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
344285ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
344385ad978cSJoe Perches		    $realline > 2) {
344405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
344505880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
344605880600SJoe Perches		}
344705880600SJoe Perches
344886406b1cSJoe Perches# Block comments use * on subsequent lines
344986406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
345086406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3451a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
345261135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3453a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
345486406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
345586406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3456a605e32eSJoe Perches		}
3457a605e32eSJoe Perches
345886406b1cSJoe Perches# Block comments use */ on trailing lines
345986406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3460c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3461c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3462c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
346386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
346486406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
346505880600SJoe Perches		}
346605880600SJoe Perches
346708eb9b80SJoe Perches# Block comment * alignment
346808eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3469af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3470af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3471af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
347208eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3473af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3474af207524SJoe Perches			my $oldindent;
347508eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3476af207524SJoe Perches			if (defined($1)) {
3477af207524SJoe Perches				$oldindent = expand_tabs($1);
3478af207524SJoe Perches			} else {
3479af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3480af207524SJoe Perches				$oldindent = expand_tabs($1);
3481af207524SJoe Perches			}
348208eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
348308eb9b80SJoe Perches			my $newindent = $1;
348408eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3485af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
348608eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
348708eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
348808eb9b80SJoe Perches			}
348908eb9b80SJoe Perches		}
349008eb9b80SJoe Perches
34917f619191SJoe Perches# check for missing blank lines after struct/union declarations
34927f619191SJoe Perches# with exceptions for various attributes and macros
34937f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
34947f619191SJoe Perches		    $line =~ /^\+/ &&
34957f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
34967f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
34977f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
34987f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
34997f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
35007f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
35017f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
35020bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
35037f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3504d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3505d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3506d752fcc8SJoe Perches			    $fix) {
3507f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3508d752fcc8SJoe Perches			}
35097f619191SJoe Perches		}
35107f619191SJoe Perches
3511365dd4eaSJoe Perches# check for multiple consecutive blank lines
3512365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3513365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3514365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3515d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3516d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3517d752fcc8SJoe Perches			    $fix) {
3518f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3519d752fcc8SJoe Perches			}
3520d752fcc8SJoe Perches
3521365dd4eaSJoe Perches			$last_blank_line = $linenr;
3522365dd4eaSJoe Perches		}
3523365dd4eaSJoe Perches
35243b617e3bSJoe Perches# check for missing blank lines after declarations
35253f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
35263f7bac03SJoe Perches			# actual declarations
35273f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
35285a4e1fd3SJoe Perches			# function pointer declarations
35295a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
35303f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
35313f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
35323f7bac03SJoe Perches			# known declaration macros
35333f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
35343f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
35353f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
35363f7bac03SJoe Perches			# other possible extensions of declaration lines
35373f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
35383f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
35393f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
35403f7bac03SJoe Perches			# looks like a declaration
35413f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
35425a4e1fd3SJoe Perches			# function pointer declarations
35435a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
35443f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
35453f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
35463f7bac03SJoe Perches			# known declaration macros
35473f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
35483f7bac03SJoe Perches			# start of struct or union or enum
3549328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
35503f7bac03SJoe Perches			# start or end of block or continuation of declaration
35513f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
35523f7bac03SJoe Perches			# bitfield continuation
35533f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
35543f7bac03SJoe Perches			# other possible extensions of declaration lines
35553f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
35563f7bac03SJoe Perches			# indentation of previous and current line are the same
35573f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3558d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3559d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3560d752fcc8SJoe Perches			    $fix) {
3561f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3562d752fcc8SJoe Perches			}
35633b617e3bSJoe Perches		}
35643b617e3bSJoe Perches
35655f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
35666b4c5bebSAndy Whitcroft# Exceptions:
35676b4c5bebSAndy Whitcroft#  1) within comments
35686b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
35696b4c5bebSAndy Whitcroft#  3) hanging labels
35703705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
35715f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35723705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
35733705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
35743705ce5bSJoe Perches			    $fix) {
3575194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
35763705ce5bSJoe Perches			}
35775f7ddae6SRaffaele Recalcati		}
35785f7ddae6SRaffaele Recalcati
3579b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3580b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3581b9ea10d6SAndy Whitcroft
35825751a24eSJoe Perches# check for unusual line ending [ or (
35835751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
35845751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
35855751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
35865751a24eSJoe Perches		}
35875751a24eSJoe Perches
35884dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
35894dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
35904dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
35914dbed76fSJoe Perches			$context_function = $1;
35924dbed76fSJoe Perches		}
35934dbed76fSJoe Perches
35944dbed76fSJoe Perches# check if this appears to be the end of function declaration
35954dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
35964dbed76fSJoe Perches			undef $context_function;
35974dbed76fSJoe Perches		}
35984dbed76fSJoe Perches
3599032a4c0fSJoe Perches# check indentation of any line with a bare else
3600840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3601032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3602032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3603032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3604840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3605840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3606840080a0SJoe Perches			     defined $lines[$linenr] &&
3607840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3608032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3609032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3610032a4c0fSJoe Perches			}
3611032a4c0fSJoe Perches		}
3612032a4c0fSJoe Perches
3613c00df19aSJoe Perches# check indentation of a line with a break;
3614c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3615c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3616c00df19aSJoe Perches			my $tabs = $1;
3617c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3618c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3619c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3620c00df19aSJoe Perches			}
3621c00df19aSJoe Perches		}
3622c00df19aSJoe Perches
3623c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3624cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3625000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3626000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3627c2fdda0dSAndy Whitcroft		}
362822f2a2efSAndy Whitcroft
362956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
363056e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
363156e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
363256e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
363356e77d70SJoe Perches		}
363456e77d70SJoe Perches
36359c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
36362b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
36372b474a1aSAndy Whitcroft		    $realline_next);
36383e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3639ca819864SJoe Perches		if ($linenr > $suppress_statement &&
36401b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3641170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3642f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3643171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3644171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3645171ae1a4SAndy Whitcroft
36463e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
36473e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
36483e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
36493e469cdcSAndy Whitcroft			# until we hit end of it.
36503e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
36513e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
36523e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
36533e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
36543e469cdcSAndy Whitcroft			}
3655f74bd194SAndy Whitcroft
36562b474a1aSAndy Whitcroft			# Find the real next line.
36572b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
36582b474a1aSAndy Whitcroft			if (defined $realline_next &&
36592b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
36602b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
36612b474a1aSAndy Whitcroft				$realline_next++;
36622b474a1aSAndy Whitcroft			}
36632b474a1aSAndy Whitcroft
3664171ae1a4SAndy Whitcroft			my $s = $stat;
3665171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3666cf655043SAndy Whitcroft
3667c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3668171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3669c2fdda0dSAndy Whitcroft
3670c2fdda0dSAndy Whitcroft			# Ignore functions being called
3671171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3672c2fdda0dSAndy Whitcroft
3673463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3674463f2864SAndy Whitcroft
3675c45dcabdSAndy Whitcroft			# declarations always start with types
3676d2506586SAndy 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) {
3677c45dcabdSAndy Whitcroft				my $type = $1;
3678c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3679c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3680c45dcabdSAndy Whitcroft
36816c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3682a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3683c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3684c2fdda0dSAndy Whitcroft			}
36858905a67cSAndy Whitcroft
36866c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
368765863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3688c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
36899c0ca6f9SAndy Whitcroft			}
36908905a67cSAndy Whitcroft
36918905a67cSAndy Whitcroft			# Check for any sort of function declaration.
36928905a67cSAndy Whitcroft			# int foo(something bar, other baz);
36938905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3694171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
36958905a67cSAndy Whitcroft				my ($name_len) = length($1);
36968905a67cSAndy Whitcroft
3697cf655043SAndy Whitcroft				my $ctx = $s;
3698773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
36998905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3700cf655043SAndy Whitcroft
37018905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3702c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
37038905a67cSAndy Whitcroft
3704c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
37058905a67cSAndy Whitcroft					}
37068905a67cSAndy Whitcroft				}
37078905a67cSAndy Whitcroft			}
37088905a67cSAndy Whitcroft
37099c0ca6f9SAndy Whitcroft		}
37109c0ca6f9SAndy Whitcroft
371100df344fSAndy Whitcroft#
371200df344fSAndy Whitcroft# Checks which may be anchored in the context.
371300df344fSAndy Whitcroft#
371400df344fSAndy Whitcroft
371500df344fSAndy Whitcroft# Check for switch () and associated case and default
371600df344fSAndy Whitcroft# statements should be at the same indent.
371700df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
371800df344fSAndy Whitcroft			my $err = '';
371900df344fSAndy Whitcroft			my $sep = '';
372000df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
372100df344fSAndy Whitcroft			shift(@ctx);
372200df344fSAndy Whitcroft			for my $ctx (@ctx) {
372300df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
372400df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
372500df344fSAndy Whitcroft							$indent != $cindent) {
372600df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
372700df344fSAndy Whitcroft					$sep = '';
372800df344fSAndy Whitcroft				} else {
372900df344fSAndy Whitcroft					$sep = "[...]\n";
373000df344fSAndy Whitcroft				}
373100df344fSAndy Whitcroft			}
373200df344fSAndy Whitcroft			if ($err ne '') {
3733000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3734000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3735de7d4f0eSAndy Whitcroft			}
3736de7d4f0eSAndy Whitcroft		}
3737de7d4f0eSAndy Whitcroft
3738de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3739de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
37400fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3741773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3742773647a0SAndy Whitcroft
37439c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
37448eef05ddSJoe Perches
37458eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
37468eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
37478eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
37488eef05ddSJoe Perches			}
37498eef05ddSJoe Perches
3750de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3751de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3752de7d4f0eSAndy Whitcroft
3753548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3754548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3755de7d4f0eSAndy Whitcroft
3756548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3757548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3758548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3759548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3760548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3761773647a0SAndy Whitcroft				$ctx_ln++;
3762773647a0SAndy Whitcroft			}
3763548596d5SAndy Whitcroft
376453210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
376553210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3766773647a0SAndy Whitcroft
3767773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3768000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3769000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
377001464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
377100df344fSAndy Whitcroft			}
3772773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3773773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3774773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3775773647a0SAndy Whitcroft			{
37769c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
37779c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3778000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3779000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
378001464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
37819c0ca6f9SAndy Whitcroft				}
37829c0ca6f9SAndy Whitcroft			}
378300df344fSAndy Whitcroft		}
378400df344fSAndy Whitcroft
37854d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3786f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
37873e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
37883e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
37893e469cdcSAndy Whitcroft					if (!defined $stat);
37904d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
37914d001e4dSAndy Whitcroft
37924d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
37934d001e4dSAndy Whitcroft
37949f5af480SJoe Perches			# remove inline comments
37959f5af480SJoe Perches			$s =~ s/$;/ /g;
37969f5af480SJoe Perches			$c =~ s/$;/ /g;
37974d001e4dSAndy Whitcroft
37984d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
37996f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
38006f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
38014d001e4dSAndy Whitcroft
38029f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
38039f5af480SJoe Perches			# none on the first line, and are going to readd them
38049f5af480SJoe Perches			# where necessary.
38059f5af480SJoe Perches			$s =~ s/\n./\n/gs;
38069f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
38079f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
38089f5af480SJoe Perches			}
38099f5af480SJoe Perches
38104d001e4dSAndy Whitcroft			# We want to check the first line inside the block
38114d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
38124d001e4dSAndy Whitcroft			#  1) any blank line termination
38134d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
38144d001e4dSAndy Whitcroft			#  3) any do (...) {
38154d001e4dSAndy Whitcroft			my $continuation = 0;
38164d001e4dSAndy Whitcroft			my $check = 0;
38174d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
38184d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
38194d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
38204d001e4dSAndy Whitcroft				$continuation = 1;
38214d001e4dSAndy Whitcroft			}
38229bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
38234d001e4dSAndy Whitcroft				$check = 1;
38244d001e4dSAndy Whitcroft				$cond_lines++;
38254d001e4dSAndy Whitcroft			}
38264d001e4dSAndy Whitcroft
38274d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
38284d001e4dSAndy Whitcroft			# preprocessor statement.
38294d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
38304d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
38314d001e4dSAndy Whitcroft				$check = 0;
38324d001e4dSAndy Whitcroft			}
38334d001e4dSAndy Whitcroft
38349bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3835740504c6SAndy Whitcroft			$continuation = 0;
38369bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
38379bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
38384d001e4dSAndy Whitcroft
3839f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3840f16fa28fSAndy Whitcroft				# is not linear.
3841f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3842f16fa28fSAndy Whitcroft					$check = 0;
3843f16fa28fSAndy Whitcroft				}
3844f16fa28fSAndy Whitcroft
38459bd49efeSAndy Whitcroft				# Ignore:
38469bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
38479bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
38489bd49efeSAndy Whitcroft				#  3) labels.
3849740504c6SAndy Whitcroft				if ($continuation ||
3850740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
38519bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
38529bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3853740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
385430dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
38559bd49efeSAndy Whitcroft						$cond_lines++;
38569bd49efeSAndy Whitcroft					}
38574d001e4dSAndy Whitcroft				}
385830dad6ebSAndy Whitcroft			}
38594d001e4dSAndy Whitcroft
38604d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
38614d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
38624d001e4dSAndy Whitcroft
38634d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
38644d001e4dSAndy Whitcroft			# this is not this patch's fault.
38654d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
38664d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
38674d001e4dSAndy Whitcroft				$check = 0;
38684d001e4dSAndy Whitcroft			}
38694d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
38704d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
38714d001e4dSAndy Whitcroft			}
38724d001e4dSAndy Whitcroft
38739bd49efeSAndy 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";
38744d001e4dSAndy Whitcroft
38759f5af480SJoe Perches			if ($check && $s ne '' &&
3876713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
38779f5af480SJoe Perches			     ($sindent < $indent) ||
3878f6950a73SJoe Perches			     ($sindent == $indent &&
3879f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3880713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
3881000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3882000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
38834d001e4dSAndy Whitcroft			}
38844d001e4dSAndy Whitcroft		}
38854d001e4dSAndy Whitcroft
38866c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
38876c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
38881f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
38891f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
38906c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3891c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3892c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3893cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3894cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
38951f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3896c2fdda0dSAndy Whitcroft		}
38976c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
38986c72ffaaSAndy Whitcroft
389900df344fSAndy Whitcroft#ignore lines not being added
39003705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
390100df344fSAndy Whitcroft
390211ca40a0SJoe Perches# check for dereferences that span multiple lines
390311ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
390411ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
390511ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
390611ca40a0SJoe Perches			my $ref = $1;
390711ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
390811ca40a0SJoe Perches			$ref .= $1;
390911ca40a0SJoe Perches			$ref =~ s/\s//g;
391011ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
391111ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
391211ca40a0SJoe Perches		}
391311ca40a0SJoe Perches
3914a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3915c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3916a1ce18e4SJoe Perches			my $type = $1;
3917a1ce18e4SJoe Perches			my $var = $2;
3918207a8e84SJoe Perches			$var = "" if (!defined $var);
3919207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3920a1ce18e4SJoe Perches				my $sign = $1;
3921a1ce18e4SJoe Perches				my $pointer = $2;
3922a1ce18e4SJoe Perches
3923a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3924a1ce18e4SJoe Perches
3925a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3926a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3927a1ce18e4SJoe Perches				    $fix) {
3928a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3929207a8e84SJoe Perches					my $comp_pointer = $pointer;
3930207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3931207a8e84SJoe Perches					$decl .= $comp_pointer;
3932207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3933207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3934a1ce18e4SJoe Perches				}
3935a1ce18e4SJoe Perches			}
3936a1ce18e4SJoe Perches		}
3937a1ce18e4SJoe Perches
3938653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
39397429c690SAndy Whitcroft		if ($dbg_type) {
39407429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3941000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3942000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
39437429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3944000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3945000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
39467429c690SAndy Whitcroft			}
3947653d4876SAndy Whitcroft			next;
3948653d4876SAndy Whitcroft		}
3949a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3950a1ef277eSAndy Whitcroft		if ($dbg_attr) {
39519360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3952000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3953000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
39549360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3955000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3956000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3957a1ef277eSAndy Whitcroft			}
3958a1ef277eSAndy Whitcroft			next;
3959a1ef277eSAndy Whitcroft		}
3960653d4876SAndy Whitcroft
3961f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
396299423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
396399423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3964d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3965d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3966f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3967f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3968f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3969d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3970d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3971f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3972d752fcc8SJoe Perches				$fixedline = $line;
39738d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3974f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3975d752fcc8SJoe Perches			}
3976f0a594c1SAndy Whitcroft		}
3977f0a594c1SAndy Whitcroft
397800df344fSAndy Whitcroft#
397900df344fSAndy Whitcroft# Checks which are anchored on the added line.
398000df344fSAndy Whitcroft#
398100df344fSAndy Whitcroft
3982653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3983c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3984653d4876SAndy Whitcroft			my $path = $1;
3985653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3986000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3987495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3988495e9d84SJoe Perches			}
3989495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3990495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3991495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3992653d4876SAndy Whitcroft			}
3993653d4876SAndy Whitcroft		}
3994653d4876SAndy Whitcroft
399500df344fSAndy Whitcroft# no C99 // comments
399600df344fSAndy Whitcroft		if ($line =~ m{//}) {
39973705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
39983705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
39993705ce5bSJoe Perches			    $fix) {
4000194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
40013705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
40023705ce5bSJoe Perches					my $comment = trim($1);
4003194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
40043705ce5bSJoe Perches				}
40053705ce5bSJoe Perches			}
400600df344fSAndy Whitcroft		}
400700df344fSAndy Whitcroft		# Remove C99 comments.
40080a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
40096c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
40100a920b5bSAndy Whitcroft
40112b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
40122b474a1aSAndy Whitcroft# the whole statement.
40132b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
40142b474a1aSAndy Whitcroft		if (defined $realline_next &&
40152b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
40162b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
40172b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
40182b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
40193cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
40203cbf62dfSAndy Whitcroft			# a prefix:
40213cbf62dfSAndy Whitcroft			#   XXX(foo);
40223cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4023653d4876SAndy Whitcroft			my $name = $1;
402487a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
40253cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
40263cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
40273cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
40283cbf62dfSAndy Whitcroft
40293cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
40302b474a1aSAndy Whitcroft				\n.}\s*$|
403148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
403248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
403348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
40342b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
40352b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
403648012058SAndy Whitcroft			    )/x) {
40372b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
40382b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
40392b474a1aSAndy Whitcroft			} else {
40402b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
40410a920b5bSAndy Whitcroft			}
40420a920b5bSAndy Whitcroft		}
40432b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
40442b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
40452b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
40462b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
40472b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
40482b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
40492b474a1aSAndy Whitcroft		}
40502b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
40512b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4052000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4053000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
40542b474a1aSAndy Whitcroft		}
40550a920b5bSAndy Whitcroft
40565150bda4SJoe Eloff# check for global initialisers.
40576d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
4058d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
40596d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4060d5e616fcSJoe Perches			    $fix) {
40616d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4062d5e616fcSJoe Perches			}
4063f0a594c1SAndy Whitcroft		}
40640a920b5bSAndy Whitcroft# check for static initialisers.
40656d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4066d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
40676d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4068d5e616fcSJoe Perches				      $herecurr) &&
4069d5e616fcSJoe Perches			    $fix) {
40706d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4071d5e616fcSJoe Perches			}
40720a920b5bSAndy Whitcroft		}
40730a920b5bSAndy Whitcroft
40741813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
40751813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
40761813087dSJoe Perches			my $tmp = trim($1);
40771813087dSJoe Perches			WARN("MISORDERED_TYPE",
40781813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
40791813087dSJoe Perches		}
40801813087dSJoe Perches
4081809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4082809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4083809e082eSJoe Perches			my $type = trim($1);
4084809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4085809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4086809e082eSJoe Perches			my $new_type = $type;
4087809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4088809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4089809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4090809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4091809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4092809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4093809e082eSJoe Perches			$new_type = trim($new_type);
4094809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4095809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4096809e082eSJoe Perches			    $fix) {
4097809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4098809e082eSJoe Perches			}
4099809e082eSJoe Perches		}
4100809e082eSJoe Perches
4101cb710ecaSJoe Perches# check for static const char * arrays.
4102cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4103000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4104000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4105cb710ecaSJoe Perches				$herecurr);
4106cb710ecaSJoe Perches		}
4107cb710ecaSJoe Perches
410877b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
410977b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
411077b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
411177b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
411277b8c0a8SJoe Perches			    $fix) {
411377b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
411477b8c0a8SJoe Perches			}
411577b8c0a8SJoe Perches		}
411677b8c0a8SJoe Perches
4117cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4118cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4119000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4120000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4121cb710ecaSJoe Perches				$herecurr);
4122cb710ecaSJoe Perches		}
4123cb710ecaSJoe Perches
4124ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4125ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4126ab7e23f3SJoe Perches			my $found = $1;
4127ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4128ab7e23f3SJoe Perches				WARN("CONST_CONST",
4129ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4130ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4131ab7e23f3SJoe Perches				WARN("CONST_CONST",
4132ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4133ab7e23f3SJoe Perches			}
4134ab7e23f3SJoe Perches		}
4135ab7e23f3SJoe Perches
41369b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
41379b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
41389b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
41399b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
41409b0fa60dSJoe Perches				$herecurr);
41419b0fa60dSJoe Perches               }
41429b0fa60dSJoe Perches
4143b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4144b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4145b598b670SJoe Perches			my $array = $1;
4146b598b670SJoe 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*\))@) {
4147b598b670SJoe Perches				my $array_div = $1;
4148b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4149b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4150b598b670SJoe Perches				    $fix) {
4151b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4152b598b670SJoe Perches				}
4153b598b670SJoe Perches			}
4154b598b670SJoe Perches		}
4155b598b670SJoe Perches
4156b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
415716b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4158b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4159b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4160b36190c5SJoe Perches			    $fix) {
4161194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4162b36190c5SJoe Perches			}
4163b36190c5SJoe Perches		}
4164b36190c5SJoe Perches
4165653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4166653d4876SAndy Whitcroft# make sense.
4167653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
41688054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4169c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
41708ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
417146d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4172000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4173000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
41740a920b5bSAndy Whitcroft		}
41750a920b5bSAndy Whitcroft
41760a920b5bSAndy Whitcroft# * goes on variable not on type
417765863862SAndy Whitcroft		# (char*[ const])
4178bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4179bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
41803705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4181d8aaf121SAndy Whitcroft
418265863862SAndy Whitcroft			# Should start with a space.
418365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
418465863862SAndy Whitcroft			# Should not end with a space.
418565863862SAndy Whitcroft			$to =~ s/\s+$//;
418665863862SAndy Whitcroft			# '*'s should not have spaces between.
4187f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
418865863862SAndy Whitcroft			}
4189d8aaf121SAndy Whitcroft
41903705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
419165863862SAndy Whitcroft			if ($from ne $to) {
41923705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
41933705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
41943705ce5bSJoe Perches				    $fix) {
41953705ce5bSJoe Perches					my $sub_from = $ident;
41963705ce5bSJoe Perches					my $sub_to = $ident;
41973705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4198194f66fcSJoe Perches					$fixed[$fixlinenr] =~
41993705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
42003705ce5bSJoe Perches				}
420165863862SAndy Whitcroft			}
4202bfcb2cc7SAndy Whitcroft		}
4203bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4204bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
42053705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4206d8aaf121SAndy Whitcroft
420765863862SAndy Whitcroft			# Should start with a space.
420865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
420965863862SAndy Whitcroft			# Should not end with a space.
421065863862SAndy Whitcroft			$to =~ s/\s+$//;
421165863862SAndy Whitcroft			# '*'s should not have spaces between.
4212f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
421365863862SAndy Whitcroft			}
421465863862SAndy Whitcroft			# Modifiers should have spaces.
421565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
421665863862SAndy Whitcroft
42173705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4218667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
42193705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
42203705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
42213705ce5bSJoe Perches				    $fix) {
42223705ce5bSJoe Perches
42233705ce5bSJoe Perches					my $sub_from = $match;
42243705ce5bSJoe Perches					my $sub_to = $match;
42253705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4226194f66fcSJoe Perches					$fixed[$fixlinenr] =~
42273705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
42283705ce5bSJoe Perches				}
422965863862SAndy Whitcroft			}
42300a920b5bSAndy Whitcroft		}
42310a920b5bSAndy Whitcroft
42329d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
42339d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
42340675a8fbSJean Delvare			my $msg_level = \&WARN;
42350675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
42360675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
42379d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
42389d3e3c70SJoe Perches		}
42390a920b5bSAndy Whitcroft
42409d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
42418905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4242000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4243000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
42448905a67cSAndy Whitcroft		}
42458905a67cSAndy Whitcroft
424617441227SJoe Perches# check for uses of printk_ratelimit
424717441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4248000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4249000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
425017441227SJoe Perches		}
425117441227SJoe Perches
4252eeef5733SJoe Perches# printk should use KERN_* levels
4253eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4254000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4255eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
425600df344fSAndy Whitcroft		}
42570a920b5bSAndy Whitcroft
4258243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4259243f3803SJoe Perches			my $orig = $1;
4260243f3803SJoe Perches			my $level = lc($orig);
4261243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
42628f26b837SJoe Perches			my $level2 = $level;
42638f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4264243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4265daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4266243f3803SJoe Perches		}
4267243f3803SJoe Perches
4268dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4269dc139313SJoe Perches			my $orig = $1;
4270dc139313SJoe Perches			my $level = lc($orig);
4271dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4272dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4273dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4274dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4275dc139313SJoe Perches		}
4276dc139313SJoe Perches
427791c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
427891c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
427991c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
428091c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
428191c9afafSAndy Lutomirski			WARN("ENOSYS",
428291c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
428391c9afafSAndy Lutomirski		}
428491c9afafSAndy Lutomirski
42856b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
42866b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
42876b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
42886b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
42896b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
42906b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
42916b9ea5ffSJakub Kicinski			    $fix) {
42926b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
42936b9ea5ffSJakub Kicinski			}
42946b9ea5ffSJakub Kicinski		}
42956b9ea5ffSJakub Kicinski
4296653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4297653d4876SAndy Whitcroft# or if closed on same line
42985b57980dSJoe Perches		if ($perl_version_ok &&
42992d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
43002d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
43012d453e3bSJoe Perches		    $sline !~ /}/) {
43028d182478SJoe Perches			if (ERROR("OPEN_BRACE",
43032d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
43048d182478SJoe Perches			    $fix) {
43058d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
43068d182478SJoe Perches				my $fixed_line = $rawline;
43078d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
43088d182478SJoe Perches				my $line1 = $1;
43098d182478SJoe Perches				my $line2 = $2;
43108d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
43118d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
43128d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
43138d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
43148d182478SJoe Perches				}
43158d182478SJoe Perches			}
43160a920b5bSAndy Whitcroft		}
4317653d4876SAndy Whitcroft
43188905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
43198905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
43208905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
43218d182478SJoe Perches			if (ERROR("OPEN_BRACE",
43228d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
43238d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
43248d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
43258d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
43268d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
43278d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
43288d182478SJoe Perches				$fixedline = $rawline;
43298d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
43308d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
43318d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
43328d182478SJoe Perches				}
43338d182478SJoe Perches			}
43348905a67cSAndy Whitcroft		}
43358905a67cSAndy Whitcroft
43360c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
43373705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
43383705ce5bSJoe Perches			if (WARN("SPACING",
43393705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
43403705ce5bSJoe Perches			    $fix) {
4341194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43423705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
43433705ce5bSJoe Perches			}
43440c73b4ebSAndy Whitcroft		}
43450c73b4ebSAndy Whitcroft
434631070b5dSJoe Perches# Function pointer declarations
434731070b5dSJoe Perches# check spacing between type, funcptr, and args
434831070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
434991f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
435031070b5dSJoe Perches			my $declare = $1;
435131070b5dSJoe Perches			my $pre_pointer_space = $2;
435231070b5dSJoe Perches			my $post_pointer_space = $3;
435331070b5dSJoe Perches			my $funcname = $4;
435431070b5dSJoe Perches			my $post_funcname_space = $5;
435531070b5dSJoe Perches			my $pre_args_space = $6;
435631070b5dSJoe Perches
435791f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
435891f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
435991f72e9cSJoe Perches# don't need a space so don't warn for those.
436091f72e9cSJoe Perches			my $post_declare_space = "";
436191f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
436291f72e9cSJoe Perches				$post_declare_space = $1;
436391f72e9cSJoe Perches				$declare = rtrim($declare);
436491f72e9cSJoe Perches			}
436591f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
436631070b5dSJoe Perches				WARN("SPACING",
436731070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
436891f72e9cSJoe Perches				$post_declare_space = " ";
436931070b5dSJoe Perches			}
437031070b5dSJoe Perches
437131070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
437291f72e9cSJoe Perches# This test is not currently implemented because these declarations are
437391f72e9cSJoe Perches# equivalent to
437491f72e9cSJoe Perches#	int  foo(int bar, ...)
437591f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
437691f72e9cSJoe Perches#
437791f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
437891f72e9cSJoe Perches#				WARN("SPACING",
437991f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
438091f72e9cSJoe Perches#			}
438131070b5dSJoe Perches
438231070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
438331070b5dSJoe Perches			if (defined $pre_pointer_space &&
438431070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
438531070b5dSJoe Perches				WARN("SPACING",
438631070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
438731070b5dSJoe Perches			}
438831070b5dSJoe Perches
438931070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
439031070b5dSJoe Perches			if (defined $post_pointer_space &&
439131070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
439231070b5dSJoe Perches				WARN("SPACING",
439331070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
439431070b5dSJoe Perches			}
439531070b5dSJoe Perches
439631070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
439731070b5dSJoe Perches			if (defined $post_funcname_space &&
439831070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
439931070b5dSJoe Perches				WARN("SPACING",
440031070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
440131070b5dSJoe Perches			}
440231070b5dSJoe Perches
440331070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
440431070b5dSJoe Perches			if (defined $pre_args_space &&
440531070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
440631070b5dSJoe Perches				WARN("SPACING",
440731070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
440831070b5dSJoe Perches			}
440931070b5dSJoe Perches
441031070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4411194f66fcSJoe Perches				$fixed[$fixlinenr] =~
441291f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
441331070b5dSJoe Perches			}
441431070b5dSJoe Perches		}
441531070b5dSJoe Perches
44168d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
44178d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4418fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4419fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
44208d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
44218d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
44228d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4423fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
442438dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
44253705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
44263705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
44273705ce5bSJoe Perches				    $fix) {
4428194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
44293705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
44303705ce5bSJoe Perches				}
44318d31cfceSAndy Whitcroft			}
44328d31cfceSAndy Whitcroft		}
44338d31cfceSAndy Whitcroft
4434f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
44356c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4436c2fdda0dSAndy Whitcroft			my $name = $1;
4437773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4438773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4439c2fdda0dSAndy Whitcroft
4440c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4441773647a0SAndy Whitcroft			if ($name =~ /^(?:
4442773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4443773647a0SAndy Whitcroft				volatile|__volatile__|
4444773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4445773647a0SAndy Whitcroft				asm|__asm__)$/x)
4446773647a0SAndy Whitcroft			{
4447c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4448c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4449c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4450c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4451773647a0SAndy Whitcroft
4452773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4453c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4454c2fdda0dSAndy Whitcroft
4455c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4456c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4457773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4458c2fdda0dSAndy Whitcroft
4459c2fdda0dSAndy Whitcroft			} else {
44603705ce5bSJoe Perches				if (WARN("SPACING",
44613705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
44623705ce5bSJoe Perches					     $fix) {
4463194f66fcSJoe Perches					$fixed[$fixlinenr] =~
44643705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
44653705ce5bSJoe Perches				}
4466f0a594c1SAndy Whitcroft			}
44676c72ffaaSAndy Whitcroft		}
44689a4cad4eSEric Nelson
4469653d4876SAndy Whitcroft# Check operator spacing.
44700a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
44713705ce5bSJoe Perches			my $fixed_line = "";
44723705ce5bSJoe Perches			my $line_fixed = 0;
44733705ce5bSJoe Perches
44749c0ca6f9SAndy Whitcroft			my $ops = qr{
44759c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
44769c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
44779c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
44781f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
447984731623SJoe Perches				\?:|\?|:
44809c0ca6f9SAndy Whitcroft			}x;
4481cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
44823705ce5bSJoe Perches
44833705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
44843705ce5bSJoe Perches##			foreach my $el (@elements) {
44853705ce5bSJoe Perches##				print("el: <$el>\n");
44863705ce5bSJoe Perches##			}
44873705ce5bSJoe Perches
44883705ce5bSJoe Perches			my @fix_elements = ();
448900df344fSAndy Whitcroft			my $off = 0;
44906c72ffaaSAndy Whitcroft
44913705ce5bSJoe Perches			foreach my $el (@elements) {
44923705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
44933705ce5bSJoe Perches				$off += length($el);
44943705ce5bSJoe Perches			}
44953705ce5bSJoe Perches
44963705ce5bSJoe Perches			$off = 0;
44973705ce5bSJoe Perches
44986c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4499b34c648bSJoe Perches			my $last_after = -1;
45006c72ffaaSAndy Whitcroft
45010a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
45023705ce5bSJoe Perches
45033705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
45043705ce5bSJoe Perches
45053705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
45063705ce5bSJoe Perches
45074a0df2efSAndy Whitcroft				$off += length($elements[$n]);
45084a0df2efSAndy Whitcroft
450925985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4510773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4511773647a0SAndy Whitcroft				my $cc = '';
4512773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4513773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4514773647a0SAndy Whitcroft				}
4515773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4516773647a0SAndy Whitcroft
45174a0df2efSAndy Whitcroft				my $a = '';
45184a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
45194a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4520cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
45214a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
45224a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4523773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
45244a0df2efSAndy Whitcroft
45250a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
45264a0df2efSAndy Whitcroft
45274a0df2efSAndy Whitcroft				my $c = '';
45280a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
45294a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
45304a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4531cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
45324a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
45334a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
45348b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
45354a0df2efSAndy Whitcroft				} else {
45364a0df2efSAndy Whitcroft					$c = 'E';
45370a920b5bSAndy Whitcroft				}
45380a920b5bSAndy Whitcroft
45394a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
45404a0df2efSAndy Whitcroft
45414a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
45424a0df2efSAndy Whitcroft
45436c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4544de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
45450a920b5bSAndy Whitcroft
454674048ed8SAndy Whitcroft				# Pull out the value of this operator.
45476c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
45480a920b5bSAndy Whitcroft
45491f65f947SAndy Whitcroft				# Get the full operator variant.
45501f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
45511f65f947SAndy Whitcroft
455213214adfSAndy Whitcroft				# Ignore operators passed as parameters.
455313214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4554d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
455513214adfSAndy Whitcroft
4556cf655043SAndy Whitcroft#				# Ignore comments
4557cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
455813214adfSAndy Whitcroft
4559d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
456013214adfSAndy Whitcroft				} elsif ($op eq ';') {
4561cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4562cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
45633705ce5bSJoe Perches						if (ERROR("SPACING",
45643705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4565b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
45663705ce5bSJoe Perches							$line_fixed = 1;
45673705ce5bSJoe Perches						}
4568d8aaf121SAndy Whitcroft					}
4569d8aaf121SAndy Whitcroft
4570d8aaf121SAndy Whitcroft				# // is a comment
4571d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
45720a920b5bSAndy Whitcroft
4573b00e4814SJoe Perches				#   :   when part of a bitfield
4574b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4575b00e4814SJoe Perches					# skip the bitfield test for now
4576b00e4814SJoe Perches
45771f65f947SAndy Whitcroft				# No spaces for:
45781f65f947SAndy Whitcroft				#   ->
4579b00e4814SJoe Perches				} elsif ($op eq '->') {
45804a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
45813705ce5bSJoe Perches						if (ERROR("SPACING",
45823705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4583b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45843705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45853705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
45863705ce5bSJoe Perches							}
4587b34c648bSJoe Perches							$line_fixed = 1;
45883705ce5bSJoe Perches						}
45890a920b5bSAndy Whitcroft					}
45900a920b5bSAndy Whitcroft
45912381097bSJoe Perches				# , must not have a space before and must have a space on the right.
45920a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
45932381097bSJoe Perches					my $rtrim_before = 0;
45942381097bSJoe Perches					my $space_after = 0;
45952381097bSJoe Perches					if ($ctx =~ /Wx./) {
45962381097bSJoe Perches						if (ERROR("SPACING",
45972381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
45982381097bSJoe Perches							$line_fixed = 1;
45992381097bSJoe Perches							$rtrim_before = 1;
46002381097bSJoe Perches						}
46012381097bSJoe Perches					}
4602cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
46033705ce5bSJoe Perches						if (ERROR("SPACING",
46043705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
46053705ce5bSJoe Perches							$line_fixed = 1;
4606b34c648bSJoe Perches							$last_after = $n;
46072381097bSJoe Perches							$space_after = 1;
46082381097bSJoe Perches						}
46092381097bSJoe Perches					}
46102381097bSJoe Perches					if ($rtrim_before || $space_after) {
46112381097bSJoe Perches						if ($rtrim_before) {
46122381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
46132381097bSJoe Perches						} else {
46142381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
46152381097bSJoe Perches						}
46162381097bSJoe Perches						if ($space_after) {
46172381097bSJoe Perches							$good .= " ";
46183705ce5bSJoe Perches						}
46190a920b5bSAndy Whitcroft					}
46200a920b5bSAndy Whitcroft
46219c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
462274048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
46239c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
46249c0ca6f9SAndy Whitcroft
46259c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
46269c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
46279c0ca6f9SAndy Whitcroft				# unary operator, or a cast
46289c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
462974048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
46300d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4631cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
46323705ce5bSJoe Perches						if (ERROR("SPACING",
46333705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4634b34c648bSJoe Perches							if ($n != $last_after + 2) {
4635b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
46363705ce5bSJoe Perches								$line_fixed = 1;
46373705ce5bSJoe Perches							}
46380a920b5bSAndy Whitcroft						}
4639b34c648bSJoe Perches					}
4640a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4641171ae1a4SAndy Whitcroft						# A unary '*' may be const
4642171ae1a4SAndy Whitcroft
4643171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
46443705ce5bSJoe Perches						if (ERROR("SPACING",
46453705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4646b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
46473705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
46483705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
46493705ce5bSJoe Perches							}
4650b34c648bSJoe Perches							$line_fixed = 1;
46513705ce5bSJoe Perches						}
46520a920b5bSAndy Whitcroft					}
46530a920b5bSAndy Whitcroft
46540a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
46550a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4656773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
46573705ce5bSJoe Perches						if (ERROR("SPACING",
46583705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4659b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
46603705ce5bSJoe Perches							$line_fixed = 1;
46613705ce5bSJoe Perches						}
46620a920b5bSAndy Whitcroft					}
4663773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4664773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
46653705ce5bSJoe Perches						if (ERROR("SPACING",
46663705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4667b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
46683705ce5bSJoe Perches							$line_fixed = 1;
46693705ce5bSJoe Perches						}
4670653d4876SAndy Whitcroft					}
4671773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
46723705ce5bSJoe Perches						if (ERROR("SPACING",
46733705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4674b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
46753705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
46763705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4677773647a0SAndy Whitcroft							}
4678b34c648bSJoe Perches							$line_fixed = 1;
46793705ce5bSJoe Perches						}
46803705ce5bSJoe Perches					}
46810a920b5bSAndy Whitcroft
46820a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
46839c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
46849c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
46859c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4686c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4687c2fdda0dSAndy Whitcroft					 $op eq '%')
46880a920b5bSAndy Whitcroft				{
4689d2e025f3SJoe Perches					if ($check) {
4690d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4691d2e025f3SJoe Perches							if (CHK("SPACING",
4692d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4693d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4694d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4695d2e025f3SJoe Perches								$line_fixed = 1;
4696d2e025f3SJoe Perches							}
4697d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4698d2e025f3SJoe Perches							if (CHK("SPACING",
4699d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4700d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4701d2e025f3SJoe Perches								$line_fixed = 1;
4702d2e025f3SJoe Perches							}
4703d2e025f3SJoe Perches						}
4704d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
47053705ce5bSJoe Perches						if (ERROR("SPACING",
47063705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4707b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4708b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4709b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4710b34c648bSJoe Perches							}
47113705ce5bSJoe Perches							$line_fixed = 1;
47123705ce5bSJoe Perches						}
47130a920b5bSAndy Whitcroft					}
47140a920b5bSAndy Whitcroft
47151f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
47161f65f947SAndy Whitcroft				# terminating a case value or a label.
47171f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
47181f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
47193705ce5bSJoe Perches						if (ERROR("SPACING",
47203705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4721b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
47223705ce5bSJoe Perches							$line_fixed = 1;
47233705ce5bSJoe Perches						}
47241f65f947SAndy Whitcroft					}
47251f65f947SAndy Whitcroft
47260a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4727cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
47281f65f947SAndy Whitcroft					my $ok = 0;
47291f65f947SAndy Whitcroft
473022f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
47311f65f947SAndy Whitcroft					if (($op eq '<' &&
47321f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
47331f65f947SAndy Whitcroft					    ($op eq '>' &&
47341f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
47351f65f947SAndy Whitcroft					{
47361f65f947SAndy Whitcroft						$ok = 1;
47371f65f947SAndy Whitcroft					}
47381f65f947SAndy Whitcroft
4739e0df7e1fSJoe Perches					# for asm volatile statements
4740e0df7e1fSJoe Perches					# ignore a colon with another
4741e0df7e1fSJoe Perches					# colon immediately before or after
4742e0df7e1fSJoe Perches					if (($op eq ':') &&
4743e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4744e0df7e1fSJoe Perches						$ok = 1;
4745e0df7e1fSJoe Perches					}
4746e0df7e1fSJoe Perches
474784731623SJoe Perches					# messages are ERROR, but ?: are CHK
47481f65f947SAndy Whitcroft					if ($ok == 0) {
47490675a8fbSJean Delvare						my $msg_level = \&ERROR;
47500675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
475184731623SJoe Perches
47520675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
47533705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4754b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4755b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4756b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4757b34c648bSJoe Perches							}
47583705ce5bSJoe Perches							$line_fixed = 1;
47593705ce5bSJoe Perches						}
47600a920b5bSAndy Whitcroft					}
476122f2a2efSAndy Whitcroft				}
47624a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
47633705ce5bSJoe Perches
47643705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
47653705ce5bSJoe Perches
47663705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
47670a920b5bSAndy Whitcroft			}
47683705ce5bSJoe Perches
47693705ce5bSJoe Perches			if (($#elements % 2) == 0) {
47703705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
47713705ce5bSJoe Perches			}
47723705ce5bSJoe Perches
4773194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4774194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
47753705ce5bSJoe Perches			}
47763705ce5bSJoe Perches
47773705ce5bSJoe Perches
47780a920b5bSAndy Whitcroft		}
47790a920b5bSAndy Whitcroft
4780786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4781d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4782786b6326SJoe Perches			if (WARN("SPACING",
4783786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4784786b6326SJoe Perches			    $fix) {
4785194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4786786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4787786b6326SJoe Perches			}
4788786b6326SJoe Perches		}
4789786b6326SJoe Perches
4790f0a594c1SAndy Whitcroft# check for multiple assignments
4791f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4792000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4793000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4794f0a594c1SAndy Whitcroft		}
4795f0a594c1SAndy Whitcroft
479622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
479722f2a2efSAndy Whitcroft## # continuation.
479822f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
479922f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
480022f2a2efSAndy Whitcroft##
480122f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
480222f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
480322f2a2efSAndy Whitcroft## 			my $ln = $line;
480422f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
480522f2a2efSAndy Whitcroft## 			}
480622f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4807000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4808000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
480922f2a2efSAndy Whitcroft## 			}
481022f2a2efSAndy Whitcroft## 		}
4811f0a594c1SAndy Whitcroft
48120a920b5bSAndy Whitcroft#need space before brace following if, while, etc
48136b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
48146ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
48153705ce5bSJoe Perches			if (ERROR("SPACING",
48163705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
48173705ce5bSJoe Perches			    $fix) {
48186ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
48193705ce5bSJoe Perches			}
4820de7d4f0eSAndy Whitcroft		}
4821de7d4f0eSAndy Whitcroft
4822c4a62ef9SJoe Perches## # check for blank lines before declarations
4823c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4824c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4825c4a62ef9SJoe Perches##			WARN("SPACING",
4826c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4827c4a62ef9SJoe Perches##		}
4828c4a62ef9SJoe Perches##
4829c4a62ef9SJoe Perches
4830de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4831de7d4f0eSAndy Whitcroft# on the line
483294fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4833d5e616fcSJoe Perches			if (ERROR("SPACING",
4834d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4835d5e616fcSJoe Perches			    $fix) {
4836194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4837d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4838d5e616fcSJoe Perches			}
48390a920b5bSAndy Whitcroft		}
48400a920b5bSAndy Whitcroft
484122f2a2efSAndy Whitcroft# check spacing on square brackets
484222f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
48433705ce5bSJoe Perches			if (ERROR("SPACING",
48443705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
48453705ce5bSJoe Perches			    $fix) {
4846194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48473705ce5bSJoe Perches				    s/\[\s+/\[/;
48483705ce5bSJoe Perches			}
484922f2a2efSAndy Whitcroft		}
485022f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
48513705ce5bSJoe Perches			if (ERROR("SPACING",
48523705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
48533705ce5bSJoe Perches			    $fix) {
4854194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48553705ce5bSJoe Perches				    s/\s+\]/\]/;
48563705ce5bSJoe Perches			}
485722f2a2efSAndy Whitcroft		}
485822f2a2efSAndy Whitcroft
4859c45dcabdSAndy Whitcroft# check spacing on parentheses
48609c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
48619c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
48623705ce5bSJoe Perches			if (ERROR("SPACING",
48633705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
48643705ce5bSJoe Perches			    $fix) {
4865194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48663705ce5bSJoe Perches				    s/\(\s+/\(/;
48673705ce5bSJoe Perches			}
486822f2a2efSAndy Whitcroft		}
486913214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4870c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4871c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
48723705ce5bSJoe Perches			if (ERROR("SPACING",
48733705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
48743705ce5bSJoe Perches			    $fix) {
4875194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48763705ce5bSJoe Perches				    s/\s+\)/\)/;
48773705ce5bSJoe Perches			}
487822f2a2efSAndy Whitcroft		}
487922f2a2efSAndy Whitcroft
4880e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4881e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4882e2826fd0SJoe Perches
4883e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4884ea4acbb1SJoe Perches			my $var = $1;
4885ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4886ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4887ea4acbb1SJoe Perches			    $fix) {
4888ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4889ea4acbb1SJoe Perches			}
4890ea4acbb1SJoe Perches		}
4891ea4acbb1SJoe Perches
4892ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4893ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4894ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4895ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4896ea4acbb1SJoe Perches			my $var = $2;
4897ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4898ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4899ea4acbb1SJoe Perches			    $fix) {
4900ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4901ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4902ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4903ea4acbb1SJoe Perches			}
4904e2826fd0SJoe Perches		}
4905e2826fd0SJoe Perches
490663b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4907a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4908a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
49095b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
491063b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
491163b7c73eSJoe Perches			my $if_stat = $1;
491263b7c73eSJoe Perches			my $test = substr($2, 1, -1);
491363b7c73eSJoe Perches			my $herectx;
491463b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
491563b7c73eSJoe Perches				my $match = $1;
491663b7c73eSJoe Perches				# avoid parentheses around potential macro args
491763b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
491863b7c73eSJoe Perches				if (!defined($herectx)) {
491963b7c73eSJoe Perches					$herectx = $here . "\n";
492063b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
492163b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
492263b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
492363b7c73eSJoe Perches						$herectx .=  $rl . "\n";
492463b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
492563b7c73eSJoe Perches					}
492663b7c73eSJoe Perches				}
492763b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
492863b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
492963b7c73eSJoe Perches			}
493063b7c73eSJoe Perches		}
493163b7c73eSJoe Perches
49320a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
49334a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
49340a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
49353705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
49363705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
49373705ce5bSJoe Perches			    $fix) {
4938194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49393705ce5bSJoe Perches				    s/^(.)\s+/$1/;
49403705ce5bSJoe Perches			}
49410a920b5bSAndy Whitcroft		}
49420a920b5bSAndy Whitcroft
49435b9553abSJoe Perches# return is not a function
4944507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4945c45dcabdSAndy Whitcroft			my $spacing = $1;
49465b57980dSJoe Perches			if ($perl_version_ok &&
49475b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
49485b9553abSJoe Perches				my $value = $1;
49495b9553abSJoe Perches				$value = deparenthesize($value);
49505b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4951000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4952000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
49535b9553abSJoe Perches				}
4954c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4955000d1cc1SJoe Perches				ERROR("SPACING",
4956000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4957c45dcabdSAndy Whitcroft			}
4958c45dcabdSAndy Whitcroft		}
4959507e5141SJoe Perches
4960b43ae21bSJoe Perches# unnecessary return in a void function
4961b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4962b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4963b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4964b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4965b43ae21bSJoe Perches		    $linenr >= 3 &&
4966b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4967b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
49689819cf25SJoe Perches			WARN("RETURN_VOID",
4969b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
49709819cf25SJoe Perches               }
49719819cf25SJoe Perches
4972189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
49735b57980dSJoe Perches		if ($perl_version_ok &&
4974189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4975189248d8SJoe Perches			my $openparens = $1;
4976189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4977189248d8SJoe Perches			my $msg = "";
4978189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4979189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4980189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4981189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4982189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4983189248d8SJoe Perches			}
4984189248d8SJoe Perches		}
4985189248d8SJoe Perches
4986c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4987c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4988c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4989c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
49905b57980dSJoe Perches		if ($perl_version_ok &&
4991c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4992c5595fa2SJoe Perches			my $lead = $1;
4993c5595fa2SJoe Perches			my $const = $2;
4994c5595fa2SJoe Perches			my $comp = $3;
4995c5595fa2SJoe Perches			my $to = $4;
4996c5595fa2SJoe Perches			my $newcomp = $comp;
4997f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4998c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4999c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5000c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5001c5595fa2SJoe Perches			    $fix) {
5002c5595fa2SJoe Perches				if ($comp eq "<") {
5003c5595fa2SJoe Perches					$newcomp = ">";
5004c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5005c5595fa2SJoe Perches					$newcomp = ">=";
5006c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5007c5595fa2SJoe Perches					$newcomp = "<";
5008c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5009c5595fa2SJoe Perches					$newcomp = "<=";
5010c5595fa2SJoe Perches				}
5011c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5012c5595fa2SJoe Perches			}
5013c5595fa2SJoe Perches		}
5014c5595fa2SJoe Perches
5015f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5016f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
501753a3c448SAndy Whitcroft			my $name = $1;
501853a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
5019000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5020f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
502153a3c448SAndy Whitcroft			}
502253a3c448SAndy Whitcroft		}
5023c45dcabdSAndy Whitcroft
50240a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
50254a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
50263705ce5bSJoe Perches			if (ERROR("SPACING",
50273705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
50283705ce5bSJoe Perches			    $fix) {
5029194f66fcSJoe Perches				$fixed[$fixlinenr] =~
50303705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
50313705ce5bSJoe Perches			}
50320a920b5bSAndy Whitcroft		}
50330a920b5bSAndy Whitcroft
5034f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5035f5fe35ddSAndy Whitcroft# statements after the conditional.
5036170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
50373e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
50383e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
50393e469cdcSAndy Whitcroft					if (!defined $stat);
5040170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5041170d3a22SAndy Whitcroft						$remain_next, $off_next);
5042170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5043170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5044170d3a22SAndy Whitcroft
5045170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5046170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5047170d3a22SAndy Whitcroft				# then count those as offsets.
5048170d3a22SAndy Whitcroft				my ($whitespace) =
5049170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5050170d3a22SAndy Whitcroft				my $offset =
5051170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5052170d3a22SAndy Whitcroft
5053170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5054170d3a22SAndy Whitcroft								$offset} = 1;
5055170d3a22SAndy Whitcroft			}
5056170d3a22SAndy Whitcroft		}
5057170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5058c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5059170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5060171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
50618905a67cSAndy Whitcroft
5062b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
506365b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
506465b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
506565b64b3bSJoe Perches				    $fix && $perl_version_ok) {
506665b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
506765b64b3bSJoe Perches						my $space = $1;
506865b64b3bSJoe Perches						my $not = $2;
506965b64b3bSJoe Perches						my $statement = $3;
507065b64b3bSJoe Perches						my $assigned = $4;
507165b64b3bSJoe Perches						my $test = $8;
507265b64b3bSJoe Perches						my $against = $9;
507365b64b3bSJoe Perches						my $brace = $15;
507465b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
507565b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
507665b64b3bSJoe Perches						my $newline = "${space}if (";
507765b64b3bSJoe Perches						$newline .= '!' if defined($not);
507865b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
507965b64b3bSJoe Perches						$newline .= "$assigned";
508065b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
508165b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
508265b64b3bSJoe Perches						$newline .= ')';
508365b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
508465b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
508565b64b3bSJoe Perches					}
508665b64b3bSJoe Perches				}
50878905a67cSAndy Whitcroft			}
50888905a67cSAndy Whitcroft
50898905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
50908905a67cSAndy Whitcroft			# conditional.
5091773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
50928905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
509313214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
509453210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
509553210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5096773647a0SAndy Whitcroft			{
5097bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5098bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5099bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
510042bdf74cSHidetoshi Seto				my $stat_real = '';
5101bb44ad39SAndy Whitcroft
510242bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
510342bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5104bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5105bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5106bb44ad39SAndy Whitcroft				}
5107bb44ad39SAndy Whitcroft
5108000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5109000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
51108905a67cSAndy Whitcroft			}
51118905a67cSAndy Whitcroft		}
51128905a67cSAndy Whitcroft
511313214adfSAndy Whitcroft# Check for bitwise tests written as boolean
511413214adfSAndy Whitcroft		if ($line =~ /
511513214adfSAndy Whitcroft			(?:
511613214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
511713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
511813214adfSAndy Whitcroft				(?:\&\&|\|\|)
511913214adfSAndy Whitcroft			|
512013214adfSAndy Whitcroft				(?:\&\&|\|\|)
512113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
512213214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
512313214adfSAndy Whitcroft			)/x)
512413214adfSAndy Whitcroft		{
5125000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5126000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
512713214adfSAndy Whitcroft		}
512813214adfSAndy Whitcroft
51298905a67cSAndy Whitcroft# if and else should not have general statements after it
513013214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
513113214adfSAndy Whitcroft			my $s = $1;
513213214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
513313214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5134000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5135000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
51360a920b5bSAndy Whitcroft			}
513713214adfSAndy Whitcroft		}
513839667782SAndy Whitcroft# if should not continue a brace
513939667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5140000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5141048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
514239667782SAndy Whitcroft				$herecurr);
514339667782SAndy Whitcroft		}
5144a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5145a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5146a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
51473fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5148a1080bf8SAndy Whitcroft			\s*return\s+
5149a1080bf8SAndy Whitcroft		    )/xg)
5150a1080bf8SAndy Whitcroft		{
5151000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5152000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5153a1080bf8SAndy Whitcroft		}
51540a920b5bSAndy Whitcroft
51550a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
51560a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
51578b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
51580a920b5bSAndy Whitcroft		    $previndent == $indent) {
51598b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
51608b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
51618b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
51628b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
51638b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
51648b8856f4SJoe Perches				my $fixedline = $prevrawline;
51658b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
51668b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
51678b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
51688b8856f4SJoe Perches				}
51698b8856f4SJoe Perches				$fixedline = $rawline;
51708b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
51718b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
51728b8856f4SJoe Perches			}
51730a920b5bSAndy Whitcroft		}
51740a920b5bSAndy Whitcroft
51758b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5176c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5177c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5178c2fdda0dSAndy Whitcroft
5179c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5180c2fdda0dSAndy Whitcroft			# conditional.
5181773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5182c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5183c2fdda0dSAndy Whitcroft
5184c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
51858b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
51868b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
51878b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
51888b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
51898b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
51908b8856f4SJoe Perches					my $fixedline = $prevrawline;
51918b8856f4SJoe Perches					my $trailing = $rawline;
51928b8856f4SJoe Perches					$trailing =~ s/^\+//;
51938b8856f4SJoe Perches					$trailing = trim($trailing);
51948b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
51958b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
51968b8856f4SJoe Perches				}
5197c2fdda0dSAndy Whitcroft			}
5198c2fdda0dSAndy Whitcroft		}
5199c2fdda0dSAndy Whitcroft
520095e2c602SJoe Perches#Specific variable tests
5201323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5202323c1260SJoe Perches			my $var = $1;
520395e2c602SJoe Perches
520495e2c602SJoe Perches#CamelCase
5205807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5206be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
520722735ce8SJoe Perches#Ignore Page<foo> variants
5208807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5209d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5210d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5211d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5212f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5213f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
52147e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
52157e781f67SJoe Perches					my $word = $1;
52167e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5217d8b07710SJoe Perches					if ($check) {
5218d8b07710SJoe Perches						seed_camelcase_includes();
5219d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5220d8b07710SJoe Perches							seed_camelcase_file($realfile);
5221d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5222d8b07710SJoe Perches						}
5223d8b07710SJoe Perches					}
52247e781f67SJoe Perches					if (!defined $camelcase{$word}) {
52257e781f67SJoe Perches						$camelcase{$word} = 1;
5226be79794bSJoe Perches						CHK("CAMELCASE",
52277e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
52287e781f67SJoe Perches					}
5229323c1260SJoe Perches				}
5230323c1260SJoe Perches			}
52313445686aSJoe Perches		}
52320a920b5bSAndy Whitcroft
52330a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5234d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5235d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5236d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5237d5e616fcSJoe Perches			    $fix) {
5238194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5239d5e616fcSJoe Perches			}
52400a920b5bSAndy Whitcroft		}
52410a920b5bSAndy Whitcroft
52420e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
52430e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5244c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5245e09dec48SAndy Whitcroft			my $file = "$1.h";
5246e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5247e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5248e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
52497840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5250c45dcabdSAndy Whitcroft			{
52510e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
52520e212e0aSFabian Frederick				if ($asminclude > 0) {
5253e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5254000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5255000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5256e09dec48SAndy Whitcroft					} else {
5257000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5258000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5259e09dec48SAndy Whitcroft					}
52600a920b5bSAndy Whitcroft				}
52610a920b5bSAndy Whitcroft			}
52620e212e0aSFabian Frederick		}
52630a920b5bSAndy Whitcroft
5264653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5265653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5266cf655043SAndy Whitcroft# in a known good container
5267b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5268b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5269d8aaf121SAndy Whitcroft			my $ln = $linenr;
5270d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5271c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5272c45dcabdSAndy Whitcroft			my $ctx = '';
527308a2843eSJoe Perches			my $has_flow_statement = 0;
527408a2843eSJoe Perches			my $has_arg_concat = 0;
5275c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5276f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5277f74bd194SAndy Whitcroft			$ctx = $dstat;
5278c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5279a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5280c45dcabdSAndy Whitcroft
528108a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
528262e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
528308a2843eSJoe Perches
5284f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5285f59b64bfSJoe Perches			my $define_args = $1;
5286f59b64bfSJoe Perches			my $define_stmt = $dstat;
5287f59b64bfSJoe Perches			my @def_args = ();
5288f59b64bfSJoe Perches
5289f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5290f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5291f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
52928c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5293f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5294f59b64bfSJoe Perches			}
5295f59b64bfSJoe Perches
5296292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5297c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5298c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5299c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5300c45dcabdSAndy Whitcroft
5301c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5302bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5303bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
53046b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5305bf30d6edSAndy Whitcroft			{
5306c45dcabdSAndy Whitcroft			}
5307c45dcabdSAndy Whitcroft
5308342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
530933acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
531033acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5311e45bab8eSAndy Whitcroft			{
5312e45bab8eSAndy Whitcroft			}
5313e45bab8eSAndy Whitcroft
531442e15293SJoe Perches			# Make asm volatile uses seem like a generic function
531542e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
531642e15293SJoe Perches
5317c45dcabdSAndy Whitcroft			my $exceptions = qr{
5318c45dcabdSAndy Whitcroft				$Declare|
5319c45dcabdSAndy Whitcroft				module_param_named|
5320a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5321c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5322c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5323383099fdSAndy Whitcroft				__typeof__\(|
532422fd2d3eSStefani Seibold				union|
532522fd2d3eSStefani Seibold				struct|
5326ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
53276b10df42SVladimir Zapolskiy				^\"|\"$|
53286b10df42SVladimir Zapolskiy				^\[
5329c45dcabdSAndy Whitcroft			}x;
53305eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5331f59b64bfSJoe Perches
5332f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5333f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5334e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5335f59b64bfSJoe Perches
5336f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5337f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5338f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
53393cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5340356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5341f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5342f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5343e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
534472f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5345f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5346f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5347f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
53484e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5349f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5350c45dcabdSAndy Whitcroft			{
5351e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5352e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5353e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5354e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5355f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5356f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5357f74bd194SAndy Whitcroft				} else {
5358000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5359388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5360d8aaf121SAndy Whitcroft				}
5361f59b64bfSJoe Perches
5362f59b64bfSJoe Perches			}
53635207649bSJoe Perches
53645207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
53655207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
53665207649bSJoe Perches			my $first = 1;
53675207649bSJoe Perches			$define_stmt = "";
53685207649bSJoe Perches			foreach my $l (@stmt_array) {
53695207649bSJoe Perches				$l =~ s/\\$//;
53705207649bSJoe Perches				if ($first) {
53715207649bSJoe Perches					$define_stmt = $l;
53725207649bSJoe Perches					$first = 0;
53735207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
53745207649bSJoe Perches					$define_stmt .= substr($l, 1);
53755207649bSJoe Perches				}
53765207649bSJoe Perches			}
53775207649bSJoe Perches			$define_stmt =~ s/$;//g;
53785207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
53795207649bSJoe Perches			$define_stmt = trim($define_stmt);
53805207649bSJoe Perches
5381f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5382f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5383f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
53849192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
53857fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
53866dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
53877fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
53887fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5389d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5390f59b64bfSJoe Perches				if ($use_cnt > 1) {
5391f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5392f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5393f59b64bfSJoe Perches				    }
53949192d41aSJoe Perches# check if any macro arguments may have other precedence issues
53957fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
53969192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
53979192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
53989192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
53999192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
54009192d41aSJoe Perches				}
54010a920b5bSAndy Whitcroft			}
54025023d347SJoe Perches
540308a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
540408a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
540508a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
540608a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5407e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
540808a2843eSJoe Perches
540908a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
541008a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
541108a2843eSJoe Perches			}
541208a2843eSJoe Perches
5413481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
54145023d347SJoe Perches
54155023d347SJoe Perches		} else {
54165023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5417481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5418481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
54195023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
54205023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
54215023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
54225023d347SJoe Perches			}
5423653d4876SAndy Whitcroft		}
54240a920b5bSAndy Whitcroft
5425b13edf7fSJoe Perches# do {} while (0) macro tests:
5426b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5427b13edf7fSJoe Perches# macro should not end with a semicolon
54285b57980dSJoe Perches		if ($perl_version_ok &&
5429b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5430b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5431b13edf7fSJoe Perches			my $ln = $linenr;
5432b13edf7fSJoe Perches			my $cnt = $realcnt;
5433b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5434b13edf7fSJoe Perches			my $ctx = '';
5435b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5436b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5437b13edf7fSJoe Perches			$ctx = $dstat;
5438b13edf7fSJoe Perches
5439b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
54401b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5441b13edf7fSJoe Perches
5442b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5443b13edf7fSJoe Perches				my $stmts = $2;
5444b13edf7fSJoe Perches				my $semis = $3;
5445b13edf7fSJoe Perches
5446b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5447b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5448e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5449b13edf7fSJoe Perches
5450ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5451ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5452b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5453b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5454b13edf7fSJoe Perches				}
5455b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5456b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5457b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5458b13edf7fSJoe Perches				}
5459f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5460f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5461f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5462e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5463f5ef95b1SJoe Perches
5464f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5465f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5466b13edf7fSJoe Perches			}
5467b13edf7fSJoe Perches		}
5468b13edf7fSJoe Perches
5469f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
547013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
547113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5472cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
547313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5474cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5475cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5476aad4f614SJoe Perches				my @allowed = ();
5477aad4f614SJoe Perches				my $allow = 0;
547813214adfSAndy Whitcroft				my $seen = 0;
5479773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5480cf655043SAndy Whitcroft				my $ln = $linenr - 1;
548113214adfSAndy Whitcroft				for my $chunk (@chunks) {
548213214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
548313214adfSAndy Whitcroft
5484773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5485773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5486773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5487773647a0SAndy Whitcroft
5488aad4f614SJoe Perches					$allowed[$allow] = 0;
5489773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5490773647a0SAndy Whitcroft
5491773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5492773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5493773647a0SAndy Whitcroft
5494773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5495cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5496cf655043SAndy Whitcroft
5497773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
549813214adfSAndy Whitcroft
549913214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
550013214adfSAndy Whitcroft
5501aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5502cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5503cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5504aad4f614SJoe Perches						$allowed[$allow] = 1;
550513214adfSAndy Whitcroft					}
550613214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5507cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5508aad4f614SJoe Perches						$allowed[$allow] = 1;
550913214adfSAndy Whitcroft					}
5510cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5511cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5512aad4f614SJoe Perches						$allowed[$allow] = 1;
551313214adfSAndy Whitcroft					}
5514aad4f614SJoe Perches					$allow++;
551513214adfSAndy Whitcroft				}
5516aad4f614SJoe Perches				if ($seen) {
5517aad4f614SJoe Perches					my $sum_allowed = 0;
5518aad4f614SJoe Perches					foreach (@allowed) {
5519aad4f614SJoe Perches						$sum_allowed += $_;
5520aad4f614SJoe Perches					}
5521aad4f614SJoe Perches					if ($sum_allowed == 0) {
5522000d1cc1SJoe Perches						WARN("BRACES",
5523000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5524aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5525aad4f614SJoe Perches						 $seen != $allow) {
5526aad4f614SJoe Perches						CHK("BRACES",
5527aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5528aad4f614SJoe Perches					}
552913214adfSAndy Whitcroft				}
553013214adfSAndy Whitcroft			}
553113214adfSAndy Whitcroft		}
5532773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
553313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5534cf655043SAndy Whitcroft			my $allowed = 0;
5535f0a594c1SAndy Whitcroft
5536cf655043SAndy Whitcroft			# Check the pre-context.
5537cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5538cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5539cf655043SAndy Whitcroft				$allowed = 1;
5540f0a594c1SAndy Whitcroft			}
5541773647a0SAndy Whitcroft
5542773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5543773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5544773647a0SAndy Whitcroft
5545cf655043SAndy Whitcroft			# Check the condition.
5546cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5547773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5548cf655043SAndy Whitcroft			if (defined $cond) {
5549773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5550cf655043SAndy Whitcroft			}
5551cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5552cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5553cf655043SAndy Whitcroft				$allowed = 1;
5554cf655043SAndy Whitcroft			}
5555cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5556cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5557cf655043SAndy Whitcroft				$allowed = 1;
5558cf655043SAndy Whitcroft			}
5559cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5560cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5561cf655043SAndy Whitcroft				$allowed = 1;
5562cf655043SAndy Whitcroft			}
5563cf655043SAndy Whitcroft			# Check the post-context.
5564cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5565cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5566cf655043SAndy Whitcroft				if (defined $cond) {
5567773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5568cf655043SAndy Whitcroft				}
5569cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5570cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5571cf655043SAndy Whitcroft					$allowed = 1;
5572cf655043SAndy Whitcroft				}
5573cf655043SAndy Whitcroft			}
5574cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5575f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5576e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5577cf655043SAndy Whitcroft
5578000d1cc1SJoe Perches				WARN("BRACES",
5579000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5580f0a594c1SAndy Whitcroft			}
5581f0a594c1SAndy Whitcroft		}
5582f0a594c1SAndy Whitcroft
5583e4c5babdSJoe Perches# check for single line unbalanced braces
558495330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
558595330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5586e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5587e4c5babdSJoe Perches		}
5588e4c5babdSJoe Perches
55890979ae66SJoe Perches# check for unnecessary blank lines around braces
559077b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5591f8e58219SJoe Perches			if (CHK("BRACES",
5592f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5593f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5594f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5595f8e58219SJoe Perches			}
55960979ae66SJoe Perches		}
559777b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5598f8e58219SJoe Perches			if (CHK("BRACES",
5599f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5600f8e58219SJoe Perches			    $fix) {
5601f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5602f8e58219SJoe Perches			}
56030979ae66SJoe Perches		}
56040979ae66SJoe Perches
56054a0df2efSAndy Whitcroft# no volatiles please
56066c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
56076c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5608000d1cc1SJoe Perches			WARN("VOLATILE",
56098c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
56104a0df2efSAndy Whitcroft		}
56114a0df2efSAndy Whitcroft
56125e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
56135e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
56145e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
56155e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
561633acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
56175e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
56185e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
56195e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
56205e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
56215e4f6ba5SJoe Perches				     $fix &&
56225e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
56235e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
56245e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
56255e4f6ba5SJoe Perches				my $comma_close = "";
56265e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
56275e4f6ba5SJoe Perches					$comma_close = $1;
56285e4f6ba5SJoe Perches				}
56295e4f6ba5SJoe Perches
56305e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
56315e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
56325e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
56335e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
56345e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
56355e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
56365e4f6ba5SJoe Perches				$fixedline = $rawline;
56375e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
56385e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
56395e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
56405e4f6ba5SJoe Perches				}
56415e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
56425e4f6ba5SJoe Perches			}
56435e4f6ba5SJoe Perches		}
56445e4f6ba5SJoe Perches
56455e4f6ba5SJoe Perches# check for missing a space in a string concatenation
56465e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
56475e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
56485e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
56495e4f6ba5SJoe Perches		}
56505e4f6ba5SJoe Perches
565177cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5652e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5653e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5654e4b7d309SJoe Perches# function declarations
565577cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
565677cb8546SJoe Perches		    defined($context_function) &&
5657e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5658e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
565977cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5660e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
566177cb8546SJoe Perches		}
566277cb8546SJoe Perches
56635e4f6ba5SJoe Perches# check for spaces before a quoted newline
56645e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
56655e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
56665e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
56675e4f6ba5SJoe Perches			    $fix) {
56685e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
56695e4f6ba5SJoe Perches			}
56705e4f6ba5SJoe Perches
56715e4f6ba5SJoe Perches		}
56725e4f6ba5SJoe Perches
5673f17dba4fSJoe Perches# concatenated string without spaces between elements
567479682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
567579682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
567679682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
567779682c0cSJoe Perches			    $fix) {
567879682c0cSJoe Perches				while ($line =~ /($String)/g) {
567979682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
568079682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
568179682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
568279682c0cSJoe Perches				}
568379682c0cSJoe Perches			}
5684f17dba4fSJoe Perches		}
5685f17dba4fSJoe Perches
568690ad30e5SJoe Perches# uncoalesced string fragments
568733acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
568879682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
568979682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
569079682c0cSJoe Perches			    $fix) {
569179682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
569279682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
569379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
569479682c0cSJoe Perches				}
569579682c0cSJoe Perches			}
569690ad30e5SJoe Perches		}
569790ad30e5SJoe Perches
5698522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5699522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5700522b837cSAlexey Dobriyan		my $show_Z = 1;
57015e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5702522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
57035e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5704522b837cSAlexey Dobriyan			# check for %L
5705522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
57065e4f6ba5SJoe Perches				WARN("PRINTF_L",
5707522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5708522b837cSAlexey Dobriyan				$show_L = 0;
57095e4f6ba5SJoe Perches			}
5710522b837cSAlexey Dobriyan			# check for %Z
5711522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5712522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5713522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5714522b837cSAlexey Dobriyan				$show_Z = 0;
5715522b837cSAlexey Dobriyan			}
5716522b837cSAlexey Dobriyan			# check for 0x<decimal>
5717522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5718522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
57196e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
57206e300757SJoe Perches			}
57215e4f6ba5SJoe Perches		}
57225e4f6ba5SJoe Perches
57235e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
57243f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
57255e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
57265e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
57275e4f6ba5SJoe Perches		}
57285e4f6ba5SJoe Perches
572900df344fSAndy Whitcroft# warn about #if 0
5730c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
573160f89010SPrakruthi Deepak Heragu			WARN("IF_0",
573260f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
573360f89010SPrakruthi Deepak Heragu		}
573460f89010SPrakruthi Deepak Heragu
573560f89010SPrakruthi Deepak Heragu# warn about #if 1
573660f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
573760f89010SPrakruthi Deepak Heragu			WARN("IF_1",
573860f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
57394a0df2efSAndy Whitcroft		}
57404a0df2efSAndy Whitcroft
574103df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
574203df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5743100425deSJoe Perches			my $tested = quotemeta($1);
5744100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5745100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5746100425deSJoe Perches				my $func = $1;
5747100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5748100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5749100425deSJoe Perches				    $fix) {
5750100425deSJoe Perches					my $do_fix = 1;
5751100425deSJoe Perches					my $leading_tabs = "";
5752100425deSJoe Perches					my $new_leading_tabs = "";
5753100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5754100425deSJoe Perches						$leading_tabs = $1;
5755100425deSJoe Perches					} else {
5756100425deSJoe Perches						$do_fix = 0;
5757100425deSJoe Perches					}
5758100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5759100425deSJoe Perches						$new_leading_tabs = $1;
5760100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5761100425deSJoe Perches							$do_fix = 0;
5762100425deSJoe Perches						}
5763100425deSJoe Perches					} else {
5764100425deSJoe Perches						$do_fix = 0;
5765100425deSJoe Perches					}
5766100425deSJoe Perches					if ($do_fix) {
5767100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5768100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5769100425deSJoe Perches					}
5770100425deSJoe Perches				}
57714c432a8fSGreg Kroah-Hartman			}
57724c432a8fSGreg Kroah-Hartman		}
5773f0a594c1SAndy Whitcroft
5774ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5775ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5776ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5777ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5778ebfdc409SJoe Perches		    $linenr > 3) {
5779ebfdc409SJoe Perches			my $testval = $2;
5780ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5781ebfdc409SJoe Perches
5782ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5783ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5784ebfdc409SJoe Perches
5785e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5786e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5787ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5788ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5789ebfdc409SJoe Perches			}
5790ebfdc409SJoe Perches		}
5791ebfdc409SJoe Perches
5792f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5793dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5794f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5795f78d98f6SJoe Perches			my $level = $1;
5796f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5797f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5798f78d98f6SJoe Perches			    $fix) {
5799f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5800f78d98f6SJoe Perches			}
5801f78d98f6SJoe Perches		}
5802f78d98f6SJoe Perches
580345c55e92SJoe Perches# check for logging continuations
580445c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
580545c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
580645c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
580745c55e92SJoe Perches		}
580845c55e92SJoe Perches
5809abb08a53SJoe Perches# check for mask then right shift without a parentheses
58105b57980dSJoe Perches		if ($perl_version_ok &&
5811abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5812abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5813abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5814abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5815abb08a53SJoe Perches		}
5816abb08a53SJoe Perches
5817b75ac618SJoe Perches# check for pointer comparisons to NULL
58185b57980dSJoe Perches		if ($perl_version_ok) {
5819b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5820b75ac618SJoe Perches				my $val = $1;
5821b75ac618SJoe Perches				my $equal = "!";
5822b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5823b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5824b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5825b75ac618SJoe Perches					    $fix) {
5826b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5827b75ac618SJoe Perches				}
5828b75ac618SJoe Perches			}
5829b75ac618SJoe Perches		}
5830b75ac618SJoe Perches
58318716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
58328716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
58338716de38SJoe Perches			my $attr = $1;
58348716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
58358716de38SJoe Perches				my $ptr = $1;
58368716de38SJoe Perches				my $var = $2;
58378716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
58388716de38SJoe Perches				      ERROR("MISPLACED_INIT",
58398716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
58408716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
58418716de38SJoe Perches				      WARN("MISPLACED_INIT",
58428716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
58438716de38SJoe Perches				    $fix) {
5844194f66fcSJoe 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;
58458716de38SJoe Perches				}
58468716de38SJoe Perches			}
58478716de38SJoe Perches		}
58488716de38SJoe Perches
5849e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5850e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5851e970b884SJoe Perches			my $attr = $1;
5852e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5853e970b884SJoe Perches			my $attr_prefix = $1;
5854e970b884SJoe Perches			my $attr_type = $2;
5855e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5856e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5857e970b884SJoe Perches			    $fix) {
5858194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5859e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5860e970b884SJoe Perches			}
5861e970b884SJoe Perches		}
5862e970b884SJoe Perches
5863e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5864e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5865e970b884SJoe Perches			my $attr = $1;
5866e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5867e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5868e970b884SJoe Perches			    $fix) {
5869194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5870e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5871e970b884SJoe Perches				$lead = rtrim($1);
5872e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5873e970b884SJoe Perches				$lead = "${lead}const ";
5874194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5875e970b884SJoe Perches			}
5876e970b884SJoe Perches		}
5877e970b884SJoe Perches
5878c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5879c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5880c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5881c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5882c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5883c17893c7SJoe Perches			    $fix) {
5884c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5885c17893c7SJoe Perches			}
5886c17893c7SJoe Perches		}
5887c17893c7SJoe Perches
5888fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5889fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5890fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5891fbdb8138SJoe Perches			my $constant_func = $1;
5892fbdb8138SJoe Perches			my $func = $constant_func;
5893fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5894fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5895fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5896fbdb8138SJoe Perches			    $fix) {
5897194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5898fbdb8138SJoe Perches			}
5899fbdb8138SJoe Perches		}
5900fbdb8138SJoe Perches
59011a15a250SPatrick Pannuto# prefer usleep_range over udelay
590237581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
590343c1d77cSJoe Perches			my $delay = $1;
59041a15a250SPatrick Pannuto			# ignore udelay's < 10, however
590543c1d77cSJoe Perches			if (! ($delay < 10) ) {
5906000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5907458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
590843c1d77cSJoe Perches			}
590943c1d77cSJoe Perches			if ($delay > 2000) {
591043c1d77cSJoe Perches				WARN("LONG_UDELAY",
591143c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
59121a15a250SPatrick Pannuto			}
59131a15a250SPatrick Pannuto		}
59141a15a250SPatrick Pannuto
591509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
591609ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
591709ef8725SPatrick Pannuto			if ($1 < 20) {
5918000d1cc1SJoe Perches				WARN("MSLEEP",
5919458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
592009ef8725SPatrick Pannuto			}
592109ef8725SPatrick Pannuto		}
592209ef8725SPatrick Pannuto
592336ec1939SJoe Perches# check for comparisons of jiffies
592436ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
592536ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
592636ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
592736ec1939SJoe Perches		}
592836ec1939SJoe Perches
59299d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
59309d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
59319d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
59329d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
59339d7a34a5SJoe Perches		}
59349d7a34a5SJoe Perches
593500df344fSAndy Whitcroft# warn about #ifdefs in C files
5936c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
593700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
593800df344fSAndy Whitcroft#			print "$herecurr";
593900df344fSAndy Whitcroft#			$clean = 0;
594000df344fSAndy Whitcroft#		}
594100df344fSAndy Whitcroft
594222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5943c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
59443705ce5bSJoe Perches			if (ERROR("SPACING",
59453705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
59463705ce5bSJoe Perches			    $fix) {
5947194f66fcSJoe Perches				$fixed[$fixlinenr] =~
59483705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
59493705ce5bSJoe Perches			}
59503705ce5bSJoe Perches
595122f2a2efSAndy Whitcroft		}
595222f2a2efSAndy Whitcroft
59534a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5954171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5955171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
59564a0df2efSAndy Whitcroft			my $which = $1;
59574a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5958000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5959000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
59604a0df2efSAndy Whitcroft			}
59614a0df2efSAndy Whitcroft		}
59624a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5963402c2553SMichael S. Tsirkin
5964402c2553SMichael S. Tsirkin		my $barriers = qr{
5965402c2553SMichael S. Tsirkin			mb|
5966402c2553SMichael S. Tsirkin			rmb|
5967ad83ec6cSWill Deacon			wmb
5968402c2553SMichael S. Tsirkin		}x;
5969402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5970402c2553SMichael S. Tsirkin			mb__before_atomic|
5971402c2553SMichael S. Tsirkin			mb__after_atomic|
5972402c2553SMichael S. Tsirkin			store_release|
5973402c2553SMichael S. Tsirkin			load_acquire|
5974402c2553SMichael S. Tsirkin			store_mb|
5975402c2553SMichael S. Tsirkin			(?:$barriers)
5976402c2553SMichael S. Tsirkin		}x;
5977402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5978402c2553SMichael S. Tsirkin			(?:$barriers)|
597943e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
598043e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5981402c2553SMichael S. Tsirkin		}x;
5982402c2553SMichael S. Tsirkin
5983402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
59844a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5985c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5986000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
59874a0df2efSAndy Whitcroft			}
59884a0df2efSAndy Whitcroft		}
59893ad81779SPaul E. McKenney
5990f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5991f4073b0fSMichael S. Tsirkin
5992f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5993f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5994f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5995f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5996f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5997f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5998f4073b0fSMichael S. Tsirkin		}
5999f4073b0fSMichael S. Tsirkin
6000cb426e99SJoe Perches# check for waitqueue_active without a comment.
6001cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6002cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6003cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6004cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6005cb426e99SJoe Perches			}
6006cb426e99SJoe Perches		}
60073ad81779SPaul E. McKenney
60085099a722SMarco Elver# check for data_race without a comment.
60095099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
60105099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
60115099a722SMarco Elver				WARN("DATA_RACE",
60125099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
60135099a722SMarco Elver			}
60145099a722SMarco Elver		}
60155099a722SMarco Elver
60164a0df2efSAndy Whitcroft# check of hardware specific defines
6017c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6018000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6019000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
60200a920b5bSAndy Whitcroft		}
6021653d4876SAndy Whitcroft
6022596ed45bSJoe Perches# check that the storage class is not after a type
6023596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6024000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6025596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6026596ed45bSJoe Perches		}
6027596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6028596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6029596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6030596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6031596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6032596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6033596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6034d4977c78STobias Klauser		}
6035d4977c78STobias Klauser
6036de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6037de7d4f0eSAndy Whitcroft# storage class and type.
60389c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
60399c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6040000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6041000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6042de7d4f0eSAndy Whitcroft		}
6043de7d4f0eSAndy Whitcroft
60448905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
60452b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
60462b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6047d5e616fcSJoe Perches			if (WARN("INLINE",
6048d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6049d5e616fcSJoe Perches			    $fix) {
6050194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6051d5e616fcSJoe Perches
6052d5e616fcSJoe Perches			}
60538905a67cSAndy Whitcroft		}
60548905a67cSAndy Whitcroft
60553d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
60562b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
60572b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
6058000d1cc1SJoe Perches			WARN("PREFER_PACKED",
6059000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
60603d130fd0SJoe Perches		}
60613d130fd0SJoe Perches
606239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
60632b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
60642b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
6065000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
6066000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
606739b7e287SJoe Perches		}
606839b7e287SJoe Perches
6069462811d9SJoe Perches# Check for __attribute__ section, prefer __section
6070462811d9SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6071462811d9SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6072462811d9SJoe Perches			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6073462811d9SJoe Perches			my $new = substr($old, 1, -1);
6074462811d9SJoe Perches			if (WARN("PREFER_SECTION",
6075462811d9SJoe Perches				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6076462811d9SJoe Perches			    $fix) {
6077462811d9SJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6078462811d9SJoe Perches			}
6079462811d9SJoe Perches		}
6080462811d9SJoe Perches
60815f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
60822b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
60832b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
6084d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
6085d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6086d5e616fcSJoe Perches			    $fix) {
6087194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
6088d5e616fcSJoe Perches
6089d5e616fcSJoe Perches			}
60905f14d3bdSJoe Perches		}
60915f14d3bdSJoe Perches
60926061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
60932b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
60942b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
6095d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
6096d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6097d5e616fcSJoe Perches			    $fix) {
6098194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
6099d5e616fcSJoe Perches			}
61006061d949SJoe Perches		}
61016061d949SJoe Perches
6102619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
61035b57980dSJoe Perches		if ($perl_version_ok &&
6104619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6105619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6106619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6107619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6108619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6109619a908aSJoe Perches		}
6110619a908aSJoe Perches
6111fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6112e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6113fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6114e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6115e6176fa4SJoe Perches			my $type = $1;
6116e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6117e6176fa4SJoe Perches				$type = $1;
6118e6176fa4SJoe Perches				my $kernel_type = 'u';
6119e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6120e6176fa4SJoe Perches				$type =~ /(\d+)/;
6121e6176fa4SJoe Perches				$kernel_type .= $1;
6122e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6123e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6124e6176fa4SJoe Perches				    $fix) {
6125e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6126e6176fa4SJoe Perches				}
6127e6176fa4SJoe Perches			}
6128e6176fa4SJoe Perches		}
6129e6176fa4SJoe Perches
6130938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6131938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6132938224b5SJoe Perches			my $cast = $1;
6133938224b5SJoe Perches			my $const = $2;
6134938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6135938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6136938224b5SJoe Perches			    $fix) {
6137938224b5SJoe Perches				my $suffix = "";
6138938224b5SJoe Perches				my $newconst = $const;
6139938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6140938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6141938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6142938224b5SJoe Perches					$suffix .= 'LL';
6143938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6144938224b5SJoe Perches					$suffix .= 'L';
6145938224b5SJoe Perches				}
6146938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6147938224b5SJoe Perches			}
6148938224b5SJoe Perches		}
6149938224b5SJoe Perches
61508f53a9b8SJoe Perches# check for sizeof(&)
61518f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6152000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6153000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
61548f53a9b8SJoe Perches		}
61558f53a9b8SJoe Perches
615666c80b60SJoe Perches# check for sizeof without parenthesis
615766c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6158d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6159d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6160d5e616fcSJoe Perches			    $fix) {
6161194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6162d5e616fcSJoe Perches			}
616366c80b60SJoe Perches		}
616466c80b60SJoe Perches
616588982feaSJoe Perches# check for struct spinlock declarations
616688982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
616788982feaSJoe Perches			WARN("USE_SPINLOCK_T",
616888982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
616988982feaSJoe Perches		}
617088982feaSJoe Perches
6171a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
617206668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6173a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6174caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6175caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6176d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6177d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6178d5e616fcSJoe Perches				    $fix) {
6179194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6180d5e616fcSJoe Perches				}
6181a6962d72SJoe Perches			}
6182a6962d72SJoe Perches		}
6183a6962d72SJoe Perches
61840b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
61855b57980dSJoe Perches		if ($perl_version_ok &&
61860b523769SJoe Perches		    defined $stat &&
61870b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
61880b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6189e3c6bc95STobin C. Harding			my $stat_real;
6190e3c6bc95STobin C. Harding
61910b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
61920b523769SJoe Perches			$lc = $lc + $linenr;
61930b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6194ffe07513SJoe Perches				my $specifier;
6195ffe07513SJoe Perches				my $extension;
61963bd32d6aSSakari Ailus				my $qualifier;
6197ffe07513SJoe Perches				my $bad_specifier = "";
61980b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
61990b523769SJoe Perches				$fmt =~ s/%%//g;
6200e3c6bc95STobin C. Harding
62013bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6202e3c6bc95STobin C. Harding					$specifier = $1;
6203e3c6bc95STobin C. Harding					$extension = $2;
62043bd32d6aSSakari Ailus					$qualifier = $3;
6205361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
62063bd32d6aSSakari Ailus					    ($extension eq "f" &&
62073bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6208e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
62090b523769SJoe Perches						last;
62100b523769SJoe Perches					}
6211e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6212e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6213e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
62140b523769SJoe Perches						}
6215e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6216e3c6bc95STobin 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");
6217e3c6bc95STobin C. Harding					}
6218e3c6bc95STobin C. Harding				}
6219e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
62202a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
62211df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
62221df7338aSSergey Senozhatsky					my $use = "";
6223e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
62241df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6225e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
62261df7338aSSergey Senozhatsky					}
62272a9f9d85STobin C. Harding
62280b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6229e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6230e3c6bc95STobin C. Harding				}
62310b523769SJoe Perches			}
62320b523769SJoe Perches		}
62330b523769SJoe Perches
6234554e165cSAndy Whitcroft# Check for misused memsets
62355b57980dSJoe Perches		if ($perl_version_ok &&
6236d1fe9c09SJoe Perches		    defined $stat &&
62379e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6238554e165cSAndy Whitcroft
6239d7c76ba7SJoe Perches			my $ms_addr = $2;
6240d1fe9c09SJoe Perches			my $ms_val = $7;
6241d1fe9c09SJoe Perches			my $ms_size = $12;
6242d7c76ba7SJoe Perches
6243554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6244554e165cSAndy Whitcroft				ERROR("MEMSET",
6245d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6246554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6247554e165cSAndy Whitcroft				WARN("MEMSET",
6248d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6249d7c76ba7SJoe Perches			}
6250d7c76ba7SJoe Perches		}
6251d7c76ba7SJoe Perches
625298a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
62535b57980dSJoe Perches#		if ($perl_version_ok &&
6254f333195dSJoe Perches#		    defined $stat &&
6255f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6256f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6257f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6258f333195dSJoe Perches#			    $fix) {
6259f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6260f333195dSJoe Perches#			}
6261f333195dSJoe Perches#		}
626298a9bba5SJoe Perches
6263b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
62645b57980dSJoe Perches#		if ($perl_version_ok &&
6265f333195dSJoe Perches#		    defined $stat &&
6266f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6267f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6268f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6269f333195dSJoe Perches#		}
6270b6117d17SMateusz Kulikowski
62718617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
62728617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
62735b57980dSJoe Perches#		if ($perl_version_ok &&
6274f333195dSJoe Perches#		    defined $stat &&
6275f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6276f333195dSJoe Perches#
6277f333195dSJoe Perches#			my $ms_val = $7;
6278f333195dSJoe Perches#
6279f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6280f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6281f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6282f333195dSJoe Perches#				    $fix) {
6283f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6284f333195dSJoe Perches#				}
6285f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6286f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6287f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6288f333195dSJoe Perches#				    $fix) {
6289f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6290f333195dSJoe Perches#				}
6291f333195dSJoe Perches#			}
6292f333195dSJoe Perches#		}
62938617cd09SMateusz Kulikowski
6294d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
62955b57980dSJoe Perches		if ($perl_version_ok &&
6296d1fe9c09SJoe Perches		    defined $stat &&
6297d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6298d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6299d7c76ba7SJoe Perches				my $call = $1;
6300d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6301d7c76ba7SJoe Perches				my $arg1 = $3;
6302d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6303d1fe9c09SJoe Perches				my $arg2 = $8;
6304d7c76ba7SJoe Perches				my $cast;
6305d7c76ba7SJoe Perches
6306d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6307d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6308d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6309d7c76ba7SJoe Perches					$cast = $cast1;
6310d7c76ba7SJoe Perches				} else {
6311d7c76ba7SJoe Perches					$cast = $cast2;
6312d7c76ba7SJoe Perches				}
6313d7c76ba7SJoe Perches				WARN("MINMAX",
6314d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6315554e165cSAndy Whitcroft			}
6316554e165cSAndy Whitcroft		}
6317554e165cSAndy Whitcroft
63184a273195SJoe Perches# check usleep_range arguments
63195b57980dSJoe Perches		if ($perl_version_ok &&
63204a273195SJoe Perches		    defined $stat &&
63214a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
63224a273195SJoe Perches			my $min = $1;
63234a273195SJoe Perches			my $max = $7;
63244a273195SJoe Perches			if ($min eq $max) {
63254a273195SJoe Perches				WARN("USLEEP_RANGE",
6326458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
63274a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
63284a273195SJoe Perches				 $min > $max) {
63294a273195SJoe Perches				WARN("USLEEP_RANGE",
6330458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
63314a273195SJoe Perches			}
63324a273195SJoe Perches		}
63334a273195SJoe Perches
6334823b794cSJoe Perches# check for naked sscanf
63355b57980dSJoe Perches		if ($perl_version_ok &&
6336823b794cSJoe Perches		    defined $stat &&
63376c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6338823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6339823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6340823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6341823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6342823b794cSJoe Perches			$lc = $lc + $linenr;
63432a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6344823b794cSJoe Perches			WARN("NAKED_SSCANF",
6345823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6346823b794cSJoe Perches		}
6347823b794cSJoe Perches
6348afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
63495b57980dSJoe Perches		if ($perl_version_ok &&
6350afc819abSJoe Perches		    defined $stat &&
6351afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6352afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6353afc819abSJoe Perches			$lc = $lc + $linenr;
63542a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6355afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6356afc819abSJoe Perches				my $format = $6;
6357afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6358afc819abSJoe Perches				if ($count == 1 &&
6359afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6360afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6361afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6362afc819abSJoe Perches				}
6363afc819abSJoe Perches			}
6364afc819abSJoe Perches		}
6365afc819abSJoe Perches
636670dc8a48SJoe Perches# check for new externs in .h files.
636770dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
636870dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6369d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
637070dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
637170dc8a48SJoe Perches			    $fix) {
6372194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
637370dc8a48SJoe Perches			}
637470dc8a48SJoe Perches		}
637570dc8a48SJoe Perches
6376de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6377171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6378c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6379171ae1a4SAndy Whitcroft		{
6380c45dcabdSAndy Whitcroft			my $function_name = $1;
6381c45dcabdSAndy Whitcroft			my $paren_space = $2;
6382171ae1a4SAndy Whitcroft
6383171ae1a4SAndy Whitcroft			my $s = $stat;
6384171ae1a4SAndy Whitcroft			if (defined $cond) {
6385171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6386171ae1a4SAndy Whitcroft			}
6387d8b44b58SKees Cook			if ($s =~ /^\s*;/)
6388c45dcabdSAndy Whitcroft			{
6389000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6390000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6391de7d4f0eSAndy Whitcroft			}
6392de7d4f0eSAndy Whitcroft
6393171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6394000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6395000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6396171ae1a4SAndy Whitcroft			}
63979c9ba34eSAndy Whitcroft
63989c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
63999c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
64009c9ba34eSAndy Whitcroft		{
6401000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6402000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6403171ae1a4SAndy Whitcroft		}
6404171ae1a4SAndy Whitcroft
6405a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6406a0ad7596SJoe Perches		if (defined $stat &&
6407d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6408d8b44b58SKees Cook		    $1 ne "void") {
6409d8b44b58SKees Cook			my $args = trim($1);
6410ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6411ca0d8929SJoe Perches				my $arg = trim($1);
6412d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6413ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6414ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6415ca0d8929SJoe Perches				}
6416ca0d8929SJoe Perches			}
6417ca0d8929SJoe Perches		}
6418ca0d8929SJoe Perches
6419a0ad7596SJoe Perches# check for function definitions
64205b57980dSJoe Perches		if ($perl_version_ok &&
6421a0ad7596SJoe Perches		    defined $stat &&
6422a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6423a0ad7596SJoe Perches			$context_function = $1;
6424a0ad7596SJoe Perches
6425a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6426a0ad7596SJoe Perches			my $ok = 0;
6427a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6428a0ad7596SJoe Perches			my $herectx = $here . "\n";
6429a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6430a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6431a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6432a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6433a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6434a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6435a0ad7596SJoe Perches			}
6436a0ad7596SJoe Perches			if (!$ok) {
6437a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6438a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6439a0ad7596SJoe Perches			}
6440a0ad7596SJoe Perches		}
6441a0ad7596SJoe Perches
6442de7d4f0eSAndy Whitcroft# checks for new __setup's
6443de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6444de7d4f0eSAndy Whitcroft			my $name = $1;
6445de7d4f0eSAndy Whitcroft
6446de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6447000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
64482581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6449de7d4f0eSAndy Whitcroft			}
6450653d4876SAndy Whitcroft		}
64519c0ca6f9SAndy Whitcroft
6452e29a70f1SJoe Perches# check for pointless casting of alloc functions
6453e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6454000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6455000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
64569c0ca6f9SAndy Whitcroft		}
645713214adfSAndy Whitcroft
6458a640d25cSJoe Perches# alloc style
6459a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
64605b57980dSJoe Perches		if ($perl_version_ok &&
6461e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6462a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6463a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6464a640d25cSJoe Perches		}
6465a640d25cSJoe Perches
646660a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
64675b57980dSJoe Perches		if ($perl_version_ok &&
64681b4a2ed4SJoe Perches		    defined $stat &&
64691b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
647060a55369SJoe Perches			my $oldfunc = $3;
647160a55369SJoe Perches			my $a1 = $4;
647260a55369SJoe Perches			my $a2 = $10;
647360a55369SJoe Perches			my $newfunc = "kmalloc_array";
647460a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
647560a55369SJoe Perches			my $r1 = $a1;
647660a55369SJoe Perches			my $r2 = $a2;
647760a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
647860a55369SJoe Perches				$r1 = $a2;
647960a55369SJoe Perches				$r2 = $a1;
648060a55369SJoe Perches			}
6481e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6482e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
64831b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6484e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6485e3d95a2aSTobin C. Harding
6486e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
64871b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
64881b4a2ed4SJoe Perches				    $cnt == 1 &&
6489e367455aSJoe Perches				    $fix) {
6490194f66fcSJoe 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;
649160a55369SJoe Perches				}
649260a55369SJoe Perches			}
649360a55369SJoe Perches		}
649460a55369SJoe Perches
6495972fdea2SJoe Perches# check for krealloc arg reuse
64965b57980dSJoe Perches		if ($perl_version_ok &&
64974cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
64984cab63ceSJoe Perches		    $1 eq $3) {
6499972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6500972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6501972fdea2SJoe Perches		}
6502972fdea2SJoe Perches
65035ce59ae0SJoe Perches# check for alloc argument mismatch
65045ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
65055ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
65065ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
65075ce59ae0SJoe Perches		}
65085ce59ae0SJoe Perches
6509caf2a54fSJoe Perches# check for multiple semicolons
6510caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6511d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6512d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6513d5e616fcSJoe Perches			    $fix) {
6514194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6515d5e616fcSJoe Perches			}
6516d1e2ad07SJoe Perches		}
6517d1e2ad07SJoe Perches
6518cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6519cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6520cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
65210ab90191SJoe Perches			my $ull = "";
65220ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
65230ab90191SJoe Perches			if (CHK("BIT_MACRO",
65240ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
65250ab90191SJoe Perches			    $fix) {
65260ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
65270ab90191SJoe Perches			}
65280ab90191SJoe Perches		}
65290ab90191SJoe Perches
653050161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
653150161266SJoe Perches		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^CONFIG_/) {
653250161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
653350161266SJoe Perches			     "IS_ENABLED($1) is normally used as IS_ENABLED(CONFIG_$1)\n" . $herecurr);
653450161266SJoe Perches		}
653550161266SJoe Perches
65362d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
65372d632745SJoe Perches		if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
65382d632745SJoe Perches			my $config = $1;
65392d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
65402d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
65412d632745SJoe Perches			    $fix) {
65422d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
65432d632745SJoe Perches			}
65442d632745SJoe Perches		}
65452d632745SJoe Perches
6546e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6547c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6548c34c09a8SJoe Perches			my $has_break = 0;
6549c34c09a8SJoe Perches			my $has_statement = 0;
6550c34c09a8SJoe Perches			my $count = 0;
6551c34c09a8SJoe Perches			my $prevline = $linenr;
6552e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6553c34c09a8SJoe Perches				$prevline--;
6554c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6555c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6556c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6557c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6558c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6559c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6560c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6561c34c09a8SJoe Perches				$has_statement = 1;
6562c34c09a8SJoe Perches				$count++;
6563258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6564c34c09a8SJoe Perches			}
6565c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6566c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6567224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6568c34c09a8SJoe Perches			}
6569c34c09a8SJoe Perches		}
6570c34c09a8SJoe Perches
6571f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6572f36d3eb8SJoe Perches		my @fallthroughs = (
6573f36d3eb8SJoe Perches			'fallthrough',
6574f36d3eb8SJoe Perches			'@fallthrough@',
6575f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6576f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6577f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6578f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6579f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6580f36d3eb8SJoe Perches		    );
6581f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6582f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6583f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6584f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6585f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6586f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6587f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6588f36d3eb8SJoe Perches					last;
6589f36d3eb8SJoe Perches				}
6590f36d3eb8SJoe Perches			}
6591f36d3eb8SJoe Perches		}
6592f36d3eb8SJoe Perches
6593d1e2ad07SJoe Perches# check for switch/default statements without a break;
65945b57980dSJoe Perches		if ($perl_version_ok &&
6595d1e2ad07SJoe Perches		    defined $stat &&
6596d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6597d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6598e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6599e3d95a2aSTobin C. Harding
6600d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6601d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6602caf2a54fSJoe Perches		}
6603caf2a54fSJoe Perches
660413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6605d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6606d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6607d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6608d5e616fcSJoe Perches			    $fix) {
6609194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6610d5e616fcSJoe Perches			}
661113214adfSAndy Whitcroft		}
6612773647a0SAndy Whitcroft
661362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
661462ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
661562ec818fSJoe Perches			ERROR("DATE_TIME",
661662ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
661762ec818fSJoe Perches		}
661862ec818fSJoe Perches
66192c92488aSJoe Perches# check for use of yield()
66202c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
66212c92488aSJoe Perches			WARN("YIELD",
66222c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
66232c92488aSJoe Perches		}
66242c92488aSJoe Perches
6625179f8f40SJoe Perches# check for comparisons against true and false
6626179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6627179f8f40SJoe Perches			my $lead = $1;
6628179f8f40SJoe Perches			my $arg = $2;
6629179f8f40SJoe Perches			my $test = $3;
6630179f8f40SJoe Perches			my $otype = $4;
6631179f8f40SJoe Perches			my $trail = $5;
6632179f8f40SJoe Perches			my $op = "!";
6633179f8f40SJoe Perches
6634179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6635179f8f40SJoe Perches
6636179f8f40SJoe Perches			my $type = lc($otype);
6637179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6638179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6639179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6640179f8f40SJoe Perches					$op = "";
6641179f8f40SJoe Perches				}
6642179f8f40SJoe Perches
6643179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6644179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6645179f8f40SJoe Perches
6646179f8f40SJoe Perches## maybe suggesting a correct construct would better
6647179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6648179f8f40SJoe Perches
6649179f8f40SJoe Perches			}
6650179f8f40SJoe Perches		}
6651179f8f40SJoe Perches
66524882720bSThomas Gleixner# check for semaphores initialized locked
66534882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6654000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6655000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6656773647a0SAndy Whitcroft		}
66576712d858SJoe Perches
665867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
665967d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6660000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
666167d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6662773647a0SAndy Whitcroft		}
66636712d858SJoe Perches
6664ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6665f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6666000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6667ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6668f3db6639SMichael Ellerman		}
66696712d858SJoe Perches
66703d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
66713d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
66723d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
66733d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
66743d709ab5SPaul E. McKenney		}
66753d709ab5SPaul E. McKenney
66769189c7e7SJoe Perches# check for deprecated apis
66779189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
66789189c7e7SJoe Perches			my $deprecated_api = $1;
66799189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
66809189c7e7SJoe Perches			WARN("DEPRECATED_API",
66819189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
66829189c7e7SJoe Perches		}
66839189c7e7SJoe Perches
66840f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6685d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
6686ced69da1SQuentin Monnet		if (defined($const_structs) &&
6687ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
6688d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6689000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6690d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
66912b6db5cbSAndy Whitcroft		}
6692773647a0SAndy Whitcroft
6693773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6694773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6695773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6696c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6697c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6698171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6699171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6700171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6701773647a0SAndy Whitcroft		{
6702000d1cc1SJoe Perches			WARN("NR_CPUS",
6703000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6704773647a0SAndy Whitcroft		}
67059c9ba34eSAndy Whitcroft
670652ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
670752ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
670852ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
670952ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
671052ea8506SJoe Perches		}
671152ea8506SJoe Perches
6712acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
67135b57980dSJoe Perches		if ($perl_version_ok &&
6714acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6715acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6716acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6717acd9362cSJoe Perches		}
6718acd9362cSJoe Perches
6719de3f186fSDenis Efremov# nested likely/unlikely calls
6720de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6721de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6722de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6723de3f186fSDenis Efremov		}
6724de3f186fSDenis Efremov
6725691d77b6SAndy Whitcroft# whine mightly about in_atomic
6726691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6727691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6728000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6729000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6730f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6731000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6732000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6733691d77b6SAndy Whitcroft			}
6734691d77b6SAndy Whitcroft		}
67351704f47bSPeter Zijlstra
67360f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
67370f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
67380f5225b0SPeter Zijlstra			ERROR("LOCKING",
67390f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
67400f5225b0SPeter Zijlstra		}
67410f5225b0SPeter Zijlstra
67421704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
67431704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
67441704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
67451704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
67461704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
67471704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6748000d1cc1SJoe Perches				ERROR("LOCKDEP",
6749000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
67501704f47bSPeter Zijlstra			}
67511704f47bSPeter Zijlstra		}
675288f8831cSDave Jones
6753b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6754b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6755000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6756000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
675788f8831cSDave Jones		}
67582435880fSJoe Perches
675900180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
676000180468SJoe Perches# and whether or not function naming is typical and if
676100180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
67625b57980dSJoe Perches		if ($perl_version_ok &&
676300180468SJoe Perches		    defined $stat &&
676400180468SJoe 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*\)/) {
676500180468SJoe Perches			my $var = $1;
676600180468SJoe Perches			my $perms = $2;
676700180468SJoe Perches			my $show = $3;
676800180468SJoe Perches			my $store = $4;
676900180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
677000180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
677100180468SJoe Perches			    $store =~ /^${var}_store$/ &&
677200180468SJoe Perches			    $octal_perms eq "0644") {
677300180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
677400180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
677500180468SJoe Perches				    $fix) {
677600180468SJoe 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})/;
677700180468SJoe Perches				}
677800180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
677900180468SJoe Perches				 $store =~ /^NULL$/ &&
678000180468SJoe Perches				 $octal_perms eq "0444") {
678100180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
678200180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
678300180468SJoe Perches				    $fix) {
678400180468SJoe 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})/;
678500180468SJoe Perches				}
678600180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
678700180468SJoe Perches				 $store =~ /^${var}_store$/ &&
678800180468SJoe Perches				 $octal_perms eq "0200") {
678900180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
679000180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
679100180468SJoe Perches				    $fix) {
679200180468SJoe 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})/;
679300180468SJoe Perches				}
679400180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
679500180468SJoe Perches				 $octal_perms eq "0444" ||
679600180468SJoe Perches				 $octal_perms eq "0200") {
679700180468SJoe Perches				my $newshow = "$show";
679800180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
679900180468SJoe Perches				my $newstore = $store;
680000180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
680100180468SJoe Perches				my $rename = "";
680200180468SJoe Perches				if ($show ne $newshow) {
680300180468SJoe Perches					$rename .= " '$show' to '$newshow'";
680400180468SJoe Perches				}
680500180468SJoe Perches				if ($store ne $newstore) {
680600180468SJoe Perches					$rename .= " '$store' to '$newstore'";
680700180468SJoe Perches				}
680800180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
680900180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
681000180468SJoe Perches			} else {
681100180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
681200180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
681300180468SJoe Perches			}
681400180468SJoe Perches		}
681500180468SJoe Perches
6816515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6817515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
681873121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
681973121534SJoe Perches#   specific definition of not visible in sysfs.
682073121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
682173121534SJoe Perches#   use the default permissions
68225b57980dSJoe Perches		if ($perl_version_ok &&
6823459cf0aeSJoe Perches		    defined $stat &&
6824515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
68252435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
68262435880fSJoe Perches				my $func = $entry->[0];
68272435880fSJoe Perches				my $arg_pos = $entry->[1];
68282435880fSJoe Perches
6829459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6830459cf0aeSJoe Perches				$lc = $lc + $linenr;
68312a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6832459cf0aeSJoe Perches
68332435880fSJoe Perches				my $skip_args = "";
68342435880fSJoe Perches				if ($arg_pos > 1) {
68352435880fSJoe Perches					$arg_pos--;
68362435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
68372435880fSJoe Perches				}
6838f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6839459cf0aeSJoe Perches				if ($stat =~ /$test/) {
68402435880fSJoe Perches					my $val = $1;
68412435880fSJoe Perches					$val = $6 if ($skip_args ne "");
684273121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
684373121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
684473121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
68452435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6846459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6847f90774e1SJoe Perches					}
6848f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6849c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6850459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
68512435880fSJoe Perches					}
6852459cf0aeSJoe Perches				}
6853459cf0aeSJoe Perches			}
6854459cf0aeSJoe Perches		}
6855459cf0aeSJoe Perches
6856459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6857bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
685800180468SJoe Perches			my $oval = $1;
685900180468SJoe Perches			my $octal = perms_to_octal($oval);
6860f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6861459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6862f90774e1SJoe Perches			    $fix) {
686300180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
68642435880fSJoe Perches			}
686513214adfSAndy Whitcroft		}
68665a6d20ceSBjorn Andersson
68675a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
68685a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
68695a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
68705a6d20ceSBjorn Andersson			my $valid_licenses = qr{
68715a6d20ceSBjorn Andersson						GPL|
68725a6d20ceSBjorn Andersson						GPL\ v2|
68735a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
68745a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
68755a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
68765a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
68775a6d20ceSBjorn Andersson						Proprietary
68785a6d20ceSBjorn Andersson					}x;
68795a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
68805a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
68815a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
68825a6d20ceSBjorn Andersson			}
68835a6d20ceSBjorn Andersson		}
68846a8d76cbSMatteo Croce
68856a8d76cbSMatteo Croce# check for sysctl duplicate constants
68866a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
68876a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
68886a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
68896a8d76cbSMatteo Croce		}
6890515a235eSJoe Perches	}
689113214adfSAndy Whitcroft
689213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
689313214adfSAndy Whitcroft	# so just keep quiet.
689413214adfSAndy Whitcroft	if ($#rawlines == -1) {
689513214adfSAndy Whitcroft		exit(0);
68960a920b5bSAndy Whitcroft	}
68970a920b5bSAndy Whitcroft
68988905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
68998905a67cSAndy Whitcroft	# things that appear to be patches.
69008905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
69018905a67cSAndy Whitcroft		exit(0);
69028905a67cSAndy Whitcroft	}
69038905a67cSAndy Whitcroft
69048905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
69058905a67cSAndy Whitcroft	# just keep quiet.
69068905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
69078905a67cSAndy Whitcroft		exit(0);
69088905a67cSAndy Whitcroft	}
69098905a67cSAndy Whitcroft
6910a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6911000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6912000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
69130a920b5bSAndy Whitcroft	}
6914cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6915cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6916000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6917000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6918cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6919cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6920cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6921cd261496SGeert Uytterhoeven		}
69220a920b5bSAndy Whitcroft	}
69230a920b5bSAndy Whitcroft
6924f0a594c1SAndy Whitcroft	print report_dump();
692513214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
692613214adfSAndy Whitcroft		print "$filename " if ($summary_file);
69276c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
69286c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
69296c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
69306c72ffaaSAndy Whitcroft	}
69318905a67cSAndy Whitcroft
6932d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6933ef212196SJoe Perches		# If there were any defects found and not already fixing them
6934ef212196SJoe Perches		if (!$clean and !$fix) {
6935ef212196SJoe Perches			print << "EOM"
6936ef212196SJoe Perches
6937ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6938ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6939ef212196SJoe PerchesEOM
6940ef212196SJoe Perches		}
6941d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6942d2c0a235SAndy Whitcroft		# then suggest that.
6943d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6944b0781216SMike Frysinger			$rpt_cleaners = 0;
6945d8469f16SJoe Perches			print << "EOM"
6946d8469f16SJoe Perches
6947d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6948d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6949d8469f16SJoe PerchesEOM
6950d2c0a235SAndy Whitcroft		}
6951d2c0a235SAndy Whitcroft	}
6952d2c0a235SAndy Whitcroft
6953d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6954d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6955d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
69569624b8d6SJoe Perches		my $newfile = $filename;
69579624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
69583705ce5bSJoe Perches		my $linecount = 0;
69593705ce5bSJoe Perches		my $f;
69603705ce5bSJoe Perches
6961d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6962d752fcc8SJoe Perches
69633705ce5bSJoe Perches		open($f, '>', $newfile)
69643705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
69653705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
69663705ce5bSJoe Perches			$linecount++;
69673705ce5bSJoe Perches			if ($file) {
69683705ce5bSJoe Perches				if ($linecount > 3) {
69693705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
69703705ce5bSJoe Perches					print $f $fixed_line . "\n";
69713705ce5bSJoe Perches				}
69723705ce5bSJoe Perches			} else {
69733705ce5bSJoe Perches				print $f $fixed_line . "\n";
69743705ce5bSJoe Perches			}
69753705ce5bSJoe Perches		}
69763705ce5bSJoe Perches		close($f);
69773705ce5bSJoe Perches
69783705ce5bSJoe Perches		if (!$quiet) {
69793705ce5bSJoe Perches			print << "EOM";
6980d8469f16SJoe Perches
69813705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
69823705ce5bSJoe Perches
69833705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
69843705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
69853705ce5bSJoe Perches
69863705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
69873705ce5bSJoe PerchesNo warranties, expressed or implied...
69883705ce5bSJoe PerchesEOM
69893705ce5bSJoe Perches		}
69903705ce5bSJoe Perches	}
69913705ce5bSJoe Perches
6992d8469f16SJoe Perches	if ($quiet == 0) {
6993d8469f16SJoe Perches		print "\n";
6994d8469f16SJoe Perches		if ($clean == 1) {
6995d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6996d8469f16SJoe Perches		} else {
6997d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
69980a920b5bSAndy Whitcroft		}
69990a920b5bSAndy Whitcroft	}
70000a920b5bSAndy Whitcroft	return $clean;
70010a920b5bSAndy Whitcroft}
7002