xref: /linux-6.15/scripts/checkpatch.pl (revision 461e1565)
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";
546cd7f386SJoe Perchesmy $max_line_length = 80;
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";
6275ad8c57SJerome Forissiermy $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
1006cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
10156193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
102713a09deSAntonio Borneo  --tab-size=n               set the number of spaces for tab (default 8)
10377f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10477f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10577f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10677f5b10aSHannes Eder  --summary-file             include the filename in summary
10777f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
10877f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10977f5b10aSHannes Eder                             is all off)
11077f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
11177f5b10aSHannes Eder                             literally
1123705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1133705ce5bSJoe Perches                             If correctable single-line errors exist, create
1143705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1153705ce5bSJoe Perches                             with potential errors corrected to the preferred
1163705ce5bSJoe Perches                             checkpatch style
1179624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1189624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1199624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
120d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
121d62a201fSDave Hansen                             runtime errors.
122ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
123f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
124ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12575ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
126737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
127737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
12877f5b10aSHannes Eder  -h, --help, --version      display this help and exit
12977f5b10aSHannes Eder
13077f5b10aSHannes EderWhen FILE is - read standard input.
13177f5b10aSHannes EderEOM
13277f5b10aSHannes Eder
13377f5b10aSHannes Eder	exit($exitcode);
13477f5b10aSHannes Eder}
13577f5b10aSHannes Eder
1363beb42ecSJoe Perchessub uniq {
1373beb42ecSJoe Perches	my %seen;
1383beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1393beb42ecSJoe Perches}
1403beb42ecSJoe Perches
1413beb42ecSJoe Perchessub list_types {
1423beb42ecSJoe Perches	my ($exitcode) = @_;
1433beb42ecSJoe Perches
1443beb42ecSJoe Perches	my $count = 0;
1453beb42ecSJoe Perches
1463beb42ecSJoe Perches	local $/ = undef;
1473beb42ecSJoe Perches
1483beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1493beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1503beb42ecSJoe Perches
1513beb42ecSJoe Perches	my $text = <$script>;
1523beb42ecSJoe Perches	close($script);
1533beb42ecSJoe Perches
1543beb42ecSJoe Perches	my @types = ();
1550547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1560547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1573beb42ecSJoe Perches		push (@types, $_);
1583beb42ecSJoe Perches	}
1593beb42ecSJoe Perches	@types = sort(uniq(@types));
1603beb42ecSJoe Perches	print("#\tMessage type\n\n");
1613beb42ecSJoe Perches	foreach my $type (@types) {
1623beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1633beb42ecSJoe Perches	}
1643beb42ecSJoe Perches
1653beb42ecSJoe Perches	exit($exitcode);
1663beb42ecSJoe Perches}
1673beb42ecSJoe Perches
168000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
169000d1cc1SJoe Perchesif (-f $conf) {
170000d1cc1SJoe Perches	my @conf_args;
171000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
172000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
173000d1cc1SJoe Perches
174000d1cc1SJoe Perches	while (<$conffile>) {
175000d1cc1SJoe Perches		my $line = $_;
176000d1cc1SJoe Perches
177000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
178000d1cc1SJoe Perches		$line =~ s/^\s*//g;
179000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
180000d1cc1SJoe Perches
181000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
182000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
183000d1cc1SJoe Perches
184000d1cc1SJoe Perches		my @words = split(" ", $line);
185000d1cc1SJoe Perches		foreach my $word (@words) {
186000d1cc1SJoe Perches			last if ($word =~ m/^#/);
187000d1cc1SJoe Perches			push (@conf_args, $word);
188000d1cc1SJoe Perches		}
189000d1cc1SJoe Perches	}
190000d1cc1SJoe Perches	close($conffile);
191000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
192000d1cc1SJoe Perches}
193000d1cc1SJoe Perches
194737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
195737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
196737c0767SJohn Brooksforeach (@ARGV) {
197737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
198737c0767SJohn Brooks		$_ = "--color=$color";
199737c0767SJohn Brooks	}
200737c0767SJohn Brooks}
201737c0767SJohn Brooks
2020a920b5bSAndy WhitcroftGetOptions(
2036c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2040a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2050a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2060a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2076c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2088905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20934d8815fSJoe Perches	'showfile!'	=> \$showfile,
21077f5b10aSHannes Eder	'f|file!'	=> \$file,
2114a593c34SDu, Changbin	'g|git!'	=> \$git,
2126c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2136c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
214000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
21591bfe484SJoe Perches	'types=s'	=> \@use,
216000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2173beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2186cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21956193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
220713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
2216c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2228905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2238905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
22413214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2253705ce5bSJoe Perches	'fix!'		=> \$fix,
2269624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
227d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
228c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
229773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
230ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
231ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
23275ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
233737c0767SJohn Brooks	'color=s'	=> \$color,
234737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
235737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
23677f5b10aSHannes Eder	'h|help'	=> \$help,
23777f5b10aSHannes Eder	'version'	=> \$help
23877f5b10aSHannes Eder) or help(1);
23977f5b10aSHannes Eder
24077f5b10aSHannes Ederhelp(0) if ($help);
2410a920b5bSAndy Whitcroft
2423beb42ecSJoe Percheslist_types(0) if ($list_types);
2433beb42ecSJoe Perches
2449624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2452ac73b4fSJoe Perches$check_orig = $check;
2469624b8d6SJoe Perches
2470a920b5bSAndy Whitcroftmy $exit = 0;
2480a920b5bSAndy Whitcroft
2495b57980dSJoe Perchesmy $perl_version_ok = 1;
250d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2515b57980dSJoe Perches	$perl_version_ok = 0;
252d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2535b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
254d62a201fSDave Hansen}
255d62a201fSDave Hansen
25645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2570a920b5bSAndy Whitcroftif ($#ARGV < 0) {
25845107ff6SAllen Hubbe	push(@ARGV, '-');
2590a920b5bSAndy Whitcroft}
2600a920b5bSAndy Whitcroft
261737c0767SJohn Brooksif ($color =~ /^[01]$/) {
262737c0767SJohn Brooks	$color = !$color;
263737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
264737c0767SJohn Brooks	$color = 1;
265737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
266737c0767SJohn Brooks	$color = 0;
267737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
268737c0767SJohn Brooks	$color = (-t STDOUT);
269737c0767SJohn Brooks} else {
270737c0767SJohn Brooks	die "Invalid color mode: $color\n";
271737c0767SJohn Brooks}
272737c0767SJohn Brooks
273713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
274713a09deSAntonio Borneodie "Invalid TAB size: $tabsize\n" if ($tabsize < 2);
275713a09deSAntonio Borneo
27691bfe484SJoe Perchessub hash_save_array_words {
27791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
27891bfe484SJoe Perches
27991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
28091bfe484SJoe Perches	foreach my $word (@array) {
281000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
282000d1cc1SJoe Perches		$word =~ s/^\s*//g;
283000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
284000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
285000d1cc1SJoe Perches
286000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
287000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
288000d1cc1SJoe Perches
28991bfe484SJoe Perches		$hashRef->{$word}++;
290000d1cc1SJoe Perches	}
29191bfe484SJoe Perches}
29291bfe484SJoe Perches
29391bfe484SJoe Perchessub hash_show_words {
29491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
29591bfe484SJoe Perches
2963c816e49SJoe Perches	if (keys %$hashRef) {
297d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
29858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
29991bfe484SJoe Perches			print " $word";
30091bfe484SJoe Perches		}
301d8469f16SJoe Perches		print "\n";
30291bfe484SJoe Perches	}
30391bfe484SJoe Perches}
30491bfe484SJoe Perches
30591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
30691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
307000d1cc1SJoe Perches
308c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
309c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3107429c690SAndy Whitcroftmy $dbg_type = 0;
311a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
312c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
31321caa13cSAndy Whitcroft	## no critic
31421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
31521caa13cSAndy Whitcroft	die "$@" if ($@);
316c2fdda0dSAndy Whitcroft}
317c2fdda0dSAndy Whitcroft
318d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
319d2c0a235SAndy Whitcroft
3208905a67cSAndy Whitcroftif ($terse) {
3218905a67cSAndy Whitcroft	$emacs = 1;
3228905a67cSAndy Whitcroft	$quiet++;
3238905a67cSAndy Whitcroft}
3248905a67cSAndy Whitcroft
3256c72ffaaSAndy Whitcroftif ($tree) {
3266c72ffaaSAndy Whitcroft	if (defined $root) {
3276c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3286c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3296c72ffaaSAndy Whitcroft		}
3306c72ffaaSAndy Whitcroft	} else {
3316c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3326c72ffaaSAndy Whitcroft			$root = '.';
3336c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3346c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3356c72ffaaSAndy Whitcroft			$root = $1;
3366c72ffaaSAndy Whitcroft		}
3376c72ffaaSAndy Whitcroft	}
3386c72ffaaSAndy Whitcroft
3396c72ffaaSAndy Whitcroft	if (!defined $root) {
3400a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3410a920b5bSAndy Whitcroft		exit(2);
3420a920b5bSAndy Whitcroft	}
3436c72ffaaSAndy Whitcroft}
3446c72ffaaSAndy Whitcroft
3456c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3466c72ffaaSAndy Whitcroft
3472ceb532bSAndy Whitcroftour $Ident	= qr{
3482ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3492ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3502ceb532bSAndy Whitcroft		}x;
3516c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3526c72ffaaSAndy Whitcroftour $Sparse	= qr{
3536c72ffaaSAndy Whitcroft			__user|
3546c72ffaaSAndy Whitcroft			__kernel|
3556c72ffaaSAndy Whitcroft			__force|
3566c72ffaaSAndy Whitcroft			__iomem|
3576c72ffaaSAndy Whitcroft			__must_check|
358417495edSAndy Whitcroft			__kprobes|
359165e72a6SSven Eckelmann			__ref|
36033aa4597SGeert Uytterhoeven			__refconst|
36133aa4597SGeert Uytterhoeven			__refdata|
362ad315455SBoqun Feng			__rcu|
363ad315455SBoqun Feng			__private
3646c72ffaaSAndy Whitcroft		}x;
365e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
366e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
367e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
368e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
369e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3708716de38SJoe Perches
37152131292SWolfram Sang# Notes to $Attribute:
37252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3736c72ffaaSAndy Whitcroftour $Attribute	= qr{
3746c72ffaaSAndy Whitcroft			const|
37503f1df7dSJoe Perches			__percpu|
37603f1df7dSJoe Perches			__nocast|
37703f1df7dSJoe Perches			__safe|
37846d832f5SMichael S. Tsirkin			__bitwise|
37903f1df7dSJoe Perches			__packed__|
38003f1df7dSJoe Perches			__packed2__|
38103f1df7dSJoe Perches			__naked|
38203f1df7dSJoe Perches			__maybe_unused|
38303f1df7dSJoe Perches			__always_unused|
38403f1df7dSJoe Perches			__noreturn|
38503f1df7dSJoe Perches			__used|
38603f1df7dSJoe Perches			__cold|
387e23ef1f3SJoe Perches			__pure|
38803f1df7dSJoe Perches			__noclone|
38903f1df7dSJoe Perches			__deprecated|
3906c72ffaaSAndy Whitcroft			__read_mostly|
391c5967e98SJoe Perches			__ro_after_init|
3926c72ffaaSAndy Whitcroft			__kprobes|
3938716de38SJoe Perches			$InitAttribute|
39424e1d81aSAndy Whitcroft			____cacheline_aligned|
39524e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3965fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3975fe3af11SAndy Whitcroft			__weak
3986c72ffaaSAndy Whitcroft		  }x;
399c45dcabdSAndy Whitcroftour $Modifier;
40091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
4016c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
4026c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
4036c72ffaaSAndy Whitcroft
40495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
40595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
40695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
40795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4082435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
409c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
410326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
411326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
412326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
41374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4142435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
415326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
416447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
41723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4186c72ffaaSAndy Whitcroftour $Operators	= qr{
4196c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4206c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
42123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4226c72ffaaSAndy Whitcroft		  }x;
4236c72ffaaSAndy Whitcroft
42491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
42591cb5195SJoe Perches
426ab7e23f3SJoe Perchesour $BasicType;
4278905a67cSAndy Whitcroftour $NonptrType;
4281813087dSJoe Perchesour $NonptrTypeMisordered;
4298716de38SJoe Perchesour $NonptrTypeWithAttr;
4308905a67cSAndy Whitcroftour $Type;
4311813087dSJoe Perchesour $TypeMisordered;
4328905a67cSAndy Whitcroftour $Declare;
4331813087dSJoe Perchesour $DeclareMisordered;
4348905a67cSAndy Whitcroft
43515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
43615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
437171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
438171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
439171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
440171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
441171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
442171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
443171ae1a4SAndy Whitcroft}x;
444171ae1a4SAndy Whitcroft
44515662b3eSJoe Perchesour $UTF8	= qr{
44615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
44715662b3eSJoe Perches	| $NON_ASCII_UTF8
44815662b3eSJoe Perches}x;
44915662b3eSJoe Perches
450e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
451021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
452021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
453021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
454021158b4SJoe Perches)};
455e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
456fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4578ed22cadSAndy Whitcroft	atomic_t
4588ed22cadSAndy Whitcroft)};
459e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
460e6176fa4SJoe Perches	$typeC99Typedefs\b|
461e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
462e6176fa4SJoe Perches	$typeKernelTypedefs\b
463e6176fa4SJoe Perches)};
4648ed22cadSAndy Whitcroft
4656d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4666d32f7a3SJoe Perches
467691e669bSJoe Perchesour $logFunctions = qr{(?x:
468758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4697d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
47087bd499aSJoe Perches	TP_printk|
4716e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
472b0531722SJoe Perches	panic|
47306668727SJoe Perches	MODULE_[A-Z_]+|
47406668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
475691e669bSJoe Perches)};
476691e669bSJoe Perches
477e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
478e29a70f1SJoe Perches	(?:(?:devm_)?
479e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
480e29a70f1SJoe Perches		kstrdup(?:_const)? |
481e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
482*461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
483e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
484e29a70f1SJoe Perches	dma_alloc_coherent
485e29a70f1SJoe Perches)};
486e29a70f1SJoe Perches
48720112475SJoe Perchesour $signature_tags = qr{(?xi:
48820112475SJoe Perches	Signed-off-by:|
489d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
49020112475SJoe Perches	Acked-by:|
49120112475SJoe Perches	Tested-by:|
49220112475SJoe Perches	Reviewed-by:|
49320112475SJoe Perches	Reported-by:|
4948543ae12SMugunthan V N	Suggested-by:|
49520112475SJoe Perches	To:|
49620112475SJoe Perches	Cc:
49720112475SJoe Perches)};
49820112475SJoe Perches
4991813087dSJoe Perchesour @typeListMisordered = (
5001813087dSJoe Perches	qr{char\s+(?:un)?signed},
5011813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
5021813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
5031813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
5041813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
5051813087dSJoe Perches	qr{short\s+(?:un)?signed},
5061813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5071813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5081813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5091813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5101813087dSJoe Perches	qr{int\s+(?:un)?signed},
5111813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5121813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5131813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5141813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5151813087dSJoe Perches	qr{long\s+(?:un)?signed},
5161813087dSJoe Perches);
5171813087dSJoe Perches
5188905a67cSAndy Whitcroftour @typeList = (
5198905a67cSAndy Whitcroft	qr{void},
5200c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5210c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5220c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5230c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5240c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5250c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5260c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5270c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5280c773d9dSJoe Perches	qr{(?:un)?signed},
5298905a67cSAndy Whitcroft	qr{float},
5308905a67cSAndy Whitcroft	qr{double},
5318905a67cSAndy Whitcroft	qr{bool},
5328905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5338905a67cSAndy Whitcroft	qr{union\s+$Ident},
5348905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5358905a67cSAndy Whitcroft	qr{${Ident}_t},
5368905a67cSAndy Whitcroft	qr{${Ident}_handler},
5378905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5381813087dSJoe Perches	@typeListMisordered,
5398905a67cSAndy Whitcroft);
540938224b5SJoe Perches
541938224b5SJoe Perchesour $C90_int_types = qr{(?x:
542938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
543938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
544938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
545938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
546938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
547938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
548938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
549938224b5SJoe Perches
550938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
551938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
552938224b5SJoe Perches	long\s+(?:un)?signed|
553938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
554938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
555938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
556938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
557938224b5SJoe Perches
558938224b5SJoe Perches	int\s+(?:un)?signed|
559938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
560938224b5SJoe Perches)};
561938224b5SJoe Perches
562485ff23eSAlex Dowadour @typeListFile = ();
5638716de38SJoe Perchesour @typeListWithAttr = (
5648716de38SJoe Perches	@typeList,
5658716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5668716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5678716de38SJoe Perches);
5688716de38SJoe Perches
569c45dcabdSAndy Whitcroftour @modifierList = (
570c45dcabdSAndy Whitcroft	qr{fastcall},
571c45dcabdSAndy Whitcroft);
572485ff23eSAlex Dowadour @modifierListFile = ();
5738905a67cSAndy Whitcroft
5742435880fSJoe Perchesour @mode_permission_funcs = (
5752435880fSJoe Perches	["module_param", 3],
5762435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5772435880fSJoe Perches	["module_param_array_named", 5],
5782435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5792435880fSJoe Perches	["proc_create(?:_data|)", 2],
580459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
581459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
582459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
583459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
584459cf0aeSJoe Perches	["__ATTR", 2],
5852435880fSJoe Perches);
5862435880fSJoe Perches
587515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
588515a235eSJoe Perchesour $mode_perms_search = "";
589515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
590515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
591515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
592515a235eSJoe Perches}
59300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
594515a235eSJoe Perches
5959189c7e7SJoe Perchesour %deprecated_apis = (
5969189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
5979189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
5989189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
5999189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
6009189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
6019189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
6029189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
6039189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
6049189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
6059189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6069189c7e7SJoe Perches);
6079189c7e7SJoe Perches
6089189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6099189c7e7SJoe Perchesour $deprecated_apis_search = "";
6109189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6119189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6129189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6139189c7e7SJoe Perches}
6149189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6159189c7e7SJoe Perches
616b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
617b392c64fSJoe Perches	S_IWUGO		|
618b392c64fSJoe Perches	S_IWOTH		|
619b392c64fSJoe Perches	S_IRWXUGO	|
620b392c64fSJoe Perches	S_IALLUGO	|
621b392c64fSJoe Perches	0[0-7][0-7][2367]
622b392c64fSJoe Perches}x;
623b392c64fSJoe Perches
624f90774e1SJoe Perchesour %mode_permission_string_types = (
625f90774e1SJoe Perches	"S_IRWXU" => 0700,
626f90774e1SJoe Perches	"S_IRUSR" => 0400,
627f90774e1SJoe Perches	"S_IWUSR" => 0200,
628f90774e1SJoe Perches	"S_IXUSR" => 0100,
629f90774e1SJoe Perches	"S_IRWXG" => 0070,
630f90774e1SJoe Perches	"S_IRGRP" => 0040,
631f90774e1SJoe Perches	"S_IWGRP" => 0020,
632f90774e1SJoe Perches	"S_IXGRP" => 0010,
633f90774e1SJoe Perches	"S_IRWXO" => 0007,
634f90774e1SJoe Perches	"S_IROTH" => 0004,
635f90774e1SJoe Perches	"S_IWOTH" => 0002,
636f90774e1SJoe Perches	"S_IXOTH" => 0001,
637f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
638f90774e1SJoe Perches	"S_IRUGO" => 0444,
639f90774e1SJoe Perches	"S_IWUGO" => 0222,
640f90774e1SJoe Perches	"S_IXUGO" => 0111,
641f90774e1SJoe Perches);
642f90774e1SJoe Perches
643f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
644f90774e1SJoe Perchesour $mode_perms_string_search = "";
645f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
646f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
647f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
648f90774e1SJoe Perches}
64900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
65000180468SJoe Perchesour $multi_mode_perms_string_search = qr{
65100180468SJoe Perches	${single_mode_perms_string_search}
65200180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
65300180468SJoe Perches}x;
65400180468SJoe Perches
65500180468SJoe Perchessub perms_to_octal {
65600180468SJoe Perches	my ($string) = @_;
65700180468SJoe Perches
65800180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
65900180468SJoe Perches
66000180468SJoe Perches	my $val = "";
66100180468SJoe Perches	my $oval = "";
66200180468SJoe Perches	my $to = 0;
66300180468SJoe Perches	my $curpos = 0;
66400180468SJoe Perches	my $lastpos = 0;
66500180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
66600180468SJoe Perches		$curpos = pos($string);
66700180468SJoe Perches		my $match = $2;
66800180468SJoe Perches		my $omatch = $1;
66900180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
67000180468SJoe Perches		$lastpos = $curpos;
67100180468SJoe Perches		$to |= $mode_permission_string_types{$match};
67200180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
67300180468SJoe Perches		$val .= $match;
67400180468SJoe Perches		$oval .= $omatch;
67500180468SJoe Perches	}
67600180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
67700180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
67800180468SJoe Perches	return sprintf("%04o", $to);
67900180468SJoe Perches}
680f90774e1SJoe Perches
6817840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6827840a94cSWolfram Sang	irq|
683cdcee686SSergey Ryazanov	memory|
684cdcee686SSergey Ryazanov	time|
685cdcee686SSergey Ryazanov	reboot
6867840a94cSWolfram Sang)};
6877840a94cSWolfram Sang# memory.h: ARM has a custom one
6887840a94cSWolfram Sang
68966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
69066b47b4aSKees Cookmy $misspellings;
69166b47b4aSKees Cookmy %spelling_fix;
69236061e38SJoe Perches
69336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
69466b47b4aSKees Cook	while (<$spelling>) {
69566b47b4aSKees Cook		my $line = $_;
69666b47b4aSKees Cook
69766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
69866b47b4aSKees Cook		$line =~ s/^\s*//g;
69966b47b4aSKees Cook
70066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
70166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
70266b47b4aSKees Cook
70366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
70466b47b4aSKees Cook
70566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
70666b47b4aSKees Cook	}
70766b47b4aSKees Cook	close($spelling);
70836061e38SJoe Perches} else {
70936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
71036061e38SJoe Perches}
71166b47b4aSKees Cook
712ebfd7d62SJoe Perchesif ($codespell) {
713ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
714ebfd7d62SJoe Perches		while (<$spelling>) {
715ebfd7d62SJoe Perches			my $line = $_;
716ebfd7d62SJoe Perches
717ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
718ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
719ebfd7d62SJoe Perches
720ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
721ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
722ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
723ebfd7d62SJoe Perches
724ebfd7d62SJoe Perches			$line =~ s/,.*$//;
725ebfd7d62SJoe Perches
726ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
727ebfd7d62SJoe Perches
728ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
729ebfd7d62SJoe Perches		}
730ebfd7d62SJoe Perches		close($spelling);
731ebfd7d62SJoe Perches	} else {
732ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
733ebfd7d62SJoe Perches	}
734ebfd7d62SJoe Perches}
735ebfd7d62SJoe Perches
736ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
737ebfd7d62SJoe Perches
73875ad8c57SJerome Forissiersub read_words {
73975ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
74075ad8c57SJerome Forissier
74175ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
74275ad8c57SJerome Forissier		while (<$words>) {
743bf1fa1daSJoe Perches			my $line = $_;
744bf1fa1daSJoe Perches
745bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
746bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
747bf1fa1daSJoe Perches
748bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
749bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
750bf1fa1daSJoe Perches			if ($line =~ /\s/) {
75175ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
752bf1fa1daSJoe Perches				next;
753bf1fa1daSJoe Perches			}
754bf1fa1daSJoe Perches
75575ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
75675ad8c57SJerome Forissier			$$wordsRef .= $line;
757bf1fa1daSJoe Perches		}
75875ad8c57SJerome Forissier		close($file);
75975ad8c57SJerome Forissier		return 1;
760bf1fa1daSJoe Perches	}
761bf1fa1daSJoe Perches
76275ad8c57SJerome Forissier	return 0;
76375ad8c57SJerome Forissier}
76475ad8c57SJerome Forissier
76575ad8c57SJerome Forissiermy $const_structs = "";
76675ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
76775ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
76875ad8c57SJerome Forissier
76975ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
77075ad8c57SJerome Forissierif (length($typedefsfile)) {
77175ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
77275ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
77375ad8c57SJerome Forissier}
77475ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
77575ad8c57SJerome Forissier
7768905a67cSAndy Whitcroftsub build_types {
777485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
778485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7791813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7808716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
781c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
782ab7e23f3SJoe Perches	$BasicType	= qr{
783ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
784ab7e23f3SJoe Perches				(?:${all}\b)
785ab7e23f3SJoe Perches		}x;
7868905a67cSAndy Whitcroft	$NonptrType	= qr{
787d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
788cf655043SAndy Whitcroft			(?:
7896b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7908ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
791c45dcabdSAndy Whitcroft				(?:${all}\b)
792cf655043SAndy Whitcroft			)
793c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7948905a67cSAndy Whitcroft		  }x;
7951813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7961813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7971813087dSJoe Perches			(?:
7981813087dSJoe Perches				(?:${Misordered}\b)
7991813087dSJoe Perches			)
8001813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
8011813087dSJoe Perches		  }x;
8028716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
8038716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
8048716de38SJoe Perches			(?:
8058716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8068716de38SJoe Perches				(?:$typeTypedefs\b)|
8078716de38SJoe Perches				(?:${allWithAttr}\b)
8088716de38SJoe Perches			)
8098716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8108716de38SJoe Perches		  }x;
8118905a67cSAndy Whitcroft	$Type	= qr{
812c45dcabdSAndy Whitcroft			$NonptrType
8137b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
814c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8158905a67cSAndy Whitcroft		  }x;
8161813087dSJoe Perches	$TypeMisordered	= qr{
8171813087dSJoe Perches			$NonptrTypeMisordered
8187b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
8191813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8201813087dSJoe Perches		  }x;
82191cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8221813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8238905a67cSAndy Whitcroft}
8248905a67cSAndy Whitcroftbuild_types();
8256c72ffaaSAndy Whitcroft
8267d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
827d1fe9c09SJoe Perches
828d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
829d1fe9c09SJoe Perches# requires at least perl version v5.10.0
830d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
831d1fe9c09SJoe Perches
832d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8332435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
834c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8357d2367afSJoe Perches
836f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8373e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
838fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8393d102fc0SGilad Ben-Yossef	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
8403d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
841f8422308SJoe Perches)};
842f8422308SJoe Perches
8437d2367afSJoe Perchessub deparenthesize {
8447d2367afSJoe Perches	my ($string) = @_;
8457d2367afSJoe Perches	return "" if (!defined($string));
8465b9553abSJoe Perches
8475b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8485b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8495b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8505b9553abSJoe Perches	}
8515b9553abSJoe Perches
8527d2367afSJoe Perches	$string =~ s@\s+@ @g;
8535b9553abSJoe Perches
8547d2367afSJoe Perches	return $string;
8557d2367afSJoe Perches}
8567d2367afSJoe Perches
8573445686aSJoe Perchessub seed_camelcase_file {
8583445686aSJoe Perches	my ($file) = @_;
8593445686aSJoe Perches
8603445686aSJoe Perches	return if (!(-f $file));
8613445686aSJoe Perches
8623445686aSJoe Perches	local $/;
8633445686aSJoe Perches
8643445686aSJoe Perches	open(my $include_file, '<', "$file")
8653445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8663445686aSJoe Perches	my $text = <$include_file>;
8673445686aSJoe Perches	close($include_file);
8683445686aSJoe Perches
8693445686aSJoe Perches	my @lines = split('\n', $text);
8703445686aSJoe Perches
8713445686aSJoe Perches	foreach my $line (@lines) {
8723445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8733445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8743445686aSJoe Perches			$camelcase{$1} = 1;
87511ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
87611ea516aSJoe Perches			$camelcase{$1} = 1;
87711ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8783445686aSJoe Perches			$camelcase{$1} = 1;
8793445686aSJoe Perches		}
8803445686aSJoe Perches	}
8813445686aSJoe Perches}
8823445686aSJoe Perches
883cd28b119SJoe Perchesour %maintained_status = ();
884cd28b119SJoe Perches
88585b0ee18SJoe Perchessub is_maintained_obsolete {
88685b0ee18SJoe Perches	my ($filename) = @_;
88785b0ee18SJoe Perches
888f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
88985b0ee18SJoe Perches
890cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
891cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
892cd28b119SJoe Perches	}
89385b0ee18SJoe Perches
894cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
89585b0ee18SJoe Perches}
89685b0ee18SJoe Perches
8973b6e8ac9SJoe Perchessub is_SPDX_License_valid {
8983b6e8ac9SJoe Perches	my ($license) = @_;
8993b6e8ac9SJoe Perches
90056294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
9013b6e8ac9SJoe Perches
90256294112SJoe Perches	my $root_path = abs_path($root);
90356294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
9043b6e8ac9SJoe Perches	return 0 if ($status ne "");
9053b6e8ac9SJoe Perches	return 1;
9063b6e8ac9SJoe Perches}
9073b6e8ac9SJoe Perches
9083445686aSJoe Perchesmy $camelcase_seeded = 0;
9093445686aSJoe Perchessub seed_camelcase_includes {
9103445686aSJoe Perches	return if ($camelcase_seeded);
9113445686aSJoe Perches
9123445686aSJoe Perches	my $files;
913c707a81dSJoe Perches	my $camelcase_cache = "";
914c707a81dSJoe Perches	my @include_files = ();
915c707a81dSJoe Perches
916c707a81dSJoe Perches	$camelcase_seeded = 1;
917351b2a1fSJoe Perches
9183645e328SRichard Genoud	if (-e ".git") {
919dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
920351b2a1fSJoe Perches		chomp $git_last_include_commit;
921c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
922c707a81dSJoe Perches	} else {
923c707a81dSJoe Perches		my $last_mod_date = 0;
924c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
925c707a81dSJoe Perches		@include_files = split('\n', $files);
926c707a81dSJoe Perches		foreach my $file (@include_files) {
927c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
928c707a81dSJoe Perches						   localtime((stat $file)[9]));
929c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
930c707a81dSJoe Perches		}
931c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
932c707a81dSJoe Perches	}
933c707a81dSJoe Perches
934c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
935c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
936c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
937351b2a1fSJoe Perches		while (<$camelcase_file>) {
938351b2a1fSJoe Perches			chomp;
939351b2a1fSJoe Perches			$camelcase{$_} = 1;
940351b2a1fSJoe Perches		}
941351b2a1fSJoe Perches		close($camelcase_file);
942351b2a1fSJoe Perches
943351b2a1fSJoe Perches		return;
944351b2a1fSJoe Perches	}
945c707a81dSJoe Perches
9463645e328SRichard Genoud	if (-e ".git") {
947dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
948c707a81dSJoe Perches		@include_files = split('\n', $files);
9493445686aSJoe Perches	}
950c707a81dSJoe Perches
9513445686aSJoe Perches	foreach my $file (@include_files) {
9523445686aSJoe Perches		seed_camelcase_file($file);
9533445686aSJoe Perches	}
954351b2a1fSJoe Perches
955c707a81dSJoe Perches	if ($camelcase_cache ne "") {
956351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
957c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
958c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
959351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
960351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
961351b2a1fSJoe Perches		}
962351b2a1fSJoe Perches		close($camelcase_file);
963351b2a1fSJoe Perches	}
9643445686aSJoe Perches}
9653445686aSJoe Perches
966d311cd44SJoe Perchessub git_commit_info {
967d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
968d311cd44SJoe Perches
969d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
970d311cd44SJoe Perches
971dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
972d311cd44SJoe Perches	$output =~ s/^\s*//gm;
973d311cd44SJoe Perches	my @lines = split("\n", $output);
974d311cd44SJoe Perches
9750d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9760d7835fcSJoe Perches
9775a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
978d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
979d311cd44SJoe Perches# all matching commit ids, but it's very slow...
980d311cd44SJoe Perches#
981d311cd44SJoe Perches#		echo "checking commits $1..."
982d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
983d311cd44SJoe Perches#		while read line ; do
984d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
985d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
986d311cd44SJoe Perches#		done
987d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
988948b133aSHeinrich Schuchardt		$id = undef;
989d311cd44SJoe Perches	} else {
990d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
991d311cd44SJoe Perches		$desc = substr($lines[0], 41);
992d311cd44SJoe Perches	}
993d311cd44SJoe Perches
994d311cd44SJoe Perches	return ($id, $desc);
995d311cd44SJoe Perches}
996d311cd44SJoe Perches
9976c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9980a920b5bSAndy Whitcroft
99900df344fSAndy Whitcroftmy @rawlines = ();
1000c2fdda0dSAndy Whitcroftmy @lines = ();
10013705ce5bSJoe Perchesmy @fixed = ();
1002d752fcc8SJoe Perchesmy @fixed_inserted = ();
1003d752fcc8SJoe Perchesmy @fixed_deleted = ();
1004194f66fcSJoe Perchesmy $fixlinenr = -1;
1005194f66fcSJoe Perches
10064a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10074a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10084a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
10094a593c34SDu, Changbin
10104a593c34SDu, Changbinif ($git) {
10114a593c34SDu, Changbin	my @commits = ();
10120dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
10134a593c34SDu, Changbin		my $git_range;
101428898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
101528898fd1SJoe Perches			$git_range = "-$2 $1";
10164a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
10174a593c34SDu, Changbin			$git_range = "$commit_expr";
10184a593c34SDu, Changbin		} else {
10190dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
10200dea9f1eSJoe Perches		}
1021dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10220dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
102328898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
102428898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10250dea9f1eSJoe Perches			my $sha1 = $1;
10260dea9f1eSJoe Perches			my $subject = $2;
10270dea9f1eSJoe Perches			unshift(@commits, $sha1);
10280dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10294a593c34SDu, Changbin		}
10304a593c34SDu, Changbin	}
10314a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10324a593c34SDu, Changbin	@ARGV = @commits;
10334a593c34SDu, Changbin}
10344a593c34SDu, Changbin
1035c2fdda0dSAndy Whitcroftmy $vname;
103698005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
10376c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
103821caa13cSAndy Whitcroft	my $FILE;
10394a593c34SDu, Changbin	if ($git) {
10404a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10414a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10424a593c34SDu, Changbin	} elsif ($file) {
104321caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10446c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
104521caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
104621caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10476c72ffaaSAndy Whitcroft	} else {
104821caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10496c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10506c72ffaaSAndy Whitcroft	}
1051c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1052c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10534a593c34SDu, Changbin	} elsif ($git) {
10540dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1055c2fdda0dSAndy Whitcroft	} else {
1056c2fdda0dSAndy Whitcroft		$vname = $filename;
1057c2fdda0dSAndy Whitcroft	}
105821caa13cSAndy Whitcroft	while (<$FILE>) {
10590a920b5bSAndy Whitcroft		chomp;
106000df344fSAndy Whitcroft		push(@rawlines, $_);
10616c72ffaaSAndy Whitcroft	}
106221caa13cSAndy Whitcroft	close($FILE);
1063d8469f16SJoe Perches
1064d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1065d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1066d8469f16SJoe Perches		print "$vname\n";
1067d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1068d8469f16SJoe Perches	}
1069d8469f16SJoe Perches
1070c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10710a920b5bSAndy Whitcroft		$exit = 1;
10720a920b5bSAndy Whitcroft	}
107300df344fSAndy Whitcroft	@rawlines = ();
107413214adfSAndy Whitcroft	@lines = ();
10753705ce5bSJoe Perches	@fixed = ();
1076d752fcc8SJoe Perches	@fixed_inserted = ();
1077d752fcc8SJoe Perches	@fixed_deleted = ();
1078194f66fcSJoe Perches	$fixlinenr = -1;
1079485ff23eSAlex Dowad	@modifierListFile = ();
1080485ff23eSAlex Dowad	@typeListFile = ();
1081485ff23eSAlex Dowad	build_types();
10820a920b5bSAndy Whitcroft}
10830a920b5bSAndy Whitcroft
1084d8469f16SJoe Perchesif (!$quiet) {
10853c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10863c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10873c816e49SJoe Perches
10885b57980dSJoe Perches	if (!$perl_version_ok) {
1089d8469f16SJoe Perches		print << "EOM"
1090d8469f16SJoe Perches
1091d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
10925b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1093d8469f16SJoe PerchesEOM
1094d8469f16SJoe Perches	}
1095d8469f16SJoe Perches	if ($exit) {
1096d8469f16SJoe Perches		print << "EOM"
1097d8469f16SJoe Perches
1098d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1099d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1100d8469f16SJoe PerchesEOM
1101d8469f16SJoe Perches	}
1102d8469f16SJoe Perches}
1103d8469f16SJoe Perches
11040a920b5bSAndy Whitcroftexit($exit);
11050a920b5bSAndy Whitcroft
11060a920b5bSAndy Whitcroftsub top_of_kernel_tree {
11076c72ffaaSAndy Whitcroft	my ($root) = @_;
11086c72ffaaSAndy Whitcroft
11096c72ffaaSAndy Whitcroft	my @tree_check = (
11106c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
11116c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
11126c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
11136c72ffaaSAndy Whitcroft	);
11146c72ffaaSAndy Whitcroft
11156c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
11166c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
11170a920b5bSAndy Whitcroft			return 0;
11180a920b5bSAndy Whitcroft		}
11196c72ffaaSAndy Whitcroft	}
11206c72ffaaSAndy Whitcroft	return 1;
11216c72ffaaSAndy Whitcroft}
11220a920b5bSAndy Whitcroft
112320112475SJoe Perchessub parse_email {
112420112475SJoe Perches	my ($formatted_email) = @_;
112520112475SJoe Perches
112620112475SJoe Perches	my $name = "";
1127dfa05c28SJoe Perches	my $name_comment = "";
112820112475SJoe Perches	my $address = "";
112920112475SJoe Perches	my $comment = "";
113020112475SJoe Perches
113120112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
113220112475SJoe Perches		$name = $1;
113320112475SJoe Perches		$address = $2;
113420112475SJoe Perches		$comment = $3 if defined $3;
113520112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
113620112475SJoe Perches		$address = $1;
113720112475SJoe Perches		$comment = $2 if defined $2;
113820112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
113920112475SJoe Perches		$address = $1;
114020112475SJoe Perches		$comment = $2 if defined $2;
114185e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
114220112475SJoe Perches		$name = $formatted_email;
11433705ce5bSJoe Perches		$name = trim($name);
114420112475SJoe Perches		$name =~ s/^\"|\"$//g;
114520112475SJoe Perches		# If there's a name left after stripping spaces and
114620112475SJoe Perches		# leading quotes, and the address doesn't have both
114720112475SJoe Perches		# leading and trailing angle brackets, the address
114820112475SJoe Perches		# is invalid. ie:
114920112475SJoe Perches		#   "joe smith [email protected]" bad
115020112475SJoe Perches		#   "joe smith <[email protected]" bad
115120112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
115220112475SJoe Perches			$name = "";
115320112475SJoe Perches			$address = "";
115420112475SJoe Perches			$comment = "";
115520112475SJoe Perches		}
115620112475SJoe Perches	}
115720112475SJoe Perches
11583705ce5bSJoe Perches	$name = trim($name);
115920112475SJoe Perches	$name =~ s/^\"|\"$//g;
1160dfa05c28SJoe Perches	$name =~ s/(\s*\([^\)]+\))\s*//;
1161dfa05c28SJoe Perches	if (defined($1)) {
1162dfa05c28SJoe Perches		$name_comment = trim($1);
1163dfa05c28SJoe Perches	}
11643705ce5bSJoe Perches	$address = trim($address);
116520112475SJoe Perches	$address =~ s/^\<|\>$//g;
116620112475SJoe Perches
116720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
116820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
116920112475SJoe Perches		$name = "\"$name\"";
117020112475SJoe Perches	}
117120112475SJoe Perches
1172dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
117320112475SJoe Perches}
117420112475SJoe Perches
117520112475SJoe Perchessub format_email {
117620112475SJoe Perches	my ($name, $address) = @_;
117720112475SJoe Perches
117820112475SJoe Perches	my $formatted_email;
117920112475SJoe Perches
11803705ce5bSJoe Perches	$name = trim($name);
118120112475SJoe Perches	$name =~ s/^\"|\"$//g;
11823705ce5bSJoe Perches	$address = trim($address);
118320112475SJoe Perches
118420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
118520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
118620112475SJoe Perches		$name = "\"$name\"";
118720112475SJoe Perches	}
118820112475SJoe Perches
118920112475SJoe Perches	if ("$name" eq "") {
119020112475SJoe Perches		$formatted_email = "$address";
119120112475SJoe Perches	} else {
119220112475SJoe Perches		$formatted_email = "$name <$address>";
119320112475SJoe Perches	}
119420112475SJoe Perches
119520112475SJoe Perches	return $formatted_email;
119620112475SJoe Perches}
119720112475SJoe Perches
1198dfa05c28SJoe Perchessub reformat_email {
1199dfa05c28SJoe Perches	my ($email) = @_;
1200dfa05c28SJoe Perches
1201dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1202dfa05c28SJoe Perches	return format_email($email_name, $email_address);
1203dfa05c28SJoe Perches}
1204dfa05c28SJoe Perches
1205dfa05c28SJoe Perchessub same_email_addresses {
1206dfa05c28SJoe Perches	my ($email1, $email2) = @_;
1207dfa05c28SJoe Perches
1208dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1209dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1210dfa05c28SJoe Perches
1211dfa05c28SJoe Perches	return $email1_name eq $email2_name &&
1212dfa05c28SJoe Perches	       $email1_address eq $email2_address;
1213dfa05c28SJoe Perches}
1214dfa05c28SJoe Perches
1215d311cd44SJoe Perchessub which {
1216d311cd44SJoe Perches	my ($bin) = @_;
1217d311cd44SJoe Perches
1218d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1219d311cd44SJoe Perches		if (-e "$path/$bin") {
1220d311cd44SJoe Perches			return "$path/$bin";
1221d311cd44SJoe Perches		}
1222d311cd44SJoe Perches	}
1223d311cd44SJoe Perches
1224d311cd44SJoe Perches	return "";
1225d311cd44SJoe Perches}
1226d311cd44SJoe Perches
1227000d1cc1SJoe Perchessub which_conf {
1228000d1cc1SJoe Perches	my ($conf) = @_;
1229000d1cc1SJoe Perches
1230000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1231000d1cc1SJoe Perches		if (-e "$path/$conf") {
1232000d1cc1SJoe Perches			return "$path/$conf";
1233000d1cc1SJoe Perches		}
1234000d1cc1SJoe Perches	}
1235000d1cc1SJoe Perches
1236000d1cc1SJoe Perches	return "";
1237000d1cc1SJoe Perches}
1238000d1cc1SJoe Perches
12390a920b5bSAndy Whitcroftsub expand_tabs {
12400a920b5bSAndy Whitcroft	my ($str) = @_;
12410a920b5bSAndy Whitcroft
12420a920b5bSAndy Whitcroft	my $res = '';
12430a920b5bSAndy Whitcroft	my $n = 0;
12440a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12450a920b5bSAndy Whitcroft		if ($c eq "\t") {
12460a920b5bSAndy Whitcroft			$res .= ' ';
12470a920b5bSAndy Whitcroft			$n++;
1248713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
12490a920b5bSAndy Whitcroft				$res .= ' ';
12500a920b5bSAndy Whitcroft			}
12510a920b5bSAndy Whitcroft			next;
12520a920b5bSAndy Whitcroft		}
12530a920b5bSAndy Whitcroft		$res .= $c;
12540a920b5bSAndy Whitcroft		$n++;
12550a920b5bSAndy Whitcroft	}
12560a920b5bSAndy Whitcroft
12570a920b5bSAndy Whitcroft	return $res;
12580a920b5bSAndy Whitcroft}
12596c72ffaaSAndy Whitcroftsub copy_spacing {
1260773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
12616c72ffaaSAndy Whitcroft	return $res;
12626c72ffaaSAndy Whitcroft}
12630a920b5bSAndy Whitcroft
12644a0df2efSAndy Whitcroftsub line_stats {
12654a0df2efSAndy Whitcroft	my ($line) = @_;
12664a0df2efSAndy Whitcroft
12674a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12684a0df2efSAndy Whitcroft	$line =~ s/^.//;
12694a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12704a0df2efSAndy Whitcroft
12714a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12724a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12734a0df2efSAndy Whitcroft
12744a0df2efSAndy Whitcroft	return (length($line), length($white));
12754a0df2efSAndy Whitcroft}
12764a0df2efSAndy Whitcroft
1277773647a0SAndy Whitcroftmy $sanitise_quote = '';
1278773647a0SAndy Whitcroft
1279773647a0SAndy Whitcroftsub sanitise_line_reset {
1280773647a0SAndy Whitcroft	my ($in_comment) = @_;
1281773647a0SAndy Whitcroft
1282773647a0SAndy Whitcroft	if ($in_comment) {
1283773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1284773647a0SAndy Whitcroft	} else {
1285773647a0SAndy Whitcroft		$sanitise_quote = '';
1286773647a0SAndy Whitcroft	}
1287773647a0SAndy Whitcroft}
128800df344fSAndy Whitcroftsub sanitise_line {
128900df344fSAndy Whitcroft	my ($line) = @_;
129000df344fSAndy Whitcroft
129100df344fSAndy Whitcroft	my $res = '';
129200df344fSAndy Whitcroft	my $l = '';
129300df344fSAndy Whitcroft
1294c2fdda0dSAndy Whitcroft	my $qlen = 0;
1295773647a0SAndy Whitcroft	my $off = 0;
1296773647a0SAndy Whitcroft	my $c;
129700df344fSAndy Whitcroft
1298773647a0SAndy Whitcroft	# Always copy over the diff marker.
1299773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1300773647a0SAndy Whitcroft
1301773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1302773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1303773647a0SAndy Whitcroft
13048d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1305773647a0SAndy Whitcroft		# and end, all to $;.
1306773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1307773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1308773647a0SAndy Whitcroft
1309773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1310773647a0SAndy Whitcroft			$off++;
131100df344fSAndy Whitcroft			next;
1312773647a0SAndy Whitcroft		}
131381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1314773647a0SAndy Whitcroft			$sanitise_quote = '';
1315773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1316773647a0SAndy Whitcroft			$off++;
1317773647a0SAndy Whitcroft			next;
1318773647a0SAndy Whitcroft		}
1319113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1320113f04a8SDaniel Walker			$sanitise_quote = '//';
1321113f04a8SDaniel Walker
1322113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1323113f04a8SDaniel Walker			$off++;
1324113f04a8SDaniel Walker			next;
1325113f04a8SDaniel Walker		}
1326773647a0SAndy Whitcroft
1327773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1328773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1329773647a0SAndy Whitcroft		    $c eq "\\") {
1330773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1331773647a0SAndy Whitcroft			$off++;
1332773647a0SAndy Whitcroft			next;
1333773647a0SAndy Whitcroft		}
1334773647a0SAndy Whitcroft		# Regular quotes.
1335773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1336773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1337773647a0SAndy Whitcroft				$sanitise_quote = $c;
1338773647a0SAndy Whitcroft
1339773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1340773647a0SAndy Whitcroft				next;
1341773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1342773647a0SAndy Whitcroft				$sanitise_quote = '';
134300df344fSAndy Whitcroft			}
134400df344fSAndy Whitcroft		}
1345773647a0SAndy Whitcroft
1346fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1347773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1348773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1349113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1350113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1351773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1352773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
135300df344fSAndy Whitcroft		} else {
1354773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
135500df344fSAndy Whitcroft		}
1356c2fdda0dSAndy Whitcroft	}
1357c2fdda0dSAndy Whitcroft
1358113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1359113f04a8SDaniel Walker		$sanitise_quote = '';
1360113f04a8SDaniel Walker	}
1361113f04a8SDaniel Walker
1362c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1363c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1364c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1365c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1366c2fdda0dSAndy Whitcroft
1367c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1368c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1369c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1370c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1371c2fdda0dSAndy Whitcroft	}
1372c2fdda0dSAndy Whitcroft
1373dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1374dadf680dSJoe Perches		my $match = $1;
1375dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1376dadf680dSJoe Perches	}
1377dadf680dSJoe Perches
137800df344fSAndy Whitcroft	return $res;
137900df344fSAndy Whitcroft}
138000df344fSAndy Whitcroft
1381a6962d72SJoe Perchessub get_quoted_string {
1382a6962d72SJoe Perches	my ($line, $rawline) = @_;
1383a6962d72SJoe Perches
1384478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
138533acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1386a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1387a6962d72SJoe Perches}
1388a6962d72SJoe Perches
13898905a67cSAndy Whitcroftsub ctx_statement_block {
13908905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13918905a67cSAndy Whitcroft	my $line = $linenr - 1;
13928905a67cSAndy Whitcroft	my $blk = '';
13938905a67cSAndy Whitcroft	my $soff = $off;
13948905a67cSAndy Whitcroft	my $coff = $off - 1;
1395773647a0SAndy Whitcroft	my $coff_set = 0;
13968905a67cSAndy Whitcroft
139713214adfSAndy Whitcroft	my $loff = 0;
139813214adfSAndy Whitcroft
13998905a67cSAndy Whitcroft	my $type = '';
14008905a67cSAndy Whitcroft	my $level = 0;
1401a2750645SAndy Whitcroft	my @stack = ();
1402cf655043SAndy Whitcroft	my $p;
14038905a67cSAndy Whitcroft	my $c;
14048905a67cSAndy Whitcroft	my $len = 0;
140513214adfSAndy Whitcroft
140613214adfSAndy Whitcroft	my $remainder;
14078905a67cSAndy Whitcroft	while (1) {
1408a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1409a2750645SAndy Whitcroft
1410773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
14118905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
14128905a67cSAndy Whitcroft		# context.
14138905a67cSAndy Whitcroft		if ($off >= $len) {
14148905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1415dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1416c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
14178905a67cSAndy Whitcroft				$remain--;
141813214adfSAndy Whitcroft				$loff = $len;
1419c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
14208905a67cSAndy Whitcroft				$len = length($blk);
14218905a67cSAndy Whitcroft				$line++;
14228905a67cSAndy Whitcroft				last;
14238905a67cSAndy Whitcroft			}
14248905a67cSAndy Whitcroft			# Bail if there is no further context.
14258905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
142613214adfSAndy Whitcroft			if ($off >= $len) {
14278905a67cSAndy Whitcroft				last;
14288905a67cSAndy Whitcroft			}
1429f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1430f74bd194SAndy Whitcroft				$level++;
1431f74bd194SAndy Whitcroft				$type = '#';
1432f74bd194SAndy Whitcroft			}
14338905a67cSAndy Whitcroft		}
1434cf655043SAndy Whitcroft		$p = $c;
14358905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
143613214adfSAndy Whitcroft		$remainder = substr($blk, $off);
14378905a67cSAndy Whitcroft
1438773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
14394635f4fbSAndy Whitcroft
14404635f4fbSAndy Whitcroft		# Handle nested #if/#else.
14414635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
14424635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
14434635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
14444635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14454635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14464635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14474635f4fbSAndy Whitcroft		}
14484635f4fbSAndy Whitcroft
14498905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14508905a67cSAndy Whitcroft		# outermost level.
14518905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
14528905a67cSAndy Whitcroft			last;
14538905a67cSAndy Whitcroft		}
14548905a67cSAndy Whitcroft
145513214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1456773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1457773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1458773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1459773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1460773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1461773647a0SAndy Whitcroft			$coff_set = 1;
1462773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1463773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
146413214adfSAndy Whitcroft		}
146513214adfSAndy Whitcroft
14668905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14678905a67cSAndy Whitcroft			$level++;
14688905a67cSAndy Whitcroft			$type = '(';
14698905a67cSAndy Whitcroft		}
14708905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14718905a67cSAndy Whitcroft			$level--;
14728905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14738905a67cSAndy Whitcroft
14748905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
14758905a67cSAndy Whitcroft				$coff = $off;
1476773647a0SAndy Whitcroft				$coff_set = 1;
1477773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
14788905a67cSAndy Whitcroft			}
14798905a67cSAndy Whitcroft		}
14808905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14818905a67cSAndy Whitcroft			$level++;
14828905a67cSAndy Whitcroft			$type = '{';
14838905a67cSAndy Whitcroft		}
14848905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14858905a67cSAndy Whitcroft			$level--;
14868905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14878905a67cSAndy Whitcroft
14888905a67cSAndy Whitcroft			if ($level == 0) {
1489b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1490b998e001SPatrick Pannuto					$off++;
1491b998e001SPatrick Pannuto				}
14928905a67cSAndy Whitcroft				last;
14938905a67cSAndy Whitcroft			}
14948905a67cSAndy Whitcroft		}
1495f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1496f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1497f74bd194SAndy Whitcroft			$level--;
1498f74bd194SAndy Whitcroft			$type = '';
1499f74bd194SAndy Whitcroft			$off++;
1500f74bd194SAndy Whitcroft			last;
1501f74bd194SAndy Whitcroft		}
15028905a67cSAndy Whitcroft		$off++;
15038905a67cSAndy Whitcroft	}
1504a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
150513214adfSAndy Whitcroft	if ($off == $len) {
1506a3bb97a7SAndy Whitcroft		$loff = $len + 1;
150713214adfSAndy Whitcroft		$line++;
150813214adfSAndy Whitcroft		$remain--;
150913214adfSAndy Whitcroft	}
15108905a67cSAndy Whitcroft
15118905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
15128905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
15138905a67cSAndy Whitcroft
15148905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
15158905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
15168905a67cSAndy Whitcroft
1517773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
151813214adfSAndy Whitcroft
151913214adfSAndy Whitcroft	return ($statement, $condition,
152013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
152113214adfSAndy Whitcroft}
152213214adfSAndy Whitcroft
1523cf655043SAndy Whitcroftsub statement_lines {
1524cf655043SAndy Whitcroft	my ($stmt) = @_;
1525cf655043SAndy Whitcroft
1526cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1527cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1528cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1529cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1530cf655043SAndy Whitcroft
1531cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1532cf655043SAndy Whitcroft
1533cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1534cf655043SAndy Whitcroft}
1535cf655043SAndy Whitcroft
1536cf655043SAndy Whitcroftsub statement_rawlines {
1537cf655043SAndy Whitcroft	my ($stmt) = @_;
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_block_size {
1545cf655043SAndy Whitcroft	my ($stmt) = @_;
1546cf655043SAndy Whitcroft
1547cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1548cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1549cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1550cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1551cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1552cf655043SAndy Whitcroft
1553cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1554cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1555cf655043SAndy Whitcroft
1556cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1557cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1558cf655043SAndy Whitcroft
1559cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1560cf655043SAndy Whitcroft		return $stmt_lines;
1561cf655043SAndy Whitcroft	} else {
1562cf655043SAndy Whitcroft		return $stmt_statements;
1563cf655043SAndy Whitcroft	}
1564cf655043SAndy Whitcroft}
1565cf655043SAndy Whitcroft
156613214adfSAndy Whitcroftsub ctx_statement_full {
156713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
156813214adfSAndy Whitcroft	my ($statement, $condition, $level);
156913214adfSAndy Whitcroft
157013214adfSAndy Whitcroft	my (@chunks);
157113214adfSAndy Whitcroft
1572cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
157313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
157413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1575773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
157613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1577cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1578cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1579cf655043SAndy Whitcroft	}
1580cf655043SAndy Whitcroft
1581cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1582cf655043SAndy Whitcroft	# could continue the statement.
1583cf655043SAndy Whitcroft	for (;;) {
158413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
158513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1586cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1587773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1588cf655043SAndy Whitcroft		#print "C: push\n";
1589cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
159013214adfSAndy Whitcroft	}
159113214adfSAndy Whitcroft
159213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15938905a67cSAndy Whitcroft}
15948905a67cSAndy Whitcroft
15954a0df2efSAndy Whitcroftsub ctx_block_get {
1596f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15974a0df2efSAndy Whitcroft	my $line;
15984a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15994a0df2efSAndy Whitcroft	my $blk = '';
16004a0df2efSAndy Whitcroft	my @o;
16014a0df2efSAndy Whitcroft	my @c;
16024a0df2efSAndy Whitcroft	my @res = ();
16034a0df2efSAndy Whitcroft
1604f0a594c1SAndy Whitcroft	my $level = 0;
16054635f4fbSAndy Whitcroft	my @stack = ($level);
160600df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
160700df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
160800df344fSAndy Whitcroft		$remain--;
160900df344fSAndy Whitcroft
161000df344fSAndy Whitcroft		$blk .= $rawlines[$line];
16114635f4fbSAndy Whitcroft
16124635f4fbSAndy Whitcroft		# Handle nested #if/#else.
161301464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
16144635f4fbSAndy Whitcroft			push(@stack, $level);
161501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
16164635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
161701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
16184635f4fbSAndy Whitcroft			$level = pop(@stack);
16194635f4fbSAndy Whitcroft		}
16204635f4fbSAndy Whitcroft
162101464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1622f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1623f0a594c1SAndy Whitcroft			if ($off > 0) {
1624f0a594c1SAndy Whitcroft				$off--;
1625f0a594c1SAndy Whitcroft				next;
1626f0a594c1SAndy Whitcroft			}
16274a0df2efSAndy Whitcroft
1628f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1629f0a594c1SAndy Whitcroft				$level--;
1630f0a594c1SAndy Whitcroft				last if ($level == 0);
1631f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1632f0a594c1SAndy Whitcroft				$level++;
1633f0a594c1SAndy Whitcroft			}
1634f0a594c1SAndy Whitcroft		}
16354a0df2efSAndy Whitcroft
1636f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
163700df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
16384a0df2efSAndy Whitcroft		}
16394a0df2efSAndy Whitcroft
1640f0a594c1SAndy Whitcroft		last if ($level == 0);
16414a0df2efSAndy Whitcroft	}
16424a0df2efSAndy Whitcroft
1643f0a594c1SAndy Whitcroft	return ($level, @res);
16444a0df2efSAndy Whitcroft}
16454a0df2efSAndy Whitcroftsub ctx_block_outer {
16464a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16474a0df2efSAndy Whitcroft
1648f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1649f0a594c1SAndy Whitcroft	return @r;
16504a0df2efSAndy Whitcroft}
16514a0df2efSAndy Whitcroftsub ctx_block {
16524a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16534a0df2efSAndy Whitcroft
1654f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1655f0a594c1SAndy Whitcroft	return @r;
1656653d4876SAndy Whitcroft}
1657653d4876SAndy Whitcroftsub ctx_statement {
1658f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1659f0a594c1SAndy Whitcroft
1660f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1661f0a594c1SAndy Whitcroft	return @r;
1662f0a594c1SAndy Whitcroft}
1663f0a594c1SAndy Whitcroftsub ctx_block_level {
1664653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1665653d4876SAndy Whitcroft
1666f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16674a0df2efSAndy Whitcroft}
16689c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16699c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16709c0ca6f9SAndy Whitcroft
16719c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16729c0ca6f9SAndy Whitcroft}
16734a0df2efSAndy Whitcroft
16744a0df2efSAndy Whitcroftsub ctx_locate_comment {
16754a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16764a0df2efSAndy Whitcroft
16774a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1678beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
16794a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16804a0df2efSAndy Whitcroft
16814a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16824a0df2efSAndy Whitcroft	# comment.
16834a0df2efSAndy Whitcroft	my $in_comment = 0;
16844a0df2efSAndy Whitcroft	$current_comment = '';
16854a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
168600df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
168700df344fSAndy Whitcroft		#warn "           $line\n";
16884a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16894a0df2efSAndy Whitcroft			$in_comment = 1;
16904a0df2efSAndy Whitcroft		}
16914a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16924a0df2efSAndy Whitcroft			$in_comment = 1;
16934a0df2efSAndy Whitcroft		}
16944a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16954a0df2efSAndy Whitcroft			$current_comment = '';
16964a0df2efSAndy Whitcroft		}
16974a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16984a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16994a0df2efSAndy Whitcroft			$in_comment = 0;
17004a0df2efSAndy Whitcroft		}
17014a0df2efSAndy Whitcroft	}
17024a0df2efSAndy Whitcroft
17034a0df2efSAndy Whitcroft	chomp($current_comment);
17044a0df2efSAndy Whitcroft	return($current_comment);
17054a0df2efSAndy Whitcroft}
17064a0df2efSAndy Whitcroftsub ctx_has_comment {
17074a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
17084a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
17094a0df2efSAndy Whitcroft
171000df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
17114a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
17124a0df2efSAndy Whitcroft
17134a0df2efSAndy Whitcroft	return ($cmt ne '');
17144a0df2efSAndy Whitcroft}
17154a0df2efSAndy Whitcroft
17164d001e4dSAndy Whitcroftsub raw_line {
17174d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
17184d001e4dSAndy Whitcroft
17194d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
17204d001e4dSAndy Whitcroft	$cnt++;
17214d001e4dSAndy Whitcroft
17224d001e4dSAndy Whitcroft	my $line;
17234d001e4dSAndy Whitcroft	while ($cnt) {
17244d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
17254d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
17264d001e4dSAndy Whitcroft		$cnt--;
17274d001e4dSAndy Whitcroft	}
17284d001e4dSAndy Whitcroft
17294d001e4dSAndy Whitcroft	return $line;
17304d001e4dSAndy Whitcroft}
17314d001e4dSAndy Whitcroft
17322a9f9d85STobin C. Hardingsub get_stat_real {
17332a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
17342a9f9d85STobin C. Harding
17352a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
17362a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
17372a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
17382a9f9d85STobin C. Harding	}
17392a9f9d85STobin C. Harding
17402a9f9d85STobin C. Harding	return $stat_real;
17412a9f9d85STobin C. Harding}
17422a9f9d85STobin C. Harding
1743e3d95a2aSTobin C. Hardingsub get_stat_here {
1744e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1745e3d95a2aSTobin C. Harding
1746e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1747e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1748e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1749e3d95a2aSTobin C. Harding	}
1750e3d95a2aSTobin C. Harding
1751e3d95a2aSTobin C. Harding	return $herectx;
1752e3d95a2aSTobin C. Harding}
1753e3d95a2aSTobin C. Harding
17540a920b5bSAndy Whitcroftsub cat_vet {
17550a920b5bSAndy Whitcroft	my ($vet) = @_;
17569c0ca6f9SAndy Whitcroft	my ($res, $coded);
17570a920b5bSAndy Whitcroft
17589c0ca6f9SAndy Whitcroft	$res = '';
17596c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
17606c72ffaaSAndy Whitcroft		$res .= $1;
17616c72ffaaSAndy Whitcroft		if ($2 ne '') {
17629c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
17636c72ffaaSAndy Whitcroft			$res .= $coded;
17646c72ffaaSAndy Whitcroft		}
17659c0ca6f9SAndy Whitcroft	}
17669c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
17670a920b5bSAndy Whitcroft
17689c0ca6f9SAndy Whitcroft	return $res;
17690a920b5bSAndy Whitcroft}
17700a920b5bSAndy Whitcroft
1771c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1772cf655043SAndy Whitcroftmy $av_pending;
1773c2fdda0dSAndy Whitcroftmy @av_paren_type;
17741f65f947SAndy Whitcroftmy $av_pend_colon;
1775c2fdda0dSAndy Whitcroft
1776c2fdda0dSAndy Whitcroftsub annotate_reset {
1777c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1778cf655043SAndy Whitcroft	$av_pending = '_';
1779cf655043SAndy Whitcroft	@av_paren_type = ('E');
17801f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1781c2fdda0dSAndy Whitcroft}
1782c2fdda0dSAndy Whitcroft
17836c72ffaaSAndy Whitcroftsub annotate_values {
17846c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
17856c72ffaaSAndy Whitcroft
17866c72ffaaSAndy Whitcroft	my $res;
17871f65f947SAndy Whitcroft	my $var = '_' x length($stream);
17886c72ffaaSAndy Whitcroft	my $cur = $stream;
17896c72ffaaSAndy Whitcroft
1790c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
17916c72ffaaSAndy Whitcroft
17926c72ffaaSAndy Whitcroft	while (length($cur)) {
1793773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1794cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1795171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
17966c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1797c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1798c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1799cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1800c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
18016c72ffaaSAndy Whitcroft			}
18026c72ffaaSAndy Whitcroft
1803c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
18049446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
18059446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1806addcdceaSAndy Whitcroft			$type = 'c';
18079446ef56SAndy Whitcroft
1808e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1809c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
18106c72ffaaSAndy Whitcroft			$type = 'T';
18116c72ffaaSAndy Whitcroft
1812389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1813389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1814389a2fe5SAndy Whitcroft			$type = 'T';
1815389a2fe5SAndy Whitcroft
1816c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1817171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1818c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1819171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1820171ae1a4SAndy Whitcroft			if ($2 ne '') {
1821cf655043SAndy Whitcroft				$av_pending = 'N';
1822171ae1a4SAndy Whitcroft			}
1823171ae1a4SAndy Whitcroft			$type = 'E';
1824171ae1a4SAndy Whitcroft
1825c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1826171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1827171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1828171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
18296c72ffaaSAndy Whitcroft
1830c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1831cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1832c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1833cf655043SAndy Whitcroft
1834cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1835cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1836171ae1a4SAndy Whitcroft			$type = 'E';
1837cf655043SAndy Whitcroft
1838c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1839cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1840cf655043SAndy Whitcroft			$av_preprocessor = 1;
1841cf655043SAndy Whitcroft
1842cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1843cf655043SAndy Whitcroft
1844171ae1a4SAndy Whitcroft			$type = 'E';
1845cf655043SAndy Whitcroft
1846c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1847cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1848cf655043SAndy Whitcroft
1849cf655043SAndy Whitcroft			$av_preprocessor = 1;
1850cf655043SAndy Whitcroft
1851cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1852cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1853cf655043SAndy Whitcroft			pop(@av_paren_type);
1854cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1855171ae1a4SAndy Whitcroft			$type = 'E';
18566c72ffaaSAndy Whitcroft
18576c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1858c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
18596c72ffaaSAndy Whitcroft
1860171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1861171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1862171ae1a4SAndy Whitcroft			$av_pending = $type;
1863171ae1a4SAndy Whitcroft			$type = 'N';
1864171ae1a4SAndy Whitcroft
18656c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1866c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
18676c72ffaaSAndy Whitcroft			if (defined $2) {
1868cf655043SAndy Whitcroft				$av_pending = 'V';
18696c72ffaaSAndy Whitcroft			}
18706c72ffaaSAndy Whitcroft			$type = 'N';
18716c72ffaaSAndy Whitcroft
187214b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1873c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
187414b111c1SAndy Whitcroft			$av_pending = 'E';
18756c72ffaaSAndy Whitcroft			$type = 'N';
18766c72ffaaSAndy Whitcroft
18771f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
18781f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
18791f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18801f65f947SAndy Whitcroft			$type = 'N';
18811f65f947SAndy Whitcroft
188214b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1883c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
18846c72ffaaSAndy Whitcroft			$type = 'N';
18856c72ffaaSAndy Whitcroft
18866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1887c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1888cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1889cf655043SAndy Whitcroft			$av_pending = '_';
18906c72ffaaSAndy Whitcroft			$type = 'N';
18916c72ffaaSAndy Whitcroft
18926c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1893cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1894cf655043SAndy Whitcroft			if ($new_type ne '_') {
1895cf655043SAndy Whitcroft				$type = $new_type;
1896c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1897c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
18986c72ffaaSAndy Whitcroft			} else {
1899c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
19006c72ffaaSAndy Whitcroft			}
19016c72ffaaSAndy Whitcroft
1902c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1903c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1904c8cb2ca3SAndy Whitcroft			$type = 'V';
1905cf655043SAndy Whitcroft			$av_pending = 'V';
19066c72ffaaSAndy Whitcroft
19078e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
19088e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
19091f65f947SAndy Whitcroft				$av_pend_colon = 'B';
19108e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
19118e761b04SAndy Whitcroft				$av_pend_colon = 'L';
19121f65f947SAndy Whitcroft			}
19131f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
19141f65f947SAndy Whitcroft			$type = 'V';
19151f65f947SAndy Whitcroft
19166c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1917c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
19186c72ffaaSAndy Whitcroft			$type = 'V';
19196c72ffaaSAndy Whitcroft
19206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1921c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
19226c72ffaaSAndy Whitcroft			$type = 'N';
19236c72ffaaSAndy Whitcroft
1924cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1925c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
192613214adfSAndy Whitcroft			$type = 'E';
19271f65f947SAndy Whitcroft			$av_pend_colon = 'O';
192813214adfSAndy Whitcroft
19298e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
19308e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
19318e761b04SAndy Whitcroft			$type = 'C';
19328e761b04SAndy Whitcroft
19331f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
19341f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
19351f65f947SAndy Whitcroft			$type = 'N';
19361f65f947SAndy Whitcroft
19371f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
19381f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
19391f65f947SAndy Whitcroft
19401f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
19411f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
19421f65f947SAndy Whitcroft				$type = 'E';
19431f65f947SAndy Whitcroft			} else {
19441f65f947SAndy Whitcroft				$type = 'N';
19451f65f947SAndy Whitcroft			}
19461f65f947SAndy Whitcroft			$av_pend_colon = 'O';
19471f65f947SAndy Whitcroft
19488e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
194913214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
19506c72ffaaSAndy Whitcroft			$type = 'N';
19516c72ffaaSAndy Whitcroft
19520d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
195374048ed8SAndy Whitcroft			my $variant;
195474048ed8SAndy Whitcroft
195574048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
195674048ed8SAndy Whitcroft			if ($type eq 'V') {
195774048ed8SAndy Whitcroft				$variant = 'B';
195874048ed8SAndy Whitcroft			} else {
195974048ed8SAndy Whitcroft				$variant = 'U';
196074048ed8SAndy Whitcroft			}
196174048ed8SAndy Whitcroft
196274048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
196374048ed8SAndy Whitcroft			$type = 'N';
196474048ed8SAndy Whitcroft
19656c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1966c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
19676c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
19686c72ffaaSAndy Whitcroft				$type = 'N';
19696c72ffaaSAndy Whitcroft			}
19706c72ffaaSAndy Whitcroft
19716c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1972c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
19736c72ffaaSAndy Whitcroft		}
19746c72ffaaSAndy Whitcroft		if (defined $1) {
19756c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
19766c72ffaaSAndy Whitcroft			$res .= $type x length($1);
19776c72ffaaSAndy Whitcroft		}
19786c72ffaaSAndy Whitcroft	}
19796c72ffaaSAndy Whitcroft
19801f65f947SAndy Whitcroft	return ($res, $var);
19816c72ffaaSAndy Whitcroft}
19826c72ffaaSAndy Whitcroft
19838905a67cSAndy Whitcroftsub possible {
198413214adfSAndy Whitcroft	my ($possible, $line) = @_;
19859a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
19860776e594SAndy Whitcroft		^(?:
19870776e594SAndy Whitcroft			$Modifier|
19880776e594SAndy Whitcroft			$Storage|
19890776e594SAndy Whitcroft			$Type|
19909a974fdbSAndy Whitcroft			DEFINE_\S+
19919a974fdbSAndy Whitcroft		)$|
19929a974fdbSAndy Whitcroft		^(?:
19930776e594SAndy Whitcroft			goto|
19940776e594SAndy Whitcroft			return|
19950776e594SAndy Whitcroft			case|
19960776e594SAndy Whitcroft			else|
19970776e594SAndy Whitcroft			asm|__asm__|
199889a88353SAndy Whitcroft			do|
199989a88353SAndy Whitcroft			\#|
200089a88353SAndy Whitcroft			\#\#|
20019a974fdbSAndy Whitcroft		)(?:\s|$)|
20020776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
20039a974fdbSAndy Whitcroft	    )}x;
20049a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
20059a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2006c45dcabdSAndy Whitcroft		# Check for modifiers.
2007c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2008c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2009c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2010c45dcabdSAndy Whitcroft
2011c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2012c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2013d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
20149a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2015d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2016485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2017d2506586SAndy Whitcroft				}
20189a974fdbSAndy Whitcroft			}
2019c45dcabdSAndy Whitcroft
2020c45dcabdSAndy Whitcroft		} else {
202113214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2022485ff23eSAlex Dowad			push(@typeListFile, $possible);
2023c45dcabdSAndy Whitcroft		}
20248905a67cSAndy Whitcroft		build_types();
20250776e594SAndy Whitcroft	} else {
20260776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
20278905a67cSAndy Whitcroft	}
20288905a67cSAndy Whitcroft}
20298905a67cSAndy Whitcroft
20306c72ffaaSAndy Whitcroftmy $prefix = '';
20316c72ffaaSAndy Whitcroft
2032000d1cc1SJoe Perchessub show_type {
2033cbec18afSJoe Perches	my ($type) = @_;
203491bfe484SJoe Perches
2035522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2036522b837cSAlexey Dobriyan
2037cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2038cbec18afSJoe Perches
2039cbec18afSJoe Perches	return !defined $ignore_type{$type};
2040000d1cc1SJoe Perches}
2041000d1cc1SJoe Perches
2042f0a594c1SAndy Whitcroftsub report {
2043cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2044cbec18afSJoe Perches
2045cbec18afSJoe Perches	if (!show_type($type) ||
2046cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2047773647a0SAndy Whitcroft		return 0;
2048773647a0SAndy Whitcroft	}
204957230297SJoe Perches	my $output = '';
2050737c0767SJohn Brooks	if ($color) {
205157230297SJoe Perches		if ($level eq 'ERROR') {
205257230297SJoe Perches			$output .= RED;
205357230297SJoe Perches		} elsif ($level eq 'WARNING') {
205457230297SJoe Perches			$output .= YELLOW;
2055000d1cc1SJoe Perches		} else {
205657230297SJoe Perches			$output .= GREEN;
2057000d1cc1SJoe Perches		}
205857230297SJoe Perches	}
205957230297SJoe Perches	$output .= $prefix . $level . ':';
206057230297SJoe Perches	if ($show_types) {
2061737c0767SJohn Brooks		$output .= BLUE if ($color);
206257230297SJoe Perches		$output .= "$type:";
206357230297SJoe Perches	}
2064737c0767SJohn Brooks	$output .= RESET if ($color);
206557230297SJoe Perches	$output .= ' ' . $msg . "\n";
206634d8815fSJoe Perches
206734d8815fSJoe Perches	if ($showfile) {
206834d8815fSJoe Perches		my @lines = split("\n", $output, -1);
206934d8815fSJoe Perches		splice(@lines, 1, 1);
207034d8815fSJoe Perches		$output = join("\n", @lines);
207134d8815fSJoe Perches	}
207257230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
20738905a67cSAndy Whitcroft
207457230297SJoe Perches	push(our @report, $output);
2075773647a0SAndy Whitcroft
2076773647a0SAndy Whitcroft	return 1;
2077f0a594c1SAndy Whitcroft}
2078cbec18afSJoe Perches
2079f0a594c1SAndy Whitcroftsub report_dump {
208013214adfSAndy Whitcroft	our @report;
2081f0a594c1SAndy Whitcroft}
2082000d1cc1SJoe Perches
2083d752fcc8SJoe Perchessub fixup_current_range {
2084d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2085d752fcc8SJoe Perches
2086d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2087d752fcc8SJoe Perches		my $o = $1;
2088d752fcc8SJoe Perches		my $l = $2;
2089d752fcc8SJoe Perches		my $no = $o + $offset;
2090d752fcc8SJoe Perches		my $nl = $l + $length;
2091d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2092d752fcc8SJoe Perches	}
2093d752fcc8SJoe Perches}
2094d752fcc8SJoe Perches
2095d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2096d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2097d752fcc8SJoe Perches
2098d752fcc8SJoe Perches	my $range_last_linenr = 0;
2099d752fcc8SJoe Perches	my $delta_offset = 0;
2100d752fcc8SJoe Perches
2101d752fcc8SJoe Perches	my $old_linenr = 0;
2102d752fcc8SJoe Perches	my $new_linenr = 0;
2103d752fcc8SJoe Perches
2104d752fcc8SJoe Perches	my $next_insert = 0;
2105d752fcc8SJoe Perches	my $next_delete = 0;
2106d752fcc8SJoe Perches
2107d752fcc8SJoe Perches	my @lines = ();
2108d752fcc8SJoe Perches
2109d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2110d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2111d752fcc8SJoe Perches
2112d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2113d752fcc8SJoe Perches		my $save_line = 1;
2114d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2115323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2116d752fcc8SJoe Perches			$delta_offset = 0;
2117d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2118d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2119d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2120d752fcc8SJoe Perches		}
2121d752fcc8SJoe Perches
2122d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2123d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2124d752fcc8SJoe Perches			$save_line = 0;
2125d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2126d752fcc8SJoe Perches		}
2127d752fcc8SJoe Perches
2128d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2129d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2130d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2131d752fcc8SJoe Perches			$new_linenr++;
2132d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2133d752fcc8SJoe Perches		}
2134d752fcc8SJoe Perches
2135d752fcc8SJoe Perches		if ($save_line) {
2136d752fcc8SJoe Perches			push(@lines, $line);
2137d752fcc8SJoe Perches			$new_linenr++;
2138d752fcc8SJoe Perches		}
2139d752fcc8SJoe Perches
2140d752fcc8SJoe Perches		$old_linenr++;
2141d752fcc8SJoe Perches	}
2142d752fcc8SJoe Perches
2143d752fcc8SJoe Perches	return @lines;
2144d752fcc8SJoe Perches}
2145d752fcc8SJoe Perches
2146f2d7e4d4SJoe Perchessub fix_insert_line {
2147f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2148f2d7e4d4SJoe Perches
2149f2d7e4d4SJoe Perches	my $inserted = {
2150f2d7e4d4SJoe Perches		LINENR => $linenr,
2151f2d7e4d4SJoe Perches		LINE => $line,
2152f2d7e4d4SJoe Perches	};
2153f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2154f2d7e4d4SJoe Perches}
2155f2d7e4d4SJoe Perches
2156f2d7e4d4SJoe Perchessub fix_delete_line {
2157f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2158f2d7e4d4SJoe Perches
2159f2d7e4d4SJoe Perches	my $deleted = {
2160f2d7e4d4SJoe Perches		LINENR => $linenr,
2161f2d7e4d4SJoe Perches		LINE => $line,
2162f2d7e4d4SJoe Perches	};
2163f2d7e4d4SJoe Perches
2164f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2165f2d7e4d4SJoe Perches}
2166f2d7e4d4SJoe Perches
2167de7d4f0eSAndy Whitcroftsub ERROR {
2168cbec18afSJoe Perches	my ($type, $msg) = @_;
2169cbec18afSJoe Perches
2170cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2171de7d4f0eSAndy Whitcroft		our $clean = 0;
21726c72ffaaSAndy Whitcroft		our $cnt_error++;
21733705ce5bSJoe Perches		return 1;
2174de7d4f0eSAndy Whitcroft	}
21753705ce5bSJoe Perches	return 0;
2176773647a0SAndy Whitcroft}
2177de7d4f0eSAndy Whitcroftsub WARN {
2178cbec18afSJoe Perches	my ($type, $msg) = @_;
2179cbec18afSJoe Perches
2180cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2181de7d4f0eSAndy Whitcroft		our $clean = 0;
21826c72ffaaSAndy Whitcroft		our $cnt_warn++;
21833705ce5bSJoe Perches		return 1;
2184de7d4f0eSAndy Whitcroft	}
21853705ce5bSJoe Perches	return 0;
2186773647a0SAndy Whitcroft}
2187de7d4f0eSAndy Whitcroftsub CHK {
2188cbec18afSJoe Perches	my ($type, $msg) = @_;
2189cbec18afSJoe Perches
2190cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2191de7d4f0eSAndy Whitcroft		our $clean = 0;
21926c72ffaaSAndy Whitcroft		our $cnt_chk++;
21933705ce5bSJoe Perches		return 1;
21946c72ffaaSAndy Whitcroft	}
21953705ce5bSJoe Perches	return 0;
2196de7d4f0eSAndy Whitcroft}
2197de7d4f0eSAndy Whitcroft
21986ecd9674SAndy Whitcroftsub check_absolute_file {
21996ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
22006ecd9674SAndy Whitcroft	my $file = $absolute;
22016ecd9674SAndy Whitcroft
22026ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
22036ecd9674SAndy Whitcroft
22046ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
22056ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
22066ecd9674SAndy Whitcroft		if (-f "$root/$file") {
22076ecd9674SAndy Whitcroft			##print "file<$file>\n";
22086ecd9674SAndy Whitcroft			last;
22096ecd9674SAndy Whitcroft		}
22106ecd9674SAndy Whitcroft	}
22116ecd9674SAndy Whitcroft	if (! -f _)  {
22126ecd9674SAndy Whitcroft		return 0;
22136ecd9674SAndy Whitcroft	}
22146ecd9674SAndy Whitcroft
22156ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
22166ecd9674SAndy Whitcroft	my $prefix = $absolute;
22176ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
22186ecd9674SAndy Whitcroft
22196ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
22206ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2221000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2222000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
22236ecd9674SAndy Whitcroft	}
22246ecd9674SAndy Whitcroft}
22256ecd9674SAndy Whitcroft
22263705ce5bSJoe Perchessub trim {
22273705ce5bSJoe Perches	my ($string) = @_;
22283705ce5bSJoe Perches
2229b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2230b34c648bSJoe Perches
2231b34c648bSJoe Perches	return $string;
2232b34c648bSJoe Perches}
2233b34c648bSJoe Perches
2234b34c648bSJoe Perchessub ltrim {
2235b34c648bSJoe Perches	my ($string) = @_;
2236b34c648bSJoe Perches
2237b34c648bSJoe Perches	$string =~ s/^\s+//;
2238b34c648bSJoe Perches
2239b34c648bSJoe Perches	return $string;
2240b34c648bSJoe Perches}
2241b34c648bSJoe Perches
2242b34c648bSJoe Perchessub rtrim {
2243b34c648bSJoe Perches	my ($string) = @_;
2244b34c648bSJoe Perches
2245b34c648bSJoe Perches	$string =~ s/\s+$//;
22463705ce5bSJoe Perches
22473705ce5bSJoe Perches	return $string;
22483705ce5bSJoe Perches}
22493705ce5bSJoe Perches
225052ea8506SJoe Perchessub string_find_replace {
225152ea8506SJoe Perches	my ($string, $find, $replace) = @_;
225252ea8506SJoe Perches
225352ea8506SJoe Perches	$string =~ s/$find/$replace/g;
225452ea8506SJoe Perches
225552ea8506SJoe Perches	return $string;
225652ea8506SJoe Perches}
225752ea8506SJoe Perches
22583705ce5bSJoe Perchessub tabify {
22593705ce5bSJoe Perches	my ($leading) = @_;
22603705ce5bSJoe Perches
2261713a09deSAntonio Borneo	my $source_indent = $tabsize;
22623705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
22633705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
22643705ce5bSJoe Perches
22653705ce5bSJoe Perches	#convert leading spaces to tabs
22663705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
22673705ce5bSJoe Perches	#Remove spaces before a tab
22683705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
22693705ce5bSJoe Perches
22703705ce5bSJoe Perches	return "$leading";
22713705ce5bSJoe Perches}
22723705ce5bSJoe Perches
2273d1fe9c09SJoe Perchessub pos_last_openparen {
2274d1fe9c09SJoe Perches	my ($line) = @_;
2275d1fe9c09SJoe Perches
2276d1fe9c09SJoe Perches	my $pos = 0;
2277d1fe9c09SJoe Perches
2278d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2279d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2280d1fe9c09SJoe Perches
2281d1fe9c09SJoe Perches	my $last_openparen = 0;
2282d1fe9c09SJoe Perches
2283d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2284d1fe9c09SJoe Perches		return -1;
2285d1fe9c09SJoe Perches	}
2286d1fe9c09SJoe Perches
2287d1fe9c09SJoe Perches	my $len = length($line);
2288d1fe9c09SJoe Perches
2289d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2290d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2291d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2292d1fe9c09SJoe Perches			$pos += length($1) - 1;
2293d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2294d1fe9c09SJoe Perches			$last_openparen = $pos;
2295d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2296d1fe9c09SJoe Perches			last;
2297d1fe9c09SJoe Perches		}
2298d1fe9c09SJoe Perches	}
2299d1fe9c09SJoe Perches
230091cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2301d1fe9c09SJoe Perches}
2302d1fe9c09SJoe Perches
2303f36d3eb8SJoe Perchessub get_raw_comment {
2304f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2305f36d3eb8SJoe Perches	my $comment = '';
2306f36d3eb8SJoe Perches
2307f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2308f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2309f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2310f36d3eb8SJoe Perches		}
2311f36d3eb8SJoe Perches	}
2312f36d3eb8SJoe Perches
2313f36d3eb8SJoe Perches	return $comment;
2314f36d3eb8SJoe Perches}
2315f36d3eb8SJoe Perches
23160a920b5bSAndy Whitcroftsub process {
23170a920b5bSAndy Whitcroft	my $filename = shift;
23180a920b5bSAndy Whitcroft
23190a920b5bSAndy Whitcroft	my $linenr=0;
23200a920b5bSAndy Whitcroft	my $prevline="";
2321c2fdda0dSAndy Whitcroft	my $prevrawline="";
23220a920b5bSAndy Whitcroft	my $stashline="";
2323c2fdda0dSAndy Whitcroft	my $stashrawline="";
23240a920b5bSAndy Whitcroft
23254a0df2efSAndy Whitcroft	my $length;
23260a920b5bSAndy Whitcroft	my $indent;
23270a920b5bSAndy Whitcroft	my $previndent=0;
23280a920b5bSAndy Whitcroft	my $stashindent=0;
23290a920b5bSAndy Whitcroft
2330de7d4f0eSAndy Whitcroft	our $clean = 1;
23310a920b5bSAndy Whitcroft	my $signoff = 0;
2332cd261496SGeert Uytterhoeven	my $author = '';
2333cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
23340a920b5bSAndy Whitcroft	my $is_patch = 0;
2335133712a2SRob Herring	my $is_binding_patch = -1;
233629ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
233715662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
233844d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2339ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2340490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2341bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
23422a076f40SJoe Perches	my $commit_log_long_line = 0;
2343e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
234413f1937eSJoe Perches	my $reported_maintainer_file = 0;
2345fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2346fa64205dSPasi Savanainen
2347365dd4eaSJoe Perches	my $last_blank_line = 0;
23485e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2349365dd4eaSJoe Perches
235013214adfSAndy Whitcroft	our @report = ();
23516c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
23526c72ffaaSAndy Whitcroft	our $cnt_error = 0;
23536c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
23546c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
23556c72ffaaSAndy Whitcroft
23560a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
23570a920b5bSAndy Whitcroft	my $realfile = '';
23580a920b5bSAndy Whitcroft	my $realline = 0;
23590a920b5bSAndy Whitcroft	my $realcnt = 0;
23600a920b5bSAndy Whitcroft	my $here = '';
236177cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23620a920b5bSAndy Whitcroft	my $in_comment = 0;
2363c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23640a920b5bSAndy Whitcroft	my $first_line = 0;
23651e855726SWolfram Sang	my $p1_prefix = '';
23660a920b5bSAndy Whitcroft
236713214adfSAndy Whitcroft	my $prev_values = 'E';
236813214adfSAndy Whitcroft
236913214adfSAndy Whitcroft	# suppression flags
2370773647a0SAndy Whitcroft	my %suppress_ifbraces;
2371170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
23722b474a1aSAndy Whitcroft	my %suppress_export;
23733e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2374653d4876SAndy Whitcroft
23757e51f197SJoe Perches	my %signatures = ();
2376323c1260SJoe Perches
2377c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2378de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2379c2fdda0dSAndy Whitcroft	#
2380de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2381de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2382773647a0SAndy Whitcroft
2383d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2384d8b07710SJoe Perches
23859f3a8992SRob Herring	my $checklicenseline = 1;
23869f3a8992SRob Herring
2387773647a0SAndy Whitcroft	sanitise_line_reset();
2388c2fdda0dSAndy Whitcroft	my $line;
2389c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2390773647a0SAndy Whitcroft		$linenr++;
2391773647a0SAndy Whitcroft		$line = $rawline;
2392c2fdda0dSAndy Whitcroft
23933705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
23943705ce5bSJoe Perches
2395773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2396de7d4f0eSAndy Whitcroft			$setup_docs = 0;
23978c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2398de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2399de7d4f0eSAndy Whitcroft			}
2400773647a0SAndy Whitcroft			#next;
2401de7d4f0eSAndy Whitcroft		}
240274fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2403773647a0SAndy Whitcroft			$realline=$1-1;
2404773647a0SAndy Whitcroft			if (defined $2) {
2405773647a0SAndy Whitcroft				$realcnt=$3+1;
2406773647a0SAndy Whitcroft			} else {
2407773647a0SAndy Whitcroft				$realcnt=1+1;
2408773647a0SAndy Whitcroft			}
2409c45dcabdSAndy Whitcroft			$in_comment = 0;
2410773647a0SAndy Whitcroft
2411773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2412773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2413773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2414773647a0SAndy Whitcroft			# at context start.
2415773647a0SAndy Whitcroft			my $edge;
241601fa9147SAndy Whitcroft			my $cnt = $realcnt;
241701fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
241801fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
241901fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
242001fa9147SAndy Whitcroft				$cnt--;
242101fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2422721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2423fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2424fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2425fae17daeSAndy Whitcroft					($edge) = $1;
2426fae17daeSAndy Whitcroft					last;
2427fae17daeSAndy Whitcroft				}
2428773647a0SAndy Whitcroft			}
2429773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2430773647a0SAndy Whitcroft				$in_comment = 1;
2431773647a0SAndy Whitcroft			}
2432773647a0SAndy Whitcroft
2433773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2434773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2435773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2436773647a0SAndy Whitcroft			if (!defined $edge &&
243783242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2438773647a0SAndy Whitcroft			{
2439773647a0SAndy Whitcroft				$in_comment = 1;
2440773647a0SAndy Whitcroft			}
2441773647a0SAndy Whitcroft
2442773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2443773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2444773647a0SAndy Whitcroft
2445171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2446773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2447171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2448773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2449773647a0SAndy Whitcroft		}
2450773647a0SAndy Whitcroft		push(@lines, $line);
2451773647a0SAndy Whitcroft
2452773647a0SAndy Whitcroft		if ($realcnt > 1) {
2453773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2454773647a0SAndy Whitcroft		} else {
2455773647a0SAndy Whitcroft			$realcnt = 0;
2456773647a0SAndy Whitcroft		}
2457773647a0SAndy Whitcroft
2458773647a0SAndy Whitcroft		#print "==>$rawline\n";
2459773647a0SAndy Whitcroft		#print "-->$line\n";
2460de7d4f0eSAndy Whitcroft
2461de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2462de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2463de7d4f0eSAndy Whitcroft		}
2464de7d4f0eSAndy Whitcroft	}
2465de7d4f0eSAndy Whitcroft
24666c72ffaaSAndy Whitcroft	$prefix = '';
24676c72ffaaSAndy Whitcroft
2468773647a0SAndy Whitcroft	$realcnt = 0;
2469773647a0SAndy Whitcroft	$linenr = 0;
2470194f66fcSJoe Perches	$fixlinenr = -1;
24710a920b5bSAndy Whitcroft	foreach my $line (@lines) {
24720a920b5bSAndy Whitcroft		$linenr++;
2473194f66fcSJoe Perches		$fixlinenr++;
24741b5539b1SJoe Perches		my $sline = $line;	#copy of $line
24751b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
24760a920b5bSAndy Whitcroft
2477c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2478f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
24796c72ffaaSAndy Whitcroft
248012c253abSJoe Perches# check if it's a mode change, rename or start of a patch
248112c253abSJoe Perches		if (!$in_commit_log &&
248212c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
248312c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
248412c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
248512c253abSJoe Perches			$is_patch = 1;
248612c253abSJoe Perches		}
248712c253abSJoe Perches
24880a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2489e518e9a5SJoe Perches		if (!$in_commit_log &&
249074fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
249174fd4f34SJoe Perches			my $context = $4;
24920a920b5bSAndy Whitcroft			$is_patch = 1;
24934a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
24940a920b5bSAndy Whitcroft			$realline=$1-1;
24950a920b5bSAndy Whitcroft			if (defined $2) {
24960a920b5bSAndy Whitcroft				$realcnt=$3+1;
24970a920b5bSAndy Whitcroft			} else {
24980a920b5bSAndy Whitcroft				$realcnt=1+1;
24990a920b5bSAndy Whitcroft			}
2500c2fdda0dSAndy Whitcroft			annotate_reset();
250113214adfSAndy Whitcroft			$prev_values = 'E';
250213214adfSAndy Whitcroft
2503773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2504170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
25052b474a1aSAndy Whitcroft			%suppress_export = ();
25063e469cdcSAndy Whitcroft			$suppress_statement = 0;
250774fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
250874fd4f34SJoe Perches				$context_function = $1;
250974fd4f34SJoe Perches			} else {
251074fd4f34SJoe Perches				undef $context_function;
251174fd4f34SJoe Perches			}
25120a920b5bSAndy Whitcroft			next;
25130a920b5bSAndy Whitcroft
25144a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
25154a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
25164a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2517773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
25180a920b5bSAndy Whitcroft			$realline++;
2519d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
25200a920b5bSAndy Whitcroft
25214a0df2efSAndy Whitcroft			# Measure the line length and indent.
2522c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
25230a920b5bSAndy Whitcroft
25240a920b5bSAndy Whitcroft			# Track the previous line.
25250a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
25260a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2527c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2528c2fdda0dSAndy Whitcroft
2529773647a0SAndy Whitcroft			#warn "line<$line>\n";
25306c72ffaaSAndy Whitcroft
2531d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2532d8aaf121SAndy Whitcroft			$realcnt--;
25330a920b5bSAndy Whitcroft		}
25340a920b5bSAndy Whitcroft
2535cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2536cc77cdcaSAndy Whitcroft
25376c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
25386c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2539773647a0SAndy Whitcroft
25402ac73b4fSJoe Perches		my $found_file = 0;
2541773647a0SAndy Whitcroft		# extract the filename as it passes
25423bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
25433bf9a009SRabin Vincent			$realfile = $1;
25442b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2545270c49a0SJoe Perches			$in_commit_log = 0;
25462ac73b4fSJoe Perches			$found_file = 1;
25473bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2548773647a0SAndy Whitcroft			$realfile = $1;
25492b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2550270c49a0SJoe Perches			$in_commit_log = 0;
25511e855726SWolfram Sang
25521e855726SWolfram Sang			$p1_prefix = $1;
2553e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2554e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2555000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2556000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
25571e855726SWolfram Sang			}
2558773647a0SAndy Whitcroft
2559c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2560000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2561000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2562773647a0SAndy Whitcroft			}
25632ac73b4fSJoe Perches			$found_file = 1;
25642ac73b4fSJoe Perches		}
25652ac73b4fSJoe Perches
256634d8815fSJoe Perches#make up the handle for any error we report on this line
256734d8815fSJoe Perches		if ($showfile) {
256834d8815fSJoe Perches			$prefix = "$realfile:$realline: "
256934d8815fSJoe Perches		} elsif ($emacs) {
25707d3a9f67SJoe Perches			if ($file) {
25717d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
25727d3a9f67SJoe Perches			} else {
257334d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
257434d8815fSJoe Perches			}
25757d3a9f67SJoe Perches		}
257634d8815fSJoe Perches
25772ac73b4fSJoe Perches		if ($found_file) {
257885b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
257985b0ee18SJoe Perches				WARN("OBSOLETE",
258085b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
258185b0ee18SJoe Perches			}
25827bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25832ac73b4fSJoe Perches				$check = 1;
25842ac73b4fSJoe Perches			} else {
25852ac73b4fSJoe Perches				$check = $check_orig;
25862ac73b4fSJoe Perches			}
25879f3a8992SRob Herring			$checklicenseline = 1;
2588133712a2SRob Herring
2589133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2590133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2591133712a2SRob Herring
2592133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2593133712a2SRob Herring
2594133712a2SRob Herring				if (($last_binding_patch != -1) &&
2595133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2596133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2597133712a2SRob Herring					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2598133712a2SRob Herring				}
2599133712a2SRob Herring			}
2600133712a2SRob Herring
2601773647a0SAndy Whitcroft			next;
2602773647a0SAndy Whitcroft		}
2603773647a0SAndy Whitcroft
2604389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
26050a920b5bSAndy Whitcroft
2606c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2607c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2608c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
26090a920b5bSAndy Whitcroft
26106c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
26116c72ffaaSAndy Whitcroft
2612490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2613490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2614490b292cSJoe Perches		if ($in_commit_log) {
2615490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2616490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2617490b292cSJoe Perches			}
2618490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2619490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2620490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2621490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2622490b292cSJoe Perches		}
2623490b292cSJoe Perches
2624e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2625e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2626e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2627e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2628e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2629e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2630e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2631e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2632e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2633e518e9a5SJoe Perches		}
2634e518e9a5SJoe Perches
26353bf9a009SRabin Vincent# Check for incorrect file permissions
26363bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
26373bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
263804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
263904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2640000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2641000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
26423bf9a009SRabin Vincent			}
26433bf9a009SRabin Vincent		}
26443bf9a009SRabin Vincent
2645cd261496SGeert Uytterhoeven# Check the patch for a From:
2646cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2647cd261496SGeert Uytterhoeven			$author = $1;
2648cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2649cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2650dfa05c28SJoe Perches			$author = reformat_email($author);
2651cd261496SGeert Uytterhoeven		}
2652cd261496SGeert Uytterhoeven
265320112475SJoe Perches# Check the patch for a signoff:
2654dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
26554a0df2efSAndy Whitcroft			$signoff++;
265615662b3eSJoe Perches			$in_commit_log = 0;
2657cd261496SGeert Uytterhoeven			if ($author ne '') {
2658dfa05c28SJoe Perches				if (same_email_addresses($1, $author)) {
2659cd261496SGeert Uytterhoeven					$authorsignoff = 1;
2660cd261496SGeert Uytterhoeven				}
2661cd261496SGeert Uytterhoeven			}
26620a920b5bSAndy Whitcroft		}
266320112475SJoe Perches
266444d303ebSJoe Perches# Check for patch separator
266544d303ebSJoe Perches		if ($line =~ /^---$/) {
266644d303ebSJoe Perches			$has_patch_separator = 1;
266744d303ebSJoe Perches			$in_commit_log = 0;
266844d303ebSJoe Perches		}
266944d303ebSJoe Perches
2670e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2671e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2672e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2673e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2674e0d975b1SJoe Perches		}
2675e0d975b1SJoe Perches
267620112475SJoe Perches# Check signature styles
2677270c49a0SJoe Perches		if (!$in_header_lines &&
2678ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
267920112475SJoe Perches			my $space_before = $1;
268020112475SJoe Perches			my $sign_off = $2;
268120112475SJoe Perches			my $space_after = $3;
268220112475SJoe Perches			my $email = $4;
268320112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
268420112475SJoe Perches
2685ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2686ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2687ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2688ce0338dfSJoe Perches			}
268920112475SJoe Perches			if (defined $space_before && $space_before ne "") {
26903705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26913705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
26923705ce5bSJoe Perches				    $fix) {
2693194f66fcSJoe Perches					$fixed[$fixlinenr] =
26943705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26953705ce5bSJoe Perches				}
269620112475SJoe Perches			}
269720112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
26983705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26993705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
27003705ce5bSJoe Perches				    $fix) {
2701194f66fcSJoe Perches					$fixed[$fixlinenr] =
27023705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27033705ce5bSJoe Perches				}
27043705ce5bSJoe Perches
270520112475SJoe Perches			}
270620112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
27073705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27083705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
27093705ce5bSJoe Perches				    $fix) {
2710194f66fcSJoe Perches					$fixed[$fixlinenr] =
27113705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27123705ce5bSJoe Perches				}
271320112475SJoe Perches			}
271420112475SJoe Perches
2715dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
271620112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
271720112475SJoe Perches			if ($suggested_email eq "") {
2718000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2719000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
272020112475SJoe Perches			} else {
272120112475SJoe Perches				my $dequoted = $suggested_email;
272220112475SJoe Perches				$dequoted =~ s/^"//;
272320112475SJoe Perches				$dequoted =~ s/" </ </;
272420112475SJoe Perches				# Don't force email to have quotes
272520112475SJoe Perches				# Allow just an angle bracketed address
2726dfa05c28SJoe Perches				if (!same_email_addresses($email, $suggested_email)) {
2727000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2728000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
272920112475SJoe Perches				}
27300a920b5bSAndy Whitcroft			}
27317e51f197SJoe Perches
27327e51f197SJoe Perches# Check for duplicate signatures
27337e51f197SJoe Perches			my $sig_nospace = $line;
27347e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
27357e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
27367e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
27377e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
27387e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
27397e51f197SJoe Perches			} else {
27407e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
27417e51f197SJoe Perches			}
27426c5d24eeSSean Christopherson
27436c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
27446c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
27456c5d24eeSSean Christopherson				if ($email eq $author) {
27466c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27476c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
27486c5d24eeSSean Christopherson				}
27496c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
27506c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27516c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
27526c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
27536c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27546c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27556c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
27566c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27576c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27586c5d24eeSSean Christopherson				}
27596c5d24eeSSean Christopherson			}
27600a920b5bSAndy Whitcroft		}
27610a920b5bSAndy Whitcroft
2762a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2763a2fe16b9SJoe Perches		if ($in_header_lines &&
2764a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2765a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2766a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2767a2fe16b9SJoe Perches		}
2768a2fe16b9SJoe Perches
276944d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
277044d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
27717ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
277244d303ebSJoe Perches			      "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
27737ebd05efSChristopher Covington		}
27747ebd05efSChristopher Covington
2775369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2776369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2777369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2778369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2779369c8dd3SJoe Perches					# timestamp
2780634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2781634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2782634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2783634cffccSJoe Perches					# stack dump address styles
2784369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2785369c8dd3SJoe Perches		}
2786369c8dd3SJoe Perches
27872a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
27882a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2789bf4daf12SJoe Perches		    length($line) > 75 &&
2790bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2791bf4daf12SJoe Perches					# file delta changes
2792bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2793bf4daf12SJoe Perches					# filename then :
2794bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2795bf4daf12SJoe Perches					# A Fixes: or Link: line
2796bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
27972a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
27982a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
27992a076f40SJoe Perches			$commit_log_long_line = 1;
28002a076f40SJoe Perches		}
28012a076f40SJoe Perches
2802bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2803bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2804bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2805bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2806bf4daf12SJoe Perches		}
2807bf4daf12SJoe Perches
28080d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2809369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2810a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
2811e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2812fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2813aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2814369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2815bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2816fe043ea1SJoe Perches			my $init_char = "c";
2817fe043ea1SJoe Perches			my $orig_commit = "";
28180d7835fcSJoe Perches			my $short = 1;
28190d7835fcSJoe Perches			my $long = 0;
28200d7835fcSJoe Perches			my $case = 1;
28210d7835fcSJoe Perches			my $space = 1;
28220d7835fcSJoe Perches			my $hasdesc = 0;
282319c146a6SJoe Perches			my $hasparens = 0;
28240d7835fcSJoe Perches			my $id = '0123456789ab';
28250d7835fcSJoe Perches			my $orig_desc = "commit description";
28260d7835fcSJoe Perches			my $description = "";
28270d7835fcSJoe Perches
2828fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2829fe043ea1SJoe Perches				$init_char = $1;
2830fe043ea1SJoe Perches				$orig_commit = lc($2);
2831fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2832fe043ea1SJoe Perches				$orig_commit = lc($1);
2833fe043ea1SJoe Perches			}
2834fe043ea1SJoe Perches
28350d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
28360d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
28370d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
28380d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
28390d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
28400d7835fcSJoe Perches				$orig_desc = $1;
284119c146a6SJoe Perches				$hasparens = 1;
28420d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
28430d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
28440d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
28450d7835fcSJoe Perches				$orig_desc = $1;
284619c146a6SJoe Perches				$hasparens = 1;
2847b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2848b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2849b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2850b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2851b671fde0SJoe Perches				$orig_desc = $1;
2852b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2853b671fde0SJoe Perches				$orig_desc .= " " . $1;
285419c146a6SJoe Perches				$hasparens = 1;
28550d7835fcSJoe Perches			}
28560d7835fcSJoe Perches
28570d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
28580d7835fcSJoe Perches							      $id, $orig_desc);
28590d7835fcSJoe Perches
2860948b133aSHeinrich Schuchardt			if (defined($id) &&
2861948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2862d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
28630d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
28640d7835fcSJoe Perches			}
2865d311cd44SJoe Perches		}
2866d311cd44SJoe Perches
286713f1937eSJoe Perches# Check for added, moved or deleted files
286813f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
286913f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
287013f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
287113f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
287213f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2873a82603a8SAndrew Jeffery			$is_patch = 1;
287413f1937eSJoe Perches			$reported_maintainer_file = 1;
287513f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
287613f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
287713f1937eSJoe Perches		}
287813f1937eSJoe Perches
2879e400edb1SRob Herring# Check for adding new DT bindings not in schema format
2880e400edb1SRob Herring		if (!$in_commit_log &&
2881e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
2882e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2883e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
2884e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2885e400edb1SRob Herring		}
2886e400edb1SRob Herring
288700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
28888905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2889000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2890000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
28916c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2892de7d4f0eSAndy Whitcroft		}
2893de7d4f0eSAndy Whitcroft
2894de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2895de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2896171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2897171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2898171ae1a4SAndy Whitcroft
2899171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2900171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2901171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2902171ae1a4SAndy Whitcroft
290334d99219SJoe Perches			CHK("INVALID_UTF8",
2904000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
290500df344fSAndy Whitcroft		}
29060a920b5bSAndy Whitcroft
290715662b3eSJoe Perches# Check if it's the start of a commit log
290815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
290915662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2910eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2911eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
291215662b3eSJoe Perches			$in_header_lines = 0;
291315662b3eSJoe Perches			$in_commit_log = 1;
2914ed43c4e5SAllen Hubbe			$has_commit_log = 1;
291515662b3eSJoe Perches		}
291615662b3eSJoe Perches
2917fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2918fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2919fa64205dSPasi Savanainen		if ($in_header_lines &&
2920fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2921fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2922fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2923fa64205dSPasi Savanainen		}
2924fa64205dSPasi Savanainen
2925fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
292615662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2927fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
292815662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
292915662b3eSJoe Perches		}
293015662b3eSJoe Perches
2931d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2932d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2933d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2934d6430f71SJoe Perches				my $file = $1;
2935d6430f71SJoe Perches
2936d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2937d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2938d6430f71SJoe Perches					#
2939d6430f71SJoe Perches				} else {
2940d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2941d6430f71SJoe Perches				}
2942d6430f71SJoe Perches			}
2943d6430f71SJoe Perches		}
2944d6430f71SJoe Perches
294566b47b4aSKees Cook# Check for various typo / spelling mistakes
294666d7a382SJoe Perches		if (defined($misspellings) &&
294766d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2948ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
294966b47b4aSKees Cook				my $typo = $1;
295066b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
295166b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
295266b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
29530675a8fbSJean Delvare				my $msg_level = \&WARN;
29540675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
29550675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
295666b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
295766b47b4aSKees Cook				    $fix) {
295866b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
295966b47b4aSKees Cook				}
296066b47b4aSKees Cook			}
296166b47b4aSKees Cook		}
296266b47b4aSKees Cook
2963a8dd86bfSMatteo Croce# check for invalid commit id
2964a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2965a8dd86bfSMatteo Croce			my $id;
2966a8dd86bfSMatteo Croce			my $description;
2967a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
2968a8dd86bfSMatteo Croce			if (!defined($id)) {
2969a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
2970a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2971a8dd86bfSMatteo Croce			}
2972a8dd86bfSMatteo Croce		}
2973a8dd86bfSMatteo Croce
297430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
297530670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
297600df344fSAndy Whitcroft
29770a920b5bSAndy Whitcroft#trailing whitespace
29789c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2979c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2980d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2981d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2982d5e616fcSJoe Perches			    $fix) {
2983194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2984d5e616fcSJoe Perches			}
2985c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2986c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29873705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
29883705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
29893705ce5bSJoe Perches			    $fix) {
2990194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
29913705ce5bSJoe Perches			}
29923705ce5bSJoe Perches
2993d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29940a920b5bSAndy Whitcroft		}
29955368df20SAndy Whitcroft
29964783f894SJosh Triplett# Check for FSF mailing addresses.
2997109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
29981bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
29993e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
30003e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
30014783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30020675a8fbSJean Delvare			my $msg_level = \&ERROR;
30030675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
30040675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
30054783f894SJosh 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)
30064783f894SJosh Triplett		}
30074783f894SJosh Triplett
30083354957aSAndi Kleen# check for Kconfig help text having a real description
30099fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
30109fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
30113354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3012678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3013678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3014678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3015678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
30163354957aSAndi Kleen			my $length = 0;
30179fe287d7SAndy Whitcroft			my $cnt = $realcnt;
30189fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
30199fe287d7SAndy Whitcroft			my $f;
3020a1385803SAndy Whitcroft			my $is_start = 0;
30219fe287d7SAndy Whitcroft			my $is_end = 0;
3022a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
30239fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
30249fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
30259fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
30269fe287d7SAndy Whitcroft
30279fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
30288d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3029a1385803SAndy Whitcroft
303086adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3031a1385803SAndy Whitcroft					$is_start = 1;
303284af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
303384af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
303484af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
303584af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
303684af7a61SUlf Magnusson					}
3037a1385803SAndy Whitcroft					$length = -1;
3038a1385803SAndy Whitcroft				}
3039a1385803SAndy Whitcroft
30409fe287d7SAndy Whitcroft				$f =~ s/^.//;
30413354957aSAndi Kleen				$f =~ s/#.*//;
30423354957aSAndi Kleen				$f =~ s/^\s+//;
30433354957aSAndi Kleen				next if ($f =~ /^$/);
3044678ae162SUlf Magnusson
3045678ae162SUlf Magnusson				# This only checks context lines in the patch
3046678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3047678ae162SUlf Magnusson				# positives, even though some of these are
3048678ae162SUlf Magnusson				# common words in help texts
3049678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3050678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
30519fe287d7SAndy Whitcroft					$is_end = 1;
30529fe287d7SAndy Whitcroft					last;
30539fe287d7SAndy Whitcroft				}
30543354957aSAndi Kleen				$length++;
30553354957aSAndi Kleen			}
305656193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3057000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
305856193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
305956193274SVadim Bendebury			}
3060a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
30613354957aSAndi Kleen		}
30623354957aSAndi Kleen
3063628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
3064628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
3065628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
3066628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
3067628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
3068628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3069628f91a2SJoe Perches			    $fix) {
3070628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3071628f91a2SJoe Perches			}
3072628f91a2SJoe Perches		}
3073628f91a2SJoe Perches
3074327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3075327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3076327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3077327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3078327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3079327953e9SChristoph Jaeger		}
3080327953e9SChristoph Jaeger
3081c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3082c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3083c68e5878SArnaud Lacombe			my $flag = $1;
3084c68e5878SArnaud Lacombe			my $replacement = {
3085c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3086c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3087c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3088c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3089c68e5878SArnaud Lacombe			};
3090c68e5878SArnaud Lacombe
3091c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3092c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3093c68e5878SArnaud Lacombe		}
3094c68e5878SArnaud Lacombe
3095bff5da43SRob Herring# check for DT compatible documentation
30967dd05b38SFlorian Vaussard		if (defined $root &&
30977dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
30987dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
30997dd05b38SFlorian Vaussard
3100bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3101bff5da43SRob Herring
3102cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3103852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3104cc93319bSFlorian Vaussard
3105bff5da43SRob Herring			foreach my $compat (@compats) {
3106bff5da43SRob Herring				my $compat2 = $compat;
3107185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3108185d566bSRob Herring				my $compat3 = $compat;
3109185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3110185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3111bff5da43SRob Herring				if ( $? >> 8 ) {
3112bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3113bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3114bff5da43SRob Herring				}
3115bff5da43SRob Herring
31164fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
31174fbf32a6SFlorian Vaussard				my $vendor = $1;
3118852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3119bff5da43SRob Herring				if ( $? >> 8 ) {
3120bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3121cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3122bff5da43SRob Herring				}
3123bff5da43SRob Herring			}
3124bff5da43SRob Herring		}
3125bff5da43SRob Herring
31269f3a8992SRob Herring# check for using SPDX license tag at beginning of files
31279f3a8992SRob Herring		if ($realline == $checklicenseline) {
31289f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
31299f3a8992SRob Herring				$checklicenseline = 2;
31309f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
31319f3a8992SRob Herring				my $comment = "";
31329f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
31339f3a8992SRob Herring					$comment = '/*';
31349f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
31359f3a8992SRob Herring					$comment = '//';
3136c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
31379f3a8992SRob Herring					$comment = '#';
31389f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
31399f3a8992SRob Herring					$comment = '..';
31409f3a8992SRob Herring				}
31419f3a8992SRob Herring
3142fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3143fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3144fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3145ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3146fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3147fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3148fdf13693SJoe Perches				}
3149fdf13693SJoe Perches
31509f3a8992SRob Herring				if ($comment !~ /^$/ &&
3151ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
31529f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
31539f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
31543b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
31553b6e8ac9SJoe Perches					my $spdx_license = $1;
31563b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
31573b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
31583b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
31593b6e8ac9SJoe Perches					}
316050c92900SLubomir Rintel					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
316150c92900SLubomir Rintel					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
316250c92900SLubomir Rintel						my $msg_level = \&WARN;
316350c92900SLubomir Rintel						$msg_level = \&CHK if ($file);
316450c92900SLubomir Rintel						if (&{$msg_level}("SPDX_LICENSE_TAG",
316550c92900SLubomir Rintel
316650c92900SLubomir Rintel								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
316750c92900SLubomir Rintel						    $fix) {
316850c92900SLubomir Rintel							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
316950c92900SLubomir Rintel						}
317050c92900SLubomir Rintel					}
31719f3a8992SRob Herring				}
31729f3a8992SRob Herring			}
31739f3a8992SRob Herring		}
31749f3a8992SRob Herring
31755368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3176d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
31775368df20SAndy Whitcroft
3178a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3179a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3180a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3181a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3182a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3183a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3184a8da38a9SJoe Perches		}
3185a8da38a9SJoe Perches
318647e0c88bSJoe Perches# line length limit (with some exclusions)
318747e0c88bSJoe Perches#
318847e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
318947e0c88bSJoe Perches#	logging functions like pr_info that end in a string
319047e0c88bSJoe Perches#	lines with a single string
319147e0c88bSJoe Perches#	#defines that are a single string
31922e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
319347e0c88bSJoe Perches#
319447e0c88bSJoe Perches# There are 3 different line length message types:
3195ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
319647e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
319747e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
319847e0c88bSJoe Perches#
319947e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
320047e0c88bSJoe Perches#
320147e0c88bSJoe Perches
3202b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
320347e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
320447e0c88bSJoe Perches
320547e0c88bSJoe Perches			# Check the allowed long line types first
320647e0c88bSJoe Perches
320747e0c88bSJoe Perches			# logging functions that end in a string that starts
320847e0c88bSJoe Perches			# before $max_line_length
320947e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
321047e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
321147e0c88bSJoe Perches				$msg_type = "";
321247e0c88bSJoe Perches
321347e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
321447e0c88bSJoe Perches			# #defines with only strings
321547e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
321647e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
321747e0c88bSJoe Perches				$msg_type = "";
321847e0c88bSJoe Perches
3219cc147506SJoe Perches			# More special cases
3220cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3221cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3222d560a5f8SJoe Perches				$msg_type = "";
3223d560a5f8SJoe Perches
32242e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
32252e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
32262e4bbbc5SAndreas Brauchli				$msg_type = "";
32272e4bbbc5SAndreas Brauchli
322847e0c88bSJoe Perches			# Otherwise set the alternate message types
322947e0c88bSJoe Perches
323047e0c88bSJoe Perches			# a comment starts before $max_line_length
323147e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
323247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
323347e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
323447e0c88bSJoe Perches
323547e0c88bSJoe Perches			# a quoted string starts before $max_line_length
323647e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
323747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
323847e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
323947e0c88bSJoe Perches			}
324047e0c88bSJoe Perches
324147e0c88bSJoe Perches			if ($msg_type ne "" &&
324247e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
324347e0c88bSJoe Perches				WARN($msg_type,
32446cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
32450a920b5bSAndy Whitcroft			}
324647e0c88bSJoe Perches		}
32470a920b5bSAndy Whitcroft
32488905a67cSAndy Whitcroft# check for adding lines without a newline.
32498905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3250000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3251000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
32528905a67cSAndy Whitcroft		}
32538905a67cSAndy Whitcroft
3254b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3255de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
32560a920b5bSAndy Whitcroft
32570a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3258713a09deSAntonio Borneo# more than $tabsize must use tabs.
3259c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3260c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3261c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3262d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32633705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
32643705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
32653705ce5bSJoe Perches			    $fix) {
3266194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
32673705ce5bSJoe Perches			}
32680a920b5bSAndy Whitcroft		}
32690a920b5bSAndy Whitcroft
327008e44365SAlberto Panizzo# check for space before tabs.
327108e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
327208e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32733705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
32743705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
32753705ce5bSJoe Perches			    $fix) {
3276194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3277713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3278194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3279c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
32803705ce5bSJoe Perches			}
328108e44365SAlberto Panizzo		}
328208e44365SAlberto Panizzo
32836a487211SJoe Perches# check for assignments on the start of a line
32846a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
32856a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
32866a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
32876a487211SJoe Perches		}
32886a487211SJoe Perches
3289d1fe9c09SJoe Perches# check for && or || at the start of a line
3290d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3291d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3292d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3293d1fe9c09SJoe Perches		}
3294d1fe9c09SJoe Perches
3295a91e8994SJoe Perches# check indentation starts on a tab stop
32965b57980dSJoe Perches		if ($perl_version_ok &&
3297bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3298a91e8994SJoe Perches			my $indent = length($1);
3299713a09deSAntonio Borneo			if ($indent % $tabsize) {
3300a91e8994SJoe Perches				if (WARN("TABSTOP",
3301a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3302a91e8994SJoe Perches				    $fix) {
3303713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3304a91e8994SJoe Perches				}
3305a91e8994SJoe Perches			}
3306a91e8994SJoe Perches		}
3307a91e8994SJoe Perches
3308d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
33095b57980dSJoe Perches		if ($perl_version_ok &&
3310fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3311d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3312d1fe9c09SJoe Perches			my $oldindent = $1;
3313d1fe9c09SJoe Perches			my $rest = $2;
3314d1fe9c09SJoe Perches
3315d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3316d1fe9c09SJoe Perches			if ($pos >= 0) {
3317b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3318b34a26f3SJoe Perches				my $newindent = $2;
3319d1fe9c09SJoe Perches
3320d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3321713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3322713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3323d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3324d1fe9c09SJoe Perches
3325d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3326d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
33273705ce5bSJoe Perches
33283705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
33293705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
33303705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3331194f66fcSJoe Perches						$fixed[$fixlinenr] =~
33323705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
33333705ce5bSJoe Perches					}
3334d1fe9c09SJoe Perches				}
3335d1fe9c09SJoe Perches			}
3336d1fe9c09SJoe Perches		}
3337d1fe9c09SJoe Perches
33386ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
33396ab3a970SJoe Perches# avoid checking a few false positives:
33406ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
33416ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
33426ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
33436ab3a970SJoe Perches#   multiline macros that define functions
33446ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
33456ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
33466ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
33473705ce5bSJoe Perches			if (CHK("SPACING",
3348f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
33493705ce5bSJoe Perches			    $fix) {
3350194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3351f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
33523705ce5bSJoe Perches			}
3353aad4f614SJoe Perches		}
3354aad4f614SJoe Perches
335586406b1cSJoe Perches# Block comment styles
335686406b1cSJoe Perches# Networking with an initial /*
335705880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3358fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
335985ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
336085ad978cSJoe Perches		    $realline > 2) {
336105880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
336205880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
336305880600SJoe Perches		}
336405880600SJoe Perches
336586406b1cSJoe Perches# Block comments use * on subsequent lines
336686406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
336786406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3368a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
336961135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3370a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
337186406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
337286406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3373a605e32eSJoe Perches		}
3374a605e32eSJoe Perches
337586406b1cSJoe Perches# Block comments use */ on trailing lines
337686406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3377c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3378c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3379c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
338086406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
338186406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
338205880600SJoe Perches		}
338305880600SJoe Perches
338408eb9b80SJoe Perches# Block comment * alignment
338508eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3386af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3387af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3388af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
338908eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3390af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3391af207524SJoe Perches			my $oldindent;
339208eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3393af207524SJoe Perches			if (defined($1)) {
3394af207524SJoe Perches				$oldindent = expand_tabs($1);
3395af207524SJoe Perches			} else {
3396af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3397af207524SJoe Perches				$oldindent = expand_tabs($1);
3398af207524SJoe Perches			}
339908eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
340008eb9b80SJoe Perches			my $newindent = $1;
340108eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3402af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
340308eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
340408eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
340508eb9b80SJoe Perches			}
340608eb9b80SJoe Perches		}
340708eb9b80SJoe Perches
34087f619191SJoe Perches# check for missing blank lines after struct/union declarations
34097f619191SJoe Perches# with exceptions for various attributes and macros
34107f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
34117f619191SJoe Perches		    $line =~ /^\+/ &&
34127f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
34137f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
34147f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
34157f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
34167f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
34177f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
34187f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
34190bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
34207f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3421d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3422d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3423d752fcc8SJoe Perches			    $fix) {
3424f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3425d752fcc8SJoe Perches			}
34267f619191SJoe Perches		}
34277f619191SJoe Perches
3428365dd4eaSJoe Perches# check for multiple consecutive blank lines
3429365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3430365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3431365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3432d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3433d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3434d752fcc8SJoe Perches			    $fix) {
3435f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3436d752fcc8SJoe Perches			}
3437d752fcc8SJoe Perches
3438365dd4eaSJoe Perches			$last_blank_line = $linenr;
3439365dd4eaSJoe Perches		}
3440365dd4eaSJoe Perches
34413b617e3bSJoe Perches# check for missing blank lines after declarations
34423f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
34433f7bac03SJoe Perches			# actual declarations
34443f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
34455a4e1fd3SJoe Perches			# function pointer declarations
34465a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34473f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34483f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34493f7bac03SJoe Perches			# known declaration macros
34503f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
34513f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
34523f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
34533f7bac03SJoe Perches			# other possible extensions of declaration lines
34543f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
34553f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
34563f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
34573f7bac03SJoe Perches			# looks like a declaration
34583f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
34595a4e1fd3SJoe Perches			# function pointer declarations
34605a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34613f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34623f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34633f7bac03SJoe Perches			# known declaration macros
34643f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
34653f7bac03SJoe Perches			# start of struct or union or enum
3466328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
34673f7bac03SJoe Perches			# start or end of block or continuation of declaration
34683f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
34693f7bac03SJoe Perches			# bitfield continuation
34703f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
34713f7bac03SJoe Perches			# other possible extensions of declaration lines
34723f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
34733f7bac03SJoe Perches			# indentation of previous and current line are the same
34743f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3475d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3476d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3477d752fcc8SJoe Perches			    $fix) {
3478f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3479d752fcc8SJoe Perches			}
34803b617e3bSJoe Perches		}
34813b617e3bSJoe Perches
34825f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
34836b4c5bebSAndy Whitcroft# Exceptions:
34846b4c5bebSAndy Whitcroft#  1) within comments
34856b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
34866b4c5bebSAndy Whitcroft#  3) hanging labels
34873705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
34885f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34893705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
34903705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
34913705ce5bSJoe Perches			    $fix) {
3492194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
34933705ce5bSJoe Perches			}
34945f7ddae6SRaffaele Recalcati		}
34955f7ddae6SRaffaele Recalcati
3496b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3497b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3498b9ea10d6SAndy Whitcroft
34995751a24eSJoe Perches# check for unusual line ending [ or (
35005751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
35015751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
35025751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
35035751a24eSJoe Perches		}
35045751a24eSJoe Perches
35054dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
35064dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
35074dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
35084dbed76fSJoe Perches			$context_function = $1;
35094dbed76fSJoe Perches		}
35104dbed76fSJoe Perches
35114dbed76fSJoe Perches# check if this appears to be the end of function declaration
35124dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
35134dbed76fSJoe Perches			undef $context_function;
35144dbed76fSJoe Perches		}
35154dbed76fSJoe Perches
3516032a4c0fSJoe Perches# check indentation of any line with a bare else
3517840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3518032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3519032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3520032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3521840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3522840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3523840080a0SJoe Perches			     defined $lines[$linenr] &&
3524840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3525032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3526032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3527032a4c0fSJoe Perches			}
3528032a4c0fSJoe Perches		}
3529032a4c0fSJoe Perches
3530c00df19aSJoe Perches# check indentation of a line with a break;
3531c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3532c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3533c00df19aSJoe Perches			my $tabs = $1;
3534c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3535c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3536c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3537c00df19aSJoe Perches			}
3538c00df19aSJoe Perches		}
3539c00df19aSJoe Perches
3540c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3541cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3542000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3543000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3544c2fdda0dSAndy Whitcroft		}
354522f2a2efSAndy Whitcroft
354656e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
354756e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
354856e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
354956e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
355056e77d70SJoe Perches		}
355156e77d70SJoe Perches
35529c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
35532b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
35542b474a1aSAndy Whitcroft		    $realline_next);
35553e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3556ca819864SJoe Perches		if ($linenr > $suppress_statement &&
35571b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3558170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3559f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3560171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3561171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3562171ae1a4SAndy Whitcroft
35633e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
35643e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
35653e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
35663e469cdcSAndy Whitcroft			# until we hit end of it.
35673e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
35683e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
35693e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
35703e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
35713e469cdcSAndy Whitcroft			}
3572f74bd194SAndy Whitcroft
35732b474a1aSAndy Whitcroft			# Find the real next line.
35742b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
35752b474a1aSAndy Whitcroft			if (defined $realline_next &&
35762b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
35772b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
35782b474a1aSAndy Whitcroft				$realline_next++;
35792b474a1aSAndy Whitcroft			}
35802b474a1aSAndy Whitcroft
3581171ae1a4SAndy Whitcroft			my $s = $stat;
3582171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3583cf655043SAndy Whitcroft
3584c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3585171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3586c2fdda0dSAndy Whitcroft
3587c2fdda0dSAndy Whitcroft			# Ignore functions being called
3588171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3589c2fdda0dSAndy Whitcroft
3590463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3591463f2864SAndy Whitcroft
3592c45dcabdSAndy Whitcroft			# declarations always start with types
3593d2506586SAndy 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) {
3594c45dcabdSAndy Whitcroft				my $type = $1;
3595c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3596c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3597c45dcabdSAndy Whitcroft
35986c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3599a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3600c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3601c2fdda0dSAndy Whitcroft			}
36028905a67cSAndy Whitcroft
36036c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
360465863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3605c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
36069c0ca6f9SAndy Whitcroft			}
36078905a67cSAndy Whitcroft
36088905a67cSAndy Whitcroft			# Check for any sort of function declaration.
36098905a67cSAndy Whitcroft			# int foo(something bar, other baz);
36108905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3611171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
36128905a67cSAndy Whitcroft				my ($name_len) = length($1);
36138905a67cSAndy Whitcroft
3614cf655043SAndy Whitcroft				my $ctx = $s;
3615773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
36168905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3617cf655043SAndy Whitcroft
36188905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3619c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
36208905a67cSAndy Whitcroft
3621c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
36228905a67cSAndy Whitcroft					}
36238905a67cSAndy Whitcroft				}
36248905a67cSAndy Whitcroft			}
36258905a67cSAndy Whitcroft
36269c0ca6f9SAndy Whitcroft		}
36279c0ca6f9SAndy Whitcroft
362800df344fSAndy Whitcroft#
362900df344fSAndy Whitcroft# Checks which may be anchored in the context.
363000df344fSAndy Whitcroft#
363100df344fSAndy Whitcroft
363200df344fSAndy Whitcroft# Check for switch () and associated case and default
363300df344fSAndy Whitcroft# statements should be at the same indent.
363400df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
363500df344fSAndy Whitcroft			my $err = '';
363600df344fSAndy Whitcroft			my $sep = '';
363700df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
363800df344fSAndy Whitcroft			shift(@ctx);
363900df344fSAndy Whitcroft			for my $ctx (@ctx) {
364000df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
364100df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
364200df344fSAndy Whitcroft							$indent != $cindent) {
364300df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
364400df344fSAndy Whitcroft					$sep = '';
364500df344fSAndy Whitcroft				} else {
364600df344fSAndy Whitcroft					$sep = "[...]\n";
364700df344fSAndy Whitcroft				}
364800df344fSAndy Whitcroft			}
364900df344fSAndy Whitcroft			if ($err ne '') {
3650000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3651000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3652de7d4f0eSAndy Whitcroft			}
3653de7d4f0eSAndy Whitcroft		}
3654de7d4f0eSAndy Whitcroft
3655de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3656de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
36570fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3658773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3659773647a0SAndy Whitcroft
36609c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
36618eef05ddSJoe Perches
36628eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
36638eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
36648eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
36658eef05ddSJoe Perches			}
36668eef05ddSJoe Perches
3667de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3668de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3669de7d4f0eSAndy Whitcroft
3670548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3671548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3672de7d4f0eSAndy Whitcroft
3673548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3674548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3675548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3676548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3677548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3678773647a0SAndy Whitcroft				$ctx_ln++;
3679773647a0SAndy Whitcroft			}
3680548596d5SAndy Whitcroft
368153210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
368253210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3683773647a0SAndy Whitcroft
3684773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3685000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3686000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
368701464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
368800df344fSAndy Whitcroft			}
3689773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3690773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3691773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3692773647a0SAndy Whitcroft			{
36939c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
36949c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3695000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3696000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
369701464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
36989c0ca6f9SAndy Whitcroft				}
36999c0ca6f9SAndy Whitcroft			}
370000df344fSAndy Whitcroft		}
370100df344fSAndy Whitcroft
37024d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3703f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
37043e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
37053e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
37063e469cdcSAndy Whitcroft					if (!defined $stat);
37074d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
37084d001e4dSAndy Whitcroft
37094d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
37104d001e4dSAndy Whitcroft
37119f5af480SJoe Perches			# remove inline comments
37129f5af480SJoe Perches			$s =~ s/$;/ /g;
37139f5af480SJoe Perches			$c =~ s/$;/ /g;
37144d001e4dSAndy Whitcroft
37154d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
37166f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
37176f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
37184d001e4dSAndy Whitcroft
37199f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
37209f5af480SJoe Perches			# none on the first line, and are going to readd them
37219f5af480SJoe Perches			# where necessary.
37229f5af480SJoe Perches			$s =~ s/\n./\n/gs;
37239f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
37249f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
37259f5af480SJoe Perches			}
37269f5af480SJoe Perches
37274d001e4dSAndy Whitcroft			# We want to check the first line inside the block
37284d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
37294d001e4dSAndy Whitcroft			#  1) any blank line termination
37304d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
37314d001e4dSAndy Whitcroft			#  3) any do (...) {
37324d001e4dSAndy Whitcroft			my $continuation = 0;
37334d001e4dSAndy Whitcroft			my $check = 0;
37344d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
37354d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
37364d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
37374d001e4dSAndy Whitcroft				$continuation = 1;
37384d001e4dSAndy Whitcroft			}
37399bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
37404d001e4dSAndy Whitcroft				$check = 1;
37414d001e4dSAndy Whitcroft				$cond_lines++;
37424d001e4dSAndy Whitcroft			}
37434d001e4dSAndy Whitcroft
37444d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
37454d001e4dSAndy Whitcroft			# preprocessor statement.
37464d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
37474d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
37484d001e4dSAndy Whitcroft				$check = 0;
37494d001e4dSAndy Whitcroft			}
37504d001e4dSAndy Whitcroft
37519bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3752740504c6SAndy Whitcroft			$continuation = 0;
37539bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
37549bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
37554d001e4dSAndy Whitcroft
3756f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3757f16fa28fSAndy Whitcroft				# is not linear.
3758f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3759f16fa28fSAndy Whitcroft					$check = 0;
3760f16fa28fSAndy Whitcroft				}
3761f16fa28fSAndy Whitcroft
37629bd49efeSAndy Whitcroft				# Ignore:
37639bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
37649bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
37659bd49efeSAndy Whitcroft				#  3) labels.
3766740504c6SAndy Whitcroft				if ($continuation ||
3767740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
37689bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
37699bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3770740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
377130dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
37729bd49efeSAndy Whitcroft						$cond_lines++;
37739bd49efeSAndy Whitcroft					}
37744d001e4dSAndy Whitcroft				}
377530dad6ebSAndy Whitcroft			}
37764d001e4dSAndy Whitcroft
37774d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
37784d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
37794d001e4dSAndy Whitcroft
37804d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
37814d001e4dSAndy Whitcroft			# this is not this patch's fault.
37824d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
37834d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
37844d001e4dSAndy Whitcroft				$check = 0;
37854d001e4dSAndy Whitcroft			}
37864d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
37874d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
37884d001e4dSAndy Whitcroft			}
37894d001e4dSAndy Whitcroft
37909bd49efeSAndy 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";
37914d001e4dSAndy Whitcroft
37929f5af480SJoe Perches			if ($check && $s ne '' &&
3793713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
37949f5af480SJoe Perches			     ($sindent < $indent) ||
3795f6950a73SJoe Perches			     ($sindent == $indent &&
3796f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3797713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
3798000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3799000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
38004d001e4dSAndy Whitcroft			}
38014d001e4dSAndy Whitcroft		}
38024d001e4dSAndy Whitcroft
38036c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
38046c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
38051f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
38061f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
38076c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3808c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3809c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3810cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3811cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
38121f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3813c2fdda0dSAndy Whitcroft		}
38146c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
38156c72ffaaSAndy Whitcroft
381600df344fSAndy Whitcroft#ignore lines not being added
38173705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
381800df344fSAndy Whitcroft
381911ca40a0SJoe Perches# check for dereferences that span multiple lines
382011ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
382111ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
382211ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
382311ca40a0SJoe Perches			my $ref = $1;
382411ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
382511ca40a0SJoe Perches			$ref .= $1;
382611ca40a0SJoe Perches			$ref =~ s/\s//g;
382711ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
382811ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
382911ca40a0SJoe Perches		}
383011ca40a0SJoe Perches
3831a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3832c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3833a1ce18e4SJoe Perches			my $type = $1;
3834a1ce18e4SJoe Perches			my $var = $2;
3835207a8e84SJoe Perches			$var = "" if (!defined $var);
3836207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3837a1ce18e4SJoe Perches				my $sign = $1;
3838a1ce18e4SJoe Perches				my $pointer = $2;
3839a1ce18e4SJoe Perches
3840a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3841a1ce18e4SJoe Perches
3842a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3843a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3844a1ce18e4SJoe Perches				    $fix) {
3845a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3846207a8e84SJoe Perches					my $comp_pointer = $pointer;
3847207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3848207a8e84SJoe Perches					$decl .= $comp_pointer;
3849207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3850207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3851a1ce18e4SJoe Perches				}
3852a1ce18e4SJoe Perches			}
3853a1ce18e4SJoe Perches		}
3854a1ce18e4SJoe Perches
3855653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
38567429c690SAndy Whitcroft		if ($dbg_type) {
38577429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3858000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3859000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
38607429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3861000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3862000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
38637429c690SAndy Whitcroft			}
3864653d4876SAndy Whitcroft			next;
3865653d4876SAndy Whitcroft		}
3866a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3867a1ef277eSAndy Whitcroft		if ($dbg_attr) {
38689360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3869000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3870000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
38719360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3872000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3873000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3874a1ef277eSAndy Whitcroft			}
3875a1ef277eSAndy Whitcroft			next;
3876a1ef277eSAndy Whitcroft		}
3877653d4876SAndy Whitcroft
3878f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
387999423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
388099423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3881d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3882d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3883f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3884f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3885f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3886d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3887d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3888f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3889d752fcc8SJoe Perches				$fixedline = $line;
38908d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3891f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3892d752fcc8SJoe Perches			}
3893f0a594c1SAndy Whitcroft		}
3894f0a594c1SAndy Whitcroft
389500df344fSAndy Whitcroft#
389600df344fSAndy Whitcroft# Checks which are anchored on the added line.
389700df344fSAndy Whitcroft#
389800df344fSAndy Whitcroft
3899653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3900c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3901653d4876SAndy Whitcroft			my $path = $1;
3902653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3903000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3904495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3905495e9d84SJoe Perches			}
3906495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3907495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3908495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3909653d4876SAndy Whitcroft			}
3910653d4876SAndy Whitcroft		}
3911653d4876SAndy Whitcroft
391200df344fSAndy Whitcroft# no C99 // comments
391300df344fSAndy Whitcroft		if ($line =~ m{//}) {
39143705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
39153705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
39163705ce5bSJoe Perches			    $fix) {
3917194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
39183705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
39193705ce5bSJoe Perches					my $comment = trim($1);
3920194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
39213705ce5bSJoe Perches				}
39223705ce5bSJoe Perches			}
392300df344fSAndy Whitcroft		}
392400df344fSAndy Whitcroft		# Remove C99 comments.
39250a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
39266c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
39270a920b5bSAndy Whitcroft
39282b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
39292b474a1aSAndy Whitcroft# the whole statement.
39302b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
39312b474a1aSAndy Whitcroft		if (defined $realline_next &&
39322b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
39332b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
39342b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39352b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39363cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
39373cbf62dfSAndy Whitcroft			# a prefix:
39383cbf62dfSAndy Whitcroft			#   XXX(foo);
39393cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3940653d4876SAndy Whitcroft			my $name = $1;
394187a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
39423cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
39433cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
39443cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
39453cbf62dfSAndy Whitcroft
39463cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
39472b474a1aSAndy Whitcroft				\n.}\s*$|
394848012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
394948012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
395048012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
39512b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
39522b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
395348012058SAndy Whitcroft			    )/x) {
39542b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
39552b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
39562b474a1aSAndy Whitcroft			} else {
39572b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
39580a920b5bSAndy Whitcroft			}
39590a920b5bSAndy Whitcroft		}
39602b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
39612b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
39622b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39632b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39642b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
39652b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
39662b474a1aSAndy Whitcroft		}
39672b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
39682b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3969000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3970000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
39712b474a1aSAndy Whitcroft		}
39720a920b5bSAndy Whitcroft
39735150bda4SJoe Eloff# check for global initialisers.
39746d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3975d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
39766d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3977d5e616fcSJoe Perches			    $fix) {
39786d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3979d5e616fcSJoe Perches			}
3980f0a594c1SAndy Whitcroft		}
39810a920b5bSAndy Whitcroft# check for static initialisers.
39826d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3983d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
39846d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3985d5e616fcSJoe Perches				      $herecurr) &&
3986d5e616fcSJoe Perches			    $fix) {
39876d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3988d5e616fcSJoe Perches			}
39890a920b5bSAndy Whitcroft		}
39900a920b5bSAndy Whitcroft
39911813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
39921813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
39931813087dSJoe Perches			my $tmp = trim($1);
39941813087dSJoe Perches			WARN("MISORDERED_TYPE",
39951813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
39961813087dSJoe Perches		}
39971813087dSJoe Perches
3998809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3999809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4000809e082eSJoe Perches			my $type = trim($1);
4001809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4002809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4003809e082eSJoe Perches			my $new_type = $type;
4004809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4005809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4006809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4007809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4008809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4009809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4010809e082eSJoe Perches			$new_type = trim($new_type);
4011809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4012809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4013809e082eSJoe Perches			    $fix) {
4014809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4015809e082eSJoe Perches			}
4016809e082eSJoe Perches		}
4017809e082eSJoe Perches
4018cb710ecaSJoe Perches# check for static const char * arrays.
4019cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4020000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4021000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4022cb710ecaSJoe Perches				$herecurr);
4023cb710ecaSJoe Perches		}
4024cb710ecaSJoe Perches
402577b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
402677b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
402777b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
402877b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
402977b8c0a8SJoe Perches			    $fix) {
403077b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
403177b8c0a8SJoe Perches			}
403277b8c0a8SJoe Perches		}
403377b8c0a8SJoe Perches
4034cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4035cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4036000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4037000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4038cb710ecaSJoe Perches				$herecurr);
4039cb710ecaSJoe Perches		}
4040cb710ecaSJoe Perches
4041ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4042ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4043ab7e23f3SJoe Perches			my $found = $1;
4044ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4045ab7e23f3SJoe Perches				WARN("CONST_CONST",
4046ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4047ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4048ab7e23f3SJoe Perches				WARN("CONST_CONST",
4049ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4050ab7e23f3SJoe Perches			}
4051ab7e23f3SJoe Perches		}
4052ab7e23f3SJoe Perches
40539b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
40549b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
40559b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
40569b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
40579b0fa60dSJoe Perches				$herecurr);
40589b0fa60dSJoe Perches               }
40599b0fa60dSJoe Perches
4060b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4061b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4062b598b670SJoe Perches			my $array = $1;
4063b598b670SJoe 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*\))@) {
4064b598b670SJoe Perches				my $array_div = $1;
4065b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4066b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4067b598b670SJoe Perches				    $fix) {
4068b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4069b598b670SJoe Perches				}
4070b598b670SJoe Perches			}
4071b598b670SJoe Perches		}
4072b598b670SJoe Perches
4073b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
407416b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4075b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4076b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4077b36190c5SJoe Perches			    $fix) {
4078194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4079b36190c5SJoe Perches			}
4080b36190c5SJoe Perches		}
4081b36190c5SJoe Perches
4082653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4083653d4876SAndy Whitcroft# make sense.
4084653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
40858054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4086c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
40878ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
408846d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4089000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4090000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
40910a920b5bSAndy Whitcroft		}
40920a920b5bSAndy Whitcroft
40930a920b5bSAndy Whitcroft# * goes on variable not on type
409465863862SAndy Whitcroft		# (char*[ const])
4095bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4096bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
40973705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4098d8aaf121SAndy Whitcroft
409965863862SAndy Whitcroft			# Should start with a space.
410065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
410165863862SAndy Whitcroft			# Should not end with a space.
410265863862SAndy Whitcroft			$to =~ s/\s+$//;
410365863862SAndy Whitcroft			# '*'s should not have spaces between.
4104f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
410565863862SAndy Whitcroft			}
4106d8aaf121SAndy Whitcroft
41073705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
410865863862SAndy Whitcroft			if ($from ne $to) {
41093705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
41103705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
41113705ce5bSJoe Perches				    $fix) {
41123705ce5bSJoe Perches					my $sub_from = $ident;
41133705ce5bSJoe Perches					my $sub_to = $ident;
41143705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4115194f66fcSJoe Perches					$fixed[$fixlinenr] =~
41163705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
41173705ce5bSJoe Perches				}
411865863862SAndy Whitcroft			}
4119bfcb2cc7SAndy Whitcroft		}
4120bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4121bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
41223705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4123d8aaf121SAndy Whitcroft
412465863862SAndy Whitcroft			# Should start with a space.
412565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
412665863862SAndy Whitcroft			# Should not end with a space.
412765863862SAndy Whitcroft			$to =~ s/\s+$//;
412865863862SAndy Whitcroft			# '*'s should not have spaces between.
4129f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
413065863862SAndy Whitcroft			}
413165863862SAndy Whitcroft			# Modifiers should have spaces.
413265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
413365863862SAndy Whitcroft
41343705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4135667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
41363705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
41373705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
41383705ce5bSJoe Perches				    $fix) {
41393705ce5bSJoe Perches
41403705ce5bSJoe Perches					my $sub_from = $match;
41413705ce5bSJoe Perches					my $sub_to = $match;
41423705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4143194f66fcSJoe Perches					$fixed[$fixlinenr] =~
41443705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
41453705ce5bSJoe Perches				}
414665863862SAndy Whitcroft			}
41470a920b5bSAndy Whitcroft		}
41480a920b5bSAndy Whitcroft
41499d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
41509d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
41510675a8fbSJean Delvare			my $msg_level = \&WARN;
41520675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
41530675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
41549d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
41559d3e3c70SJoe Perches		}
41560a920b5bSAndy Whitcroft
41579d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
41588905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4159000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4160000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
41618905a67cSAndy Whitcroft		}
41628905a67cSAndy Whitcroft
416317441227SJoe Perches# check for uses of printk_ratelimit
416417441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4165000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4166000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
416717441227SJoe Perches		}
416817441227SJoe Perches
4169eeef5733SJoe Perches# printk should use KERN_* levels
4170eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4171000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4172eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
417300df344fSAndy Whitcroft		}
41740a920b5bSAndy Whitcroft
4175243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4176243f3803SJoe Perches			my $orig = $1;
4177243f3803SJoe Perches			my $level = lc($orig);
4178243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
41798f26b837SJoe Perches			my $level2 = $level;
41808f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4181243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4182daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4183243f3803SJoe Perches		}
4184243f3803SJoe Perches
4185dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4186dc139313SJoe Perches			my $orig = $1;
4187dc139313SJoe Perches			my $level = lc($orig);
4188dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4189dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4190dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4191dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4192dc139313SJoe Perches		}
4193dc139313SJoe Perches
419491c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
419591c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
419691c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
419791c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
419891c9afafSAndy Lutomirski			WARN("ENOSYS",
419991c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
420091c9afafSAndy Lutomirski		}
420191c9afafSAndy Lutomirski
4202653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4203653d4876SAndy Whitcroft# or if closed on same line
42045b57980dSJoe Perches		if ($perl_version_ok &&
42052d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
42062d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
42072d453e3bSJoe Perches		    $sline !~ /}/) {
42088d182478SJoe Perches			if (ERROR("OPEN_BRACE",
42092d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
42108d182478SJoe Perches			    $fix) {
42118d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
42128d182478SJoe Perches				my $fixed_line = $rawline;
42138d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
42148d182478SJoe Perches				my $line1 = $1;
42158d182478SJoe Perches				my $line2 = $2;
42168d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
42178d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
42188d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
42198d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
42208d182478SJoe Perches				}
42218d182478SJoe Perches			}
42220a920b5bSAndy Whitcroft		}
4223653d4876SAndy Whitcroft
42248905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
42258905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
42268905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
42278d182478SJoe Perches			if (ERROR("OPEN_BRACE",
42288d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
42298d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
42308d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
42318d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
42328d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
42338d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
42348d182478SJoe Perches				$fixedline = $rawline;
42358d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
42368d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
42378d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
42388d182478SJoe Perches				}
42398d182478SJoe Perches			}
42408905a67cSAndy Whitcroft		}
42418905a67cSAndy Whitcroft
42420c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
42433705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
42443705ce5bSJoe Perches			if (WARN("SPACING",
42453705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
42463705ce5bSJoe Perches			    $fix) {
4247194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42483705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
42493705ce5bSJoe Perches			}
42500c73b4ebSAndy Whitcroft		}
42510c73b4ebSAndy Whitcroft
425231070b5dSJoe Perches# Function pointer declarations
425331070b5dSJoe Perches# check spacing between type, funcptr, and args
425431070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
425591f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
425631070b5dSJoe Perches			my $declare = $1;
425731070b5dSJoe Perches			my $pre_pointer_space = $2;
425831070b5dSJoe Perches			my $post_pointer_space = $3;
425931070b5dSJoe Perches			my $funcname = $4;
426031070b5dSJoe Perches			my $post_funcname_space = $5;
426131070b5dSJoe Perches			my $pre_args_space = $6;
426231070b5dSJoe Perches
426391f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
426491f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
426591f72e9cSJoe Perches# don't need a space so don't warn for those.
426691f72e9cSJoe Perches			my $post_declare_space = "";
426791f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
426891f72e9cSJoe Perches				$post_declare_space = $1;
426991f72e9cSJoe Perches				$declare = rtrim($declare);
427091f72e9cSJoe Perches			}
427191f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
427231070b5dSJoe Perches				WARN("SPACING",
427331070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
427491f72e9cSJoe Perches				$post_declare_space = " ";
427531070b5dSJoe Perches			}
427631070b5dSJoe Perches
427731070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
427891f72e9cSJoe Perches# This test is not currently implemented because these declarations are
427991f72e9cSJoe Perches# equivalent to
428091f72e9cSJoe Perches#	int  foo(int bar, ...)
428191f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
428291f72e9cSJoe Perches#
428391f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
428491f72e9cSJoe Perches#				WARN("SPACING",
428591f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
428691f72e9cSJoe Perches#			}
428731070b5dSJoe Perches
428831070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
428931070b5dSJoe Perches			if (defined $pre_pointer_space &&
429031070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
429131070b5dSJoe Perches				WARN("SPACING",
429231070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
429331070b5dSJoe Perches			}
429431070b5dSJoe Perches
429531070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
429631070b5dSJoe Perches			if (defined $post_pointer_space &&
429731070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
429831070b5dSJoe Perches				WARN("SPACING",
429931070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
430031070b5dSJoe Perches			}
430131070b5dSJoe Perches
430231070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
430331070b5dSJoe Perches			if (defined $post_funcname_space &&
430431070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
430531070b5dSJoe Perches				WARN("SPACING",
430631070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
430731070b5dSJoe Perches			}
430831070b5dSJoe Perches
430931070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
431031070b5dSJoe Perches			if (defined $pre_args_space &&
431131070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
431231070b5dSJoe Perches				WARN("SPACING",
431331070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
431431070b5dSJoe Perches			}
431531070b5dSJoe Perches
431631070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4317194f66fcSJoe Perches				$fixed[$fixlinenr] =~
431891f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
431931070b5dSJoe Perches			}
432031070b5dSJoe Perches		}
432131070b5dSJoe Perches
43228d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
43238d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4324fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4325fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
43268d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
43278d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
43288d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4329fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
433038dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
43313705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
43323705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
43333705ce5bSJoe Perches				    $fix) {
4334194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
43353705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
43363705ce5bSJoe Perches				}
43378d31cfceSAndy Whitcroft			}
43388d31cfceSAndy Whitcroft		}
43398d31cfceSAndy Whitcroft
4340f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
43416c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4342c2fdda0dSAndy Whitcroft			my $name = $1;
4343773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4344773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4345c2fdda0dSAndy Whitcroft
4346c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4347773647a0SAndy Whitcroft			if ($name =~ /^(?:
4348773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4349773647a0SAndy Whitcroft				volatile|__volatile__|
4350773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4351773647a0SAndy Whitcroft				asm|__asm__)$/x)
4352773647a0SAndy Whitcroft			{
4353c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4354c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4355c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4356c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4357773647a0SAndy Whitcroft
4358773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4359c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4360c2fdda0dSAndy Whitcroft
4361c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4362c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4363773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4364c2fdda0dSAndy Whitcroft
4365c2fdda0dSAndy Whitcroft			} else {
43663705ce5bSJoe Perches				if (WARN("SPACING",
43673705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
43683705ce5bSJoe Perches					     $fix) {
4369194f66fcSJoe Perches					$fixed[$fixlinenr] =~
43703705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
43713705ce5bSJoe Perches				}
4372f0a594c1SAndy Whitcroft			}
43736c72ffaaSAndy Whitcroft		}
43749a4cad4eSEric Nelson
4375653d4876SAndy Whitcroft# Check operator spacing.
43760a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
43773705ce5bSJoe Perches			my $fixed_line = "";
43783705ce5bSJoe Perches			my $line_fixed = 0;
43793705ce5bSJoe Perches
43809c0ca6f9SAndy Whitcroft			my $ops = qr{
43819c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
43829c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
43839c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
43841f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
438584731623SJoe Perches				\?:|\?|:
43869c0ca6f9SAndy Whitcroft			}x;
4387cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
43883705ce5bSJoe Perches
43893705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
43903705ce5bSJoe Perches##			foreach my $el (@elements) {
43913705ce5bSJoe Perches##				print("el: <$el>\n");
43923705ce5bSJoe Perches##			}
43933705ce5bSJoe Perches
43943705ce5bSJoe Perches			my @fix_elements = ();
439500df344fSAndy Whitcroft			my $off = 0;
43966c72ffaaSAndy Whitcroft
43973705ce5bSJoe Perches			foreach my $el (@elements) {
43983705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
43993705ce5bSJoe Perches				$off += length($el);
44003705ce5bSJoe Perches			}
44013705ce5bSJoe Perches
44023705ce5bSJoe Perches			$off = 0;
44033705ce5bSJoe Perches
44046c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4405b34c648bSJoe Perches			my $last_after = -1;
44066c72ffaaSAndy Whitcroft
44070a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
44083705ce5bSJoe Perches
44093705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
44103705ce5bSJoe Perches
44113705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
44123705ce5bSJoe Perches
44134a0df2efSAndy Whitcroft				$off += length($elements[$n]);
44144a0df2efSAndy Whitcroft
441525985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4416773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4417773647a0SAndy Whitcroft				my $cc = '';
4418773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4419773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4420773647a0SAndy Whitcroft				}
4421773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4422773647a0SAndy Whitcroft
44234a0df2efSAndy Whitcroft				my $a = '';
44244a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
44254a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4426cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
44274a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
44284a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4429773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
44304a0df2efSAndy Whitcroft
44310a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
44324a0df2efSAndy Whitcroft
44334a0df2efSAndy Whitcroft				my $c = '';
44340a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
44354a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
44364a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4437cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
44384a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
44394a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
44408b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
44414a0df2efSAndy Whitcroft				} else {
44424a0df2efSAndy Whitcroft					$c = 'E';
44430a920b5bSAndy Whitcroft				}
44440a920b5bSAndy Whitcroft
44454a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
44464a0df2efSAndy Whitcroft
44474a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
44484a0df2efSAndy Whitcroft
44496c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4450de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
44510a920b5bSAndy Whitcroft
445274048ed8SAndy Whitcroft				# Pull out the value of this operator.
44536c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
44540a920b5bSAndy Whitcroft
44551f65f947SAndy Whitcroft				# Get the full operator variant.
44561f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
44571f65f947SAndy Whitcroft
445813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
445913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4460d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
446113214adfSAndy Whitcroft
4462cf655043SAndy Whitcroft#				# Ignore comments
4463cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
446413214adfSAndy Whitcroft
4465d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
446613214adfSAndy Whitcroft				} elsif ($op eq ';') {
4467cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4468cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
44693705ce5bSJoe Perches						if (ERROR("SPACING",
44703705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4471b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44723705ce5bSJoe Perches							$line_fixed = 1;
44733705ce5bSJoe Perches						}
4474d8aaf121SAndy Whitcroft					}
4475d8aaf121SAndy Whitcroft
4476d8aaf121SAndy Whitcroft				# // is a comment
4477d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
44780a920b5bSAndy Whitcroft
4479b00e4814SJoe Perches				#   :   when part of a bitfield
4480b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4481b00e4814SJoe Perches					# skip the bitfield test for now
4482b00e4814SJoe Perches
44831f65f947SAndy Whitcroft				# No spaces for:
44841f65f947SAndy Whitcroft				#   ->
4485b00e4814SJoe Perches				} elsif ($op eq '->') {
44864a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
44873705ce5bSJoe Perches						if (ERROR("SPACING",
44883705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4489b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44903705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44913705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44923705ce5bSJoe Perches							}
4493b34c648bSJoe Perches							$line_fixed = 1;
44943705ce5bSJoe Perches						}
44950a920b5bSAndy Whitcroft					}
44960a920b5bSAndy Whitcroft
44972381097bSJoe Perches				# , must not have a space before and must have a space on the right.
44980a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
44992381097bSJoe Perches					my $rtrim_before = 0;
45002381097bSJoe Perches					my $space_after = 0;
45012381097bSJoe Perches					if ($ctx =~ /Wx./) {
45022381097bSJoe Perches						if (ERROR("SPACING",
45032381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
45042381097bSJoe Perches							$line_fixed = 1;
45052381097bSJoe Perches							$rtrim_before = 1;
45062381097bSJoe Perches						}
45072381097bSJoe Perches					}
4508cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
45093705ce5bSJoe Perches						if (ERROR("SPACING",
45103705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
45113705ce5bSJoe Perches							$line_fixed = 1;
4512b34c648bSJoe Perches							$last_after = $n;
45132381097bSJoe Perches							$space_after = 1;
45142381097bSJoe Perches						}
45152381097bSJoe Perches					}
45162381097bSJoe Perches					if ($rtrim_before || $space_after) {
45172381097bSJoe Perches						if ($rtrim_before) {
45182381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45192381097bSJoe Perches						} else {
45202381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45212381097bSJoe Perches						}
45222381097bSJoe Perches						if ($space_after) {
45232381097bSJoe Perches							$good .= " ";
45243705ce5bSJoe Perches						}
45250a920b5bSAndy Whitcroft					}
45260a920b5bSAndy Whitcroft
45279c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
452874048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
45299c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
45309c0ca6f9SAndy Whitcroft
45319c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
45329c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
45339c0ca6f9SAndy Whitcroft				# unary operator, or a cast
45349c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
453574048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
45360d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4537cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
45383705ce5bSJoe Perches						if (ERROR("SPACING",
45393705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4540b34c648bSJoe Perches							if ($n != $last_after + 2) {
4541b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
45423705ce5bSJoe Perches								$line_fixed = 1;
45433705ce5bSJoe Perches							}
45440a920b5bSAndy Whitcroft						}
4545b34c648bSJoe Perches					}
4546a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4547171ae1a4SAndy Whitcroft						# A unary '*' may be const
4548171ae1a4SAndy Whitcroft
4549171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
45503705ce5bSJoe Perches						if (ERROR("SPACING",
45513705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4552b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
45533705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45543705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
45553705ce5bSJoe Perches							}
4556b34c648bSJoe Perches							$line_fixed = 1;
45573705ce5bSJoe Perches						}
45580a920b5bSAndy Whitcroft					}
45590a920b5bSAndy Whitcroft
45600a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
45610a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4562773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
45633705ce5bSJoe Perches						if (ERROR("SPACING",
45643705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4565b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
45663705ce5bSJoe Perches							$line_fixed = 1;
45673705ce5bSJoe Perches						}
45680a920b5bSAndy Whitcroft					}
4569773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4570773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
45713705ce5bSJoe Perches						if (ERROR("SPACING",
45723705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4573b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45743705ce5bSJoe Perches							$line_fixed = 1;
45753705ce5bSJoe Perches						}
4576653d4876SAndy Whitcroft					}
4577773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
45783705ce5bSJoe Perches						if (ERROR("SPACING",
45793705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4580b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45813705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45823705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4583773647a0SAndy Whitcroft							}
4584b34c648bSJoe Perches							$line_fixed = 1;
45853705ce5bSJoe Perches						}
45863705ce5bSJoe Perches					}
45870a920b5bSAndy Whitcroft
45880a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
45899c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
45909c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
45919c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4592c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4593c2fdda0dSAndy Whitcroft					 $op eq '%')
45940a920b5bSAndy Whitcroft				{
4595d2e025f3SJoe Perches					if ($check) {
4596d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4597d2e025f3SJoe Perches							if (CHK("SPACING",
4598d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4599d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4600d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4601d2e025f3SJoe Perches								$line_fixed = 1;
4602d2e025f3SJoe Perches							}
4603d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4604d2e025f3SJoe Perches							if (CHK("SPACING",
4605d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4606d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4607d2e025f3SJoe Perches								$line_fixed = 1;
4608d2e025f3SJoe Perches							}
4609d2e025f3SJoe Perches						}
4610d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
46113705ce5bSJoe Perches						if (ERROR("SPACING",
46123705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4613b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4614b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4615b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4616b34c648bSJoe Perches							}
46173705ce5bSJoe Perches							$line_fixed = 1;
46183705ce5bSJoe Perches						}
46190a920b5bSAndy Whitcroft					}
46200a920b5bSAndy Whitcroft
46211f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
46221f65f947SAndy Whitcroft				# terminating a case value or a label.
46231f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
46241f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
46253705ce5bSJoe Perches						if (ERROR("SPACING",
46263705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4627b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
46283705ce5bSJoe Perches							$line_fixed = 1;
46293705ce5bSJoe Perches						}
46301f65f947SAndy Whitcroft					}
46311f65f947SAndy Whitcroft
46320a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4633cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
46341f65f947SAndy Whitcroft					my $ok = 0;
46351f65f947SAndy Whitcroft
463622f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
46371f65f947SAndy Whitcroft					if (($op eq '<' &&
46381f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
46391f65f947SAndy Whitcroft					    ($op eq '>' &&
46401f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
46411f65f947SAndy Whitcroft					{
46421f65f947SAndy Whitcroft						$ok = 1;
46431f65f947SAndy Whitcroft					}
46441f65f947SAndy Whitcroft
4645e0df7e1fSJoe Perches					# for asm volatile statements
4646e0df7e1fSJoe Perches					# ignore a colon with another
4647e0df7e1fSJoe Perches					# colon immediately before or after
4648e0df7e1fSJoe Perches					if (($op eq ':') &&
4649e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4650e0df7e1fSJoe Perches						$ok = 1;
4651e0df7e1fSJoe Perches					}
4652e0df7e1fSJoe Perches
465384731623SJoe Perches					# messages are ERROR, but ?: are CHK
46541f65f947SAndy Whitcroft					if ($ok == 0) {
46550675a8fbSJean Delvare						my $msg_level = \&ERROR;
46560675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
465784731623SJoe Perches
46580675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
46593705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4660b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4661b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4662b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4663b34c648bSJoe Perches							}
46643705ce5bSJoe Perches							$line_fixed = 1;
46653705ce5bSJoe Perches						}
46660a920b5bSAndy Whitcroft					}
466722f2a2efSAndy Whitcroft				}
46684a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
46693705ce5bSJoe Perches
46703705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
46713705ce5bSJoe Perches
46723705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
46730a920b5bSAndy Whitcroft			}
46743705ce5bSJoe Perches
46753705ce5bSJoe Perches			if (($#elements % 2) == 0) {
46763705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
46773705ce5bSJoe Perches			}
46783705ce5bSJoe Perches
4679194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4680194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
46813705ce5bSJoe Perches			}
46823705ce5bSJoe Perches
46833705ce5bSJoe Perches
46840a920b5bSAndy Whitcroft		}
46850a920b5bSAndy Whitcroft
4686786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4687d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4688786b6326SJoe Perches			if (WARN("SPACING",
4689786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4690786b6326SJoe Perches			    $fix) {
4691194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4692786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4693786b6326SJoe Perches			}
4694786b6326SJoe Perches		}
4695786b6326SJoe Perches
4696f0a594c1SAndy Whitcroft# check for multiple assignments
4697f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4698000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4699000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4700f0a594c1SAndy Whitcroft		}
4701f0a594c1SAndy Whitcroft
470222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
470322f2a2efSAndy Whitcroft## # continuation.
470422f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
470522f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
470622f2a2efSAndy Whitcroft##
470722f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
470822f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
470922f2a2efSAndy Whitcroft## 			my $ln = $line;
471022f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
471122f2a2efSAndy Whitcroft## 			}
471222f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4713000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4714000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
471522f2a2efSAndy Whitcroft## 			}
471622f2a2efSAndy Whitcroft## 		}
4717f0a594c1SAndy Whitcroft
47180a920b5bSAndy Whitcroft#need space before brace following if, while, etc
47196b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
47206ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
47213705ce5bSJoe Perches			if (ERROR("SPACING",
47223705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
47233705ce5bSJoe Perches			    $fix) {
47246ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
47253705ce5bSJoe Perches			}
4726de7d4f0eSAndy Whitcroft		}
4727de7d4f0eSAndy Whitcroft
4728c4a62ef9SJoe Perches## # check for blank lines before declarations
4729c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4730c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4731c4a62ef9SJoe Perches##			WARN("SPACING",
4732c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4733c4a62ef9SJoe Perches##		}
4734c4a62ef9SJoe Perches##
4735c4a62ef9SJoe Perches
4736de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4737de7d4f0eSAndy Whitcroft# on the line
473894fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4739d5e616fcSJoe Perches			if (ERROR("SPACING",
4740d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4741d5e616fcSJoe Perches			    $fix) {
4742194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4743d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4744d5e616fcSJoe Perches			}
47450a920b5bSAndy Whitcroft		}
47460a920b5bSAndy Whitcroft
474722f2a2efSAndy Whitcroft# check spacing on square brackets
474822f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
47493705ce5bSJoe Perches			if (ERROR("SPACING",
47503705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
47513705ce5bSJoe Perches			    $fix) {
4752194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47533705ce5bSJoe Perches				    s/\[\s+/\[/;
47543705ce5bSJoe Perches			}
475522f2a2efSAndy Whitcroft		}
475622f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
47573705ce5bSJoe Perches			if (ERROR("SPACING",
47583705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
47593705ce5bSJoe Perches			    $fix) {
4760194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47613705ce5bSJoe Perches				    s/\s+\]/\]/;
47623705ce5bSJoe Perches			}
476322f2a2efSAndy Whitcroft		}
476422f2a2efSAndy Whitcroft
4765c45dcabdSAndy Whitcroft# check spacing on parentheses
47669c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
47679c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
47683705ce5bSJoe Perches			if (ERROR("SPACING",
47693705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
47703705ce5bSJoe Perches			    $fix) {
4771194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47723705ce5bSJoe Perches				    s/\(\s+/\(/;
47733705ce5bSJoe Perches			}
477422f2a2efSAndy Whitcroft		}
477513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4776c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4777c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
47783705ce5bSJoe Perches			if (ERROR("SPACING",
47793705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
47803705ce5bSJoe Perches			    $fix) {
4781194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47823705ce5bSJoe Perches				    s/\s+\)/\)/;
47833705ce5bSJoe Perches			}
478422f2a2efSAndy Whitcroft		}
478522f2a2efSAndy Whitcroft
4786e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4787e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4788e2826fd0SJoe Perches
4789e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4790ea4acbb1SJoe Perches			my $var = $1;
4791ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4792ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4793ea4acbb1SJoe Perches			    $fix) {
4794ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4795ea4acbb1SJoe Perches			}
4796ea4acbb1SJoe Perches		}
4797ea4acbb1SJoe Perches
4798ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4799ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4800ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4801ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4802ea4acbb1SJoe Perches			my $var = $2;
4803ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4804ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4805ea4acbb1SJoe Perches			    $fix) {
4806ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4807ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4808ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4809ea4acbb1SJoe Perches			}
4810e2826fd0SJoe Perches		}
4811e2826fd0SJoe Perches
481263b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4813a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4814a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
48155b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
481663b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
481763b7c73eSJoe Perches			my $if_stat = $1;
481863b7c73eSJoe Perches			my $test = substr($2, 1, -1);
481963b7c73eSJoe Perches			my $herectx;
482063b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
482163b7c73eSJoe Perches				my $match = $1;
482263b7c73eSJoe Perches				# avoid parentheses around potential macro args
482363b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
482463b7c73eSJoe Perches				if (!defined($herectx)) {
482563b7c73eSJoe Perches					$herectx = $here . "\n";
482663b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
482763b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
482863b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
482963b7c73eSJoe Perches						$herectx .=  $rl . "\n";
483063b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
483163b7c73eSJoe Perches					}
483263b7c73eSJoe Perches				}
483363b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
483463b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
483563b7c73eSJoe Perches			}
483663b7c73eSJoe Perches		}
483763b7c73eSJoe Perches
48380a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
48394a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
48400a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
48413705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
48423705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
48433705ce5bSJoe Perches			    $fix) {
4844194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48453705ce5bSJoe Perches				    s/^(.)\s+/$1/;
48463705ce5bSJoe Perches			}
48470a920b5bSAndy Whitcroft		}
48480a920b5bSAndy Whitcroft
48495b9553abSJoe Perches# return is not a function
4850507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4851c45dcabdSAndy Whitcroft			my $spacing = $1;
48525b57980dSJoe Perches			if ($perl_version_ok &&
48535b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
48545b9553abSJoe Perches				my $value = $1;
48555b9553abSJoe Perches				$value = deparenthesize($value);
48565b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4857000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4858000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
48595b9553abSJoe Perches				}
4860c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4861000d1cc1SJoe Perches				ERROR("SPACING",
4862000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4863c45dcabdSAndy Whitcroft			}
4864c45dcabdSAndy Whitcroft		}
4865507e5141SJoe Perches
4866b43ae21bSJoe Perches# unnecessary return in a void function
4867b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4868b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4869b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4870b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4871b43ae21bSJoe Perches		    $linenr >= 3 &&
4872b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4873b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
48749819cf25SJoe Perches			WARN("RETURN_VOID",
4875b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
48769819cf25SJoe Perches               }
48779819cf25SJoe Perches
4878189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
48795b57980dSJoe Perches		if ($perl_version_ok &&
4880189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4881189248d8SJoe Perches			my $openparens = $1;
4882189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4883189248d8SJoe Perches			my $msg = "";
4884189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4885189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4886189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4887189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4888189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4889189248d8SJoe Perches			}
4890189248d8SJoe Perches		}
4891189248d8SJoe Perches
4892c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4893c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4894c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4895c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
48965b57980dSJoe Perches		if ($perl_version_ok &&
4897c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4898c5595fa2SJoe Perches			my $lead = $1;
4899c5595fa2SJoe Perches			my $const = $2;
4900c5595fa2SJoe Perches			my $comp = $3;
4901c5595fa2SJoe Perches			my $to = $4;
4902c5595fa2SJoe Perches			my $newcomp = $comp;
4903f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4904c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4905c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4906c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4907c5595fa2SJoe Perches			    $fix) {
4908c5595fa2SJoe Perches				if ($comp eq "<") {
4909c5595fa2SJoe Perches					$newcomp = ">";
4910c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4911c5595fa2SJoe Perches					$newcomp = ">=";
4912c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4913c5595fa2SJoe Perches					$newcomp = "<";
4914c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4915c5595fa2SJoe Perches					$newcomp = "<=";
4916c5595fa2SJoe Perches				}
4917c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4918c5595fa2SJoe Perches			}
4919c5595fa2SJoe Perches		}
4920c5595fa2SJoe Perches
4921f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4922f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
492353a3c448SAndy Whitcroft			my $name = $1;
492453a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4925000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4926f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
492753a3c448SAndy Whitcroft			}
492853a3c448SAndy Whitcroft		}
4929c45dcabdSAndy Whitcroft
49300a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
49314a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
49323705ce5bSJoe Perches			if (ERROR("SPACING",
49333705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
49343705ce5bSJoe Perches			    $fix) {
4935194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49363705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
49373705ce5bSJoe Perches			}
49380a920b5bSAndy Whitcroft		}
49390a920b5bSAndy Whitcroft
4940f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4941f5fe35ddSAndy Whitcroft# statements after the conditional.
4942170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
49433e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
49443e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
49453e469cdcSAndy Whitcroft					if (!defined $stat);
4946170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4947170d3a22SAndy Whitcroft						$remain_next, $off_next);
4948170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4949170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4950170d3a22SAndy Whitcroft
4951170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4952170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4953170d3a22SAndy Whitcroft				# then count those as offsets.
4954170d3a22SAndy Whitcroft				my ($whitespace) =
4955170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4956170d3a22SAndy Whitcroft				my $offset =
4957170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4958170d3a22SAndy Whitcroft
4959170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4960170d3a22SAndy Whitcroft								$offset} = 1;
4961170d3a22SAndy Whitcroft			}
4962170d3a22SAndy Whitcroft		}
4963170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4964c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4965170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4966171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
49678905a67cSAndy Whitcroft
4968b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4969000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4970000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
49718905a67cSAndy Whitcroft			}
49728905a67cSAndy Whitcroft
49738905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
49748905a67cSAndy Whitcroft			# conditional.
4975773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
49768905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
497713214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
497853210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
497953210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4980773647a0SAndy Whitcroft			{
4981bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4982bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4983bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
498442bdf74cSHidetoshi Seto				my $stat_real = '';
4985bb44ad39SAndy Whitcroft
498642bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
498742bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4988bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4989bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4990bb44ad39SAndy Whitcroft				}
4991bb44ad39SAndy Whitcroft
4992000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4993000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
49948905a67cSAndy Whitcroft			}
49958905a67cSAndy Whitcroft		}
49968905a67cSAndy Whitcroft
499713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
499813214adfSAndy Whitcroft		if ($line =~ /
499913214adfSAndy Whitcroft			(?:
500013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
500113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
500213214adfSAndy Whitcroft				(?:\&\&|\|\|)
500313214adfSAndy Whitcroft			|
500413214adfSAndy Whitcroft				(?:\&\&|\|\|)
500513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
500613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
500713214adfSAndy Whitcroft			)/x)
500813214adfSAndy Whitcroft		{
5009000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5010000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
501113214adfSAndy Whitcroft		}
501213214adfSAndy Whitcroft
50138905a67cSAndy Whitcroft# if and else should not have general statements after it
501413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
501513214adfSAndy Whitcroft			my $s = $1;
501613214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
501713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5018000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5019000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
50200a920b5bSAndy Whitcroft			}
502113214adfSAndy Whitcroft		}
502239667782SAndy Whitcroft# if should not continue a brace
502339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5024000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5025048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
502639667782SAndy Whitcroft				$herecurr);
502739667782SAndy Whitcroft		}
5028a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5029a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5030a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
50313fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5032a1080bf8SAndy Whitcroft			\s*return\s+
5033a1080bf8SAndy Whitcroft		    )/xg)
5034a1080bf8SAndy Whitcroft		{
5035000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5036000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5037a1080bf8SAndy Whitcroft		}
50380a920b5bSAndy Whitcroft
50390a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
50400a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
50418b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
50420a920b5bSAndy Whitcroft		    $previndent == $indent) {
50438b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
50448b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
50458b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50468b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
50478b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
50488b8856f4SJoe Perches				my $fixedline = $prevrawline;
50498b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
50508b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
50518b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50528b8856f4SJoe Perches				}
50538b8856f4SJoe Perches				$fixedline = $rawline;
50548b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
50558b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
50568b8856f4SJoe Perches			}
50570a920b5bSAndy Whitcroft		}
50580a920b5bSAndy Whitcroft
50598b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5060c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5061c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5062c2fdda0dSAndy Whitcroft
5063c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5064c2fdda0dSAndy Whitcroft			# conditional.
5065773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5066c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5067c2fdda0dSAndy Whitcroft
5068c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
50698b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
50708b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
50718b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50728b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
50738b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
50748b8856f4SJoe Perches					my $fixedline = $prevrawline;
50758b8856f4SJoe Perches					my $trailing = $rawline;
50768b8856f4SJoe Perches					$trailing =~ s/^\+//;
50778b8856f4SJoe Perches					$trailing = trim($trailing);
50788b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
50798b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50808b8856f4SJoe Perches				}
5081c2fdda0dSAndy Whitcroft			}
5082c2fdda0dSAndy Whitcroft		}
5083c2fdda0dSAndy Whitcroft
508495e2c602SJoe Perches#Specific variable tests
5085323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5086323c1260SJoe Perches			my $var = $1;
508795e2c602SJoe Perches
508895e2c602SJoe Perches#CamelCase
5089807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5090be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
509122735ce8SJoe Perches#Ignore Page<foo> variants
5092807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5093d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5094d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5095d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5096f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5097f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
50987e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
50997e781f67SJoe Perches					my $word = $1;
51007e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5101d8b07710SJoe Perches					if ($check) {
5102d8b07710SJoe Perches						seed_camelcase_includes();
5103d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5104d8b07710SJoe Perches							seed_camelcase_file($realfile);
5105d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5106d8b07710SJoe Perches						}
5107d8b07710SJoe Perches					}
51087e781f67SJoe Perches					if (!defined $camelcase{$word}) {
51097e781f67SJoe Perches						$camelcase{$word} = 1;
5110be79794bSJoe Perches						CHK("CAMELCASE",
51117e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
51127e781f67SJoe Perches					}
5113323c1260SJoe Perches				}
5114323c1260SJoe Perches			}
51153445686aSJoe Perches		}
51160a920b5bSAndy Whitcroft
51170a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5118d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5119d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5120d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5121d5e616fcSJoe Perches			    $fix) {
5122194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5123d5e616fcSJoe Perches			}
51240a920b5bSAndy Whitcroft		}
51250a920b5bSAndy Whitcroft
51260e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
51270e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5128c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5129e09dec48SAndy Whitcroft			my $file = "$1.h";
5130e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5131e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5132e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
51337840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5134c45dcabdSAndy Whitcroft			{
51350e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
51360e212e0aSFabian Frederick				if ($asminclude > 0) {
5137e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5138000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5139000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5140e09dec48SAndy Whitcroft					} else {
5141000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5142000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5143e09dec48SAndy Whitcroft					}
51440a920b5bSAndy Whitcroft				}
51450a920b5bSAndy Whitcroft			}
51460e212e0aSFabian Frederick		}
51470a920b5bSAndy Whitcroft
5148653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5149653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5150cf655043SAndy Whitcroft# in a known good container
5151b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5152b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5153d8aaf121SAndy Whitcroft			my $ln = $linenr;
5154d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5155c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5156c45dcabdSAndy Whitcroft			my $ctx = '';
515708a2843eSJoe Perches			my $has_flow_statement = 0;
515808a2843eSJoe Perches			my $has_arg_concat = 0;
5159c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5160f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5161f74bd194SAndy Whitcroft			$ctx = $dstat;
5162c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5163a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5164c45dcabdSAndy Whitcroft
516508a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
516662e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
516708a2843eSJoe Perches
5168f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5169f59b64bfSJoe Perches			my $define_args = $1;
5170f59b64bfSJoe Perches			my $define_stmt = $dstat;
5171f59b64bfSJoe Perches			my @def_args = ();
5172f59b64bfSJoe Perches
5173f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5174f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5175f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
51768c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5177f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5178f59b64bfSJoe Perches			}
5179f59b64bfSJoe Perches
5180292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5181c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5182c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5183c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5184c45dcabdSAndy Whitcroft
5185c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5186bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5187bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
51886b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5189bf30d6edSAndy Whitcroft			{
5190c45dcabdSAndy Whitcroft			}
5191c45dcabdSAndy Whitcroft
5192342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
519333acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
519433acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5195e45bab8eSAndy Whitcroft			{
5196e45bab8eSAndy Whitcroft			}
5197e45bab8eSAndy Whitcroft
519842e15293SJoe Perches			# Make asm volatile uses seem like a generic function
519942e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
520042e15293SJoe Perches
5201c45dcabdSAndy Whitcroft			my $exceptions = qr{
5202c45dcabdSAndy Whitcroft				$Declare|
5203c45dcabdSAndy Whitcroft				module_param_named|
5204a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5205c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5206c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5207383099fdSAndy Whitcroft				__typeof__\(|
520822fd2d3eSStefani Seibold				union|
520922fd2d3eSStefani Seibold				struct|
5210ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
52116b10df42SVladimir Zapolskiy				^\"|\"$|
52126b10df42SVladimir Zapolskiy				^\[
5213c45dcabdSAndy Whitcroft			}x;
52145eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5215f59b64bfSJoe Perches
5216f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5217f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5218e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5219f59b64bfSJoe Perches
5220f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5221f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5222f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
52233cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5224356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5225f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5226f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5227e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
522872f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5229f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5230f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5231f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
52324e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5233f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5234c45dcabdSAndy Whitcroft			{
5235e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5236e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5237e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5238e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5239f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5240f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5241f74bd194SAndy Whitcroft				} else {
5242000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5243388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5244d8aaf121SAndy Whitcroft				}
5245f59b64bfSJoe Perches
5246f59b64bfSJoe Perches			}
52475207649bSJoe Perches
52485207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
52495207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
52505207649bSJoe Perches			my $first = 1;
52515207649bSJoe Perches			$define_stmt = "";
52525207649bSJoe Perches			foreach my $l (@stmt_array) {
52535207649bSJoe Perches				$l =~ s/\\$//;
52545207649bSJoe Perches				if ($first) {
52555207649bSJoe Perches					$define_stmt = $l;
52565207649bSJoe Perches					$first = 0;
52575207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
52585207649bSJoe Perches					$define_stmt .= substr($l, 1);
52595207649bSJoe Perches				}
52605207649bSJoe Perches			}
52615207649bSJoe Perches			$define_stmt =~ s/$;//g;
52625207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
52635207649bSJoe Perches			$define_stmt = trim($define_stmt);
52645207649bSJoe Perches
5265f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5266f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5267f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
52689192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
52697fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
52706dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
52717fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
52727fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5273d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5274f59b64bfSJoe Perches				if ($use_cnt > 1) {
5275f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5276f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5277f59b64bfSJoe Perches				    }
52789192d41aSJoe Perches# check if any macro arguments may have other precedence issues
52797fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
52809192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
52819192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
52829192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
52839192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
52849192d41aSJoe Perches				}
52850a920b5bSAndy Whitcroft			}
52865023d347SJoe Perches
528708a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
528808a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
528908a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
529008a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5291e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
529208a2843eSJoe Perches
529308a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
529408a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
529508a2843eSJoe Perches			}
529608a2843eSJoe Perches
5297481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
52985023d347SJoe Perches
52995023d347SJoe Perches		} else {
53005023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5301481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5302481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
53035023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
53045023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
53055023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
53065023d347SJoe Perches			}
5307653d4876SAndy Whitcroft		}
53080a920b5bSAndy Whitcroft
5309b13edf7fSJoe Perches# do {} while (0) macro tests:
5310b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5311b13edf7fSJoe Perches# macro should not end with a semicolon
53125b57980dSJoe Perches		if ($perl_version_ok &&
5313b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5314b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5315b13edf7fSJoe Perches			my $ln = $linenr;
5316b13edf7fSJoe Perches			my $cnt = $realcnt;
5317b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5318b13edf7fSJoe Perches			my $ctx = '';
5319b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5320b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5321b13edf7fSJoe Perches			$ctx = $dstat;
5322b13edf7fSJoe Perches
5323b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
53241b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5325b13edf7fSJoe Perches
5326b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5327b13edf7fSJoe Perches				my $stmts = $2;
5328b13edf7fSJoe Perches				my $semis = $3;
5329b13edf7fSJoe Perches
5330b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5331b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5332e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5333b13edf7fSJoe Perches
5334ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5335ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5336b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5337b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5338b13edf7fSJoe Perches				}
5339b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5340b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5341b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5342b13edf7fSJoe Perches				}
5343f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5344f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5345f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5346e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5347f5ef95b1SJoe Perches
5348f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5349f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5350b13edf7fSJoe Perches			}
5351b13edf7fSJoe Perches		}
5352b13edf7fSJoe Perches
5353f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
535413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
535513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5356cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
535713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5358cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5359cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5360aad4f614SJoe Perches				my @allowed = ();
5361aad4f614SJoe Perches				my $allow = 0;
536213214adfSAndy Whitcroft				my $seen = 0;
5363773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5364cf655043SAndy Whitcroft				my $ln = $linenr - 1;
536513214adfSAndy Whitcroft				for my $chunk (@chunks) {
536613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
536713214adfSAndy Whitcroft
5368773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5369773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5370773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5371773647a0SAndy Whitcroft
5372aad4f614SJoe Perches					$allowed[$allow] = 0;
5373773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5374773647a0SAndy Whitcroft
5375773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5376773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5377773647a0SAndy Whitcroft
5378773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5379cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5380cf655043SAndy Whitcroft
5381773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
538213214adfSAndy Whitcroft
538313214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
538413214adfSAndy Whitcroft
5385aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5386cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5387cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5388aad4f614SJoe Perches						$allowed[$allow] = 1;
538913214adfSAndy Whitcroft					}
539013214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5391cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5392aad4f614SJoe Perches						$allowed[$allow] = 1;
539313214adfSAndy Whitcroft					}
5394cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5395cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5396aad4f614SJoe Perches						$allowed[$allow] = 1;
539713214adfSAndy Whitcroft					}
5398aad4f614SJoe Perches					$allow++;
539913214adfSAndy Whitcroft				}
5400aad4f614SJoe Perches				if ($seen) {
5401aad4f614SJoe Perches					my $sum_allowed = 0;
5402aad4f614SJoe Perches					foreach (@allowed) {
5403aad4f614SJoe Perches						$sum_allowed += $_;
5404aad4f614SJoe Perches					}
5405aad4f614SJoe Perches					if ($sum_allowed == 0) {
5406000d1cc1SJoe Perches						WARN("BRACES",
5407000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5408aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5409aad4f614SJoe Perches						 $seen != $allow) {
5410aad4f614SJoe Perches						CHK("BRACES",
5411aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5412aad4f614SJoe Perches					}
541313214adfSAndy Whitcroft				}
541413214adfSAndy Whitcroft			}
541513214adfSAndy Whitcroft		}
5416773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
541713214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5418cf655043SAndy Whitcroft			my $allowed = 0;
5419f0a594c1SAndy Whitcroft
5420cf655043SAndy Whitcroft			# Check the pre-context.
5421cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5422cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5423cf655043SAndy Whitcroft				$allowed = 1;
5424f0a594c1SAndy Whitcroft			}
5425773647a0SAndy Whitcroft
5426773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5427773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5428773647a0SAndy Whitcroft
5429cf655043SAndy Whitcroft			# Check the condition.
5430cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5431773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5432cf655043SAndy Whitcroft			if (defined $cond) {
5433773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5434cf655043SAndy Whitcroft			}
5435cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5436cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5437cf655043SAndy Whitcroft				$allowed = 1;
5438cf655043SAndy Whitcroft			}
5439cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5440cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5441cf655043SAndy Whitcroft				$allowed = 1;
5442cf655043SAndy Whitcroft			}
5443cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5444cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5445cf655043SAndy Whitcroft				$allowed = 1;
5446cf655043SAndy Whitcroft			}
5447cf655043SAndy Whitcroft			# Check the post-context.
5448cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5449cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5450cf655043SAndy Whitcroft				if (defined $cond) {
5451773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5452cf655043SAndy Whitcroft				}
5453cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5454cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5455cf655043SAndy Whitcroft					$allowed = 1;
5456cf655043SAndy Whitcroft				}
5457cf655043SAndy Whitcroft			}
5458cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5459f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5460e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5461cf655043SAndy Whitcroft
5462000d1cc1SJoe Perches				WARN("BRACES",
5463000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5464f0a594c1SAndy Whitcroft			}
5465f0a594c1SAndy Whitcroft		}
5466f0a594c1SAndy Whitcroft
5467e4c5babdSJoe Perches# check for single line unbalanced braces
546895330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
546995330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5470e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5471e4c5babdSJoe Perches		}
5472e4c5babdSJoe Perches
54730979ae66SJoe Perches# check for unnecessary blank lines around braces
547477b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5475f8e58219SJoe Perches			if (CHK("BRACES",
5476f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5477f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5478f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5479f8e58219SJoe Perches			}
54800979ae66SJoe Perches		}
548177b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5482f8e58219SJoe Perches			if (CHK("BRACES",
5483f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5484f8e58219SJoe Perches			    $fix) {
5485f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5486f8e58219SJoe Perches			}
54870979ae66SJoe Perches		}
54880979ae66SJoe Perches
54894a0df2efSAndy Whitcroft# no volatiles please
54906c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
54916c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5492000d1cc1SJoe Perches			WARN("VOLATILE",
54938c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
54944a0df2efSAndy Whitcroft		}
54954a0df2efSAndy Whitcroft
54965e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
54975e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
54985e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
54995e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
550033acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
55015e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
55025e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
55035e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
55045e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
55055e4f6ba5SJoe Perches				     $fix &&
55065e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
55075e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
55085e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
55095e4f6ba5SJoe Perches				my $comma_close = "";
55105e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
55115e4f6ba5SJoe Perches					$comma_close = $1;
55125e4f6ba5SJoe Perches				}
55135e4f6ba5SJoe Perches
55145e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
55155e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
55165e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
55175e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
55185e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
55195e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
55205e4f6ba5SJoe Perches				$fixedline = $rawline;
55215e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
55225e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
55235e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
55245e4f6ba5SJoe Perches				}
55255e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
55265e4f6ba5SJoe Perches			}
55275e4f6ba5SJoe Perches		}
55285e4f6ba5SJoe Perches
55295e4f6ba5SJoe Perches# check for missing a space in a string concatenation
55305e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
55315e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
55325e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
55335e4f6ba5SJoe Perches		}
55345e4f6ba5SJoe Perches
553577cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5536e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5537e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5538e4b7d309SJoe Perches# function declarations
553977cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
554077cb8546SJoe Perches		    defined($context_function) &&
5541e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5542e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
554377cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5544e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
554577cb8546SJoe Perches		}
554677cb8546SJoe Perches
55475e4f6ba5SJoe Perches# check for spaces before a quoted newline
55485e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
55495e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
55505e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
55515e4f6ba5SJoe Perches			    $fix) {
55525e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
55535e4f6ba5SJoe Perches			}
55545e4f6ba5SJoe Perches
55555e4f6ba5SJoe Perches		}
55565e4f6ba5SJoe Perches
5557f17dba4fSJoe Perches# concatenated string without spaces between elements
555879682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
555979682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
556079682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
556179682c0cSJoe Perches			    $fix) {
556279682c0cSJoe Perches				while ($line =~ /($String)/g) {
556379682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
556479682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
556579682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
556679682c0cSJoe Perches				}
556779682c0cSJoe Perches			}
5568f17dba4fSJoe Perches		}
5569f17dba4fSJoe Perches
557090ad30e5SJoe Perches# uncoalesced string fragments
557133acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
557279682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
557379682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
557479682c0cSJoe Perches			    $fix) {
557579682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
557679682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
557779682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
557879682c0cSJoe Perches				}
557979682c0cSJoe Perches			}
558090ad30e5SJoe Perches		}
558190ad30e5SJoe Perches
5582522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5583522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5584522b837cSAlexey Dobriyan		my $show_Z = 1;
55855e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5586522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
55875e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5588522b837cSAlexey Dobriyan			# check for %L
5589522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
55905e4f6ba5SJoe Perches				WARN("PRINTF_L",
5591522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5592522b837cSAlexey Dobriyan				$show_L = 0;
55935e4f6ba5SJoe Perches			}
5594522b837cSAlexey Dobriyan			# check for %Z
5595522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5596522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5597522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5598522b837cSAlexey Dobriyan				$show_Z = 0;
5599522b837cSAlexey Dobriyan			}
5600522b837cSAlexey Dobriyan			# check for 0x<decimal>
5601522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5602522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
56036e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
56046e300757SJoe Perches			}
56055e4f6ba5SJoe Perches		}
56065e4f6ba5SJoe Perches
56075e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
56083f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
56095e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
56105e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
56115e4f6ba5SJoe Perches		}
56125e4f6ba5SJoe Perches
561300df344fSAndy Whitcroft# warn about #if 0
5614c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
561560f89010SPrakruthi Deepak Heragu			WARN("IF_0",
561660f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
561760f89010SPrakruthi Deepak Heragu		}
561860f89010SPrakruthi Deepak Heragu
561960f89010SPrakruthi Deepak Heragu# warn about #if 1
562060f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
562160f89010SPrakruthi Deepak Heragu			WARN("IF_1",
562260f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
56234a0df2efSAndy Whitcroft		}
56244a0df2efSAndy Whitcroft
562503df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
562603df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5627100425deSJoe Perches			my $tested = quotemeta($1);
5628100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5629100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5630100425deSJoe Perches				my $func = $1;
5631100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5632100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5633100425deSJoe Perches				    $fix) {
5634100425deSJoe Perches					my $do_fix = 1;
5635100425deSJoe Perches					my $leading_tabs = "";
5636100425deSJoe Perches					my $new_leading_tabs = "";
5637100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5638100425deSJoe Perches						$leading_tabs = $1;
5639100425deSJoe Perches					} else {
5640100425deSJoe Perches						$do_fix = 0;
5641100425deSJoe Perches					}
5642100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5643100425deSJoe Perches						$new_leading_tabs = $1;
5644100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5645100425deSJoe Perches							$do_fix = 0;
5646100425deSJoe Perches						}
5647100425deSJoe Perches					} else {
5648100425deSJoe Perches						$do_fix = 0;
5649100425deSJoe Perches					}
5650100425deSJoe Perches					if ($do_fix) {
5651100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5652100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5653100425deSJoe Perches					}
5654100425deSJoe Perches				}
56554c432a8fSGreg Kroah-Hartman			}
56564c432a8fSGreg Kroah-Hartman		}
5657f0a594c1SAndy Whitcroft
5658ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5659ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5660ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5661ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5662ebfdc409SJoe Perches		    $linenr > 3) {
5663ebfdc409SJoe Perches			my $testval = $2;
5664ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5665ebfdc409SJoe Perches
5666ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5667ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5668ebfdc409SJoe Perches
5669e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5670e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5671ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5672ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5673ebfdc409SJoe Perches			}
5674ebfdc409SJoe Perches		}
5675ebfdc409SJoe Perches
5676f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5677dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5678f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5679f78d98f6SJoe Perches			my $level = $1;
5680f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5681f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5682f78d98f6SJoe Perches			    $fix) {
5683f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5684f78d98f6SJoe Perches			}
5685f78d98f6SJoe Perches		}
5686f78d98f6SJoe Perches
568745c55e92SJoe Perches# check for logging continuations
568845c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
568945c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
569045c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
569145c55e92SJoe Perches		}
569245c55e92SJoe Perches
5693abb08a53SJoe Perches# check for mask then right shift without a parentheses
56945b57980dSJoe Perches		if ($perl_version_ok &&
5695abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5696abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5697abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5698abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5699abb08a53SJoe Perches		}
5700abb08a53SJoe Perches
5701b75ac618SJoe Perches# check for pointer comparisons to NULL
57025b57980dSJoe Perches		if ($perl_version_ok) {
5703b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5704b75ac618SJoe Perches				my $val = $1;
5705b75ac618SJoe Perches				my $equal = "!";
5706b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5707b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5708b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5709b75ac618SJoe Perches					    $fix) {
5710b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5711b75ac618SJoe Perches				}
5712b75ac618SJoe Perches			}
5713b75ac618SJoe Perches		}
5714b75ac618SJoe Perches
57158716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
57168716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
57178716de38SJoe Perches			my $attr = $1;
57188716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
57198716de38SJoe Perches				my $ptr = $1;
57208716de38SJoe Perches				my $var = $2;
57218716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
57228716de38SJoe Perches				      ERROR("MISPLACED_INIT",
57238716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
57248716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
57258716de38SJoe Perches				      WARN("MISPLACED_INIT",
57268716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
57278716de38SJoe Perches				    $fix) {
5728194f66fcSJoe 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;
57298716de38SJoe Perches				}
57308716de38SJoe Perches			}
57318716de38SJoe Perches		}
57328716de38SJoe Perches
5733e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5734e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5735e970b884SJoe Perches			my $attr = $1;
5736e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5737e970b884SJoe Perches			my $attr_prefix = $1;
5738e970b884SJoe Perches			my $attr_type = $2;
5739e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5740e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5741e970b884SJoe Perches			    $fix) {
5742194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5743e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5744e970b884SJoe Perches			}
5745e970b884SJoe Perches		}
5746e970b884SJoe Perches
5747e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5748e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5749e970b884SJoe Perches			my $attr = $1;
5750e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5751e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5752e970b884SJoe Perches			    $fix) {
5753194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5754e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5755e970b884SJoe Perches				$lead = rtrim($1);
5756e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5757e970b884SJoe Perches				$lead = "${lead}const ";
5758194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5759e970b884SJoe Perches			}
5760e970b884SJoe Perches		}
5761e970b884SJoe Perches
5762c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5763c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5764c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5765c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5766c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5767c17893c7SJoe Perches			    $fix) {
5768c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5769c17893c7SJoe Perches			}
5770c17893c7SJoe Perches		}
5771c17893c7SJoe Perches
5772fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5773fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5774fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5775fbdb8138SJoe Perches			my $constant_func = $1;
5776fbdb8138SJoe Perches			my $func = $constant_func;
5777fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5778fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5779fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5780fbdb8138SJoe Perches			    $fix) {
5781194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5782fbdb8138SJoe Perches			}
5783fbdb8138SJoe Perches		}
5784fbdb8138SJoe Perches
57851a15a250SPatrick Pannuto# prefer usleep_range over udelay
578637581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
578743c1d77cSJoe Perches			my $delay = $1;
57881a15a250SPatrick Pannuto			# ignore udelay's < 10, however
578943c1d77cSJoe Perches			if (! ($delay < 10) ) {
5790000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5791458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
579243c1d77cSJoe Perches			}
579343c1d77cSJoe Perches			if ($delay > 2000) {
579443c1d77cSJoe Perches				WARN("LONG_UDELAY",
579543c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
57961a15a250SPatrick Pannuto			}
57971a15a250SPatrick Pannuto		}
57981a15a250SPatrick Pannuto
579909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
580009ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
580109ef8725SPatrick Pannuto			if ($1 < 20) {
5802000d1cc1SJoe Perches				WARN("MSLEEP",
5803458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
580409ef8725SPatrick Pannuto			}
580509ef8725SPatrick Pannuto		}
580609ef8725SPatrick Pannuto
580736ec1939SJoe Perches# check for comparisons of jiffies
580836ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
580936ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
581036ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
581136ec1939SJoe Perches		}
581236ec1939SJoe Perches
58139d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
58149d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
58159d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
58169d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
58179d7a34a5SJoe Perches		}
58189d7a34a5SJoe Perches
581900df344fSAndy Whitcroft# warn about #ifdefs in C files
5820c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
582100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
582200df344fSAndy Whitcroft#			print "$herecurr";
582300df344fSAndy Whitcroft#			$clean = 0;
582400df344fSAndy Whitcroft#		}
582500df344fSAndy Whitcroft
582622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5827c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
58283705ce5bSJoe Perches			if (ERROR("SPACING",
58293705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
58303705ce5bSJoe Perches			    $fix) {
5831194f66fcSJoe Perches				$fixed[$fixlinenr] =~
58323705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
58333705ce5bSJoe Perches			}
58343705ce5bSJoe Perches
583522f2a2efSAndy Whitcroft		}
583622f2a2efSAndy Whitcroft
58374a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5838171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5839171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
58404a0df2efSAndy Whitcroft			my $which = $1;
58414a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5842000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5843000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
58444a0df2efSAndy Whitcroft			}
58454a0df2efSAndy Whitcroft		}
58464a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5847402c2553SMichael S. Tsirkin
5848402c2553SMichael S. Tsirkin		my $barriers = qr{
5849402c2553SMichael S. Tsirkin			mb|
5850402c2553SMichael S. Tsirkin			rmb|
5851402c2553SMichael S. Tsirkin			wmb|
5852402c2553SMichael S. Tsirkin			read_barrier_depends
5853402c2553SMichael S. Tsirkin		}x;
5854402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5855402c2553SMichael S. Tsirkin			mb__before_atomic|
5856402c2553SMichael S. Tsirkin			mb__after_atomic|
5857402c2553SMichael S. Tsirkin			store_release|
5858402c2553SMichael S. Tsirkin			load_acquire|
5859402c2553SMichael S. Tsirkin			store_mb|
5860402c2553SMichael S. Tsirkin			(?:$barriers)
5861402c2553SMichael S. Tsirkin		}x;
5862402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5863402c2553SMichael S. Tsirkin			(?:$barriers)|
586443e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
586543e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5866402c2553SMichael S. Tsirkin		}x;
5867402c2553SMichael S. Tsirkin
5868402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
58694a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5870c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5871000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
58724a0df2efSAndy Whitcroft			}
58734a0df2efSAndy Whitcroft		}
58743ad81779SPaul E. McKenney
5875f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5876f4073b0fSMichael S. Tsirkin
5877f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5878f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5879f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5880f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5881f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5882f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5883f4073b0fSMichael S. Tsirkin		}
5884f4073b0fSMichael S. Tsirkin
5885cb426e99SJoe Perches# check for waitqueue_active without a comment.
5886cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5887cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5888cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5889cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5890cb426e99SJoe Perches			}
5891cb426e99SJoe Perches		}
58923ad81779SPaul E. McKenney
589391db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
589491db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
589591db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
589691db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
589791db2592SPaul E. McKenney		}
589891db2592SPaul E. McKenney
58994a0df2efSAndy Whitcroft# check of hardware specific defines
5900c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5901000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5902000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
59030a920b5bSAndy Whitcroft		}
5904653d4876SAndy Whitcroft
5905596ed45bSJoe Perches# check that the storage class is not after a type
5906596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5907000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5908596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5909596ed45bSJoe Perches		}
5910596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5911596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5912596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5913596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5914596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5915596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5916596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5917d4977c78STobias Klauser		}
5918d4977c78STobias Klauser
5919de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5920de7d4f0eSAndy Whitcroft# storage class and type.
59219c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
59229c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5923000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5924000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5925de7d4f0eSAndy Whitcroft		}
5926de7d4f0eSAndy Whitcroft
59278905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
59282b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59292b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5930d5e616fcSJoe Perches			if (WARN("INLINE",
5931d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5932d5e616fcSJoe Perches			    $fix) {
5933194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5934d5e616fcSJoe Perches
5935d5e616fcSJoe Perches			}
59368905a67cSAndy Whitcroft		}
59378905a67cSAndy Whitcroft
59383d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
59392b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59402b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5941000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5942000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
59433d130fd0SJoe Perches		}
59443d130fd0SJoe Perches
594539b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
59462b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59472b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5948000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5949000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
595039b7e287SJoe Perches		}
595139b7e287SJoe Perches
5952462811d9SJoe Perches# Check for __attribute__ section, prefer __section
5953462811d9SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5954462811d9SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5955462811d9SJoe Perches			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5956462811d9SJoe Perches			my $new = substr($old, 1, -1);
5957462811d9SJoe Perches			if (WARN("PREFER_SECTION",
5958462811d9SJoe Perches				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5959462811d9SJoe Perches			    $fix) {
5960462811d9SJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5961462811d9SJoe Perches			}
5962462811d9SJoe Perches		}
5963462811d9SJoe Perches
59645f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
59652b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59662b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5967d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5968d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5969d5e616fcSJoe Perches			    $fix) {
5970194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5971d5e616fcSJoe Perches
5972d5e616fcSJoe Perches			}
59735f14d3bdSJoe Perches		}
59745f14d3bdSJoe Perches
59756061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
59762b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59772b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5978d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5979d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5980d5e616fcSJoe Perches			    $fix) {
5981194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5982d5e616fcSJoe Perches			}
59836061d949SJoe Perches		}
59846061d949SJoe Perches
5985619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
59865b57980dSJoe Perches		if ($perl_version_ok &&
5987619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5988619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5989619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5990619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5991619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5992619a908aSJoe Perches		}
5993619a908aSJoe Perches
5994fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5995e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5996fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5997e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5998e6176fa4SJoe Perches			my $type = $1;
5999e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6000e6176fa4SJoe Perches				$type = $1;
6001e6176fa4SJoe Perches				my $kernel_type = 'u';
6002e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6003e6176fa4SJoe Perches				$type =~ /(\d+)/;
6004e6176fa4SJoe Perches				$kernel_type .= $1;
6005e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6006e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6007e6176fa4SJoe Perches				    $fix) {
6008e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6009e6176fa4SJoe Perches				}
6010e6176fa4SJoe Perches			}
6011e6176fa4SJoe Perches		}
6012e6176fa4SJoe Perches
6013938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6014938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6015938224b5SJoe Perches			my $cast = $1;
6016938224b5SJoe Perches			my $const = $2;
6017938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6018938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6019938224b5SJoe Perches			    $fix) {
6020938224b5SJoe Perches				my $suffix = "";
6021938224b5SJoe Perches				my $newconst = $const;
6022938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6023938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6024938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6025938224b5SJoe Perches					$suffix .= 'LL';
6026938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6027938224b5SJoe Perches					$suffix .= 'L';
6028938224b5SJoe Perches				}
6029938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6030938224b5SJoe Perches			}
6031938224b5SJoe Perches		}
6032938224b5SJoe Perches
60338f53a9b8SJoe Perches# check for sizeof(&)
60348f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6035000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6036000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
60378f53a9b8SJoe Perches		}
60388f53a9b8SJoe Perches
603966c80b60SJoe Perches# check for sizeof without parenthesis
604066c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6041d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6042d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6043d5e616fcSJoe Perches			    $fix) {
6044194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6045d5e616fcSJoe Perches			}
604666c80b60SJoe Perches		}
604766c80b60SJoe Perches
604888982feaSJoe Perches# check for struct spinlock declarations
604988982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
605088982feaSJoe Perches			WARN("USE_SPINLOCK_T",
605188982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
605288982feaSJoe Perches		}
605388982feaSJoe Perches
6054a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
605506668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6056a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6057caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6058caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6059d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6060d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6061d5e616fcSJoe Perches				    $fix) {
6062194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6063d5e616fcSJoe Perches				}
6064a6962d72SJoe Perches			}
6065a6962d72SJoe Perches		}
6066a6962d72SJoe Perches
60670b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
60685b57980dSJoe Perches		if ($perl_version_ok &&
60690b523769SJoe Perches		    defined $stat &&
60700b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
60710b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6072e3c6bc95STobin C. Harding			my $stat_real;
6073e3c6bc95STobin C. Harding
60740b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
60750b523769SJoe Perches			$lc = $lc + $linenr;
60760b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6077ffe07513SJoe Perches				my $specifier;
6078ffe07513SJoe Perches				my $extension;
60793bd32d6aSSakari Ailus				my $qualifier;
6080ffe07513SJoe Perches				my $bad_specifier = "";
60810b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
60820b523769SJoe Perches				$fmt =~ s/%%//g;
6083e3c6bc95STobin C. Harding
60843bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6085e3c6bc95STobin C. Harding					$specifier = $1;
6086e3c6bc95STobin C. Harding					$extension = $2;
60873bd32d6aSSakari Ailus					$qualifier = $3;
6088361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
60893bd32d6aSSakari Ailus					    ($extension eq "f" &&
60903bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6091e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
60920b523769SJoe Perches						last;
60930b523769SJoe Perches					}
6094e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6095e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6096e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
60970b523769SJoe Perches						}
6098e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6099e3c6bc95STobin 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");
6100e3c6bc95STobin C. Harding					}
6101e3c6bc95STobin C. Harding				}
6102e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
61032a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
61041df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
61051df7338aSSergey Senozhatsky					my $use = "";
6106e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
61071df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6108e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
61091df7338aSSergey Senozhatsky					}
61102a9f9d85STobin C. Harding
61110b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6112e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6113e3c6bc95STobin C. Harding				}
61140b523769SJoe Perches			}
61150b523769SJoe Perches		}
61160b523769SJoe Perches
6117554e165cSAndy Whitcroft# Check for misused memsets
61185b57980dSJoe Perches		if ($perl_version_ok &&
6119d1fe9c09SJoe Perches		    defined $stat &&
61209e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6121554e165cSAndy Whitcroft
6122d7c76ba7SJoe Perches			my $ms_addr = $2;
6123d1fe9c09SJoe Perches			my $ms_val = $7;
6124d1fe9c09SJoe Perches			my $ms_size = $12;
6125d7c76ba7SJoe Perches
6126554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6127554e165cSAndy Whitcroft				ERROR("MEMSET",
6128d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6129554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6130554e165cSAndy Whitcroft				WARN("MEMSET",
6131d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6132d7c76ba7SJoe Perches			}
6133d7c76ba7SJoe Perches		}
6134d7c76ba7SJoe Perches
613598a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
61365b57980dSJoe Perches#		if ($perl_version_ok &&
6137f333195dSJoe Perches#		    defined $stat &&
6138f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6139f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6140f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6141f333195dSJoe Perches#			    $fix) {
6142f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6143f333195dSJoe Perches#			}
6144f333195dSJoe Perches#		}
614598a9bba5SJoe Perches
6146b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
61475b57980dSJoe Perches#		if ($perl_version_ok &&
6148f333195dSJoe Perches#		    defined $stat &&
6149f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6150f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6151f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6152f333195dSJoe Perches#		}
6153b6117d17SMateusz Kulikowski
61548617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
61558617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
61565b57980dSJoe Perches#		if ($perl_version_ok &&
6157f333195dSJoe Perches#		    defined $stat &&
6158f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6159f333195dSJoe Perches#
6160f333195dSJoe Perches#			my $ms_val = $7;
6161f333195dSJoe Perches#
6162f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6163f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6164f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6165f333195dSJoe Perches#				    $fix) {
6166f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6167f333195dSJoe Perches#				}
6168f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6169f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6170f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6171f333195dSJoe Perches#				    $fix) {
6172f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6173f333195dSJoe Perches#				}
6174f333195dSJoe Perches#			}
6175f333195dSJoe Perches#		}
61768617cd09SMateusz Kulikowski
6177d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
61785b57980dSJoe Perches		if ($perl_version_ok &&
6179d1fe9c09SJoe Perches		    defined $stat &&
6180d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6181d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6182d7c76ba7SJoe Perches				my $call = $1;
6183d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6184d7c76ba7SJoe Perches				my $arg1 = $3;
6185d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6186d1fe9c09SJoe Perches				my $arg2 = $8;
6187d7c76ba7SJoe Perches				my $cast;
6188d7c76ba7SJoe Perches
6189d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6190d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6191d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6192d7c76ba7SJoe Perches					$cast = $cast1;
6193d7c76ba7SJoe Perches				} else {
6194d7c76ba7SJoe Perches					$cast = $cast2;
6195d7c76ba7SJoe Perches				}
6196d7c76ba7SJoe Perches				WARN("MINMAX",
6197d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6198554e165cSAndy Whitcroft			}
6199554e165cSAndy Whitcroft		}
6200554e165cSAndy Whitcroft
62014a273195SJoe Perches# check usleep_range arguments
62025b57980dSJoe Perches		if ($perl_version_ok &&
62034a273195SJoe Perches		    defined $stat &&
62044a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
62054a273195SJoe Perches			my $min = $1;
62064a273195SJoe Perches			my $max = $7;
62074a273195SJoe Perches			if ($min eq $max) {
62084a273195SJoe Perches				WARN("USLEEP_RANGE",
6209458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
62104a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
62114a273195SJoe Perches				 $min > $max) {
62124a273195SJoe Perches				WARN("USLEEP_RANGE",
6213458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
62144a273195SJoe Perches			}
62154a273195SJoe Perches		}
62164a273195SJoe Perches
6217823b794cSJoe Perches# check for naked sscanf
62185b57980dSJoe Perches		if ($perl_version_ok &&
6219823b794cSJoe Perches		    defined $stat &&
62206c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6221823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6222823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6223823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6224823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6225823b794cSJoe Perches			$lc = $lc + $linenr;
62262a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6227823b794cSJoe Perches			WARN("NAKED_SSCANF",
6228823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6229823b794cSJoe Perches		}
6230823b794cSJoe Perches
6231afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
62325b57980dSJoe Perches		if ($perl_version_ok &&
6233afc819abSJoe Perches		    defined $stat &&
6234afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6235afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6236afc819abSJoe Perches			$lc = $lc + $linenr;
62372a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6238afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6239afc819abSJoe Perches				my $format = $6;
6240afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6241afc819abSJoe Perches				if ($count == 1 &&
6242afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6243afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6244afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6245afc819abSJoe Perches				}
6246afc819abSJoe Perches			}
6247afc819abSJoe Perches		}
6248afc819abSJoe Perches
624970dc8a48SJoe Perches# check for new externs in .h files.
625070dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
625170dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6252d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
625370dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
625470dc8a48SJoe Perches			    $fix) {
6255194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
625670dc8a48SJoe Perches			}
625770dc8a48SJoe Perches		}
625870dc8a48SJoe Perches
6259de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6260171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6261c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6262171ae1a4SAndy Whitcroft		{
6263c45dcabdSAndy Whitcroft			my $function_name = $1;
6264c45dcabdSAndy Whitcroft			my $paren_space = $2;
6265171ae1a4SAndy Whitcroft
6266171ae1a4SAndy Whitcroft			my $s = $stat;
6267171ae1a4SAndy Whitcroft			if (defined $cond) {
6268171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6269171ae1a4SAndy Whitcroft			}
6270c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6271c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6272c45dcabdSAndy Whitcroft			{
6273000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6274000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6275de7d4f0eSAndy Whitcroft			}
6276de7d4f0eSAndy Whitcroft
6277171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6278000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6279000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6280171ae1a4SAndy Whitcroft			}
62819c9ba34eSAndy Whitcroft
62829c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
62839c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
62849c9ba34eSAndy Whitcroft		{
6285000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6286000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6287171ae1a4SAndy Whitcroft		}
6288171ae1a4SAndy Whitcroft
6289a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
629016b7f3c8SJoe Perches# while avoiding uninitialized_var(x)
6291a0ad7596SJoe Perches		if (defined $stat &&
629216b7f3c8SJoe Perches		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
629316b7f3c8SJoe Perches		    (!defined($1) ||
629416b7f3c8SJoe Perches		     (defined($1) && $1 ne "uninitialized_var")) &&
629516b7f3c8SJoe Perches		     $2 ne "void") {
629616b7f3c8SJoe Perches			my $args = trim($2);
6297ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6298ca0d8929SJoe Perches				my $arg = trim($1);
629916b7f3c8SJoe Perches				if ($arg =~ /^$Type$/ &&
630016b7f3c8SJoe Perches					$arg !~ /enum\s+$Ident$/) {
6301ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6302ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6303ca0d8929SJoe Perches				}
6304ca0d8929SJoe Perches			}
6305ca0d8929SJoe Perches		}
6306ca0d8929SJoe Perches
6307a0ad7596SJoe Perches# check for function definitions
63085b57980dSJoe Perches		if ($perl_version_ok &&
6309a0ad7596SJoe Perches		    defined $stat &&
6310a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6311a0ad7596SJoe Perches			$context_function = $1;
6312a0ad7596SJoe Perches
6313a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6314a0ad7596SJoe Perches			my $ok = 0;
6315a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6316a0ad7596SJoe Perches			my $herectx = $here . "\n";
6317a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6318a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6319a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6320a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6321a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6322a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6323a0ad7596SJoe Perches			}
6324a0ad7596SJoe Perches			if (!$ok) {
6325a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6326a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6327a0ad7596SJoe Perches			}
6328a0ad7596SJoe Perches		}
6329a0ad7596SJoe Perches
6330de7d4f0eSAndy Whitcroft# checks for new __setup's
6331de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6332de7d4f0eSAndy Whitcroft			my $name = $1;
6333de7d4f0eSAndy Whitcroft
6334de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6335000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
63368c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6337de7d4f0eSAndy Whitcroft			}
6338653d4876SAndy Whitcroft		}
63399c0ca6f9SAndy Whitcroft
6340e29a70f1SJoe Perches# check for pointless casting of alloc functions
6341e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6342000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6343000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
63449c0ca6f9SAndy Whitcroft		}
634513214adfSAndy Whitcroft
6346a640d25cSJoe Perches# alloc style
6347a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
63485b57980dSJoe Perches		if ($perl_version_ok &&
6349e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6350a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6351a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6352a640d25cSJoe Perches		}
6353a640d25cSJoe Perches
635460a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
63555b57980dSJoe Perches		if ($perl_version_ok &&
63561b4a2ed4SJoe Perches		    defined $stat &&
63571b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
635860a55369SJoe Perches			my $oldfunc = $3;
635960a55369SJoe Perches			my $a1 = $4;
636060a55369SJoe Perches			my $a2 = $10;
636160a55369SJoe Perches			my $newfunc = "kmalloc_array";
636260a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
636360a55369SJoe Perches			my $r1 = $a1;
636460a55369SJoe Perches			my $r2 = $a2;
636560a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
636660a55369SJoe Perches				$r1 = $a2;
636760a55369SJoe Perches				$r2 = $a1;
636860a55369SJoe Perches			}
6369e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6370e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
63711b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6372e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6373e3d95a2aSTobin C. Harding
6374e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
63751b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
63761b4a2ed4SJoe Perches				    $cnt == 1 &&
6377e367455aSJoe Perches				    $fix) {
6378194f66fcSJoe 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;
637960a55369SJoe Perches				}
638060a55369SJoe Perches			}
638160a55369SJoe Perches		}
638260a55369SJoe Perches
6383972fdea2SJoe Perches# check for krealloc arg reuse
63845b57980dSJoe Perches		if ($perl_version_ok &&
63854cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
63864cab63ceSJoe Perches		    $1 eq $3) {
6387972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6388972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6389972fdea2SJoe Perches		}
6390972fdea2SJoe Perches
63915ce59ae0SJoe Perches# check for alloc argument mismatch
63925ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
63935ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
63945ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
63955ce59ae0SJoe Perches		}
63965ce59ae0SJoe Perches
6397caf2a54fSJoe Perches# check for multiple semicolons
6398caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6399d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6400d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6401d5e616fcSJoe Perches			    $fix) {
6402194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6403d5e616fcSJoe Perches			}
6404d1e2ad07SJoe Perches		}
6405d1e2ad07SJoe Perches
6406cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6407cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6408cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
64090ab90191SJoe Perches			my $ull = "";
64100ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
64110ab90191SJoe Perches			if (CHK("BIT_MACRO",
64120ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
64130ab90191SJoe Perches			    $fix) {
64140ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
64150ab90191SJoe Perches			}
64160ab90191SJoe Perches		}
64170ab90191SJoe Perches
64182d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
64192d632745SJoe 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*$/) {
64202d632745SJoe Perches			my $config = $1;
64212d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
64222d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
64232d632745SJoe Perches			    $fix) {
64242d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
64252d632745SJoe Perches			}
64262d632745SJoe Perches		}
64272d632745SJoe Perches
6428e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6429c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6430c34c09a8SJoe Perches			my $has_break = 0;
6431c34c09a8SJoe Perches			my $has_statement = 0;
6432c34c09a8SJoe Perches			my $count = 0;
6433c34c09a8SJoe Perches			my $prevline = $linenr;
6434e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6435c34c09a8SJoe Perches				$prevline--;
6436c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6437c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6438c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6439c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6440c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6441c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6442c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6443c34c09a8SJoe Perches				$has_statement = 1;
6444c34c09a8SJoe Perches				$count++;
6445258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6446c34c09a8SJoe Perches			}
6447c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6448c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6449224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6450c34c09a8SJoe Perches			}
6451c34c09a8SJoe Perches		}
6452c34c09a8SJoe Perches
6453f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6454f36d3eb8SJoe Perches		my @fallthroughs = (
6455f36d3eb8SJoe Perches			'fallthrough',
6456f36d3eb8SJoe Perches			'@fallthrough@',
6457f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6458f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6459f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6460f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6461f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6462f36d3eb8SJoe Perches		    );
6463f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6464f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6465f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6466f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6467f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6468f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6469f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6470f36d3eb8SJoe Perches					last;
6471f36d3eb8SJoe Perches				}
6472f36d3eb8SJoe Perches			}
6473f36d3eb8SJoe Perches		}
6474f36d3eb8SJoe Perches
6475d1e2ad07SJoe Perches# check for switch/default statements without a break;
64765b57980dSJoe Perches		if ($perl_version_ok &&
6477d1e2ad07SJoe Perches		    defined $stat &&
6478d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6479d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6480e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6481e3d95a2aSTobin C. Harding
6482d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6483d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6484caf2a54fSJoe Perches		}
6485caf2a54fSJoe Perches
648613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6487d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6488d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6489d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6490d5e616fcSJoe Perches			    $fix) {
6491194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6492d5e616fcSJoe Perches			}
649313214adfSAndy Whitcroft		}
6494773647a0SAndy Whitcroft
649562ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
649662ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
649762ec818fSJoe Perches			ERROR("DATE_TIME",
649862ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
649962ec818fSJoe Perches		}
650062ec818fSJoe Perches
65012c92488aSJoe Perches# check for use of yield()
65022c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
65032c92488aSJoe Perches			WARN("YIELD",
65042c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
65052c92488aSJoe Perches		}
65062c92488aSJoe Perches
6507179f8f40SJoe Perches# check for comparisons against true and false
6508179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6509179f8f40SJoe Perches			my $lead = $1;
6510179f8f40SJoe Perches			my $arg = $2;
6511179f8f40SJoe Perches			my $test = $3;
6512179f8f40SJoe Perches			my $otype = $4;
6513179f8f40SJoe Perches			my $trail = $5;
6514179f8f40SJoe Perches			my $op = "!";
6515179f8f40SJoe Perches
6516179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6517179f8f40SJoe Perches
6518179f8f40SJoe Perches			my $type = lc($otype);
6519179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6520179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6521179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6522179f8f40SJoe Perches					$op = "";
6523179f8f40SJoe Perches				}
6524179f8f40SJoe Perches
6525179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6526179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6527179f8f40SJoe Perches
6528179f8f40SJoe Perches## maybe suggesting a correct construct would better
6529179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6530179f8f40SJoe Perches
6531179f8f40SJoe Perches			}
6532179f8f40SJoe Perches		}
6533179f8f40SJoe Perches
65344882720bSThomas Gleixner# check for semaphores initialized locked
65354882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6536000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6537000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6538773647a0SAndy Whitcroft		}
65396712d858SJoe Perches
654067d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
654167d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6542000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
654367d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6544773647a0SAndy Whitcroft		}
65456712d858SJoe Perches
6546ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6547f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6548000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6549ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6550f3db6639SMichael Ellerman		}
65516712d858SJoe Perches
65523d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
65533d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
65543d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
65553d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
65563d709ab5SPaul E. McKenney		}
65573d709ab5SPaul E. McKenney
65589189c7e7SJoe Perches# check for deprecated apis
65599189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
65609189c7e7SJoe Perches			my $deprecated_api = $1;
65619189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
65629189c7e7SJoe Perches			WARN("DEPRECATED_API",
65639189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
65649189c7e7SJoe Perches		}
65659189c7e7SJoe Perches
65660f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6567d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
65686903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6569d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6570000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6571d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
65722b6db5cbSAndy Whitcroft		}
6573773647a0SAndy Whitcroft
6574773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6575773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6576773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6577c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6578c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6579171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6580171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6581171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6582773647a0SAndy Whitcroft		{
6583000d1cc1SJoe Perches			WARN("NR_CPUS",
6584000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6585773647a0SAndy Whitcroft		}
65869c9ba34eSAndy Whitcroft
658752ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
658852ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
658952ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
659052ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
659152ea8506SJoe Perches		}
659252ea8506SJoe Perches
6593acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
65945b57980dSJoe Perches		if ($perl_version_ok &&
6595acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6596acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6597acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6598acd9362cSJoe Perches		}
6599acd9362cSJoe Perches
6600de3f186fSDenis Efremov# nested likely/unlikely calls
6601de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6602de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6603de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6604de3f186fSDenis Efremov		}
6605de3f186fSDenis Efremov
6606691d77b6SAndy Whitcroft# whine mightly about in_atomic
6607691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6608691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6609000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6610000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6611f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6612000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6613000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6614691d77b6SAndy Whitcroft			}
6615691d77b6SAndy Whitcroft		}
66161704f47bSPeter Zijlstra
66170f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
66180f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
66190f5225b0SPeter Zijlstra			ERROR("LOCKING",
66200f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
66210f5225b0SPeter Zijlstra		}
66220f5225b0SPeter Zijlstra
66231704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
66241704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
66251704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
66261704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
66271704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
66281704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6629000d1cc1SJoe Perches				ERROR("LOCKDEP",
6630000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
66311704f47bSPeter Zijlstra			}
66321704f47bSPeter Zijlstra		}
663388f8831cSDave Jones
6634b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6635b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6636000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6637000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
663888f8831cSDave Jones		}
66392435880fSJoe Perches
664000180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
664100180468SJoe Perches# and whether or not function naming is typical and if
664200180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
66435b57980dSJoe Perches		if ($perl_version_ok &&
664400180468SJoe Perches		    defined $stat &&
664500180468SJoe 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*\)/) {
664600180468SJoe Perches			my $var = $1;
664700180468SJoe Perches			my $perms = $2;
664800180468SJoe Perches			my $show = $3;
664900180468SJoe Perches			my $store = $4;
665000180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
665100180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
665200180468SJoe Perches			    $store =~ /^${var}_store$/ &&
665300180468SJoe Perches			    $octal_perms eq "0644") {
665400180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
665500180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
665600180468SJoe Perches				    $fix) {
665700180468SJoe 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})/;
665800180468SJoe Perches				}
665900180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
666000180468SJoe Perches				 $store =~ /^NULL$/ &&
666100180468SJoe Perches				 $octal_perms eq "0444") {
666200180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
666300180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
666400180468SJoe Perches				    $fix) {
666500180468SJoe 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})/;
666600180468SJoe Perches				}
666700180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
666800180468SJoe Perches				 $store =~ /^${var}_store$/ &&
666900180468SJoe Perches				 $octal_perms eq "0200") {
667000180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
667100180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
667200180468SJoe Perches				    $fix) {
667300180468SJoe 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})/;
667400180468SJoe Perches				}
667500180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
667600180468SJoe Perches				 $octal_perms eq "0444" ||
667700180468SJoe Perches				 $octal_perms eq "0200") {
667800180468SJoe Perches				my $newshow = "$show";
667900180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
668000180468SJoe Perches				my $newstore = $store;
668100180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
668200180468SJoe Perches				my $rename = "";
668300180468SJoe Perches				if ($show ne $newshow) {
668400180468SJoe Perches					$rename .= " '$show' to '$newshow'";
668500180468SJoe Perches				}
668600180468SJoe Perches				if ($store ne $newstore) {
668700180468SJoe Perches					$rename .= " '$store' to '$newstore'";
668800180468SJoe Perches				}
668900180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
669000180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
669100180468SJoe Perches			} else {
669200180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
669300180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
669400180468SJoe Perches			}
669500180468SJoe Perches		}
669600180468SJoe Perches
6697515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6698515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
669973121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
670073121534SJoe Perches#   specific definition of not visible in sysfs.
670173121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
670273121534SJoe Perches#   use the default permissions
67035b57980dSJoe Perches		if ($perl_version_ok &&
6704459cf0aeSJoe Perches		    defined $stat &&
6705515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
67062435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
67072435880fSJoe Perches				my $func = $entry->[0];
67082435880fSJoe Perches				my $arg_pos = $entry->[1];
67092435880fSJoe Perches
6710459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6711459cf0aeSJoe Perches				$lc = $lc + $linenr;
67122a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6713459cf0aeSJoe Perches
67142435880fSJoe Perches				my $skip_args = "";
67152435880fSJoe Perches				if ($arg_pos > 1) {
67162435880fSJoe Perches					$arg_pos--;
67172435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
67182435880fSJoe Perches				}
6719f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6720459cf0aeSJoe Perches				if ($stat =~ /$test/) {
67212435880fSJoe Perches					my $val = $1;
67222435880fSJoe Perches					$val = $6 if ($skip_args ne "");
672373121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
672473121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
672573121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
67262435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6727459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6728f90774e1SJoe Perches					}
6729f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6730c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6731459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
67322435880fSJoe Perches					}
6733459cf0aeSJoe Perches				}
6734459cf0aeSJoe Perches			}
6735459cf0aeSJoe Perches		}
6736459cf0aeSJoe Perches
6737459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6738bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
673900180468SJoe Perches			my $oval = $1;
674000180468SJoe Perches			my $octal = perms_to_octal($oval);
6741f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6742459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6743f90774e1SJoe Perches			    $fix) {
674400180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
67452435880fSJoe Perches			}
674613214adfSAndy Whitcroft		}
67475a6d20ceSBjorn Andersson
67485a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
67495a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
67505a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
67515a6d20ceSBjorn Andersson			my $valid_licenses = qr{
67525a6d20ceSBjorn Andersson						GPL|
67535a6d20ceSBjorn Andersson						GPL\ v2|
67545a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
67555a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
67565a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
67575a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
67585a6d20ceSBjorn Andersson						Proprietary
67595a6d20ceSBjorn Andersson					}x;
67605a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
67615a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
67625a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
67635a6d20ceSBjorn Andersson			}
67645a6d20ceSBjorn Andersson		}
67656a8d76cbSMatteo Croce
67666a8d76cbSMatteo Croce# check for sysctl duplicate constants
67676a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
67686a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
67696a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
67706a8d76cbSMatteo Croce		}
6771515a235eSJoe Perches	}
677213214adfSAndy Whitcroft
677313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
677413214adfSAndy Whitcroft	# so just keep quiet.
677513214adfSAndy Whitcroft	if ($#rawlines == -1) {
677613214adfSAndy Whitcroft		exit(0);
67770a920b5bSAndy Whitcroft	}
67780a920b5bSAndy Whitcroft
67798905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
67808905a67cSAndy Whitcroft	# things that appear to be patches.
67818905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
67828905a67cSAndy Whitcroft		exit(0);
67838905a67cSAndy Whitcroft	}
67848905a67cSAndy Whitcroft
67858905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
67868905a67cSAndy Whitcroft	# just keep quiet.
67878905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
67888905a67cSAndy Whitcroft		exit(0);
67898905a67cSAndy Whitcroft	}
67908905a67cSAndy Whitcroft
6791a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6792000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6793000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
67940a920b5bSAndy Whitcroft	}
6795cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6796cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6797000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6798000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6799cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6800cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6801cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6802cd261496SGeert Uytterhoeven		}
68030a920b5bSAndy Whitcroft	}
68040a920b5bSAndy Whitcroft
6805f0a594c1SAndy Whitcroft	print report_dump();
680613214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
680713214adfSAndy Whitcroft		print "$filename " if ($summary_file);
68086c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
68096c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
68106c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
68116c72ffaaSAndy Whitcroft	}
68128905a67cSAndy Whitcroft
6813d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6814ef212196SJoe Perches		# If there were any defects found and not already fixing them
6815ef212196SJoe Perches		if (!$clean and !$fix) {
6816ef212196SJoe Perches			print << "EOM"
6817ef212196SJoe Perches
6818ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6819ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6820ef212196SJoe PerchesEOM
6821ef212196SJoe Perches		}
6822d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6823d2c0a235SAndy Whitcroft		# then suggest that.
6824d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6825b0781216SMike Frysinger			$rpt_cleaners = 0;
6826d8469f16SJoe Perches			print << "EOM"
6827d8469f16SJoe Perches
6828d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6829d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6830d8469f16SJoe PerchesEOM
6831d2c0a235SAndy Whitcroft		}
6832d2c0a235SAndy Whitcroft	}
6833d2c0a235SAndy Whitcroft
6834d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6835d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6836d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
68379624b8d6SJoe Perches		my $newfile = $filename;
68389624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
68393705ce5bSJoe Perches		my $linecount = 0;
68403705ce5bSJoe Perches		my $f;
68413705ce5bSJoe Perches
6842d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6843d752fcc8SJoe Perches
68443705ce5bSJoe Perches		open($f, '>', $newfile)
68453705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
68463705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
68473705ce5bSJoe Perches			$linecount++;
68483705ce5bSJoe Perches			if ($file) {
68493705ce5bSJoe Perches				if ($linecount > 3) {
68503705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
68513705ce5bSJoe Perches					print $f $fixed_line . "\n";
68523705ce5bSJoe Perches				}
68533705ce5bSJoe Perches			} else {
68543705ce5bSJoe Perches				print $f $fixed_line . "\n";
68553705ce5bSJoe Perches			}
68563705ce5bSJoe Perches		}
68573705ce5bSJoe Perches		close($f);
68583705ce5bSJoe Perches
68593705ce5bSJoe Perches		if (!$quiet) {
68603705ce5bSJoe Perches			print << "EOM";
6861d8469f16SJoe Perches
68623705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
68633705ce5bSJoe Perches
68643705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
68653705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
68663705ce5bSJoe Perches
68673705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
68683705ce5bSJoe PerchesNo warranties, expressed or implied...
68693705ce5bSJoe PerchesEOM
68703705ce5bSJoe Perches		}
68713705ce5bSJoe Perches	}
68723705ce5bSJoe Perches
6873d8469f16SJoe Perches	if ($quiet == 0) {
6874d8469f16SJoe Perches		print "\n";
6875d8469f16SJoe Perches		if ($clean == 1) {
6876d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6877d8469f16SJoe Perches		} else {
6878d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
68790a920b5bSAndy Whitcroft		}
68800a920b5bSAndy Whitcroft	}
68810a920b5bSAndy Whitcroft	return $clean;
68820a920b5bSAndy Whitcroft}
6883