xref: /linux-6.15/scripts/checkpatch.pl (revision 44d303eb)
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)?) |
482e29a70f1SJoe Perches	(?:\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
2338*44d303ebSJoe 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
2664*44d303ebSJoe Perches# Check for patch separator
2665*44d303ebSJoe Perches		if ($line =~ /^---$/) {
2666*44d303ebSJoe Perches			$has_patch_separator = 1;
2667*44d303ebSJoe Perches			$in_commit_log = 0;
2668*44d303ebSJoe Perches		}
2669*44d303ebSJoe 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
2769*44d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
2770*44d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
27717ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
2772*44d303ebSJoe 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					}
31609f3a8992SRob Herring				}
31619f3a8992SRob Herring			}
31629f3a8992SRob Herring		}
31639f3a8992SRob Herring
31645368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3165d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
31665368df20SAndy Whitcroft
3167a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3168a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3169a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3170a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3171a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3172a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3173a8da38a9SJoe Perches		}
3174a8da38a9SJoe Perches
317547e0c88bSJoe Perches# line length limit (with some exclusions)
317647e0c88bSJoe Perches#
317747e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
317847e0c88bSJoe Perches#	logging functions like pr_info that end in a string
317947e0c88bSJoe Perches#	lines with a single string
318047e0c88bSJoe Perches#	#defines that are a single string
31812e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
318247e0c88bSJoe Perches#
318347e0c88bSJoe Perches# There are 3 different line length message types:
3184ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
318547e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
318647e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
318747e0c88bSJoe Perches#
318847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
318947e0c88bSJoe Perches#
319047e0c88bSJoe Perches
3191b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
319247e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
319347e0c88bSJoe Perches
319447e0c88bSJoe Perches			# Check the allowed long line types first
319547e0c88bSJoe Perches
319647e0c88bSJoe Perches			# logging functions that end in a string that starts
319747e0c88bSJoe Perches			# before $max_line_length
319847e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
319947e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
320047e0c88bSJoe Perches				$msg_type = "";
320147e0c88bSJoe Perches
320247e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
320347e0c88bSJoe Perches			# #defines with only strings
320447e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
320547e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
320647e0c88bSJoe Perches				$msg_type = "";
320747e0c88bSJoe Perches
3208cc147506SJoe Perches			# More special cases
3209cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3210cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3211d560a5f8SJoe Perches				$msg_type = "";
3212d560a5f8SJoe Perches
32132e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
32142e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
32152e4bbbc5SAndreas Brauchli				$msg_type = "";
32162e4bbbc5SAndreas Brauchli
321747e0c88bSJoe Perches			# Otherwise set the alternate message types
321847e0c88bSJoe Perches
321947e0c88bSJoe Perches			# a comment starts before $max_line_length
322047e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
322147e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
322247e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
322347e0c88bSJoe Perches
322447e0c88bSJoe Perches			# a quoted string starts before $max_line_length
322547e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
322647e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
322747e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
322847e0c88bSJoe Perches			}
322947e0c88bSJoe Perches
323047e0c88bSJoe Perches			if ($msg_type ne "" &&
323147e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
323247e0c88bSJoe Perches				WARN($msg_type,
32336cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
32340a920b5bSAndy Whitcroft			}
323547e0c88bSJoe Perches		}
32360a920b5bSAndy Whitcroft
32378905a67cSAndy Whitcroft# check for adding lines without a newline.
32388905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3239000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3240000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
32418905a67cSAndy Whitcroft		}
32428905a67cSAndy Whitcroft
3243b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3244de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
32450a920b5bSAndy Whitcroft
32460a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3247713a09deSAntonio Borneo# more than $tabsize must use tabs.
3248c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3249c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3250c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3251d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32523705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
32533705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
32543705ce5bSJoe Perches			    $fix) {
3255194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
32563705ce5bSJoe Perches			}
32570a920b5bSAndy Whitcroft		}
32580a920b5bSAndy Whitcroft
325908e44365SAlberto Panizzo# check for space before tabs.
326008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
326108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32623705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
32633705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
32643705ce5bSJoe Perches			    $fix) {
3265194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3266713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3267194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3268c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
32693705ce5bSJoe Perches			}
327008e44365SAlberto Panizzo		}
327108e44365SAlberto Panizzo
32726a487211SJoe Perches# check for assignments on the start of a line
32736a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
32746a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
32756a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
32766a487211SJoe Perches		}
32776a487211SJoe Perches
3278d1fe9c09SJoe Perches# check for && or || at the start of a line
3279d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3280d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3281d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3282d1fe9c09SJoe Perches		}
3283d1fe9c09SJoe Perches
3284a91e8994SJoe Perches# check indentation starts on a tab stop
32855b57980dSJoe Perches		if ($perl_version_ok &&
3286bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3287a91e8994SJoe Perches			my $indent = length($1);
3288713a09deSAntonio Borneo			if ($indent % $tabsize) {
3289a91e8994SJoe Perches				if (WARN("TABSTOP",
3290a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3291a91e8994SJoe Perches				    $fix) {
3292713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3293a91e8994SJoe Perches				}
3294a91e8994SJoe Perches			}
3295a91e8994SJoe Perches		}
3296a91e8994SJoe Perches
3297d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
32985b57980dSJoe Perches		if ($perl_version_ok &&
3299fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3300d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3301d1fe9c09SJoe Perches			my $oldindent = $1;
3302d1fe9c09SJoe Perches			my $rest = $2;
3303d1fe9c09SJoe Perches
3304d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3305d1fe9c09SJoe Perches			if ($pos >= 0) {
3306b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3307b34a26f3SJoe Perches				my $newindent = $2;
3308d1fe9c09SJoe Perches
3309d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3310713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3311713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3312d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3313d1fe9c09SJoe Perches
3314d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3315d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
33163705ce5bSJoe Perches
33173705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
33183705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
33193705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3320194f66fcSJoe Perches						$fixed[$fixlinenr] =~
33213705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
33223705ce5bSJoe Perches					}
3323d1fe9c09SJoe Perches				}
3324d1fe9c09SJoe Perches			}
3325d1fe9c09SJoe Perches		}
3326d1fe9c09SJoe Perches
33276ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
33286ab3a970SJoe Perches# avoid checking a few false positives:
33296ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
33306ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
33316ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
33326ab3a970SJoe Perches#   multiline macros that define functions
33336ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
33346ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
33356ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
33363705ce5bSJoe Perches			if (CHK("SPACING",
3337f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
33383705ce5bSJoe Perches			    $fix) {
3339194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3340f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
33413705ce5bSJoe Perches			}
3342aad4f614SJoe Perches		}
3343aad4f614SJoe Perches
334486406b1cSJoe Perches# Block comment styles
334586406b1cSJoe Perches# Networking with an initial /*
334605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3347fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
334885ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
334985ad978cSJoe Perches		    $realline > 2) {
335005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
335105880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
335205880600SJoe Perches		}
335305880600SJoe Perches
335486406b1cSJoe Perches# Block comments use * on subsequent lines
335586406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
335686406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3357a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
335861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3359a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
336086406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
336186406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3362a605e32eSJoe Perches		}
3363a605e32eSJoe Perches
336486406b1cSJoe Perches# Block comments use */ on trailing lines
336586406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3366c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3367c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3368c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
336986406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
337086406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
337105880600SJoe Perches		}
337205880600SJoe Perches
337308eb9b80SJoe Perches# Block comment * alignment
337408eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3375af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3376af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3377af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
337808eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3379af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3380af207524SJoe Perches			my $oldindent;
338108eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3382af207524SJoe Perches			if (defined($1)) {
3383af207524SJoe Perches				$oldindent = expand_tabs($1);
3384af207524SJoe Perches			} else {
3385af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3386af207524SJoe Perches				$oldindent = expand_tabs($1);
3387af207524SJoe Perches			}
338808eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
338908eb9b80SJoe Perches			my $newindent = $1;
339008eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3391af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
339208eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
339308eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
339408eb9b80SJoe Perches			}
339508eb9b80SJoe Perches		}
339608eb9b80SJoe Perches
33977f619191SJoe Perches# check for missing blank lines after struct/union declarations
33987f619191SJoe Perches# with exceptions for various attributes and macros
33997f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
34007f619191SJoe Perches		    $line =~ /^\+/ &&
34017f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
34027f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
34037f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
34047f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
34057f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
34067f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
34077f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
34080bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
34097f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3410d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3411d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3412d752fcc8SJoe Perches			    $fix) {
3413f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3414d752fcc8SJoe Perches			}
34157f619191SJoe Perches		}
34167f619191SJoe Perches
3417365dd4eaSJoe Perches# check for multiple consecutive blank lines
3418365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3419365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3420365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3421d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3422d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3423d752fcc8SJoe Perches			    $fix) {
3424f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3425d752fcc8SJoe Perches			}
3426d752fcc8SJoe Perches
3427365dd4eaSJoe Perches			$last_blank_line = $linenr;
3428365dd4eaSJoe Perches		}
3429365dd4eaSJoe Perches
34303b617e3bSJoe Perches# check for missing blank lines after declarations
34313f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
34323f7bac03SJoe Perches			# actual declarations
34333f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
34345a4e1fd3SJoe Perches			# function pointer declarations
34355a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34363f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34373f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34383f7bac03SJoe Perches			# known declaration macros
34393f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
34403f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
34413f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
34423f7bac03SJoe Perches			# other possible extensions of declaration lines
34433f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
34443f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
34453f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
34463f7bac03SJoe Perches			# looks like a declaration
34473f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
34485a4e1fd3SJoe Perches			# function pointer declarations
34495a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34503f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34513f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34523f7bac03SJoe Perches			# known declaration macros
34533f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
34543f7bac03SJoe Perches			# start of struct or union or enum
3455328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
34563f7bac03SJoe Perches			# start or end of block or continuation of declaration
34573f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
34583f7bac03SJoe Perches			# bitfield continuation
34593f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
34603f7bac03SJoe Perches			# other possible extensions of declaration lines
34613f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
34623f7bac03SJoe Perches			# indentation of previous and current line are the same
34633f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3464d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3465d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3466d752fcc8SJoe Perches			    $fix) {
3467f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3468d752fcc8SJoe Perches			}
34693b617e3bSJoe Perches		}
34703b617e3bSJoe Perches
34715f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
34726b4c5bebSAndy Whitcroft# Exceptions:
34736b4c5bebSAndy Whitcroft#  1) within comments
34746b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
34756b4c5bebSAndy Whitcroft#  3) hanging labels
34763705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
34775f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34783705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
34793705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
34803705ce5bSJoe Perches			    $fix) {
3481194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
34823705ce5bSJoe Perches			}
34835f7ddae6SRaffaele Recalcati		}
34845f7ddae6SRaffaele Recalcati
3485b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3486b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3487b9ea10d6SAndy Whitcroft
34885751a24eSJoe Perches# check for unusual line ending [ or (
34895751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
34905751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
34915751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
34925751a24eSJoe Perches		}
34935751a24eSJoe Perches
34944dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
34954dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
34964dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
34974dbed76fSJoe Perches			$context_function = $1;
34984dbed76fSJoe Perches		}
34994dbed76fSJoe Perches
35004dbed76fSJoe Perches# check if this appears to be the end of function declaration
35014dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
35024dbed76fSJoe Perches			undef $context_function;
35034dbed76fSJoe Perches		}
35044dbed76fSJoe Perches
3505032a4c0fSJoe Perches# check indentation of any line with a bare else
3506840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3507032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3508032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3509032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3510840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3511840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3512840080a0SJoe Perches			     defined $lines[$linenr] &&
3513840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3514032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3515032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3516032a4c0fSJoe Perches			}
3517032a4c0fSJoe Perches		}
3518032a4c0fSJoe Perches
3519c00df19aSJoe Perches# check indentation of a line with a break;
3520c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3521c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3522c00df19aSJoe Perches			my $tabs = $1;
3523c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3524c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3525c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3526c00df19aSJoe Perches			}
3527c00df19aSJoe Perches		}
3528c00df19aSJoe Perches
3529c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3530cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3531000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3532000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3533c2fdda0dSAndy Whitcroft		}
353422f2a2efSAndy Whitcroft
353556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
353656e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
353756e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
353856e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
353956e77d70SJoe Perches		}
354056e77d70SJoe Perches
35419c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
35422b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
35432b474a1aSAndy Whitcroft		    $realline_next);
35443e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3545ca819864SJoe Perches		if ($linenr > $suppress_statement &&
35461b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3547170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3548f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3549171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3550171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3551171ae1a4SAndy Whitcroft
35523e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
35533e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
35543e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
35553e469cdcSAndy Whitcroft			# until we hit end of it.
35563e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
35573e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
35583e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
35593e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
35603e469cdcSAndy Whitcroft			}
3561f74bd194SAndy Whitcroft
35622b474a1aSAndy Whitcroft			# Find the real next line.
35632b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
35642b474a1aSAndy Whitcroft			if (defined $realline_next &&
35652b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
35662b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
35672b474a1aSAndy Whitcroft				$realline_next++;
35682b474a1aSAndy Whitcroft			}
35692b474a1aSAndy Whitcroft
3570171ae1a4SAndy Whitcroft			my $s = $stat;
3571171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3572cf655043SAndy Whitcroft
3573c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3574171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3575c2fdda0dSAndy Whitcroft
3576c2fdda0dSAndy Whitcroft			# Ignore functions being called
3577171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3578c2fdda0dSAndy Whitcroft
3579463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3580463f2864SAndy Whitcroft
3581c45dcabdSAndy Whitcroft			# declarations always start with types
3582d2506586SAndy 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) {
3583c45dcabdSAndy Whitcroft				my $type = $1;
3584c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3585c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3586c45dcabdSAndy Whitcroft
35876c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3588a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3589c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3590c2fdda0dSAndy Whitcroft			}
35918905a67cSAndy Whitcroft
35926c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
359365863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3594c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
35959c0ca6f9SAndy Whitcroft			}
35968905a67cSAndy Whitcroft
35978905a67cSAndy Whitcroft			# Check for any sort of function declaration.
35988905a67cSAndy Whitcroft			# int foo(something bar, other baz);
35998905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3600171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
36018905a67cSAndy Whitcroft				my ($name_len) = length($1);
36028905a67cSAndy Whitcroft
3603cf655043SAndy Whitcroft				my $ctx = $s;
3604773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
36058905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3606cf655043SAndy Whitcroft
36078905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3608c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
36098905a67cSAndy Whitcroft
3610c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
36118905a67cSAndy Whitcroft					}
36128905a67cSAndy Whitcroft				}
36138905a67cSAndy Whitcroft			}
36148905a67cSAndy Whitcroft
36159c0ca6f9SAndy Whitcroft		}
36169c0ca6f9SAndy Whitcroft
361700df344fSAndy Whitcroft#
361800df344fSAndy Whitcroft# Checks which may be anchored in the context.
361900df344fSAndy Whitcroft#
362000df344fSAndy Whitcroft
362100df344fSAndy Whitcroft# Check for switch () and associated case and default
362200df344fSAndy Whitcroft# statements should be at the same indent.
362300df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
362400df344fSAndy Whitcroft			my $err = '';
362500df344fSAndy Whitcroft			my $sep = '';
362600df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
362700df344fSAndy Whitcroft			shift(@ctx);
362800df344fSAndy Whitcroft			for my $ctx (@ctx) {
362900df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
363000df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
363100df344fSAndy Whitcroft							$indent != $cindent) {
363200df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
363300df344fSAndy Whitcroft					$sep = '';
363400df344fSAndy Whitcroft				} else {
363500df344fSAndy Whitcroft					$sep = "[...]\n";
363600df344fSAndy Whitcroft				}
363700df344fSAndy Whitcroft			}
363800df344fSAndy Whitcroft			if ($err ne '') {
3639000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3640000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3641de7d4f0eSAndy Whitcroft			}
3642de7d4f0eSAndy Whitcroft		}
3643de7d4f0eSAndy Whitcroft
3644de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3645de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
36460fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3647773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3648773647a0SAndy Whitcroft
36499c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
36508eef05ddSJoe Perches
36518eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
36528eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
36538eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
36548eef05ddSJoe Perches			}
36558eef05ddSJoe Perches
3656de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3657de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3658de7d4f0eSAndy Whitcroft
3659548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3660548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3661de7d4f0eSAndy Whitcroft
3662548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3663548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3664548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3665548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3666548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3667773647a0SAndy Whitcroft				$ctx_ln++;
3668773647a0SAndy Whitcroft			}
3669548596d5SAndy Whitcroft
367053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
367153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3672773647a0SAndy Whitcroft
3673773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3674000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3675000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
367601464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
367700df344fSAndy Whitcroft			}
3678773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3679773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3680773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3681773647a0SAndy Whitcroft			{
36829c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
36839c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3684000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3685000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
368601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
36879c0ca6f9SAndy Whitcroft				}
36889c0ca6f9SAndy Whitcroft			}
368900df344fSAndy Whitcroft		}
369000df344fSAndy Whitcroft
36914d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3692f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
36933e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
36943e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
36953e469cdcSAndy Whitcroft					if (!defined $stat);
36964d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
36974d001e4dSAndy Whitcroft
36984d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
36994d001e4dSAndy Whitcroft
37009f5af480SJoe Perches			# remove inline comments
37019f5af480SJoe Perches			$s =~ s/$;/ /g;
37029f5af480SJoe Perches			$c =~ s/$;/ /g;
37034d001e4dSAndy Whitcroft
37044d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
37056f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
37066f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
37074d001e4dSAndy Whitcroft
37089f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
37099f5af480SJoe Perches			# none on the first line, and are going to readd them
37109f5af480SJoe Perches			# where necessary.
37119f5af480SJoe Perches			$s =~ s/\n./\n/gs;
37129f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
37139f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
37149f5af480SJoe Perches			}
37159f5af480SJoe Perches
37164d001e4dSAndy Whitcroft			# We want to check the first line inside the block
37174d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
37184d001e4dSAndy Whitcroft			#  1) any blank line termination
37194d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
37204d001e4dSAndy Whitcroft			#  3) any do (...) {
37214d001e4dSAndy Whitcroft			my $continuation = 0;
37224d001e4dSAndy Whitcroft			my $check = 0;
37234d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
37244d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
37254d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
37264d001e4dSAndy Whitcroft				$continuation = 1;
37274d001e4dSAndy Whitcroft			}
37289bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
37294d001e4dSAndy Whitcroft				$check = 1;
37304d001e4dSAndy Whitcroft				$cond_lines++;
37314d001e4dSAndy Whitcroft			}
37324d001e4dSAndy Whitcroft
37334d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
37344d001e4dSAndy Whitcroft			# preprocessor statement.
37354d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
37364d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
37374d001e4dSAndy Whitcroft				$check = 0;
37384d001e4dSAndy Whitcroft			}
37394d001e4dSAndy Whitcroft
37409bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3741740504c6SAndy Whitcroft			$continuation = 0;
37429bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
37439bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
37444d001e4dSAndy Whitcroft
3745f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3746f16fa28fSAndy Whitcroft				# is not linear.
3747f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3748f16fa28fSAndy Whitcroft					$check = 0;
3749f16fa28fSAndy Whitcroft				}
3750f16fa28fSAndy Whitcroft
37519bd49efeSAndy Whitcroft				# Ignore:
37529bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
37539bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
37549bd49efeSAndy Whitcroft				#  3) labels.
3755740504c6SAndy Whitcroft				if ($continuation ||
3756740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
37579bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
37589bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3759740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
376030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
37619bd49efeSAndy Whitcroft						$cond_lines++;
37629bd49efeSAndy Whitcroft					}
37634d001e4dSAndy Whitcroft				}
376430dad6ebSAndy Whitcroft			}
37654d001e4dSAndy Whitcroft
37664d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
37674d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
37684d001e4dSAndy Whitcroft
37694d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
37704d001e4dSAndy Whitcroft			# this is not this patch's fault.
37714d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
37724d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
37734d001e4dSAndy Whitcroft				$check = 0;
37744d001e4dSAndy Whitcroft			}
37754d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
37764d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
37774d001e4dSAndy Whitcroft			}
37784d001e4dSAndy Whitcroft
37799bd49efeSAndy 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";
37804d001e4dSAndy Whitcroft
37819f5af480SJoe Perches			if ($check && $s ne '' &&
3782713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
37839f5af480SJoe Perches			     ($sindent < $indent) ||
3784f6950a73SJoe Perches			     ($sindent == $indent &&
3785f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3786713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
3787000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3788000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
37894d001e4dSAndy Whitcroft			}
37904d001e4dSAndy Whitcroft		}
37914d001e4dSAndy Whitcroft
37926c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
37936c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
37941f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
37951f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
37966c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3797c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3798c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3799cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3800cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
38011f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3802c2fdda0dSAndy Whitcroft		}
38036c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
38046c72ffaaSAndy Whitcroft
380500df344fSAndy Whitcroft#ignore lines not being added
38063705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
380700df344fSAndy Whitcroft
380811ca40a0SJoe Perches# check for dereferences that span multiple lines
380911ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
381011ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
381111ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
381211ca40a0SJoe Perches			my $ref = $1;
381311ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
381411ca40a0SJoe Perches			$ref .= $1;
381511ca40a0SJoe Perches			$ref =~ s/\s//g;
381611ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
381711ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
381811ca40a0SJoe Perches		}
381911ca40a0SJoe Perches
3820a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3821c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3822a1ce18e4SJoe Perches			my $type = $1;
3823a1ce18e4SJoe Perches			my $var = $2;
3824207a8e84SJoe Perches			$var = "" if (!defined $var);
3825207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3826a1ce18e4SJoe Perches				my $sign = $1;
3827a1ce18e4SJoe Perches				my $pointer = $2;
3828a1ce18e4SJoe Perches
3829a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3830a1ce18e4SJoe Perches
3831a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3832a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3833a1ce18e4SJoe Perches				    $fix) {
3834a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3835207a8e84SJoe Perches					my $comp_pointer = $pointer;
3836207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3837207a8e84SJoe Perches					$decl .= $comp_pointer;
3838207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3839207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3840a1ce18e4SJoe Perches				}
3841a1ce18e4SJoe Perches			}
3842a1ce18e4SJoe Perches		}
3843a1ce18e4SJoe Perches
3844653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
38457429c690SAndy Whitcroft		if ($dbg_type) {
38467429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3847000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3848000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
38497429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3850000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3851000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
38527429c690SAndy Whitcroft			}
3853653d4876SAndy Whitcroft			next;
3854653d4876SAndy Whitcroft		}
3855a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3856a1ef277eSAndy Whitcroft		if ($dbg_attr) {
38579360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3858000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3859000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
38609360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3861000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3862000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3863a1ef277eSAndy Whitcroft			}
3864a1ef277eSAndy Whitcroft			next;
3865a1ef277eSAndy Whitcroft		}
3866653d4876SAndy Whitcroft
3867f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
386899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
386999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3870d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3871d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3872f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3873f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3874f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3875d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3876d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3877f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3878d752fcc8SJoe Perches				$fixedline = $line;
38798d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3880f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3881d752fcc8SJoe Perches			}
3882f0a594c1SAndy Whitcroft		}
3883f0a594c1SAndy Whitcroft
388400df344fSAndy Whitcroft#
388500df344fSAndy Whitcroft# Checks which are anchored on the added line.
388600df344fSAndy Whitcroft#
388700df344fSAndy Whitcroft
3888653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3889c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3890653d4876SAndy Whitcroft			my $path = $1;
3891653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3892000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3893495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3894495e9d84SJoe Perches			}
3895495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3896495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3897495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3898653d4876SAndy Whitcroft			}
3899653d4876SAndy Whitcroft		}
3900653d4876SAndy Whitcroft
390100df344fSAndy Whitcroft# no C99 // comments
390200df344fSAndy Whitcroft		if ($line =~ m{//}) {
39033705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
39043705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
39053705ce5bSJoe Perches			    $fix) {
3906194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
39073705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
39083705ce5bSJoe Perches					my $comment = trim($1);
3909194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
39103705ce5bSJoe Perches				}
39113705ce5bSJoe Perches			}
391200df344fSAndy Whitcroft		}
391300df344fSAndy Whitcroft		# Remove C99 comments.
39140a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
39156c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
39160a920b5bSAndy Whitcroft
39172b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
39182b474a1aSAndy Whitcroft# the whole statement.
39192b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
39202b474a1aSAndy Whitcroft		if (defined $realline_next &&
39212b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
39222b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
39232b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39242b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39253cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
39263cbf62dfSAndy Whitcroft			# a prefix:
39273cbf62dfSAndy Whitcroft			#   XXX(foo);
39283cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3929653d4876SAndy Whitcroft			my $name = $1;
393087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
39313cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
39323cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
39333cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
39343cbf62dfSAndy Whitcroft
39353cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
39362b474a1aSAndy Whitcroft				\n.}\s*$|
393748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
393848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
393948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
39402b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
39412b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
394248012058SAndy Whitcroft			    )/x) {
39432b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
39442b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
39452b474a1aSAndy Whitcroft			} else {
39462b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
39470a920b5bSAndy Whitcroft			}
39480a920b5bSAndy Whitcroft		}
39492b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
39502b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
39512b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39522b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39532b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
39542b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
39552b474a1aSAndy Whitcroft		}
39562b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
39572b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3958000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3959000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
39602b474a1aSAndy Whitcroft		}
39610a920b5bSAndy Whitcroft
39625150bda4SJoe Eloff# check for global initialisers.
39636d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3964d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
39656d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3966d5e616fcSJoe Perches			    $fix) {
39676d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3968d5e616fcSJoe Perches			}
3969f0a594c1SAndy Whitcroft		}
39700a920b5bSAndy Whitcroft# check for static initialisers.
39716d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3972d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
39736d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3974d5e616fcSJoe Perches				      $herecurr) &&
3975d5e616fcSJoe Perches			    $fix) {
39766d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3977d5e616fcSJoe Perches			}
39780a920b5bSAndy Whitcroft		}
39790a920b5bSAndy Whitcroft
39801813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
39811813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
39821813087dSJoe Perches			my $tmp = trim($1);
39831813087dSJoe Perches			WARN("MISORDERED_TYPE",
39841813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
39851813087dSJoe Perches		}
39861813087dSJoe Perches
3987809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3988809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3989809e082eSJoe Perches			my $type = trim($1);
3990809e082eSJoe Perches			next if ($type !~ /\bint\b/);
3991809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3992809e082eSJoe Perches			my $new_type = $type;
3993809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
3994809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3995809e082eSJoe Perches			$new_type =~ s/^const\s+//;
3996809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3997809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
3998809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
3999809e082eSJoe Perches			$new_type = trim($new_type);
4000809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4001809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4002809e082eSJoe Perches			    $fix) {
4003809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4004809e082eSJoe Perches			}
4005809e082eSJoe Perches		}
4006809e082eSJoe Perches
4007cb710ecaSJoe Perches# check for static const char * arrays.
4008cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4009000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4010000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4011cb710ecaSJoe Perches				$herecurr);
4012cb710ecaSJoe Perches		}
4013cb710ecaSJoe Perches
401477b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
401577b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
401677b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
401777b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
401877b8c0a8SJoe Perches			    $fix) {
401977b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
402077b8c0a8SJoe Perches			}
402177b8c0a8SJoe Perches		}
402277b8c0a8SJoe Perches
4023cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4024cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4025000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4026000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4027cb710ecaSJoe Perches				$herecurr);
4028cb710ecaSJoe Perches		}
4029cb710ecaSJoe Perches
4030ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4031ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4032ab7e23f3SJoe Perches			my $found = $1;
4033ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4034ab7e23f3SJoe Perches				WARN("CONST_CONST",
4035ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4036ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4037ab7e23f3SJoe Perches				WARN("CONST_CONST",
4038ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4039ab7e23f3SJoe Perches			}
4040ab7e23f3SJoe Perches		}
4041ab7e23f3SJoe Perches
40429b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
40439b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
40449b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
40459b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
40469b0fa60dSJoe Perches				$herecurr);
40479b0fa60dSJoe Perches               }
40489b0fa60dSJoe Perches
4049b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4050b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4051b598b670SJoe Perches			my $array = $1;
4052b598b670SJoe 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*\))@) {
4053b598b670SJoe Perches				my $array_div = $1;
4054b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4055b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4056b598b670SJoe Perches				    $fix) {
4057b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4058b598b670SJoe Perches				}
4059b598b670SJoe Perches			}
4060b598b670SJoe Perches		}
4061b598b670SJoe Perches
4062b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
4063b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4064b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4065b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4066b36190c5SJoe Perches			    $fix) {
4067194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4068b36190c5SJoe Perches			}
4069b36190c5SJoe Perches		}
4070b36190c5SJoe Perches
4071653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4072653d4876SAndy Whitcroft# make sense.
4073653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
40748054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4075c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
40768ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
407746d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4078000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4079000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
40800a920b5bSAndy Whitcroft		}
40810a920b5bSAndy Whitcroft
40820a920b5bSAndy Whitcroft# * goes on variable not on type
408365863862SAndy Whitcroft		# (char*[ const])
4084bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4085bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
40863705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4087d8aaf121SAndy Whitcroft
408865863862SAndy Whitcroft			# Should start with a space.
408965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
409065863862SAndy Whitcroft			# Should not end with a space.
409165863862SAndy Whitcroft			$to =~ s/\s+$//;
409265863862SAndy Whitcroft			# '*'s should not have spaces between.
4093f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
409465863862SAndy Whitcroft			}
4095d8aaf121SAndy Whitcroft
40963705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
409765863862SAndy Whitcroft			if ($from ne $to) {
40983705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40993705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
41003705ce5bSJoe Perches				    $fix) {
41013705ce5bSJoe Perches					my $sub_from = $ident;
41023705ce5bSJoe Perches					my $sub_to = $ident;
41033705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4104194f66fcSJoe Perches					$fixed[$fixlinenr] =~
41053705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
41063705ce5bSJoe Perches				}
410765863862SAndy Whitcroft			}
4108bfcb2cc7SAndy Whitcroft		}
4109bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4110bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
41113705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4112d8aaf121SAndy Whitcroft
411365863862SAndy Whitcroft			# Should start with a space.
411465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
411565863862SAndy Whitcroft			# Should not end with a space.
411665863862SAndy Whitcroft			$to =~ s/\s+$//;
411765863862SAndy Whitcroft			# '*'s should not have spaces between.
4118f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
411965863862SAndy Whitcroft			}
412065863862SAndy Whitcroft			# Modifiers should have spaces.
412165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
412265863862SAndy Whitcroft
41233705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4124667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
41253705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
41263705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
41273705ce5bSJoe Perches				    $fix) {
41283705ce5bSJoe Perches
41293705ce5bSJoe Perches					my $sub_from = $match;
41303705ce5bSJoe Perches					my $sub_to = $match;
41313705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4132194f66fcSJoe Perches					$fixed[$fixlinenr] =~
41333705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
41343705ce5bSJoe Perches				}
413565863862SAndy Whitcroft			}
41360a920b5bSAndy Whitcroft		}
41370a920b5bSAndy Whitcroft
41389d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
41399d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
41400675a8fbSJean Delvare			my $msg_level = \&WARN;
41410675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
41420675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
41439d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
41449d3e3c70SJoe Perches		}
41450a920b5bSAndy Whitcroft
41469d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
41478905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4148000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4149000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
41508905a67cSAndy Whitcroft		}
41518905a67cSAndy Whitcroft
415217441227SJoe Perches# check for uses of printk_ratelimit
415317441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4154000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4155000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
415617441227SJoe Perches		}
415717441227SJoe Perches
4158eeef5733SJoe Perches# printk should use KERN_* levels
4159eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4160000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4161eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
416200df344fSAndy Whitcroft		}
41630a920b5bSAndy Whitcroft
4164243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4165243f3803SJoe Perches			my $orig = $1;
4166243f3803SJoe Perches			my $level = lc($orig);
4167243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
41688f26b837SJoe Perches			my $level2 = $level;
41698f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4170243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4171daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4172243f3803SJoe Perches		}
4173243f3803SJoe Perches
4174dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4175dc139313SJoe Perches			my $orig = $1;
4176dc139313SJoe Perches			my $level = lc($orig);
4177dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4178dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4179dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4180dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4181dc139313SJoe Perches		}
4182dc139313SJoe Perches
418391c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
418491c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
418591c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
418691c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
418791c9afafSAndy Lutomirski			WARN("ENOSYS",
418891c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
418991c9afafSAndy Lutomirski		}
419091c9afafSAndy Lutomirski
4191653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4192653d4876SAndy Whitcroft# or if closed on same line
41935b57980dSJoe Perches		if ($perl_version_ok &&
41942d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
41952d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
41962d453e3bSJoe Perches		    $sline !~ /}/) {
41978d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41982d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
41998d182478SJoe Perches			    $fix) {
42008d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
42018d182478SJoe Perches				my $fixed_line = $rawline;
42028d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
42038d182478SJoe Perches				my $line1 = $1;
42048d182478SJoe Perches				my $line2 = $2;
42058d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
42068d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
42078d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
42088d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
42098d182478SJoe Perches				}
42108d182478SJoe Perches			}
42110a920b5bSAndy Whitcroft		}
4212653d4876SAndy Whitcroft
42138905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
42148905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
42158905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
42168d182478SJoe Perches			if (ERROR("OPEN_BRACE",
42178d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
42188d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
42198d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
42208d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
42218d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
42228d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
42238d182478SJoe Perches				$fixedline = $rawline;
42248d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
42258d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
42268d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
42278d182478SJoe Perches				}
42288d182478SJoe Perches			}
42298905a67cSAndy Whitcroft		}
42308905a67cSAndy Whitcroft
42310c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
42323705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
42333705ce5bSJoe Perches			if (WARN("SPACING",
42343705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
42353705ce5bSJoe Perches			    $fix) {
4236194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42373705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
42383705ce5bSJoe Perches			}
42390c73b4ebSAndy Whitcroft		}
42400c73b4ebSAndy Whitcroft
424131070b5dSJoe Perches# Function pointer declarations
424231070b5dSJoe Perches# check spacing between type, funcptr, and args
424331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
424491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
424531070b5dSJoe Perches			my $declare = $1;
424631070b5dSJoe Perches			my $pre_pointer_space = $2;
424731070b5dSJoe Perches			my $post_pointer_space = $3;
424831070b5dSJoe Perches			my $funcname = $4;
424931070b5dSJoe Perches			my $post_funcname_space = $5;
425031070b5dSJoe Perches			my $pre_args_space = $6;
425131070b5dSJoe Perches
425291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
425391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
425491f72e9cSJoe Perches# don't need a space so don't warn for those.
425591f72e9cSJoe Perches			my $post_declare_space = "";
425691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
425791f72e9cSJoe Perches				$post_declare_space = $1;
425891f72e9cSJoe Perches				$declare = rtrim($declare);
425991f72e9cSJoe Perches			}
426091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
426131070b5dSJoe Perches				WARN("SPACING",
426231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
426391f72e9cSJoe Perches				$post_declare_space = " ";
426431070b5dSJoe Perches			}
426531070b5dSJoe Perches
426631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
426791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
426891f72e9cSJoe Perches# equivalent to
426991f72e9cSJoe Perches#	int  foo(int bar, ...)
427091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
427191f72e9cSJoe Perches#
427291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
427391f72e9cSJoe Perches#				WARN("SPACING",
427491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
427591f72e9cSJoe Perches#			}
427631070b5dSJoe Perches
427731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
427831070b5dSJoe Perches			if (defined $pre_pointer_space &&
427931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
428031070b5dSJoe Perches				WARN("SPACING",
428131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
428231070b5dSJoe Perches			}
428331070b5dSJoe Perches
428431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
428531070b5dSJoe Perches			if (defined $post_pointer_space &&
428631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
428731070b5dSJoe Perches				WARN("SPACING",
428831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
428931070b5dSJoe Perches			}
429031070b5dSJoe Perches
429131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
429231070b5dSJoe Perches			if (defined $post_funcname_space &&
429331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
429431070b5dSJoe Perches				WARN("SPACING",
429531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
429631070b5dSJoe Perches			}
429731070b5dSJoe Perches
429831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
429931070b5dSJoe Perches			if (defined $pre_args_space &&
430031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
430131070b5dSJoe Perches				WARN("SPACING",
430231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
430331070b5dSJoe Perches			}
430431070b5dSJoe Perches
430531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4306194f66fcSJoe Perches				$fixed[$fixlinenr] =~
430791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
430831070b5dSJoe Perches			}
430931070b5dSJoe Perches		}
431031070b5dSJoe Perches
43118d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
43128d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4313fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4314fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
43158d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
43168d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
43178d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4318fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
431938dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
43203705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
43213705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
43223705ce5bSJoe Perches				    $fix) {
4323194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
43243705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
43253705ce5bSJoe Perches				}
43268d31cfceSAndy Whitcroft			}
43278d31cfceSAndy Whitcroft		}
43288d31cfceSAndy Whitcroft
4329f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
43306c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4331c2fdda0dSAndy Whitcroft			my $name = $1;
4332773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4333773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4334c2fdda0dSAndy Whitcroft
4335c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4336773647a0SAndy Whitcroft			if ($name =~ /^(?:
4337773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4338773647a0SAndy Whitcroft				volatile|__volatile__|
4339773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4340773647a0SAndy Whitcroft				asm|__asm__)$/x)
4341773647a0SAndy Whitcroft			{
4342c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4343c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4344c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4345c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4346773647a0SAndy Whitcroft
4347773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4348c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4349c2fdda0dSAndy Whitcroft
4350c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4351c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4352773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4353c2fdda0dSAndy Whitcroft
4354c2fdda0dSAndy Whitcroft			} else {
43553705ce5bSJoe Perches				if (WARN("SPACING",
43563705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
43573705ce5bSJoe Perches					     $fix) {
4358194f66fcSJoe Perches					$fixed[$fixlinenr] =~
43593705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
43603705ce5bSJoe Perches				}
4361f0a594c1SAndy Whitcroft			}
43626c72ffaaSAndy Whitcroft		}
43639a4cad4eSEric Nelson
4364653d4876SAndy Whitcroft# Check operator spacing.
43650a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
43663705ce5bSJoe Perches			my $fixed_line = "";
43673705ce5bSJoe Perches			my $line_fixed = 0;
43683705ce5bSJoe Perches
43699c0ca6f9SAndy Whitcroft			my $ops = qr{
43709c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
43719c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
43729c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
43731f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
437484731623SJoe Perches				\?:|\?|:
43759c0ca6f9SAndy Whitcroft			}x;
4376cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
43773705ce5bSJoe Perches
43783705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
43793705ce5bSJoe Perches##			foreach my $el (@elements) {
43803705ce5bSJoe Perches##				print("el: <$el>\n");
43813705ce5bSJoe Perches##			}
43823705ce5bSJoe Perches
43833705ce5bSJoe Perches			my @fix_elements = ();
438400df344fSAndy Whitcroft			my $off = 0;
43856c72ffaaSAndy Whitcroft
43863705ce5bSJoe Perches			foreach my $el (@elements) {
43873705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
43883705ce5bSJoe Perches				$off += length($el);
43893705ce5bSJoe Perches			}
43903705ce5bSJoe Perches
43913705ce5bSJoe Perches			$off = 0;
43923705ce5bSJoe Perches
43936c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4394b34c648bSJoe Perches			my $last_after = -1;
43956c72ffaaSAndy Whitcroft
43960a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
43973705ce5bSJoe Perches
43983705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
43993705ce5bSJoe Perches
44003705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
44013705ce5bSJoe Perches
44024a0df2efSAndy Whitcroft				$off += length($elements[$n]);
44034a0df2efSAndy Whitcroft
440425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4405773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4406773647a0SAndy Whitcroft				my $cc = '';
4407773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4408773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4409773647a0SAndy Whitcroft				}
4410773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4411773647a0SAndy Whitcroft
44124a0df2efSAndy Whitcroft				my $a = '';
44134a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
44144a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4415cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
44164a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
44174a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4418773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
44194a0df2efSAndy Whitcroft
44200a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
44214a0df2efSAndy Whitcroft
44224a0df2efSAndy Whitcroft				my $c = '';
44230a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
44244a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
44254a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4426cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
44274a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
44284a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
44298b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
44304a0df2efSAndy Whitcroft				} else {
44314a0df2efSAndy Whitcroft					$c = 'E';
44320a920b5bSAndy Whitcroft				}
44330a920b5bSAndy Whitcroft
44344a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
44354a0df2efSAndy Whitcroft
44364a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
44374a0df2efSAndy Whitcroft
44386c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4439de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
44400a920b5bSAndy Whitcroft
444174048ed8SAndy Whitcroft				# Pull out the value of this operator.
44426c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
44430a920b5bSAndy Whitcroft
44441f65f947SAndy Whitcroft				# Get the full operator variant.
44451f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
44461f65f947SAndy Whitcroft
444713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
444813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4449d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
445013214adfSAndy Whitcroft
4451cf655043SAndy Whitcroft#				# Ignore comments
4452cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
445313214adfSAndy Whitcroft
4454d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
445513214adfSAndy Whitcroft				} elsif ($op eq ';') {
4456cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4457cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
44583705ce5bSJoe Perches						if (ERROR("SPACING",
44593705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4460b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44613705ce5bSJoe Perches							$line_fixed = 1;
44623705ce5bSJoe Perches						}
4463d8aaf121SAndy Whitcroft					}
4464d8aaf121SAndy Whitcroft
4465d8aaf121SAndy Whitcroft				# // is a comment
4466d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
44670a920b5bSAndy Whitcroft
4468b00e4814SJoe Perches				#   :   when part of a bitfield
4469b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4470b00e4814SJoe Perches					# skip the bitfield test for now
4471b00e4814SJoe Perches
44721f65f947SAndy Whitcroft				# No spaces for:
44731f65f947SAndy Whitcroft				#   ->
4474b00e4814SJoe Perches				} elsif ($op eq '->') {
44754a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
44763705ce5bSJoe Perches						if (ERROR("SPACING",
44773705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4478b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44793705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44803705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44813705ce5bSJoe Perches							}
4482b34c648bSJoe Perches							$line_fixed = 1;
44833705ce5bSJoe Perches						}
44840a920b5bSAndy Whitcroft					}
44850a920b5bSAndy Whitcroft
44862381097bSJoe Perches				# , must not have a space before and must have a space on the right.
44870a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
44882381097bSJoe Perches					my $rtrim_before = 0;
44892381097bSJoe Perches					my $space_after = 0;
44902381097bSJoe Perches					if ($ctx =~ /Wx./) {
44912381097bSJoe Perches						if (ERROR("SPACING",
44922381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
44932381097bSJoe Perches							$line_fixed = 1;
44942381097bSJoe Perches							$rtrim_before = 1;
44952381097bSJoe Perches						}
44962381097bSJoe Perches					}
4497cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
44983705ce5bSJoe Perches						if (ERROR("SPACING",
44993705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
45003705ce5bSJoe Perches							$line_fixed = 1;
4501b34c648bSJoe Perches							$last_after = $n;
45022381097bSJoe Perches							$space_after = 1;
45032381097bSJoe Perches						}
45042381097bSJoe Perches					}
45052381097bSJoe Perches					if ($rtrim_before || $space_after) {
45062381097bSJoe Perches						if ($rtrim_before) {
45072381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45082381097bSJoe Perches						} else {
45092381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45102381097bSJoe Perches						}
45112381097bSJoe Perches						if ($space_after) {
45122381097bSJoe Perches							$good .= " ";
45133705ce5bSJoe Perches						}
45140a920b5bSAndy Whitcroft					}
45150a920b5bSAndy Whitcroft
45169c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
451774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
45189c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
45199c0ca6f9SAndy Whitcroft
45209c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
45219c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
45229c0ca6f9SAndy Whitcroft				# unary operator, or a cast
45239c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
452474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
45250d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4526cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
45273705ce5bSJoe Perches						if (ERROR("SPACING",
45283705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4529b34c648bSJoe Perches							if ($n != $last_after + 2) {
4530b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
45313705ce5bSJoe Perches								$line_fixed = 1;
45323705ce5bSJoe Perches							}
45330a920b5bSAndy Whitcroft						}
4534b34c648bSJoe Perches					}
4535a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4536171ae1a4SAndy Whitcroft						# A unary '*' may be const
4537171ae1a4SAndy Whitcroft
4538171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
45393705ce5bSJoe Perches						if (ERROR("SPACING",
45403705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4541b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
45423705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45433705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
45443705ce5bSJoe Perches							}
4545b34c648bSJoe Perches							$line_fixed = 1;
45463705ce5bSJoe Perches						}
45470a920b5bSAndy Whitcroft					}
45480a920b5bSAndy Whitcroft
45490a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
45500a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4551773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
45523705ce5bSJoe Perches						if (ERROR("SPACING",
45533705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4554b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
45553705ce5bSJoe Perches							$line_fixed = 1;
45563705ce5bSJoe Perches						}
45570a920b5bSAndy Whitcroft					}
4558773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4559773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
45603705ce5bSJoe Perches						if (ERROR("SPACING",
45613705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4562b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45633705ce5bSJoe Perches							$line_fixed = 1;
45643705ce5bSJoe Perches						}
4565653d4876SAndy Whitcroft					}
4566773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
45673705ce5bSJoe Perches						if (ERROR("SPACING",
45683705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4569b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45703705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45713705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4572773647a0SAndy Whitcroft							}
4573b34c648bSJoe Perches							$line_fixed = 1;
45743705ce5bSJoe Perches						}
45753705ce5bSJoe Perches					}
45760a920b5bSAndy Whitcroft
45770a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
45789c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
45799c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
45809c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4581c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4582c2fdda0dSAndy Whitcroft					 $op eq '%')
45830a920b5bSAndy Whitcroft				{
4584d2e025f3SJoe Perches					if ($check) {
4585d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4586d2e025f3SJoe Perches							if (CHK("SPACING",
4587d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4588d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4589d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4590d2e025f3SJoe Perches								$line_fixed = 1;
4591d2e025f3SJoe Perches							}
4592d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4593d2e025f3SJoe Perches							if (CHK("SPACING",
4594d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4595d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4596d2e025f3SJoe Perches								$line_fixed = 1;
4597d2e025f3SJoe Perches							}
4598d2e025f3SJoe Perches						}
4599d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
46003705ce5bSJoe Perches						if (ERROR("SPACING",
46013705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4602b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4603b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4604b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4605b34c648bSJoe Perches							}
46063705ce5bSJoe Perches							$line_fixed = 1;
46073705ce5bSJoe Perches						}
46080a920b5bSAndy Whitcroft					}
46090a920b5bSAndy Whitcroft
46101f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
46111f65f947SAndy Whitcroft				# terminating a case value or a label.
46121f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
46131f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
46143705ce5bSJoe Perches						if (ERROR("SPACING",
46153705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4616b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
46173705ce5bSJoe Perches							$line_fixed = 1;
46183705ce5bSJoe Perches						}
46191f65f947SAndy Whitcroft					}
46201f65f947SAndy Whitcroft
46210a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4622cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
46231f65f947SAndy Whitcroft					my $ok = 0;
46241f65f947SAndy Whitcroft
462522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
46261f65f947SAndy Whitcroft					if (($op eq '<' &&
46271f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
46281f65f947SAndy Whitcroft					    ($op eq '>' &&
46291f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
46301f65f947SAndy Whitcroft					{
46311f65f947SAndy Whitcroft						$ok = 1;
46321f65f947SAndy Whitcroft					}
46331f65f947SAndy Whitcroft
4634e0df7e1fSJoe Perches					# for asm volatile statements
4635e0df7e1fSJoe Perches					# ignore a colon with another
4636e0df7e1fSJoe Perches					# colon immediately before or after
4637e0df7e1fSJoe Perches					if (($op eq ':') &&
4638e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4639e0df7e1fSJoe Perches						$ok = 1;
4640e0df7e1fSJoe Perches					}
4641e0df7e1fSJoe Perches
464284731623SJoe Perches					# messages are ERROR, but ?: are CHK
46431f65f947SAndy Whitcroft					if ($ok == 0) {
46440675a8fbSJean Delvare						my $msg_level = \&ERROR;
46450675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
464684731623SJoe Perches
46470675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
46483705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4649b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4650b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4651b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4652b34c648bSJoe Perches							}
46533705ce5bSJoe Perches							$line_fixed = 1;
46543705ce5bSJoe Perches						}
46550a920b5bSAndy Whitcroft					}
465622f2a2efSAndy Whitcroft				}
46574a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
46583705ce5bSJoe Perches
46593705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
46603705ce5bSJoe Perches
46613705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
46620a920b5bSAndy Whitcroft			}
46633705ce5bSJoe Perches
46643705ce5bSJoe Perches			if (($#elements % 2) == 0) {
46653705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
46663705ce5bSJoe Perches			}
46673705ce5bSJoe Perches
4668194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4669194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
46703705ce5bSJoe Perches			}
46713705ce5bSJoe Perches
46723705ce5bSJoe Perches
46730a920b5bSAndy Whitcroft		}
46740a920b5bSAndy Whitcroft
4675786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4676d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4677786b6326SJoe Perches			if (WARN("SPACING",
4678786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4679786b6326SJoe Perches			    $fix) {
4680194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4681786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4682786b6326SJoe Perches			}
4683786b6326SJoe Perches		}
4684786b6326SJoe Perches
4685f0a594c1SAndy Whitcroft# check for multiple assignments
4686f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4687000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4688000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4689f0a594c1SAndy Whitcroft		}
4690f0a594c1SAndy Whitcroft
469122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
469222f2a2efSAndy Whitcroft## # continuation.
469322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
469422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
469522f2a2efSAndy Whitcroft##
469622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
469722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
469822f2a2efSAndy Whitcroft## 			my $ln = $line;
469922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
470022f2a2efSAndy Whitcroft## 			}
470122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4702000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4703000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
470422f2a2efSAndy Whitcroft## 			}
470522f2a2efSAndy Whitcroft## 		}
4706f0a594c1SAndy Whitcroft
47070a920b5bSAndy Whitcroft#need space before brace following if, while, etc
47086b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
47096ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
47103705ce5bSJoe Perches			if (ERROR("SPACING",
47113705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
47123705ce5bSJoe Perches			    $fix) {
47136ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
47143705ce5bSJoe Perches			}
4715de7d4f0eSAndy Whitcroft		}
4716de7d4f0eSAndy Whitcroft
4717c4a62ef9SJoe Perches## # check for blank lines before declarations
4718c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4719c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4720c4a62ef9SJoe Perches##			WARN("SPACING",
4721c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4722c4a62ef9SJoe Perches##		}
4723c4a62ef9SJoe Perches##
4724c4a62ef9SJoe Perches
4725de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4726de7d4f0eSAndy Whitcroft# on the line
472794fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4728d5e616fcSJoe Perches			if (ERROR("SPACING",
4729d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4730d5e616fcSJoe Perches			    $fix) {
4731194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4732d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4733d5e616fcSJoe Perches			}
47340a920b5bSAndy Whitcroft		}
47350a920b5bSAndy Whitcroft
473622f2a2efSAndy Whitcroft# check spacing on square brackets
473722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
47383705ce5bSJoe Perches			if (ERROR("SPACING",
47393705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
47403705ce5bSJoe Perches			    $fix) {
4741194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47423705ce5bSJoe Perches				    s/\[\s+/\[/;
47433705ce5bSJoe Perches			}
474422f2a2efSAndy Whitcroft		}
474522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
47463705ce5bSJoe Perches			if (ERROR("SPACING",
47473705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
47483705ce5bSJoe Perches			    $fix) {
4749194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47503705ce5bSJoe Perches				    s/\s+\]/\]/;
47513705ce5bSJoe Perches			}
475222f2a2efSAndy Whitcroft		}
475322f2a2efSAndy Whitcroft
4754c45dcabdSAndy Whitcroft# check spacing on parentheses
47559c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
47569c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
47573705ce5bSJoe Perches			if (ERROR("SPACING",
47583705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
47593705ce5bSJoe Perches			    $fix) {
4760194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47613705ce5bSJoe Perches				    s/\(\s+/\(/;
47623705ce5bSJoe Perches			}
476322f2a2efSAndy Whitcroft		}
476413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4765c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4766c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
47673705ce5bSJoe Perches			if (ERROR("SPACING",
47683705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
47693705ce5bSJoe Perches			    $fix) {
4770194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47713705ce5bSJoe Perches				    s/\s+\)/\)/;
47723705ce5bSJoe Perches			}
477322f2a2efSAndy Whitcroft		}
477422f2a2efSAndy Whitcroft
4775e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4776e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4777e2826fd0SJoe Perches
4778e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4779ea4acbb1SJoe Perches			my $var = $1;
4780ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4781ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4782ea4acbb1SJoe Perches			    $fix) {
4783ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4784ea4acbb1SJoe Perches			}
4785ea4acbb1SJoe Perches		}
4786ea4acbb1SJoe Perches
4787ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4788ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4789ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4790ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4791ea4acbb1SJoe Perches			my $var = $2;
4792ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4793ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4794ea4acbb1SJoe Perches			    $fix) {
4795ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4796ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4797ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4798ea4acbb1SJoe Perches			}
4799e2826fd0SJoe Perches		}
4800e2826fd0SJoe Perches
480163b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4802a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4803a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
48045b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
480563b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
480663b7c73eSJoe Perches			my $if_stat = $1;
480763b7c73eSJoe Perches			my $test = substr($2, 1, -1);
480863b7c73eSJoe Perches			my $herectx;
480963b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
481063b7c73eSJoe Perches				my $match = $1;
481163b7c73eSJoe Perches				# avoid parentheses around potential macro args
481263b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
481363b7c73eSJoe Perches				if (!defined($herectx)) {
481463b7c73eSJoe Perches					$herectx = $here . "\n";
481563b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
481663b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
481763b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
481863b7c73eSJoe Perches						$herectx .=  $rl . "\n";
481963b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
482063b7c73eSJoe Perches					}
482163b7c73eSJoe Perches				}
482263b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
482363b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
482463b7c73eSJoe Perches			}
482563b7c73eSJoe Perches		}
482663b7c73eSJoe Perches
48270a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
48284a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
48290a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
48303705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
48313705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
48323705ce5bSJoe Perches			    $fix) {
4833194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48343705ce5bSJoe Perches				    s/^(.)\s+/$1/;
48353705ce5bSJoe Perches			}
48360a920b5bSAndy Whitcroft		}
48370a920b5bSAndy Whitcroft
48385b9553abSJoe Perches# return is not a function
4839507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4840c45dcabdSAndy Whitcroft			my $spacing = $1;
48415b57980dSJoe Perches			if ($perl_version_ok &&
48425b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
48435b9553abSJoe Perches				my $value = $1;
48445b9553abSJoe Perches				$value = deparenthesize($value);
48455b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4846000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4847000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
48485b9553abSJoe Perches				}
4849c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4850000d1cc1SJoe Perches				ERROR("SPACING",
4851000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4852c45dcabdSAndy Whitcroft			}
4853c45dcabdSAndy Whitcroft		}
4854507e5141SJoe Perches
4855b43ae21bSJoe Perches# unnecessary return in a void function
4856b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4857b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4858b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4859b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4860b43ae21bSJoe Perches		    $linenr >= 3 &&
4861b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4862b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
48639819cf25SJoe Perches			WARN("RETURN_VOID",
4864b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
48659819cf25SJoe Perches               }
48669819cf25SJoe Perches
4867189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
48685b57980dSJoe Perches		if ($perl_version_ok &&
4869189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4870189248d8SJoe Perches			my $openparens = $1;
4871189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4872189248d8SJoe Perches			my $msg = "";
4873189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4874189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4875189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4876189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4877189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4878189248d8SJoe Perches			}
4879189248d8SJoe Perches		}
4880189248d8SJoe Perches
4881c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4882c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4883c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4884c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
48855b57980dSJoe Perches		if ($perl_version_ok &&
4886c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4887c5595fa2SJoe Perches			my $lead = $1;
4888c5595fa2SJoe Perches			my $const = $2;
4889c5595fa2SJoe Perches			my $comp = $3;
4890c5595fa2SJoe Perches			my $to = $4;
4891c5595fa2SJoe Perches			my $newcomp = $comp;
4892f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4893c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4894c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4895c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4896c5595fa2SJoe Perches			    $fix) {
4897c5595fa2SJoe Perches				if ($comp eq "<") {
4898c5595fa2SJoe Perches					$newcomp = ">";
4899c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4900c5595fa2SJoe Perches					$newcomp = ">=";
4901c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4902c5595fa2SJoe Perches					$newcomp = "<";
4903c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4904c5595fa2SJoe Perches					$newcomp = "<=";
4905c5595fa2SJoe Perches				}
4906c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4907c5595fa2SJoe Perches			}
4908c5595fa2SJoe Perches		}
4909c5595fa2SJoe Perches
4910f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4911f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
491253a3c448SAndy Whitcroft			my $name = $1;
491353a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4914000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4915f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
491653a3c448SAndy Whitcroft			}
491753a3c448SAndy Whitcroft		}
4918c45dcabdSAndy Whitcroft
49190a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
49204a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
49213705ce5bSJoe Perches			if (ERROR("SPACING",
49223705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
49233705ce5bSJoe Perches			    $fix) {
4924194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49253705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
49263705ce5bSJoe Perches			}
49270a920b5bSAndy Whitcroft		}
49280a920b5bSAndy Whitcroft
4929f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4930f5fe35ddSAndy Whitcroft# statements after the conditional.
4931170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
49323e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
49333e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
49343e469cdcSAndy Whitcroft					if (!defined $stat);
4935170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4936170d3a22SAndy Whitcroft						$remain_next, $off_next);
4937170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4938170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4939170d3a22SAndy Whitcroft
4940170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4941170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4942170d3a22SAndy Whitcroft				# then count those as offsets.
4943170d3a22SAndy Whitcroft				my ($whitespace) =
4944170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4945170d3a22SAndy Whitcroft				my $offset =
4946170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4947170d3a22SAndy Whitcroft
4948170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4949170d3a22SAndy Whitcroft								$offset} = 1;
4950170d3a22SAndy Whitcroft			}
4951170d3a22SAndy Whitcroft		}
4952170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4953c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4954170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4955171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
49568905a67cSAndy Whitcroft
4957b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4958000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4959000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
49608905a67cSAndy Whitcroft			}
49618905a67cSAndy Whitcroft
49628905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
49638905a67cSAndy Whitcroft			# conditional.
4964773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
49658905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
496613214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
496753210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
496853210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4969773647a0SAndy Whitcroft			{
4970bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4971bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4972bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
497342bdf74cSHidetoshi Seto				my $stat_real = '';
4974bb44ad39SAndy Whitcroft
497542bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
497642bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4977bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4978bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4979bb44ad39SAndy Whitcroft				}
4980bb44ad39SAndy Whitcroft
4981000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4982000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
49838905a67cSAndy Whitcroft			}
49848905a67cSAndy Whitcroft		}
49858905a67cSAndy Whitcroft
498613214adfSAndy Whitcroft# Check for bitwise tests written as boolean
498713214adfSAndy Whitcroft		if ($line =~ /
498813214adfSAndy Whitcroft			(?:
498913214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
499013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
499113214adfSAndy Whitcroft				(?:\&\&|\|\|)
499213214adfSAndy Whitcroft			|
499313214adfSAndy Whitcroft				(?:\&\&|\|\|)
499413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
499513214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
499613214adfSAndy Whitcroft			)/x)
499713214adfSAndy Whitcroft		{
4998000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4999000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
500013214adfSAndy Whitcroft		}
500113214adfSAndy Whitcroft
50028905a67cSAndy Whitcroft# if and else should not have general statements after it
500313214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
500413214adfSAndy Whitcroft			my $s = $1;
500513214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
500613214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5007000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5008000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
50090a920b5bSAndy Whitcroft			}
501013214adfSAndy Whitcroft		}
501139667782SAndy Whitcroft# if should not continue a brace
501239667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5013000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5014048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
501539667782SAndy Whitcroft				$herecurr);
501639667782SAndy Whitcroft		}
5017a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5018a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5019a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
50203fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5021a1080bf8SAndy Whitcroft			\s*return\s+
5022a1080bf8SAndy Whitcroft		    )/xg)
5023a1080bf8SAndy Whitcroft		{
5024000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5025000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5026a1080bf8SAndy Whitcroft		}
50270a920b5bSAndy Whitcroft
50280a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
50290a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
50308b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
50310a920b5bSAndy Whitcroft		    $previndent == $indent) {
50328b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
50338b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
50348b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50358b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
50368b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
50378b8856f4SJoe Perches				my $fixedline = $prevrawline;
50388b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
50398b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
50408b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50418b8856f4SJoe Perches				}
50428b8856f4SJoe Perches				$fixedline = $rawline;
50438b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
50448b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
50458b8856f4SJoe Perches			}
50460a920b5bSAndy Whitcroft		}
50470a920b5bSAndy Whitcroft
50488b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5049c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5050c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5051c2fdda0dSAndy Whitcroft
5052c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5053c2fdda0dSAndy Whitcroft			# conditional.
5054773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5055c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5056c2fdda0dSAndy Whitcroft
5057c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
50588b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
50598b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
50608b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50618b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
50628b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
50638b8856f4SJoe Perches					my $fixedline = $prevrawline;
50648b8856f4SJoe Perches					my $trailing = $rawline;
50658b8856f4SJoe Perches					$trailing =~ s/^\+//;
50668b8856f4SJoe Perches					$trailing = trim($trailing);
50678b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
50688b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50698b8856f4SJoe Perches				}
5070c2fdda0dSAndy Whitcroft			}
5071c2fdda0dSAndy Whitcroft		}
5072c2fdda0dSAndy Whitcroft
507395e2c602SJoe Perches#Specific variable tests
5074323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5075323c1260SJoe Perches			my $var = $1;
507695e2c602SJoe Perches
507795e2c602SJoe Perches#CamelCase
5078807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5079be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
508022735ce8SJoe Perches#Ignore Page<foo> variants
5081807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5082d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5083d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5084d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5085f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5086f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
50877e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
50887e781f67SJoe Perches					my $word = $1;
50897e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5090d8b07710SJoe Perches					if ($check) {
5091d8b07710SJoe Perches						seed_camelcase_includes();
5092d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5093d8b07710SJoe Perches							seed_camelcase_file($realfile);
5094d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5095d8b07710SJoe Perches						}
5096d8b07710SJoe Perches					}
50977e781f67SJoe Perches					if (!defined $camelcase{$word}) {
50987e781f67SJoe Perches						$camelcase{$word} = 1;
5099be79794bSJoe Perches						CHK("CAMELCASE",
51007e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
51017e781f67SJoe Perches					}
5102323c1260SJoe Perches				}
5103323c1260SJoe Perches			}
51043445686aSJoe Perches		}
51050a920b5bSAndy Whitcroft
51060a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5107d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5108d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5109d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5110d5e616fcSJoe Perches			    $fix) {
5111194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5112d5e616fcSJoe Perches			}
51130a920b5bSAndy Whitcroft		}
51140a920b5bSAndy Whitcroft
51150e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
51160e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5117c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5118e09dec48SAndy Whitcroft			my $file = "$1.h";
5119e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5120e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5121e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
51227840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5123c45dcabdSAndy Whitcroft			{
51240e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
51250e212e0aSFabian Frederick				if ($asminclude > 0) {
5126e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5127000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5128000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5129e09dec48SAndy Whitcroft					} else {
5130000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5131000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5132e09dec48SAndy Whitcroft					}
51330a920b5bSAndy Whitcroft				}
51340a920b5bSAndy Whitcroft			}
51350e212e0aSFabian Frederick		}
51360a920b5bSAndy Whitcroft
5137653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5138653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5139cf655043SAndy Whitcroft# in a known good container
5140b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5141b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5142d8aaf121SAndy Whitcroft			my $ln = $linenr;
5143d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5144c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5145c45dcabdSAndy Whitcroft			my $ctx = '';
514608a2843eSJoe Perches			my $has_flow_statement = 0;
514708a2843eSJoe Perches			my $has_arg_concat = 0;
5148c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5149f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5150f74bd194SAndy Whitcroft			$ctx = $dstat;
5151c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5152a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5153c45dcabdSAndy Whitcroft
515408a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
515562e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
515608a2843eSJoe Perches
5157f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5158f59b64bfSJoe Perches			my $define_args = $1;
5159f59b64bfSJoe Perches			my $define_stmt = $dstat;
5160f59b64bfSJoe Perches			my @def_args = ();
5161f59b64bfSJoe Perches
5162f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5163f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5164f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
51658c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5166f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5167f59b64bfSJoe Perches			}
5168f59b64bfSJoe Perches
5169292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5170c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5171c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5172c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5173c45dcabdSAndy Whitcroft
5174c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5175bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5176bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
51776b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5178bf30d6edSAndy Whitcroft			{
5179c45dcabdSAndy Whitcroft			}
5180c45dcabdSAndy Whitcroft
5181342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
518233acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
518333acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5184e45bab8eSAndy Whitcroft			{
5185e45bab8eSAndy Whitcroft			}
5186e45bab8eSAndy Whitcroft
518742e15293SJoe Perches			# Make asm volatile uses seem like a generic function
518842e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
518942e15293SJoe Perches
5190c45dcabdSAndy Whitcroft			my $exceptions = qr{
5191c45dcabdSAndy Whitcroft				$Declare|
5192c45dcabdSAndy Whitcroft				module_param_named|
5193a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5194c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5195c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5196383099fdSAndy Whitcroft				__typeof__\(|
519722fd2d3eSStefani Seibold				union|
519822fd2d3eSStefani Seibold				struct|
5199ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
52006b10df42SVladimir Zapolskiy				^\"|\"$|
52016b10df42SVladimir Zapolskiy				^\[
5202c45dcabdSAndy Whitcroft			}x;
52035eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5204f59b64bfSJoe Perches
5205f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5206f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5207e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5208f59b64bfSJoe Perches
5209f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5210f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5211f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
52123cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5213356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5214f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5215f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5216e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
521772f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5218f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5219f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5220f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
52214e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5222f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5223c45dcabdSAndy Whitcroft			{
5224e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5225e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5226e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5227e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5228f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5229f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5230f74bd194SAndy Whitcroft				} else {
5231000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5232388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5233d8aaf121SAndy Whitcroft				}
5234f59b64bfSJoe Perches
5235f59b64bfSJoe Perches			}
52365207649bSJoe Perches
52375207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
52385207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
52395207649bSJoe Perches			my $first = 1;
52405207649bSJoe Perches			$define_stmt = "";
52415207649bSJoe Perches			foreach my $l (@stmt_array) {
52425207649bSJoe Perches				$l =~ s/\\$//;
52435207649bSJoe Perches				if ($first) {
52445207649bSJoe Perches					$define_stmt = $l;
52455207649bSJoe Perches					$first = 0;
52465207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
52475207649bSJoe Perches					$define_stmt .= substr($l, 1);
52485207649bSJoe Perches				}
52495207649bSJoe Perches			}
52505207649bSJoe Perches			$define_stmt =~ s/$;//g;
52515207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
52525207649bSJoe Perches			$define_stmt = trim($define_stmt);
52535207649bSJoe Perches
5254f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5255f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5256f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
52579192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
52587fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
52596dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
52607fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
52617fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5262d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5263f59b64bfSJoe Perches				if ($use_cnt > 1) {
5264f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5265f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5266f59b64bfSJoe Perches				    }
52679192d41aSJoe Perches# check if any macro arguments may have other precedence issues
52687fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
52699192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
52709192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
52719192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
52729192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
52739192d41aSJoe Perches				}
52740a920b5bSAndy Whitcroft			}
52755023d347SJoe Perches
527608a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
527708a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
527808a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
527908a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5280e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
528108a2843eSJoe Perches
528208a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
528308a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
528408a2843eSJoe Perches			}
528508a2843eSJoe Perches
5286481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
52875023d347SJoe Perches
52885023d347SJoe Perches		} else {
52895023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5290481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5291481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
52925023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
52935023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
52945023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
52955023d347SJoe Perches			}
5296653d4876SAndy Whitcroft		}
52970a920b5bSAndy Whitcroft
5298b13edf7fSJoe Perches# do {} while (0) macro tests:
5299b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5300b13edf7fSJoe Perches# macro should not end with a semicolon
53015b57980dSJoe Perches		if ($perl_version_ok &&
5302b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5303b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5304b13edf7fSJoe Perches			my $ln = $linenr;
5305b13edf7fSJoe Perches			my $cnt = $realcnt;
5306b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5307b13edf7fSJoe Perches			my $ctx = '';
5308b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5309b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5310b13edf7fSJoe Perches			$ctx = $dstat;
5311b13edf7fSJoe Perches
5312b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
53131b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5314b13edf7fSJoe Perches
5315b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5316b13edf7fSJoe Perches				my $stmts = $2;
5317b13edf7fSJoe Perches				my $semis = $3;
5318b13edf7fSJoe Perches
5319b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5320b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5321e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5322b13edf7fSJoe Perches
5323ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5324ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5325b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5326b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5327b13edf7fSJoe Perches				}
5328b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5329b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5330b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5331b13edf7fSJoe Perches				}
5332f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5333f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5334f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5335e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5336f5ef95b1SJoe Perches
5337f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5338f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5339b13edf7fSJoe Perches			}
5340b13edf7fSJoe Perches		}
5341b13edf7fSJoe Perches
5342f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
534313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
534413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5345cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
534613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5347cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5348cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5349aad4f614SJoe Perches				my @allowed = ();
5350aad4f614SJoe Perches				my $allow = 0;
535113214adfSAndy Whitcroft				my $seen = 0;
5352773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5353cf655043SAndy Whitcroft				my $ln = $linenr - 1;
535413214adfSAndy Whitcroft				for my $chunk (@chunks) {
535513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
535613214adfSAndy Whitcroft
5357773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5358773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5359773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5360773647a0SAndy Whitcroft
5361aad4f614SJoe Perches					$allowed[$allow] = 0;
5362773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5363773647a0SAndy Whitcroft
5364773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5365773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5366773647a0SAndy Whitcroft
5367773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5368cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5369cf655043SAndy Whitcroft
5370773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
537113214adfSAndy Whitcroft
537213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
537313214adfSAndy Whitcroft
5374aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5375cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5376cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5377aad4f614SJoe Perches						$allowed[$allow] = 1;
537813214adfSAndy Whitcroft					}
537913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5380cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5381aad4f614SJoe Perches						$allowed[$allow] = 1;
538213214adfSAndy Whitcroft					}
5383cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5384cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5385aad4f614SJoe Perches						$allowed[$allow] = 1;
538613214adfSAndy Whitcroft					}
5387aad4f614SJoe Perches					$allow++;
538813214adfSAndy Whitcroft				}
5389aad4f614SJoe Perches				if ($seen) {
5390aad4f614SJoe Perches					my $sum_allowed = 0;
5391aad4f614SJoe Perches					foreach (@allowed) {
5392aad4f614SJoe Perches						$sum_allowed += $_;
5393aad4f614SJoe Perches					}
5394aad4f614SJoe Perches					if ($sum_allowed == 0) {
5395000d1cc1SJoe Perches						WARN("BRACES",
5396000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5397aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5398aad4f614SJoe Perches						 $seen != $allow) {
5399aad4f614SJoe Perches						CHK("BRACES",
5400aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5401aad4f614SJoe Perches					}
540213214adfSAndy Whitcroft				}
540313214adfSAndy Whitcroft			}
540413214adfSAndy Whitcroft		}
5405773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
540613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5407cf655043SAndy Whitcroft			my $allowed = 0;
5408f0a594c1SAndy Whitcroft
5409cf655043SAndy Whitcroft			# Check the pre-context.
5410cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5411cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5412cf655043SAndy Whitcroft				$allowed = 1;
5413f0a594c1SAndy Whitcroft			}
5414773647a0SAndy Whitcroft
5415773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5416773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5417773647a0SAndy Whitcroft
5418cf655043SAndy Whitcroft			# Check the condition.
5419cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5420773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5421cf655043SAndy Whitcroft			if (defined $cond) {
5422773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5423cf655043SAndy Whitcroft			}
5424cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5425cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5426cf655043SAndy Whitcroft				$allowed = 1;
5427cf655043SAndy Whitcroft			}
5428cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5429cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5430cf655043SAndy Whitcroft				$allowed = 1;
5431cf655043SAndy Whitcroft			}
5432cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5433cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5434cf655043SAndy Whitcroft				$allowed = 1;
5435cf655043SAndy Whitcroft			}
5436cf655043SAndy Whitcroft			# Check the post-context.
5437cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5438cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5439cf655043SAndy Whitcroft				if (defined $cond) {
5440773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5441cf655043SAndy Whitcroft				}
5442cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5443cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5444cf655043SAndy Whitcroft					$allowed = 1;
5445cf655043SAndy Whitcroft				}
5446cf655043SAndy Whitcroft			}
5447cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5448f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5449e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5450cf655043SAndy Whitcroft
5451000d1cc1SJoe Perches				WARN("BRACES",
5452000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5453f0a594c1SAndy Whitcroft			}
5454f0a594c1SAndy Whitcroft		}
5455f0a594c1SAndy Whitcroft
5456e4c5babdSJoe Perches# check for single line unbalanced braces
545795330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
545895330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5459e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5460e4c5babdSJoe Perches		}
5461e4c5babdSJoe Perches
54620979ae66SJoe Perches# check for unnecessary blank lines around braces
546377b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5464f8e58219SJoe Perches			if (CHK("BRACES",
5465f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5466f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5467f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5468f8e58219SJoe Perches			}
54690979ae66SJoe Perches		}
547077b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5471f8e58219SJoe Perches			if (CHK("BRACES",
5472f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5473f8e58219SJoe Perches			    $fix) {
5474f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5475f8e58219SJoe Perches			}
54760979ae66SJoe Perches		}
54770979ae66SJoe Perches
54784a0df2efSAndy Whitcroft# no volatiles please
54796c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
54806c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5481000d1cc1SJoe Perches			WARN("VOLATILE",
54828c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
54834a0df2efSAndy Whitcroft		}
54844a0df2efSAndy Whitcroft
54855e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
54865e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
54875e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
54885e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
548933acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
54905e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
54915e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
54925e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
54935e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
54945e4f6ba5SJoe Perches				     $fix &&
54955e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
54965e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
54975e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
54985e4f6ba5SJoe Perches				my $comma_close = "";
54995e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
55005e4f6ba5SJoe Perches					$comma_close = $1;
55015e4f6ba5SJoe Perches				}
55025e4f6ba5SJoe Perches
55035e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
55045e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
55055e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
55065e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
55075e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
55085e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
55095e4f6ba5SJoe Perches				$fixedline = $rawline;
55105e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
55115e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
55125e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
55135e4f6ba5SJoe Perches				}
55145e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
55155e4f6ba5SJoe Perches			}
55165e4f6ba5SJoe Perches		}
55175e4f6ba5SJoe Perches
55185e4f6ba5SJoe Perches# check for missing a space in a string concatenation
55195e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
55205e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
55215e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
55225e4f6ba5SJoe Perches		}
55235e4f6ba5SJoe Perches
552477cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5525e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5526e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5527e4b7d309SJoe Perches# function declarations
552877cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
552977cb8546SJoe Perches		    defined($context_function) &&
5530e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5531e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
553277cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5533e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
553477cb8546SJoe Perches		}
553577cb8546SJoe Perches
55365e4f6ba5SJoe Perches# check for spaces before a quoted newline
55375e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
55385e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
55395e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
55405e4f6ba5SJoe Perches			    $fix) {
55415e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
55425e4f6ba5SJoe Perches			}
55435e4f6ba5SJoe Perches
55445e4f6ba5SJoe Perches		}
55455e4f6ba5SJoe Perches
5546f17dba4fSJoe Perches# concatenated string without spaces between elements
554779682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
554879682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
554979682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
555079682c0cSJoe Perches			    $fix) {
555179682c0cSJoe Perches				while ($line =~ /($String)/g) {
555279682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
555379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
555479682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
555579682c0cSJoe Perches				}
555679682c0cSJoe Perches			}
5557f17dba4fSJoe Perches		}
5558f17dba4fSJoe Perches
555990ad30e5SJoe Perches# uncoalesced string fragments
556033acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
556179682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
556279682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
556379682c0cSJoe Perches			    $fix) {
556479682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
556579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
556679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
556779682c0cSJoe Perches				}
556879682c0cSJoe Perches			}
556990ad30e5SJoe Perches		}
557090ad30e5SJoe Perches
5571522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5572522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5573522b837cSAlexey Dobriyan		my $show_Z = 1;
55745e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5575522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
55765e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5577522b837cSAlexey Dobriyan			# check for %L
5578522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
55795e4f6ba5SJoe Perches				WARN("PRINTF_L",
5580522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5581522b837cSAlexey Dobriyan				$show_L = 0;
55825e4f6ba5SJoe Perches			}
5583522b837cSAlexey Dobriyan			# check for %Z
5584522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5585522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5586522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5587522b837cSAlexey Dobriyan				$show_Z = 0;
5588522b837cSAlexey Dobriyan			}
5589522b837cSAlexey Dobriyan			# check for 0x<decimal>
5590522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5591522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
55926e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
55936e300757SJoe Perches			}
55945e4f6ba5SJoe Perches		}
55955e4f6ba5SJoe Perches
55965e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
55973f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
55985e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
55995e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
56005e4f6ba5SJoe Perches		}
56015e4f6ba5SJoe Perches
560200df344fSAndy Whitcroft# warn about #if 0
5603c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
560460f89010SPrakruthi Deepak Heragu			WARN("IF_0",
560560f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
560660f89010SPrakruthi Deepak Heragu		}
560760f89010SPrakruthi Deepak Heragu
560860f89010SPrakruthi Deepak Heragu# warn about #if 1
560960f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
561060f89010SPrakruthi Deepak Heragu			WARN("IF_1",
561160f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
56124a0df2efSAndy Whitcroft		}
56134a0df2efSAndy Whitcroft
561403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
561503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5616100425deSJoe Perches			my $tested = quotemeta($1);
5617100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5618100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5619100425deSJoe Perches				my $func = $1;
5620100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5621100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5622100425deSJoe Perches				    $fix) {
5623100425deSJoe Perches					my $do_fix = 1;
5624100425deSJoe Perches					my $leading_tabs = "";
5625100425deSJoe Perches					my $new_leading_tabs = "";
5626100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5627100425deSJoe Perches						$leading_tabs = $1;
5628100425deSJoe Perches					} else {
5629100425deSJoe Perches						$do_fix = 0;
5630100425deSJoe Perches					}
5631100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5632100425deSJoe Perches						$new_leading_tabs = $1;
5633100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5634100425deSJoe Perches							$do_fix = 0;
5635100425deSJoe Perches						}
5636100425deSJoe Perches					} else {
5637100425deSJoe Perches						$do_fix = 0;
5638100425deSJoe Perches					}
5639100425deSJoe Perches					if ($do_fix) {
5640100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5641100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5642100425deSJoe Perches					}
5643100425deSJoe Perches				}
56444c432a8fSGreg Kroah-Hartman			}
56454c432a8fSGreg Kroah-Hartman		}
5646f0a594c1SAndy Whitcroft
5647ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5648ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5649ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5650ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5651ebfdc409SJoe Perches		    $linenr > 3) {
5652ebfdc409SJoe Perches			my $testval = $2;
5653ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5654ebfdc409SJoe Perches
5655ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5656ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5657ebfdc409SJoe Perches
5658e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5659e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5660ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5661ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5662ebfdc409SJoe Perches			}
5663ebfdc409SJoe Perches		}
5664ebfdc409SJoe Perches
5665f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5666dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5667f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5668f78d98f6SJoe Perches			my $level = $1;
5669f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5670f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5671f78d98f6SJoe Perches			    $fix) {
5672f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5673f78d98f6SJoe Perches			}
5674f78d98f6SJoe Perches		}
5675f78d98f6SJoe Perches
567645c55e92SJoe Perches# check for logging continuations
567745c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
567845c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
567945c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
568045c55e92SJoe Perches		}
568145c55e92SJoe Perches
5682abb08a53SJoe Perches# check for mask then right shift without a parentheses
56835b57980dSJoe Perches		if ($perl_version_ok &&
5684abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5685abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5686abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5687abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5688abb08a53SJoe Perches		}
5689abb08a53SJoe Perches
5690b75ac618SJoe Perches# check for pointer comparisons to NULL
56915b57980dSJoe Perches		if ($perl_version_ok) {
5692b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5693b75ac618SJoe Perches				my $val = $1;
5694b75ac618SJoe Perches				my $equal = "!";
5695b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5696b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5697b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5698b75ac618SJoe Perches					    $fix) {
5699b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5700b75ac618SJoe Perches				}
5701b75ac618SJoe Perches			}
5702b75ac618SJoe Perches		}
5703b75ac618SJoe Perches
57048716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
57058716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
57068716de38SJoe Perches			my $attr = $1;
57078716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
57088716de38SJoe Perches				my $ptr = $1;
57098716de38SJoe Perches				my $var = $2;
57108716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
57118716de38SJoe Perches				      ERROR("MISPLACED_INIT",
57128716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
57138716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
57148716de38SJoe Perches				      WARN("MISPLACED_INIT",
57158716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
57168716de38SJoe Perches				    $fix) {
5717194f66fcSJoe 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;
57188716de38SJoe Perches				}
57198716de38SJoe Perches			}
57208716de38SJoe Perches		}
57218716de38SJoe Perches
5722e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5723e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5724e970b884SJoe Perches			my $attr = $1;
5725e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5726e970b884SJoe Perches			my $attr_prefix = $1;
5727e970b884SJoe Perches			my $attr_type = $2;
5728e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5729e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5730e970b884SJoe Perches			    $fix) {
5731194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5732e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5733e970b884SJoe Perches			}
5734e970b884SJoe Perches		}
5735e970b884SJoe Perches
5736e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5737e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5738e970b884SJoe Perches			my $attr = $1;
5739e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5740e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5741e970b884SJoe Perches			    $fix) {
5742194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5743e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5744e970b884SJoe Perches				$lead = rtrim($1);
5745e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5746e970b884SJoe Perches				$lead = "${lead}const ";
5747194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5748e970b884SJoe Perches			}
5749e970b884SJoe Perches		}
5750e970b884SJoe Perches
5751c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5752c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5753c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5754c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5755c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5756c17893c7SJoe Perches			    $fix) {
5757c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5758c17893c7SJoe Perches			}
5759c17893c7SJoe Perches		}
5760c17893c7SJoe Perches
5761fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5762fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5763fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5764fbdb8138SJoe Perches			my $constant_func = $1;
5765fbdb8138SJoe Perches			my $func = $constant_func;
5766fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5767fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5768fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5769fbdb8138SJoe Perches			    $fix) {
5770194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5771fbdb8138SJoe Perches			}
5772fbdb8138SJoe Perches		}
5773fbdb8138SJoe Perches
57741a15a250SPatrick Pannuto# prefer usleep_range over udelay
577537581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
577643c1d77cSJoe Perches			my $delay = $1;
57771a15a250SPatrick Pannuto			# ignore udelay's < 10, however
577843c1d77cSJoe Perches			if (! ($delay < 10) ) {
5779000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5780458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
578143c1d77cSJoe Perches			}
578243c1d77cSJoe Perches			if ($delay > 2000) {
578343c1d77cSJoe Perches				WARN("LONG_UDELAY",
578443c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
57851a15a250SPatrick Pannuto			}
57861a15a250SPatrick Pannuto		}
57871a15a250SPatrick Pannuto
578809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
578909ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
579009ef8725SPatrick Pannuto			if ($1 < 20) {
5791000d1cc1SJoe Perches				WARN("MSLEEP",
5792458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
579309ef8725SPatrick Pannuto			}
579409ef8725SPatrick Pannuto		}
579509ef8725SPatrick Pannuto
579636ec1939SJoe Perches# check for comparisons of jiffies
579736ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
579836ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
579936ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
580036ec1939SJoe Perches		}
580136ec1939SJoe Perches
58029d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
58039d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
58049d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
58059d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
58069d7a34a5SJoe Perches		}
58079d7a34a5SJoe Perches
580800df344fSAndy Whitcroft# warn about #ifdefs in C files
5809c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
581000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
581100df344fSAndy Whitcroft#			print "$herecurr";
581200df344fSAndy Whitcroft#			$clean = 0;
581300df344fSAndy Whitcroft#		}
581400df344fSAndy Whitcroft
581522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5816c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
58173705ce5bSJoe Perches			if (ERROR("SPACING",
58183705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
58193705ce5bSJoe Perches			    $fix) {
5820194f66fcSJoe Perches				$fixed[$fixlinenr] =~
58213705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
58223705ce5bSJoe Perches			}
58233705ce5bSJoe Perches
582422f2a2efSAndy Whitcroft		}
582522f2a2efSAndy Whitcroft
58264a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5827171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5828171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
58294a0df2efSAndy Whitcroft			my $which = $1;
58304a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5831000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5832000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
58334a0df2efSAndy Whitcroft			}
58344a0df2efSAndy Whitcroft		}
58354a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5836402c2553SMichael S. Tsirkin
5837402c2553SMichael S. Tsirkin		my $barriers = qr{
5838402c2553SMichael S. Tsirkin			mb|
5839402c2553SMichael S. Tsirkin			rmb|
5840402c2553SMichael S. Tsirkin			wmb|
5841402c2553SMichael S. Tsirkin			read_barrier_depends
5842402c2553SMichael S. Tsirkin		}x;
5843402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5844402c2553SMichael S. Tsirkin			mb__before_atomic|
5845402c2553SMichael S. Tsirkin			mb__after_atomic|
5846402c2553SMichael S. Tsirkin			store_release|
5847402c2553SMichael S. Tsirkin			load_acquire|
5848402c2553SMichael S. Tsirkin			store_mb|
5849402c2553SMichael S. Tsirkin			(?:$barriers)
5850402c2553SMichael S. Tsirkin		}x;
5851402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5852402c2553SMichael S. Tsirkin			(?:$barriers)|
585343e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
585443e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5855402c2553SMichael S. Tsirkin		}x;
5856402c2553SMichael S. Tsirkin
5857402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
58584a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5859c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5860000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
58614a0df2efSAndy Whitcroft			}
58624a0df2efSAndy Whitcroft		}
58633ad81779SPaul E. McKenney
5864f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5865f4073b0fSMichael S. Tsirkin
5866f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5867f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5868f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5869f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5870f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5871f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5872f4073b0fSMichael S. Tsirkin		}
5873f4073b0fSMichael S. Tsirkin
5874cb426e99SJoe Perches# check for waitqueue_active without a comment.
5875cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5876cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5877cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5878cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5879cb426e99SJoe Perches			}
5880cb426e99SJoe Perches		}
58813ad81779SPaul E. McKenney
588291db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
588391db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
588491db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
588591db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
588691db2592SPaul E. McKenney		}
588791db2592SPaul E. McKenney
58884a0df2efSAndy Whitcroft# check of hardware specific defines
5889c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5890000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5891000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
58920a920b5bSAndy Whitcroft		}
5893653d4876SAndy Whitcroft
5894596ed45bSJoe Perches# check that the storage class is not after a type
5895596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5896000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5897596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5898596ed45bSJoe Perches		}
5899596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5900596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5901596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5902596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5903596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5904596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5905596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5906d4977c78STobias Klauser		}
5907d4977c78STobias Klauser
5908de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5909de7d4f0eSAndy Whitcroft# storage class and type.
59109c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
59119c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5912000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5913000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5914de7d4f0eSAndy Whitcroft		}
5915de7d4f0eSAndy Whitcroft
59168905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
59172b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59182b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5919d5e616fcSJoe Perches			if (WARN("INLINE",
5920d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5921d5e616fcSJoe Perches			    $fix) {
5922194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5923d5e616fcSJoe Perches
5924d5e616fcSJoe Perches			}
59258905a67cSAndy Whitcroft		}
59268905a67cSAndy Whitcroft
59273d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
59282b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59292b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5930000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5931000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
59323d130fd0SJoe Perches		}
59333d130fd0SJoe Perches
593439b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
59352b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59362b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5937000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5938000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
593939b7e287SJoe Perches		}
594039b7e287SJoe Perches
5941462811d9SJoe Perches# Check for __attribute__ section, prefer __section
5942462811d9SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5943462811d9SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5944462811d9SJoe Perches			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5945462811d9SJoe Perches			my $new = substr($old, 1, -1);
5946462811d9SJoe Perches			if (WARN("PREFER_SECTION",
5947462811d9SJoe Perches				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5948462811d9SJoe Perches			    $fix) {
5949462811d9SJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5950462811d9SJoe Perches			}
5951462811d9SJoe Perches		}
5952462811d9SJoe Perches
59535f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
59542b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59552b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5956d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5957d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5958d5e616fcSJoe Perches			    $fix) {
5959194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5960d5e616fcSJoe Perches
5961d5e616fcSJoe Perches			}
59625f14d3bdSJoe Perches		}
59635f14d3bdSJoe Perches
59646061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
59652b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59662b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5967d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5968d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5969d5e616fcSJoe Perches			    $fix) {
5970194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5971d5e616fcSJoe Perches			}
59726061d949SJoe Perches		}
59736061d949SJoe Perches
5974619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
59755b57980dSJoe Perches		if ($perl_version_ok &&
5976619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5977619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5978619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5979619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5980619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5981619a908aSJoe Perches		}
5982619a908aSJoe Perches
5983fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5984e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5985fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5986e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5987e6176fa4SJoe Perches			my $type = $1;
5988e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5989e6176fa4SJoe Perches				$type = $1;
5990e6176fa4SJoe Perches				my $kernel_type = 'u';
5991e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5992e6176fa4SJoe Perches				$type =~ /(\d+)/;
5993e6176fa4SJoe Perches				$kernel_type .= $1;
5994e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5995e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5996e6176fa4SJoe Perches				    $fix) {
5997e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5998e6176fa4SJoe Perches				}
5999e6176fa4SJoe Perches			}
6000e6176fa4SJoe Perches		}
6001e6176fa4SJoe Perches
6002938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6003938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6004938224b5SJoe Perches			my $cast = $1;
6005938224b5SJoe Perches			my $const = $2;
6006938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6007938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6008938224b5SJoe Perches			    $fix) {
6009938224b5SJoe Perches				my $suffix = "";
6010938224b5SJoe Perches				my $newconst = $const;
6011938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6012938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6013938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6014938224b5SJoe Perches					$suffix .= 'LL';
6015938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6016938224b5SJoe Perches					$suffix .= 'L';
6017938224b5SJoe Perches				}
6018938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6019938224b5SJoe Perches			}
6020938224b5SJoe Perches		}
6021938224b5SJoe Perches
60228f53a9b8SJoe Perches# check for sizeof(&)
60238f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6024000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6025000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
60268f53a9b8SJoe Perches		}
60278f53a9b8SJoe Perches
602866c80b60SJoe Perches# check for sizeof without parenthesis
602966c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6030d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6031d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6032d5e616fcSJoe Perches			    $fix) {
6033194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6034d5e616fcSJoe Perches			}
603566c80b60SJoe Perches		}
603666c80b60SJoe Perches
603788982feaSJoe Perches# check for struct spinlock declarations
603888982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
603988982feaSJoe Perches			WARN("USE_SPINLOCK_T",
604088982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
604188982feaSJoe Perches		}
604288982feaSJoe Perches
6043a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
604406668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6045a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6046caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6047caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6048d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6049d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6050d5e616fcSJoe Perches				    $fix) {
6051194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6052d5e616fcSJoe Perches				}
6053a6962d72SJoe Perches			}
6054a6962d72SJoe Perches		}
6055a6962d72SJoe Perches
60560b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
60575b57980dSJoe Perches		if ($perl_version_ok &&
60580b523769SJoe Perches		    defined $stat &&
60590b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
60600b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6061e3c6bc95STobin C. Harding			my $stat_real;
6062e3c6bc95STobin C. Harding
60630b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
60640b523769SJoe Perches			$lc = $lc + $linenr;
60650b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6066ffe07513SJoe Perches				my $specifier;
6067ffe07513SJoe Perches				my $extension;
60683bd32d6aSSakari Ailus				my $qualifier;
6069ffe07513SJoe Perches				my $bad_specifier = "";
60700b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
60710b523769SJoe Perches				$fmt =~ s/%%//g;
6072e3c6bc95STobin C. Harding
60733bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6074e3c6bc95STobin C. Harding					$specifier = $1;
6075e3c6bc95STobin C. Harding					$extension = $2;
60763bd32d6aSSakari Ailus					$qualifier = $3;
6077361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
60783bd32d6aSSakari Ailus					    ($extension eq "f" &&
60793bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6080e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
60810b523769SJoe Perches						last;
60820b523769SJoe Perches					}
6083e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6084e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6085e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
60860b523769SJoe Perches						}
6087e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6088e3c6bc95STobin 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");
6089e3c6bc95STobin C. Harding					}
6090e3c6bc95STobin C. Harding				}
6091e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
60922a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
60931df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
60941df7338aSSergey Senozhatsky					my $use = "";
6095e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
60961df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6097e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
60981df7338aSSergey Senozhatsky					}
60992a9f9d85STobin C. Harding
61000b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6101e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6102e3c6bc95STobin C. Harding				}
61030b523769SJoe Perches			}
61040b523769SJoe Perches		}
61050b523769SJoe Perches
6106554e165cSAndy Whitcroft# Check for misused memsets
61075b57980dSJoe Perches		if ($perl_version_ok &&
6108d1fe9c09SJoe Perches		    defined $stat &&
61099e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6110554e165cSAndy Whitcroft
6111d7c76ba7SJoe Perches			my $ms_addr = $2;
6112d1fe9c09SJoe Perches			my $ms_val = $7;
6113d1fe9c09SJoe Perches			my $ms_size = $12;
6114d7c76ba7SJoe Perches
6115554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6116554e165cSAndy Whitcroft				ERROR("MEMSET",
6117d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6118554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6119554e165cSAndy Whitcroft				WARN("MEMSET",
6120d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6121d7c76ba7SJoe Perches			}
6122d7c76ba7SJoe Perches		}
6123d7c76ba7SJoe Perches
612498a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
61255b57980dSJoe Perches#		if ($perl_version_ok &&
6126f333195dSJoe Perches#		    defined $stat &&
6127f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6128f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6129f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6130f333195dSJoe Perches#			    $fix) {
6131f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6132f333195dSJoe Perches#			}
6133f333195dSJoe Perches#		}
613498a9bba5SJoe Perches
6135b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
61365b57980dSJoe Perches#		if ($perl_version_ok &&
6137f333195dSJoe Perches#		    defined $stat &&
6138f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6139f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6140f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6141f333195dSJoe Perches#		}
6142b6117d17SMateusz Kulikowski
61438617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
61448617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
61455b57980dSJoe Perches#		if ($perl_version_ok &&
6146f333195dSJoe Perches#		    defined $stat &&
6147f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6148f333195dSJoe Perches#
6149f333195dSJoe Perches#			my $ms_val = $7;
6150f333195dSJoe Perches#
6151f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6152f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6153f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6154f333195dSJoe Perches#				    $fix) {
6155f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6156f333195dSJoe Perches#				}
6157f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6158f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6159f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6160f333195dSJoe Perches#				    $fix) {
6161f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6162f333195dSJoe Perches#				}
6163f333195dSJoe Perches#			}
6164f333195dSJoe Perches#		}
61658617cd09SMateusz Kulikowski
6166d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
61675b57980dSJoe Perches		if ($perl_version_ok &&
6168d1fe9c09SJoe Perches		    defined $stat &&
6169d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6170d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6171d7c76ba7SJoe Perches				my $call = $1;
6172d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6173d7c76ba7SJoe Perches				my $arg1 = $3;
6174d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6175d1fe9c09SJoe Perches				my $arg2 = $8;
6176d7c76ba7SJoe Perches				my $cast;
6177d7c76ba7SJoe Perches
6178d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6179d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6180d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6181d7c76ba7SJoe Perches					$cast = $cast1;
6182d7c76ba7SJoe Perches				} else {
6183d7c76ba7SJoe Perches					$cast = $cast2;
6184d7c76ba7SJoe Perches				}
6185d7c76ba7SJoe Perches				WARN("MINMAX",
6186d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6187554e165cSAndy Whitcroft			}
6188554e165cSAndy Whitcroft		}
6189554e165cSAndy Whitcroft
61904a273195SJoe Perches# check usleep_range arguments
61915b57980dSJoe Perches		if ($perl_version_ok &&
61924a273195SJoe Perches		    defined $stat &&
61934a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
61944a273195SJoe Perches			my $min = $1;
61954a273195SJoe Perches			my $max = $7;
61964a273195SJoe Perches			if ($min eq $max) {
61974a273195SJoe Perches				WARN("USLEEP_RANGE",
6198458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61994a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
62004a273195SJoe Perches				 $min > $max) {
62014a273195SJoe Perches				WARN("USLEEP_RANGE",
6202458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
62034a273195SJoe Perches			}
62044a273195SJoe Perches		}
62054a273195SJoe Perches
6206823b794cSJoe Perches# check for naked sscanf
62075b57980dSJoe Perches		if ($perl_version_ok &&
6208823b794cSJoe Perches		    defined $stat &&
62096c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6210823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6211823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6212823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6213823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6214823b794cSJoe Perches			$lc = $lc + $linenr;
62152a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6216823b794cSJoe Perches			WARN("NAKED_SSCANF",
6217823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6218823b794cSJoe Perches		}
6219823b794cSJoe Perches
6220afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
62215b57980dSJoe Perches		if ($perl_version_ok &&
6222afc819abSJoe Perches		    defined $stat &&
6223afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6224afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6225afc819abSJoe Perches			$lc = $lc + $linenr;
62262a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6227afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6228afc819abSJoe Perches				my $format = $6;
6229afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6230afc819abSJoe Perches				if ($count == 1 &&
6231afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6232afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6233afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6234afc819abSJoe Perches				}
6235afc819abSJoe Perches			}
6236afc819abSJoe Perches		}
6237afc819abSJoe Perches
623870dc8a48SJoe Perches# check for new externs in .h files.
623970dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
624070dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6241d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
624270dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
624370dc8a48SJoe Perches			    $fix) {
6244194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
624570dc8a48SJoe Perches			}
624670dc8a48SJoe Perches		}
624770dc8a48SJoe Perches
6248de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6249171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6250c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6251171ae1a4SAndy Whitcroft		{
6252c45dcabdSAndy Whitcroft			my $function_name = $1;
6253c45dcabdSAndy Whitcroft			my $paren_space = $2;
6254171ae1a4SAndy Whitcroft
6255171ae1a4SAndy Whitcroft			my $s = $stat;
6256171ae1a4SAndy Whitcroft			if (defined $cond) {
6257171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6258171ae1a4SAndy Whitcroft			}
6259c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6260c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6261c45dcabdSAndy Whitcroft			{
6262000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6263000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6264de7d4f0eSAndy Whitcroft			}
6265de7d4f0eSAndy Whitcroft
6266171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6267000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6268000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6269171ae1a4SAndy Whitcroft			}
62709c9ba34eSAndy Whitcroft
62719c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
62729c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
62739c9ba34eSAndy Whitcroft		{
6274000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6275000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6276171ae1a4SAndy Whitcroft		}
6277171ae1a4SAndy Whitcroft
6278a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6279a0ad7596SJoe Perches		if (defined $stat &&
628025bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6281ca0d8929SJoe Perches		    $1 ne "void") {
6282ca0d8929SJoe Perches			my $args = trim($1);
6283ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6284ca0d8929SJoe Perches				my $arg = trim($1);
6285ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6286ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6287ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6288ca0d8929SJoe Perches				}
6289ca0d8929SJoe Perches			}
6290ca0d8929SJoe Perches		}
6291ca0d8929SJoe Perches
6292a0ad7596SJoe Perches# check for function definitions
62935b57980dSJoe Perches		if ($perl_version_ok &&
6294a0ad7596SJoe Perches		    defined $stat &&
6295a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6296a0ad7596SJoe Perches			$context_function = $1;
6297a0ad7596SJoe Perches
6298a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6299a0ad7596SJoe Perches			my $ok = 0;
6300a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6301a0ad7596SJoe Perches			my $herectx = $here . "\n";
6302a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6303a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6304a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6305a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6306a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6307a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6308a0ad7596SJoe Perches			}
6309a0ad7596SJoe Perches			if (!$ok) {
6310a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6311a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6312a0ad7596SJoe Perches			}
6313a0ad7596SJoe Perches		}
6314a0ad7596SJoe Perches
6315de7d4f0eSAndy Whitcroft# checks for new __setup's
6316de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6317de7d4f0eSAndy Whitcroft			my $name = $1;
6318de7d4f0eSAndy Whitcroft
6319de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6320000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
63218c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6322de7d4f0eSAndy Whitcroft			}
6323653d4876SAndy Whitcroft		}
63249c0ca6f9SAndy Whitcroft
6325e29a70f1SJoe Perches# check for pointless casting of alloc functions
6326e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6327000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6328000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
63299c0ca6f9SAndy Whitcroft		}
633013214adfSAndy Whitcroft
6331a640d25cSJoe Perches# alloc style
6332a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
63335b57980dSJoe Perches		if ($perl_version_ok &&
6334e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6335a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6336a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6337a640d25cSJoe Perches		}
6338a640d25cSJoe Perches
633960a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
63405b57980dSJoe Perches		if ($perl_version_ok &&
63411b4a2ed4SJoe Perches		    defined $stat &&
63421b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
634360a55369SJoe Perches			my $oldfunc = $3;
634460a55369SJoe Perches			my $a1 = $4;
634560a55369SJoe Perches			my $a2 = $10;
634660a55369SJoe Perches			my $newfunc = "kmalloc_array";
634760a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
634860a55369SJoe Perches			my $r1 = $a1;
634960a55369SJoe Perches			my $r2 = $a2;
635060a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
635160a55369SJoe Perches				$r1 = $a2;
635260a55369SJoe Perches				$r2 = $a1;
635360a55369SJoe Perches			}
6354e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6355e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
63561b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6357e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6358e3d95a2aSTobin C. Harding
6359e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
63601b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
63611b4a2ed4SJoe Perches				    $cnt == 1 &&
6362e367455aSJoe Perches				    $fix) {
6363194f66fcSJoe 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;
636460a55369SJoe Perches				}
636560a55369SJoe Perches			}
636660a55369SJoe Perches		}
636760a55369SJoe Perches
6368972fdea2SJoe Perches# check for krealloc arg reuse
63695b57980dSJoe Perches		if ($perl_version_ok &&
63704cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
63714cab63ceSJoe Perches		    $1 eq $3) {
6372972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6373972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6374972fdea2SJoe Perches		}
6375972fdea2SJoe Perches
63765ce59ae0SJoe Perches# check for alloc argument mismatch
63775ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
63785ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
63795ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
63805ce59ae0SJoe Perches		}
63815ce59ae0SJoe Perches
6382caf2a54fSJoe Perches# check for multiple semicolons
6383caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6384d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6385d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6386d5e616fcSJoe Perches			    $fix) {
6387194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6388d5e616fcSJoe Perches			}
6389d1e2ad07SJoe Perches		}
6390d1e2ad07SJoe Perches
6391cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6392cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6393cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
63940ab90191SJoe Perches			my $ull = "";
63950ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
63960ab90191SJoe Perches			if (CHK("BIT_MACRO",
63970ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
63980ab90191SJoe Perches			    $fix) {
63990ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
64000ab90191SJoe Perches			}
64010ab90191SJoe Perches		}
64020ab90191SJoe Perches
64032d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
64042d632745SJoe 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*$/) {
64052d632745SJoe Perches			my $config = $1;
64062d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
64072d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
64082d632745SJoe Perches			    $fix) {
64092d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
64102d632745SJoe Perches			}
64112d632745SJoe Perches		}
64122d632745SJoe Perches
6413e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6414c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6415c34c09a8SJoe Perches			my $has_break = 0;
6416c34c09a8SJoe Perches			my $has_statement = 0;
6417c34c09a8SJoe Perches			my $count = 0;
6418c34c09a8SJoe Perches			my $prevline = $linenr;
6419e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6420c34c09a8SJoe Perches				$prevline--;
6421c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6422c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6423c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6424c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6425c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6426c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6427c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6428c34c09a8SJoe Perches				$has_statement = 1;
6429c34c09a8SJoe Perches				$count++;
6430258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6431c34c09a8SJoe Perches			}
6432c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6433c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6434224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6435c34c09a8SJoe Perches			}
6436c34c09a8SJoe Perches		}
6437c34c09a8SJoe Perches
6438f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6439f36d3eb8SJoe Perches		my @fallthroughs = (
6440f36d3eb8SJoe Perches			'fallthrough',
6441f36d3eb8SJoe Perches			'@fallthrough@',
6442f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6443f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6444f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6445f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6446f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6447f36d3eb8SJoe Perches		    );
6448f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6449f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6450f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6451f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6452f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6453f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6454f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6455f36d3eb8SJoe Perches					last;
6456f36d3eb8SJoe Perches				}
6457f36d3eb8SJoe Perches			}
6458f36d3eb8SJoe Perches		}
6459f36d3eb8SJoe Perches
6460d1e2ad07SJoe Perches# check for switch/default statements without a break;
64615b57980dSJoe Perches		if ($perl_version_ok &&
6462d1e2ad07SJoe Perches		    defined $stat &&
6463d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6464d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6465e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6466e3d95a2aSTobin C. Harding
6467d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6468d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6469caf2a54fSJoe Perches		}
6470caf2a54fSJoe Perches
647113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6472d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6473d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6474d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6475d5e616fcSJoe Perches			    $fix) {
6476194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6477d5e616fcSJoe Perches			}
647813214adfSAndy Whitcroft		}
6479773647a0SAndy Whitcroft
648062ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
648162ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
648262ec818fSJoe Perches			ERROR("DATE_TIME",
648362ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
648462ec818fSJoe Perches		}
648562ec818fSJoe Perches
64862c92488aSJoe Perches# check for use of yield()
64872c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
64882c92488aSJoe Perches			WARN("YIELD",
64892c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
64902c92488aSJoe Perches		}
64912c92488aSJoe Perches
6492179f8f40SJoe Perches# check for comparisons against true and false
6493179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6494179f8f40SJoe Perches			my $lead = $1;
6495179f8f40SJoe Perches			my $arg = $2;
6496179f8f40SJoe Perches			my $test = $3;
6497179f8f40SJoe Perches			my $otype = $4;
6498179f8f40SJoe Perches			my $trail = $5;
6499179f8f40SJoe Perches			my $op = "!";
6500179f8f40SJoe Perches
6501179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6502179f8f40SJoe Perches
6503179f8f40SJoe Perches			my $type = lc($otype);
6504179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6505179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6506179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6507179f8f40SJoe Perches					$op = "";
6508179f8f40SJoe Perches				}
6509179f8f40SJoe Perches
6510179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6511179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6512179f8f40SJoe Perches
6513179f8f40SJoe Perches## maybe suggesting a correct construct would better
6514179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6515179f8f40SJoe Perches
6516179f8f40SJoe Perches			}
6517179f8f40SJoe Perches		}
6518179f8f40SJoe Perches
65194882720bSThomas Gleixner# check for semaphores initialized locked
65204882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6521000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6522000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6523773647a0SAndy Whitcroft		}
65246712d858SJoe Perches
652567d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
652667d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6527000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
652867d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6529773647a0SAndy Whitcroft		}
65306712d858SJoe Perches
6531ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6532f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6533000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6534ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6535f3db6639SMichael Ellerman		}
65366712d858SJoe Perches
65373d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
65383d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
65393d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
65403d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
65413d709ab5SPaul E. McKenney		}
65423d709ab5SPaul E. McKenney
65439189c7e7SJoe Perches# check for deprecated apis
65449189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
65459189c7e7SJoe Perches			my $deprecated_api = $1;
65469189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
65479189c7e7SJoe Perches			WARN("DEPRECATED_API",
65489189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
65499189c7e7SJoe Perches		}
65509189c7e7SJoe Perches
65510f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6552d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
65536903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6554d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6555000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6556d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
65572b6db5cbSAndy Whitcroft		}
6558773647a0SAndy Whitcroft
6559773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6560773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6561773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6562c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6563c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6564171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6565171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6566171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6567773647a0SAndy Whitcroft		{
6568000d1cc1SJoe Perches			WARN("NR_CPUS",
6569000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6570773647a0SAndy Whitcroft		}
65719c9ba34eSAndy Whitcroft
657252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
657352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
657452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
657552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
657652ea8506SJoe Perches		}
657752ea8506SJoe Perches
6578acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
65795b57980dSJoe Perches		if ($perl_version_ok &&
6580acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6581acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6582acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6583acd9362cSJoe Perches		}
6584acd9362cSJoe Perches
6585de3f186fSDenis Efremov# nested likely/unlikely calls
6586de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6587de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6588de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6589de3f186fSDenis Efremov		}
6590de3f186fSDenis Efremov
6591691d77b6SAndy Whitcroft# whine mightly about in_atomic
6592691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6593691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6594000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6595000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6596f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6597000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6598000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6599691d77b6SAndy Whitcroft			}
6600691d77b6SAndy Whitcroft		}
66011704f47bSPeter Zijlstra
66020f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
66030f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
66040f5225b0SPeter Zijlstra			ERROR("LOCKING",
66050f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
66060f5225b0SPeter Zijlstra		}
66070f5225b0SPeter Zijlstra
66081704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
66091704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
66101704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
66111704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
66121704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
66131704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6614000d1cc1SJoe Perches				ERROR("LOCKDEP",
6615000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
66161704f47bSPeter Zijlstra			}
66171704f47bSPeter Zijlstra		}
661888f8831cSDave Jones
6619b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6620b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6621000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6622000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
662388f8831cSDave Jones		}
66242435880fSJoe Perches
662500180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
662600180468SJoe Perches# and whether or not function naming is typical and if
662700180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
66285b57980dSJoe Perches		if ($perl_version_ok &&
662900180468SJoe Perches		    defined $stat &&
663000180468SJoe 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*\)/) {
663100180468SJoe Perches			my $var = $1;
663200180468SJoe Perches			my $perms = $2;
663300180468SJoe Perches			my $show = $3;
663400180468SJoe Perches			my $store = $4;
663500180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
663600180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
663700180468SJoe Perches			    $store =~ /^${var}_store$/ &&
663800180468SJoe Perches			    $octal_perms eq "0644") {
663900180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
664000180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
664100180468SJoe Perches				    $fix) {
664200180468SJoe 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})/;
664300180468SJoe Perches				}
664400180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
664500180468SJoe Perches				 $store =~ /^NULL$/ &&
664600180468SJoe Perches				 $octal_perms eq "0444") {
664700180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
664800180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
664900180468SJoe Perches				    $fix) {
665000180468SJoe 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})/;
665100180468SJoe Perches				}
665200180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
665300180468SJoe Perches				 $store =~ /^${var}_store$/ &&
665400180468SJoe Perches				 $octal_perms eq "0200") {
665500180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
665600180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
665700180468SJoe Perches				    $fix) {
665800180468SJoe 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})/;
665900180468SJoe Perches				}
666000180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
666100180468SJoe Perches				 $octal_perms eq "0444" ||
666200180468SJoe Perches				 $octal_perms eq "0200") {
666300180468SJoe Perches				my $newshow = "$show";
666400180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
666500180468SJoe Perches				my $newstore = $store;
666600180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
666700180468SJoe Perches				my $rename = "";
666800180468SJoe Perches				if ($show ne $newshow) {
666900180468SJoe Perches					$rename .= " '$show' to '$newshow'";
667000180468SJoe Perches				}
667100180468SJoe Perches				if ($store ne $newstore) {
667200180468SJoe Perches					$rename .= " '$store' to '$newstore'";
667300180468SJoe Perches				}
667400180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
667500180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
667600180468SJoe Perches			} else {
667700180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
667800180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
667900180468SJoe Perches			}
668000180468SJoe Perches		}
668100180468SJoe Perches
6682515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6683515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
668473121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
668573121534SJoe Perches#   specific definition of not visible in sysfs.
668673121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
668773121534SJoe Perches#   use the default permissions
66885b57980dSJoe Perches		if ($perl_version_ok &&
6689459cf0aeSJoe Perches		    defined $stat &&
6690515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
66912435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
66922435880fSJoe Perches				my $func = $entry->[0];
66932435880fSJoe Perches				my $arg_pos = $entry->[1];
66942435880fSJoe Perches
6695459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6696459cf0aeSJoe Perches				$lc = $lc + $linenr;
66972a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6698459cf0aeSJoe Perches
66992435880fSJoe Perches				my $skip_args = "";
67002435880fSJoe Perches				if ($arg_pos > 1) {
67012435880fSJoe Perches					$arg_pos--;
67022435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
67032435880fSJoe Perches				}
6704f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6705459cf0aeSJoe Perches				if ($stat =~ /$test/) {
67062435880fSJoe Perches					my $val = $1;
67072435880fSJoe Perches					$val = $6 if ($skip_args ne "");
670873121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
670973121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
671073121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
67112435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6712459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6713f90774e1SJoe Perches					}
6714f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6715c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6716459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
67172435880fSJoe Perches					}
6718459cf0aeSJoe Perches				}
6719459cf0aeSJoe Perches			}
6720459cf0aeSJoe Perches		}
6721459cf0aeSJoe Perches
6722459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6723bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
672400180468SJoe Perches			my $oval = $1;
672500180468SJoe Perches			my $octal = perms_to_octal($oval);
6726f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6727459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6728f90774e1SJoe Perches			    $fix) {
672900180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
67302435880fSJoe Perches			}
673113214adfSAndy Whitcroft		}
67325a6d20ceSBjorn Andersson
67335a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
67345a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
67355a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
67365a6d20ceSBjorn Andersson			my $valid_licenses = qr{
67375a6d20ceSBjorn Andersson						GPL|
67385a6d20ceSBjorn Andersson						GPL\ v2|
67395a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
67405a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
67415a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
67425a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
67435a6d20ceSBjorn Andersson						Proprietary
67445a6d20ceSBjorn Andersson					}x;
67455a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
67465a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
67475a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
67485a6d20ceSBjorn Andersson			}
67495a6d20ceSBjorn Andersson		}
67506a8d76cbSMatteo Croce
67516a8d76cbSMatteo Croce# check for sysctl duplicate constants
67526a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
67536a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
67546a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
67556a8d76cbSMatteo Croce		}
6756515a235eSJoe Perches	}
675713214adfSAndy Whitcroft
675813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
675913214adfSAndy Whitcroft	# so just keep quiet.
676013214adfSAndy Whitcroft	if ($#rawlines == -1) {
676113214adfSAndy Whitcroft		exit(0);
67620a920b5bSAndy Whitcroft	}
67630a920b5bSAndy Whitcroft
67648905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
67658905a67cSAndy Whitcroft	# things that appear to be patches.
67668905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
67678905a67cSAndy Whitcroft		exit(0);
67688905a67cSAndy Whitcroft	}
67698905a67cSAndy Whitcroft
67708905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
67718905a67cSAndy Whitcroft	# just keep quiet.
67728905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
67738905a67cSAndy Whitcroft		exit(0);
67748905a67cSAndy Whitcroft	}
67758905a67cSAndy Whitcroft
6776a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6777000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6778000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
67790a920b5bSAndy Whitcroft	}
6780cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6781cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6782000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6783000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6784cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6785cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6786cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6787cd261496SGeert Uytterhoeven		}
67880a920b5bSAndy Whitcroft	}
67890a920b5bSAndy Whitcroft
6790f0a594c1SAndy Whitcroft	print report_dump();
679113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
679213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
67936c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
67946c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
67956c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
67966c72ffaaSAndy Whitcroft	}
67978905a67cSAndy Whitcroft
6798d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6799ef212196SJoe Perches		# If there were any defects found and not already fixing them
6800ef212196SJoe Perches		if (!$clean and !$fix) {
6801ef212196SJoe Perches			print << "EOM"
6802ef212196SJoe Perches
6803ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6804ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6805ef212196SJoe PerchesEOM
6806ef212196SJoe Perches		}
6807d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6808d2c0a235SAndy Whitcroft		# then suggest that.
6809d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6810b0781216SMike Frysinger			$rpt_cleaners = 0;
6811d8469f16SJoe Perches			print << "EOM"
6812d8469f16SJoe Perches
6813d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6814d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6815d8469f16SJoe PerchesEOM
6816d2c0a235SAndy Whitcroft		}
6817d2c0a235SAndy Whitcroft	}
6818d2c0a235SAndy Whitcroft
6819d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6820d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6821d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
68229624b8d6SJoe Perches		my $newfile = $filename;
68239624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
68243705ce5bSJoe Perches		my $linecount = 0;
68253705ce5bSJoe Perches		my $f;
68263705ce5bSJoe Perches
6827d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6828d752fcc8SJoe Perches
68293705ce5bSJoe Perches		open($f, '>', $newfile)
68303705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
68313705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
68323705ce5bSJoe Perches			$linecount++;
68333705ce5bSJoe Perches			if ($file) {
68343705ce5bSJoe Perches				if ($linecount > 3) {
68353705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
68363705ce5bSJoe Perches					print $f $fixed_line . "\n";
68373705ce5bSJoe Perches				}
68383705ce5bSJoe Perches			} else {
68393705ce5bSJoe Perches				print $f $fixed_line . "\n";
68403705ce5bSJoe Perches			}
68413705ce5bSJoe Perches		}
68423705ce5bSJoe Perches		close($f);
68433705ce5bSJoe Perches
68443705ce5bSJoe Perches		if (!$quiet) {
68453705ce5bSJoe Perches			print << "EOM";
6846d8469f16SJoe Perches
68473705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
68483705ce5bSJoe Perches
68493705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
68503705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
68513705ce5bSJoe Perches
68523705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
68533705ce5bSJoe PerchesNo warranties, expressed or implied...
68543705ce5bSJoe PerchesEOM
68553705ce5bSJoe Perches		}
68563705ce5bSJoe Perches	}
68573705ce5bSJoe Perches
6858d8469f16SJoe Perches	if ($quiet == 0) {
6859d8469f16SJoe Perches		print "\n";
6860d8469f16SJoe Perches		if ($clean == 1) {
6861d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6862d8469f16SJoe Perches		} else {
6863d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
68640a920b5bSAndy Whitcroft		}
68650a920b5bSAndy Whitcroft	}
68660a920b5bSAndy Whitcroft	return $clean;
68670a920b5bSAndy Whitcroft}
6868