xref: /linux-6.15/scripts/checkpatch.pl (revision 713a09de)
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';
67*713a09deSAntonio 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
102*713a09deSAntonio 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,
220*713a09deSAntonio 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
273*713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
274*713a09deSAntonio Borneodie "Invalid TAB size: $tabsize\n" if ($tabsize < 2);
275*713a09deSAntonio 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++;
1248*713a09deSAntonio 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
2261*713a09deSAntonio 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
2338ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2339490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2340bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
23412a076f40SJoe Perches	my $commit_log_long_line = 0;
2342e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
234313f1937eSJoe Perches	my $reported_maintainer_file = 0;
2344fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2345fa64205dSPasi Savanainen
2346365dd4eaSJoe Perches	my $last_blank_line = 0;
23475e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2348365dd4eaSJoe Perches
234913214adfSAndy Whitcroft	our @report = ();
23506c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
23516c72ffaaSAndy Whitcroft	our $cnt_error = 0;
23526c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
23536c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
23546c72ffaaSAndy Whitcroft
23550a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
23560a920b5bSAndy Whitcroft	my $realfile = '';
23570a920b5bSAndy Whitcroft	my $realline = 0;
23580a920b5bSAndy Whitcroft	my $realcnt = 0;
23590a920b5bSAndy Whitcroft	my $here = '';
236077cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23610a920b5bSAndy Whitcroft	my $in_comment = 0;
2362c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23630a920b5bSAndy Whitcroft	my $first_line = 0;
23641e855726SWolfram Sang	my $p1_prefix = '';
23650a920b5bSAndy Whitcroft
236613214adfSAndy Whitcroft	my $prev_values = 'E';
236713214adfSAndy Whitcroft
236813214adfSAndy Whitcroft	# suppression flags
2369773647a0SAndy Whitcroft	my %suppress_ifbraces;
2370170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
23712b474a1aSAndy Whitcroft	my %suppress_export;
23723e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2373653d4876SAndy Whitcroft
23747e51f197SJoe Perches	my %signatures = ();
2375323c1260SJoe Perches
2376c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2377de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2378c2fdda0dSAndy Whitcroft	#
2379de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2380de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2381773647a0SAndy Whitcroft
2382d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2383d8b07710SJoe Perches
23849f3a8992SRob Herring	my $checklicenseline = 1;
23859f3a8992SRob Herring
2386773647a0SAndy Whitcroft	sanitise_line_reset();
2387c2fdda0dSAndy Whitcroft	my $line;
2388c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2389773647a0SAndy Whitcroft		$linenr++;
2390773647a0SAndy Whitcroft		$line = $rawline;
2391c2fdda0dSAndy Whitcroft
23923705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
23933705ce5bSJoe Perches
2394773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2395de7d4f0eSAndy Whitcroft			$setup_docs = 0;
23968c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2397de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2398de7d4f0eSAndy Whitcroft			}
2399773647a0SAndy Whitcroft			#next;
2400de7d4f0eSAndy Whitcroft		}
240174fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2402773647a0SAndy Whitcroft			$realline=$1-1;
2403773647a0SAndy Whitcroft			if (defined $2) {
2404773647a0SAndy Whitcroft				$realcnt=$3+1;
2405773647a0SAndy Whitcroft			} else {
2406773647a0SAndy Whitcroft				$realcnt=1+1;
2407773647a0SAndy Whitcroft			}
2408c45dcabdSAndy Whitcroft			$in_comment = 0;
2409773647a0SAndy Whitcroft
2410773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2411773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2412773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2413773647a0SAndy Whitcroft			# at context start.
2414773647a0SAndy Whitcroft			my $edge;
241501fa9147SAndy Whitcroft			my $cnt = $realcnt;
241601fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
241701fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
241801fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
241901fa9147SAndy Whitcroft				$cnt--;
242001fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2421721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2422fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2423fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2424fae17daeSAndy Whitcroft					($edge) = $1;
2425fae17daeSAndy Whitcroft					last;
2426fae17daeSAndy Whitcroft				}
2427773647a0SAndy Whitcroft			}
2428773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2429773647a0SAndy Whitcroft				$in_comment = 1;
2430773647a0SAndy Whitcroft			}
2431773647a0SAndy Whitcroft
2432773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2433773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2434773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2435773647a0SAndy Whitcroft			if (!defined $edge &&
243683242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2437773647a0SAndy Whitcroft			{
2438773647a0SAndy Whitcroft				$in_comment = 1;
2439773647a0SAndy Whitcroft			}
2440773647a0SAndy Whitcroft
2441773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2442773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2443773647a0SAndy Whitcroft
2444171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2445773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2446171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2447773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2448773647a0SAndy Whitcroft		}
2449773647a0SAndy Whitcroft		push(@lines, $line);
2450773647a0SAndy Whitcroft
2451773647a0SAndy Whitcroft		if ($realcnt > 1) {
2452773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2453773647a0SAndy Whitcroft		} else {
2454773647a0SAndy Whitcroft			$realcnt = 0;
2455773647a0SAndy Whitcroft		}
2456773647a0SAndy Whitcroft
2457773647a0SAndy Whitcroft		#print "==>$rawline\n";
2458773647a0SAndy Whitcroft		#print "-->$line\n";
2459de7d4f0eSAndy Whitcroft
2460de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2461de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2462de7d4f0eSAndy Whitcroft		}
2463de7d4f0eSAndy Whitcroft	}
2464de7d4f0eSAndy Whitcroft
24656c72ffaaSAndy Whitcroft	$prefix = '';
24666c72ffaaSAndy Whitcroft
2467773647a0SAndy Whitcroft	$realcnt = 0;
2468773647a0SAndy Whitcroft	$linenr = 0;
2469194f66fcSJoe Perches	$fixlinenr = -1;
24700a920b5bSAndy Whitcroft	foreach my $line (@lines) {
24710a920b5bSAndy Whitcroft		$linenr++;
2472194f66fcSJoe Perches		$fixlinenr++;
24731b5539b1SJoe Perches		my $sline = $line;	#copy of $line
24741b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
24750a920b5bSAndy Whitcroft
2476c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2477f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
24786c72ffaaSAndy Whitcroft
247912c253abSJoe Perches# check if it's a mode change, rename or start of a patch
248012c253abSJoe Perches		if (!$in_commit_log &&
248112c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
248212c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
248312c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
248412c253abSJoe Perches			$is_patch = 1;
248512c253abSJoe Perches		}
248612c253abSJoe Perches
24870a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2488e518e9a5SJoe Perches		if (!$in_commit_log &&
248974fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
249074fd4f34SJoe Perches			my $context = $4;
24910a920b5bSAndy Whitcroft			$is_patch = 1;
24924a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
24930a920b5bSAndy Whitcroft			$realline=$1-1;
24940a920b5bSAndy Whitcroft			if (defined $2) {
24950a920b5bSAndy Whitcroft				$realcnt=$3+1;
24960a920b5bSAndy Whitcroft			} else {
24970a920b5bSAndy Whitcroft				$realcnt=1+1;
24980a920b5bSAndy Whitcroft			}
2499c2fdda0dSAndy Whitcroft			annotate_reset();
250013214adfSAndy Whitcroft			$prev_values = 'E';
250113214adfSAndy Whitcroft
2502773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2503170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
25042b474a1aSAndy Whitcroft			%suppress_export = ();
25053e469cdcSAndy Whitcroft			$suppress_statement = 0;
250674fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
250774fd4f34SJoe Perches				$context_function = $1;
250874fd4f34SJoe Perches			} else {
250974fd4f34SJoe Perches				undef $context_function;
251074fd4f34SJoe Perches			}
25110a920b5bSAndy Whitcroft			next;
25120a920b5bSAndy Whitcroft
25134a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
25144a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
25154a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2516773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
25170a920b5bSAndy Whitcroft			$realline++;
2518d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
25190a920b5bSAndy Whitcroft
25204a0df2efSAndy Whitcroft			# Measure the line length and indent.
2521c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
25220a920b5bSAndy Whitcroft
25230a920b5bSAndy Whitcroft			# Track the previous line.
25240a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
25250a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2526c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2527c2fdda0dSAndy Whitcroft
2528773647a0SAndy Whitcroft			#warn "line<$line>\n";
25296c72ffaaSAndy Whitcroft
2530d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2531d8aaf121SAndy Whitcroft			$realcnt--;
25320a920b5bSAndy Whitcroft		}
25330a920b5bSAndy Whitcroft
2534cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2535cc77cdcaSAndy Whitcroft
25366c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
25376c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2538773647a0SAndy Whitcroft
25392ac73b4fSJoe Perches		my $found_file = 0;
2540773647a0SAndy Whitcroft		# extract the filename as it passes
25413bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
25423bf9a009SRabin Vincent			$realfile = $1;
25432b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2544270c49a0SJoe Perches			$in_commit_log = 0;
25452ac73b4fSJoe Perches			$found_file = 1;
25463bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2547773647a0SAndy Whitcroft			$realfile = $1;
25482b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2549270c49a0SJoe Perches			$in_commit_log = 0;
25501e855726SWolfram Sang
25511e855726SWolfram Sang			$p1_prefix = $1;
2552e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2553e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2554000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2555000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
25561e855726SWolfram Sang			}
2557773647a0SAndy Whitcroft
2558c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2559000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2560000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2561773647a0SAndy Whitcroft			}
25622ac73b4fSJoe Perches			$found_file = 1;
25632ac73b4fSJoe Perches		}
25642ac73b4fSJoe Perches
256534d8815fSJoe Perches#make up the handle for any error we report on this line
256634d8815fSJoe Perches		if ($showfile) {
256734d8815fSJoe Perches			$prefix = "$realfile:$realline: "
256834d8815fSJoe Perches		} elsif ($emacs) {
25697d3a9f67SJoe Perches			if ($file) {
25707d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
25717d3a9f67SJoe Perches			} else {
257234d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
257334d8815fSJoe Perches			}
25747d3a9f67SJoe Perches		}
257534d8815fSJoe Perches
25762ac73b4fSJoe Perches		if ($found_file) {
257785b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
257885b0ee18SJoe Perches				WARN("OBSOLETE",
257985b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
258085b0ee18SJoe Perches			}
25817bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25822ac73b4fSJoe Perches				$check = 1;
25832ac73b4fSJoe Perches			} else {
25842ac73b4fSJoe Perches				$check = $check_orig;
25852ac73b4fSJoe Perches			}
25869f3a8992SRob Herring			$checklicenseline = 1;
2587133712a2SRob Herring
2588133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2589133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2590133712a2SRob Herring
2591133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2592133712a2SRob Herring
2593133712a2SRob Herring				if (($last_binding_patch != -1) &&
2594133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2595133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2596133712a2SRob Herring					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2597133712a2SRob Herring				}
2598133712a2SRob Herring			}
2599133712a2SRob Herring
2600773647a0SAndy Whitcroft			next;
2601773647a0SAndy Whitcroft		}
2602773647a0SAndy Whitcroft
2603389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
26040a920b5bSAndy Whitcroft
2605c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2606c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2607c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
26080a920b5bSAndy Whitcroft
26096c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
26106c72ffaaSAndy Whitcroft
2611490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2612490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2613490b292cSJoe Perches		if ($in_commit_log) {
2614490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2615490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2616490b292cSJoe Perches			}
2617490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2618490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2619490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2620490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2621490b292cSJoe Perches		}
2622490b292cSJoe Perches
2623e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2624e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2625e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2626e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2627e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2628e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2629e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2630e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2631e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2632e518e9a5SJoe Perches		}
2633e518e9a5SJoe Perches
26343bf9a009SRabin Vincent# Check for incorrect file permissions
26353bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
26363bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
263704db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
263804db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2639000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2640000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
26413bf9a009SRabin Vincent			}
26423bf9a009SRabin Vincent		}
26433bf9a009SRabin Vincent
2644cd261496SGeert Uytterhoeven# Check the patch for a From:
2645cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2646cd261496SGeert Uytterhoeven			$author = $1;
2647cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2648cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2649dfa05c28SJoe Perches			$author = reformat_email($author);
2650cd261496SGeert Uytterhoeven		}
2651cd261496SGeert Uytterhoeven
265220112475SJoe Perches# Check the patch for a signoff:
2653dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
26544a0df2efSAndy Whitcroft			$signoff++;
265515662b3eSJoe Perches			$in_commit_log = 0;
2656cd261496SGeert Uytterhoeven			if ($author ne '') {
2657dfa05c28SJoe Perches				if (same_email_addresses($1, $author)) {
2658cd261496SGeert Uytterhoeven					$authorsignoff = 1;
2659cd261496SGeert Uytterhoeven				}
2660cd261496SGeert Uytterhoeven			}
26610a920b5bSAndy Whitcroft		}
266220112475SJoe Perches
2663e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2664e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2665e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2666e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2667e0d975b1SJoe Perches		}
2668e0d975b1SJoe Perches
266920112475SJoe Perches# Check signature styles
2670270c49a0SJoe Perches		if (!$in_header_lines &&
2671ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
267220112475SJoe Perches			my $space_before = $1;
267320112475SJoe Perches			my $sign_off = $2;
267420112475SJoe Perches			my $space_after = $3;
267520112475SJoe Perches			my $email = $4;
267620112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
267720112475SJoe Perches
2678ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2679ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2680ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2681ce0338dfSJoe Perches			}
268220112475SJoe Perches			if (defined $space_before && $space_before ne "") {
26833705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26843705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
26853705ce5bSJoe Perches				    $fix) {
2686194f66fcSJoe Perches					$fixed[$fixlinenr] =
26873705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26883705ce5bSJoe Perches				}
268920112475SJoe Perches			}
269020112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
26913705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26923705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
26933705ce5bSJoe Perches				    $fix) {
2694194f66fcSJoe Perches					$fixed[$fixlinenr] =
26953705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26963705ce5bSJoe Perches				}
26973705ce5bSJoe Perches
269820112475SJoe Perches			}
269920112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
27003705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
27013705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
27023705ce5bSJoe Perches				    $fix) {
2703194f66fcSJoe Perches					$fixed[$fixlinenr] =
27043705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
27053705ce5bSJoe Perches				}
270620112475SJoe Perches			}
270720112475SJoe Perches
2708dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
270920112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
271020112475SJoe Perches			if ($suggested_email eq "") {
2711000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2712000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
271320112475SJoe Perches			} else {
271420112475SJoe Perches				my $dequoted = $suggested_email;
271520112475SJoe Perches				$dequoted =~ s/^"//;
271620112475SJoe Perches				$dequoted =~ s/" </ </;
271720112475SJoe Perches				# Don't force email to have quotes
271820112475SJoe Perches				# Allow just an angle bracketed address
2719dfa05c28SJoe Perches				if (!same_email_addresses($email, $suggested_email)) {
2720000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2721000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
272220112475SJoe Perches				}
27230a920b5bSAndy Whitcroft			}
27247e51f197SJoe Perches
27257e51f197SJoe Perches# Check for duplicate signatures
27267e51f197SJoe Perches			my $sig_nospace = $line;
27277e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
27287e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
27297e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
27307e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
27317e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
27327e51f197SJoe Perches			} else {
27337e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
27347e51f197SJoe Perches			}
27356c5d24eeSSean Christopherson
27366c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
27376c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
27386c5d24eeSSean Christopherson				if ($email eq $author) {
27396c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27406c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
27416c5d24eeSSean Christopherson				}
27426c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
27436c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27446c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
27456c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
27466c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27476c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27486c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
27496c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27506c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27516c5d24eeSSean Christopherson				}
27526c5d24eeSSean Christopherson			}
27530a920b5bSAndy Whitcroft		}
27540a920b5bSAndy Whitcroft
2755a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2756a2fe16b9SJoe Perches		if ($in_header_lines &&
2757a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2758a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2759a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2760a2fe16b9SJoe Perches		}
2761a2fe16b9SJoe Perches
27627ebd05efSChristopher Covington# Check for unwanted Gerrit info
27637ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
27647ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
27657ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
27667ebd05efSChristopher Covington		}
27677ebd05efSChristopher Covington
2768369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2769369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2770369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2771369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2772369c8dd3SJoe Perches					# timestamp
2773634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2774634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2775634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2776634cffccSJoe Perches					# stack dump address styles
2777369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2778369c8dd3SJoe Perches		}
2779369c8dd3SJoe Perches
27802a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
27812a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2782bf4daf12SJoe Perches		    length($line) > 75 &&
2783bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2784bf4daf12SJoe Perches					# file delta changes
2785bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2786bf4daf12SJoe Perches					# filename then :
2787bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2788bf4daf12SJoe Perches					# A Fixes: or Link: line
2789bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
27902a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
27912a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
27922a076f40SJoe Perches			$commit_log_long_line = 1;
27932a076f40SJoe Perches		}
27942a076f40SJoe Perches
2795bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2796bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2797bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2798bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2799bf4daf12SJoe Perches		}
2800bf4daf12SJoe Perches
28010d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2802369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2803a8972573SJohn Hubbard		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
2804e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2805fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2806aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2807369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2808bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2809fe043ea1SJoe Perches			my $init_char = "c";
2810fe043ea1SJoe Perches			my $orig_commit = "";
28110d7835fcSJoe Perches			my $short = 1;
28120d7835fcSJoe Perches			my $long = 0;
28130d7835fcSJoe Perches			my $case = 1;
28140d7835fcSJoe Perches			my $space = 1;
28150d7835fcSJoe Perches			my $hasdesc = 0;
281619c146a6SJoe Perches			my $hasparens = 0;
28170d7835fcSJoe Perches			my $id = '0123456789ab';
28180d7835fcSJoe Perches			my $orig_desc = "commit description";
28190d7835fcSJoe Perches			my $description = "";
28200d7835fcSJoe Perches
2821fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2822fe043ea1SJoe Perches				$init_char = $1;
2823fe043ea1SJoe Perches				$orig_commit = lc($2);
2824fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2825fe043ea1SJoe Perches				$orig_commit = lc($1);
2826fe043ea1SJoe Perches			}
2827fe043ea1SJoe Perches
28280d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
28290d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
28300d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
28310d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
28320d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
28330d7835fcSJoe Perches				$orig_desc = $1;
283419c146a6SJoe Perches				$hasparens = 1;
28350d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
28360d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
28370d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
28380d7835fcSJoe Perches				$orig_desc = $1;
283919c146a6SJoe Perches				$hasparens = 1;
2840b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2841b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2842b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2843b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2844b671fde0SJoe Perches				$orig_desc = $1;
2845b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2846b671fde0SJoe Perches				$orig_desc .= " " . $1;
284719c146a6SJoe Perches				$hasparens = 1;
28480d7835fcSJoe Perches			}
28490d7835fcSJoe Perches
28500d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
28510d7835fcSJoe Perches							      $id, $orig_desc);
28520d7835fcSJoe Perches
2853948b133aSHeinrich Schuchardt			if (defined($id) &&
2854948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2855d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
28560d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
28570d7835fcSJoe Perches			}
2858d311cd44SJoe Perches		}
2859d311cd44SJoe Perches
286013f1937eSJoe Perches# Check for added, moved or deleted files
286113f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
286213f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
286313f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
286413f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
286513f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2866a82603a8SAndrew Jeffery			$is_patch = 1;
286713f1937eSJoe Perches			$reported_maintainer_file = 1;
286813f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
286913f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
287013f1937eSJoe Perches		}
287113f1937eSJoe Perches
2872e400edb1SRob Herring# Check for adding new DT bindings not in schema format
2873e400edb1SRob Herring		if (!$in_commit_log &&
2874e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
2875e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2876e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
2877e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2878e400edb1SRob Herring		}
2879e400edb1SRob Herring
288000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
28818905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2882000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2883000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
28846c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2885de7d4f0eSAndy Whitcroft		}
2886de7d4f0eSAndy Whitcroft
2887de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2888de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2889171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2890171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2891171ae1a4SAndy Whitcroft
2892171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2893171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2894171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2895171ae1a4SAndy Whitcroft
289634d99219SJoe Perches			CHK("INVALID_UTF8",
2897000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
289800df344fSAndy Whitcroft		}
28990a920b5bSAndy Whitcroft
290015662b3eSJoe Perches# Check if it's the start of a commit log
290115662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
290215662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2903eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2904eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
290515662b3eSJoe Perches			$in_header_lines = 0;
290615662b3eSJoe Perches			$in_commit_log = 1;
2907ed43c4e5SAllen Hubbe			$has_commit_log = 1;
290815662b3eSJoe Perches		}
290915662b3eSJoe Perches
2910fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2911fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2912fa64205dSPasi Savanainen		if ($in_header_lines &&
2913fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2914fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2915fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2916fa64205dSPasi Savanainen		}
2917fa64205dSPasi Savanainen
2918fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
291915662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2920fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
292115662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
292215662b3eSJoe Perches		}
292315662b3eSJoe Perches
2924d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2925d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2926d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2927d6430f71SJoe Perches				my $file = $1;
2928d6430f71SJoe Perches
2929d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2930d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2931d6430f71SJoe Perches					#
2932d6430f71SJoe Perches				} else {
2933d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2934d6430f71SJoe Perches				}
2935d6430f71SJoe Perches			}
2936d6430f71SJoe Perches		}
2937d6430f71SJoe Perches
293866b47b4aSKees Cook# Check for various typo / spelling mistakes
293966d7a382SJoe Perches		if (defined($misspellings) &&
294066d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2941ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
294266b47b4aSKees Cook				my $typo = $1;
294366b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
294466b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
294566b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
29460675a8fbSJean Delvare				my $msg_level = \&WARN;
29470675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
29480675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
294966b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
295066b47b4aSKees Cook				    $fix) {
295166b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
295266b47b4aSKees Cook				}
295366b47b4aSKees Cook			}
295466b47b4aSKees Cook		}
295566b47b4aSKees Cook
2956a8dd86bfSMatteo Croce# check for invalid commit id
2957a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2958a8dd86bfSMatteo Croce			my $id;
2959a8dd86bfSMatteo Croce			my $description;
2960a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
2961a8dd86bfSMatteo Croce			if (!defined($id)) {
2962a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
2963a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2964a8dd86bfSMatteo Croce			}
2965a8dd86bfSMatteo Croce		}
2966a8dd86bfSMatteo Croce
296730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
296830670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
296900df344fSAndy Whitcroft
29700a920b5bSAndy Whitcroft#trailing whitespace
29719c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2972c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2973d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2974d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2975d5e616fcSJoe Perches			    $fix) {
2976194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2977d5e616fcSJoe Perches			}
2978c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2979c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29803705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
29813705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
29823705ce5bSJoe Perches			    $fix) {
2983194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
29843705ce5bSJoe Perches			}
29853705ce5bSJoe Perches
2986d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29870a920b5bSAndy Whitcroft		}
29885368df20SAndy Whitcroft
29894783f894SJosh Triplett# Check for FSF mailing addresses.
2990109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
29911bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
29923e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
29933e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
29944783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29950675a8fbSJean Delvare			my $msg_level = \&ERROR;
29960675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
29970675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
29984783f894SJosh 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)
29994783f894SJosh Triplett		}
30004783f894SJosh Triplett
30013354957aSAndi Kleen# check for Kconfig help text having a real description
30029fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
30039fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
30043354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
3005678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
3006678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
3007678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
3008678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
30093354957aSAndi Kleen			my $length = 0;
30109fe287d7SAndy Whitcroft			my $cnt = $realcnt;
30119fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
30129fe287d7SAndy Whitcroft			my $f;
3013a1385803SAndy Whitcroft			my $is_start = 0;
30149fe287d7SAndy Whitcroft			my $is_end = 0;
3015a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
30169fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
30179fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
30189fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
30199fe287d7SAndy Whitcroft
30209fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
30218d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
3022a1385803SAndy Whitcroft
302386adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3024a1385803SAndy Whitcroft					$is_start = 1;
302584af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
302684af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
302784af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
302884af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
302984af7a61SUlf Magnusson					}
3030a1385803SAndy Whitcroft					$length = -1;
3031a1385803SAndy Whitcroft				}
3032a1385803SAndy Whitcroft
30339fe287d7SAndy Whitcroft				$f =~ s/^.//;
30343354957aSAndi Kleen				$f =~ s/#.*//;
30353354957aSAndi Kleen				$f =~ s/^\s+//;
30363354957aSAndi Kleen				next if ($f =~ /^$/);
3037678ae162SUlf Magnusson
3038678ae162SUlf Magnusson				# This only checks context lines in the patch
3039678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3040678ae162SUlf Magnusson				# positives, even though some of these are
3041678ae162SUlf Magnusson				# common words in help texts
3042678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3043678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
30449fe287d7SAndy Whitcroft					$is_end = 1;
30459fe287d7SAndy Whitcroft					last;
30469fe287d7SAndy Whitcroft				}
30473354957aSAndi Kleen				$length++;
30483354957aSAndi Kleen			}
304956193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3050000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
305156193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
305256193274SVadim Bendebury			}
3053a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
30543354957aSAndi Kleen		}
30553354957aSAndi Kleen
3056628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
3057628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
3058628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
3059628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
3060628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
3061628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3062628f91a2SJoe Perches			    $fix) {
3063628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3064628f91a2SJoe Perches			}
3065628f91a2SJoe Perches		}
3066628f91a2SJoe Perches
3067327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3068327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3069327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3070327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3071327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3072327953e9SChristoph Jaeger		}
3073327953e9SChristoph Jaeger
3074c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3075c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3076c68e5878SArnaud Lacombe			my $flag = $1;
3077c68e5878SArnaud Lacombe			my $replacement = {
3078c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3079c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3080c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3081c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3082c68e5878SArnaud Lacombe			};
3083c68e5878SArnaud Lacombe
3084c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3085c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3086c68e5878SArnaud Lacombe		}
3087c68e5878SArnaud Lacombe
3088bff5da43SRob Herring# check for DT compatible documentation
30897dd05b38SFlorian Vaussard		if (defined $root &&
30907dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
30917dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
30927dd05b38SFlorian Vaussard
3093bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3094bff5da43SRob Herring
3095cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3096852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3097cc93319bSFlorian Vaussard
3098bff5da43SRob Herring			foreach my $compat (@compats) {
3099bff5da43SRob Herring				my $compat2 = $compat;
3100185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3101185d566bSRob Herring				my $compat3 = $compat;
3102185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3103185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3104bff5da43SRob Herring				if ( $? >> 8 ) {
3105bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3106bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3107bff5da43SRob Herring				}
3108bff5da43SRob Herring
31094fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
31104fbf32a6SFlorian Vaussard				my $vendor = $1;
3111852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3112bff5da43SRob Herring				if ( $? >> 8 ) {
3113bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3114cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3115bff5da43SRob Herring				}
3116bff5da43SRob Herring			}
3117bff5da43SRob Herring		}
3118bff5da43SRob Herring
31199f3a8992SRob Herring# check for using SPDX license tag at beginning of files
31209f3a8992SRob Herring		if ($realline == $checklicenseline) {
31219f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
31229f3a8992SRob Herring				$checklicenseline = 2;
31239f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
31249f3a8992SRob Herring				my $comment = "";
31259f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
31269f3a8992SRob Herring					$comment = '/*';
31279f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
31289f3a8992SRob Herring					$comment = '//';
3129c8df0ab6SLubomir Rintel				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
31309f3a8992SRob Herring					$comment = '#';
31319f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
31329f3a8992SRob Herring					$comment = '..';
31339f3a8992SRob Herring				}
31349f3a8992SRob Herring
3135fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3136fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3137fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3138ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3139fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3140fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3141fdf13693SJoe Perches				}
3142fdf13693SJoe Perches
31439f3a8992SRob Herring				if ($comment !~ /^$/ &&
3144ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
31459f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
31469f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
31473b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
31483b6e8ac9SJoe Perches					my $spdx_license = $1;
31493b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
31503b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
31513b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
31523b6e8ac9SJoe Perches					}
31539f3a8992SRob Herring				}
31549f3a8992SRob Herring			}
31559f3a8992SRob Herring		}
31569f3a8992SRob Herring
31575368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3158d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
31595368df20SAndy Whitcroft
3160a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3161a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3162a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3163a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3164a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3165a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3166a8da38a9SJoe Perches		}
3167a8da38a9SJoe Perches
316847e0c88bSJoe Perches# line length limit (with some exclusions)
316947e0c88bSJoe Perches#
317047e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
317147e0c88bSJoe Perches#	logging functions like pr_info that end in a string
317247e0c88bSJoe Perches#	lines with a single string
317347e0c88bSJoe Perches#	#defines that are a single string
31742e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
317547e0c88bSJoe Perches#
317647e0c88bSJoe Perches# There are 3 different line length message types:
3177ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
317847e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
317947e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
318047e0c88bSJoe Perches#
318147e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
318247e0c88bSJoe Perches#
318347e0c88bSJoe Perches
3184b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
318547e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
318647e0c88bSJoe Perches
318747e0c88bSJoe Perches			# Check the allowed long line types first
318847e0c88bSJoe Perches
318947e0c88bSJoe Perches			# logging functions that end in a string that starts
319047e0c88bSJoe Perches			# before $max_line_length
319147e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
319247e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
319347e0c88bSJoe Perches				$msg_type = "";
319447e0c88bSJoe Perches
319547e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
319647e0c88bSJoe Perches			# #defines with only strings
319747e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
319847e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
319947e0c88bSJoe Perches				$msg_type = "";
320047e0c88bSJoe Perches
3201cc147506SJoe Perches			# More special cases
3202cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3203cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3204d560a5f8SJoe Perches				$msg_type = "";
3205d560a5f8SJoe Perches
32062e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
32072e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
32082e4bbbc5SAndreas Brauchli				$msg_type = "";
32092e4bbbc5SAndreas Brauchli
321047e0c88bSJoe Perches			# Otherwise set the alternate message types
321147e0c88bSJoe Perches
321247e0c88bSJoe Perches			# a comment starts before $max_line_length
321347e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
321447e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
321547e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
321647e0c88bSJoe Perches
321747e0c88bSJoe Perches			# a quoted string starts before $max_line_length
321847e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
321947e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
322047e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
322147e0c88bSJoe Perches			}
322247e0c88bSJoe Perches
322347e0c88bSJoe Perches			if ($msg_type ne "" &&
322447e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
322547e0c88bSJoe Perches				WARN($msg_type,
32266cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
32270a920b5bSAndy Whitcroft			}
322847e0c88bSJoe Perches		}
32290a920b5bSAndy Whitcroft
32308905a67cSAndy Whitcroft# check for adding lines without a newline.
32318905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3232000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3233000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
32348905a67cSAndy Whitcroft		}
32358905a67cSAndy Whitcroft
3236b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3237de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
32380a920b5bSAndy Whitcroft
32390a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3240*713a09deSAntonio Borneo# more than $tabsize must use tabs.
3241c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3242c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3243c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3244d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32453705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
32463705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
32473705ce5bSJoe Perches			    $fix) {
3248194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
32493705ce5bSJoe Perches			}
32500a920b5bSAndy Whitcroft		}
32510a920b5bSAndy Whitcroft
325208e44365SAlberto Panizzo# check for space before tabs.
325308e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
325408e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32553705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
32563705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
32573705ce5bSJoe Perches			    $fix) {
3258194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3259*713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3260194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3261c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
32623705ce5bSJoe Perches			}
326308e44365SAlberto Panizzo		}
326408e44365SAlberto Panizzo
32656a487211SJoe Perches# check for assignments on the start of a line
32666a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
32676a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
32686a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
32696a487211SJoe Perches		}
32706a487211SJoe Perches
3271d1fe9c09SJoe Perches# check for && or || at the start of a line
3272d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3273d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3274d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3275d1fe9c09SJoe Perches		}
3276d1fe9c09SJoe Perches
3277a91e8994SJoe Perches# check indentation starts on a tab stop
32785b57980dSJoe Perches		if ($perl_version_ok &&
3279bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3280a91e8994SJoe Perches			my $indent = length($1);
3281*713a09deSAntonio Borneo			if ($indent % $tabsize) {
3282a91e8994SJoe Perches				if (WARN("TABSTOP",
3283a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3284a91e8994SJoe Perches				    $fix) {
3285*713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3286a91e8994SJoe Perches				}
3287a91e8994SJoe Perches			}
3288a91e8994SJoe Perches		}
3289a91e8994SJoe Perches
3290d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
32915b57980dSJoe Perches		if ($perl_version_ok &&
3292fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3293d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3294d1fe9c09SJoe Perches			my $oldindent = $1;
3295d1fe9c09SJoe Perches			my $rest = $2;
3296d1fe9c09SJoe Perches
3297d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3298d1fe9c09SJoe Perches			if ($pos >= 0) {
3299b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3300b34a26f3SJoe Perches				my $newindent = $2;
3301d1fe9c09SJoe Perches
3302d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3303*713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3304*713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3305d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3306d1fe9c09SJoe Perches
3307d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3308d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
33093705ce5bSJoe Perches
33103705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
33113705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
33123705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3313194f66fcSJoe Perches						$fixed[$fixlinenr] =~
33143705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
33153705ce5bSJoe Perches					}
3316d1fe9c09SJoe Perches				}
3317d1fe9c09SJoe Perches			}
3318d1fe9c09SJoe Perches		}
3319d1fe9c09SJoe Perches
33206ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
33216ab3a970SJoe Perches# avoid checking a few false positives:
33226ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
33236ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
33246ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
33256ab3a970SJoe Perches#   multiline macros that define functions
33266ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
33276ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
33286ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
33293705ce5bSJoe Perches			if (CHK("SPACING",
3330f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
33313705ce5bSJoe Perches			    $fix) {
3332194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3333f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
33343705ce5bSJoe Perches			}
3335aad4f614SJoe Perches		}
3336aad4f614SJoe Perches
333786406b1cSJoe Perches# Block comment styles
333886406b1cSJoe Perches# Networking with an initial /*
333905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3340fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
334185ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
334285ad978cSJoe Perches		    $realline > 2) {
334305880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
334405880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
334505880600SJoe Perches		}
334605880600SJoe Perches
334786406b1cSJoe Perches# Block comments use * on subsequent lines
334886406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
334986406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3350a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
335161135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3352a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
335386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
335486406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3355a605e32eSJoe Perches		}
3356a605e32eSJoe Perches
335786406b1cSJoe Perches# Block comments use */ on trailing lines
335886406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3359c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3360c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3361c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
336286406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
336386406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
336405880600SJoe Perches		}
336505880600SJoe Perches
336608eb9b80SJoe Perches# Block comment * alignment
336708eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3368af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3369af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3370af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
337108eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3372af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3373af207524SJoe Perches			my $oldindent;
337408eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3375af207524SJoe Perches			if (defined($1)) {
3376af207524SJoe Perches				$oldindent = expand_tabs($1);
3377af207524SJoe Perches			} else {
3378af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3379af207524SJoe Perches				$oldindent = expand_tabs($1);
3380af207524SJoe Perches			}
338108eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
338208eb9b80SJoe Perches			my $newindent = $1;
338308eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3384af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
338508eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
338608eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
338708eb9b80SJoe Perches			}
338808eb9b80SJoe Perches		}
338908eb9b80SJoe Perches
33907f619191SJoe Perches# check for missing blank lines after struct/union declarations
33917f619191SJoe Perches# with exceptions for various attributes and macros
33927f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
33937f619191SJoe Perches		    $line =~ /^\+/ &&
33947f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
33957f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
33967f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
33977f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
33987f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
33997f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
34007f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
34010bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
34027f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3403d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3404d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3405d752fcc8SJoe Perches			    $fix) {
3406f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3407d752fcc8SJoe Perches			}
34087f619191SJoe Perches		}
34097f619191SJoe Perches
3410365dd4eaSJoe Perches# check for multiple consecutive blank lines
3411365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3412365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3413365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3414d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3415d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3416d752fcc8SJoe Perches			    $fix) {
3417f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3418d752fcc8SJoe Perches			}
3419d752fcc8SJoe Perches
3420365dd4eaSJoe Perches			$last_blank_line = $linenr;
3421365dd4eaSJoe Perches		}
3422365dd4eaSJoe Perches
34233b617e3bSJoe Perches# check for missing blank lines after declarations
34243f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
34253f7bac03SJoe Perches			# actual declarations
34263f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
34275a4e1fd3SJoe Perches			# function pointer declarations
34285a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34293f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34303f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34313f7bac03SJoe Perches			# known declaration macros
34323f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
34333f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
34343f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
34353f7bac03SJoe Perches			# other possible extensions of declaration lines
34363f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
34373f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
34383f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
34393f7bac03SJoe Perches			# looks like a declaration
34403f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
34415a4e1fd3SJoe Perches			# function pointer declarations
34425a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34433f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34443f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34453f7bac03SJoe Perches			# known declaration macros
34463f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
34473f7bac03SJoe Perches			# start of struct or union or enum
3448328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
34493f7bac03SJoe Perches			# start or end of block or continuation of declaration
34503f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
34513f7bac03SJoe Perches			# bitfield continuation
34523f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
34533f7bac03SJoe Perches			# other possible extensions of declaration lines
34543f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
34553f7bac03SJoe Perches			# indentation of previous and current line are the same
34563f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3457d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3458d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3459d752fcc8SJoe Perches			    $fix) {
3460f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3461d752fcc8SJoe Perches			}
34623b617e3bSJoe Perches		}
34633b617e3bSJoe Perches
34645f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
34656b4c5bebSAndy Whitcroft# Exceptions:
34666b4c5bebSAndy Whitcroft#  1) within comments
34676b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
34686b4c5bebSAndy Whitcroft#  3) hanging labels
34693705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
34705f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34713705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
34723705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
34733705ce5bSJoe Perches			    $fix) {
3474194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
34753705ce5bSJoe Perches			}
34765f7ddae6SRaffaele Recalcati		}
34775f7ddae6SRaffaele Recalcati
3478b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3479b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3480b9ea10d6SAndy Whitcroft
34815751a24eSJoe Perches# check for unusual line ending [ or (
34825751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
34835751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
34845751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
34855751a24eSJoe Perches		}
34865751a24eSJoe Perches
34874dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
34884dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
34894dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
34904dbed76fSJoe Perches			$context_function = $1;
34914dbed76fSJoe Perches		}
34924dbed76fSJoe Perches
34934dbed76fSJoe Perches# check if this appears to be the end of function declaration
34944dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
34954dbed76fSJoe Perches			undef $context_function;
34964dbed76fSJoe Perches		}
34974dbed76fSJoe Perches
3498032a4c0fSJoe Perches# check indentation of any line with a bare else
3499840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3500032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3501032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3502032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3503840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3504840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3505840080a0SJoe Perches			     defined $lines[$linenr] &&
3506840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3507032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3508032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3509032a4c0fSJoe Perches			}
3510032a4c0fSJoe Perches		}
3511032a4c0fSJoe Perches
3512c00df19aSJoe Perches# check indentation of a line with a break;
3513c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3514c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3515c00df19aSJoe Perches			my $tabs = $1;
3516c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3517c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3518c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3519c00df19aSJoe Perches			}
3520c00df19aSJoe Perches		}
3521c00df19aSJoe Perches
3522c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3523cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3524000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3525000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3526c2fdda0dSAndy Whitcroft		}
352722f2a2efSAndy Whitcroft
352856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
352956e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
353056e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
353156e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
353256e77d70SJoe Perches		}
353356e77d70SJoe Perches
35349c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
35352b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
35362b474a1aSAndy Whitcroft		    $realline_next);
35373e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3538ca819864SJoe Perches		if ($linenr > $suppress_statement &&
35391b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3540170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3541f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3542171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3543171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3544171ae1a4SAndy Whitcroft
35453e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
35463e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
35473e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
35483e469cdcSAndy Whitcroft			# until we hit end of it.
35493e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
35503e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
35513e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
35523e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
35533e469cdcSAndy Whitcroft			}
3554f74bd194SAndy Whitcroft
35552b474a1aSAndy Whitcroft			# Find the real next line.
35562b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
35572b474a1aSAndy Whitcroft			if (defined $realline_next &&
35582b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
35592b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
35602b474a1aSAndy Whitcroft				$realline_next++;
35612b474a1aSAndy Whitcroft			}
35622b474a1aSAndy Whitcroft
3563171ae1a4SAndy Whitcroft			my $s = $stat;
3564171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3565cf655043SAndy Whitcroft
3566c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3567171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3568c2fdda0dSAndy Whitcroft
3569c2fdda0dSAndy Whitcroft			# Ignore functions being called
3570171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3571c2fdda0dSAndy Whitcroft
3572463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3573463f2864SAndy Whitcroft
3574c45dcabdSAndy Whitcroft			# declarations always start with types
3575d2506586SAndy 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) {
3576c45dcabdSAndy Whitcroft				my $type = $1;
3577c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3578c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3579c45dcabdSAndy Whitcroft
35806c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3581a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3582c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3583c2fdda0dSAndy Whitcroft			}
35848905a67cSAndy Whitcroft
35856c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
358665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3587c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
35889c0ca6f9SAndy Whitcroft			}
35898905a67cSAndy Whitcroft
35908905a67cSAndy Whitcroft			# Check for any sort of function declaration.
35918905a67cSAndy Whitcroft			# int foo(something bar, other baz);
35928905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3593171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
35948905a67cSAndy Whitcroft				my ($name_len) = length($1);
35958905a67cSAndy Whitcroft
3596cf655043SAndy Whitcroft				my $ctx = $s;
3597773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
35988905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3599cf655043SAndy Whitcroft
36008905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3601c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
36028905a67cSAndy Whitcroft
3603c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
36048905a67cSAndy Whitcroft					}
36058905a67cSAndy Whitcroft				}
36068905a67cSAndy Whitcroft			}
36078905a67cSAndy Whitcroft
36089c0ca6f9SAndy Whitcroft		}
36099c0ca6f9SAndy Whitcroft
361000df344fSAndy Whitcroft#
361100df344fSAndy Whitcroft# Checks which may be anchored in the context.
361200df344fSAndy Whitcroft#
361300df344fSAndy Whitcroft
361400df344fSAndy Whitcroft# Check for switch () and associated case and default
361500df344fSAndy Whitcroft# statements should be at the same indent.
361600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
361700df344fSAndy Whitcroft			my $err = '';
361800df344fSAndy Whitcroft			my $sep = '';
361900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
362000df344fSAndy Whitcroft			shift(@ctx);
362100df344fSAndy Whitcroft			for my $ctx (@ctx) {
362200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
362300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
362400df344fSAndy Whitcroft							$indent != $cindent) {
362500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
362600df344fSAndy Whitcroft					$sep = '';
362700df344fSAndy Whitcroft				} else {
362800df344fSAndy Whitcroft					$sep = "[...]\n";
362900df344fSAndy Whitcroft				}
363000df344fSAndy Whitcroft			}
363100df344fSAndy Whitcroft			if ($err ne '') {
3632000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3633000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3634de7d4f0eSAndy Whitcroft			}
3635de7d4f0eSAndy Whitcroft		}
3636de7d4f0eSAndy Whitcroft
3637de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3638de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
36390fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3640773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3641773647a0SAndy Whitcroft
36429c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
36438eef05ddSJoe Perches
36448eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
36458eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
36468eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
36478eef05ddSJoe Perches			}
36488eef05ddSJoe Perches
3649de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3650de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3651de7d4f0eSAndy Whitcroft
3652548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3653548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3654de7d4f0eSAndy Whitcroft
3655548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3656548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3657548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3658548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3659548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3660773647a0SAndy Whitcroft				$ctx_ln++;
3661773647a0SAndy Whitcroft			}
3662548596d5SAndy Whitcroft
366353210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
366453210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3665773647a0SAndy Whitcroft
3666773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3667000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3668000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
366901464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
367000df344fSAndy Whitcroft			}
3671773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3672773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3673773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3674773647a0SAndy Whitcroft			{
36759c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
36769c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3677000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3678000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
367901464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
36809c0ca6f9SAndy Whitcroft				}
36819c0ca6f9SAndy Whitcroft			}
368200df344fSAndy Whitcroft		}
368300df344fSAndy Whitcroft
36844d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3685f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
36863e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
36873e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
36883e469cdcSAndy Whitcroft					if (!defined $stat);
36894d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
36904d001e4dSAndy Whitcroft
36914d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
36924d001e4dSAndy Whitcroft
36939f5af480SJoe Perches			# remove inline comments
36949f5af480SJoe Perches			$s =~ s/$;/ /g;
36959f5af480SJoe Perches			$c =~ s/$;/ /g;
36964d001e4dSAndy Whitcroft
36974d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
36986f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
36996f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
37004d001e4dSAndy Whitcroft
37019f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
37029f5af480SJoe Perches			# none on the first line, and are going to readd them
37039f5af480SJoe Perches			# where necessary.
37049f5af480SJoe Perches			$s =~ s/\n./\n/gs;
37059f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
37069f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
37079f5af480SJoe Perches			}
37089f5af480SJoe Perches
37094d001e4dSAndy Whitcroft			# We want to check the first line inside the block
37104d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
37114d001e4dSAndy Whitcroft			#  1) any blank line termination
37124d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
37134d001e4dSAndy Whitcroft			#  3) any do (...) {
37144d001e4dSAndy Whitcroft			my $continuation = 0;
37154d001e4dSAndy Whitcroft			my $check = 0;
37164d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
37174d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
37184d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
37194d001e4dSAndy Whitcroft				$continuation = 1;
37204d001e4dSAndy Whitcroft			}
37219bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
37224d001e4dSAndy Whitcroft				$check = 1;
37234d001e4dSAndy Whitcroft				$cond_lines++;
37244d001e4dSAndy Whitcroft			}
37254d001e4dSAndy Whitcroft
37264d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
37274d001e4dSAndy Whitcroft			# preprocessor statement.
37284d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
37294d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
37304d001e4dSAndy Whitcroft				$check = 0;
37314d001e4dSAndy Whitcroft			}
37324d001e4dSAndy Whitcroft
37339bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3734740504c6SAndy Whitcroft			$continuation = 0;
37359bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
37369bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
37374d001e4dSAndy Whitcroft
3738f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3739f16fa28fSAndy Whitcroft				# is not linear.
3740f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3741f16fa28fSAndy Whitcroft					$check = 0;
3742f16fa28fSAndy Whitcroft				}
3743f16fa28fSAndy Whitcroft
37449bd49efeSAndy Whitcroft				# Ignore:
37459bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
37469bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
37479bd49efeSAndy Whitcroft				#  3) labels.
3748740504c6SAndy Whitcroft				if ($continuation ||
3749740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
37509bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
37519bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3752740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
375330dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
37549bd49efeSAndy Whitcroft						$cond_lines++;
37559bd49efeSAndy Whitcroft					}
37564d001e4dSAndy Whitcroft				}
375730dad6ebSAndy Whitcroft			}
37584d001e4dSAndy Whitcroft
37594d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
37604d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
37614d001e4dSAndy Whitcroft
37624d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
37634d001e4dSAndy Whitcroft			# this is not this patch's fault.
37644d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
37654d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
37664d001e4dSAndy Whitcroft				$check = 0;
37674d001e4dSAndy Whitcroft			}
37684d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
37694d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
37704d001e4dSAndy Whitcroft			}
37714d001e4dSAndy Whitcroft
37729bd49efeSAndy 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";
37734d001e4dSAndy Whitcroft
37749f5af480SJoe Perches			if ($check && $s ne '' &&
3775*713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
37769f5af480SJoe Perches			     ($sindent < $indent) ||
3777f6950a73SJoe Perches			     ($sindent == $indent &&
3778f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3779*713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
3780000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3781000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
37824d001e4dSAndy Whitcroft			}
37834d001e4dSAndy Whitcroft		}
37844d001e4dSAndy Whitcroft
37856c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
37866c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
37871f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
37881f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
37896c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3790c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3791c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3792cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3793cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
37941f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3795c2fdda0dSAndy Whitcroft		}
37966c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
37976c72ffaaSAndy Whitcroft
379800df344fSAndy Whitcroft#ignore lines not being added
37993705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
380000df344fSAndy Whitcroft
380111ca40a0SJoe Perches# check for dereferences that span multiple lines
380211ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
380311ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
380411ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
380511ca40a0SJoe Perches			my $ref = $1;
380611ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
380711ca40a0SJoe Perches			$ref .= $1;
380811ca40a0SJoe Perches			$ref =~ s/\s//g;
380911ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
381011ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
381111ca40a0SJoe Perches		}
381211ca40a0SJoe Perches
3813a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3814c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3815a1ce18e4SJoe Perches			my $type = $1;
3816a1ce18e4SJoe Perches			my $var = $2;
3817207a8e84SJoe Perches			$var = "" if (!defined $var);
3818207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3819a1ce18e4SJoe Perches				my $sign = $1;
3820a1ce18e4SJoe Perches				my $pointer = $2;
3821a1ce18e4SJoe Perches
3822a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3823a1ce18e4SJoe Perches
3824a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3825a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3826a1ce18e4SJoe Perches				    $fix) {
3827a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3828207a8e84SJoe Perches					my $comp_pointer = $pointer;
3829207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3830207a8e84SJoe Perches					$decl .= $comp_pointer;
3831207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3832207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3833a1ce18e4SJoe Perches				}
3834a1ce18e4SJoe Perches			}
3835a1ce18e4SJoe Perches		}
3836a1ce18e4SJoe Perches
3837653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
38387429c690SAndy Whitcroft		if ($dbg_type) {
38397429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3840000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3841000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
38427429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3843000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3844000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
38457429c690SAndy Whitcroft			}
3846653d4876SAndy Whitcroft			next;
3847653d4876SAndy Whitcroft		}
3848a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3849a1ef277eSAndy Whitcroft		if ($dbg_attr) {
38509360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3851000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3852000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
38539360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3854000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3855000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3856a1ef277eSAndy Whitcroft			}
3857a1ef277eSAndy Whitcroft			next;
3858a1ef277eSAndy Whitcroft		}
3859653d4876SAndy Whitcroft
3860f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
386199423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
386299423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3863d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3864d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3865f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3866f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3867f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3868d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3869d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3870f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3871d752fcc8SJoe Perches				$fixedline = $line;
38728d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3873f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3874d752fcc8SJoe Perches			}
3875f0a594c1SAndy Whitcroft		}
3876f0a594c1SAndy Whitcroft
387700df344fSAndy Whitcroft#
387800df344fSAndy Whitcroft# Checks which are anchored on the added line.
387900df344fSAndy Whitcroft#
388000df344fSAndy Whitcroft
3881653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3882c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3883653d4876SAndy Whitcroft			my $path = $1;
3884653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3885000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3886495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3887495e9d84SJoe Perches			}
3888495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3889495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3890495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3891653d4876SAndy Whitcroft			}
3892653d4876SAndy Whitcroft		}
3893653d4876SAndy Whitcroft
389400df344fSAndy Whitcroft# no C99 // comments
389500df344fSAndy Whitcroft		if ($line =~ m{//}) {
38963705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
38973705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
38983705ce5bSJoe Perches			    $fix) {
3899194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
39003705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
39013705ce5bSJoe Perches					my $comment = trim($1);
3902194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
39033705ce5bSJoe Perches				}
39043705ce5bSJoe Perches			}
390500df344fSAndy Whitcroft		}
390600df344fSAndy Whitcroft		# Remove C99 comments.
39070a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
39086c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
39090a920b5bSAndy Whitcroft
39102b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
39112b474a1aSAndy Whitcroft# the whole statement.
39122b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
39132b474a1aSAndy Whitcroft		if (defined $realline_next &&
39142b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
39152b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
39162b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39172b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39183cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
39193cbf62dfSAndy Whitcroft			# a prefix:
39203cbf62dfSAndy Whitcroft			#   XXX(foo);
39213cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3922653d4876SAndy Whitcroft			my $name = $1;
392387a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
39243cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
39253cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
39263cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
39273cbf62dfSAndy Whitcroft
39283cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
39292b474a1aSAndy Whitcroft				\n.}\s*$|
393048012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
393148012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
393248012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
39332b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
39342b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
393548012058SAndy Whitcroft			    )/x) {
39362b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
39372b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
39382b474a1aSAndy Whitcroft			} else {
39392b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
39400a920b5bSAndy Whitcroft			}
39410a920b5bSAndy Whitcroft		}
39422b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
39432b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
39442b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39452b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39462b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
39472b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
39482b474a1aSAndy Whitcroft		}
39492b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
39502b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3951000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3952000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
39532b474a1aSAndy Whitcroft		}
39540a920b5bSAndy Whitcroft
39555150bda4SJoe Eloff# check for global initialisers.
39566d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3957d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
39586d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3959d5e616fcSJoe Perches			    $fix) {
39606d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3961d5e616fcSJoe Perches			}
3962f0a594c1SAndy Whitcroft		}
39630a920b5bSAndy Whitcroft# check for static initialisers.
39646d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3965d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
39666d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3967d5e616fcSJoe Perches				      $herecurr) &&
3968d5e616fcSJoe Perches			    $fix) {
39696d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3970d5e616fcSJoe Perches			}
39710a920b5bSAndy Whitcroft		}
39720a920b5bSAndy Whitcroft
39731813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
39741813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
39751813087dSJoe Perches			my $tmp = trim($1);
39761813087dSJoe Perches			WARN("MISORDERED_TYPE",
39771813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
39781813087dSJoe Perches		}
39791813087dSJoe Perches
3980809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3981809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3982809e082eSJoe Perches			my $type = trim($1);
3983809e082eSJoe Perches			next if ($type !~ /\bint\b/);
3984809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3985809e082eSJoe Perches			my $new_type = $type;
3986809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
3987809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3988809e082eSJoe Perches			$new_type =~ s/^const\s+//;
3989809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3990809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
3991809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
3992809e082eSJoe Perches			$new_type = trim($new_type);
3993809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
3994809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3995809e082eSJoe Perches			    $fix) {
3996809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3997809e082eSJoe Perches			}
3998809e082eSJoe Perches		}
3999809e082eSJoe Perches
4000cb710ecaSJoe Perches# check for static const char * arrays.
4001cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4002000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4003000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4004cb710ecaSJoe Perches				$herecurr);
4005cb710ecaSJoe Perches		}
4006cb710ecaSJoe Perches
400777b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
400877b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
400977b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
401077b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
401177b8c0a8SJoe Perches			    $fix) {
401277b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
401377b8c0a8SJoe Perches			}
401477b8c0a8SJoe Perches		}
401577b8c0a8SJoe Perches
4016cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4017cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4018000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4019000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4020cb710ecaSJoe Perches				$herecurr);
4021cb710ecaSJoe Perches		}
4022cb710ecaSJoe Perches
4023ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4024ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4025ab7e23f3SJoe Perches			my $found = $1;
4026ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4027ab7e23f3SJoe Perches				WARN("CONST_CONST",
4028ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4029ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4030ab7e23f3SJoe Perches				WARN("CONST_CONST",
4031ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4032ab7e23f3SJoe Perches			}
4033ab7e23f3SJoe Perches		}
4034ab7e23f3SJoe Perches
40359b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
40369b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
40379b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
40389b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
40399b0fa60dSJoe Perches				$herecurr);
40409b0fa60dSJoe Perches               }
40419b0fa60dSJoe Perches
4042b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4043b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4044b598b670SJoe Perches			my $array = $1;
4045b598b670SJoe 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*\))@) {
4046b598b670SJoe Perches				my $array_div = $1;
4047b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4048b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4049b598b670SJoe Perches				    $fix) {
4050b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4051b598b670SJoe Perches				}
4052b598b670SJoe Perches			}
4053b598b670SJoe Perches		}
4054b598b670SJoe Perches
4055b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
4056b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4057b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4058b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4059b36190c5SJoe Perches			    $fix) {
4060194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4061b36190c5SJoe Perches			}
4062b36190c5SJoe Perches		}
4063b36190c5SJoe Perches
4064653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4065653d4876SAndy Whitcroft# make sense.
4066653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
40678054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4068c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
40698ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
407046d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4071000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4072000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
40730a920b5bSAndy Whitcroft		}
40740a920b5bSAndy Whitcroft
40750a920b5bSAndy Whitcroft# * goes on variable not on type
407665863862SAndy Whitcroft		# (char*[ const])
4077bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4078bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
40793705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4080d8aaf121SAndy Whitcroft
408165863862SAndy Whitcroft			# Should start with a space.
408265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
408365863862SAndy Whitcroft			# Should not end with a space.
408465863862SAndy Whitcroft			$to =~ s/\s+$//;
408565863862SAndy Whitcroft			# '*'s should not have spaces between.
4086f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
408765863862SAndy Whitcroft			}
4088d8aaf121SAndy Whitcroft
40893705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
409065863862SAndy Whitcroft			if ($from ne $to) {
40913705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40923705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
40933705ce5bSJoe Perches				    $fix) {
40943705ce5bSJoe Perches					my $sub_from = $ident;
40953705ce5bSJoe Perches					my $sub_to = $ident;
40963705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4097194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40983705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40993705ce5bSJoe Perches				}
410065863862SAndy Whitcroft			}
4101bfcb2cc7SAndy Whitcroft		}
4102bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4103bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
41043705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4105d8aaf121SAndy Whitcroft
410665863862SAndy Whitcroft			# Should start with a space.
410765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
410865863862SAndy Whitcroft			# Should not end with a space.
410965863862SAndy Whitcroft			$to =~ s/\s+$//;
411065863862SAndy Whitcroft			# '*'s should not have spaces between.
4111f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
411265863862SAndy Whitcroft			}
411365863862SAndy Whitcroft			# Modifiers should have spaces.
411465863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
411565863862SAndy Whitcroft
41163705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4117667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
41183705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
41193705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
41203705ce5bSJoe Perches				    $fix) {
41213705ce5bSJoe Perches
41223705ce5bSJoe Perches					my $sub_from = $match;
41233705ce5bSJoe Perches					my $sub_to = $match;
41243705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4125194f66fcSJoe Perches					$fixed[$fixlinenr] =~
41263705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
41273705ce5bSJoe Perches				}
412865863862SAndy Whitcroft			}
41290a920b5bSAndy Whitcroft		}
41300a920b5bSAndy Whitcroft
41319d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
41329d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
41330675a8fbSJean Delvare			my $msg_level = \&WARN;
41340675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
41350675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
41369d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
41379d3e3c70SJoe Perches		}
41380a920b5bSAndy Whitcroft
41399d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
41408905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4141000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4142000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
41438905a67cSAndy Whitcroft		}
41448905a67cSAndy Whitcroft
414517441227SJoe Perches# check for uses of printk_ratelimit
414617441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4147000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4148000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
414917441227SJoe Perches		}
415017441227SJoe Perches
4151eeef5733SJoe Perches# printk should use KERN_* levels
4152eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4153000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4154eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
415500df344fSAndy Whitcroft		}
41560a920b5bSAndy Whitcroft
4157243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4158243f3803SJoe Perches			my $orig = $1;
4159243f3803SJoe Perches			my $level = lc($orig);
4160243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
41618f26b837SJoe Perches			my $level2 = $level;
41628f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4163243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4164daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4165243f3803SJoe Perches		}
4166243f3803SJoe Perches
4167dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4168dc139313SJoe Perches			my $orig = $1;
4169dc139313SJoe Perches			my $level = lc($orig);
4170dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4171dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4172dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4173dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4174dc139313SJoe Perches		}
4175dc139313SJoe Perches
417691c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
417791c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
417891c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
417991c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
418091c9afafSAndy Lutomirski			WARN("ENOSYS",
418191c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
418291c9afafSAndy Lutomirski		}
418391c9afafSAndy Lutomirski
4184653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4185653d4876SAndy Whitcroft# or if closed on same line
41865b57980dSJoe Perches		if ($perl_version_ok &&
41872d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
41882d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
41892d453e3bSJoe Perches		    $sline !~ /}/) {
41908d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41912d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
41928d182478SJoe Perches			    $fix) {
41938d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41948d182478SJoe Perches				my $fixed_line = $rawline;
41958d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
41968d182478SJoe Perches				my $line1 = $1;
41978d182478SJoe Perches				my $line2 = $2;
41988d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
41998d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
42008d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
42018d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
42028d182478SJoe Perches				}
42038d182478SJoe Perches			}
42040a920b5bSAndy Whitcroft		}
4205653d4876SAndy Whitcroft
42068905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
42078905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
42088905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
42098d182478SJoe Perches			if (ERROR("OPEN_BRACE",
42108d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
42118d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
42128d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
42138d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
42148d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
42158d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
42168d182478SJoe Perches				$fixedline = $rawline;
42178d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
42188d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
42198d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
42208d182478SJoe Perches				}
42218d182478SJoe Perches			}
42228905a67cSAndy Whitcroft		}
42238905a67cSAndy Whitcroft
42240c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
42253705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
42263705ce5bSJoe Perches			if (WARN("SPACING",
42273705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
42283705ce5bSJoe Perches			    $fix) {
4229194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42303705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
42313705ce5bSJoe Perches			}
42320c73b4ebSAndy Whitcroft		}
42330c73b4ebSAndy Whitcroft
423431070b5dSJoe Perches# Function pointer declarations
423531070b5dSJoe Perches# check spacing between type, funcptr, and args
423631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
423791f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
423831070b5dSJoe Perches			my $declare = $1;
423931070b5dSJoe Perches			my $pre_pointer_space = $2;
424031070b5dSJoe Perches			my $post_pointer_space = $3;
424131070b5dSJoe Perches			my $funcname = $4;
424231070b5dSJoe Perches			my $post_funcname_space = $5;
424331070b5dSJoe Perches			my $pre_args_space = $6;
424431070b5dSJoe Perches
424591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
424691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
424791f72e9cSJoe Perches# don't need a space so don't warn for those.
424891f72e9cSJoe Perches			my $post_declare_space = "";
424991f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
425091f72e9cSJoe Perches				$post_declare_space = $1;
425191f72e9cSJoe Perches				$declare = rtrim($declare);
425291f72e9cSJoe Perches			}
425391f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
425431070b5dSJoe Perches				WARN("SPACING",
425531070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
425691f72e9cSJoe Perches				$post_declare_space = " ";
425731070b5dSJoe Perches			}
425831070b5dSJoe Perches
425931070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
426091f72e9cSJoe Perches# This test is not currently implemented because these declarations are
426191f72e9cSJoe Perches# equivalent to
426291f72e9cSJoe Perches#	int  foo(int bar, ...)
426391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
426491f72e9cSJoe Perches#
426591f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
426691f72e9cSJoe Perches#				WARN("SPACING",
426791f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
426891f72e9cSJoe Perches#			}
426931070b5dSJoe Perches
427031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
427131070b5dSJoe Perches			if (defined $pre_pointer_space &&
427231070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
427331070b5dSJoe Perches				WARN("SPACING",
427431070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
427531070b5dSJoe Perches			}
427631070b5dSJoe Perches
427731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
427831070b5dSJoe Perches			if (defined $post_pointer_space &&
427931070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
428031070b5dSJoe Perches				WARN("SPACING",
428131070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
428231070b5dSJoe Perches			}
428331070b5dSJoe Perches
428431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
428531070b5dSJoe Perches			if (defined $post_funcname_space &&
428631070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
428731070b5dSJoe Perches				WARN("SPACING",
428831070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
428931070b5dSJoe Perches			}
429031070b5dSJoe Perches
429131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
429231070b5dSJoe Perches			if (defined $pre_args_space &&
429331070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
429431070b5dSJoe Perches				WARN("SPACING",
429531070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
429631070b5dSJoe Perches			}
429731070b5dSJoe Perches
429831070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4299194f66fcSJoe Perches				$fixed[$fixlinenr] =~
430091f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
430131070b5dSJoe Perches			}
430231070b5dSJoe Perches		}
430331070b5dSJoe Perches
43048d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
43058d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4306fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4307fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
43088d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
43098d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
43108d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4311fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
431238dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
43133705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
43143705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
43153705ce5bSJoe Perches				    $fix) {
4316194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
43173705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
43183705ce5bSJoe Perches				}
43198d31cfceSAndy Whitcroft			}
43208d31cfceSAndy Whitcroft		}
43218d31cfceSAndy Whitcroft
4322f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
43236c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4324c2fdda0dSAndy Whitcroft			my $name = $1;
4325773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4326773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4327c2fdda0dSAndy Whitcroft
4328c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4329773647a0SAndy Whitcroft			if ($name =~ /^(?:
4330773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4331773647a0SAndy Whitcroft				volatile|__volatile__|
4332773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4333773647a0SAndy Whitcroft				asm|__asm__)$/x)
4334773647a0SAndy Whitcroft			{
4335c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4336c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4337c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4338c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4339773647a0SAndy Whitcroft
4340773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4341c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4342c2fdda0dSAndy Whitcroft
4343c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4344c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4345773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4346c2fdda0dSAndy Whitcroft
4347c2fdda0dSAndy Whitcroft			} else {
43483705ce5bSJoe Perches				if (WARN("SPACING",
43493705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
43503705ce5bSJoe Perches					     $fix) {
4351194f66fcSJoe Perches					$fixed[$fixlinenr] =~
43523705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
43533705ce5bSJoe Perches				}
4354f0a594c1SAndy Whitcroft			}
43556c72ffaaSAndy Whitcroft		}
43569a4cad4eSEric Nelson
4357653d4876SAndy Whitcroft# Check operator spacing.
43580a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
43593705ce5bSJoe Perches			my $fixed_line = "";
43603705ce5bSJoe Perches			my $line_fixed = 0;
43613705ce5bSJoe Perches
43629c0ca6f9SAndy Whitcroft			my $ops = qr{
43639c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
43649c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
43659c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
43661f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
436784731623SJoe Perches				\?:|\?|:
43689c0ca6f9SAndy Whitcroft			}x;
4369cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
43703705ce5bSJoe Perches
43713705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
43723705ce5bSJoe Perches##			foreach my $el (@elements) {
43733705ce5bSJoe Perches##				print("el: <$el>\n");
43743705ce5bSJoe Perches##			}
43753705ce5bSJoe Perches
43763705ce5bSJoe Perches			my @fix_elements = ();
437700df344fSAndy Whitcroft			my $off = 0;
43786c72ffaaSAndy Whitcroft
43793705ce5bSJoe Perches			foreach my $el (@elements) {
43803705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
43813705ce5bSJoe Perches				$off += length($el);
43823705ce5bSJoe Perches			}
43833705ce5bSJoe Perches
43843705ce5bSJoe Perches			$off = 0;
43853705ce5bSJoe Perches
43866c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4387b34c648bSJoe Perches			my $last_after = -1;
43886c72ffaaSAndy Whitcroft
43890a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
43903705ce5bSJoe Perches
43913705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
43923705ce5bSJoe Perches
43933705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
43943705ce5bSJoe Perches
43954a0df2efSAndy Whitcroft				$off += length($elements[$n]);
43964a0df2efSAndy Whitcroft
439725985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4398773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4399773647a0SAndy Whitcroft				my $cc = '';
4400773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4401773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4402773647a0SAndy Whitcroft				}
4403773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4404773647a0SAndy Whitcroft
44054a0df2efSAndy Whitcroft				my $a = '';
44064a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
44074a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4408cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
44094a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
44104a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4411773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
44124a0df2efSAndy Whitcroft
44130a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
44144a0df2efSAndy Whitcroft
44154a0df2efSAndy Whitcroft				my $c = '';
44160a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
44174a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
44184a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4419cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
44204a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
44214a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
44228b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
44234a0df2efSAndy Whitcroft				} else {
44244a0df2efSAndy Whitcroft					$c = 'E';
44250a920b5bSAndy Whitcroft				}
44260a920b5bSAndy Whitcroft
44274a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
44284a0df2efSAndy Whitcroft
44294a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
44304a0df2efSAndy Whitcroft
44316c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4432de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
44330a920b5bSAndy Whitcroft
443474048ed8SAndy Whitcroft				# Pull out the value of this operator.
44356c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
44360a920b5bSAndy Whitcroft
44371f65f947SAndy Whitcroft				# Get the full operator variant.
44381f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
44391f65f947SAndy Whitcroft
444013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
444113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4442d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
444313214adfSAndy Whitcroft
4444cf655043SAndy Whitcroft#				# Ignore comments
4445cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
444613214adfSAndy Whitcroft
4447d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
444813214adfSAndy Whitcroft				} elsif ($op eq ';') {
4449cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4450cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
44513705ce5bSJoe Perches						if (ERROR("SPACING",
44523705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4453b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44543705ce5bSJoe Perches							$line_fixed = 1;
44553705ce5bSJoe Perches						}
4456d8aaf121SAndy Whitcroft					}
4457d8aaf121SAndy Whitcroft
4458d8aaf121SAndy Whitcroft				# // is a comment
4459d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
44600a920b5bSAndy Whitcroft
4461b00e4814SJoe Perches				#   :   when part of a bitfield
4462b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4463b00e4814SJoe Perches					# skip the bitfield test for now
4464b00e4814SJoe Perches
44651f65f947SAndy Whitcroft				# No spaces for:
44661f65f947SAndy Whitcroft				#   ->
4467b00e4814SJoe Perches				} elsif ($op eq '->') {
44684a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
44693705ce5bSJoe Perches						if (ERROR("SPACING",
44703705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4471b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44723705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44733705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44743705ce5bSJoe Perches							}
4475b34c648bSJoe Perches							$line_fixed = 1;
44763705ce5bSJoe Perches						}
44770a920b5bSAndy Whitcroft					}
44780a920b5bSAndy Whitcroft
44792381097bSJoe Perches				# , must not have a space before and must have a space on the right.
44800a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
44812381097bSJoe Perches					my $rtrim_before = 0;
44822381097bSJoe Perches					my $space_after = 0;
44832381097bSJoe Perches					if ($ctx =~ /Wx./) {
44842381097bSJoe Perches						if (ERROR("SPACING",
44852381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
44862381097bSJoe Perches							$line_fixed = 1;
44872381097bSJoe Perches							$rtrim_before = 1;
44882381097bSJoe Perches						}
44892381097bSJoe Perches					}
4490cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
44913705ce5bSJoe Perches						if (ERROR("SPACING",
44923705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
44933705ce5bSJoe Perches							$line_fixed = 1;
4494b34c648bSJoe Perches							$last_after = $n;
44952381097bSJoe Perches							$space_after = 1;
44962381097bSJoe Perches						}
44972381097bSJoe Perches					}
44982381097bSJoe Perches					if ($rtrim_before || $space_after) {
44992381097bSJoe Perches						if ($rtrim_before) {
45002381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45012381097bSJoe Perches						} else {
45022381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45032381097bSJoe Perches						}
45042381097bSJoe Perches						if ($space_after) {
45052381097bSJoe Perches							$good .= " ";
45063705ce5bSJoe Perches						}
45070a920b5bSAndy Whitcroft					}
45080a920b5bSAndy Whitcroft
45099c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
451074048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
45119c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
45129c0ca6f9SAndy Whitcroft
45139c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
45149c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
45159c0ca6f9SAndy Whitcroft				# unary operator, or a cast
45169c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
451774048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
45180d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4519cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
45203705ce5bSJoe Perches						if (ERROR("SPACING",
45213705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4522b34c648bSJoe Perches							if ($n != $last_after + 2) {
4523b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
45243705ce5bSJoe Perches								$line_fixed = 1;
45253705ce5bSJoe Perches							}
45260a920b5bSAndy Whitcroft						}
4527b34c648bSJoe Perches					}
4528a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4529171ae1a4SAndy Whitcroft						# A unary '*' may be const
4530171ae1a4SAndy Whitcroft
4531171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
45323705ce5bSJoe Perches						if (ERROR("SPACING",
45333705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4534b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
45353705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45363705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
45373705ce5bSJoe Perches							}
4538b34c648bSJoe Perches							$line_fixed = 1;
45393705ce5bSJoe Perches						}
45400a920b5bSAndy Whitcroft					}
45410a920b5bSAndy Whitcroft
45420a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
45430a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4544773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
45453705ce5bSJoe Perches						if (ERROR("SPACING",
45463705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4547b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
45483705ce5bSJoe Perches							$line_fixed = 1;
45493705ce5bSJoe Perches						}
45500a920b5bSAndy Whitcroft					}
4551773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4552773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
45533705ce5bSJoe Perches						if (ERROR("SPACING",
45543705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4555b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45563705ce5bSJoe Perches							$line_fixed = 1;
45573705ce5bSJoe Perches						}
4558653d4876SAndy Whitcroft					}
4559773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
45603705ce5bSJoe Perches						if (ERROR("SPACING",
45613705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4562b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45633705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45643705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4565773647a0SAndy Whitcroft							}
4566b34c648bSJoe Perches							$line_fixed = 1;
45673705ce5bSJoe Perches						}
45683705ce5bSJoe Perches					}
45690a920b5bSAndy Whitcroft
45700a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
45719c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
45729c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
45739c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4574c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4575c2fdda0dSAndy Whitcroft					 $op eq '%')
45760a920b5bSAndy Whitcroft				{
4577d2e025f3SJoe Perches					if ($check) {
4578d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4579d2e025f3SJoe Perches							if (CHK("SPACING",
4580d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4581d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4582d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4583d2e025f3SJoe Perches								$line_fixed = 1;
4584d2e025f3SJoe Perches							}
4585d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4586d2e025f3SJoe Perches							if (CHK("SPACING",
4587d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4588d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4589d2e025f3SJoe Perches								$line_fixed = 1;
4590d2e025f3SJoe Perches							}
4591d2e025f3SJoe Perches						}
4592d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
45933705ce5bSJoe Perches						if (ERROR("SPACING",
45943705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4595b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4596b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4597b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4598b34c648bSJoe Perches							}
45993705ce5bSJoe Perches							$line_fixed = 1;
46003705ce5bSJoe Perches						}
46010a920b5bSAndy Whitcroft					}
46020a920b5bSAndy Whitcroft
46031f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
46041f65f947SAndy Whitcroft				# terminating a case value or a label.
46051f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
46061f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
46073705ce5bSJoe Perches						if (ERROR("SPACING",
46083705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4609b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
46103705ce5bSJoe Perches							$line_fixed = 1;
46113705ce5bSJoe Perches						}
46121f65f947SAndy Whitcroft					}
46131f65f947SAndy Whitcroft
46140a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4615cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
46161f65f947SAndy Whitcroft					my $ok = 0;
46171f65f947SAndy Whitcroft
461822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
46191f65f947SAndy Whitcroft					if (($op eq '<' &&
46201f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
46211f65f947SAndy Whitcroft					    ($op eq '>' &&
46221f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
46231f65f947SAndy Whitcroft					{
46241f65f947SAndy Whitcroft						$ok = 1;
46251f65f947SAndy Whitcroft					}
46261f65f947SAndy Whitcroft
4627e0df7e1fSJoe Perches					# for asm volatile statements
4628e0df7e1fSJoe Perches					# ignore a colon with another
4629e0df7e1fSJoe Perches					# colon immediately before or after
4630e0df7e1fSJoe Perches					if (($op eq ':') &&
4631e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4632e0df7e1fSJoe Perches						$ok = 1;
4633e0df7e1fSJoe Perches					}
4634e0df7e1fSJoe Perches
463584731623SJoe Perches					# messages are ERROR, but ?: are CHK
46361f65f947SAndy Whitcroft					if ($ok == 0) {
46370675a8fbSJean Delvare						my $msg_level = \&ERROR;
46380675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
463984731623SJoe Perches
46400675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
46413705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4642b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4643b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4644b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4645b34c648bSJoe Perches							}
46463705ce5bSJoe Perches							$line_fixed = 1;
46473705ce5bSJoe Perches						}
46480a920b5bSAndy Whitcroft					}
464922f2a2efSAndy Whitcroft				}
46504a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
46513705ce5bSJoe Perches
46523705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
46533705ce5bSJoe Perches
46543705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
46550a920b5bSAndy Whitcroft			}
46563705ce5bSJoe Perches
46573705ce5bSJoe Perches			if (($#elements % 2) == 0) {
46583705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
46593705ce5bSJoe Perches			}
46603705ce5bSJoe Perches
4661194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4662194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
46633705ce5bSJoe Perches			}
46643705ce5bSJoe Perches
46653705ce5bSJoe Perches
46660a920b5bSAndy Whitcroft		}
46670a920b5bSAndy Whitcroft
4668786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4669d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4670786b6326SJoe Perches			if (WARN("SPACING",
4671786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4672786b6326SJoe Perches			    $fix) {
4673194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4674786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4675786b6326SJoe Perches			}
4676786b6326SJoe Perches		}
4677786b6326SJoe Perches
4678f0a594c1SAndy Whitcroft# check for multiple assignments
4679f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4680000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4681000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4682f0a594c1SAndy Whitcroft		}
4683f0a594c1SAndy Whitcroft
468422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
468522f2a2efSAndy Whitcroft## # continuation.
468622f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
468722f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
468822f2a2efSAndy Whitcroft##
468922f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
469022f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
469122f2a2efSAndy Whitcroft## 			my $ln = $line;
469222f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
469322f2a2efSAndy Whitcroft## 			}
469422f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4695000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4696000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
469722f2a2efSAndy Whitcroft## 			}
469822f2a2efSAndy Whitcroft## 		}
4699f0a594c1SAndy Whitcroft
47000a920b5bSAndy Whitcroft#need space before brace following if, while, etc
47016b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
47026ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
47033705ce5bSJoe Perches			if (ERROR("SPACING",
47043705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
47053705ce5bSJoe Perches			    $fix) {
47066ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
47073705ce5bSJoe Perches			}
4708de7d4f0eSAndy Whitcroft		}
4709de7d4f0eSAndy Whitcroft
4710c4a62ef9SJoe Perches## # check for blank lines before declarations
4711c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4712c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4713c4a62ef9SJoe Perches##			WARN("SPACING",
4714c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4715c4a62ef9SJoe Perches##		}
4716c4a62ef9SJoe Perches##
4717c4a62ef9SJoe Perches
4718de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4719de7d4f0eSAndy Whitcroft# on the line
472094fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4721d5e616fcSJoe Perches			if (ERROR("SPACING",
4722d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4723d5e616fcSJoe Perches			    $fix) {
4724194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4725d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4726d5e616fcSJoe Perches			}
47270a920b5bSAndy Whitcroft		}
47280a920b5bSAndy Whitcroft
472922f2a2efSAndy Whitcroft# check spacing on square brackets
473022f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
47313705ce5bSJoe Perches			if (ERROR("SPACING",
47323705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
47333705ce5bSJoe Perches			    $fix) {
4734194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47353705ce5bSJoe Perches				    s/\[\s+/\[/;
47363705ce5bSJoe Perches			}
473722f2a2efSAndy Whitcroft		}
473822f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
47393705ce5bSJoe Perches			if (ERROR("SPACING",
47403705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
47413705ce5bSJoe Perches			    $fix) {
4742194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47433705ce5bSJoe Perches				    s/\s+\]/\]/;
47443705ce5bSJoe Perches			}
474522f2a2efSAndy Whitcroft		}
474622f2a2efSAndy Whitcroft
4747c45dcabdSAndy Whitcroft# check spacing on parentheses
47489c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
47499c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
47503705ce5bSJoe Perches			if (ERROR("SPACING",
47513705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
47523705ce5bSJoe Perches			    $fix) {
4753194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47543705ce5bSJoe Perches				    s/\(\s+/\(/;
47553705ce5bSJoe Perches			}
475622f2a2efSAndy Whitcroft		}
475713214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4758c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4759c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
47603705ce5bSJoe Perches			if (ERROR("SPACING",
47613705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
47623705ce5bSJoe Perches			    $fix) {
4763194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47643705ce5bSJoe Perches				    s/\s+\)/\)/;
47653705ce5bSJoe Perches			}
476622f2a2efSAndy Whitcroft		}
476722f2a2efSAndy Whitcroft
4768e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4769e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4770e2826fd0SJoe Perches
4771e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4772ea4acbb1SJoe Perches			my $var = $1;
4773ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4774ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4775ea4acbb1SJoe Perches			    $fix) {
4776ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4777ea4acbb1SJoe Perches			}
4778ea4acbb1SJoe Perches		}
4779ea4acbb1SJoe Perches
4780ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4781ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4782ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4783ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4784ea4acbb1SJoe Perches			my $var = $2;
4785ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4786ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4787ea4acbb1SJoe Perches			    $fix) {
4788ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4789ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4790ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4791ea4acbb1SJoe Perches			}
4792e2826fd0SJoe Perches		}
4793e2826fd0SJoe Perches
479463b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4795a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4796a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
47975b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
479863b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
479963b7c73eSJoe Perches			my $if_stat = $1;
480063b7c73eSJoe Perches			my $test = substr($2, 1, -1);
480163b7c73eSJoe Perches			my $herectx;
480263b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
480363b7c73eSJoe Perches				my $match = $1;
480463b7c73eSJoe Perches				# avoid parentheses around potential macro args
480563b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
480663b7c73eSJoe Perches				if (!defined($herectx)) {
480763b7c73eSJoe Perches					$herectx = $here . "\n";
480863b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
480963b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
481063b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
481163b7c73eSJoe Perches						$herectx .=  $rl . "\n";
481263b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
481363b7c73eSJoe Perches					}
481463b7c73eSJoe Perches				}
481563b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
481663b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
481763b7c73eSJoe Perches			}
481863b7c73eSJoe Perches		}
481963b7c73eSJoe Perches
48200a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
48214a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
48220a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
48233705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
48243705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
48253705ce5bSJoe Perches			    $fix) {
4826194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48273705ce5bSJoe Perches				    s/^(.)\s+/$1/;
48283705ce5bSJoe Perches			}
48290a920b5bSAndy Whitcroft		}
48300a920b5bSAndy Whitcroft
48315b9553abSJoe Perches# return is not a function
4832507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4833c45dcabdSAndy Whitcroft			my $spacing = $1;
48345b57980dSJoe Perches			if ($perl_version_ok &&
48355b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
48365b9553abSJoe Perches				my $value = $1;
48375b9553abSJoe Perches				$value = deparenthesize($value);
48385b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4839000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4840000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
48415b9553abSJoe Perches				}
4842c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4843000d1cc1SJoe Perches				ERROR("SPACING",
4844000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4845c45dcabdSAndy Whitcroft			}
4846c45dcabdSAndy Whitcroft		}
4847507e5141SJoe Perches
4848b43ae21bSJoe Perches# unnecessary return in a void function
4849b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4850b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4851b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4852b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4853b43ae21bSJoe Perches		    $linenr >= 3 &&
4854b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4855b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
48569819cf25SJoe Perches			WARN("RETURN_VOID",
4857b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
48589819cf25SJoe Perches               }
48599819cf25SJoe Perches
4860189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
48615b57980dSJoe Perches		if ($perl_version_ok &&
4862189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4863189248d8SJoe Perches			my $openparens = $1;
4864189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4865189248d8SJoe Perches			my $msg = "";
4866189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4867189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4868189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4869189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4870189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4871189248d8SJoe Perches			}
4872189248d8SJoe Perches		}
4873189248d8SJoe Perches
4874c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4875c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4876c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4877c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
48785b57980dSJoe Perches		if ($perl_version_ok &&
4879c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4880c5595fa2SJoe Perches			my $lead = $1;
4881c5595fa2SJoe Perches			my $const = $2;
4882c5595fa2SJoe Perches			my $comp = $3;
4883c5595fa2SJoe Perches			my $to = $4;
4884c5595fa2SJoe Perches			my $newcomp = $comp;
4885f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4886c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4887c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4888c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4889c5595fa2SJoe Perches			    $fix) {
4890c5595fa2SJoe Perches				if ($comp eq "<") {
4891c5595fa2SJoe Perches					$newcomp = ">";
4892c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4893c5595fa2SJoe Perches					$newcomp = ">=";
4894c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4895c5595fa2SJoe Perches					$newcomp = "<";
4896c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4897c5595fa2SJoe Perches					$newcomp = "<=";
4898c5595fa2SJoe Perches				}
4899c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4900c5595fa2SJoe Perches			}
4901c5595fa2SJoe Perches		}
4902c5595fa2SJoe Perches
4903f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4904f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
490553a3c448SAndy Whitcroft			my $name = $1;
490653a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4907000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4908f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
490953a3c448SAndy Whitcroft			}
491053a3c448SAndy Whitcroft		}
4911c45dcabdSAndy Whitcroft
49120a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
49134a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
49143705ce5bSJoe Perches			if (ERROR("SPACING",
49153705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
49163705ce5bSJoe Perches			    $fix) {
4917194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49183705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
49193705ce5bSJoe Perches			}
49200a920b5bSAndy Whitcroft		}
49210a920b5bSAndy Whitcroft
4922f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4923f5fe35ddSAndy Whitcroft# statements after the conditional.
4924170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
49253e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
49263e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
49273e469cdcSAndy Whitcroft					if (!defined $stat);
4928170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4929170d3a22SAndy Whitcroft						$remain_next, $off_next);
4930170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4931170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4932170d3a22SAndy Whitcroft
4933170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4934170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4935170d3a22SAndy Whitcroft				# then count those as offsets.
4936170d3a22SAndy Whitcroft				my ($whitespace) =
4937170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4938170d3a22SAndy Whitcroft				my $offset =
4939170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4940170d3a22SAndy Whitcroft
4941170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4942170d3a22SAndy Whitcroft								$offset} = 1;
4943170d3a22SAndy Whitcroft			}
4944170d3a22SAndy Whitcroft		}
4945170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4946c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4947170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4948171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
49498905a67cSAndy Whitcroft
4950b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4951000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4952000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
49538905a67cSAndy Whitcroft			}
49548905a67cSAndy Whitcroft
49558905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
49568905a67cSAndy Whitcroft			# conditional.
4957773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
49588905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
495913214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
496053210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
496153210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4962773647a0SAndy Whitcroft			{
4963bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4964bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4965bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
496642bdf74cSHidetoshi Seto				my $stat_real = '';
4967bb44ad39SAndy Whitcroft
496842bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
496942bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4970bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4971bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4972bb44ad39SAndy Whitcroft				}
4973bb44ad39SAndy Whitcroft
4974000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4975000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
49768905a67cSAndy Whitcroft			}
49778905a67cSAndy Whitcroft		}
49788905a67cSAndy Whitcroft
497913214adfSAndy Whitcroft# Check for bitwise tests written as boolean
498013214adfSAndy Whitcroft		if ($line =~ /
498113214adfSAndy Whitcroft			(?:
498213214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
498313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
498413214adfSAndy Whitcroft				(?:\&\&|\|\|)
498513214adfSAndy Whitcroft			|
498613214adfSAndy Whitcroft				(?:\&\&|\|\|)
498713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
498813214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
498913214adfSAndy Whitcroft			)/x)
499013214adfSAndy Whitcroft		{
4991000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4992000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
499313214adfSAndy Whitcroft		}
499413214adfSAndy Whitcroft
49958905a67cSAndy Whitcroft# if and else should not have general statements after it
499613214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
499713214adfSAndy Whitcroft			my $s = $1;
499813214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
499913214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5000000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5001000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
50020a920b5bSAndy Whitcroft			}
500313214adfSAndy Whitcroft		}
500439667782SAndy Whitcroft# if should not continue a brace
500539667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5006000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5007048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
500839667782SAndy Whitcroft				$herecurr);
500939667782SAndy Whitcroft		}
5010a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5011a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5012a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
50133fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5014a1080bf8SAndy Whitcroft			\s*return\s+
5015a1080bf8SAndy Whitcroft		    )/xg)
5016a1080bf8SAndy Whitcroft		{
5017000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5018000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5019a1080bf8SAndy Whitcroft		}
50200a920b5bSAndy Whitcroft
50210a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
50220a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
50238b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
50240a920b5bSAndy Whitcroft		    $previndent == $indent) {
50258b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
50268b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
50278b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50288b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
50298b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
50308b8856f4SJoe Perches				my $fixedline = $prevrawline;
50318b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
50328b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
50338b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50348b8856f4SJoe Perches				}
50358b8856f4SJoe Perches				$fixedline = $rawline;
50368b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
50378b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
50388b8856f4SJoe Perches			}
50390a920b5bSAndy Whitcroft		}
50400a920b5bSAndy Whitcroft
50418b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5042c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5043c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5044c2fdda0dSAndy Whitcroft
5045c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5046c2fdda0dSAndy Whitcroft			# conditional.
5047773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5048c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5049c2fdda0dSAndy Whitcroft
5050c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
50518b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
50528b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
50538b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50548b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
50558b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
50568b8856f4SJoe Perches					my $fixedline = $prevrawline;
50578b8856f4SJoe Perches					my $trailing = $rawline;
50588b8856f4SJoe Perches					$trailing =~ s/^\+//;
50598b8856f4SJoe Perches					$trailing = trim($trailing);
50608b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
50618b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50628b8856f4SJoe Perches				}
5063c2fdda0dSAndy Whitcroft			}
5064c2fdda0dSAndy Whitcroft		}
5065c2fdda0dSAndy Whitcroft
506695e2c602SJoe Perches#Specific variable tests
5067323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5068323c1260SJoe Perches			my $var = $1;
506995e2c602SJoe Perches
507095e2c602SJoe Perches#CamelCase
5071807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5072be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
507322735ce8SJoe Perches#Ignore Page<foo> variants
5074807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5075d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5076d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5077d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5078f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5079f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
50807e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
50817e781f67SJoe Perches					my $word = $1;
50827e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5083d8b07710SJoe Perches					if ($check) {
5084d8b07710SJoe Perches						seed_camelcase_includes();
5085d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5086d8b07710SJoe Perches							seed_camelcase_file($realfile);
5087d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5088d8b07710SJoe Perches						}
5089d8b07710SJoe Perches					}
50907e781f67SJoe Perches					if (!defined $camelcase{$word}) {
50917e781f67SJoe Perches						$camelcase{$word} = 1;
5092be79794bSJoe Perches						CHK("CAMELCASE",
50937e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
50947e781f67SJoe Perches					}
5095323c1260SJoe Perches				}
5096323c1260SJoe Perches			}
50973445686aSJoe Perches		}
50980a920b5bSAndy Whitcroft
50990a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5100d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5101d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5102d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5103d5e616fcSJoe Perches			    $fix) {
5104194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5105d5e616fcSJoe Perches			}
51060a920b5bSAndy Whitcroft		}
51070a920b5bSAndy Whitcroft
51080e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
51090e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5110c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5111e09dec48SAndy Whitcroft			my $file = "$1.h";
5112e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5113e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5114e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
51157840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5116c45dcabdSAndy Whitcroft			{
51170e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
51180e212e0aSFabian Frederick				if ($asminclude > 0) {
5119e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5120000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5121000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5122e09dec48SAndy Whitcroft					} else {
5123000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5124000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5125e09dec48SAndy Whitcroft					}
51260a920b5bSAndy Whitcroft				}
51270a920b5bSAndy Whitcroft			}
51280e212e0aSFabian Frederick		}
51290a920b5bSAndy Whitcroft
5130653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5131653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5132cf655043SAndy Whitcroft# in a known good container
5133b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5134b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5135d8aaf121SAndy Whitcroft			my $ln = $linenr;
5136d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5137c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5138c45dcabdSAndy Whitcroft			my $ctx = '';
513908a2843eSJoe Perches			my $has_flow_statement = 0;
514008a2843eSJoe Perches			my $has_arg_concat = 0;
5141c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5142f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5143f74bd194SAndy Whitcroft			$ctx = $dstat;
5144c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5145a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5146c45dcabdSAndy Whitcroft
514708a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
514862e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
514908a2843eSJoe Perches
5150f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5151f59b64bfSJoe Perches			my $define_args = $1;
5152f59b64bfSJoe Perches			my $define_stmt = $dstat;
5153f59b64bfSJoe Perches			my @def_args = ();
5154f59b64bfSJoe Perches
5155f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5156f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5157f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
51588c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5159f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5160f59b64bfSJoe Perches			}
5161f59b64bfSJoe Perches
5162292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5163c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5164c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5165c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5166c45dcabdSAndy Whitcroft
5167c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5168bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5169bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
51706b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5171bf30d6edSAndy Whitcroft			{
5172c45dcabdSAndy Whitcroft			}
5173c45dcabdSAndy Whitcroft
5174342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
517533acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
517633acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5177e45bab8eSAndy Whitcroft			{
5178e45bab8eSAndy Whitcroft			}
5179e45bab8eSAndy Whitcroft
518042e15293SJoe Perches			# Make asm volatile uses seem like a generic function
518142e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
518242e15293SJoe Perches
5183c45dcabdSAndy Whitcroft			my $exceptions = qr{
5184c45dcabdSAndy Whitcroft				$Declare|
5185c45dcabdSAndy Whitcroft				module_param_named|
5186a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5187c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5188c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5189383099fdSAndy Whitcroft				__typeof__\(|
519022fd2d3eSStefani Seibold				union|
519122fd2d3eSStefani Seibold				struct|
5192ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
51936b10df42SVladimir Zapolskiy				^\"|\"$|
51946b10df42SVladimir Zapolskiy				^\[
5195c45dcabdSAndy Whitcroft			}x;
51965eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5197f59b64bfSJoe Perches
5198f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5199f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5200e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5201f59b64bfSJoe Perches
5202f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5203f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5204f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
52053cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5206356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5207f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5208f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5209e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
521072f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5211f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5212f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5213f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
52144e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5215f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5216c45dcabdSAndy Whitcroft			{
5217e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5218e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5219e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5220e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5221f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5222f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5223f74bd194SAndy Whitcroft				} else {
5224000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5225388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5226d8aaf121SAndy Whitcroft				}
5227f59b64bfSJoe Perches
5228f59b64bfSJoe Perches			}
52295207649bSJoe Perches
52305207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
52315207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
52325207649bSJoe Perches			my $first = 1;
52335207649bSJoe Perches			$define_stmt = "";
52345207649bSJoe Perches			foreach my $l (@stmt_array) {
52355207649bSJoe Perches				$l =~ s/\\$//;
52365207649bSJoe Perches				if ($first) {
52375207649bSJoe Perches					$define_stmt = $l;
52385207649bSJoe Perches					$first = 0;
52395207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
52405207649bSJoe Perches					$define_stmt .= substr($l, 1);
52415207649bSJoe Perches				}
52425207649bSJoe Perches			}
52435207649bSJoe Perches			$define_stmt =~ s/$;//g;
52445207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
52455207649bSJoe Perches			$define_stmt = trim($define_stmt);
52465207649bSJoe Perches
5247f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5248f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5249f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
52509192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
52517fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
52526dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
52537fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
52547fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5255d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5256f59b64bfSJoe Perches				if ($use_cnt > 1) {
5257f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5258f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5259f59b64bfSJoe Perches				    }
52609192d41aSJoe Perches# check if any macro arguments may have other precedence issues
52617fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
52629192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
52639192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
52649192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
52659192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
52669192d41aSJoe Perches				}
52670a920b5bSAndy Whitcroft			}
52685023d347SJoe Perches
526908a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
527008a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
527108a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
527208a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5273e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
527408a2843eSJoe Perches
527508a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
527608a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
527708a2843eSJoe Perches			}
527808a2843eSJoe Perches
5279481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
52805023d347SJoe Perches
52815023d347SJoe Perches		} else {
52825023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5283481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5284481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
52855023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
52865023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
52875023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
52885023d347SJoe Perches			}
5289653d4876SAndy Whitcroft		}
52900a920b5bSAndy Whitcroft
5291b13edf7fSJoe Perches# do {} while (0) macro tests:
5292b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5293b13edf7fSJoe Perches# macro should not end with a semicolon
52945b57980dSJoe Perches		if ($perl_version_ok &&
5295b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5296b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5297b13edf7fSJoe Perches			my $ln = $linenr;
5298b13edf7fSJoe Perches			my $cnt = $realcnt;
5299b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5300b13edf7fSJoe Perches			my $ctx = '';
5301b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5302b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5303b13edf7fSJoe Perches			$ctx = $dstat;
5304b13edf7fSJoe Perches
5305b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
53061b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5307b13edf7fSJoe Perches
5308b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5309b13edf7fSJoe Perches				my $stmts = $2;
5310b13edf7fSJoe Perches				my $semis = $3;
5311b13edf7fSJoe Perches
5312b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5313b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5314e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5315b13edf7fSJoe Perches
5316ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5317ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5318b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5319b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5320b13edf7fSJoe Perches				}
5321b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5322b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5323b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5324b13edf7fSJoe Perches				}
5325f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5326f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5327f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5328e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5329f5ef95b1SJoe Perches
5330f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5331f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5332b13edf7fSJoe Perches			}
5333b13edf7fSJoe Perches		}
5334b13edf7fSJoe Perches
5335f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
533613214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
533713214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5338cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
533913214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5340cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5341cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5342aad4f614SJoe Perches				my @allowed = ();
5343aad4f614SJoe Perches				my $allow = 0;
534413214adfSAndy Whitcroft				my $seen = 0;
5345773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5346cf655043SAndy Whitcroft				my $ln = $linenr - 1;
534713214adfSAndy Whitcroft				for my $chunk (@chunks) {
534813214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
534913214adfSAndy Whitcroft
5350773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5351773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5352773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5353773647a0SAndy Whitcroft
5354aad4f614SJoe Perches					$allowed[$allow] = 0;
5355773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5356773647a0SAndy Whitcroft
5357773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5358773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5359773647a0SAndy Whitcroft
5360773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5361cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5362cf655043SAndy Whitcroft
5363773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
536413214adfSAndy Whitcroft
536513214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
536613214adfSAndy Whitcroft
5367aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5368cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5369cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5370aad4f614SJoe Perches						$allowed[$allow] = 1;
537113214adfSAndy Whitcroft					}
537213214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5373cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5374aad4f614SJoe Perches						$allowed[$allow] = 1;
537513214adfSAndy Whitcroft					}
5376cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5377cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5378aad4f614SJoe Perches						$allowed[$allow] = 1;
537913214adfSAndy Whitcroft					}
5380aad4f614SJoe Perches					$allow++;
538113214adfSAndy Whitcroft				}
5382aad4f614SJoe Perches				if ($seen) {
5383aad4f614SJoe Perches					my $sum_allowed = 0;
5384aad4f614SJoe Perches					foreach (@allowed) {
5385aad4f614SJoe Perches						$sum_allowed += $_;
5386aad4f614SJoe Perches					}
5387aad4f614SJoe Perches					if ($sum_allowed == 0) {
5388000d1cc1SJoe Perches						WARN("BRACES",
5389000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5390aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5391aad4f614SJoe Perches						 $seen != $allow) {
5392aad4f614SJoe Perches						CHK("BRACES",
5393aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5394aad4f614SJoe Perches					}
539513214adfSAndy Whitcroft				}
539613214adfSAndy Whitcroft			}
539713214adfSAndy Whitcroft		}
5398773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
539913214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5400cf655043SAndy Whitcroft			my $allowed = 0;
5401f0a594c1SAndy Whitcroft
5402cf655043SAndy Whitcroft			# Check the pre-context.
5403cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5404cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5405cf655043SAndy Whitcroft				$allowed = 1;
5406f0a594c1SAndy Whitcroft			}
5407773647a0SAndy Whitcroft
5408773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5409773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5410773647a0SAndy Whitcroft
5411cf655043SAndy Whitcroft			# Check the condition.
5412cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5413773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5414cf655043SAndy Whitcroft			if (defined $cond) {
5415773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5416cf655043SAndy Whitcroft			}
5417cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5418cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5419cf655043SAndy Whitcroft				$allowed = 1;
5420cf655043SAndy Whitcroft			}
5421cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5422cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5423cf655043SAndy Whitcroft				$allowed = 1;
5424cf655043SAndy Whitcroft			}
5425cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5426cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5427cf655043SAndy Whitcroft				$allowed = 1;
5428cf655043SAndy Whitcroft			}
5429cf655043SAndy Whitcroft			# Check the post-context.
5430cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5431cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5432cf655043SAndy Whitcroft				if (defined $cond) {
5433773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5434cf655043SAndy Whitcroft				}
5435cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5436cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5437cf655043SAndy Whitcroft					$allowed = 1;
5438cf655043SAndy Whitcroft				}
5439cf655043SAndy Whitcroft			}
5440cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5441f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5442e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5443cf655043SAndy Whitcroft
5444000d1cc1SJoe Perches				WARN("BRACES",
5445000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5446f0a594c1SAndy Whitcroft			}
5447f0a594c1SAndy Whitcroft		}
5448f0a594c1SAndy Whitcroft
5449e4c5babdSJoe Perches# check for single line unbalanced braces
545095330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
545195330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5452e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5453e4c5babdSJoe Perches		}
5454e4c5babdSJoe Perches
54550979ae66SJoe Perches# check for unnecessary blank lines around braces
545677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5457f8e58219SJoe Perches			if (CHK("BRACES",
5458f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5459f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5460f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5461f8e58219SJoe Perches			}
54620979ae66SJoe Perches		}
546377b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5464f8e58219SJoe Perches			if (CHK("BRACES",
5465f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5466f8e58219SJoe Perches			    $fix) {
5467f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5468f8e58219SJoe Perches			}
54690979ae66SJoe Perches		}
54700979ae66SJoe Perches
54714a0df2efSAndy Whitcroft# no volatiles please
54726c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
54736c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5474000d1cc1SJoe Perches			WARN("VOLATILE",
54758c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
54764a0df2efSAndy Whitcroft		}
54774a0df2efSAndy Whitcroft
54785e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
54795e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
54805e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
54815e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
548233acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
54835e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
54845e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
54855e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
54865e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
54875e4f6ba5SJoe Perches				     $fix &&
54885e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
54895e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
54905e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
54915e4f6ba5SJoe Perches				my $comma_close = "";
54925e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
54935e4f6ba5SJoe Perches					$comma_close = $1;
54945e4f6ba5SJoe Perches				}
54955e4f6ba5SJoe Perches
54965e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
54975e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
54985e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
54995e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
55005e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
55015e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
55025e4f6ba5SJoe Perches				$fixedline = $rawline;
55035e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
55045e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
55055e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
55065e4f6ba5SJoe Perches				}
55075e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
55085e4f6ba5SJoe Perches			}
55095e4f6ba5SJoe Perches		}
55105e4f6ba5SJoe Perches
55115e4f6ba5SJoe Perches# check for missing a space in a string concatenation
55125e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
55135e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
55145e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
55155e4f6ba5SJoe Perches		}
55165e4f6ba5SJoe Perches
551777cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5518e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5519e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5520e4b7d309SJoe Perches# function declarations
552177cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
552277cb8546SJoe Perches		    defined($context_function) &&
5523e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5524e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
552577cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5526e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
552777cb8546SJoe Perches		}
552877cb8546SJoe Perches
55295e4f6ba5SJoe Perches# check for spaces before a quoted newline
55305e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
55315e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
55325e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
55335e4f6ba5SJoe Perches			    $fix) {
55345e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
55355e4f6ba5SJoe Perches			}
55365e4f6ba5SJoe Perches
55375e4f6ba5SJoe Perches		}
55385e4f6ba5SJoe Perches
5539f17dba4fSJoe Perches# concatenated string without spaces between elements
554079682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
554179682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
554279682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
554379682c0cSJoe Perches			    $fix) {
554479682c0cSJoe Perches				while ($line =~ /($String)/g) {
554579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
554679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
554779682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
554879682c0cSJoe Perches				}
554979682c0cSJoe Perches			}
5550f17dba4fSJoe Perches		}
5551f17dba4fSJoe Perches
555290ad30e5SJoe Perches# uncoalesced string fragments
555333acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
555479682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
555579682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
555679682c0cSJoe Perches			    $fix) {
555779682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
555879682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
555979682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
556079682c0cSJoe Perches				}
556179682c0cSJoe Perches			}
556290ad30e5SJoe Perches		}
556390ad30e5SJoe Perches
5564522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5565522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5566522b837cSAlexey Dobriyan		my $show_Z = 1;
55675e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5568522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
55695e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5570522b837cSAlexey Dobriyan			# check for %L
5571522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
55725e4f6ba5SJoe Perches				WARN("PRINTF_L",
5573522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5574522b837cSAlexey Dobriyan				$show_L = 0;
55755e4f6ba5SJoe Perches			}
5576522b837cSAlexey Dobriyan			# check for %Z
5577522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5578522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5579522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5580522b837cSAlexey Dobriyan				$show_Z = 0;
5581522b837cSAlexey Dobriyan			}
5582522b837cSAlexey Dobriyan			# check for 0x<decimal>
5583522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5584522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
55856e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
55866e300757SJoe Perches			}
55875e4f6ba5SJoe Perches		}
55885e4f6ba5SJoe Perches
55895e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
55903f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
55915e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
55925e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
55935e4f6ba5SJoe Perches		}
55945e4f6ba5SJoe Perches
559500df344fSAndy Whitcroft# warn about #if 0
5596c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
559760f89010SPrakruthi Deepak Heragu			WARN("IF_0",
559860f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
559960f89010SPrakruthi Deepak Heragu		}
560060f89010SPrakruthi Deepak Heragu
560160f89010SPrakruthi Deepak Heragu# warn about #if 1
560260f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
560360f89010SPrakruthi Deepak Heragu			WARN("IF_1",
560460f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
56054a0df2efSAndy Whitcroft		}
56064a0df2efSAndy Whitcroft
560703df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
560803df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5609100425deSJoe Perches			my $tested = quotemeta($1);
5610100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5611100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5612100425deSJoe Perches				my $func = $1;
5613100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5614100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5615100425deSJoe Perches				    $fix) {
5616100425deSJoe Perches					my $do_fix = 1;
5617100425deSJoe Perches					my $leading_tabs = "";
5618100425deSJoe Perches					my $new_leading_tabs = "";
5619100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5620100425deSJoe Perches						$leading_tabs = $1;
5621100425deSJoe Perches					} else {
5622100425deSJoe Perches						$do_fix = 0;
5623100425deSJoe Perches					}
5624100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5625100425deSJoe Perches						$new_leading_tabs = $1;
5626100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5627100425deSJoe Perches							$do_fix = 0;
5628100425deSJoe Perches						}
5629100425deSJoe Perches					} else {
5630100425deSJoe Perches						$do_fix = 0;
5631100425deSJoe Perches					}
5632100425deSJoe Perches					if ($do_fix) {
5633100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5634100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5635100425deSJoe Perches					}
5636100425deSJoe Perches				}
56374c432a8fSGreg Kroah-Hartman			}
56384c432a8fSGreg Kroah-Hartman		}
5639f0a594c1SAndy Whitcroft
5640ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5641ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5642ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5643ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5644ebfdc409SJoe Perches		    $linenr > 3) {
5645ebfdc409SJoe Perches			my $testval = $2;
5646ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5647ebfdc409SJoe Perches
5648ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5649ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5650ebfdc409SJoe Perches
5651e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5652e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5653ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5654ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5655ebfdc409SJoe Perches			}
5656ebfdc409SJoe Perches		}
5657ebfdc409SJoe Perches
5658f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5659dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5660f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5661f78d98f6SJoe Perches			my $level = $1;
5662f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5663f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5664f78d98f6SJoe Perches			    $fix) {
5665f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5666f78d98f6SJoe Perches			}
5667f78d98f6SJoe Perches		}
5668f78d98f6SJoe Perches
566945c55e92SJoe Perches# check for logging continuations
567045c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
567145c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
567245c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
567345c55e92SJoe Perches		}
567445c55e92SJoe Perches
5675abb08a53SJoe Perches# check for mask then right shift without a parentheses
56765b57980dSJoe Perches		if ($perl_version_ok &&
5677abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5678abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5679abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5680abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5681abb08a53SJoe Perches		}
5682abb08a53SJoe Perches
5683b75ac618SJoe Perches# check for pointer comparisons to NULL
56845b57980dSJoe Perches		if ($perl_version_ok) {
5685b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5686b75ac618SJoe Perches				my $val = $1;
5687b75ac618SJoe Perches				my $equal = "!";
5688b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5689b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5690b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5691b75ac618SJoe Perches					    $fix) {
5692b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5693b75ac618SJoe Perches				}
5694b75ac618SJoe Perches			}
5695b75ac618SJoe Perches		}
5696b75ac618SJoe Perches
56978716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
56988716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
56998716de38SJoe Perches			my $attr = $1;
57008716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
57018716de38SJoe Perches				my $ptr = $1;
57028716de38SJoe Perches				my $var = $2;
57038716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
57048716de38SJoe Perches				      ERROR("MISPLACED_INIT",
57058716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
57068716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
57078716de38SJoe Perches				      WARN("MISPLACED_INIT",
57088716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
57098716de38SJoe Perches				    $fix) {
5710194f66fcSJoe 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;
57118716de38SJoe Perches				}
57128716de38SJoe Perches			}
57138716de38SJoe Perches		}
57148716de38SJoe Perches
5715e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5716e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5717e970b884SJoe Perches			my $attr = $1;
5718e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5719e970b884SJoe Perches			my $attr_prefix = $1;
5720e970b884SJoe Perches			my $attr_type = $2;
5721e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5722e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5723e970b884SJoe Perches			    $fix) {
5724194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5725e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5726e970b884SJoe Perches			}
5727e970b884SJoe Perches		}
5728e970b884SJoe Perches
5729e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5730e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5731e970b884SJoe Perches			my $attr = $1;
5732e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5733e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5734e970b884SJoe Perches			    $fix) {
5735194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5736e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5737e970b884SJoe Perches				$lead = rtrim($1);
5738e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5739e970b884SJoe Perches				$lead = "${lead}const ";
5740194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5741e970b884SJoe Perches			}
5742e970b884SJoe Perches		}
5743e970b884SJoe Perches
5744c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5745c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5746c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5747c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5748c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5749c17893c7SJoe Perches			    $fix) {
5750c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5751c17893c7SJoe Perches			}
5752c17893c7SJoe Perches		}
5753c17893c7SJoe Perches
5754fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5755fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5756fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5757fbdb8138SJoe Perches			my $constant_func = $1;
5758fbdb8138SJoe Perches			my $func = $constant_func;
5759fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5760fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5761fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5762fbdb8138SJoe Perches			    $fix) {
5763194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5764fbdb8138SJoe Perches			}
5765fbdb8138SJoe Perches		}
5766fbdb8138SJoe Perches
57671a15a250SPatrick Pannuto# prefer usleep_range over udelay
576837581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
576943c1d77cSJoe Perches			my $delay = $1;
57701a15a250SPatrick Pannuto			# ignore udelay's < 10, however
577143c1d77cSJoe Perches			if (! ($delay < 10) ) {
5772000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5773458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
577443c1d77cSJoe Perches			}
577543c1d77cSJoe Perches			if ($delay > 2000) {
577643c1d77cSJoe Perches				WARN("LONG_UDELAY",
577743c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
57781a15a250SPatrick Pannuto			}
57791a15a250SPatrick Pannuto		}
57801a15a250SPatrick Pannuto
578109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
578209ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
578309ef8725SPatrick Pannuto			if ($1 < 20) {
5784000d1cc1SJoe Perches				WARN("MSLEEP",
5785458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
578609ef8725SPatrick Pannuto			}
578709ef8725SPatrick Pannuto		}
578809ef8725SPatrick Pannuto
578936ec1939SJoe Perches# check for comparisons of jiffies
579036ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
579136ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
579236ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
579336ec1939SJoe Perches		}
579436ec1939SJoe Perches
57959d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
57969d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
57979d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
57989d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
57999d7a34a5SJoe Perches		}
58009d7a34a5SJoe Perches
580100df344fSAndy Whitcroft# warn about #ifdefs in C files
5802c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
580300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
580400df344fSAndy Whitcroft#			print "$herecurr";
580500df344fSAndy Whitcroft#			$clean = 0;
580600df344fSAndy Whitcroft#		}
580700df344fSAndy Whitcroft
580822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5809c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
58103705ce5bSJoe Perches			if (ERROR("SPACING",
58113705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
58123705ce5bSJoe Perches			    $fix) {
5813194f66fcSJoe Perches				$fixed[$fixlinenr] =~
58143705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
58153705ce5bSJoe Perches			}
58163705ce5bSJoe Perches
581722f2a2efSAndy Whitcroft		}
581822f2a2efSAndy Whitcroft
58194a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5820171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5821171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
58224a0df2efSAndy Whitcroft			my $which = $1;
58234a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5824000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5825000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
58264a0df2efSAndy Whitcroft			}
58274a0df2efSAndy Whitcroft		}
58284a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5829402c2553SMichael S. Tsirkin
5830402c2553SMichael S. Tsirkin		my $barriers = qr{
5831402c2553SMichael S. Tsirkin			mb|
5832402c2553SMichael S. Tsirkin			rmb|
5833402c2553SMichael S. Tsirkin			wmb|
5834402c2553SMichael S. Tsirkin			read_barrier_depends
5835402c2553SMichael S. Tsirkin		}x;
5836402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5837402c2553SMichael S. Tsirkin			mb__before_atomic|
5838402c2553SMichael S. Tsirkin			mb__after_atomic|
5839402c2553SMichael S. Tsirkin			store_release|
5840402c2553SMichael S. Tsirkin			load_acquire|
5841402c2553SMichael S. Tsirkin			store_mb|
5842402c2553SMichael S. Tsirkin			(?:$barriers)
5843402c2553SMichael S. Tsirkin		}x;
5844402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5845402c2553SMichael S. Tsirkin			(?:$barriers)|
584643e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
584743e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5848402c2553SMichael S. Tsirkin		}x;
5849402c2553SMichael S. Tsirkin
5850402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
58514a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5852c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5853000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
58544a0df2efSAndy Whitcroft			}
58554a0df2efSAndy Whitcroft		}
58563ad81779SPaul E. McKenney
5857f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5858f4073b0fSMichael S. Tsirkin
5859f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5860f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5861f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5862f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5863f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5864f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5865f4073b0fSMichael S. Tsirkin		}
5866f4073b0fSMichael S. Tsirkin
5867cb426e99SJoe Perches# check for waitqueue_active without a comment.
5868cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5869cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5870cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5871cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5872cb426e99SJoe Perches			}
5873cb426e99SJoe Perches		}
58743ad81779SPaul E. McKenney
587591db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
587691db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
587791db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
587891db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
587991db2592SPaul E. McKenney		}
588091db2592SPaul E. McKenney
58814a0df2efSAndy Whitcroft# check of hardware specific defines
5882c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5883000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5884000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
58850a920b5bSAndy Whitcroft		}
5886653d4876SAndy Whitcroft
5887596ed45bSJoe Perches# check that the storage class is not after a type
5888596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5889000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5890596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5891596ed45bSJoe Perches		}
5892596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5893596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5894596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5895596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5896596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5897596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5898596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5899d4977c78STobias Klauser		}
5900d4977c78STobias Klauser
5901de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5902de7d4f0eSAndy Whitcroft# storage class and type.
59039c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
59049c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5905000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5906000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5907de7d4f0eSAndy Whitcroft		}
5908de7d4f0eSAndy Whitcroft
59098905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
59102b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59112b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5912d5e616fcSJoe Perches			if (WARN("INLINE",
5913d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5914d5e616fcSJoe Perches			    $fix) {
5915194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5916d5e616fcSJoe Perches
5917d5e616fcSJoe Perches			}
59188905a67cSAndy Whitcroft		}
59198905a67cSAndy Whitcroft
59203d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
59212b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59222b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5923000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5924000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
59253d130fd0SJoe Perches		}
59263d130fd0SJoe Perches
592739b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
59282b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59292b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5930000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5931000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
593239b7e287SJoe Perches		}
593339b7e287SJoe Perches
5934462811d9SJoe Perches# Check for __attribute__ section, prefer __section
5935462811d9SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5936462811d9SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5937462811d9SJoe Perches			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5938462811d9SJoe Perches			my $new = substr($old, 1, -1);
5939462811d9SJoe Perches			if (WARN("PREFER_SECTION",
5940462811d9SJoe Perches				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5941462811d9SJoe Perches			    $fix) {
5942462811d9SJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5943462811d9SJoe Perches			}
5944462811d9SJoe Perches		}
5945462811d9SJoe Perches
59465f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
59472b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59482b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5949d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5950d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5951d5e616fcSJoe Perches			    $fix) {
5952194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5953d5e616fcSJoe Perches
5954d5e616fcSJoe Perches			}
59555f14d3bdSJoe Perches		}
59565f14d3bdSJoe Perches
59576061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
59582b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59592b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5960d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5961d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5962d5e616fcSJoe Perches			    $fix) {
5963194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5964d5e616fcSJoe Perches			}
59656061d949SJoe Perches		}
59666061d949SJoe Perches
5967619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
59685b57980dSJoe Perches		if ($perl_version_ok &&
5969619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5970619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5971619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5972619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5973619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5974619a908aSJoe Perches		}
5975619a908aSJoe Perches
5976fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5977e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5978fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5979e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5980e6176fa4SJoe Perches			my $type = $1;
5981e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5982e6176fa4SJoe Perches				$type = $1;
5983e6176fa4SJoe Perches				my $kernel_type = 'u';
5984e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5985e6176fa4SJoe Perches				$type =~ /(\d+)/;
5986e6176fa4SJoe Perches				$kernel_type .= $1;
5987e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5988e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5989e6176fa4SJoe Perches				    $fix) {
5990e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5991e6176fa4SJoe Perches				}
5992e6176fa4SJoe Perches			}
5993e6176fa4SJoe Perches		}
5994e6176fa4SJoe Perches
5995938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5996938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5997938224b5SJoe Perches			my $cast = $1;
5998938224b5SJoe Perches			my $const = $2;
5999938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
6000938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6001938224b5SJoe Perches			    $fix) {
6002938224b5SJoe Perches				my $suffix = "";
6003938224b5SJoe Perches				my $newconst = $const;
6004938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
6005938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6006938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
6007938224b5SJoe Perches					$suffix .= 'LL';
6008938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
6009938224b5SJoe Perches					$suffix .= 'L';
6010938224b5SJoe Perches				}
6011938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6012938224b5SJoe Perches			}
6013938224b5SJoe Perches		}
6014938224b5SJoe Perches
60158f53a9b8SJoe Perches# check for sizeof(&)
60168f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6017000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6018000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
60198f53a9b8SJoe Perches		}
60208f53a9b8SJoe Perches
602166c80b60SJoe Perches# check for sizeof without parenthesis
602266c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6023d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6024d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6025d5e616fcSJoe Perches			    $fix) {
6026194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6027d5e616fcSJoe Perches			}
602866c80b60SJoe Perches		}
602966c80b60SJoe Perches
603088982feaSJoe Perches# check for struct spinlock declarations
603188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
603288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
603388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
603488982feaSJoe Perches		}
603588982feaSJoe Perches
6036a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
603706668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6038a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6039caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6040caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6041d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6042d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6043d5e616fcSJoe Perches				    $fix) {
6044194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6045d5e616fcSJoe Perches				}
6046a6962d72SJoe Perches			}
6047a6962d72SJoe Perches		}
6048a6962d72SJoe Perches
60490b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
60505b57980dSJoe Perches		if ($perl_version_ok &&
60510b523769SJoe Perches		    defined $stat &&
60520b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
60530b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6054e3c6bc95STobin C. Harding			my $stat_real;
6055e3c6bc95STobin C. Harding
60560b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
60570b523769SJoe Perches			$lc = $lc + $linenr;
60580b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6059ffe07513SJoe Perches				my $specifier;
6060ffe07513SJoe Perches				my $extension;
60613bd32d6aSSakari Ailus				my $qualifier;
6062ffe07513SJoe Perches				my $bad_specifier = "";
60630b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
60640b523769SJoe Perches				$fmt =~ s/%%//g;
6065e3c6bc95STobin C. Harding
60663bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6067e3c6bc95STobin C. Harding					$specifier = $1;
6068e3c6bc95STobin C. Harding					$extension = $2;
60693bd32d6aSSakari Ailus					$qualifier = $3;
6070361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
60713bd32d6aSSakari Ailus					    ($extension eq "f" &&
60723bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6073e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
60740b523769SJoe Perches						last;
60750b523769SJoe Perches					}
6076e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6077e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6078e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
60790b523769SJoe Perches						}
6080e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6081e3c6bc95STobin 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");
6082e3c6bc95STobin C. Harding					}
6083e3c6bc95STobin C. Harding				}
6084e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
60852a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
60861df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
60871df7338aSSergey Senozhatsky					my $use = "";
6088e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
60891df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6090e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
60911df7338aSSergey Senozhatsky					}
60922a9f9d85STobin C. Harding
60930b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6094e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6095e3c6bc95STobin C. Harding				}
60960b523769SJoe Perches			}
60970b523769SJoe Perches		}
60980b523769SJoe Perches
6099554e165cSAndy Whitcroft# Check for misused memsets
61005b57980dSJoe Perches		if ($perl_version_ok &&
6101d1fe9c09SJoe Perches		    defined $stat &&
61029e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6103554e165cSAndy Whitcroft
6104d7c76ba7SJoe Perches			my $ms_addr = $2;
6105d1fe9c09SJoe Perches			my $ms_val = $7;
6106d1fe9c09SJoe Perches			my $ms_size = $12;
6107d7c76ba7SJoe Perches
6108554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6109554e165cSAndy Whitcroft				ERROR("MEMSET",
6110d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6111554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6112554e165cSAndy Whitcroft				WARN("MEMSET",
6113d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6114d7c76ba7SJoe Perches			}
6115d7c76ba7SJoe Perches		}
6116d7c76ba7SJoe Perches
611798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
61185b57980dSJoe Perches#		if ($perl_version_ok &&
6119f333195dSJoe Perches#		    defined $stat &&
6120f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6121f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6122f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6123f333195dSJoe Perches#			    $fix) {
6124f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6125f333195dSJoe Perches#			}
6126f333195dSJoe Perches#		}
612798a9bba5SJoe Perches
6128b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
61295b57980dSJoe Perches#		if ($perl_version_ok &&
6130f333195dSJoe Perches#		    defined $stat &&
6131f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6132f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6133f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6134f333195dSJoe Perches#		}
6135b6117d17SMateusz Kulikowski
61368617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
61378617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
61385b57980dSJoe Perches#		if ($perl_version_ok &&
6139f333195dSJoe Perches#		    defined $stat &&
6140f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6141f333195dSJoe Perches#
6142f333195dSJoe Perches#			my $ms_val = $7;
6143f333195dSJoe Perches#
6144f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6145f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6146f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6147f333195dSJoe Perches#				    $fix) {
6148f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6149f333195dSJoe Perches#				}
6150f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6151f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6152f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6153f333195dSJoe Perches#				    $fix) {
6154f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6155f333195dSJoe Perches#				}
6156f333195dSJoe Perches#			}
6157f333195dSJoe Perches#		}
61588617cd09SMateusz Kulikowski
6159d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
61605b57980dSJoe Perches		if ($perl_version_ok &&
6161d1fe9c09SJoe Perches		    defined $stat &&
6162d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6163d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6164d7c76ba7SJoe Perches				my $call = $1;
6165d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6166d7c76ba7SJoe Perches				my $arg1 = $3;
6167d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6168d1fe9c09SJoe Perches				my $arg2 = $8;
6169d7c76ba7SJoe Perches				my $cast;
6170d7c76ba7SJoe Perches
6171d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6172d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6173d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6174d7c76ba7SJoe Perches					$cast = $cast1;
6175d7c76ba7SJoe Perches				} else {
6176d7c76ba7SJoe Perches					$cast = $cast2;
6177d7c76ba7SJoe Perches				}
6178d7c76ba7SJoe Perches				WARN("MINMAX",
6179d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6180554e165cSAndy Whitcroft			}
6181554e165cSAndy Whitcroft		}
6182554e165cSAndy Whitcroft
61834a273195SJoe Perches# check usleep_range arguments
61845b57980dSJoe Perches		if ($perl_version_ok &&
61854a273195SJoe Perches		    defined $stat &&
61864a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
61874a273195SJoe Perches			my $min = $1;
61884a273195SJoe Perches			my $max = $7;
61894a273195SJoe Perches			if ($min eq $max) {
61904a273195SJoe Perches				WARN("USLEEP_RANGE",
6191458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61924a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
61934a273195SJoe Perches				 $min > $max) {
61944a273195SJoe Perches				WARN("USLEEP_RANGE",
6195458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61964a273195SJoe Perches			}
61974a273195SJoe Perches		}
61984a273195SJoe Perches
6199823b794cSJoe Perches# check for naked sscanf
62005b57980dSJoe Perches		if ($perl_version_ok &&
6201823b794cSJoe Perches		    defined $stat &&
62026c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6203823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6204823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6205823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6206823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6207823b794cSJoe Perches			$lc = $lc + $linenr;
62082a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6209823b794cSJoe Perches			WARN("NAKED_SSCANF",
6210823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6211823b794cSJoe Perches		}
6212823b794cSJoe Perches
6213afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
62145b57980dSJoe Perches		if ($perl_version_ok &&
6215afc819abSJoe Perches		    defined $stat &&
6216afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6217afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6218afc819abSJoe Perches			$lc = $lc + $linenr;
62192a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6220afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6221afc819abSJoe Perches				my $format = $6;
6222afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6223afc819abSJoe Perches				if ($count == 1 &&
6224afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6225afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6226afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6227afc819abSJoe Perches				}
6228afc819abSJoe Perches			}
6229afc819abSJoe Perches		}
6230afc819abSJoe Perches
623170dc8a48SJoe Perches# check for new externs in .h files.
623270dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
623370dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6234d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
623570dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
623670dc8a48SJoe Perches			    $fix) {
6237194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
623870dc8a48SJoe Perches			}
623970dc8a48SJoe Perches		}
624070dc8a48SJoe Perches
6241de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6242171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6243c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6244171ae1a4SAndy Whitcroft		{
6245c45dcabdSAndy Whitcroft			my $function_name = $1;
6246c45dcabdSAndy Whitcroft			my $paren_space = $2;
6247171ae1a4SAndy Whitcroft
6248171ae1a4SAndy Whitcroft			my $s = $stat;
6249171ae1a4SAndy Whitcroft			if (defined $cond) {
6250171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6251171ae1a4SAndy Whitcroft			}
6252c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6253c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6254c45dcabdSAndy Whitcroft			{
6255000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6256000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6257de7d4f0eSAndy Whitcroft			}
6258de7d4f0eSAndy Whitcroft
6259171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6260000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6261000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6262171ae1a4SAndy Whitcroft			}
62639c9ba34eSAndy Whitcroft
62649c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
62659c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
62669c9ba34eSAndy Whitcroft		{
6267000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6268000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6269171ae1a4SAndy Whitcroft		}
6270171ae1a4SAndy Whitcroft
6271a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6272a0ad7596SJoe Perches		if (defined $stat &&
627325bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6274ca0d8929SJoe Perches		    $1 ne "void") {
6275ca0d8929SJoe Perches			my $args = trim($1);
6276ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6277ca0d8929SJoe Perches				my $arg = trim($1);
6278ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6279ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6280ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6281ca0d8929SJoe Perches				}
6282ca0d8929SJoe Perches			}
6283ca0d8929SJoe Perches		}
6284ca0d8929SJoe Perches
6285a0ad7596SJoe Perches# check for function definitions
62865b57980dSJoe Perches		if ($perl_version_ok &&
6287a0ad7596SJoe Perches		    defined $stat &&
6288a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6289a0ad7596SJoe Perches			$context_function = $1;
6290a0ad7596SJoe Perches
6291a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6292a0ad7596SJoe Perches			my $ok = 0;
6293a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6294a0ad7596SJoe Perches			my $herectx = $here . "\n";
6295a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6296a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6297a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6298a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6299a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6300a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6301a0ad7596SJoe Perches			}
6302a0ad7596SJoe Perches			if (!$ok) {
6303a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6304a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6305a0ad7596SJoe Perches			}
6306a0ad7596SJoe Perches		}
6307a0ad7596SJoe Perches
6308de7d4f0eSAndy Whitcroft# checks for new __setup's
6309de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6310de7d4f0eSAndy Whitcroft			my $name = $1;
6311de7d4f0eSAndy Whitcroft
6312de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6313000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
63148c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6315de7d4f0eSAndy Whitcroft			}
6316653d4876SAndy Whitcroft		}
63179c0ca6f9SAndy Whitcroft
6318e29a70f1SJoe Perches# check for pointless casting of alloc functions
6319e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6320000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6321000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
63229c0ca6f9SAndy Whitcroft		}
632313214adfSAndy Whitcroft
6324a640d25cSJoe Perches# alloc style
6325a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
63265b57980dSJoe Perches		if ($perl_version_ok &&
6327e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6328a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6329a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6330a640d25cSJoe Perches		}
6331a640d25cSJoe Perches
633260a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
63335b57980dSJoe Perches		if ($perl_version_ok &&
63341b4a2ed4SJoe Perches		    defined $stat &&
63351b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
633660a55369SJoe Perches			my $oldfunc = $3;
633760a55369SJoe Perches			my $a1 = $4;
633860a55369SJoe Perches			my $a2 = $10;
633960a55369SJoe Perches			my $newfunc = "kmalloc_array";
634060a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
634160a55369SJoe Perches			my $r1 = $a1;
634260a55369SJoe Perches			my $r2 = $a2;
634360a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
634460a55369SJoe Perches				$r1 = $a2;
634560a55369SJoe Perches				$r2 = $a1;
634660a55369SJoe Perches			}
6347e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6348e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
63491b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6350e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6351e3d95a2aSTobin C. Harding
6352e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
63531b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
63541b4a2ed4SJoe Perches				    $cnt == 1 &&
6355e367455aSJoe Perches				    $fix) {
6356194f66fcSJoe 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;
635760a55369SJoe Perches				}
635860a55369SJoe Perches			}
635960a55369SJoe Perches		}
636060a55369SJoe Perches
6361972fdea2SJoe Perches# check for krealloc arg reuse
63625b57980dSJoe Perches		if ($perl_version_ok &&
63634cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
63644cab63ceSJoe Perches		    $1 eq $3) {
6365972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6366972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6367972fdea2SJoe Perches		}
6368972fdea2SJoe Perches
63695ce59ae0SJoe Perches# check for alloc argument mismatch
63705ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
63715ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
63725ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
63735ce59ae0SJoe Perches		}
63745ce59ae0SJoe Perches
6375caf2a54fSJoe Perches# check for multiple semicolons
6376caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6377d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6378d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6379d5e616fcSJoe Perches			    $fix) {
6380194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6381d5e616fcSJoe Perches			}
6382d1e2ad07SJoe Perches		}
6383d1e2ad07SJoe Perches
6384cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6385cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6386cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
63870ab90191SJoe Perches			my $ull = "";
63880ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
63890ab90191SJoe Perches			if (CHK("BIT_MACRO",
63900ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
63910ab90191SJoe Perches			    $fix) {
63920ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
63930ab90191SJoe Perches			}
63940ab90191SJoe Perches		}
63950ab90191SJoe Perches
63962d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
63972d632745SJoe 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*$/) {
63982d632745SJoe Perches			my $config = $1;
63992d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
64002d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
64012d632745SJoe Perches			    $fix) {
64022d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
64032d632745SJoe Perches			}
64042d632745SJoe Perches		}
64052d632745SJoe Perches
6406e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6407c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6408c34c09a8SJoe Perches			my $has_break = 0;
6409c34c09a8SJoe Perches			my $has_statement = 0;
6410c34c09a8SJoe Perches			my $count = 0;
6411c34c09a8SJoe Perches			my $prevline = $linenr;
6412e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6413c34c09a8SJoe Perches				$prevline--;
6414c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6415c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6416c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6417c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6418c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6419c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6420c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6421c34c09a8SJoe Perches				$has_statement = 1;
6422c34c09a8SJoe Perches				$count++;
6423258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6424c34c09a8SJoe Perches			}
6425c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6426c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6427224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6428c34c09a8SJoe Perches			}
6429c34c09a8SJoe Perches		}
6430c34c09a8SJoe Perches
6431f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
6432f36d3eb8SJoe Perches		my @fallthroughs = (
6433f36d3eb8SJoe Perches			'fallthrough',
6434f36d3eb8SJoe Perches			'@fallthrough@',
6435f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
6436f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6437f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6438f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6439f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6440f36d3eb8SJoe Perches		    );
6441f36d3eb8SJoe Perches		if ($raw_comment ne '') {
6442f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
6443f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
6444f36d3eb8SJoe Perches					my $msg_level = \&WARN;
6445f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
6446f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
6447f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6448f36d3eb8SJoe Perches					last;
6449f36d3eb8SJoe Perches				}
6450f36d3eb8SJoe Perches			}
6451f36d3eb8SJoe Perches		}
6452f36d3eb8SJoe Perches
6453d1e2ad07SJoe Perches# check for switch/default statements without a break;
64545b57980dSJoe Perches		if ($perl_version_ok &&
6455d1e2ad07SJoe Perches		    defined $stat &&
6456d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6457d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6458e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6459e3d95a2aSTobin C. Harding
6460d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6461d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6462caf2a54fSJoe Perches		}
6463caf2a54fSJoe Perches
646413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6465d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6466d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6467d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6468d5e616fcSJoe Perches			    $fix) {
6469194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6470d5e616fcSJoe Perches			}
647113214adfSAndy Whitcroft		}
6472773647a0SAndy Whitcroft
647362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
647462ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
647562ec818fSJoe Perches			ERROR("DATE_TIME",
647662ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
647762ec818fSJoe Perches		}
647862ec818fSJoe Perches
64792c92488aSJoe Perches# check for use of yield()
64802c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
64812c92488aSJoe Perches			WARN("YIELD",
64822c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
64832c92488aSJoe Perches		}
64842c92488aSJoe Perches
6485179f8f40SJoe Perches# check for comparisons against true and false
6486179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6487179f8f40SJoe Perches			my $lead = $1;
6488179f8f40SJoe Perches			my $arg = $2;
6489179f8f40SJoe Perches			my $test = $3;
6490179f8f40SJoe Perches			my $otype = $4;
6491179f8f40SJoe Perches			my $trail = $5;
6492179f8f40SJoe Perches			my $op = "!";
6493179f8f40SJoe Perches
6494179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6495179f8f40SJoe Perches
6496179f8f40SJoe Perches			my $type = lc($otype);
6497179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6498179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6499179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6500179f8f40SJoe Perches					$op = "";
6501179f8f40SJoe Perches				}
6502179f8f40SJoe Perches
6503179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6504179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6505179f8f40SJoe Perches
6506179f8f40SJoe Perches## maybe suggesting a correct construct would better
6507179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6508179f8f40SJoe Perches
6509179f8f40SJoe Perches			}
6510179f8f40SJoe Perches		}
6511179f8f40SJoe Perches
65124882720bSThomas Gleixner# check for semaphores initialized locked
65134882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6514000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6515000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6516773647a0SAndy Whitcroft		}
65176712d858SJoe Perches
651867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
651967d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6520000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
652167d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6522773647a0SAndy Whitcroft		}
65236712d858SJoe Perches
6524ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6525f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6526000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6527ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6528f3db6639SMichael Ellerman		}
65296712d858SJoe Perches
65303d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
65313d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
65323d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
65333d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
65343d709ab5SPaul E. McKenney		}
65353d709ab5SPaul E. McKenney
65369189c7e7SJoe Perches# check for deprecated apis
65379189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
65389189c7e7SJoe Perches			my $deprecated_api = $1;
65399189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
65409189c7e7SJoe Perches			WARN("DEPRECATED_API",
65419189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
65429189c7e7SJoe Perches		}
65439189c7e7SJoe Perches
65440f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6545d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
65466903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6547d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6548000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6549d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
65502b6db5cbSAndy Whitcroft		}
6551773647a0SAndy Whitcroft
6552773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6553773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6554773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6555c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6556c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6557171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6558171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6559171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6560773647a0SAndy Whitcroft		{
6561000d1cc1SJoe Perches			WARN("NR_CPUS",
6562000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6563773647a0SAndy Whitcroft		}
65649c9ba34eSAndy Whitcroft
656552ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
656652ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
656752ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
656852ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
656952ea8506SJoe Perches		}
657052ea8506SJoe Perches
6571acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
65725b57980dSJoe Perches		if ($perl_version_ok &&
6573acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6574acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6575acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6576acd9362cSJoe Perches		}
6577acd9362cSJoe Perches
6578de3f186fSDenis Efremov# nested likely/unlikely calls
6579de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6580de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6581de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6582de3f186fSDenis Efremov		}
6583de3f186fSDenis Efremov
6584691d77b6SAndy Whitcroft# whine mightly about in_atomic
6585691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6586691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6587000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6588000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6589f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6590000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6591000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6592691d77b6SAndy Whitcroft			}
6593691d77b6SAndy Whitcroft		}
65941704f47bSPeter Zijlstra
65950f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
65960f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
65970f5225b0SPeter Zijlstra			ERROR("LOCKING",
65980f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
65990f5225b0SPeter Zijlstra		}
66000f5225b0SPeter Zijlstra
66011704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
66021704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
66031704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
66041704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
66051704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
66061704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6607000d1cc1SJoe Perches				ERROR("LOCKDEP",
6608000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
66091704f47bSPeter Zijlstra			}
66101704f47bSPeter Zijlstra		}
661188f8831cSDave Jones
6612b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6613b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6614000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6615000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
661688f8831cSDave Jones		}
66172435880fSJoe Perches
661800180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
661900180468SJoe Perches# and whether or not function naming is typical and if
662000180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
66215b57980dSJoe Perches		if ($perl_version_ok &&
662200180468SJoe Perches		    defined $stat &&
662300180468SJoe 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*\)/) {
662400180468SJoe Perches			my $var = $1;
662500180468SJoe Perches			my $perms = $2;
662600180468SJoe Perches			my $show = $3;
662700180468SJoe Perches			my $store = $4;
662800180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
662900180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
663000180468SJoe Perches			    $store =~ /^${var}_store$/ &&
663100180468SJoe Perches			    $octal_perms eq "0644") {
663200180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
663300180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
663400180468SJoe Perches				    $fix) {
663500180468SJoe 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})/;
663600180468SJoe Perches				}
663700180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
663800180468SJoe Perches				 $store =~ /^NULL$/ &&
663900180468SJoe Perches				 $octal_perms eq "0444") {
664000180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
664100180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
664200180468SJoe Perches				    $fix) {
664300180468SJoe 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})/;
664400180468SJoe Perches				}
664500180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
664600180468SJoe Perches				 $store =~ /^${var}_store$/ &&
664700180468SJoe Perches				 $octal_perms eq "0200") {
664800180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
664900180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
665000180468SJoe Perches				    $fix) {
665100180468SJoe 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})/;
665200180468SJoe Perches				}
665300180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
665400180468SJoe Perches				 $octal_perms eq "0444" ||
665500180468SJoe Perches				 $octal_perms eq "0200") {
665600180468SJoe Perches				my $newshow = "$show";
665700180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
665800180468SJoe Perches				my $newstore = $store;
665900180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
666000180468SJoe Perches				my $rename = "";
666100180468SJoe Perches				if ($show ne $newshow) {
666200180468SJoe Perches					$rename .= " '$show' to '$newshow'";
666300180468SJoe Perches				}
666400180468SJoe Perches				if ($store ne $newstore) {
666500180468SJoe Perches					$rename .= " '$store' to '$newstore'";
666600180468SJoe Perches				}
666700180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
666800180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
666900180468SJoe Perches			} else {
667000180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
667100180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
667200180468SJoe Perches			}
667300180468SJoe Perches		}
667400180468SJoe Perches
6675515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6676515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
667773121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
667873121534SJoe Perches#   specific definition of not visible in sysfs.
667973121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
668073121534SJoe Perches#   use the default permissions
66815b57980dSJoe Perches		if ($perl_version_ok &&
6682459cf0aeSJoe Perches		    defined $stat &&
6683515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
66842435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
66852435880fSJoe Perches				my $func = $entry->[0];
66862435880fSJoe Perches				my $arg_pos = $entry->[1];
66872435880fSJoe Perches
6688459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6689459cf0aeSJoe Perches				$lc = $lc + $linenr;
66902a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6691459cf0aeSJoe Perches
66922435880fSJoe Perches				my $skip_args = "";
66932435880fSJoe Perches				if ($arg_pos > 1) {
66942435880fSJoe Perches					$arg_pos--;
66952435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
66962435880fSJoe Perches				}
6697f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6698459cf0aeSJoe Perches				if ($stat =~ /$test/) {
66992435880fSJoe Perches					my $val = $1;
67002435880fSJoe Perches					$val = $6 if ($skip_args ne "");
670173121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
670273121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
670373121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
67042435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6705459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6706f90774e1SJoe Perches					}
6707f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6708c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6709459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
67102435880fSJoe Perches					}
6711459cf0aeSJoe Perches				}
6712459cf0aeSJoe Perches			}
6713459cf0aeSJoe Perches		}
6714459cf0aeSJoe Perches
6715459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6716bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
671700180468SJoe Perches			my $oval = $1;
671800180468SJoe Perches			my $octal = perms_to_octal($oval);
6719f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6720459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6721f90774e1SJoe Perches			    $fix) {
672200180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
67232435880fSJoe Perches			}
672413214adfSAndy Whitcroft		}
67255a6d20ceSBjorn Andersson
67265a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
67275a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
67285a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
67295a6d20ceSBjorn Andersson			my $valid_licenses = qr{
67305a6d20ceSBjorn Andersson						GPL|
67315a6d20ceSBjorn Andersson						GPL\ v2|
67325a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
67335a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
67345a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
67355a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
67365a6d20ceSBjorn Andersson						Proprietary
67375a6d20ceSBjorn Andersson					}x;
67385a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
67395a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
67405a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
67415a6d20ceSBjorn Andersson			}
67425a6d20ceSBjorn Andersson		}
67436a8d76cbSMatteo Croce
67446a8d76cbSMatteo Croce# check for sysctl duplicate constants
67456a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
67466a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
67476a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
67486a8d76cbSMatteo Croce		}
6749515a235eSJoe Perches	}
675013214adfSAndy Whitcroft
675113214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
675213214adfSAndy Whitcroft	# so just keep quiet.
675313214adfSAndy Whitcroft	if ($#rawlines == -1) {
675413214adfSAndy Whitcroft		exit(0);
67550a920b5bSAndy Whitcroft	}
67560a920b5bSAndy Whitcroft
67578905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
67588905a67cSAndy Whitcroft	# things that appear to be patches.
67598905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
67608905a67cSAndy Whitcroft		exit(0);
67618905a67cSAndy Whitcroft	}
67628905a67cSAndy Whitcroft
67638905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
67648905a67cSAndy Whitcroft	# just keep quiet.
67658905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
67668905a67cSAndy Whitcroft		exit(0);
67678905a67cSAndy Whitcroft	}
67688905a67cSAndy Whitcroft
6769a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6770000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6771000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
67720a920b5bSAndy Whitcroft	}
6773cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6774cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6775000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6776000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6777cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6778cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6779cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6780cd261496SGeert Uytterhoeven		}
67810a920b5bSAndy Whitcroft	}
67820a920b5bSAndy Whitcroft
6783f0a594c1SAndy Whitcroft	print report_dump();
678413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
678513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
67866c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
67876c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
67886c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
67896c72ffaaSAndy Whitcroft	}
67908905a67cSAndy Whitcroft
6791d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6792ef212196SJoe Perches		# If there were any defects found and not already fixing them
6793ef212196SJoe Perches		if (!$clean and !$fix) {
6794ef212196SJoe Perches			print << "EOM"
6795ef212196SJoe Perches
6796ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6797ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6798ef212196SJoe PerchesEOM
6799ef212196SJoe Perches		}
6800d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6801d2c0a235SAndy Whitcroft		# then suggest that.
6802d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6803b0781216SMike Frysinger			$rpt_cleaners = 0;
6804d8469f16SJoe Perches			print << "EOM"
6805d8469f16SJoe Perches
6806d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6807d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6808d8469f16SJoe PerchesEOM
6809d2c0a235SAndy Whitcroft		}
6810d2c0a235SAndy Whitcroft	}
6811d2c0a235SAndy Whitcroft
6812d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6813d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6814d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
68159624b8d6SJoe Perches		my $newfile = $filename;
68169624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
68173705ce5bSJoe Perches		my $linecount = 0;
68183705ce5bSJoe Perches		my $f;
68193705ce5bSJoe Perches
6820d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6821d752fcc8SJoe Perches
68223705ce5bSJoe Perches		open($f, '>', $newfile)
68233705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
68243705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
68253705ce5bSJoe Perches			$linecount++;
68263705ce5bSJoe Perches			if ($file) {
68273705ce5bSJoe Perches				if ($linecount > 3) {
68283705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
68293705ce5bSJoe Perches					print $f $fixed_line . "\n";
68303705ce5bSJoe Perches				}
68313705ce5bSJoe Perches			} else {
68323705ce5bSJoe Perches				print $f $fixed_line . "\n";
68333705ce5bSJoe Perches			}
68343705ce5bSJoe Perches		}
68353705ce5bSJoe Perches		close($f);
68363705ce5bSJoe Perches
68373705ce5bSJoe Perches		if (!$quiet) {
68383705ce5bSJoe Perches			print << "EOM";
6839d8469f16SJoe Perches
68403705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
68413705ce5bSJoe Perches
68423705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
68433705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
68443705ce5bSJoe Perches
68453705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
68463705ce5bSJoe PerchesNo warranties, expressed or implied...
68473705ce5bSJoe PerchesEOM
68483705ce5bSJoe Perches		}
68493705ce5bSJoe Perches	}
68503705ce5bSJoe Perches
6851d8469f16SJoe Perches	if ($quiet == 0) {
6852d8469f16SJoe Perches		print "\n";
6853d8469f16SJoe Perches		if ($clean == 1) {
6854d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6855d8469f16SJoe Perches		} else {
6856d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
68570a920b5bSAndy Whitcroft		}
68580a920b5bSAndy Whitcroft	}
68590a920b5bSAndy Whitcroft	return $clean;
68600a920b5bSAndy Whitcroft}
6861