xref: /linux-6.15/scripts/checkpatch.pl (revision cd28b119)
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';
6777f5b10aSHannes Eder
6877f5b10aSHannes Edersub help {
6977f5b10aSHannes Eder	my ($exitcode) = @_;
7077f5b10aSHannes Eder
7177f5b10aSHannes Eder	print << "EOM";
7277f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7377f5b10aSHannes EderVersion: $V
7477f5b10aSHannes Eder
7577f5b10aSHannes EderOptions:
7677f5b10aSHannes Eder  -q, --quiet                quiet
7777f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7877f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7977f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
8077f5b10aSHannes Eder  --emacs                    emacs compile window format
8177f5b10aSHannes Eder  --terse                    one line per report
8234d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
834a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
844a593c34SDu, Changbin                             single git commit with:
854a593c34SDu, Changbin                               <rev>
864a593c34SDu, Changbin                               <rev>^
874a593c34SDu, Changbin                               <rev>~n
884a593c34SDu, Changbin                             multiple git commits with:
894a593c34SDu, Changbin                               <rev1>..<rev2>
904a593c34SDu, Changbin                               <rev1>...<rev2>
914a593c34SDu, Changbin                               <rev>-<count>
924a593c34SDu, Changbin                             git merges are ignored
9377f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9477f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
953beb42ecSJoe Perches  --list-types               list the possible message types
9691bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
97000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
983beb42ecSJoe Perches  --show-types               show the specific message type in the output
996cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
10056193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
10177f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10277f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10377f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10477f5b10aSHannes Eder  --summary-file             include the filename in summary
10577f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
10677f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10777f5b10aSHannes Eder                             is all off)
10877f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10977f5b10aSHannes Eder                             literally
1103705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1113705ce5bSJoe Perches                             If correctable single-line errors exist, create
1123705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1133705ce5bSJoe Perches                             with potential errors corrected to the preferred
1143705ce5bSJoe Perches                             checkpatch style
1159624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1169624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1179624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
118d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
119d62a201fSDave Hansen                             runtime errors.
120ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
121f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
122ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12375ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
124737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
125737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
12677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
12777f5b10aSHannes Eder
12877f5b10aSHannes EderWhen FILE is - read standard input.
12977f5b10aSHannes EderEOM
13077f5b10aSHannes Eder
13177f5b10aSHannes Eder	exit($exitcode);
13277f5b10aSHannes Eder}
13377f5b10aSHannes Eder
1343beb42ecSJoe Perchessub uniq {
1353beb42ecSJoe Perches	my %seen;
1363beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1373beb42ecSJoe Perches}
1383beb42ecSJoe Perches
1393beb42ecSJoe Perchessub list_types {
1403beb42ecSJoe Perches	my ($exitcode) = @_;
1413beb42ecSJoe Perches
1423beb42ecSJoe Perches	my $count = 0;
1433beb42ecSJoe Perches
1443beb42ecSJoe Perches	local $/ = undef;
1453beb42ecSJoe Perches
1463beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1473beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1483beb42ecSJoe Perches
1493beb42ecSJoe Perches	my $text = <$script>;
1503beb42ecSJoe Perches	close($script);
1513beb42ecSJoe Perches
1523beb42ecSJoe Perches	my @types = ();
1530547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1540547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1553beb42ecSJoe Perches		push (@types, $_);
1563beb42ecSJoe Perches	}
1573beb42ecSJoe Perches	@types = sort(uniq(@types));
1583beb42ecSJoe Perches	print("#\tMessage type\n\n");
1593beb42ecSJoe Perches	foreach my $type (@types) {
1603beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1613beb42ecSJoe Perches	}
1623beb42ecSJoe Perches
1633beb42ecSJoe Perches	exit($exitcode);
1643beb42ecSJoe Perches}
1653beb42ecSJoe Perches
166000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
167000d1cc1SJoe Perchesif (-f $conf) {
168000d1cc1SJoe Perches	my @conf_args;
169000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
170000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
171000d1cc1SJoe Perches
172000d1cc1SJoe Perches	while (<$conffile>) {
173000d1cc1SJoe Perches		my $line = $_;
174000d1cc1SJoe Perches
175000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
176000d1cc1SJoe Perches		$line =~ s/^\s*//g;
177000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
178000d1cc1SJoe Perches
179000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
180000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
181000d1cc1SJoe Perches
182000d1cc1SJoe Perches		my @words = split(" ", $line);
183000d1cc1SJoe Perches		foreach my $word (@words) {
184000d1cc1SJoe Perches			last if ($word =~ m/^#/);
185000d1cc1SJoe Perches			push (@conf_args, $word);
186000d1cc1SJoe Perches		}
187000d1cc1SJoe Perches	}
188000d1cc1SJoe Perches	close($conffile);
189000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
190000d1cc1SJoe Perches}
191000d1cc1SJoe Perches
192737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
193737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
194737c0767SJohn Brooksforeach (@ARGV) {
195737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
196737c0767SJohn Brooks		$_ = "--color=$color";
197737c0767SJohn Brooks	}
198737c0767SJohn Brooks}
199737c0767SJohn Brooks
2000a920b5bSAndy WhitcroftGetOptions(
2016c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2020a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2030a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2040a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2056c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2068905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20734d8815fSJoe Perches	'showfile!'	=> \$showfile,
20877f5b10aSHannes Eder	'f|file!'	=> \$file,
2094a593c34SDu, Changbin	'g|git!'	=> \$git,
2106c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2116c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
212000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
21391bfe484SJoe Perches	'types=s'	=> \@use,
214000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2153beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2166cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21756193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2186c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2198905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2208905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
22113214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2223705ce5bSJoe Perches	'fix!'		=> \$fix,
2239624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
224d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
225c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
226773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
227ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
228ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
22975ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
230737c0767SJohn Brooks	'color=s'	=> \$color,
231737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
232737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
23377f5b10aSHannes Eder	'h|help'	=> \$help,
23477f5b10aSHannes Eder	'version'	=> \$help
23577f5b10aSHannes Eder) or help(1);
23677f5b10aSHannes Eder
23777f5b10aSHannes Ederhelp(0) if ($help);
2380a920b5bSAndy Whitcroft
2393beb42ecSJoe Percheslist_types(0) if ($list_types);
2403beb42ecSJoe Perches
2419624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2422ac73b4fSJoe Perches$check_orig = $check;
2439624b8d6SJoe Perches
2440a920b5bSAndy Whitcroftmy $exit = 0;
2450a920b5bSAndy Whitcroft
2465b57980dSJoe Perchesmy $perl_version_ok = 1;
247d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2485b57980dSJoe Perches	$perl_version_ok = 0;
249d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2505b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
251d62a201fSDave Hansen}
252d62a201fSDave Hansen
25345107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2540a920b5bSAndy Whitcroftif ($#ARGV < 0) {
25545107ff6SAllen Hubbe	push(@ARGV, '-');
2560a920b5bSAndy Whitcroft}
2570a920b5bSAndy Whitcroft
258737c0767SJohn Brooksif ($color =~ /^[01]$/) {
259737c0767SJohn Brooks	$color = !$color;
260737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
261737c0767SJohn Brooks	$color = 1;
262737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
263737c0767SJohn Brooks	$color = 0;
264737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
265737c0767SJohn Brooks	$color = (-t STDOUT);
266737c0767SJohn Brooks} else {
267737c0767SJohn Brooks	die "Invalid color mode: $color\n";
268737c0767SJohn Brooks}
269737c0767SJohn Brooks
27091bfe484SJoe Perchessub hash_save_array_words {
27191bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
27291bfe484SJoe Perches
27391bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
27491bfe484SJoe Perches	foreach my $word (@array) {
275000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
276000d1cc1SJoe Perches		$word =~ s/^\s*//g;
277000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
278000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
279000d1cc1SJoe Perches
280000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
281000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
282000d1cc1SJoe Perches
28391bfe484SJoe Perches		$hashRef->{$word}++;
284000d1cc1SJoe Perches	}
28591bfe484SJoe Perches}
28691bfe484SJoe Perches
28791bfe484SJoe Perchessub hash_show_words {
28891bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
28991bfe484SJoe Perches
2903c816e49SJoe Perches	if (keys %$hashRef) {
291d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
29258cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
29391bfe484SJoe Perches			print " $word";
29491bfe484SJoe Perches		}
295d8469f16SJoe Perches		print "\n";
29691bfe484SJoe Perches	}
29791bfe484SJoe Perches}
29891bfe484SJoe Perches
29991bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
30091bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
301000d1cc1SJoe Perches
302c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
303c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3047429c690SAndy Whitcroftmy $dbg_type = 0;
305a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
306c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
30721caa13cSAndy Whitcroft	## no critic
30821caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
30921caa13cSAndy Whitcroft	die "$@" if ($@);
310c2fdda0dSAndy Whitcroft}
311c2fdda0dSAndy Whitcroft
312d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
313d2c0a235SAndy Whitcroft
3148905a67cSAndy Whitcroftif ($terse) {
3158905a67cSAndy Whitcroft	$emacs = 1;
3168905a67cSAndy Whitcroft	$quiet++;
3178905a67cSAndy Whitcroft}
3188905a67cSAndy Whitcroft
3196c72ffaaSAndy Whitcroftif ($tree) {
3206c72ffaaSAndy Whitcroft	if (defined $root) {
3216c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3226c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3236c72ffaaSAndy Whitcroft		}
3246c72ffaaSAndy Whitcroft	} else {
3256c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3266c72ffaaSAndy Whitcroft			$root = '.';
3276c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3286c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3296c72ffaaSAndy Whitcroft			$root = $1;
3306c72ffaaSAndy Whitcroft		}
3316c72ffaaSAndy Whitcroft	}
3326c72ffaaSAndy Whitcroft
3336c72ffaaSAndy Whitcroft	if (!defined $root) {
3340a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3350a920b5bSAndy Whitcroft		exit(2);
3360a920b5bSAndy Whitcroft	}
3376c72ffaaSAndy Whitcroft}
3386c72ffaaSAndy Whitcroft
3396c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3406c72ffaaSAndy Whitcroft
3412ceb532bSAndy Whitcroftour $Ident	= qr{
3422ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3432ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3442ceb532bSAndy Whitcroft		}x;
3456c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3466c72ffaaSAndy Whitcroftour $Sparse	= qr{
3476c72ffaaSAndy Whitcroft			__user|
3486c72ffaaSAndy Whitcroft			__kernel|
3496c72ffaaSAndy Whitcroft			__force|
3506c72ffaaSAndy Whitcroft			__iomem|
3516c72ffaaSAndy Whitcroft			__must_check|
352417495edSAndy Whitcroft			__kprobes|
353165e72a6SSven Eckelmann			__ref|
35433aa4597SGeert Uytterhoeven			__refconst|
35533aa4597SGeert Uytterhoeven			__refdata|
356ad315455SBoqun Feng			__rcu|
357ad315455SBoqun Feng			__private
3586c72ffaaSAndy Whitcroft		}x;
359e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
360e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
361e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
362e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
363e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3648716de38SJoe Perches
36552131292SWolfram Sang# Notes to $Attribute:
36652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3676c72ffaaSAndy Whitcroftour $Attribute	= qr{
3686c72ffaaSAndy Whitcroft			const|
36903f1df7dSJoe Perches			__percpu|
37003f1df7dSJoe Perches			__nocast|
37103f1df7dSJoe Perches			__safe|
37246d832f5SMichael S. Tsirkin			__bitwise|
37303f1df7dSJoe Perches			__packed__|
37403f1df7dSJoe Perches			__packed2__|
37503f1df7dSJoe Perches			__naked|
37603f1df7dSJoe Perches			__maybe_unused|
37703f1df7dSJoe Perches			__always_unused|
37803f1df7dSJoe Perches			__noreturn|
37903f1df7dSJoe Perches			__used|
38003f1df7dSJoe Perches			__cold|
381e23ef1f3SJoe Perches			__pure|
38203f1df7dSJoe Perches			__noclone|
38303f1df7dSJoe Perches			__deprecated|
3846c72ffaaSAndy Whitcroft			__read_mostly|
385c5967e98SJoe Perches			__ro_after_init|
3866c72ffaaSAndy Whitcroft			__kprobes|
3878716de38SJoe Perches			$InitAttribute|
38824e1d81aSAndy Whitcroft			____cacheline_aligned|
38924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3905fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3915fe3af11SAndy Whitcroft			__weak
3926c72ffaaSAndy Whitcroft		  }x;
393c45dcabdSAndy Whitcroftour $Modifier;
39491cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3956c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3966c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3976c72ffaaSAndy Whitcroft
39895e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
39995e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
40095e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
40195e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4022435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
403c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
404326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
405326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
406326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
40774349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4082435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
409326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
410447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
41123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4126c72ffaaSAndy Whitcroftour $Operators	= qr{
4136c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4146c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
41523f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4166c72ffaaSAndy Whitcroft		  }x;
4176c72ffaaSAndy Whitcroft
41891cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
41991cb5195SJoe Perches
420ab7e23f3SJoe Perchesour $BasicType;
4218905a67cSAndy Whitcroftour $NonptrType;
4221813087dSJoe Perchesour $NonptrTypeMisordered;
4238716de38SJoe Perchesour $NonptrTypeWithAttr;
4248905a67cSAndy Whitcroftour $Type;
4251813087dSJoe Perchesour $TypeMisordered;
4268905a67cSAndy Whitcroftour $Declare;
4271813087dSJoe Perchesour $DeclareMisordered;
4288905a67cSAndy Whitcroft
42915662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
43015662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
431171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
432171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
433171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
434171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
435171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
436171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
437171ae1a4SAndy Whitcroft}x;
438171ae1a4SAndy Whitcroft
43915662b3eSJoe Perchesour $UTF8	= qr{
44015662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
44115662b3eSJoe Perches	| $NON_ASCII_UTF8
44215662b3eSJoe Perches}x;
44315662b3eSJoe Perches
444e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
445021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
446021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
447021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
448021158b4SJoe Perches)};
449e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
450fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4518ed22cadSAndy Whitcroft	atomic_t
4528ed22cadSAndy Whitcroft)};
453e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
454e6176fa4SJoe Perches	$typeC99Typedefs\b|
455e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
456e6176fa4SJoe Perches	$typeKernelTypedefs\b
457e6176fa4SJoe Perches)};
4588ed22cadSAndy Whitcroft
4596d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4606d32f7a3SJoe Perches
461691e669bSJoe Perchesour $logFunctions = qr{(?x:
462758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4637d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
46487bd499aSJoe Perches	TP_printk|
4656e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
466b0531722SJoe Perches	panic|
46706668727SJoe Perches	MODULE_[A-Z_]+|
46806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
469691e669bSJoe Perches)};
470691e669bSJoe Perches
471e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
472e29a70f1SJoe Perches	(?:(?:devm_)?
473e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
474e29a70f1SJoe Perches		kstrdup(?:_const)? |
475e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
476e29a70f1SJoe Perches	(?:\w+)?alloc_skb(?:ip_align)? |
477e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
478e29a70f1SJoe Perches	dma_alloc_coherent
479e29a70f1SJoe Perches)};
480e29a70f1SJoe Perches
48120112475SJoe Perchesour $signature_tags = qr{(?xi:
48220112475SJoe Perches	Signed-off-by:|
483d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
48420112475SJoe Perches	Acked-by:|
48520112475SJoe Perches	Tested-by:|
48620112475SJoe Perches	Reviewed-by:|
48720112475SJoe Perches	Reported-by:|
4888543ae12SMugunthan V N	Suggested-by:|
48920112475SJoe Perches	To:|
49020112475SJoe Perches	Cc:
49120112475SJoe Perches)};
49220112475SJoe Perches
4931813087dSJoe Perchesour @typeListMisordered = (
4941813087dSJoe Perches	qr{char\s+(?:un)?signed},
4951813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4961813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4971813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4981813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4991813087dSJoe Perches	qr{short\s+(?:un)?signed},
5001813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5011813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5021813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5031813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5041813087dSJoe Perches	qr{int\s+(?:un)?signed},
5051813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5061813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5071813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5081813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5091813087dSJoe Perches	qr{long\s+(?:un)?signed},
5101813087dSJoe Perches);
5111813087dSJoe Perches
5128905a67cSAndy Whitcroftour @typeList = (
5138905a67cSAndy Whitcroft	qr{void},
5140c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5150c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5160c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5170c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5180c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5190c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5200c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5210c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5220c773d9dSJoe Perches	qr{(?:un)?signed},
5238905a67cSAndy Whitcroft	qr{float},
5248905a67cSAndy Whitcroft	qr{double},
5258905a67cSAndy Whitcroft	qr{bool},
5268905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5278905a67cSAndy Whitcroft	qr{union\s+$Ident},
5288905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5298905a67cSAndy Whitcroft	qr{${Ident}_t},
5308905a67cSAndy Whitcroft	qr{${Ident}_handler},
5318905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5321813087dSJoe Perches	@typeListMisordered,
5338905a67cSAndy Whitcroft);
534938224b5SJoe Perches
535938224b5SJoe Perchesour $C90_int_types = qr{(?x:
536938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
537938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
538938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
539938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
540938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
541938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
542938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
543938224b5SJoe Perches
544938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
545938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
546938224b5SJoe Perches	long\s+(?:un)?signed|
547938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
548938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
549938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
550938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
551938224b5SJoe Perches
552938224b5SJoe Perches	int\s+(?:un)?signed|
553938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
554938224b5SJoe Perches)};
555938224b5SJoe Perches
556485ff23eSAlex Dowadour @typeListFile = ();
5578716de38SJoe Perchesour @typeListWithAttr = (
5588716de38SJoe Perches	@typeList,
5598716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5608716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5618716de38SJoe Perches);
5628716de38SJoe Perches
563c45dcabdSAndy Whitcroftour @modifierList = (
564c45dcabdSAndy Whitcroft	qr{fastcall},
565c45dcabdSAndy Whitcroft);
566485ff23eSAlex Dowadour @modifierListFile = ();
5678905a67cSAndy Whitcroft
5682435880fSJoe Perchesour @mode_permission_funcs = (
5692435880fSJoe Perches	["module_param", 3],
5702435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5712435880fSJoe Perches	["module_param_array_named", 5],
5722435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5732435880fSJoe Perches	["proc_create(?:_data|)", 2],
574459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
575459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
576459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
577459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
578459cf0aeSJoe Perches	["__ATTR", 2],
5792435880fSJoe Perches);
5802435880fSJoe Perches
581515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
582515a235eSJoe Perchesour $mode_perms_search = "";
583515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
584515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
585515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
586515a235eSJoe Perches}
58700180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
588515a235eSJoe Perches
5899189c7e7SJoe Perchesour %deprecated_apis = (
5909189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
5919189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
5929189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
5939189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
5949189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
5959189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
5969189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
5979189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
5989189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
5999189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6009189c7e7SJoe Perches);
6019189c7e7SJoe Perches
6029189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6039189c7e7SJoe Perchesour $deprecated_apis_search = "";
6049189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6059189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6069189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6079189c7e7SJoe Perches}
6089189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6099189c7e7SJoe Perches
610b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
611b392c64fSJoe Perches	S_IWUGO		|
612b392c64fSJoe Perches	S_IWOTH		|
613b392c64fSJoe Perches	S_IRWXUGO	|
614b392c64fSJoe Perches	S_IALLUGO	|
615b392c64fSJoe Perches	0[0-7][0-7][2367]
616b392c64fSJoe Perches}x;
617b392c64fSJoe Perches
618f90774e1SJoe Perchesour %mode_permission_string_types = (
619f90774e1SJoe Perches	"S_IRWXU" => 0700,
620f90774e1SJoe Perches	"S_IRUSR" => 0400,
621f90774e1SJoe Perches	"S_IWUSR" => 0200,
622f90774e1SJoe Perches	"S_IXUSR" => 0100,
623f90774e1SJoe Perches	"S_IRWXG" => 0070,
624f90774e1SJoe Perches	"S_IRGRP" => 0040,
625f90774e1SJoe Perches	"S_IWGRP" => 0020,
626f90774e1SJoe Perches	"S_IXGRP" => 0010,
627f90774e1SJoe Perches	"S_IRWXO" => 0007,
628f90774e1SJoe Perches	"S_IROTH" => 0004,
629f90774e1SJoe Perches	"S_IWOTH" => 0002,
630f90774e1SJoe Perches	"S_IXOTH" => 0001,
631f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
632f90774e1SJoe Perches	"S_IRUGO" => 0444,
633f90774e1SJoe Perches	"S_IWUGO" => 0222,
634f90774e1SJoe Perches	"S_IXUGO" => 0111,
635f90774e1SJoe Perches);
636f90774e1SJoe Perches
637f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
638f90774e1SJoe Perchesour $mode_perms_string_search = "";
639f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
640f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
641f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
642f90774e1SJoe Perches}
64300180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
64400180468SJoe Perchesour $multi_mode_perms_string_search = qr{
64500180468SJoe Perches	${single_mode_perms_string_search}
64600180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
64700180468SJoe Perches}x;
64800180468SJoe Perches
64900180468SJoe Perchessub perms_to_octal {
65000180468SJoe Perches	my ($string) = @_;
65100180468SJoe Perches
65200180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
65300180468SJoe Perches
65400180468SJoe Perches	my $val = "";
65500180468SJoe Perches	my $oval = "";
65600180468SJoe Perches	my $to = 0;
65700180468SJoe Perches	my $curpos = 0;
65800180468SJoe Perches	my $lastpos = 0;
65900180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
66000180468SJoe Perches		$curpos = pos($string);
66100180468SJoe Perches		my $match = $2;
66200180468SJoe Perches		my $omatch = $1;
66300180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
66400180468SJoe Perches		$lastpos = $curpos;
66500180468SJoe Perches		$to |= $mode_permission_string_types{$match};
66600180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
66700180468SJoe Perches		$val .= $match;
66800180468SJoe Perches		$oval .= $omatch;
66900180468SJoe Perches	}
67000180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
67100180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
67200180468SJoe Perches	return sprintf("%04o", $to);
67300180468SJoe Perches}
674f90774e1SJoe Perches
6757840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6767840a94cSWolfram Sang	irq|
677cdcee686SSergey Ryazanov	memory|
678cdcee686SSergey Ryazanov	time|
679cdcee686SSergey Ryazanov	reboot
6807840a94cSWolfram Sang)};
6817840a94cSWolfram Sang# memory.h: ARM has a custom one
6827840a94cSWolfram Sang
68366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
68466b47b4aSKees Cookmy $misspellings;
68566b47b4aSKees Cookmy %spelling_fix;
68636061e38SJoe Perches
68736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
68866b47b4aSKees Cook	while (<$spelling>) {
68966b47b4aSKees Cook		my $line = $_;
69066b47b4aSKees Cook
69166b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
69266b47b4aSKees Cook		$line =~ s/^\s*//g;
69366b47b4aSKees Cook
69466b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
69566b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
69666b47b4aSKees Cook
69766b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
69866b47b4aSKees Cook
69966b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
70066b47b4aSKees Cook	}
70166b47b4aSKees Cook	close($spelling);
70236061e38SJoe Perches} else {
70336061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
70436061e38SJoe Perches}
70566b47b4aSKees Cook
706ebfd7d62SJoe Perchesif ($codespell) {
707ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
708ebfd7d62SJoe Perches		while (<$spelling>) {
709ebfd7d62SJoe Perches			my $line = $_;
710ebfd7d62SJoe Perches
711ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
712ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
713ebfd7d62SJoe Perches
714ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
715ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
716ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
717ebfd7d62SJoe Perches
718ebfd7d62SJoe Perches			$line =~ s/,.*$//;
719ebfd7d62SJoe Perches
720ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
721ebfd7d62SJoe Perches
722ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
723ebfd7d62SJoe Perches		}
724ebfd7d62SJoe Perches		close($spelling);
725ebfd7d62SJoe Perches	} else {
726ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
727ebfd7d62SJoe Perches	}
728ebfd7d62SJoe Perches}
729ebfd7d62SJoe Perches
730ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
731ebfd7d62SJoe Perches
73275ad8c57SJerome Forissiersub read_words {
73375ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
73475ad8c57SJerome Forissier
73575ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
73675ad8c57SJerome Forissier		while (<$words>) {
737bf1fa1daSJoe Perches			my $line = $_;
738bf1fa1daSJoe Perches
739bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
740bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
741bf1fa1daSJoe Perches
742bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
743bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
744bf1fa1daSJoe Perches			if ($line =~ /\s/) {
74575ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
746bf1fa1daSJoe Perches				next;
747bf1fa1daSJoe Perches			}
748bf1fa1daSJoe Perches
74975ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
75075ad8c57SJerome Forissier			$$wordsRef .= $line;
751bf1fa1daSJoe Perches		}
75275ad8c57SJerome Forissier		close($file);
75375ad8c57SJerome Forissier		return 1;
754bf1fa1daSJoe Perches	}
755bf1fa1daSJoe Perches
75675ad8c57SJerome Forissier	return 0;
75775ad8c57SJerome Forissier}
75875ad8c57SJerome Forissier
75975ad8c57SJerome Forissiermy $const_structs = "";
76075ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
76175ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
76275ad8c57SJerome Forissier
76375ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
76475ad8c57SJerome Forissierif (length($typedefsfile)) {
76575ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
76675ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
76775ad8c57SJerome Forissier}
76875ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
76975ad8c57SJerome Forissier
7708905a67cSAndy Whitcroftsub build_types {
771485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
772485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7731813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7748716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
775c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
776ab7e23f3SJoe Perches	$BasicType	= qr{
777ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
778ab7e23f3SJoe Perches				(?:${all}\b)
779ab7e23f3SJoe Perches		}x;
7808905a67cSAndy Whitcroft	$NonptrType	= qr{
781d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
782cf655043SAndy Whitcroft			(?:
7836b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7848ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
785c45dcabdSAndy Whitcroft				(?:${all}\b)
786cf655043SAndy Whitcroft			)
787c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7888905a67cSAndy Whitcroft		  }x;
7891813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7901813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7911813087dSJoe Perches			(?:
7921813087dSJoe Perches				(?:${Misordered}\b)
7931813087dSJoe Perches			)
7941813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7951813087dSJoe Perches		  }x;
7968716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7978716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7988716de38SJoe Perches			(?:
7998716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8008716de38SJoe Perches				(?:$typeTypedefs\b)|
8018716de38SJoe Perches				(?:${allWithAttr}\b)
8028716de38SJoe Perches			)
8038716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8048716de38SJoe Perches		  }x;
8058905a67cSAndy Whitcroft	$Type	= qr{
806c45dcabdSAndy Whitcroft			$NonptrType
8071574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
808c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8098905a67cSAndy Whitcroft		  }x;
8101813087dSJoe Perches	$TypeMisordered	= qr{
8111813087dSJoe Perches			$NonptrTypeMisordered
8121813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
8131813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8141813087dSJoe Perches		  }x;
81591cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8161813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8178905a67cSAndy Whitcroft}
8188905a67cSAndy Whitcroftbuild_types();
8196c72ffaaSAndy Whitcroft
8207d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
821d1fe9c09SJoe Perches
822d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
823d1fe9c09SJoe Perches# requires at least perl version v5.10.0
824d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
825d1fe9c09SJoe Perches
826d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8272435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
828c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8297d2367afSJoe Perches
830f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8313e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
832fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8333d102fc0SGilad Ben-Yossef	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
8343d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
835f8422308SJoe Perches)};
836f8422308SJoe Perches
8377d2367afSJoe Perchessub deparenthesize {
8387d2367afSJoe Perches	my ($string) = @_;
8397d2367afSJoe Perches	return "" if (!defined($string));
8405b9553abSJoe Perches
8415b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8425b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8435b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8445b9553abSJoe Perches	}
8455b9553abSJoe Perches
8467d2367afSJoe Perches	$string =~ s@\s+@ @g;
8475b9553abSJoe Perches
8487d2367afSJoe Perches	return $string;
8497d2367afSJoe Perches}
8507d2367afSJoe Perches
8513445686aSJoe Perchessub seed_camelcase_file {
8523445686aSJoe Perches	my ($file) = @_;
8533445686aSJoe Perches
8543445686aSJoe Perches	return if (!(-f $file));
8553445686aSJoe Perches
8563445686aSJoe Perches	local $/;
8573445686aSJoe Perches
8583445686aSJoe Perches	open(my $include_file, '<', "$file")
8593445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8603445686aSJoe Perches	my $text = <$include_file>;
8613445686aSJoe Perches	close($include_file);
8623445686aSJoe Perches
8633445686aSJoe Perches	my @lines = split('\n', $text);
8643445686aSJoe Perches
8653445686aSJoe Perches	foreach my $line (@lines) {
8663445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8673445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8683445686aSJoe Perches			$camelcase{$1} = 1;
86911ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
87011ea516aSJoe Perches			$camelcase{$1} = 1;
87111ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8723445686aSJoe Perches			$camelcase{$1} = 1;
8733445686aSJoe Perches		}
8743445686aSJoe Perches	}
8753445686aSJoe Perches}
8763445686aSJoe Perches
877*cd28b119SJoe Perchesour %maintained_status = ();
878*cd28b119SJoe Perches
87985b0ee18SJoe Perchessub is_maintained_obsolete {
88085b0ee18SJoe Perches	my ($filename) = @_;
88185b0ee18SJoe Perches
882f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
88385b0ee18SJoe Perches
884*cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
885*cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
886*cd28b119SJoe Perches	}
88785b0ee18SJoe Perches
888*cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
88985b0ee18SJoe Perches}
89085b0ee18SJoe Perches
8913b6e8ac9SJoe Perchessub is_SPDX_License_valid {
8923b6e8ac9SJoe Perches	my ($license) = @_;
8933b6e8ac9SJoe Perches
89456294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
8953b6e8ac9SJoe Perches
89656294112SJoe Perches	my $root_path = abs_path($root);
89756294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
8983b6e8ac9SJoe Perches	return 0 if ($status ne "");
8993b6e8ac9SJoe Perches	return 1;
9003b6e8ac9SJoe Perches}
9013b6e8ac9SJoe Perches
9023445686aSJoe Perchesmy $camelcase_seeded = 0;
9033445686aSJoe Perchessub seed_camelcase_includes {
9043445686aSJoe Perches	return if ($camelcase_seeded);
9053445686aSJoe Perches
9063445686aSJoe Perches	my $files;
907c707a81dSJoe Perches	my $camelcase_cache = "";
908c707a81dSJoe Perches	my @include_files = ();
909c707a81dSJoe Perches
910c707a81dSJoe Perches	$camelcase_seeded = 1;
911351b2a1fSJoe Perches
9123645e328SRichard Genoud	if (-e ".git") {
913dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
914351b2a1fSJoe Perches		chomp $git_last_include_commit;
915c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
916c707a81dSJoe Perches	} else {
917c707a81dSJoe Perches		my $last_mod_date = 0;
918c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
919c707a81dSJoe Perches		@include_files = split('\n', $files);
920c707a81dSJoe Perches		foreach my $file (@include_files) {
921c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
922c707a81dSJoe Perches						   localtime((stat $file)[9]));
923c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
924c707a81dSJoe Perches		}
925c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
926c707a81dSJoe Perches	}
927c707a81dSJoe Perches
928c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
929c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
930c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
931351b2a1fSJoe Perches		while (<$camelcase_file>) {
932351b2a1fSJoe Perches			chomp;
933351b2a1fSJoe Perches			$camelcase{$_} = 1;
934351b2a1fSJoe Perches		}
935351b2a1fSJoe Perches		close($camelcase_file);
936351b2a1fSJoe Perches
937351b2a1fSJoe Perches		return;
938351b2a1fSJoe Perches	}
939c707a81dSJoe Perches
9403645e328SRichard Genoud	if (-e ".git") {
941dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
942c707a81dSJoe Perches		@include_files = split('\n', $files);
9433445686aSJoe Perches	}
944c707a81dSJoe Perches
9453445686aSJoe Perches	foreach my $file (@include_files) {
9463445686aSJoe Perches		seed_camelcase_file($file);
9473445686aSJoe Perches	}
948351b2a1fSJoe Perches
949c707a81dSJoe Perches	if ($camelcase_cache ne "") {
950351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
951c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
952c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
953351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
954351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
955351b2a1fSJoe Perches		}
956351b2a1fSJoe Perches		close($camelcase_file);
957351b2a1fSJoe Perches	}
9583445686aSJoe Perches}
9593445686aSJoe Perches
960d311cd44SJoe Perchessub git_commit_info {
961d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
962d311cd44SJoe Perches
963d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
964d311cd44SJoe Perches
965dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
966d311cd44SJoe Perches	$output =~ s/^\s*//gm;
967d311cd44SJoe Perches	my @lines = split("\n", $output);
968d311cd44SJoe Perches
9690d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9700d7835fcSJoe Perches
9715a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
972d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
973d311cd44SJoe Perches# all matching commit ids, but it's very slow...
974d311cd44SJoe Perches#
975d311cd44SJoe Perches#		echo "checking commits $1..."
976d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
977d311cd44SJoe Perches#		while read line ; do
978d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
979d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
980d311cd44SJoe Perches#		done
981d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
982948b133aSHeinrich Schuchardt		$id = undef;
983d311cd44SJoe Perches	} else {
984d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
985d311cd44SJoe Perches		$desc = substr($lines[0], 41);
986d311cd44SJoe Perches	}
987d311cd44SJoe Perches
988d311cd44SJoe Perches	return ($id, $desc);
989d311cd44SJoe Perches}
990d311cd44SJoe Perches
9916c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9920a920b5bSAndy Whitcroft
99300df344fSAndy Whitcroftmy @rawlines = ();
994c2fdda0dSAndy Whitcroftmy @lines = ();
9953705ce5bSJoe Perchesmy @fixed = ();
996d752fcc8SJoe Perchesmy @fixed_inserted = ();
997d752fcc8SJoe Perchesmy @fixed_deleted = ();
998194f66fcSJoe Perchesmy $fixlinenr = -1;
999194f66fcSJoe Perches
10004a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
10014a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
10024a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
10034a593c34SDu, Changbin
10044a593c34SDu, Changbinif ($git) {
10054a593c34SDu, Changbin	my @commits = ();
10060dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
10074a593c34SDu, Changbin		my $git_range;
100828898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
100928898fd1SJoe Perches			$git_range = "-$2 $1";
10104a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
10114a593c34SDu, Changbin			$git_range = "$commit_expr";
10124a593c34SDu, Changbin		} else {
10130dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
10140dea9f1eSJoe Perches		}
1015dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10160dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
101728898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
101828898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10190dea9f1eSJoe Perches			my $sha1 = $1;
10200dea9f1eSJoe Perches			my $subject = $2;
10210dea9f1eSJoe Perches			unshift(@commits, $sha1);
10220dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10234a593c34SDu, Changbin		}
10244a593c34SDu, Changbin	}
10254a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10264a593c34SDu, Changbin	@ARGV = @commits;
10274a593c34SDu, Changbin}
10284a593c34SDu, Changbin
1029c2fdda0dSAndy Whitcroftmy $vname;
103098005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
10316c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
103221caa13cSAndy Whitcroft	my $FILE;
10334a593c34SDu, Changbin	if ($git) {
10344a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10354a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10364a593c34SDu, Changbin	} elsif ($file) {
103721caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10386c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
103921caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
104021caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10416c72ffaaSAndy Whitcroft	} else {
104221caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10436c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10446c72ffaaSAndy Whitcroft	}
1045c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1046c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10474a593c34SDu, Changbin	} elsif ($git) {
10480dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1049c2fdda0dSAndy Whitcroft	} else {
1050c2fdda0dSAndy Whitcroft		$vname = $filename;
1051c2fdda0dSAndy Whitcroft	}
105221caa13cSAndy Whitcroft	while (<$FILE>) {
10530a920b5bSAndy Whitcroft		chomp;
105400df344fSAndy Whitcroft		push(@rawlines, $_);
10556c72ffaaSAndy Whitcroft	}
105621caa13cSAndy Whitcroft	close($FILE);
1057d8469f16SJoe Perches
1058d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1059d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1060d8469f16SJoe Perches		print "$vname\n";
1061d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1062d8469f16SJoe Perches	}
1063d8469f16SJoe Perches
1064c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10650a920b5bSAndy Whitcroft		$exit = 1;
10660a920b5bSAndy Whitcroft	}
106700df344fSAndy Whitcroft	@rawlines = ();
106813214adfSAndy Whitcroft	@lines = ();
10693705ce5bSJoe Perches	@fixed = ();
1070d752fcc8SJoe Perches	@fixed_inserted = ();
1071d752fcc8SJoe Perches	@fixed_deleted = ();
1072194f66fcSJoe Perches	$fixlinenr = -1;
1073485ff23eSAlex Dowad	@modifierListFile = ();
1074485ff23eSAlex Dowad	@typeListFile = ();
1075485ff23eSAlex Dowad	build_types();
10760a920b5bSAndy Whitcroft}
10770a920b5bSAndy Whitcroft
1078d8469f16SJoe Perchesif (!$quiet) {
10793c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10803c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10813c816e49SJoe Perches
10825b57980dSJoe Perches	if (!$perl_version_ok) {
1083d8469f16SJoe Perches		print << "EOM"
1084d8469f16SJoe Perches
1085d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
10865b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1087d8469f16SJoe PerchesEOM
1088d8469f16SJoe Perches	}
1089d8469f16SJoe Perches	if ($exit) {
1090d8469f16SJoe Perches		print << "EOM"
1091d8469f16SJoe Perches
1092d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1093d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1094d8469f16SJoe PerchesEOM
1095d8469f16SJoe Perches	}
1096d8469f16SJoe Perches}
1097d8469f16SJoe Perches
10980a920b5bSAndy Whitcroftexit($exit);
10990a920b5bSAndy Whitcroft
11000a920b5bSAndy Whitcroftsub top_of_kernel_tree {
11016c72ffaaSAndy Whitcroft	my ($root) = @_;
11026c72ffaaSAndy Whitcroft
11036c72ffaaSAndy Whitcroft	my @tree_check = (
11046c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
11056c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
11066c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
11076c72ffaaSAndy Whitcroft	);
11086c72ffaaSAndy Whitcroft
11096c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
11106c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
11110a920b5bSAndy Whitcroft			return 0;
11120a920b5bSAndy Whitcroft		}
11136c72ffaaSAndy Whitcroft	}
11146c72ffaaSAndy Whitcroft	return 1;
11156c72ffaaSAndy Whitcroft}
11160a920b5bSAndy Whitcroft
111720112475SJoe Perchessub parse_email {
111820112475SJoe Perches	my ($formatted_email) = @_;
111920112475SJoe Perches
112020112475SJoe Perches	my $name = "";
112120112475SJoe Perches	my $address = "";
112220112475SJoe Perches	my $comment = "";
112320112475SJoe Perches
112420112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
112520112475SJoe Perches		$name = $1;
112620112475SJoe Perches		$address = $2;
112720112475SJoe Perches		$comment = $3 if defined $3;
112820112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
112920112475SJoe Perches		$address = $1;
113020112475SJoe Perches		$comment = $2 if defined $2;
113120112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
113220112475SJoe Perches		$address = $1;
113320112475SJoe Perches		$comment = $2 if defined $2;
113485e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
113520112475SJoe Perches		$name = $formatted_email;
11363705ce5bSJoe Perches		$name = trim($name);
113720112475SJoe Perches		$name =~ s/^\"|\"$//g;
113820112475SJoe Perches		# If there's a name left after stripping spaces and
113920112475SJoe Perches		# leading quotes, and the address doesn't have both
114020112475SJoe Perches		# leading and trailing angle brackets, the address
114120112475SJoe Perches		# is invalid. ie:
114220112475SJoe Perches		#   "joe smith [email protected]" bad
114320112475SJoe Perches		#   "joe smith <[email protected]" bad
114420112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
114520112475SJoe Perches			$name = "";
114620112475SJoe Perches			$address = "";
114720112475SJoe Perches			$comment = "";
114820112475SJoe Perches		}
114920112475SJoe Perches	}
115020112475SJoe Perches
11513705ce5bSJoe Perches	$name = trim($name);
115220112475SJoe Perches	$name =~ s/^\"|\"$//g;
11533705ce5bSJoe Perches	$address = trim($address);
115420112475SJoe Perches	$address =~ s/^\<|\>$//g;
115520112475SJoe Perches
115620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
115720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
115820112475SJoe Perches		$name = "\"$name\"";
115920112475SJoe Perches	}
116020112475SJoe Perches
116120112475SJoe Perches	return ($name, $address, $comment);
116220112475SJoe Perches}
116320112475SJoe Perches
116420112475SJoe Perchessub format_email {
116520112475SJoe Perches	my ($name, $address) = @_;
116620112475SJoe Perches
116720112475SJoe Perches	my $formatted_email;
116820112475SJoe Perches
11693705ce5bSJoe Perches	$name = trim($name);
117020112475SJoe Perches	$name =~ s/^\"|\"$//g;
11713705ce5bSJoe Perches	$address = trim($address);
117220112475SJoe Perches
117320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
117420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
117520112475SJoe Perches		$name = "\"$name\"";
117620112475SJoe Perches	}
117720112475SJoe Perches
117820112475SJoe Perches	if ("$name" eq "") {
117920112475SJoe Perches		$formatted_email = "$address";
118020112475SJoe Perches	} else {
118120112475SJoe Perches		$formatted_email = "$name <$address>";
118220112475SJoe Perches	}
118320112475SJoe Perches
118420112475SJoe Perches	return $formatted_email;
118520112475SJoe Perches}
118620112475SJoe Perches
1187d311cd44SJoe Perchessub which {
1188d311cd44SJoe Perches	my ($bin) = @_;
1189d311cd44SJoe Perches
1190d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1191d311cd44SJoe Perches		if (-e "$path/$bin") {
1192d311cd44SJoe Perches			return "$path/$bin";
1193d311cd44SJoe Perches		}
1194d311cd44SJoe Perches	}
1195d311cd44SJoe Perches
1196d311cd44SJoe Perches	return "";
1197d311cd44SJoe Perches}
1198d311cd44SJoe Perches
1199000d1cc1SJoe Perchessub which_conf {
1200000d1cc1SJoe Perches	my ($conf) = @_;
1201000d1cc1SJoe Perches
1202000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1203000d1cc1SJoe Perches		if (-e "$path/$conf") {
1204000d1cc1SJoe Perches			return "$path/$conf";
1205000d1cc1SJoe Perches		}
1206000d1cc1SJoe Perches	}
1207000d1cc1SJoe Perches
1208000d1cc1SJoe Perches	return "";
1209000d1cc1SJoe Perches}
1210000d1cc1SJoe Perches
12110a920b5bSAndy Whitcroftsub expand_tabs {
12120a920b5bSAndy Whitcroft	my ($str) = @_;
12130a920b5bSAndy Whitcroft
12140a920b5bSAndy Whitcroft	my $res = '';
12150a920b5bSAndy Whitcroft	my $n = 0;
12160a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12170a920b5bSAndy Whitcroft		if ($c eq "\t") {
12180a920b5bSAndy Whitcroft			$res .= ' ';
12190a920b5bSAndy Whitcroft			$n++;
12200a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
12210a920b5bSAndy Whitcroft				$res .= ' ';
12220a920b5bSAndy Whitcroft			}
12230a920b5bSAndy Whitcroft			next;
12240a920b5bSAndy Whitcroft		}
12250a920b5bSAndy Whitcroft		$res .= $c;
12260a920b5bSAndy Whitcroft		$n++;
12270a920b5bSAndy Whitcroft	}
12280a920b5bSAndy Whitcroft
12290a920b5bSAndy Whitcroft	return $res;
12300a920b5bSAndy Whitcroft}
12316c72ffaaSAndy Whitcroftsub copy_spacing {
1232773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
12336c72ffaaSAndy Whitcroft	return $res;
12346c72ffaaSAndy Whitcroft}
12350a920b5bSAndy Whitcroft
12364a0df2efSAndy Whitcroftsub line_stats {
12374a0df2efSAndy Whitcroft	my ($line) = @_;
12384a0df2efSAndy Whitcroft
12394a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12404a0df2efSAndy Whitcroft	$line =~ s/^.//;
12414a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12424a0df2efSAndy Whitcroft
12434a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12444a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12454a0df2efSAndy Whitcroft
12464a0df2efSAndy Whitcroft	return (length($line), length($white));
12474a0df2efSAndy Whitcroft}
12484a0df2efSAndy Whitcroft
1249773647a0SAndy Whitcroftmy $sanitise_quote = '';
1250773647a0SAndy Whitcroft
1251773647a0SAndy Whitcroftsub sanitise_line_reset {
1252773647a0SAndy Whitcroft	my ($in_comment) = @_;
1253773647a0SAndy Whitcroft
1254773647a0SAndy Whitcroft	if ($in_comment) {
1255773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1256773647a0SAndy Whitcroft	} else {
1257773647a0SAndy Whitcroft		$sanitise_quote = '';
1258773647a0SAndy Whitcroft	}
1259773647a0SAndy Whitcroft}
126000df344fSAndy Whitcroftsub sanitise_line {
126100df344fSAndy Whitcroft	my ($line) = @_;
126200df344fSAndy Whitcroft
126300df344fSAndy Whitcroft	my $res = '';
126400df344fSAndy Whitcroft	my $l = '';
126500df344fSAndy Whitcroft
1266c2fdda0dSAndy Whitcroft	my $qlen = 0;
1267773647a0SAndy Whitcroft	my $off = 0;
1268773647a0SAndy Whitcroft	my $c;
126900df344fSAndy Whitcroft
1270773647a0SAndy Whitcroft	# Always copy over the diff marker.
1271773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1272773647a0SAndy Whitcroft
1273773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1274773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1275773647a0SAndy Whitcroft
12768d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1277773647a0SAndy Whitcroft		# and end, all to $;.
1278773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1279773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1280773647a0SAndy Whitcroft
1281773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1282773647a0SAndy Whitcroft			$off++;
128300df344fSAndy Whitcroft			next;
1284773647a0SAndy Whitcroft		}
128581bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1286773647a0SAndy Whitcroft			$sanitise_quote = '';
1287773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1288773647a0SAndy Whitcroft			$off++;
1289773647a0SAndy Whitcroft			next;
1290773647a0SAndy Whitcroft		}
1291113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1292113f04a8SDaniel Walker			$sanitise_quote = '//';
1293113f04a8SDaniel Walker
1294113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1295113f04a8SDaniel Walker			$off++;
1296113f04a8SDaniel Walker			next;
1297113f04a8SDaniel Walker		}
1298773647a0SAndy Whitcroft
1299773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1300773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1301773647a0SAndy Whitcroft		    $c eq "\\") {
1302773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1303773647a0SAndy Whitcroft			$off++;
1304773647a0SAndy Whitcroft			next;
1305773647a0SAndy Whitcroft		}
1306773647a0SAndy Whitcroft		# Regular quotes.
1307773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1308773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1309773647a0SAndy Whitcroft				$sanitise_quote = $c;
1310773647a0SAndy Whitcroft
1311773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1312773647a0SAndy Whitcroft				next;
1313773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1314773647a0SAndy Whitcroft				$sanitise_quote = '';
131500df344fSAndy Whitcroft			}
131600df344fSAndy Whitcroft		}
1317773647a0SAndy Whitcroft
1318fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1319773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1320773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1321113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1322113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1323773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1324773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
132500df344fSAndy Whitcroft		} else {
1326773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
132700df344fSAndy Whitcroft		}
1328c2fdda0dSAndy Whitcroft	}
1329c2fdda0dSAndy Whitcroft
1330113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1331113f04a8SDaniel Walker		$sanitise_quote = '';
1332113f04a8SDaniel Walker	}
1333113f04a8SDaniel Walker
1334c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1335c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1336c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1337c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1338c2fdda0dSAndy Whitcroft
1339c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1340c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1341c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1342c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1343c2fdda0dSAndy Whitcroft	}
1344c2fdda0dSAndy Whitcroft
1345dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1346dadf680dSJoe Perches		my $match = $1;
1347dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1348dadf680dSJoe Perches	}
1349dadf680dSJoe Perches
135000df344fSAndy Whitcroft	return $res;
135100df344fSAndy Whitcroft}
135200df344fSAndy Whitcroft
1353a6962d72SJoe Perchessub get_quoted_string {
1354a6962d72SJoe Perches	my ($line, $rawline) = @_;
1355a6962d72SJoe Perches
1356478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
135733acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1358a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1359a6962d72SJoe Perches}
1360a6962d72SJoe Perches
13618905a67cSAndy Whitcroftsub ctx_statement_block {
13628905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13638905a67cSAndy Whitcroft	my $line = $linenr - 1;
13648905a67cSAndy Whitcroft	my $blk = '';
13658905a67cSAndy Whitcroft	my $soff = $off;
13668905a67cSAndy Whitcroft	my $coff = $off - 1;
1367773647a0SAndy Whitcroft	my $coff_set = 0;
13688905a67cSAndy Whitcroft
136913214adfSAndy Whitcroft	my $loff = 0;
137013214adfSAndy Whitcroft
13718905a67cSAndy Whitcroft	my $type = '';
13728905a67cSAndy Whitcroft	my $level = 0;
1373a2750645SAndy Whitcroft	my @stack = ();
1374cf655043SAndy Whitcroft	my $p;
13758905a67cSAndy Whitcroft	my $c;
13768905a67cSAndy Whitcroft	my $len = 0;
137713214adfSAndy Whitcroft
137813214adfSAndy Whitcroft	my $remainder;
13798905a67cSAndy Whitcroft	while (1) {
1380a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1381a2750645SAndy Whitcroft
1382773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
13838905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
13848905a67cSAndy Whitcroft		# context.
13858905a67cSAndy Whitcroft		if ($off >= $len) {
13868905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1387dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1388c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13898905a67cSAndy Whitcroft				$remain--;
139013214adfSAndy Whitcroft				$loff = $len;
1391c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13928905a67cSAndy Whitcroft				$len = length($blk);
13938905a67cSAndy Whitcroft				$line++;
13948905a67cSAndy Whitcroft				last;
13958905a67cSAndy Whitcroft			}
13968905a67cSAndy Whitcroft			# Bail if there is no further context.
13978905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
139813214adfSAndy Whitcroft			if ($off >= $len) {
13998905a67cSAndy Whitcroft				last;
14008905a67cSAndy Whitcroft			}
1401f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1402f74bd194SAndy Whitcroft				$level++;
1403f74bd194SAndy Whitcroft				$type = '#';
1404f74bd194SAndy Whitcroft			}
14058905a67cSAndy Whitcroft		}
1406cf655043SAndy Whitcroft		$p = $c;
14078905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
140813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
14098905a67cSAndy Whitcroft
1410773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
14114635f4fbSAndy Whitcroft
14124635f4fbSAndy Whitcroft		# Handle nested #if/#else.
14134635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
14144635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
14154635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
14164635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14174635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14184635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14194635f4fbSAndy Whitcroft		}
14204635f4fbSAndy Whitcroft
14218905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14228905a67cSAndy Whitcroft		# outermost level.
14238905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
14248905a67cSAndy Whitcroft			last;
14258905a67cSAndy Whitcroft		}
14268905a67cSAndy Whitcroft
142713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1428773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1429773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1430773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1431773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1432773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1433773647a0SAndy Whitcroft			$coff_set = 1;
1434773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1435773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
143613214adfSAndy Whitcroft		}
143713214adfSAndy Whitcroft
14388905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14398905a67cSAndy Whitcroft			$level++;
14408905a67cSAndy Whitcroft			$type = '(';
14418905a67cSAndy Whitcroft		}
14428905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14438905a67cSAndy Whitcroft			$level--;
14448905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14458905a67cSAndy Whitcroft
14468905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
14478905a67cSAndy Whitcroft				$coff = $off;
1448773647a0SAndy Whitcroft				$coff_set = 1;
1449773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
14508905a67cSAndy Whitcroft			}
14518905a67cSAndy Whitcroft		}
14528905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14538905a67cSAndy Whitcroft			$level++;
14548905a67cSAndy Whitcroft			$type = '{';
14558905a67cSAndy Whitcroft		}
14568905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14578905a67cSAndy Whitcroft			$level--;
14588905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14598905a67cSAndy Whitcroft
14608905a67cSAndy Whitcroft			if ($level == 0) {
1461b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1462b998e001SPatrick Pannuto					$off++;
1463b998e001SPatrick Pannuto				}
14648905a67cSAndy Whitcroft				last;
14658905a67cSAndy Whitcroft			}
14668905a67cSAndy Whitcroft		}
1467f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1468f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1469f74bd194SAndy Whitcroft			$level--;
1470f74bd194SAndy Whitcroft			$type = '';
1471f74bd194SAndy Whitcroft			$off++;
1472f74bd194SAndy Whitcroft			last;
1473f74bd194SAndy Whitcroft		}
14748905a67cSAndy Whitcroft		$off++;
14758905a67cSAndy Whitcroft	}
1476a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
147713214adfSAndy Whitcroft	if ($off == $len) {
1478a3bb97a7SAndy Whitcroft		$loff = $len + 1;
147913214adfSAndy Whitcroft		$line++;
148013214adfSAndy Whitcroft		$remain--;
148113214adfSAndy Whitcroft	}
14828905a67cSAndy Whitcroft
14838905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
14848905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
14858905a67cSAndy Whitcroft
14868905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
14878905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
14888905a67cSAndy Whitcroft
1489773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
149013214adfSAndy Whitcroft
149113214adfSAndy Whitcroft	return ($statement, $condition,
149213214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
149313214adfSAndy Whitcroft}
149413214adfSAndy Whitcroft
1495cf655043SAndy Whitcroftsub statement_lines {
1496cf655043SAndy Whitcroft	my ($stmt) = @_;
1497cf655043SAndy Whitcroft
1498cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1499cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1500cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1501cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1502cf655043SAndy Whitcroft
1503cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1504cf655043SAndy Whitcroft
1505cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1506cf655043SAndy Whitcroft}
1507cf655043SAndy Whitcroft
1508cf655043SAndy Whitcroftsub statement_rawlines {
1509cf655043SAndy Whitcroft	my ($stmt) = @_;
1510cf655043SAndy Whitcroft
1511cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1512cf655043SAndy Whitcroft
1513cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1514cf655043SAndy Whitcroft}
1515cf655043SAndy Whitcroft
1516cf655043SAndy Whitcroftsub statement_block_size {
1517cf655043SAndy Whitcroft	my ($stmt) = @_;
1518cf655043SAndy Whitcroft
1519cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1520cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1521cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1522cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1523cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1524cf655043SAndy Whitcroft
1525cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1526cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1527cf655043SAndy Whitcroft
1528cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1529cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1530cf655043SAndy Whitcroft
1531cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1532cf655043SAndy Whitcroft		return $stmt_lines;
1533cf655043SAndy Whitcroft	} else {
1534cf655043SAndy Whitcroft		return $stmt_statements;
1535cf655043SAndy Whitcroft	}
1536cf655043SAndy Whitcroft}
1537cf655043SAndy Whitcroft
153813214adfSAndy Whitcroftsub ctx_statement_full {
153913214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
154013214adfSAndy Whitcroft	my ($statement, $condition, $level);
154113214adfSAndy Whitcroft
154213214adfSAndy Whitcroft	my (@chunks);
154313214adfSAndy Whitcroft
1544cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
154513214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
154613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1547773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
154813214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1549cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1550cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1551cf655043SAndy Whitcroft	}
1552cf655043SAndy Whitcroft
1553cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1554cf655043SAndy Whitcroft	# could continue the statement.
1555cf655043SAndy Whitcroft	for (;;) {
155613214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
155713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1558cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1559773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1560cf655043SAndy Whitcroft		#print "C: push\n";
1561cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
156213214adfSAndy Whitcroft	}
156313214adfSAndy Whitcroft
156413214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15658905a67cSAndy Whitcroft}
15668905a67cSAndy Whitcroft
15674a0df2efSAndy Whitcroftsub ctx_block_get {
1568f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15694a0df2efSAndy Whitcroft	my $line;
15704a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15714a0df2efSAndy Whitcroft	my $blk = '';
15724a0df2efSAndy Whitcroft	my @o;
15734a0df2efSAndy Whitcroft	my @c;
15744a0df2efSAndy Whitcroft	my @res = ();
15754a0df2efSAndy Whitcroft
1576f0a594c1SAndy Whitcroft	my $level = 0;
15774635f4fbSAndy Whitcroft	my @stack = ($level);
157800df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
157900df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
158000df344fSAndy Whitcroft		$remain--;
158100df344fSAndy Whitcroft
158200df344fSAndy Whitcroft		$blk .= $rawlines[$line];
15834635f4fbSAndy Whitcroft
15844635f4fbSAndy Whitcroft		# Handle nested #if/#else.
158501464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
15864635f4fbSAndy Whitcroft			push(@stack, $level);
158701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
15884635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
158901464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15904635f4fbSAndy Whitcroft			$level = pop(@stack);
15914635f4fbSAndy Whitcroft		}
15924635f4fbSAndy Whitcroft
159301464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1594f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1595f0a594c1SAndy Whitcroft			if ($off > 0) {
1596f0a594c1SAndy Whitcroft				$off--;
1597f0a594c1SAndy Whitcroft				next;
1598f0a594c1SAndy Whitcroft			}
15994a0df2efSAndy Whitcroft
1600f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1601f0a594c1SAndy Whitcroft				$level--;
1602f0a594c1SAndy Whitcroft				last if ($level == 0);
1603f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1604f0a594c1SAndy Whitcroft				$level++;
1605f0a594c1SAndy Whitcroft			}
1606f0a594c1SAndy Whitcroft		}
16074a0df2efSAndy Whitcroft
1608f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
160900df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
16104a0df2efSAndy Whitcroft		}
16114a0df2efSAndy Whitcroft
1612f0a594c1SAndy Whitcroft		last if ($level == 0);
16134a0df2efSAndy Whitcroft	}
16144a0df2efSAndy Whitcroft
1615f0a594c1SAndy Whitcroft	return ($level, @res);
16164a0df2efSAndy Whitcroft}
16174a0df2efSAndy Whitcroftsub ctx_block_outer {
16184a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16194a0df2efSAndy Whitcroft
1620f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1621f0a594c1SAndy Whitcroft	return @r;
16224a0df2efSAndy Whitcroft}
16234a0df2efSAndy Whitcroftsub ctx_block {
16244a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16254a0df2efSAndy Whitcroft
1626f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1627f0a594c1SAndy Whitcroft	return @r;
1628653d4876SAndy Whitcroft}
1629653d4876SAndy Whitcroftsub ctx_statement {
1630f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1631f0a594c1SAndy Whitcroft
1632f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1633f0a594c1SAndy Whitcroft	return @r;
1634f0a594c1SAndy Whitcroft}
1635f0a594c1SAndy Whitcroftsub ctx_block_level {
1636653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1637653d4876SAndy Whitcroft
1638f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16394a0df2efSAndy Whitcroft}
16409c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16419c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16429c0ca6f9SAndy Whitcroft
16439c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16449c0ca6f9SAndy Whitcroft}
16454a0df2efSAndy Whitcroft
16464a0df2efSAndy Whitcroftsub ctx_locate_comment {
16474a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16484a0df2efSAndy Whitcroft
16494a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1650beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
16514a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16524a0df2efSAndy Whitcroft
16534a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16544a0df2efSAndy Whitcroft	# comment.
16554a0df2efSAndy Whitcroft	my $in_comment = 0;
16564a0df2efSAndy Whitcroft	$current_comment = '';
16574a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
165800df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
165900df344fSAndy Whitcroft		#warn "           $line\n";
16604a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16614a0df2efSAndy Whitcroft			$in_comment = 1;
16624a0df2efSAndy Whitcroft		}
16634a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16644a0df2efSAndy Whitcroft			$in_comment = 1;
16654a0df2efSAndy Whitcroft		}
16664a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16674a0df2efSAndy Whitcroft			$current_comment = '';
16684a0df2efSAndy Whitcroft		}
16694a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16704a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16714a0df2efSAndy Whitcroft			$in_comment = 0;
16724a0df2efSAndy Whitcroft		}
16734a0df2efSAndy Whitcroft	}
16744a0df2efSAndy Whitcroft
16754a0df2efSAndy Whitcroft	chomp($current_comment);
16764a0df2efSAndy Whitcroft	return($current_comment);
16774a0df2efSAndy Whitcroft}
16784a0df2efSAndy Whitcroftsub ctx_has_comment {
16794a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16804a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
16814a0df2efSAndy Whitcroft
168200df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
16834a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
16844a0df2efSAndy Whitcroft
16854a0df2efSAndy Whitcroft	return ($cmt ne '');
16864a0df2efSAndy Whitcroft}
16874a0df2efSAndy Whitcroft
16884d001e4dSAndy Whitcroftsub raw_line {
16894d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16904d001e4dSAndy Whitcroft
16914d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16924d001e4dSAndy Whitcroft	$cnt++;
16934d001e4dSAndy Whitcroft
16944d001e4dSAndy Whitcroft	my $line;
16954d001e4dSAndy Whitcroft	while ($cnt) {
16964d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16974d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16984d001e4dSAndy Whitcroft		$cnt--;
16994d001e4dSAndy Whitcroft	}
17004d001e4dSAndy Whitcroft
17014d001e4dSAndy Whitcroft	return $line;
17024d001e4dSAndy Whitcroft}
17034d001e4dSAndy Whitcroft
17042a9f9d85STobin C. Hardingsub get_stat_real {
17052a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
17062a9f9d85STobin C. Harding
17072a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
17082a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
17092a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
17102a9f9d85STobin C. Harding	}
17112a9f9d85STobin C. Harding
17122a9f9d85STobin C. Harding	return $stat_real;
17132a9f9d85STobin C. Harding}
17142a9f9d85STobin C. Harding
1715e3d95a2aSTobin C. Hardingsub get_stat_here {
1716e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1717e3d95a2aSTobin C. Harding
1718e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1719e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1720e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1721e3d95a2aSTobin C. Harding	}
1722e3d95a2aSTobin C. Harding
1723e3d95a2aSTobin C. Harding	return $herectx;
1724e3d95a2aSTobin C. Harding}
1725e3d95a2aSTobin C. Harding
17260a920b5bSAndy Whitcroftsub cat_vet {
17270a920b5bSAndy Whitcroft	my ($vet) = @_;
17289c0ca6f9SAndy Whitcroft	my ($res, $coded);
17290a920b5bSAndy Whitcroft
17309c0ca6f9SAndy Whitcroft	$res = '';
17316c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
17326c72ffaaSAndy Whitcroft		$res .= $1;
17336c72ffaaSAndy Whitcroft		if ($2 ne '') {
17349c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
17356c72ffaaSAndy Whitcroft			$res .= $coded;
17366c72ffaaSAndy Whitcroft		}
17379c0ca6f9SAndy Whitcroft	}
17389c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
17390a920b5bSAndy Whitcroft
17409c0ca6f9SAndy Whitcroft	return $res;
17410a920b5bSAndy Whitcroft}
17420a920b5bSAndy Whitcroft
1743c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1744cf655043SAndy Whitcroftmy $av_pending;
1745c2fdda0dSAndy Whitcroftmy @av_paren_type;
17461f65f947SAndy Whitcroftmy $av_pend_colon;
1747c2fdda0dSAndy Whitcroft
1748c2fdda0dSAndy Whitcroftsub annotate_reset {
1749c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1750cf655043SAndy Whitcroft	$av_pending = '_';
1751cf655043SAndy Whitcroft	@av_paren_type = ('E');
17521f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1753c2fdda0dSAndy Whitcroft}
1754c2fdda0dSAndy Whitcroft
17556c72ffaaSAndy Whitcroftsub annotate_values {
17566c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
17576c72ffaaSAndy Whitcroft
17586c72ffaaSAndy Whitcroft	my $res;
17591f65f947SAndy Whitcroft	my $var = '_' x length($stream);
17606c72ffaaSAndy Whitcroft	my $cur = $stream;
17616c72ffaaSAndy Whitcroft
1762c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
17636c72ffaaSAndy Whitcroft
17646c72ffaaSAndy Whitcroft	while (length($cur)) {
1765773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1766cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1767171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
17686c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1769c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1770c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1771cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1772c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
17736c72ffaaSAndy Whitcroft			}
17746c72ffaaSAndy Whitcroft
1775c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
17769446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
17779446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1778addcdceaSAndy Whitcroft			$type = 'c';
17799446ef56SAndy Whitcroft
1780e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1781c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
17826c72ffaaSAndy Whitcroft			$type = 'T';
17836c72ffaaSAndy Whitcroft
1784389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1785389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1786389a2fe5SAndy Whitcroft			$type = 'T';
1787389a2fe5SAndy Whitcroft
1788c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1789171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1790c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1791171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1792171ae1a4SAndy Whitcroft			if ($2 ne '') {
1793cf655043SAndy Whitcroft				$av_pending = 'N';
1794171ae1a4SAndy Whitcroft			}
1795171ae1a4SAndy Whitcroft			$type = 'E';
1796171ae1a4SAndy Whitcroft
1797c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1798171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1799171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1800171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
18016c72ffaaSAndy Whitcroft
1802c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1803cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1804c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1805cf655043SAndy Whitcroft
1806cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1807cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1808171ae1a4SAndy Whitcroft			$type = 'E';
1809cf655043SAndy Whitcroft
1810c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1811cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1812cf655043SAndy Whitcroft			$av_preprocessor = 1;
1813cf655043SAndy Whitcroft
1814cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1815cf655043SAndy Whitcroft
1816171ae1a4SAndy Whitcroft			$type = 'E';
1817cf655043SAndy Whitcroft
1818c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1819cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1820cf655043SAndy Whitcroft
1821cf655043SAndy Whitcroft			$av_preprocessor = 1;
1822cf655043SAndy Whitcroft
1823cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1824cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1825cf655043SAndy Whitcroft			pop(@av_paren_type);
1826cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1827171ae1a4SAndy Whitcroft			$type = 'E';
18286c72ffaaSAndy Whitcroft
18296c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1830c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
18316c72ffaaSAndy Whitcroft
1832171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1833171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1834171ae1a4SAndy Whitcroft			$av_pending = $type;
1835171ae1a4SAndy Whitcroft			$type = 'N';
1836171ae1a4SAndy Whitcroft
18376c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1838c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
18396c72ffaaSAndy Whitcroft			if (defined $2) {
1840cf655043SAndy Whitcroft				$av_pending = 'V';
18416c72ffaaSAndy Whitcroft			}
18426c72ffaaSAndy Whitcroft			$type = 'N';
18436c72ffaaSAndy Whitcroft
184414b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1845c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
184614b111c1SAndy Whitcroft			$av_pending = 'E';
18476c72ffaaSAndy Whitcroft			$type = 'N';
18486c72ffaaSAndy Whitcroft
18491f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
18501f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
18511f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18521f65f947SAndy Whitcroft			$type = 'N';
18531f65f947SAndy Whitcroft
185414b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1855c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
18566c72ffaaSAndy Whitcroft			$type = 'N';
18576c72ffaaSAndy Whitcroft
18586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1859c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1860cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1861cf655043SAndy Whitcroft			$av_pending = '_';
18626c72ffaaSAndy Whitcroft			$type = 'N';
18636c72ffaaSAndy Whitcroft
18646c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1865cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1866cf655043SAndy Whitcroft			if ($new_type ne '_') {
1867cf655043SAndy Whitcroft				$type = $new_type;
1868c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1869c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
18706c72ffaaSAndy Whitcroft			} else {
1871c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
18726c72ffaaSAndy Whitcroft			}
18736c72ffaaSAndy Whitcroft
1874c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1875c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1876c8cb2ca3SAndy Whitcroft			$type = 'V';
1877cf655043SAndy Whitcroft			$av_pending = 'V';
18786c72ffaaSAndy Whitcroft
18798e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
18808e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
18811f65f947SAndy Whitcroft				$av_pend_colon = 'B';
18828e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
18838e761b04SAndy Whitcroft				$av_pend_colon = 'L';
18841f65f947SAndy Whitcroft			}
18851f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
18861f65f947SAndy Whitcroft			$type = 'V';
18871f65f947SAndy Whitcroft
18886c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1889c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
18906c72ffaaSAndy Whitcroft			$type = 'V';
18916c72ffaaSAndy Whitcroft
18926c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1893c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
18946c72ffaaSAndy Whitcroft			$type = 'N';
18956c72ffaaSAndy Whitcroft
1896cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1897c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
189813214adfSAndy Whitcroft			$type = 'E';
18991f65f947SAndy Whitcroft			$av_pend_colon = 'O';
190013214adfSAndy Whitcroft
19018e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
19028e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
19038e761b04SAndy Whitcroft			$type = 'C';
19048e761b04SAndy Whitcroft
19051f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
19061f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
19071f65f947SAndy Whitcroft			$type = 'N';
19081f65f947SAndy Whitcroft
19091f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
19101f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
19111f65f947SAndy Whitcroft
19121f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
19131f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
19141f65f947SAndy Whitcroft				$type = 'E';
19151f65f947SAndy Whitcroft			} else {
19161f65f947SAndy Whitcroft				$type = 'N';
19171f65f947SAndy Whitcroft			}
19181f65f947SAndy Whitcroft			$av_pend_colon = 'O';
19191f65f947SAndy Whitcroft
19208e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
192113214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
19226c72ffaaSAndy Whitcroft			$type = 'N';
19236c72ffaaSAndy Whitcroft
19240d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
192574048ed8SAndy Whitcroft			my $variant;
192674048ed8SAndy Whitcroft
192774048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
192874048ed8SAndy Whitcroft			if ($type eq 'V') {
192974048ed8SAndy Whitcroft				$variant = 'B';
193074048ed8SAndy Whitcroft			} else {
193174048ed8SAndy Whitcroft				$variant = 'U';
193274048ed8SAndy Whitcroft			}
193374048ed8SAndy Whitcroft
193474048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
193574048ed8SAndy Whitcroft			$type = 'N';
193674048ed8SAndy Whitcroft
19376c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1938c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
19396c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
19406c72ffaaSAndy Whitcroft				$type = 'N';
19416c72ffaaSAndy Whitcroft			}
19426c72ffaaSAndy Whitcroft
19436c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1944c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
19456c72ffaaSAndy Whitcroft		}
19466c72ffaaSAndy Whitcroft		if (defined $1) {
19476c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
19486c72ffaaSAndy Whitcroft			$res .= $type x length($1);
19496c72ffaaSAndy Whitcroft		}
19506c72ffaaSAndy Whitcroft	}
19516c72ffaaSAndy Whitcroft
19521f65f947SAndy Whitcroft	return ($res, $var);
19536c72ffaaSAndy Whitcroft}
19546c72ffaaSAndy Whitcroft
19558905a67cSAndy Whitcroftsub possible {
195613214adfSAndy Whitcroft	my ($possible, $line) = @_;
19579a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
19580776e594SAndy Whitcroft		^(?:
19590776e594SAndy Whitcroft			$Modifier|
19600776e594SAndy Whitcroft			$Storage|
19610776e594SAndy Whitcroft			$Type|
19629a974fdbSAndy Whitcroft			DEFINE_\S+
19639a974fdbSAndy Whitcroft		)$|
19649a974fdbSAndy Whitcroft		^(?:
19650776e594SAndy Whitcroft			goto|
19660776e594SAndy Whitcroft			return|
19670776e594SAndy Whitcroft			case|
19680776e594SAndy Whitcroft			else|
19690776e594SAndy Whitcroft			asm|__asm__|
197089a88353SAndy Whitcroft			do|
197189a88353SAndy Whitcroft			\#|
197289a88353SAndy Whitcroft			\#\#|
19739a974fdbSAndy Whitcroft		)(?:\s|$)|
19740776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
19759a974fdbSAndy Whitcroft	    )}x;
19769a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
19779a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1978c45dcabdSAndy Whitcroft		# Check for modifiers.
1979c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1980c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1981c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1982c45dcabdSAndy Whitcroft
1983c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1984c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1985d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
19869a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1987d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1988485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1989d2506586SAndy Whitcroft				}
19909a974fdbSAndy Whitcroft			}
1991c45dcabdSAndy Whitcroft
1992c45dcabdSAndy Whitcroft		} else {
199313214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1994485ff23eSAlex Dowad			push(@typeListFile, $possible);
1995c45dcabdSAndy Whitcroft		}
19968905a67cSAndy Whitcroft		build_types();
19970776e594SAndy Whitcroft	} else {
19980776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
19998905a67cSAndy Whitcroft	}
20008905a67cSAndy Whitcroft}
20018905a67cSAndy Whitcroft
20026c72ffaaSAndy Whitcroftmy $prefix = '';
20036c72ffaaSAndy Whitcroft
2004000d1cc1SJoe Perchessub show_type {
2005cbec18afSJoe Perches	my ($type) = @_;
200691bfe484SJoe Perches
2007522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2008522b837cSAlexey Dobriyan
2009cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2010cbec18afSJoe Perches
2011cbec18afSJoe Perches	return !defined $ignore_type{$type};
2012000d1cc1SJoe Perches}
2013000d1cc1SJoe Perches
2014f0a594c1SAndy Whitcroftsub report {
2015cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2016cbec18afSJoe Perches
2017cbec18afSJoe Perches	if (!show_type($type) ||
2018cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2019773647a0SAndy Whitcroft		return 0;
2020773647a0SAndy Whitcroft	}
202157230297SJoe Perches	my $output = '';
2022737c0767SJohn Brooks	if ($color) {
202357230297SJoe Perches		if ($level eq 'ERROR') {
202457230297SJoe Perches			$output .= RED;
202557230297SJoe Perches		} elsif ($level eq 'WARNING') {
202657230297SJoe Perches			$output .= YELLOW;
2027000d1cc1SJoe Perches		} else {
202857230297SJoe Perches			$output .= GREEN;
2029000d1cc1SJoe Perches		}
203057230297SJoe Perches	}
203157230297SJoe Perches	$output .= $prefix . $level . ':';
203257230297SJoe Perches	if ($show_types) {
2033737c0767SJohn Brooks		$output .= BLUE if ($color);
203457230297SJoe Perches		$output .= "$type:";
203557230297SJoe Perches	}
2036737c0767SJohn Brooks	$output .= RESET if ($color);
203757230297SJoe Perches	$output .= ' ' . $msg . "\n";
203834d8815fSJoe Perches
203934d8815fSJoe Perches	if ($showfile) {
204034d8815fSJoe Perches		my @lines = split("\n", $output, -1);
204134d8815fSJoe Perches		splice(@lines, 1, 1);
204234d8815fSJoe Perches		$output = join("\n", @lines);
204334d8815fSJoe Perches	}
204457230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
20458905a67cSAndy Whitcroft
204657230297SJoe Perches	push(our @report, $output);
2047773647a0SAndy Whitcroft
2048773647a0SAndy Whitcroft	return 1;
2049f0a594c1SAndy Whitcroft}
2050cbec18afSJoe Perches
2051f0a594c1SAndy Whitcroftsub report_dump {
205213214adfSAndy Whitcroft	our @report;
2053f0a594c1SAndy Whitcroft}
2054000d1cc1SJoe Perches
2055d752fcc8SJoe Perchessub fixup_current_range {
2056d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2057d752fcc8SJoe Perches
2058d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2059d752fcc8SJoe Perches		my $o = $1;
2060d752fcc8SJoe Perches		my $l = $2;
2061d752fcc8SJoe Perches		my $no = $o + $offset;
2062d752fcc8SJoe Perches		my $nl = $l + $length;
2063d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2064d752fcc8SJoe Perches	}
2065d752fcc8SJoe Perches}
2066d752fcc8SJoe Perches
2067d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2068d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2069d752fcc8SJoe Perches
2070d752fcc8SJoe Perches	my $range_last_linenr = 0;
2071d752fcc8SJoe Perches	my $delta_offset = 0;
2072d752fcc8SJoe Perches
2073d752fcc8SJoe Perches	my $old_linenr = 0;
2074d752fcc8SJoe Perches	my $new_linenr = 0;
2075d752fcc8SJoe Perches
2076d752fcc8SJoe Perches	my $next_insert = 0;
2077d752fcc8SJoe Perches	my $next_delete = 0;
2078d752fcc8SJoe Perches
2079d752fcc8SJoe Perches	my @lines = ();
2080d752fcc8SJoe Perches
2081d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2082d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2083d752fcc8SJoe Perches
2084d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2085d752fcc8SJoe Perches		my $save_line = 1;
2086d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2087323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2088d752fcc8SJoe Perches			$delta_offset = 0;
2089d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2090d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2091d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2092d752fcc8SJoe Perches		}
2093d752fcc8SJoe Perches
2094d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2095d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2096d752fcc8SJoe Perches			$save_line = 0;
2097d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2098d752fcc8SJoe Perches		}
2099d752fcc8SJoe Perches
2100d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2101d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2102d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2103d752fcc8SJoe Perches			$new_linenr++;
2104d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2105d752fcc8SJoe Perches		}
2106d752fcc8SJoe Perches
2107d752fcc8SJoe Perches		if ($save_line) {
2108d752fcc8SJoe Perches			push(@lines, $line);
2109d752fcc8SJoe Perches			$new_linenr++;
2110d752fcc8SJoe Perches		}
2111d752fcc8SJoe Perches
2112d752fcc8SJoe Perches		$old_linenr++;
2113d752fcc8SJoe Perches	}
2114d752fcc8SJoe Perches
2115d752fcc8SJoe Perches	return @lines;
2116d752fcc8SJoe Perches}
2117d752fcc8SJoe Perches
2118f2d7e4d4SJoe Perchessub fix_insert_line {
2119f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2120f2d7e4d4SJoe Perches
2121f2d7e4d4SJoe Perches	my $inserted = {
2122f2d7e4d4SJoe Perches		LINENR => $linenr,
2123f2d7e4d4SJoe Perches		LINE => $line,
2124f2d7e4d4SJoe Perches	};
2125f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2126f2d7e4d4SJoe Perches}
2127f2d7e4d4SJoe Perches
2128f2d7e4d4SJoe Perchessub fix_delete_line {
2129f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2130f2d7e4d4SJoe Perches
2131f2d7e4d4SJoe Perches	my $deleted = {
2132f2d7e4d4SJoe Perches		LINENR => $linenr,
2133f2d7e4d4SJoe Perches		LINE => $line,
2134f2d7e4d4SJoe Perches	};
2135f2d7e4d4SJoe Perches
2136f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2137f2d7e4d4SJoe Perches}
2138f2d7e4d4SJoe Perches
2139de7d4f0eSAndy Whitcroftsub ERROR {
2140cbec18afSJoe Perches	my ($type, $msg) = @_;
2141cbec18afSJoe Perches
2142cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2143de7d4f0eSAndy Whitcroft		our $clean = 0;
21446c72ffaaSAndy Whitcroft		our $cnt_error++;
21453705ce5bSJoe Perches		return 1;
2146de7d4f0eSAndy Whitcroft	}
21473705ce5bSJoe Perches	return 0;
2148773647a0SAndy Whitcroft}
2149de7d4f0eSAndy Whitcroftsub WARN {
2150cbec18afSJoe Perches	my ($type, $msg) = @_;
2151cbec18afSJoe Perches
2152cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2153de7d4f0eSAndy Whitcroft		our $clean = 0;
21546c72ffaaSAndy Whitcroft		our $cnt_warn++;
21553705ce5bSJoe Perches		return 1;
2156de7d4f0eSAndy Whitcroft	}
21573705ce5bSJoe Perches	return 0;
2158773647a0SAndy Whitcroft}
2159de7d4f0eSAndy Whitcroftsub CHK {
2160cbec18afSJoe Perches	my ($type, $msg) = @_;
2161cbec18afSJoe Perches
2162cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2163de7d4f0eSAndy Whitcroft		our $clean = 0;
21646c72ffaaSAndy Whitcroft		our $cnt_chk++;
21653705ce5bSJoe Perches		return 1;
21666c72ffaaSAndy Whitcroft	}
21673705ce5bSJoe Perches	return 0;
2168de7d4f0eSAndy Whitcroft}
2169de7d4f0eSAndy Whitcroft
21706ecd9674SAndy Whitcroftsub check_absolute_file {
21716ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
21726ecd9674SAndy Whitcroft	my $file = $absolute;
21736ecd9674SAndy Whitcroft
21746ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
21756ecd9674SAndy Whitcroft
21766ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
21776ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
21786ecd9674SAndy Whitcroft		if (-f "$root/$file") {
21796ecd9674SAndy Whitcroft			##print "file<$file>\n";
21806ecd9674SAndy Whitcroft			last;
21816ecd9674SAndy Whitcroft		}
21826ecd9674SAndy Whitcroft	}
21836ecd9674SAndy Whitcroft	if (! -f _)  {
21846ecd9674SAndy Whitcroft		return 0;
21856ecd9674SAndy Whitcroft	}
21866ecd9674SAndy Whitcroft
21876ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
21886ecd9674SAndy Whitcroft	my $prefix = $absolute;
21896ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
21906ecd9674SAndy Whitcroft
21916ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
21926ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2193000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2194000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
21956ecd9674SAndy Whitcroft	}
21966ecd9674SAndy Whitcroft}
21976ecd9674SAndy Whitcroft
21983705ce5bSJoe Perchessub trim {
21993705ce5bSJoe Perches	my ($string) = @_;
22003705ce5bSJoe Perches
2201b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2202b34c648bSJoe Perches
2203b34c648bSJoe Perches	return $string;
2204b34c648bSJoe Perches}
2205b34c648bSJoe Perches
2206b34c648bSJoe Perchessub ltrim {
2207b34c648bSJoe Perches	my ($string) = @_;
2208b34c648bSJoe Perches
2209b34c648bSJoe Perches	$string =~ s/^\s+//;
2210b34c648bSJoe Perches
2211b34c648bSJoe Perches	return $string;
2212b34c648bSJoe Perches}
2213b34c648bSJoe Perches
2214b34c648bSJoe Perchessub rtrim {
2215b34c648bSJoe Perches	my ($string) = @_;
2216b34c648bSJoe Perches
2217b34c648bSJoe Perches	$string =~ s/\s+$//;
22183705ce5bSJoe Perches
22193705ce5bSJoe Perches	return $string;
22203705ce5bSJoe Perches}
22213705ce5bSJoe Perches
222252ea8506SJoe Perchessub string_find_replace {
222352ea8506SJoe Perches	my ($string, $find, $replace) = @_;
222452ea8506SJoe Perches
222552ea8506SJoe Perches	$string =~ s/$find/$replace/g;
222652ea8506SJoe Perches
222752ea8506SJoe Perches	return $string;
222852ea8506SJoe Perches}
222952ea8506SJoe Perches
22303705ce5bSJoe Perchessub tabify {
22313705ce5bSJoe Perches	my ($leading) = @_;
22323705ce5bSJoe Perches
22333705ce5bSJoe Perches	my $source_indent = 8;
22343705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
22353705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
22363705ce5bSJoe Perches
22373705ce5bSJoe Perches	#convert leading spaces to tabs
22383705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
22393705ce5bSJoe Perches	#Remove spaces before a tab
22403705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
22413705ce5bSJoe Perches
22423705ce5bSJoe Perches	return "$leading";
22433705ce5bSJoe Perches}
22443705ce5bSJoe Perches
2245d1fe9c09SJoe Perchessub pos_last_openparen {
2246d1fe9c09SJoe Perches	my ($line) = @_;
2247d1fe9c09SJoe Perches
2248d1fe9c09SJoe Perches	my $pos = 0;
2249d1fe9c09SJoe Perches
2250d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2251d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2252d1fe9c09SJoe Perches
2253d1fe9c09SJoe Perches	my $last_openparen = 0;
2254d1fe9c09SJoe Perches
2255d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2256d1fe9c09SJoe Perches		return -1;
2257d1fe9c09SJoe Perches	}
2258d1fe9c09SJoe Perches
2259d1fe9c09SJoe Perches	my $len = length($line);
2260d1fe9c09SJoe Perches
2261d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2262d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2263d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2264d1fe9c09SJoe Perches			$pos += length($1) - 1;
2265d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2266d1fe9c09SJoe Perches			$last_openparen = $pos;
2267d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2268d1fe9c09SJoe Perches			last;
2269d1fe9c09SJoe Perches		}
2270d1fe9c09SJoe Perches	}
2271d1fe9c09SJoe Perches
227291cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2273d1fe9c09SJoe Perches}
2274d1fe9c09SJoe Perches
22750a920b5bSAndy Whitcroftsub process {
22760a920b5bSAndy Whitcroft	my $filename = shift;
22770a920b5bSAndy Whitcroft
22780a920b5bSAndy Whitcroft	my $linenr=0;
22790a920b5bSAndy Whitcroft	my $prevline="";
2280c2fdda0dSAndy Whitcroft	my $prevrawline="";
22810a920b5bSAndy Whitcroft	my $stashline="";
2282c2fdda0dSAndy Whitcroft	my $stashrawline="";
22830a920b5bSAndy Whitcroft
22844a0df2efSAndy Whitcroft	my $length;
22850a920b5bSAndy Whitcroft	my $indent;
22860a920b5bSAndy Whitcroft	my $previndent=0;
22870a920b5bSAndy Whitcroft	my $stashindent=0;
22880a920b5bSAndy Whitcroft
2289de7d4f0eSAndy Whitcroft	our $clean = 1;
22900a920b5bSAndy Whitcroft	my $signoff = 0;
2291cd261496SGeert Uytterhoeven	my $author = '';
2292cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
22930a920b5bSAndy Whitcroft	my $is_patch = 0;
2294133712a2SRob Herring	my $is_binding_patch = -1;
229529ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
229615662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2297ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2298490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2299bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
23002a076f40SJoe Perches	my $commit_log_long_line = 0;
2301e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
230213f1937eSJoe Perches	my $reported_maintainer_file = 0;
2303fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2304fa64205dSPasi Savanainen
2305365dd4eaSJoe Perches	my $last_blank_line = 0;
23065e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2307365dd4eaSJoe Perches
230813214adfSAndy Whitcroft	our @report = ();
23096c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
23106c72ffaaSAndy Whitcroft	our $cnt_error = 0;
23116c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
23126c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
23136c72ffaaSAndy Whitcroft
23140a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
23150a920b5bSAndy Whitcroft	my $realfile = '';
23160a920b5bSAndy Whitcroft	my $realline = 0;
23170a920b5bSAndy Whitcroft	my $realcnt = 0;
23180a920b5bSAndy Whitcroft	my $here = '';
231977cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23200a920b5bSAndy Whitcroft	my $in_comment = 0;
2321c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23220a920b5bSAndy Whitcroft	my $first_line = 0;
23231e855726SWolfram Sang	my $p1_prefix = '';
23240a920b5bSAndy Whitcroft
232513214adfSAndy Whitcroft	my $prev_values = 'E';
232613214adfSAndy Whitcroft
232713214adfSAndy Whitcroft	# suppression flags
2328773647a0SAndy Whitcroft	my %suppress_ifbraces;
2329170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
23302b474a1aSAndy Whitcroft	my %suppress_export;
23313e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2332653d4876SAndy Whitcroft
23337e51f197SJoe Perches	my %signatures = ();
2334323c1260SJoe Perches
2335c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2336de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2337c2fdda0dSAndy Whitcroft	#
2338de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2339de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2340773647a0SAndy Whitcroft
2341d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2342d8b07710SJoe Perches
23439f3a8992SRob Herring	my $checklicenseline = 1;
23449f3a8992SRob Herring
2345773647a0SAndy Whitcroft	sanitise_line_reset();
2346c2fdda0dSAndy Whitcroft	my $line;
2347c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2348773647a0SAndy Whitcroft		$linenr++;
2349773647a0SAndy Whitcroft		$line = $rawline;
2350c2fdda0dSAndy Whitcroft
23513705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
23523705ce5bSJoe Perches
2353773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2354de7d4f0eSAndy Whitcroft			$setup_docs = 0;
23558c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2356de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2357de7d4f0eSAndy Whitcroft			}
2358773647a0SAndy Whitcroft			#next;
2359de7d4f0eSAndy Whitcroft		}
236074fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2361773647a0SAndy Whitcroft			$realline=$1-1;
2362773647a0SAndy Whitcroft			if (defined $2) {
2363773647a0SAndy Whitcroft				$realcnt=$3+1;
2364773647a0SAndy Whitcroft			} else {
2365773647a0SAndy Whitcroft				$realcnt=1+1;
2366773647a0SAndy Whitcroft			}
2367c45dcabdSAndy Whitcroft			$in_comment = 0;
2368773647a0SAndy Whitcroft
2369773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2370773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2371773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2372773647a0SAndy Whitcroft			# at context start.
2373773647a0SAndy Whitcroft			my $edge;
237401fa9147SAndy Whitcroft			my $cnt = $realcnt;
237501fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
237601fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
237701fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
237801fa9147SAndy Whitcroft				$cnt--;
237901fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2380721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2381fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2382fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2383fae17daeSAndy Whitcroft					($edge) = $1;
2384fae17daeSAndy Whitcroft					last;
2385fae17daeSAndy Whitcroft				}
2386773647a0SAndy Whitcroft			}
2387773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2388773647a0SAndy Whitcroft				$in_comment = 1;
2389773647a0SAndy Whitcroft			}
2390773647a0SAndy Whitcroft
2391773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2392773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2393773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2394773647a0SAndy Whitcroft			if (!defined $edge &&
239583242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2396773647a0SAndy Whitcroft			{
2397773647a0SAndy Whitcroft				$in_comment = 1;
2398773647a0SAndy Whitcroft			}
2399773647a0SAndy Whitcroft
2400773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2401773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2402773647a0SAndy Whitcroft
2403171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2404773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2405171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2406773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2407773647a0SAndy Whitcroft		}
2408773647a0SAndy Whitcroft		push(@lines, $line);
2409773647a0SAndy Whitcroft
2410773647a0SAndy Whitcroft		if ($realcnt > 1) {
2411773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2412773647a0SAndy Whitcroft		} else {
2413773647a0SAndy Whitcroft			$realcnt = 0;
2414773647a0SAndy Whitcroft		}
2415773647a0SAndy Whitcroft
2416773647a0SAndy Whitcroft		#print "==>$rawline\n";
2417773647a0SAndy Whitcroft		#print "-->$line\n";
2418de7d4f0eSAndy Whitcroft
2419de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2420de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2421de7d4f0eSAndy Whitcroft		}
2422de7d4f0eSAndy Whitcroft	}
2423de7d4f0eSAndy Whitcroft
24246c72ffaaSAndy Whitcroft	$prefix = '';
24256c72ffaaSAndy Whitcroft
2426773647a0SAndy Whitcroft	$realcnt = 0;
2427773647a0SAndy Whitcroft	$linenr = 0;
2428194f66fcSJoe Perches	$fixlinenr = -1;
24290a920b5bSAndy Whitcroft	foreach my $line (@lines) {
24300a920b5bSAndy Whitcroft		$linenr++;
2431194f66fcSJoe Perches		$fixlinenr++;
24321b5539b1SJoe Perches		my $sline = $line;	#copy of $line
24331b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
24340a920b5bSAndy Whitcroft
2435c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
24366c72ffaaSAndy Whitcroft
243712c253abSJoe Perches# check if it's a mode change, rename or start of a patch
243812c253abSJoe Perches		if (!$in_commit_log &&
243912c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
244012c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
244112c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
244212c253abSJoe Perches			$is_patch = 1;
244312c253abSJoe Perches		}
244412c253abSJoe Perches
24450a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2446e518e9a5SJoe Perches		if (!$in_commit_log &&
244774fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
244874fd4f34SJoe Perches			my $context = $4;
24490a920b5bSAndy Whitcroft			$is_patch = 1;
24504a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
24510a920b5bSAndy Whitcroft			$realline=$1-1;
24520a920b5bSAndy Whitcroft			if (defined $2) {
24530a920b5bSAndy Whitcroft				$realcnt=$3+1;
24540a920b5bSAndy Whitcroft			} else {
24550a920b5bSAndy Whitcroft				$realcnt=1+1;
24560a920b5bSAndy Whitcroft			}
2457c2fdda0dSAndy Whitcroft			annotate_reset();
245813214adfSAndy Whitcroft			$prev_values = 'E';
245913214adfSAndy Whitcroft
2460773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2461170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
24622b474a1aSAndy Whitcroft			%suppress_export = ();
24633e469cdcSAndy Whitcroft			$suppress_statement = 0;
246474fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
246574fd4f34SJoe Perches				$context_function = $1;
246674fd4f34SJoe Perches			} else {
246774fd4f34SJoe Perches				undef $context_function;
246874fd4f34SJoe Perches			}
24690a920b5bSAndy Whitcroft			next;
24700a920b5bSAndy Whitcroft
24714a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
24724a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
24734a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2474773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
24750a920b5bSAndy Whitcroft			$realline++;
2476d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
24770a920b5bSAndy Whitcroft
24784a0df2efSAndy Whitcroft			# Measure the line length and indent.
2479c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
24800a920b5bSAndy Whitcroft
24810a920b5bSAndy Whitcroft			# Track the previous line.
24820a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
24830a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2484c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2485c2fdda0dSAndy Whitcroft
2486773647a0SAndy Whitcroft			#warn "line<$line>\n";
24876c72ffaaSAndy Whitcroft
2488d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2489d8aaf121SAndy Whitcroft			$realcnt--;
24900a920b5bSAndy Whitcroft		}
24910a920b5bSAndy Whitcroft
2492cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2493cc77cdcaSAndy Whitcroft
24946c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
24956c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2496773647a0SAndy Whitcroft
24972ac73b4fSJoe Perches		my $found_file = 0;
2498773647a0SAndy Whitcroft		# extract the filename as it passes
24993bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
25003bf9a009SRabin Vincent			$realfile = $1;
25012b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2502270c49a0SJoe Perches			$in_commit_log = 0;
25032ac73b4fSJoe Perches			$found_file = 1;
25043bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2505773647a0SAndy Whitcroft			$realfile = $1;
25062b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2507270c49a0SJoe Perches			$in_commit_log = 0;
25081e855726SWolfram Sang
25091e855726SWolfram Sang			$p1_prefix = $1;
2510e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2511e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2512000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2513000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
25141e855726SWolfram Sang			}
2515773647a0SAndy Whitcroft
2516c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2517000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2518000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2519773647a0SAndy Whitcroft			}
25202ac73b4fSJoe Perches			$found_file = 1;
25212ac73b4fSJoe Perches		}
25222ac73b4fSJoe Perches
252334d8815fSJoe Perches#make up the handle for any error we report on this line
252434d8815fSJoe Perches		if ($showfile) {
252534d8815fSJoe Perches			$prefix = "$realfile:$realline: "
252634d8815fSJoe Perches		} elsif ($emacs) {
25277d3a9f67SJoe Perches			if ($file) {
25287d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
25297d3a9f67SJoe Perches			} else {
253034d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
253134d8815fSJoe Perches			}
25327d3a9f67SJoe Perches		}
253334d8815fSJoe Perches
25342ac73b4fSJoe Perches		if ($found_file) {
253585b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
253685b0ee18SJoe Perches				WARN("OBSOLETE",
253785b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
253885b0ee18SJoe Perches			}
25397bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25402ac73b4fSJoe Perches				$check = 1;
25412ac73b4fSJoe Perches			} else {
25422ac73b4fSJoe Perches				$check = $check_orig;
25432ac73b4fSJoe Perches			}
25449f3a8992SRob Herring			$checklicenseline = 1;
2545133712a2SRob Herring
2546133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2547133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2548133712a2SRob Herring
2549133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2550133712a2SRob Herring
2551133712a2SRob Herring				if (($last_binding_patch != -1) &&
2552133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2553133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2554133712a2SRob Herring					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2555133712a2SRob Herring				}
2556133712a2SRob Herring			}
2557133712a2SRob Herring
2558773647a0SAndy Whitcroft			next;
2559773647a0SAndy Whitcroft		}
2560773647a0SAndy Whitcroft
2561389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
25620a920b5bSAndy Whitcroft
2563c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2564c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2565c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
25660a920b5bSAndy Whitcroft
25676c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
25686c72ffaaSAndy Whitcroft
2569490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2570490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2571490b292cSJoe Perches		if ($in_commit_log) {
2572490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2573490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2574490b292cSJoe Perches			}
2575490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2576490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2577490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2578490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2579490b292cSJoe Perches		}
2580490b292cSJoe Perches
2581e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2582e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2583e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2584e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2585e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2586e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2587e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2588e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2589e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2590e518e9a5SJoe Perches		}
2591e518e9a5SJoe Perches
25923bf9a009SRabin Vincent# Check for incorrect file permissions
25933bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
25943bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
259504db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
259604db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2597000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2598000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
25993bf9a009SRabin Vincent			}
26003bf9a009SRabin Vincent		}
26013bf9a009SRabin Vincent
2602cd261496SGeert Uytterhoeven# Check the patch for a From:
2603cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2604cd261496SGeert Uytterhoeven			$author = $1;
2605cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2606cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2607cd261496SGeert Uytterhoeven		}
2608cd261496SGeert Uytterhoeven
260920112475SJoe Perches# Check the patch for a signoff:
2610d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
26114a0df2efSAndy Whitcroft			$signoff++;
261215662b3eSJoe Perches			$in_commit_log = 0;
2613cd261496SGeert Uytterhoeven			if ($author ne '') {
2614cd261496SGeert Uytterhoeven				my $l = $line;
2615cd261496SGeert Uytterhoeven				$l =~ s/"//g;
2616cd261496SGeert Uytterhoeven				if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2617cd261496SGeert Uytterhoeven				    $authorsignoff = 1;
2618cd261496SGeert Uytterhoeven				}
2619cd261496SGeert Uytterhoeven			}
26200a920b5bSAndy Whitcroft		}
262120112475SJoe Perches
2622e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2623e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2624e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2625e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2626e0d975b1SJoe Perches		}
2627e0d975b1SJoe Perches
262820112475SJoe Perches# Check signature styles
2629270c49a0SJoe Perches		if (!$in_header_lines &&
2630ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
263120112475SJoe Perches			my $space_before = $1;
263220112475SJoe Perches			my $sign_off = $2;
263320112475SJoe Perches			my $space_after = $3;
263420112475SJoe Perches			my $email = $4;
263520112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
263620112475SJoe Perches
2637ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2638ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2639ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2640ce0338dfSJoe Perches			}
264120112475SJoe Perches			if (defined $space_before && $space_before ne "") {
26423705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26433705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
26443705ce5bSJoe Perches				    $fix) {
2645194f66fcSJoe Perches					$fixed[$fixlinenr] =
26463705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26473705ce5bSJoe Perches				}
264820112475SJoe Perches			}
264920112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
26503705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26513705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
26523705ce5bSJoe Perches				    $fix) {
2653194f66fcSJoe Perches					$fixed[$fixlinenr] =
26543705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26553705ce5bSJoe Perches				}
26563705ce5bSJoe Perches
265720112475SJoe Perches			}
265820112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
26593705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26603705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
26613705ce5bSJoe Perches				    $fix) {
2662194f66fcSJoe Perches					$fixed[$fixlinenr] =
26633705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26643705ce5bSJoe Perches				}
266520112475SJoe Perches			}
266620112475SJoe Perches
266720112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
266820112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
266920112475SJoe Perches			if ($suggested_email eq "") {
2670000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2671000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
267220112475SJoe Perches			} else {
267320112475SJoe Perches				my $dequoted = $suggested_email;
267420112475SJoe Perches				$dequoted =~ s/^"//;
267520112475SJoe Perches				$dequoted =~ s/" </ </;
267620112475SJoe Perches				# Don't force email to have quotes
267720112475SJoe Perches				# Allow just an angle bracketed address
267820112475SJoe Perches				if ("$dequoted$comment" ne $email &&
267920112475SJoe Perches				    "<$email_address>$comment" ne $email &&
268020112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2681000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2682000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
268320112475SJoe Perches				}
26840a920b5bSAndy Whitcroft			}
26857e51f197SJoe Perches
26867e51f197SJoe Perches# Check for duplicate signatures
26877e51f197SJoe Perches			my $sig_nospace = $line;
26887e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
26897e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
26907e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
26917e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
26927e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
26937e51f197SJoe Perches			} else {
26947e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
26957e51f197SJoe Perches			}
26966c5d24eeSSean Christopherson
26976c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
26986c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
26996c5d24eeSSean Christopherson				if ($email eq $author) {
27006c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27016c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
27026c5d24eeSSean Christopherson				}
27036c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
27046c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27056c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
27066c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
27076c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27086c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27096c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
27106c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27116c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27126c5d24eeSSean Christopherson				}
27136c5d24eeSSean Christopherson			}
27140a920b5bSAndy Whitcroft		}
27150a920b5bSAndy Whitcroft
2716a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2717a2fe16b9SJoe Perches		if ($in_header_lines &&
2718a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2719a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2720a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2721a2fe16b9SJoe Perches		}
2722a2fe16b9SJoe Perches
27237ebd05efSChristopher Covington# Check for unwanted Gerrit info
27247ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
27257ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
27267ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
27277ebd05efSChristopher Covington		}
27287ebd05efSChristopher Covington
2729369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2730369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2731369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2732369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2733369c8dd3SJoe Perches					# timestamp
2734634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2735634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2736634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2737634cffccSJoe Perches					# stack dump address styles
2738369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2739369c8dd3SJoe Perches		}
2740369c8dd3SJoe Perches
27412a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
27422a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2743bf4daf12SJoe Perches		    length($line) > 75 &&
2744bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2745bf4daf12SJoe Perches					# file delta changes
2746bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2747bf4daf12SJoe Perches					# filename then :
2748bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2749bf4daf12SJoe Perches					# A Fixes: or Link: line
2750bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
27512a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
27522a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
27532a076f40SJoe Perches			$commit_log_long_line = 1;
27542a076f40SJoe Perches		}
27552a076f40SJoe Perches
2756bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2757bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2758bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2759bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2760bf4daf12SJoe Perches		}
2761bf4daf12SJoe Perches
27620d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2763369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2764aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2765e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2766fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2767aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2768369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2769bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2770fe043ea1SJoe Perches			my $init_char = "c";
2771fe043ea1SJoe Perches			my $orig_commit = "";
27720d7835fcSJoe Perches			my $short = 1;
27730d7835fcSJoe Perches			my $long = 0;
27740d7835fcSJoe Perches			my $case = 1;
27750d7835fcSJoe Perches			my $space = 1;
27760d7835fcSJoe Perches			my $hasdesc = 0;
277719c146a6SJoe Perches			my $hasparens = 0;
27780d7835fcSJoe Perches			my $id = '0123456789ab';
27790d7835fcSJoe Perches			my $orig_desc = "commit description";
27800d7835fcSJoe Perches			my $description = "";
27810d7835fcSJoe Perches
2782fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2783fe043ea1SJoe Perches				$init_char = $1;
2784fe043ea1SJoe Perches				$orig_commit = lc($2);
2785fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2786fe043ea1SJoe Perches				$orig_commit = lc($1);
2787fe043ea1SJoe Perches			}
2788fe043ea1SJoe Perches
27890d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
27900d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
27910d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
27920d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
27930d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
27940d7835fcSJoe Perches				$orig_desc = $1;
279519c146a6SJoe Perches				$hasparens = 1;
27960d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
27970d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
27980d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
27990d7835fcSJoe Perches				$orig_desc = $1;
280019c146a6SJoe Perches				$hasparens = 1;
2801b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2802b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2803b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2804b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2805b671fde0SJoe Perches				$orig_desc = $1;
2806b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2807b671fde0SJoe Perches				$orig_desc .= " " . $1;
280819c146a6SJoe Perches				$hasparens = 1;
28090d7835fcSJoe Perches			}
28100d7835fcSJoe Perches
28110d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
28120d7835fcSJoe Perches							      $id, $orig_desc);
28130d7835fcSJoe Perches
2814948b133aSHeinrich Schuchardt			if (defined($id) &&
2815948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2816d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
28170d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
28180d7835fcSJoe Perches			}
2819d311cd44SJoe Perches		}
2820d311cd44SJoe Perches
282113f1937eSJoe Perches# Check for added, moved or deleted files
282213f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
282313f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
282413f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
282513f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
282613f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2827a82603a8SAndrew Jeffery			$is_patch = 1;
282813f1937eSJoe Perches			$reported_maintainer_file = 1;
282913f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
283013f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
283113f1937eSJoe Perches		}
283213f1937eSJoe Perches
2833e400edb1SRob Herring# Check for adding new DT bindings not in schema format
2834e400edb1SRob Herring		if (!$in_commit_log &&
2835e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
2836e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2837e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
2838e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2839e400edb1SRob Herring		}
2840e400edb1SRob Herring
284100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
28428905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2843000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2844000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
28456c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2846de7d4f0eSAndy Whitcroft		}
2847de7d4f0eSAndy Whitcroft
2848de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2849de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2850171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2851171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2852171ae1a4SAndy Whitcroft
2853171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2854171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2855171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2856171ae1a4SAndy Whitcroft
285734d99219SJoe Perches			CHK("INVALID_UTF8",
2858000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
285900df344fSAndy Whitcroft		}
28600a920b5bSAndy Whitcroft
286115662b3eSJoe Perches# Check if it's the start of a commit log
286215662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
286315662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2864eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2865eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
286615662b3eSJoe Perches			$in_header_lines = 0;
286715662b3eSJoe Perches			$in_commit_log = 1;
2868ed43c4e5SAllen Hubbe			$has_commit_log = 1;
286915662b3eSJoe Perches		}
287015662b3eSJoe Perches
2871fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2872fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2873fa64205dSPasi Savanainen		if ($in_header_lines &&
2874fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2875fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2876fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2877fa64205dSPasi Savanainen		}
2878fa64205dSPasi Savanainen
2879fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
288015662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2881fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
288215662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
288315662b3eSJoe Perches		}
288415662b3eSJoe Perches
2885d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2886d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2887d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2888d6430f71SJoe Perches				my $file = $1;
2889d6430f71SJoe Perches
2890d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2891d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2892d6430f71SJoe Perches					#
2893d6430f71SJoe Perches				} else {
2894d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2895d6430f71SJoe Perches				}
2896d6430f71SJoe Perches			}
2897d6430f71SJoe Perches		}
2898d6430f71SJoe Perches
289966b47b4aSKees Cook# Check for various typo / spelling mistakes
290066d7a382SJoe Perches		if (defined($misspellings) &&
290166d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2902ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
290366b47b4aSKees Cook				my $typo = $1;
290466b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
290566b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
290666b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
29070675a8fbSJean Delvare				my $msg_level = \&WARN;
29080675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
29090675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
291066b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
291166b47b4aSKees Cook				    $fix) {
291266b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
291366b47b4aSKees Cook				}
291466b47b4aSKees Cook			}
291566b47b4aSKees Cook		}
291666b47b4aSKees Cook
2917a8dd86bfSMatteo Croce# check for invalid commit id
2918a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2919a8dd86bfSMatteo Croce			my $id;
2920a8dd86bfSMatteo Croce			my $description;
2921a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
2922a8dd86bfSMatteo Croce			if (!defined($id)) {
2923a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
2924a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2925a8dd86bfSMatteo Croce			}
2926a8dd86bfSMatteo Croce		}
2927a8dd86bfSMatteo Croce
292830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
292930670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
293000df344fSAndy Whitcroft
29310a920b5bSAndy Whitcroft#trailing whitespace
29329c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2933c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2934d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2935d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2936d5e616fcSJoe Perches			    $fix) {
2937194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2938d5e616fcSJoe Perches			}
2939c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2940c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29413705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
29423705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
29433705ce5bSJoe Perches			    $fix) {
2944194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
29453705ce5bSJoe Perches			}
29463705ce5bSJoe Perches
2947d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29480a920b5bSAndy Whitcroft		}
29495368df20SAndy Whitcroft
29504783f894SJosh Triplett# Check for FSF mailing addresses.
2951109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
29521bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
29533e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
29543e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
29554783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29560675a8fbSJean Delvare			my $msg_level = \&ERROR;
29570675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
29580675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
29594783f894SJosh 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)
29604783f894SJosh Triplett		}
29614783f894SJosh Triplett
29623354957aSAndi Kleen# check for Kconfig help text having a real description
29639fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
29649fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
29653354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2966678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
2967678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
2968678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
2969678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
29703354957aSAndi Kleen			my $length = 0;
29719fe287d7SAndy Whitcroft			my $cnt = $realcnt;
29729fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
29739fe287d7SAndy Whitcroft			my $f;
2974a1385803SAndy Whitcroft			my $is_start = 0;
29759fe287d7SAndy Whitcroft			my $is_end = 0;
2976a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
29779fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
29789fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
29799fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
29809fe287d7SAndy Whitcroft
29819fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
29828d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2983a1385803SAndy Whitcroft
298486adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
2985a1385803SAndy Whitcroft					$is_start = 1;
298684af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
298784af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
298884af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
298984af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
299084af7a61SUlf Magnusson					}
2991a1385803SAndy Whitcroft					$length = -1;
2992a1385803SAndy Whitcroft				}
2993a1385803SAndy Whitcroft
29949fe287d7SAndy Whitcroft				$f =~ s/^.//;
29953354957aSAndi Kleen				$f =~ s/#.*//;
29963354957aSAndi Kleen				$f =~ s/^\s+//;
29973354957aSAndi Kleen				next if ($f =~ /^$/);
2998678ae162SUlf Magnusson
2999678ae162SUlf Magnusson				# This only checks context lines in the patch
3000678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
3001678ae162SUlf Magnusson				# positives, even though some of these are
3002678ae162SUlf Magnusson				# common words in help texts
3003678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3004678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
30059fe287d7SAndy Whitcroft					$is_end = 1;
30069fe287d7SAndy Whitcroft					last;
30079fe287d7SAndy Whitcroft				}
30083354957aSAndi Kleen				$length++;
30093354957aSAndi Kleen			}
301056193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3011000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
301256193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
301356193274SVadim Bendebury			}
3014a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
30153354957aSAndi Kleen		}
30163354957aSAndi Kleen
3017628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
3018628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
3019628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
3020628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
3021628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
3022628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3023628f91a2SJoe Perches			    $fix) {
3024628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3025628f91a2SJoe Perches			}
3026628f91a2SJoe Perches		}
3027628f91a2SJoe Perches
3028327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3029327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3030327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3031327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3032327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3033327953e9SChristoph Jaeger		}
3034327953e9SChristoph Jaeger
3035c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3036c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3037c68e5878SArnaud Lacombe			my $flag = $1;
3038c68e5878SArnaud Lacombe			my $replacement = {
3039c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3040c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3041c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3042c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3043c68e5878SArnaud Lacombe			};
3044c68e5878SArnaud Lacombe
3045c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3046c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3047c68e5878SArnaud Lacombe		}
3048c68e5878SArnaud Lacombe
3049bff5da43SRob Herring# check for DT compatible documentation
30507dd05b38SFlorian Vaussard		if (defined $root &&
30517dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
30527dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
30537dd05b38SFlorian Vaussard
3054bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3055bff5da43SRob Herring
3056cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3057852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3058cc93319bSFlorian Vaussard
3059bff5da43SRob Herring			foreach my $compat (@compats) {
3060bff5da43SRob Herring				my $compat2 = $compat;
3061185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3062185d566bSRob Herring				my $compat3 = $compat;
3063185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3064185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3065bff5da43SRob Herring				if ( $? >> 8 ) {
3066bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3067bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3068bff5da43SRob Herring				}
3069bff5da43SRob Herring
30704fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
30714fbf32a6SFlorian Vaussard				my $vendor = $1;
3072852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3073bff5da43SRob Herring				if ( $? >> 8 ) {
3074bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3075cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3076bff5da43SRob Herring				}
3077bff5da43SRob Herring			}
3078bff5da43SRob Herring		}
3079bff5da43SRob Herring
30809f3a8992SRob Herring# check for using SPDX license tag at beginning of files
30819f3a8992SRob Herring		if ($realline == $checklicenseline) {
30829f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
30839f3a8992SRob Herring				$checklicenseline = 2;
30849f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
30859f3a8992SRob Herring				my $comment = "";
30869f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
30879f3a8992SRob Herring					$comment = '/*';
30889f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
30899f3a8992SRob Herring					$comment = '//';
30909f3a8992SRob Herring				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
30919f3a8992SRob Herring					$comment = '#';
30929f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
30939f3a8992SRob Herring					$comment = '..';
30949f3a8992SRob Herring				}
30959f3a8992SRob Herring
3096fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3097fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3098fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3099ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3100fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3101fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3102fdf13693SJoe Perches				}
3103fdf13693SJoe Perches
31049f3a8992SRob Herring				if ($comment !~ /^$/ &&
3105ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
31069f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
31079f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
31083b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
31093b6e8ac9SJoe Perches					my $spdx_license = $1;
31103b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
31113b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
31123b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
31133b6e8ac9SJoe Perches					}
31149f3a8992SRob Herring				}
31159f3a8992SRob Herring			}
31169f3a8992SRob Herring		}
31179f3a8992SRob Herring
31185368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3119d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
31205368df20SAndy Whitcroft
3121a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3122a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3123a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3124a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3125a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3126a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3127a8da38a9SJoe Perches		}
3128a8da38a9SJoe Perches
312947e0c88bSJoe Perches# line length limit (with some exclusions)
313047e0c88bSJoe Perches#
313147e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
313247e0c88bSJoe Perches#	logging functions like pr_info that end in a string
313347e0c88bSJoe Perches#	lines with a single string
313447e0c88bSJoe Perches#	#defines that are a single string
31352e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
313647e0c88bSJoe Perches#
313747e0c88bSJoe Perches# There are 3 different line length message types:
3138ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
313947e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
314047e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
314147e0c88bSJoe Perches#
314247e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
314347e0c88bSJoe Perches#
314447e0c88bSJoe Perches
3145b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
314647e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
314747e0c88bSJoe Perches
314847e0c88bSJoe Perches			# Check the allowed long line types first
314947e0c88bSJoe Perches
315047e0c88bSJoe Perches			# logging functions that end in a string that starts
315147e0c88bSJoe Perches			# before $max_line_length
315247e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
315347e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
315447e0c88bSJoe Perches				$msg_type = "";
315547e0c88bSJoe Perches
315647e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
315747e0c88bSJoe Perches			# #defines with only strings
315847e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
315947e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
316047e0c88bSJoe Perches				$msg_type = "";
316147e0c88bSJoe Perches
3162cc147506SJoe Perches			# More special cases
3163cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3164cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3165d560a5f8SJoe Perches				$msg_type = "";
3166d560a5f8SJoe Perches
31672e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
31682e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
31692e4bbbc5SAndreas Brauchli				$msg_type = "";
31702e4bbbc5SAndreas Brauchli
317147e0c88bSJoe Perches			# Otherwise set the alternate message types
317247e0c88bSJoe Perches
317347e0c88bSJoe Perches			# a comment starts before $max_line_length
317447e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
317547e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
317647e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
317747e0c88bSJoe Perches
317847e0c88bSJoe Perches			# a quoted string starts before $max_line_length
317947e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
318047e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
318147e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
318247e0c88bSJoe Perches			}
318347e0c88bSJoe Perches
318447e0c88bSJoe Perches			if ($msg_type ne "" &&
318547e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
318647e0c88bSJoe Perches				WARN($msg_type,
31876cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
31880a920b5bSAndy Whitcroft			}
318947e0c88bSJoe Perches		}
31900a920b5bSAndy Whitcroft
31918905a67cSAndy Whitcroft# check for adding lines without a newline.
31928905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3193000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3194000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
31958905a67cSAndy Whitcroft		}
31968905a67cSAndy Whitcroft
3197b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3198de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
31990a920b5bSAndy Whitcroft
32000a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
32010a920b5bSAndy Whitcroft# more than 8 must use tabs.
3202c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3203c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3204c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3205d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32063705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
32073705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
32083705ce5bSJoe Perches			    $fix) {
3209194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
32103705ce5bSJoe Perches			}
32110a920b5bSAndy Whitcroft		}
32120a920b5bSAndy Whitcroft
321308e44365SAlberto Panizzo# check for space before tabs.
321408e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
321508e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32163705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
32173705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
32183705ce5bSJoe Perches			    $fix) {
3219194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3220d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
3221194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3222c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
32233705ce5bSJoe Perches			}
322408e44365SAlberto Panizzo		}
322508e44365SAlberto Panizzo
32266a487211SJoe Perches# check for assignments on the start of a line
32276a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
32286a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
32296a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
32306a487211SJoe Perches		}
32316a487211SJoe Perches
3232d1fe9c09SJoe Perches# check for && or || at the start of a line
3233d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3234d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3235d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3236d1fe9c09SJoe Perches		}
3237d1fe9c09SJoe Perches
3238a91e8994SJoe Perches# check indentation starts on a tab stop
32395b57980dSJoe Perches		if ($perl_version_ok &&
3240bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3241a91e8994SJoe Perches			my $indent = length($1);
3242a91e8994SJoe Perches			if ($indent % 8) {
3243a91e8994SJoe Perches				if (WARN("TABSTOP",
3244a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3245a91e8994SJoe Perches				    $fix) {
3246a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3247a91e8994SJoe Perches				}
3248a91e8994SJoe Perches			}
3249a91e8994SJoe Perches		}
3250a91e8994SJoe Perches
3251d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
32525b57980dSJoe Perches		if ($perl_version_ok &&
3253fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3254d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3255d1fe9c09SJoe Perches			my $oldindent = $1;
3256d1fe9c09SJoe Perches			my $rest = $2;
3257d1fe9c09SJoe Perches
3258d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3259d1fe9c09SJoe Perches			if ($pos >= 0) {
3260b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3261b34a26f3SJoe Perches				my $newindent = $2;
3262d1fe9c09SJoe Perches
3263d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3264d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3265d1fe9c09SJoe Perches					" "  x ($pos % 8);
3266d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3267d1fe9c09SJoe Perches
3268d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3269d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
32703705ce5bSJoe Perches
32713705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
32723705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
32733705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3274194f66fcSJoe Perches						$fixed[$fixlinenr] =~
32753705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
32763705ce5bSJoe Perches					}
3277d1fe9c09SJoe Perches				}
3278d1fe9c09SJoe Perches			}
3279d1fe9c09SJoe Perches		}
3280d1fe9c09SJoe Perches
32816ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
32826ab3a970SJoe Perches# avoid checking a few false positives:
32836ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
32846ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
32856ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
32866ab3a970SJoe Perches#   multiline macros that define functions
32876ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
32886ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
32896ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
32903705ce5bSJoe Perches			if (CHK("SPACING",
3291f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
32923705ce5bSJoe Perches			    $fix) {
3293194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3294f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
32953705ce5bSJoe Perches			}
3296aad4f614SJoe Perches		}
3297aad4f614SJoe Perches
329886406b1cSJoe Perches# Block comment styles
329986406b1cSJoe Perches# Networking with an initial /*
330005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3301fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
330285ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
330385ad978cSJoe Perches		    $realline > 2) {
330405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
330505880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
330605880600SJoe Perches		}
330705880600SJoe Perches
330886406b1cSJoe Perches# Block comments use * on subsequent lines
330986406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
331086406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3311a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
331261135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3313a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
331486406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
331586406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3316a605e32eSJoe Perches		}
3317a605e32eSJoe Perches
331886406b1cSJoe Perches# Block comments use */ on trailing lines
331986406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3320c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3321c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3322c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
332386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
332486406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
332505880600SJoe Perches		}
332605880600SJoe Perches
332708eb9b80SJoe Perches# Block comment * alignment
332808eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3329af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3330af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3331af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
333208eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3333af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3334af207524SJoe Perches			my $oldindent;
333508eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3336af207524SJoe Perches			if (defined($1)) {
3337af207524SJoe Perches				$oldindent = expand_tabs($1);
3338af207524SJoe Perches			} else {
3339af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3340af207524SJoe Perches				$oldindent = expand_tabs($1);
3341af207524SJoe Perches			}
334208eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
334308eb9b80SJoe Perches			my $newindent = $1;
334408eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3345af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
334608eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
334708eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
334808eb9b80SJoe Perches			}
334908eb9b80SJoe Perches		}
335008eb9b80SJoe Perches
33517f619191SJoe Perches# check for missing blank lines after struct/union declarations
33527f619191SJoe Perches# with exceptions for various attributes and macros
33537f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
33547f619191SJoe Perches		    $line =~ /^\+/ &&
33557f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
33567f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
33577f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
33587f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
33597f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
33607f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
33617f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
33620bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
33637f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3364d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3365d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3366d752fcc8SJoe Perches			    $fix) {
3367f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3368d752fcc8SJoe Perches			}
33697f619191SJoe Perches		}
33707f619191SJoe Perches
3371365dd4eaSJoe Perches# check for multiple consecutive blank lines
3372365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3373365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3374365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3375d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3376d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3377d752fcc8SJoe Perches			    $fix) {
3378f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3379d752fcc8SJoe Perches			}
3380d752fcc8SJoe Perches
3381365dd4eaSJoe Perches			$last_blank_line = $linenr;
3382365dd4eaSJoe Perches		}
3383365dd4eaSJoe Perches
33843b617e3bSJoe Perches# check for missing blank lines after declarations
33853f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
33863f7bac03SJoe Perches			# actual declarations
33873f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33885a4e1fd3SJoe Perches			# function pointer declarations
33895a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
33903f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
33913f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
33923f7bac03SJoe Perches			# known declaration macros
33933f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
33943f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
33953f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
33963f7bac03SJoe Perches			# other possible extensions of declaration lines
33973f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
33983f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
33993f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
34003f7bac03SJoe Perches			# looks like a declaration
34013f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
34025a4e1fd3SJoe Perches			# function pointer declarations
34035a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34043f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34053f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34063f7bac03SJoe Perches			# known declaration macros
34073f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
34083f7bac03SJoe Perches			# start of struct or union or enum
3409328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
34103f7bac03SJoe Perches			# start or end of block or continuation of declaration
34113f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
34123f7bac03SJoe Perches			# bitfield continuation
34133f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
34143f7bac03SJoe Perches			# other possible extensions of declaration lines
34153f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
34163f7bac03SJoe Perches			# indentation of previous and current line are the same
34173f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3418d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3419d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3420d752fcc8SJoe Perches			    $fix) {
3421f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3422d752fcc8SJoe Perches			}
34233b617e3bSJoe Perches		}
34243b617e3bSJoe Perches
34255f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
34266b4c5bebSAndy Whitcroft# Exceptions:
34276b4c5bebSAndy Whitcroft#  1) within comments
34286b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
34296b4c5bebSAndy Whitcroft#  3) hanging labels
34303705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
34315f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34323705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
34333705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
34343705ce5bSJoe Perches			    $fix) {
3435194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
34363705ce5bSJoe Perches			}
34375f7ddae6SRaffaele Recalcati		}
34385f7ddae6SRaffaele Recalcati
3439b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3440b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3441b9ea10d6SAndy Whitcroft
34425751a24eSJoe Perches# check for unusual line ending [ or (
34435751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
34445751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
34455751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
34465751a24eSJoe Perches		}
34475751a24eSJoe Perches
34484dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
34494dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
34504dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
34514dbed76fSJoe Perches			$context_function = $1;
34524dbed76fSJoe Perches		}
34534dbed76fSJoe Perches
34544dbed76fSJoe Perches# check if this appears to be the end of function declaration
34554dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
34564dbed76fSJoe Perches			undef $context_function;
34574dbed76fSJoe Perches		}
34584dbed76fSJoe Perches
3459032a4c0fSJoe Perches# check indentation of any line with a bare else
3460840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3461032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3462032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3463032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3464840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3465840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3466840080a0SJoe Perches			     defined $lines[$linenr] &&
3467840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3468032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3469032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3470032a4c0fSJoe Perches			}
3471032a4c0fSJoe Perches		}
3472032a4c0fSJoe Perches
3473c00df19aSJoe Perches# check indentation of a line with a break;
3474c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3475c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3476c00df19aSJoe Perches			my $tabs = $1;
3477c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3478c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3479c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3480c00df19aSJoe Perches			}
3481c00df19aSJoe Perches		}
3482c00df19aSJoe Perches
3483c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3484cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3485000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3486000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3487c2fdda0dSAndy Whitcroft		}
348822f2a2efSAndy Whitcroft
348956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
349056e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
349156e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
349256e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
349356e77d70SJoe Perches		}
349456e77d70SJoe Perches
34959c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
34962b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
34972b474a1aSAndy Whitcroft		    $realline_next);
34983e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3499ca819864SJoe Perches		if ($linenr > $suppress_statement &&
35001b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3501170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3502f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3503171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3504171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3505171ae1a4SAndy Whitcroft
35063e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
35073e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
35083e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
35093e469cdcSAndy Whitcroft			# until we hit end of it.
35103e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
35113e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
35123e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
35133e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
35143e469cdcSAndy Whitcroft			}
3515f74bd194SAndy Whitcroft
35162b474a1aSAndy Whitcroft			# Find the real next line.
35172b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
35182b474a1aSAndy Whitcroft			if (defined $realline_next &&
35192b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
35202b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
35212b474a1aSAndy Whitcroft				$realline_next++;
35222b474a1aSAndy Whitcroft			}
35232b474a1aSAndy Whitcroft
3524171ae1a4SAndy Whitcroft			my $s = $stat;
3525171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3526cf655043SAndy Whitcroft
3527c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3528171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3529c2fdda0dSAndy Whitcroft
3530c2fdda0dSAndy Whitcroft			# Ignore functions being called
3531171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3532c2fdda0dSAndy Whitcroft
3533463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3534463f2864SAndy Whitcroft
3535c45dcabdSAndy Whitcroft			# declarations always start with types
3536d2506586SAndy 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) {
3537c45dcabdSAndy Whitcroft				my $type = $1;
3538c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3539c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3540c45dcabdSAndy Whitcroft
35416c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3542a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3543c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3544c2fdda0dSAndy Whitcroft			}
35458905a67cSAndy Whitcroft
35466c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
354765863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3548c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
35499c0ca6f9SAndy Whitcroft			}
35508905a67cSAndy Whitcroft
35518905a67cSAndy Whitcroft			# Check for any sort of function declaration.
35528905a67cSAndy Whitcroft			# int foo(something bar, other baz);
35538905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3554171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
35558905a67cSAndy Whitcroft				my ($name_len) = length($1);
35568905a67cSAndy Whitcroft
3557cf655043SAndy Whitcroft				my $ctx = $s;
3558773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
35598905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3560cf655043SAndy Whitcroft
35618905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3562c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
35638905a67cSAndy Whitcroft
3564c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
35658905a67cSAndy Whitcroft					}
35668905a67cSAndy Whitcroft				}
35678905a67cSAndy Whitcroft			}
35688905a67cSAndy Whitcroft
35699c0ca6f9SAndy Whitcroft		}
35709c0ca6f9SAndy Whitcroft
357100df344fSAndy Whitcroft#
357200df344fSAndy Whitcroft# Checks which may be anchored in the context.
357300df344fSAndy Whitcroft#
357400df344fSAndy Whitcroft
357500df344fSAndy Whitcroft# Check for switch () and associated case and default
357600df344fSAndy Whitcroft# statements should be at the same indent.
357700df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
357800df344fSAndy Whitcroft			my $err = '';
357900df344fSAndy Whitcroft			my $sep = '';
358000df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
358100df344fSAndy Whitcroft			shift(@ctx);
358200df344fSAndy Whitcroft			for my $ctx (@ctx) {
358300df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
358400df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
358500df344fSAndy Whitcroft							$indent != $cindent) {
358600df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
358700df344fSAndy Whitcroft					$sep = '';
358800df344fSAndy Whitcroft				} else {
358900df344fSAndy Whitcroft					$sep = "[...]\n";
359000df344fSAndy Whitcroft				}
359100df344fSAndy Whitcroft			}
359200df344fSAndy Whitcroft			if ($err ne '') {
3593000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3594000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3595de7d4f0eSAndy Whitcroft			}
3596de7d4f0eSAndy Whitcroft		}
3597de7d4f0eSAndy Whitcroft
3598de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3599de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
36000fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3601773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3602773647a0SAndy Whitcroft
36039c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
36048eef05ddSJoe Perches
36058eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
36068eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
36078eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
36088eef05ddSJoe Perches			}
36098eef05ddSJoe Perches
3610de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3611de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3612de7d4f0eSAndy Whitcroft
3613548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3614548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3615de7d4f0eSAndy Whitcroft
3616548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3617548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3618548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3619548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3620548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3621773647a0SAndy Whitcroft				$ctx_ln++;
3622773647a0SAndy Whitcroft			}
3623548596d5SAndy Whitcroft
362453210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
362553210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3626773647a0SAndy Whitcroft
3627773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3628000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3629000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
363001464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
363100df344fSAndy Whitcroft			}
3632773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3633773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3634773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3635773647a0SAndy Whitcroft			{
36369c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
36379c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3638000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3639000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
364001464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
36419c0ca6f9SAndy Whitcroft				}
36429c0ca6f9SAndy Whitcroft			}
364300df344fSAndy Whitcroft		}
364400df344fSAndy Whitcroft
36454d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3646f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
36473e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
36483e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
36493e469cdcSAndy Whitcroft					if (!defined $stat);
36504d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
36514d001e4dSAndy Whitcroft
36524d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
36534d001e4dSAndy Whitcroft
36549f5af480SJoe Perches			# remove inline comments
36559f5af480SJoe Perches			$s =~ s/$;/ /g;
36569f5af480SJoe Perches			$c =~ s/$;/ /g;
36574d001e4dSAndy Whitcroft
36584d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
36596f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
36606f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
36614d001e4dSAndy Whitcroft
36629f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
36639f5af480SJoe Perches			# none on the first line, and are going to readd them
36649f5af480SJoe Perches			# where necessary.
36659f5af480SJoe Perches			$s =~ s/\n./\n/gs;
36669f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
36679f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
36689f5af480SJoe Perches			}
36699f5af480SJoe Perches
36704d001e4dSAndy Whitcroft			# We want to check the first line inside the block
36714d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
36724d001e4dSAndy Whitcroft			#  1) any blank line termination
36734d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
36744d001e4dSAndy Whitcroft			#  3) any do (...) {
36754d001e4dSAndy Whitcroft			my $continuation = 0;
36764d001e4dSAndy Whitcroft			my $check = 0;
36774d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
36784d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
36794d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
36804d001e4dSAndy Whitcroft				$continuation = 1;
36814d001e4dSAndy Whitcroft			}
36829bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
36834d001e4dSAndy Whitcroft				$check = 1;
36844d001e4dSAndy Whitcroft				$cond_lines++;
36854d001e4dSAndy Whitcroft			}
36864d001e4dSAndy Whitcroft
36874d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
36884d001e4dSAndy Whitcroft			# preprocessor statement.
36894d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
36904d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
36914d001e4dSAndy Whitcroft				$check = 0;
36924d001e4dSAndy Whitcroft			}
36934d001e4dSAndy Whitcroft
36949bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3695740504c6SAndy Whitcroft			$continuation = 0;
36969bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
36979bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
36984d001e4dSAndy Whitcroft
3699f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3700f16fa28fSAndy Whitcroft				# is not linear.
3701f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3702f16fa28fSAndy Whitcroft					$check = 0;
3703f16fa28fSAndy Whitcroft				}
3704f16fa28fSAndy Whitcroft
37059bd49efeSAndy Whitcroft				# Ignore:
37069bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
37079bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
37089bd49efeSAndy Whitcroft				#  3) labels.
3709740504c6SAndy Whitcroft				if ($continuation ||
3710740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
37119bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
37129bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3713740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
371430dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
37159bd49efeSAndy Whitcroft						$cond_lines++;
37169bd49efeSAndy Whitcroft					}
37174d001e4dSAndy Whitcroft				}
371830dad6ebSAndy Whitcroft			}
37194d001e4dSAndy Whitcroft
37204d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
37214d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
37224d001e4dSAndy Whitcroft
37234d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
37244d001e4dSAndy Whitcroft			# this is not this patch's fault.
37254d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
37264d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
37274d001e4dSAndy Whitcroft				$check = 0;
37284d001e4dSAndy Whitcroft			}
37294d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
37304d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
37314d001e4dSAndy Whitcroft			}
37324d001e4dSAndy Whitcroft
37339bd49efeSAndy 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";
37344d001e4dSAndy Whitcroft
37359f5af480SJoe Perches			if ($check && $s ne '' &&
37369f5af480SJoe Perches			    (($sindent % 8) != 0 ||
37379f5af480SJoe Perches			     ($sindent < $indent) ||
3738f6950a73SJoe Perches			     ($sindent == $indent &&
3739f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
37409f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3741000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3742000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
37434d001e4dSAndy Whitcroft			}
37444d001e4dSAndy Whitcroft		}
37454d001e4dSAndy Whitcroft
37466c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
37476c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
37481f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
37491f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
37506c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3751c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3752c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3753cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3754cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
37551f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3756c2fdda0dSAndy Whitcroft		}
37576c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
37586c72ffaaSAndy Whitcroft
375900df344fSAndy Whitcroft#ignore lines not being added
37603705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
376100df344fSAndy Whitcroft
376211ca40a0SJoe Perches# check for dereferences that span multiple lines
376311ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
376411ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
376511ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
376611ca40a0SJoe Perches			my $ref = $1;
376711ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
376811ca40a0SJoe Perches			$ref .= $1;
376911ca40a0SJoe Perches			$ref =~ s/\s//g;
377011ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
377111ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
377211ca40a0SJoe Perches		}
377311ca40a0SJoe Perches
3774a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3775c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3776a1ce18e4SJoe Perches			my $type = $1;
3777a1ce18e4SJoe Perches			my $var = $2;
3778207a8e84SJoe Perches			$var = "" if (!defined $var);
3779207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3780a1ce18e4SJoe Perches				my $sign = $1;
3781a1ce18e4SJoe Perches				my $pointer = $2;
3782a1ce18e4SJoe Perches
3783a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3784a1ce18e4SJoe Perches
3785a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3786a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3787a1ce18e4SJoe Perches				    $fix) {
3788a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3789207a8e84SJoe Perches					my $comp_pointer = $pointer;
3790207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3791207a8e84SJoe Perches					$decl .= $comp_pointer;
3792207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3793207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3794a1ce18e4SJoe Perches				}
3795a1ce18e4SJoe Perches			}
3796a1ce18e4SJoe Perches		}
3797a1ce18e4SJoe Perches
3798653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
37997429c690SAndy Whitcroft		if ($dbg_type) {
38007429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3801000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3802000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
38037429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3804000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3805000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
38067429c690SAndy Whitcroft			}
3807653d4876SAndy Whitcroft			next;
3808653d4876SAndy Whitcroft		}
3809a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3810a1ef277eSAndy Whitcroft		if ($dbg_attr) {
38119360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3812000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3813000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
38149360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3815000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3816000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3817a1ef277eSAndy Whitcroft			}
3818a1ef277eSAndy Whitcroft			next;
3819a1ef277eSAndy Whitcroft		}
3820653d4876SAndy Whitcroft
3821f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
382299423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
382399423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3824d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3825d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3826f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3827f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3828f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3829d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3830d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3831f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3832d752fcc8SJoe Perches				$fixedline = $line;
38338d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3834f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3835d752fcc8SJoe Perches			}
3836f0a594c1SAndy Whitcroft		}
3837f0a594c1SAndy Whitcroft
383800df344fSAndy Whitcroft#
383900df344fSAndy Whitcroft# Checks which are anchored on the added line.
384000df344fSAndy Whitcroft#
384100df344fSAndy Whitcroft
3842653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3843c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3844653d4876SAndy Whitcroft			my $path = $1;
3845653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3846000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3847495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3848495e9d84SJoe Perches			}
3849495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3850495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3851495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3852653d4876SAndy Whitcroft			}
3853653d4876SAndy Whitcroft		}
3854653d4876SAndy Whitcroft
385500df344fSAndy Whitcroft# no C99 // comments
385600df344fSAndy Whitcroft		if ($line =~ m{//}) {
38573705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
38583705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
38593705ce5bSJoe Perches			    $fix) {
3860194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
38613705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
38623705ce5bSJoe Perches					my $comment = trim($1);
3863194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
38643705ce5bSJoe Perches				}
38653705ce5bSJoe Perches			}
386600df344fSAndy Whitcroft		}
386700df344fSAndy Whitcroft		# Remove C99 comments.
38680a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
38696c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
38700a920b5bSAndy Whitcroft
38712b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
38722b474a1aSAndy Whitcroft# the whole statement.
38732b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
38742b474a1aSAndy Whitcroft		if (defined $realline_next &&
38752b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
38762b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
38772b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
38782b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
38793cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
38803cbf62dfSAndy Whitcroft			# a prefix:
38813cbf62dfSAndy Whitcroft			#   XXX(foo);
38823cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3883653d4876SAndy Whitcroft			my $name = $1;
388487a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
38853cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
38863cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
38873cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38883cbf62dfSAndy Whitcroft
38893cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
38902b474a1aSAndy Whitcroft				\n.}\s*$|
389148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
389248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
389348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
38942b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
38952b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
389648012058SAndy Whitcroft			    )/x) {
38972b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
38982b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
38992b474a1aSAndy Whitcroft			} else {
39002b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
39010a920b5bSAndy Whitcroft			}
39020a920b5bSAndy Whitcroft		}
39032b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
39042b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
39052b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39062b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39072b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
39082b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
39092b474a1aSAndy Whitcroft		}
39102b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
39112b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3912000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3913000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
39142b474a1aSAndy Whitcroft		}
39150a920b5bSAndy Whitcroft
39165150bda4SJoe Eloff# check for global initialisers.
39176d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3918d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
39196d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3920d5e616fcSJoe Perches			    $fix) {
39216d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3922d5e616fcSJoe Perches			}
3923f0a594c1SAndy Whitcroft		}
39240a920b5bSAndy Whitcroft# check for static initialisers.
39256d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3926d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
39276d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3928d5e616fcSJoe Perches				      $herecurr) &&
3929d5e616fcSJoe Perches			    $fix) {
39306d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3931d5e616fcSJoe Perches			}
39320a920b5bSAndy Whitcroft		}
39330a920b5bSAndy Whitcroft
39341813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
39351813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
39361813087dSJoe Perches			my $tmp = trim($1);
39371813087dSJoe Perches			WARN("MISORDERED_TYPE",
39381813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
39391813087dSJoe Perches		}
39401813087dSJoe Perches
3941809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3942809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3943809e082eSJoe Perches			my $type = trim($1);
3944809e082eSJoe Perches			next if ($type !~ /\bint\b/);
3945809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3946809e082eSJoe Perches			my $new_type = $type;
3947809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
3948809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3949809e082eSJoe Perches			$new_type =~ s/^const\s+//;
3950809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3951809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
3952809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
3953809e082eSJoe Perches			$new_type = trim($new_type);
3954809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
3955809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3956809e082eSJoe Perches			    $fix) {
3957809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3958809e082eSJoe Perches			}
3959809e082eSJoe Perches		}
3960809e082eSJoe Perches
3961cb710ecaSJoe Perches# check for static const char * arrays.
3962cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3963000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3964000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3965cb710ecaSJoe Perches				$herecurr);
3966cb710ecaSJoe Perches		}
3967cb710ecaSJoe Perches
396877b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
396977b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
397077b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
397177b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
397277b8c0a8SJoe Perches			    $fix) {
397377b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
397477b8c0a8SJoe Perches			}
397577b8c0a8SJoe Perches		}
397677b8c0a8SJoe Perches
3977cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3978cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3979000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3980000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3981cb710ecaSJoe Perches				$herecurr);
3982cb710ecaSJoe Perches		}
3983cb710ecaSJoe Perches
3984ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3985ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3986ab7e23f3SJoe Perches			my $found = $1;
3987ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3988ab7e23f3SJoe Perches				WARN("CONST_CONST",
3989ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3990ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3991ab7e23f3SJoe Perches				WARN("CONST_CONST",
3992ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3993ab7e23f3SJoe Perches			}
3994ab7e23f3SJoe Perches		}
3995ab7e23f3SJoe Perches
39969b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
39979b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
39989b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
39999b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
40009b0fa60dSJoe Perches				$herecurr);
40019b0fa60dSJoe Perches               }
40029b0fa60dSJoe Perches
4003b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4004b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4005b598b670SJoe Perches			my $array = $1;
4006b598b670SJoe 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*\))@) {
4007b598b670SJoe Perches				my $array_div = $1;
4008b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4009b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4010b598b670SJoe Perches				    $fix) {
4011b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4012b598b670SJoe Perches				}
4013b598b670SJoe Perches			}
4014b598b670SJoe Perches		}
4015b598b670SJoe Perches
4016b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
4017b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4018b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4019b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4020b36190c5SJoe Perches			    $fix) {
4021194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4022b36190c5SJoe Perches			}
4023b36190c5SJoe Perches		}
4024b36190c5SJoe Perches
4025653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4026653d4876SAndy Whitcroft# make sense.
4027653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
40288054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4029c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
40308ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
403146d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4032000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4033000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
40340a920b5bSAndy Whitcroft		}
40350a920b5bSAndy Whitcroft
40360a920b5bSAndy Whitcroft# * goes on variable not on type
403765863862SAndy Whitcroft		# (char*[ const])
4038bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4039bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
40403705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4041d8aaf121SAndy Whitcroft
404265863862SAndy Whitcroft			# Should start with a space.
404365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
404465863862SAndy Whitcroft			# Should not end with a space.
404565863862SAndy Whitcroft			$to =~ s/\s+$//;
404665863862SAndy Whitcroft			# '*'s should not have spaces between.
4047f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
404865863862SAndy Whitcroft			}
4049d8aaf121SAndy Whitcroft
40503705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
405165863862SAndy Whitcroft			if ($from ne $to) {
40523705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40533705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
40543705ce5bSJoe Perches				    $fix) {
40553705ce5bSJoe Perches					my $sub_from = $ident;
40563705ce5bSJoe Perches					my $sub_to = $ident;
40573705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4058194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40593705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40603705ce5bSJoe Perches				}
406165863862SAndy Whitcroft			}
4062bfcb2cc7SAndy Whitcroft		}
4063bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4064bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
40653705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4066d8aaf121SAndy Whitcroft
406765863862SAndy Whitcroft			# Should start with a space.
406865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
406965863862SAndy Whitcroft			# Should not end with a space.
407065863862SAndy Whitcroft			$to =~ s/\s+$//;
407165863862SAndy Whitcroft			# '*'s should not have spaces between.
4072f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
407365863862SAndy Whitcroft			}
407465863862SAndy Whitcroft			# Modifiers should have spaces.
407565863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
407665863862SAndy Whitcroft
40773705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4078667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
40793705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40803705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
40813705ce5bSJoe Perches				    $fix) {
40823705ce5bSJoe Perches
40833705ce5bSJoe Perches					my $sub_from = $match;
40843705ce5bSJoe Perches					my $sub_to = $match;
40853705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4086194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40873705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40883705ce5bSJoe Perches				}
408965863862SAndy Whitcroft			}
40900a920b5bSAndy Whitcroft		}
40910a920b5bSAndy Whitcroft
40929d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
40939d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
40940675a8fbSJean Delvare			my $msg_level = \&WARN;
40950675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
40960675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
40979d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
40989d3e3c70SJoe Perches		}
40990a920b5bSAndy Whitcroft
41009d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
41018905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4102000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4103000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
41048905a67cSAndy Whitcroft		}
41058905a67cSAndy Whitcroft
410617441227SJoe Perches# check for uses of printk_ratelimit
410717441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4108000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4109000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
411017441227SJoe Perches		}
411117441227SJoe Perches
4112eeef5733SJoe Perches# printk should use KERN_* levels
4113eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4114000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4115eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
411600df344fSAndy Whitcroft		}
41170a920b5bSAndy Whitcroft
4118243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4119243f3803SJoe Perches			my $orig = $1;
4120243f3803SJoe Perches			my $level = lc($orig);
4121243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
41228f26b837SJoe Perches			my $level2 = $level;
41238f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4124243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4125daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4126243f3803SJoe Perches		}
4127243f3803SJoe Perches
4128243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
4129d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
4130d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4131d5e616fcSJoe Perches			    $fix) {
4132194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4133d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
4134d5e616fcSJoe Perches			}
4135243f3803SJoe Perches		}
4136243f3803SJoe Perches
4137dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4138dc139313SJoe Perches			my $orig = $1;
4139dc139313SJoe Perches			my $level = lc($orig);
4140dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4141dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4142dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4143dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4144dc139313SJoe Perches		}
4145dc139313SJoe Perches
414691c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
414791c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
414891c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
414991c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
415091c9afafSAndy Lutomirski			WARN("ENOSYS",
415191c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
415291c9afafSAndy Lutomirski		}
415391c9afafSAndy Lutomirski
4154653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4155653d4876SAndy Whitcroft# or if closed on same line
41565b57980dSJoe Perches		if ($perl_version_ok &&
41572d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
41582d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
41592d453e3bSJoe Perches		    $sline !~ /}/) {
41608d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41612d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
41628d182478SJoe Perches			    $fix) {
41638d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41648d182478SJoe Perches				my $fixed_line = $rawline;
41658d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
41668d182478SJoe Perches				my $line1 = $1;
41678d182478SJoe Perches				my $line2 = $2;
41688d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
41698d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
41708d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
41718d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
41728d182478SJoe Perches				}
41738d182478SJoe Perches			}
41740a920b5bSAndy Whitcroft		}
4175653d4876SAndy Whitcroft
41768905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
41778905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
41788905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
41798d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41808d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
41818d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41828d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
41838d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41848d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
41858d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
41868d182478SJoe Perches				$fixedline = $rawline;
41878d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
41888d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
41898d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41908d182478SJoe Perches				}
41918d182478SJoe Perches			}
41928905a67cSAndy Whitcroft		}
41938905a67cSAndy Whitcroft
41940c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
41953705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
41963705ce5bSJoe Perches			if (WARN("SPACING",
41973705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
41983705ce5bSJoe Perches			    $fix) {
4199194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42003705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
42013705ce5bSJoe Perches			}
42020c73b4ebSAndy Whitcroft		}
42030c73b4ebSAndy Whitcroft
420431070b5dSJoe Perches# Function pointer declarations
420531070b5dSJoe Perches# check spacing between type, funcptr, and args
420631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
420791f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
420831070b5dSJoe Perches			my $declare = $1;
420931070b5dSJoe Perches			my $pre_pointer_space = $2;
421031070b5dSJoe Perches			my $post_pointer_space = $3;
421131070b5dSJoe Perches			my $funcname = $4;
421231070b5dSJoe Perches			my $post_funcname_space = $5;
421331070b5dSJoe Perches			my $pre_args_space = $6;
421431070b5dSJoe Perches
421591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
421691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
421791f72e9cSJoe Perches# don't need a space so don't warn for those.
421891f72e9cSJoe Perches			my $post_declare_space = "";
421991f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
422091f72e9cSJoe Perches				$post_declare_space = $1;
422191f72e9cSJoe Perches				$declare = rtrim($declare);
422291f72e9cSJoe Perches			}
422391f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
422431070b5dSJoe Perches				WARN("SPACING",
422531070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
422691f72e9cSJoe Perches				$post_declare_space = " ";
422731070b5dSJoe Perches			}
422831070b5dSJoe Perches
422931070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
423091f72e9cSJoe Perches# This test is not currently implemented because these declarations are
423191f72e9cSJoe Perches# equivalent to
423291f72e9cSJoe Perches#	int  foo(int bar, ...)
423391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
423491f72e9cSJoe Perches#
423591f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
423691f72e9cSJoe Perches#				WARN("SPACING",
423791f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
423891f72e9cSJoe Perches#			}
423931070b5dSJoe Perches
424031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
424131070b5dSJoe Perches			if (defined $pre_pointer_space &&
424231070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
424331070b5dSJoe Perches				WARN("SPACING",
424431070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
424531070b5dSJoe Perches			}
424631070b5dSJoe Perches
424731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
424831070b5dSJoe Perches			if (defined $post_pointer_space &&
424931070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
425031070b5dSJoe Perches				WARN("SPACING",
425131070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
425231070b5dSJoe Perches			}
425331070b5dSJoe Perches
425431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
425531070b5dSJoe Perches			if (defined $post_funcname_space &&
425631070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
425731070b5dSJoe Perches				WARN("SPACING",
425831070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
425931070b5dSJoe Perches			}
426031070b5dSJoe Perches
426131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
426231070b5dSJoe Perches			if (defined $pre_args_space &&
426331070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
426431070b5dSJoe Perches				WARN("SPACING",
426531070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
426631070b5dSJoe Perches			}
426731070b5dSJoe Perches
426831070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4269194f66fcSJoe Perches				$fixed[$fixlinenr] =~
427091f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
427131070b5dSJoe Perches			}
427231070b5dSJoe Perches		}
427331070b5dSJoe Perches
42748d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
42758d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4276fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4277fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
42788d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
42798d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
42808d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4281fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
428238dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
42833705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
42843705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
42853705ce5bSJoe Perches				    $fix) {
4286194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
42873705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
42883705ce5bSJoe Perches				}
42898d31cfceSAndy Whitcroft			}
42908d31cfceSAndy Whitcroft		}
42918d31cfceSAndy Whitcroft
4292f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
42936c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4294c2fdda0dSAndy Whitcroft			my $name = $1;
4295773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4296773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4297c2fdda0dSAndy Whitcroft
4298c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4299773647a0SAndy Whitcroft			if ($name =~ /^(?:
4300773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4301773647a0SAndy Whitcroft				volatile|__volatile__|
4302773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4303773647a0SAndy Whitcroft				asm|__asm__)$/x)
4304773647a0SAndy Whitcroft			{
4305c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4306c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4307c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4308c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4309773647a0SAndy Whitcroft
4310773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4311c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4312c2fdda0dSAndy Whitcroft
4313c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4314c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4315773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4316c2fdda0dSAndy Whitcroft
4317c2fdda0dSAndy Whitcroft			} else {
43183705ce5bSJoe Perches				if (WARN("SPACING",
43193705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
43203705ce5bSJoe Perches					     $fix) {
4321194f66fcSJoe Perches					$fixed[$fixlinenr] =~
43223705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
43233705ce5bSJoe Perches				}
4324f0a594c1SAndy Whitcroft			}
43256c72ffaaSAndy Whitcroft		}
43269a4cad4eSEric Nelson
4327653d4876SAndy Whitcroft# Check operator spacing.
43280a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
43293705ce5bSJoe Perches			my $fixed_line = "";
43303705ce5bSJoe Perches			my $line_fixed = 0;
43313705ce5bSJoe Perches
43329c0ca6f9SAndy Whitcroft			my $ops = qr{
43339c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
43349c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
43359c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
43361f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
433784731623SJoe Perches				\?:|\?|:
43389c0ca6f9SAndy Whitcroft			}x;
4339cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
43403705ce5bSJoe Perches
43413705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
43423705ce5bSJoe Perches##			foreach my $el (@elements) {
43433705ce5bSJoe Perches##				print("el: <$el>\n");
43443705ce5bSJoe Perches##			}
43453705ce5bSJoe Perches
43463705ce5bSJoe Perches			my @fix_elements = ();
434700df344fSAndy Whitcroft			my $off = 0;
43486c72ffaaSAndy Whitcroft
43493705ce5bSJoe Perches			foreach my $el (@elements) {
43503705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
43513705ce5bSJoe Perches				$off += length($el);
43523705ce5bSJoe Perches			}
43533705ce5bSJoe Perches
43543705ce5bSJoe Perches			$off = 0;
43553705ce5bSJoe Perches
43566c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4357b34c648bSJoe Perches			my $last_after = -1;
43586c72ffaaSAndy Whitcroft
43590a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
43603705ce5bSJoe Perches
43613705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
43623705ce5bSJoe Perches
43633705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
43643705ce5bSJoe Perches
43654a0df2efSAndy Whitcroft				$off += length($elements[$n]);
43664a0df2efSAndy Whitcroft
436725985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4368773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4369773647a0SAndy Whitcroft				my $cc = '';
4370773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4371773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4372773647a0SAndy Whitcroft				}
4373773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4374773647a0SAndy Whitcroft
43754a0df2efSAndy Whitcroft				my $a = '';
43764a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
43774a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4378cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
43794a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
43804a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4381773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
43824a0df2efSAndy Whitcroft
43830a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
43844a0df2efSAndy Whitcroft
43854a0df2efSAndy Whitcroft				my $c = '';
43860a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
43874a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
43884a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4389cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
43904a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
43914a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
43928b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
43934a0df2efSAndy Whitcroft				} else {
43944a0df2efSAndy Whitcroft					$c = 'E';
43950a920b5bSAndy Whitcroft				}
43960a920b5bSAndy Whitcroft
43974a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
43984a0df2efSAndy Whitcroft
43994a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
44004a0df2efSAndy Whitcroft
44016c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4402de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
44030a920b5bSAndy Whitcroft
440474048ed8SAndy Whitcroft				# Pull out the value of this operator.
44056c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
44060a920b5bSAndy Whitcroft
44071f65f947SAndy Whitcroft				# Get the full operator variant.
44081f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
44091f65f947SAndy Whitcroft
441013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
441113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4412d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
441313214adfSAndy Whitcroft
4414cf655043SAndy Whitcroft#				# Ignore comments
4415cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
441613214adfSAndy Whitcroft
4417d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
441813214adfSAndy Whitcroft				} elsif ($op eq ';') {
4419cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4420cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
44213705ce5bSJoe Perches						if (ERROR("SPACING",
44223705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4423b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44243705ce5bSJoe Perches							$line_fixed = 1;
44253705ce5bSJoe Perches						}
4426d8aaf121SAndy Whitcroft					}
4427d8aaf121SAndy Whitcroft
4428d8aaf121SAndy Whitcroft				# // is a comment
4429d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
44300a920b5bSAndy Whitcroft
4431b00e4814SJoe Perches				#   :   when part of a bitfield
4432b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4433b00e4814SJoe Perches					# skip the bitfield test for now
4434b00e4814SJoe Perches
44351f65f947SAndy Whitcroft				# No spaces for:
44361f65f947SAndy Whitcroft				#   ->
4437b00e4814SJoe Perches				} elsif ($op eq '->') {
44384a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
44393705ce5bSJoe Perches						if (ERROR("SPACING",
44403705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4441b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44423705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44433705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44443705ce5bSJoe Perches							}
4445b34c648bSJoe Perches							$line_fixed = 1;
44463705ce5bSJoe Perches						}
44470a920b5bSAndy Whitcroft					}
44480a920b5bSAndy Whitcroft
44492381097bSJoe Perches				# , must not have a space before and must have a space on the right.
44500a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
44512381097bSJoe Perches					my $rtrim_before = 0;
44522381097bSJoe Perches					my $space_after = 0;
44532381097bSJoe Perches					if ($ctx =~ /Wx./) {
44542381097bSJoe Perches						if (ERROR("SPACING",
44552381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
44562381097bSJoe Perches							$line_fixed = 1;
44572381097bSJoe Perches							$rtrim_before = 1;
44582381097bSJoe Perches						}
44592381097bSJoe Perches					}
4460cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
44613705ce5bSJoe Perches						if (ERROR("SPACING",
44623705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
44633705ce5bSJoe Perches							$line_fixed = 1;
4464b34c648bSJoe Perches							$last_after = $n;
44652381097bSJoe Perches							$space_after = 1;
44662381097bSJoe Perches						}
44672381097bSJoe Perches					}
44682381097bSJoe Perches					if ($rtrim_before || $space_after) {
44692381097bSJoe Perches						if ($rtrim_before) {
44702381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44712381097bSJoe Perches						} else {
44722381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
44732381097bSJoe Perches						}
44742381097bSJoe Perches						if ($space_after) {
44752381097bSJoe Perches							$good .= " ";
44763705ce5bSJoe Perches						}
44770a920b5bSAndy Whitcroft					}
44780a920b5bSAndy Whitcroft
44799c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
448074048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
44819c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
44829c0ca6f9SAndy Whitcroft
44839c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
44849c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
44859c0ca6f9SAndy Whitcroft				# unary operator, or a cast
44869c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
448774048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
44880d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4489cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
44903705ce5bSJoe Perches						if (ERROR("SPACING",
44913705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4492b34c648bSJoe Perches							if ($n != $last_after + 2) {
4493b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
44943705ce5bSJoe Perches								$line_fixed = 1;
44953705ce5bSJoe Perches							}
44960a920b5bSAndy Whitcroft						}
4497b34c648bSJoe Perches					}
4498a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4499171ae1a4SAndy Whitcroft						# A unary '*' may be const
4500171ae1a4SAndy Whitcroft
4501171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
45023705ce5bSJoe Perches						if (ERROR("SPACING",
45033705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4504b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
45053705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45063705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
45073705ce5bSJoe Perches							}
4508b34c648bSJoe Perches							$line_fixed = 1;
45093705ce5bSJoe Perches						}
45100a920b5bSAndy Whitcroft					}
45110a920b5bSAndy Whitcroft
45120a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
45130a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4514773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
45153705ce5bSJoe Perches						if (ERROR("SPACING",
45163705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4517b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
45183705ce5bSJoe Perches							$line_fixed = 1;
45193705ce5bSJoe Perches						}
45200a920b5bSAndy Whitcroft					}
4521773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4522773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
45233705ce5bSJoe Perches						if (ERROR("SPACING",
45243705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4525b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45263705ce5bSJoe Perches							$line_fixed = 1;
45273705ce5bSJoe Perches						}
4528653d4876SAndy Whitcroft					}
4529773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
45303705ce5bSJoe Perches						if (ERROR("SPACING",
45313705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4532b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45333705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45343705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4535773647a0SAndy Whitcroft							}
4536b34c648bSJoe Perches							$line_fixed = 1;
45373705ce5bSJoe Perches						}
45383705ce5bSJoe Perches					}
45390a920b5bSAndy Whitcroft
45400a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
45419c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
45429c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
45439c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4544c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4545c2fdda0dSAndy Whitcroft					 $op eq '%')
45460a920b5bSAndy Whitcroft				{
4547d2e025f3SJoe Perches					if ($check) {
4548d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4549d2e025f3SJoe Perches							if (CHK("SPACING",
4550d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4551d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4552d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4553d2e025f3SJoe Perches								$line_fixed = 1;
4554d2e025f3SJoe Perches							}
4555d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4556d2e025f3SJoe Perches							if (CHK("SPACING",
4557d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4558d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4559d2e025f3SJoe Perches								$line_fixed = 1;
4560d2e025f3SJoe Perches							}
4561d2e025f3SJoe Perches						}
4562d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
45633705ce5bSJoe Perches						if (ERROR("SPACING",
45643705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4565b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4566b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4567b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4568b34c648bSJoe Perches							}
45693705ce5bSJoe Perches							$line_fixed = 1;
45703705ce5bSJoe Perches						}
45710a920b5bSAndy Whitcroft					}
45720a920b5bSAndy Whitcroft
45731f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
45741f65f947SAndy Whitcroft				# terminating a case value or a label.
45751f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
45761f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
45773705ce5bSJoe Perches						if (ERROR("SPACING",
45783705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4579b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45803705ce5bSJoe Perches							$line_fixed = 1;
45813705ce5bSJoe Perches						}
45821f65f947SAndy Whitcroft					}
45831f65f947SAndy Whitcroft
45840a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4585cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
45861f65f947SAndy Whitcroft					my $ok = 0;
45871f65f947SAndy Whitcroft
458822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
45891f65f947SAndy Whitcroft					if (($op eq '<' &&
45901f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
45911f65f947SAndy Whitcroft					    ($op eq '>' &&
45921f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
45931f65f947SAndy Whitcroft					{
45941f65f947SAndy Whitcroft					    	$ok = 1;
45951f65f947SAndy Whitcroft					}
45961f65f947SAndy Whitcroft
4597e0df7e1fSJoe Perches					# for asm volatile statements
4598e0df7e1fSJoe Perches					# ignore a colon with another
4599e0df7e1fSJoe Perches					# colon immediately before or after
4600e0df7e1fSJoe Perches					if (($op eq ':') &&
4601e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4602e0df7e1fSJoe Perches						$ok = 1;
4603e0df7e1fSJoe Perches					}
4604e0df7e1fSJoe Perches
460584731623SJoe Perches					# messages are ERROR, but ?: are CHK
46061f65f947SAndy Whitcroft					if ($ok == 0) {
46070675a8fbSJean Delvare						my $msg_level = \&ERROR;
46080675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
460984731623SJoe Perches
46100675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
46113705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4612b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4613b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4614b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4615b34c648bSJoe Perches							}
46163705ce5bSJoe Perches							$line_fixed = 1;
46173705ce5bSJoe Perches						}
46180a920b5bSAndy Whitcroft					}
461922f2a2efSAndy Whitcroft				}
46204a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
46213705ce5bSJoe Perches
46223705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
46233705ce5bSJoe Perches
46243705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
46250a920b5bSAndy Whitcroft			}
46263705ce5bSJoe Perches
46273705ce5bSJoe Perches			if (($#elements % 2) == 0) {
46283705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
46293705ce5bSJoe Perches			}
46303705ce5bSJoe Perches
4631194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4632194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
46333705ce5bSJoe Perches			}
46343705ce5bSJoe Perches
46353705ce5bSJoe Perches
46360a920b5bSAndy Whitcroft		}
46370a920b5bSAndy Whitcroft
4638786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4639d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4640786b6326SJoe Perches			if (WARN("SPACING",
4641786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4642786b6326SJoe Perches			    $fix) {
4643194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4644786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4645786b6326SJoe Perches			}
4646786b6326SJoe Perches		}
4647786b6326SJoe Perches
4648f0a594c1SAndy Whitcroft# check for multiple assignments
4649f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4650000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4651000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4652f0a594c1SAndy Whitcroft		}
4653f0a594c1SAndy Whitcroft
465422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
465522f2a2efSAndy Whitcroft## # continuation.
465622f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
465722f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
465822f2a2efSAndy Whitcroft##
465922f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
466022f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
466122f2a2efSAndy Whitcroft## 			my $ln = $line;
466222f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
466322f2a2efSAndy Whitcroft## 			}
466422f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4665000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4666000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
466722f2a2efSAndy Whitcroft## 			}
466822f2a2efSAndy Whitcroft## 		}
4669f0a594c1SAndy Whitcroft
46700a920b5bSAndy Whitcroft#need space before brace following if, while, etc
46716b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
46726ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
46733705ce5bSJoe Perches			if (ERROR("SPACING",
46743705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
46753705ce5bSJoe Perches			    $fix) {
46766ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
46773705ce5bSJoe Perches			}
4678de7d4f0eSAndy Whitcroft		}
4679de7d4f0eSAndy Whitcroft
4680c4a62ef9SJoe Perches## # check for blank lines before declarations
4681c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4682c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4683c4a62ef9SJoe Perches##			WARN("SPACING",
4684c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4685c4a62ef9SJoe Perches##		}
4686c4a62ef9SJoe Perches##
4687c4a62ef9SJoe Perches
4688de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4689de7d4f0eSAndy Whitcroft# on the line
469094fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4691d5e616fcSJoe Perches			if (ERROR("SPACING",
4692d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4693d5e616fcSJoe Perches			    $fix) {
4694194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4695d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4696d5e616fcSJoe Perches			}
46970a920b5bSAndy Whitcroft		}
46980a920b5bSAndy Whitcroft
469922f2a2efSAndy Whitcroft# check spacing on square brackets
470022f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
47013705ce5bSJoe Perches			if (ERROR("SPACING",
47023705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
47033705ce5bSJoe Perches			    $fix) {
4704194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47053705ce5bSJoe Perches				    s/\[\s+/\[/;
47063705ce5bSJoe Perches			}
470722f2a2efSAndy Whitcroft		}
470822f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
47093705ce5bSJoe Perches			if (ERROR("SPACING",
47103705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
47113705ce5bSJoe Perches			    $fix) {
4712194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47133705ce5bSJoe Perches				    s/\s+\]/\]/;
47143705ce5bSJoe Perches			}
471522f2a2efSAndy Whitcroft		}
471622f2a2efSAndy Whitcroft
4717c45dcabdSAndy Whitcroft# check spacing on parentheses
47189c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
47199c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
47203705ce5bSJoe Perches			if (ERROR("SPACING",
47213705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
47223705ce5bSJoe Perches			    $fix) {
4723194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47243705ce5bSJoe Perches				    s/\(\s+/\(/;
47253705ce5bSJoe Perches			}
472622f2a2efSAndy Whitcroft		}
472713214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4728c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4729c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
47303705ce5bSJoe Perches			if (ERROR("SPACING",
47313705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
47323705ce5bSJoe Perches			    $fix) {
4733194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47343705ce5bSJoe Perches				    s/\s+\)/\)/;
47353705ce5bSJoe Perches			}
473622f2a2efSAndy Whitcroft		}
473722f2a2efSAndy Whitcroft
4738e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4739e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4740e2826fd0SJoe Perches
4741e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4742ea4acbb1SJoe Perches			my $var = $1;
4743ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4744ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4745ea4acbb1SJoe Perches			    $fix) {
4746ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4747ea4acbb1SJoe Perches			}
4748ea4acbb1SJoe Perches		}
4749ea4acbb1SJoe Perches
4750ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4751ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4752ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4753ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4754ea4acbb1SJoe Perches			my $var = $2;
4755ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4756ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4757ea4acbb1SJoe Perches			    $fix) {
4758ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4759ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4760ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4761ea4acbb1SJoe Perches			}
4762e2826fd0SJoe Perches		}
4763e2826fd0SJoe Perches
476463b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4765a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4766a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
47675b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
476863b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
476963b7c73eSJoe Perches			my $if_stat = $1;
477063b7c73eSJoe Perches			my $test = substr($2, 1, -1);
477163b7c73eSJoe Perches			my $herectx;
477263b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
477363b7c73eSJoe Perches				my $match = $1;
477463b7c73eSJoe Perches				# avoid parentheses around potential macro args
477563b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
477663b7c73eSJoe Perches				if (!defined($herectx)) {
477763b7c73eSJoe Perches					$herectx = $here . "\n";
477863b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
477963b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
478063b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
478163b7c73eSJoe Perches						$herectx .=  $rl . "\n";
478263b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
478363b7c73eSJoe Perches					}
478463b7c73eSJoe Perches				}
478563b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
478663b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
478763b7c73eSJoe Perches			}
478863b7c73eSJoe Perches		}
478963b7c73eSJoe Perches
47900a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
47914a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
47920a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
47933705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
47943705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
47953705ce5bSJoe Perches			    $fix) {
4796194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47973705ce5bSJoe Perches				    s/^(.)\s+/$1/;
47983705ce5bSJoe Perches			}
47990a920b5bSAndy Whitcroft		}
48000a920b5bSAndy Whitcroft
48015b9553abSJoe Perches# return is not a function
4802507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4803c45dcabdSAndy Whitcroft			my $spacing = $1;
48045b57980dSJoe Perches			if ($perl_version_ok &&
48055b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
48065b9553abSJoe Perches				my $value = $1;
48075b9553abSJoe Perches				$value = deparenthesize($value);
48085b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4809000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4810000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
48115b9553abSJoe Perches				}
4812c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4813000d1cc1SJoe Perches				ERROR("SPACING",
4814000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4815c45dcabdSAndy Whitcroft			}
4816c45dcabdSAndy Whitcroft		}
4817507e5141SJoe Perches
4818b43ae21bSJoe Perches# unnecessary return in a void function
4819b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4820b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4821b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4822b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4823b43ae21bSJoe Perches		    $linenr >= 3 &&
4824b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4825b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
48269819cf25SJoe Perches			WARN("RETURN_VOID",
4827b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
48289819cf25SJoe Perches               }
48299819cf25SJoe Perches
4830189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
48315b57980dSJoe Perches		if ($perl_version_ok &&
4832189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4833189248d8SJoe Perches			my $openparens = $1;
4834189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4835189248d8SJoe Perches			my $msg = "";
4836189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4837189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4838189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4839189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4840189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4841189248d8SJoe Perches			}
4842189248d8SJoe Perches		}
4843189248d8SJoe Perches
4844c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4845c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4846c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4847c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
48485b57980dSJoe Perches		if ($perl_version_ok &&
4849c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4850c5595fa2SJoe Perches			my $lead = $1;
4851c5595fa2SJoe Perches			my $const = $2;
4852c5595fa2SJoe Perches			my $comp = $3;
4853c5595fa2SJoe Perches			my $to = $4;
4854c5595fa2SJoe Perches			my $newcomp = $comp;
4855f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4856c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4857c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4858c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4859c5595fa2SJoe Perches			    $fix) {
4860c5595fa2SJoe Perches				if ($comp eq "<") {
4861c5595fa2SJoe Perches					$newcomp = ">";
4862c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4863c5595fa2SJoe Perches					$newcomp = ">=";
4864c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4865c5595fa2SJoe Perches					$newcomp = "<";
4866c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4867c5595fa2SJoe Perches					$newcomp = "<=";
4868c5595fa2SJoe Perches				}
4869c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4870c5595fa2SJoe Perches			}
4871c5595fa2SJoe Perches		}
4872c5595fa2SJoe Perches
4873f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4874f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
487553a3c448SAndy Whitcroft			my $name = $1;
487653a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4877000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4878f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
487953a3c448SAndy Whitcroft			}
488053a3c448SAndy Whitcroft		}
4881c45dcabdSAndy Whitcroft
48820a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
48834a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
48843705ce5bSJoe Perches			if (ERROR("SPACING",
48853705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
48863705ce5bSJoe Perches			    $fix) {
4887194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48883705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
48893705ce5bSJoe Perches			}
48900a920b5bSAndy Whitcroft		}
48910a920b5bSAndy Whitcroft
4892f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4893f5fe35ddSAndy Whitcroft# statements after the conditional.
4894170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
48953e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
48963e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
48973e469cdcSAndy Whitcroft					if (!defined $stat);
4898170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4899170d3a22SAndy Whitcroft						$remain_next, $off_next);
4900170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4901170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4902170d3a22SAndy Whitcroft
4903170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4904170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4905170d3a22SAndy Whitcroft				# then count those as offsets.
4906170d3a22SAndy Whitcroft				my ($whitespace) =
4907170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4908170d3a22SAndy Whitcroft				my $offset =
4909170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4910170d3a22SAndy Whitcroft
4911170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4912170d3a22SAndy Whitcroft								$offset} = 1;
4913170d3a22SAndy Whitcroft			}
4914170d3a22SAndy Whitcroft		}
4915170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4916c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4917170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4918171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
49198905a67cSAndy Whitcroft
4920b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4921000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4922000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
49238905a67cSAndy Whitcroft			}
49248905a67cSAndy Whitcroft
49258905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
49268905a67cSAndy Whitcroft			# conditional.
4927773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
49288905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
492913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
493053210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
493153210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4932773647a0SAndy Whitcroft			{
4933bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4934bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4935bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
493642bdf74cSHidetoshi Seto				my $stat_real = '';
4937bb44ad39SAndy Whitcroft
493842bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
493942bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4940bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4941bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4942bb44ad39SAndy Whitcroft				}
4943bb44ad39SAndy Whitcroft
4944000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4945000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
49468905a67cSAndy Whitcroft			}
49478905a67cSAndy Whitcroft		}
49488905a67cSAndy Whitcroft
494913214adfSAndy Whitcroft# Check for bitwise tests written as boolean
495013214adfSAndy Whitcroft		if ($line =~ /
495113214adfSAndy Whitcroft			(?:
495213214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
495313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
495413214adfSAndy Whitcroft				(?:\&\&|\|\|)
495513214adfSAndy Whitcroft			|
495613214adfSAndy Whitcroft				(?:\&\&|\|\|)
495713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
495813214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
495913214adfSAndy Whitcroft			)/x)
496013214adfSAndy Whitcroft		{
4961000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4962000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
496313214adfSAndy Whitcroft		}
496413214adfSAndy Whitcroft
49658905a67cSAndy Whitcroft# if and else should not have general statements after it
496613214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
496713214adfSAndy Whitcroft			my $s = $1;
496813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
496913214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4970000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4971000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
49720a920b5bSAndy Whitcroft			}
497313214adfSAndy Whitcroft		}
497439667782SAndy Whitcroft# if should not continue a brace
497539667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4976000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4977048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
497839667782SAndy Whitcroft				$herecurr);
497939667782SAndy Whitcroft		}
4980a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4981a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4982a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
49833fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4984a1080bf8SAndy Whitcroft			\s*return\s+
4985a1080bf8SAndy Whitcroft		    )/xg)
4986a1080bf8SAndy Whitcroft		{
4987000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4988000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4989a1080bf8SAndy Whitcroft		}
49900a920b5bSAndy Whitcroft
49910a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
49920a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
49938b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
49940a920b5bSAndy Whitcroft		    $previndent == $indent) {
49958b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
49968b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
49978b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49988b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49998b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
50008b8856f4SJoe Perches				my $fixedline = $prevrawline;
50018b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
50028b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
50038b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50048b8856f4SJoe Perches				}
50058b8856f4SJoe Perches				$fixedline = $rawline;
50068b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
50078b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
50088b8856f4SJoe Perches			}
50090a920b5bSAndy Whitcroft		}
50100a920b5bSAndy Whitcroft
50118b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5012c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5013c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5014c2fdda0dSAndy Whitcroft
5015c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5016c2fdda0dSAndy Whitcroft			# conditional.
5017773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5018c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5019c2fdda0dSAndy Whitcroft
5020c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
50218b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
50228b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
50238b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50248b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
50258b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
50268b8856f4SJoe Perches					my $fixedline = $prevrawline;
50278b8856f4SJoe Perches					my $trailing = $rawline;
50288b8856f4SJoe Perches					$trailing =~ s/^\+//;
50298b8856f4SJoe Perches					$trailing = trim($trailing);
50308b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
50318b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50328b8856f4SJoe Perches				}
5033c2fdda0dSAndy Whitcroft			}
5034c2fdda0dSAndy Whitcroft		}
5035c2fdda0dSAndy Whitcroft
503695e2c602SJoe Perches#Specific variable tests
5037323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5038323c1260SJoe Perches			my $var = $1;
503995e2c602SJoe Perches
504095e2c602SJoe Perches#CamelCase
5041807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5042be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
504322735ce8SJoe Perches#Ignore Page<foo> variants
5044807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5045d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5046d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5047d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5048f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5049f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
50507e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
50517e781f67SJoe Perches					my $word = $1;
50527e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5053d8b07710SJoe Perches					if ($check) {
5054d8b07710SJoe Perches						seed_camelcase_includes();
5055d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5056d8b07710SJoe Perches							seed_camelcase_file($realfile);
5057d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5058d8b07710SJoe Perches						}
5059d8b07710SJoe Perches					}
50607e781f67SJoe Perches					if (!defined $camelcase{$word}) {
50617e781f67SJoe Perches						$camelcase{$word} = 1;
5062be79794bSJoe Perches						CHK("CAMELCASE",
50637e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
50647e781f67SJoe Perches					}
5065323c1260SJoe Perches				}
5066323c1260SJoe Perches			}
50673445686aSJoe Perches		}
50680a920b5bSAndy Whitcroft
50690a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5070d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5071d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5072d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5073d5e616fcSJoe Perches			    $fix) {
5074194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5075d5e616fcSJoe Perches			}
50760a920b5bSAndy Whitcroft		}
50770a920b5bSAndy Whitcroft
50780e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
50790e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5080c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5081e09dec48SAndy Whitcroft			my $file = "$1.h";
5082e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5083e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5084e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
50857840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5086c45dcabdSAndy Whitcroft			{
50870e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
50880e212e0aSFabian Frederick				if ($asminclude > 0) {
5089e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5090000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5091000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5092e09dec48SAndy Whitcroft					} else {
5093000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5094000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5095e09dec48SAndy Whitcroft					}
50960a920b5bSAndy Whitcroft				}
50970a920b5bSAndy Whitcroft			}
50980e212e0aSFabian Frederick		}
50990a920b5bSAndy Whitcroft
5100653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5101653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5102cf655043SAndy Whitcroft# in a known good container
5103b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5104b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5105d8aaf121SAndy Whitcroft			my $ln = $linenr;
5106d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5107c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5108c45dcabdSAndy Whitcroft			my $ctx = '';
510908a2843eSJoe Perches			my $has_flow_statement = 0;
511008a2843eSJoe Perches			my $has_arg_concat = 0;
5111c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5112f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5113f74bd194SAndy Whitcroft			$ctx = $dstat;
5114c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5115a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5116c45dcabdSAndy Whitcroft
511708a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
511862e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
511908a2843eSJoe Perches
5120f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5121f59b64bfSJoe Perches			my $define_args = $1;
5122f59b64bfSJoe Perches			my $define_stmt = $dstat;
5123f59b64bfSJoe Perches			my @def_args = ();
5124f59b64bfSJoe Perches
5125f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5126f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5127f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
51288c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5129f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5130f59b64bfSJoe Perches			}
5131f59b64bfSJoe Perches
5132292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5133c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5134c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5135c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5136c45dcabdSAndy Whitcroft
5137c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5138bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5139bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
51406b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5141bf30d6edSAndy Whitcroft			{
5142c45dcabdSAndy Whitcroft			}
5143c45dcabdSAndy Whitcroft
5144e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
514533acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
514633acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5147e45bab8eSAndy Whitcroft			{
5148e45bab8eSAndy Whitcroft			}
5149e45bab8eSAndy Whitcroft
515042e15293SJoe Perches			# Make asm volatile uses seem like a generic function
515142e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
515242e15293SJoe Perches
5153c45dcabdSAndy Whitcroft			my $exceptions = qr{
5154c45dcabdSAndy Whitcroft				$Declare|
5155c45dcabdSAndy Whitcroft				module_param_named|
5156a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5157c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5158c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5159383099fdSAndy Whitcroft				__typeof__\(|
516022fd2d3eSStefani Seibold				union|
516122fd2d3eSStefani Seibold				struct|
5162ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
51636b10df42SVladimir Zapolskiy				^\"|\"$|
51646b10df42SVladimir Zapolskiy				^\[
5165c45dcabdSAndy Whitcroft			}x;
51665eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5167f59b64bfSJoe Perches
5168f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5169f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5170e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5171f59b64bfSJoe Perches
5172f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5173f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5174f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
51753cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5176356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5177f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5178f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5179e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
518072f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5181f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5182f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5183f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
51844e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5185f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5186c45dcabdSAndy Whitcroft			{
5187e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5188e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5189e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5190e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5191f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5192f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5193f74bd194SAndy Whitcroft				} else {
5194000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5195388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5196d8aaf121SAndy Whitcroft				}
5197f59b64bfSJoe Perches
5198f59b64bfSJoe Perches			}
51995207649bSJoe Perches
52005207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
52015207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
52025207649bSJoe Perches			my $first = 1;
52035207649bSJoe Perches			$define_stmt = "";
52045207649bSJoe Perches			foreach my $l (@stmt_array) {
52055207649bSJoe Perches				$l =~ s/\\$//;
52065207649bSJoe Perches				if ($first) {
52075207649bSJoe Perches					$define_stmt = $l;
52085207649bSJoe Perches					$first = 0;
52095207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
52105207649bSJoe Perches					$define_stmt .= substr($l, 1);
52115207649bSJoe Perches				}
52125207649bSJoe Perches			}
52135207649bSJoe Perches			$define_stmt =~ s/$;//g;
52145207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
52155207649bSJoe Perches			$define_stmt = trim($define_stmt);
52165207649bSJoe Perches
5217f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5218f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5219f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
52209192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
52217fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
52226dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
52237fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
52247fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5225d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5226f59b64bfSJoe Perches				if ($use_cnt > 1) {
5227f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5228f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5229f59b64bfSJoe Perches				    }
52309192d41aSJoe Perches# check if any macro arguments may have other precedence issues
52317fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
52329192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
52339192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
52349192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
52359192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
52369192d41aSJoe Perches				}
52370a920b5bSAndy Whitcroft			}
52385023d347SJoe Perches
523908a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
524008a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
524108a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
524208a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5243e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
524408a2843eSJoe Perches
524508a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
524608a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
524708a2843eSJoe Perches			}
524808a2843eSJoe Perches
5249481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
52505023d347SJoe Perches
52515023d347SJoe Perches		} else {
52525023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5253481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5254481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
52555023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
52565023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
52575023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
52585023d347SJoe Perches			}
5259653d4876SAndy Whitcroft		}
52600a920b5bSAndy Whitcroft
5261b13edf7fSJoe Perches# do {} while (0) macro tests:
5262b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5263b13edf7fSJoe Perches# macro should not end with a semicolon
52645b57980dSJoe Perches		if ($perl_version_ok &&
5265b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5266b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5267b13edf7fSJoe Perches			my $ln = $linenr;
5268b13edf7fSJoe Perches			my $cnt = $realcnt;
5269b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5270b13edf7fSJoe Perches			my $ctx = '';
5271b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5272b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5273b13edf7fSJoe Perches			$ctx = $dstat;
5274b13edf7fSJoe Perches
5275b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
52761b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5277b13edf7fSJoe Perches
5278b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5279b13edf7fSJoe Perches				my $stmts = $2;
5280b13edf7fSJoe Perches				my $semis = $3;
5281b13edf7fSJoe Perches
5282b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5283b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5284e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5285b13edf7fSJoe Perches
5286ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5287ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5288b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5289b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5290b13edf7fSJoe Perches				}
5291b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5292b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5293b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5294b13edf7fSJoe Perches				}
5295f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5296f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5297f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5298e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5299f5ef95b1SJoe Perches
5300f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5301f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5302b13edf7fSJoe Perches			}
5303b13edf7fSJoe Perches		}
5304b13edf7fSJoe Perches
5305f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
530613214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
530713214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5308cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
530913214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5310cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5311cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5312aad4f614SJoe Perches				my @allowed = ();
5313aad4f614SJoe Perches				my $allow = 0;
531413214adfSAndy Whitcroft				my $seen = 0;
5315773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5316cf655043SAndy Whitcroft				my $ln = $linenr - 1;
531713214adfSAndy Whitcroft				for my $chunk (@chunks) {
531813214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
531913214adfSAndy Whitcroft
5320773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5321773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5322773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5323773647a0SAndy Whitcroft
5324aad4f614SJoe Perches					$allowed[$allow] = 0;
5325773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5326773647a0SAndy Whitcroft
5327773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5328773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5329773647a0SAndy Whitcroft
5330773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5331cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5332cf655043SAndy Whitcroft
5333773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
533413214adfSAndy Whitcroft
533513214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
533613214adfSAndy Whitcroft
5337aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5338cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5339cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5340aad4f614SJoe Perches						$allowed[$allow] = 1;
534113214adfSAndy Whitcroft					}
534213214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5343cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5344aad4f614SJoe Perches						$allowed[$allow] = 1;
534513214adfSAndy Whitcroft					}
5346cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5347cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5348aad4f614SJoe Perches						$allowed[$allow] = 1;
534913214adfSAndy Whitcroft					}
5350aad4f614SJoe Perches					$allow++;
535113214adfSAndy Whitcroft				}
5352aad4f614SJoe Perches				if ($seen) {
5353aad4f614SJoe Perches					my $sum_allowed = 0;
5354aad4f614SJoe Perches					foreach (@allowed) {
5355aad4f614SJoe Perches						$sum_allowed += $_;
5356aad4f614SJoe Perches					}
5357aad4f614SJoe Perches					if ($sum_allowed == 0) {
5358000d1cc1SJoe Perches						WARN("BRACES",
5359000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5360aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5361aad4f614SJoe Perches						 $seen != $allow) {
5362aad4f614SJoe Perches						CHK("BRACES",
5363aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5364aad4f614SJoe Perches					}
536513214adfSAndy Whitcroft				}
536613214adfSAndy Whitcroft			}
536713214adfSAndy Whitcroft		}
5368773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
536913214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5370cf655043SAndy Whitcroft			my $allowed = 0;
5371f0a594c1SAndy Whitcroft
5372cf655043SAndy Whitcroft			# Check the pre-context.
5373cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5374cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5375cf655043SAndy Whitcroft				$allowed = 1;
5376f0a594c1SAndy Whitcroft			}
5377773647a0SAndy Whitcroft
5378773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5379773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5380773647a0SAndy Whitcroft
5381cf655043SAndy Whitcroft			# Check the condition.
5382cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5383773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5384cf655043SAndy Whitcroft			if (defined $cond) {
5385773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5386cf655043SAndy Whitcroft			}
5387cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5388cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5389cf655043SAndy Whitcroft				$allowed = 1;
5390cf655043SAndy Whitcroft			}
5391cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5392cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5393cf655043SAndy Whitcroft				$allowed = 1;
5394cf655043SAndy Whitcroft			}
5395cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5396cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5397cf655043SAndy Whitcroft				$allowed = 1;
5398cf655043SAndy Whitcroft			}
5399cf655043SAndy Whitcroft			# Check the post-context.
5400cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5401cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5402cf655043SAndy Whitcroft				if (defined $cond) {
5403773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5404cf655043SAndy Whitcroft				}
5405cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5406cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5407cf655043SAndy Whitcroft					$allowed = 1;
5408cf655043SAndy Whitcroft				}
5409cf655043SAndy Whitcroft			}
5410cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5411f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5412e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5413cf655043SAndy Whitcroft
5414000d1cc1SJoe Perches				WARN("BRACES",
5415000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5416f0a594c1SAndy Whitcroft			}
5417f0a594c1SAndy Whitcroft		}
5418f0a594c1SAndy Whitcroft
5419e4c5babdSJoe Perches# check for single line unbalanced braces
542095330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
542195330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5422e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5423e4c5babdSJoe Perches		}
5424e4c5babdSJoe Perches
54250979ae66SJoe Perches# check for unnecessary blank lines around braces
542677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5427f8e58219SJoe Perches			if (CHK("BRACES",
5428f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5429f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5430f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5431f8e58219SJoe Perches			}
54320979ae66SJoe Perches		}
543377b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5434f8e58219SJoe Perches			if (CHK("BRACES",
5435f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5436f8e58219SJoe Perches			    $fix) {
5437f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5438f8e58219SJoe Perches			}
54390979ae66SJoe Perches		}
54400979ae66SJoe Perches
54414a0df2efSAndy Whitcroft# no volatiles please
54426c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
54436c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5444000d1cc1SJoe Perches			WARN("VOLATILE",
54458c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
54464a0df2efSAndy Whitcroft		}
54474a0df2efSAndy Whitcroft
54485e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
54495e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
54505e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
54515e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
545233acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
54535e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
54545e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
54555e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
54565e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
54575e4f6ba5SJoe Perches				     $fix &&
54585e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
54595e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
54605e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
54615e4f6ba5SJoe Perches				my $comma_close = "";
54625e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
54635e4f6ba5SJoe Perches					$comma_close = $1;
54645e4f6ba5SJoe Perches				}
54655e4f6ba5SJoe Perches
54665e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
54675e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
54685e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
54695e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
54705e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
54715e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
54725e4f6ba5SJoe Perches				$fixedline = $rawline;
54735e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
54745e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
54755e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54765e4f6ba5SJoe Perches				}
54775e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
54785e4f6ba5SJoe Perches			}
54795e4f6ba5SJoe Perches		}
54805e4f6ba5SJoe Perches
54815e4f6ba5SJoe Perches# check for missing a space in a string concatenation
54825e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
54835e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
54845e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
54855e4f6ba5SJoe Perches		}
54865e4f6ba5SJoe Perches
548777cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5488e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5489e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5490e4b7d309SJoe Perches# function declarations
549177cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
549277cb8546SJoe Perches		    defined($context_function) &&
5493e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5494e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
549577cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5496e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
549777cb8546SJoe Perches		}
549877cb8546SJoe Perches
54995e4f6ba5SJoe Perches# check for spaces before a quoted newline
55005e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
55015e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
55025e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
55035e4f6ba5SJoe Perches			    $fix) {
55045e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
55055e4f6ba5SJoe Perches			}
55065e4f6ba5SJoe Perches
55075e4f6ba5SJoe Perches		}
55085e4f6ba5SJoe Perches
5509f17dba4fSJoe Perches# concatenated string without spaces between elements
551079682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
551179682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
551279682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
551379682c0cSJoe Perches			    $fix) {
551479682c0cSJoe Perches				while ($line =~ /($String)/g) {
551579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
551679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
551779682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
551879682c0cSJoe Perches				}
551979682c0cSJoe Perches			}
5520f17dba4fSJoe Perches		}
5521f17dba4fSJoe Perches
552290ad30e5SJoe Perches# uncoalesced string fragments
552333acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
552479682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
552579682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
552679682c0cSJoe Perches			    $fix) {
552779682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
552879682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
552979682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
553079682c0cSJoe Perches				}
553179682c0cSJoe Perches			}
553290ad30e5SJoe Perches		}
553390ad30e5SJoe Perches
5534522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5535522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5536522b837cSAlexey Dobriyan		my $show_Z = 1;
55375e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5538522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
55395e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5540522b837cSAlexey Dobriyan			# check for %L
5541522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
55425e4f6ba5SJoe Perches				WARN("PRINTF_L",
5543522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5544522b837cSAlexey Dobriyan				$show_L = 0;
55455e4f6ba5SJoe Perches			}
5546522b837cSAlexey Dobriyan			# check for %Z
5547522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5548522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5549522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5550522b837cSAlexey Dobriyan				$show_Z = 0;
5551522b837cSAlexey Dobriyan			}
5552522b837cSAlexey Dobriyan			# check for 0x<decimal>
5553522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5554522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
55556e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
55566e300757SJoe Perches			}
55575e4f6ba5SJoe Perches		}
55585e4f6ba5SJoe Perches
55595e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
55603f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
55615e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
55625e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
55635e4f6ba5SJoe Perches		}
55645e4f6ba5SJoe Perches
556500df344fSAndy Whitcroft# warn about #if 0
5566c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
556760f89010SPrakruthi Deepak Heragu			WARN("IF_0",
556860f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
556960f89010SPrakruthi Deepak Heragu		}
557060f89010SPrakruthi Deepak Heragu
557160f89010SPrakruthi Deepak Heragu# warn about #if 1
557260f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
557360f89010SPrakruthi Deepak Heragu			WARN("IF_1",
557460f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
55754a0df2efSAndy Whitcroft		}
55764a0df2efSAndy Whitcroft
557703df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
557803df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5579100425deSJoe Perches			my $tested = quotemeta($1);
5580100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5581100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5582100425deSJoe Perches				my $func = $1;
5583100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5584100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5585100425deSJoe Perches				    $fix) {
5586100425deSJoe Perches					my $do_fix = 1;
5587100425deSJoe Perches					my $leading_tabs = "";
5588100425deSJoe Perches					my $new_leading_tabs = "";
5589100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5590100425deSJoe Perches						$leading_tabs = $1;
5591100425deSJoe Perches					} else {
5592100425deSJoe Perches						$do_fix = 0;
5593100425deSJoe Perches					}
5594100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5595100425deSJoe Perches						$new_leading_tabs = $1;
5596100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5597100425deSJoe Perches							$do_fix = 0;
5598100425deSJoe Perches						}
5599100425deSJoe Perches					} else {
5600100425deSJoe Perches						$do_fix = 0;
5601100425deSJoe Perches					}
5602100425deSJoe Perches					if ($do_fix) {
5603100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5604100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5605100425deSJoe Perches					}
5606100425deSJoe Perches				}
56074c432a8fSGreg Kroah-Hartman			}
56084c432a8fSGreg Kroah-Hartman		}
5609f0a594c1SAndy Whitcroft
5610ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5611ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5612ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5613ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5614ebfdc409SJoe Perches		    $linenr > 3) {
5615ebfdc409SJoe Perches			my $testval = $2;
5616ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5617ebfdc409SJoe Perches
5618ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5619ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5620ebfdc409SJoe Perches
5621e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5622e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5623ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5624ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5625ebfdc409SJoe Perches			}
5626ebfdc409SJoe Perches		}
5627ebfdc409SJoe Perches
5628f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5629dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5630f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5631f78d98f6SJoe Perches			my $level = $1;
5632f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5633f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5634f78d98f6SJoe Perches			    $fix) {
5635f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5636f78d98f6SJoe Perches			}
5637f78d98f6SJoe Perches		}
5638f78d98f6SJoe Perches
563945c55e92SJoe Perches# check for logging continuations
564045c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
564145c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
564245c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
564345c55e92SJoe Perches		}
564445c55e92SJoe Perches
5645abb08a53SJoe Perches# check for mask then right shift without a parentheses
56465b57980dSJoe Perches		if ($perl_version_ok &&
5647abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5648abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5649abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5650abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5651abb08a53SJoe Perches		}
5652abb08a53SJoe Perches
5653b75ac618SJoe Perches# check for pointer comparisons to NULL
56545b57980dSJoe Perches		if ($perl_version_ok) {
5655b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5656b75ac618SJoe Perches				my $val = $1;
5657b75ac618SJoe Perches				my $equal = "!";
5658b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5659b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5660b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5661b75ac618SJoe Perches					    $fix) {
5662b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5663b75ac618SJoe Perches				}
5664b75ac618SJoe Perches			}
5665b75ac618SJoe Perches		}
5666b75ac618SJoe Perches
56678716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
56688716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
56698716de38SJoe Perches			my $attr = $1;
56708716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
56718716de38SJoe Perches				my $ptr = $1;
56728716de38SJoe Perches				my $var = $2;
56738716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
56748716de38SJoe Perches				      ERROR("MISPLACED_INIT",
56758716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
56768716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
56778716de38SJoe Perches				      WARN("MISPLACED_INIT",
56788716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
56798716de38SJoe Perches				    $fix) {
5680194f66fcSJoe 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;
56818716de38SJoe Perches				}
56828716de38SJoe Perches			}
56838716de38SJoe Perches		}
56848716de38SJoe Perches
5685e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5686e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5687e970b884SJoe Perches			my $attr = $1;
5688e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5689e970b884SJoe Perches			my $attr_prefix = $1;
5690e970b884SJoe Perches			my $attr_type = $2;
5691e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5692e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5693e970b884SJoe Perches			    $fix) {
5694194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5695e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5696e970b884SJoe Perches			}
5697e970b884SJoe Perches		}
5698e970b884SJoe Perches
5699e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5700e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5701e970b884SJoe Perches			my $attr = $1;
5702e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5703e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5704e970b884SJoe Perches			    $fix) {
5705194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5706e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5707e970b884SJoe Perches				$lead = rtrim($1);
5708e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5709e970b884SJoe Perches				$lead = "${lead}const ";
5710194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5711e970b884SJoe Perches			}
5712e970b884SJoe Perches		}
5713e970b884SJoe Perches
5714c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5715c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5716c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5717c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5718c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5719c17893c7SJoe Perches			    $fix) {
5720c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5721c17893c7SJoe Perches			}
5722c17893c7SJoe Perches		}
5723c17893c7SJoe Perches
5724fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5725fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5726fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5727fbdb8138SJoe Perches			my $constant_func = $1;
5728fbdb8138SJoe Perches			my $func = $constant_func;
5729fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5730fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5731fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5732fbdb8138SJoe Perches			    $fix) {
5733194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5734fbdb8138SJoe Perches			}
5735fbdb8138SJoe Perches		}
5736fbdb8138SJoe Perches
57371a15a250SPatrick Pannuto# prefer usleep_range over udelay
573837581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
573943c1d77cSJoe Perches			my $delay = $1;
57401a15a250SPatrick Pannuto			# ignore udelay's < 10, however
574143c1d77cSJoe Perches			if (! ($delay < 10) ) {
5742000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5743458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
574443c1d77cSJoe Perches			}
574543c1d77cSJoe Perches			if ($delay > 2000) {
574643c1d77cSJoe Perches				WARN("LONG_UDELAY",
574743c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
57481a15a250SPatrick Pannuto			}
57491a15a250SPatrick Pannuto		}
57501a15a250SPatrick Pannuto
575109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
575209ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
575309ef8725SPatrick Pannuto			if ($1 < 20) {
5754000d1cc1SJoe Perches				WARN("MSLEEP",
5755458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
575609ef8725SPatrick Pannuto			}
575709ef8725SPatrick Pannuto		}
575809ef8725SPatrick Pannuto
575936ec1939SJoe Perches# check for comparisons of jiffies
576036ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
576136ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
576236ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
576336ec1939SJoe Perches		}
576436ec1939SJoe Perches
57659d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
57669d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
57679d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
57689d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
57699d7a34a5SJoe Perches		}
57709d7a34a5SJoe Perches
577100df344fSAndy Whitcroft# warn about #ifdefs in C files
5772c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
577300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
577400df344fSAndy Whitcroft#			print "$herecurr";
577500df344fSAndy Whitcroft#			$clean = 0;
577600df344fSAndy Whitcroft#		}
577700df344fSAndy Whitcroft
577822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5779c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
57803705ce5bSJoe Perches			if (ERROR("SPACING",
57813705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
57823705ce5bSJoe Perches			    $fix) {
5783194f66fcSJoe Perches				$fixed[$fixlinenr] =~
57843705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
57853705ce5bSJoe Perches			}
57863705ce5bSJoe Perches
578722f2a2efSAndy Whitcroft		}
578822f2a2efSAndy Whitcroft
57894a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5790171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5791171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
57924a0df2efSAndy Whitcroft			my $which = $1;
57934a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5794000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5795000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
57964a0df2efSAndy Whitcroft			}
57974a0df2efSAndy Whitcroft		}
57984a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5799402c2553SMichael S. Tsirkin
5800402c2553SMichael S. Tsirkin		my $barriers = qr{
5801402c2553SMichael S. Tsirkin			mb|
5802402c2553SMichael S. Tsirkin			rmb|
5803402c2553SMichael S. Tsirkin			wmb|
5804402c2553SMichael S. Tsirkin			read_barrier_depends
5805402c2553SMichael S. Tsirkin		}x;
5806402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5807402c2553SMichael S. Tsirkin			mb__before_atomic|
5808402c2553SMichael S. Tsirkin			mb__after_atomic|
5809402c2553SMichael S. Tsirkin			store_release|
5810402c2553SMichael S. Tsirkin			load_acquire|
5811402c2553SMichael S. Tsirkin			store_mb|
5812402c2553SMichael S. Tsirkin			(?:$barriers)
5813402c2553SMichael S. Tsirkin		}x;
5814402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5815402c2553SMichael S. Tsirkin			(?:$barriers)|
581643e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
581743e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5818402c2553SMichael S. Tsirkin		}x;
5819402c2553SMichael S. Tsirkin
5820402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
58214a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5822c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5823000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
58244a0df2efSAndy Whitcroft			}
58254a0df2efSAndy Whitcroft		}
58263ad81779SPaul E. McKenney
5827f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5828f4073b0fSMichael S. Tsirkin
5829f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5830f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5831f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5832f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5833f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5834f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5835f4073b0fSMichael S. Tsirkin		}
5836f4073b0fSMichael S. Tsirkin
5837cb426e99SJoe Perches# check for waitqueue_active without a comment.
5838cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5839cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5840cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5841cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5842cb426e99SJoe Perches			}
5843cb426e99SJoe Perches		}
58443ad81779SPaul E. McKenney
584591db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
584691db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
584791db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
584891db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
584991db2592SPaul E. McKenney		}
585091db2592SPaul E. McKenney
58514a0df2efSAndy Whitcroft# check of hardware specific defines
5852c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5853000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5854000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
58550a920b5bSAndy Whitcroft		}
5856653d4876SAndy Whitcroft
5857596ed45bSJoe Perches# check that the storage class is not after a type
5858596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5859000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5860596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5861596ed45bSJoe Perches		}
5862596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5863596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5864596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5865596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5866596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5867596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5868596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5869d4977c78STobias Klauser		}
5870d4977c78STobias Klauser
5871de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5872de7d4f0eSAndy Whitcroft# storage class and type.
58739c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
58749c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5875000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5876000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5877de7d4f0eSAndy Whitcroft		}
5878de7d4f0eSAndy Whitcroft
58798905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
58802b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58812b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5882d5e616fcSJoe Perches			if (WARN("INLINE",
5883d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5884d5e616fcSJoe Perches			    $fix) {
5885194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5886d5e616fcSJoe Perches
5887d5e616fcSJoe Perches			}
58888905a67cSAndy Whitcroft		}
58898905a67cSAndy Whitcroft
58903d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
58912b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58922b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5893000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5894000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
58953d130fd0SJoe Perches		}
58963d130fd0SJoe Perches
589739b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
58982b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58992b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5900000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5901000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
590239b7e287SJoe Perches		}
590339b7e287SJoe Perches
5904462811d9SJoe Perches# Check for __attribute__ section, prefer __section
5905462811d9SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5906462811d9SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5907462811d9SJoe Perches			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5908462811d9SJoe Perches			my $new = substr($old, 1, -1);
5909462811d9SJoe Perches			if (WARN("PREFER_SECTION",
5910462811d9SJoe Perches				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5911462811d9SJoe Perches			    $fix) {
5912462811d9SJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5913462811d9SJoe Perches			}
5914462811d9SJoe Perches		}
5915462811d9SJoe Perches
59165f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
59172b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59182b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5919d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5920d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5921d5e616fcSJoe Perches			    $fix) {
5922194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5923d5e616fcSJoe Perches
5924d5e616fcSJoe Perches			}
59255f14d3bdSJoe Perches		}
59265f14d3bdSJoe Perches
59276061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
59282b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59292b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5930d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5931d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5932d5e616fcSJoe Perches			    $fix) {
5933194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5934d5e616fcSJoe Perches			}
59356061d949SJoe Perches		}
59366061d949SJoe Perches
5937619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
59385b57980dSJoe Perches		if ($perl_version_ok &&
5939619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5940619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5941619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5942619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5943619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5944619a908aSJoe Perches		}
5945619a908aSJoe Perches
5946fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5947e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5948fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5949e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5950e6176fa4SJoe Perches			my $type = $1;
5951e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5952e6176fa4SJoe Perches				$type = $1;
5953e6176fa4SJoe Perches				my $kernel_type = 'u';
5954e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5955e6176fa4SJoe Perches				$type =~ /(\d+)/;
5956e6176fa4SJoe Perches				$kernel_type .= $1;
5957e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5958e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5959e6176fa4SJoe Perches				    $fix) {
5960e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5961e6176fa4SJoe Perches				}
5962e6176fa4SJoe Perches			}
5963e6176fa4SJoe Perches		}
5964e6176fa4SJoe Perches
5965938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5966938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5967938224b5SJoe Perches			my $cast = $1;
5968938224b5SJoe Perches			my $const = $2;
5969938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5970938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5971938224b5SJoe Perches			    $fix) {
5972938224b5SJoe Perches				my $suffix = "";
5973938224b5SJoe Perches				my $newconst = $const;
5974938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5975938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5976938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5977938224b5SJoe Perches					$suffix .= 'LL';
5978938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5979938224b5SJoe Perches					$suffix .= 'L';
5980938224b5SJoe Perches				}
5981938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5982938224b5SJoe Perches			}
5983938224b5SJoe Perches		}
5984938224b5SJoe Perches
59858f53a9b8SJoe Perches# check for sizeof(&)
59868f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5987000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5988000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
59898f53a9b8SJoe Perches		}
59908f53a9b8SJoe Perches
599166c80b60SJoe Perches# check for sizeof without parenthesis
599266c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5993d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5994d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5995d5e616fcSJoe Perches			    $fix) {
5996194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5997d5e616fcSJoe Perches			}
599866c80b60SJoe Perches		}
599966c80b60SJoe Perches
600088982feaSJoe Perches# check for struct spinlock declarations
600188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
600288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
600388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
600488982feaSJoe Perches		}
600588982feaSJoe Perches
6006a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
600706668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6008a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6009caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6010caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6011d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6012d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6013d5e616fcSJoe Perches				    $fix) {
6014194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6015d5e616fcSJoe Perches				}
6016a6962d72SJoe Perches			}
6017a6962d72SJoe Perches		}
6018a6962d72SJoe Perches
60190b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
60205b57980dSJoe Perches		if ($perl_version_ok &&
60210b523769SJoe Perches		    defined $stat &&
60220b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
60230b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6024e3c6bc95STobin C. Harding			my $stat_real;
6025e3c6bc95STobin C. Harding
60260b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
60270b523769SJoe Perches			$lc = $lc + $linenr;
60280b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6029ffe07513SJoe Perches				my $specifier;
6030ffe07513SJoe Perches				my $extension;
60313bd32d6aSSakari Ailus				my $qualifier;
6032ffe07513SJoe Perches				my $bad_specifier = "";
60330b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
60340b523769SJoe Perches				$fmt =~ s/%%//g;
6035e3c6bc95STobin C. Harding
60363bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6037e3c6bc95STobin C. Harding					$specifier = $1;
6038e3c6bc95STobin C. Harding					$extension = $2;
60393bd32d6aSSakari Ailus					$qualifier = $3;
6040361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
60413bd32d6aSSakari Ailus					    ($extension eq "f" &&
60423bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6043e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
60440b523769SJoe Perches						last;
60450b523769SJoe Perches					}
6046e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6047e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6048e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
60490b523769SJoe Perches						}
6050e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6051e3c6bc95STobin 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");
6052e3c6bc95STobin C. Harding					}
6053e3c6bc95STobin C. Harding				}
6054e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
60552a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
60561df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
60571df7338aSSergey Senozhatsky					my $use = "";
6058e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
60591df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6060e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
60611df7338aSSergey Senozhatsky					}
60622a9f9d85STobin C. Harding
60630b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6064e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6065e3c6bc95STobin C. Harding				}
60660b523769SJoe Perches			}
60670b523769SJoe Perches		}
60680b523769SJoe Perches
6069554e165cSAndy Whitcroft# Check for misused memsets
60705b57980dSJoe Perches		if ($perl_version_ok &&
6071d1fe9c09SJoe Perches		    defined $stat &&
60729e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6073554e165cSAndy Whitcroft
6074d7c76ba7SJoe Perches			my $ms_addr = $2;
6075d1fe9c09SJoe Perches			my $ms_val = $7;
6076d1fe9c09SJoe Perches			my $ms_size = $12;
6077d7c76ba7SJoe Perches
6078554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6079554e165cSAndy Whitcroft				ERROR("MEMSET",
6080d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6081554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6082554e165cSAndy Whitcroft				WARN("MEMSET",
6083d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6084d7c76ba7SJoe Perches			}
6085d7c76ba7SJoe Perches		}
6086d7c76ba7SJoe Perches
608798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
60885b57980dSJoe Perches#		if ($perl_version_ok &&
6089f333195dSJoe Perches#		    defined $stat &&
6090f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6091f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6092f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6093f333195dSJoe Perches#			    $fix) {
6094f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6095f333195dSJoe Perches#			}
6096f333195dSJoe Perches#		}
609798a9bba5SJoe Perches
6098b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
60995b57980dSJoe Perches#		if ($perl_version_ok &&
6100f333195dSJoe Perches#		    defined $stat &&
6101f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6102f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6103f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6104f333195dSJoe Perches#		}
6105b6117d17SMateusz Kulikowski
61068617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
61078617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
61085b57980dSJoe Perches#		if ($perl_version_ok &&
6109f333195dSJoe Perches#		    defined $stat &&
6110f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6111f333195dSJoe Perches#
6112f333195dSJoe Perches#			my $ms_val = $7;
6113f333195dSJoe Perches#
6114f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6115f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6116f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6117f333195dSJoe Perches#				    $fix) {
6118f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6119f333195dSJoe Perches#				}
6120f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6121f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6122f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6123f333195dSJoe Perches#				    $fix) {
6124f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6125f333195dSJoe Perches#				}
6126f333195dSJoe Perches#			}
6127f333195dSJoe Perches#		}
61288617cd09SMateusz Kulikowski
6129d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
61305b57980dSJoe Perches		if ($perl_version_ok &&
6131d1fe9c09SJoe Perches		    defined $stat &&
6132d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6133d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6134d7c76ba7SJoe Perches				my $call = $1;
6135d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6136d7c76ba7SJoe Perches				my $arg1 = $3;
6137d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6138d1fe9c09SJoe Perches				my $arg2 = $8;
6139d7c76ba7SJoe Perches				my $cast;
6140d7c76ba7SJoe Perches
6141d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6142d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6143d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6144d7c76ba7SJoe Perches					$cast = $cast1;
6145d7c76ba7SJoe Perches				} else {
6146d7c76ba7SJoe Perches					$cast = $cast2;
6147d7c76ba7SJoe Perches				}
6148d7c76ba7SJoe Perches				WARN("MINMAX",
6149d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6150554e165cSAndy Whitcroft			}
6151554e165cSAndy Whitcroft		}
6152554e165cSAndy Whitcroft
61534a273195SJoe Perches# check usleep_range arguments
61545b57980dSJoe Perches		if ($perl_version_ok &&
61554a273195SJoe Perches		    defined $stat &&
61564a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
61574a273195SJoe Perches			my $min = $1;
61584a273195SJoe Perches			my $max = $7;
61594a273195SJoe Perches			if ($min eq $max) {
61604a273195SJoe Perches				WARN("USLEEP_RANGE",
6161458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61624a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
61634a273195SJoe Perches				 $min > $max) {
61644a273195SJoe Perches				WARN("USLEEP_RANGE",
6165458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61664a273195SJoe Perches			}
61674a273195SJoe Perches		}
61684a273195SJoe Perches
6169823b794cSJoe Perches# check for naked sscanf
61705b57980dSJoe Perches		if ($perl_version_ok &&
6171823b794cSJoe Perches		    defined $stat &&
61726c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6173823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6174823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6175823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6176823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6177823b794cSJoe Perches			$lc = $lc + $linenr;
61782a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6179823b794cSJoe Perches			WARN("NAKED_SSCANF",
6180823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6181823b794cSJoe Perches		}
6182823b794cSJoe Perches
6183afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
61845b57980dSJoe Perches		if ($perl_version_ok &&
6185afc819abSJoe Perches		    defined $stat &&
6186afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6187afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6188afc819abSJoe Perches			$lc = $lc + $linenr;
61892a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6190afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6191afc819abSJoe Perches				my $format = $6;
6192afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6193afc819abSJoe Perches				if ($count == 1 &&
6194afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6195afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6196afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6197afc819abSJoe Perches				}
6198afc819abSJoe Perches			}
6199afc819abSJoe Perches		}
6200afc819abSJoe Perches
620170dc8a48SJoe Perches# check for new externs in .h files.
620270dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
620370dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6204d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
620570dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
620670dc8a48SJoe Perches			    $fix) {
6207194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
620870dc8a48SJoe Perches			}
620970dc8a48SJoe Perches		}
621070dc8a48SJoe Perches
6211de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6212171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6213c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6214171ae1a4SAndy Whitcroft		{
6215c45dcabdSAndy Whitcroft			my $function_name = $1;
6216c45dcabdSAndy Whitcroft			my $paren_space = $2;
6217171ae1a4SAndy Whitcroft
6218171ae1a4SAndy Whitcroft			my $s = $stat;
6219171ae1a4SAndy Whitcroft			if (defined $cond) {
6220171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6221171ae1a4SAndy Whitcroft			}
6222c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6223c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6224c45dcabdSAndy Whitcroft			{
6225000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6226000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6227de7d4f0eSAndy Whitcroft			}
6228de7d4f0eSAndy Whitcroft
6229171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6230000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6231000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6232171ae1a4SAndy Whitcroft			}
62339c9ba34eSAndy Whitcroft
62349c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
62359c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
62369c9ba34eSAndy Whitcroft		{
6237000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6238000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6239171ae1a4SAndy Whitcroft		}
6240171ae1a4SAndy Whitcroft
6241a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6242a0ad7596SJoe Perches		if (defined $stat &&
624325bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6244ca0d8929SJoe Perches		    $1 ne "void") {
6245ca0d8929SJoe Perches			my $args = trim($1);
6246ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6247ca0d8929SJoe Perches				my $arg = trim($1);
6248ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6249ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6250ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6251ca0d8929SJoe Perches				}
6252ca0d8929SJoe Perches			}
6253ca0d8929SJoe Perches		}
6254ca0d8929SJoe Perches
6255a0ad7596SJoe Perches# check for function definitions
62565b57980dSJoe Perches		if ($perl_version_ok &&
6257a0ad7596SJoe Perches		    defined $stat &&
6258a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6259a0ad7596SJoe Perches			$context_function = $1;
6260a0ad7596SJoe Perches
6261a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6262a0ad7596SJoe Perches			my $ok = 0;
6263a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6264a0ad7596SJoe Perches			my $herectx = $here . "\n";
6265a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6266a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6267a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6268a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6269a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6270a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6271a0ad7596SJoe Perches			}
6272a0ad7596SJoe Perches			if (!$ok) {
6273a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6274a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6275a0ad7596SJoe Perches			}
6276a0ad7596SJoe Perches		}
6277a0ad7596SJoe Perches
6278de7d4f0eSAndy Whitcroft# checks for new __setup's
6279de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6280de7d4f0eSAndy Whitcroft			my $name = $1;
6281de7d4f0eSAndy Whitcroft
6282de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6283000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
62848c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6285de7d4f0eSAndy Whitcroft			}
6286653d4876SAndy Whitcroft		}
62879c0ca6f9SAndy Whitcroft
6288e29a70f1SJoe Perches# check for pointless casting of alloc functions
6289e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6290000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6291000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
62929c0ca6f9SAndy Whitcroft		}
629313214adfSAndy Whitcroft
6294a640d25cSJoe Perches# alloc style
6295a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
62965b57980dSJoe Perches		if ($perl_version_ok &&
6297e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6298a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6299a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6300a640d25cSJoe Perches		}
6301a640d25cSJoe Perches
630260a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
63035b57980dSJoe Perches		if ($perl_version_ok &&
63041b4a2ed4SJoe Perches		    defined $stat &&
63051b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
630660a55369SJoe Perches			my $oldfunc = $3;
630760a55369SJoe Perches			my $a1 = $4;
630860a55369SJoe Perches			my $a2 = $10;
630960a55369SJoe Perches			my $newfunc = "kmalloc_array";
631060a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
631160a55369SJoe Perches			my $r1 = $a1;
631260a55369SJoe Perches			my $r2 = $a2;
631360a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
631460a55369SJoe Perches				$r1 = $a2;
631560a55369SJoe Perches				$r2 = $a1;
631660a55369SJoe Perches			}
6317e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6318e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
63191b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6320e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6321e3d95a2aSTobin C. Harding
6322e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
63231b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
63241b4a2ed4SJoe Perches				    $cnt == 1 &&
6325e367455aSJoe Perches				    $fix) {
6326194f66fcSJoe 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;
632760a55369SJoe Perches				}
632860a55369SJoe Perches			}
632960a55369SJoe Perches		}
633060a55369SJoe Perches
6331972fdea2SJoe Perches# check for krealloc arg reuse
63325b57980dSJoe Perches		if ($perl_version_ok &&
63334cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
63344cab63ceSJoe Perches		    $1 eq $3) {
6335972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6336972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6337972fdea2SJoe Perches		}
6338972fdea2SJoe Perches
63395ce59ae0SJoe Perches# check for alloc argument mismatch
63405ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
63415ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
63425ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
63435ce59ae0SJoe Perches		}
63445ce59ae0SJoe Perches
6345caf2a54fSJoe Perches# check for multiple semicolons
6346caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6347d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6348d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6349d5e616fcSJoe Perches			    $fix) {
6350194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6351d5e616fcSJoe Perches			}
6352d1e2ad07SJoe Perches		}
6353d1e2ad07SJoe Perches
6354cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6355cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6356cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
63570ab90191SJoe Perches			my $ull = "";
63580ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
63590ab90191SJoe Perches			if (CHK("BIT_MACRO",
63600ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
63610ab90191SJoe Perches			    $fix) {
63620ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
63630ab90191SJoe Perches			}
63640ab90191SJoe Perches		}
63650ab90191SJoe Perches
63662d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
63672d632745SJoe 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*$/) {
63682d632745SJoe Perches			my $config = $1;
63692d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
63702d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
63712d632745SJoe Perches			    $fix) {
63722d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
63732d632745SJoe Perches			}
63742d632745SJoe Perches		}
63752d632745SJoe Perches
6376e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6377c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6378c34c09a8SJoe Perches			my $has_break = 0;
6379c34c09a8SJoe Perches			my $has_statement = 0;
6380c34c09a8SJoe Perches			my $count = 0;
6381c34c09a8SJoe Perches			my $prevline = $linenr;
6382e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6383c34c09a8SJoe Perches				$prevline--;
6384c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6385c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6386c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6387c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6388c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6389c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6390c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6391c34c09a8SJoe Perches				$has_statement = 1;
6392c34c09a8SJoe Perches				$count++;
6393258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6394c34c09a8SJoe Perches			}
6395c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6396c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6397224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6398c34c09a8SJoe Perches			}
6399c34c09a8SJoe Perches		}
6400c34c09a8SJoe Perches
6401d1e2ad07SJoe Perches# check for switch/default statements without a break;
64025b57980dSJoe Perches		if ($perl_version_ok &&
6403d1e2ad07SJoe Perches		    defined $stat &&
6404d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6405d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6406e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6407e3d95a2aSTobin C. Harding
6408d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6409d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6410caf2a54fSJoe Perches		}
6411caf2a54fSJoe Perches
641213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6413d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6414d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6415d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6416d5e616fcSJoe Perches			    $fix) {
6417194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6418d5e616fcSJoe Perches			}
641913214adfSAndy Whitcroft		}
6420773647a0SAndy Whitcroft
642162ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
642262ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
642362ec818fSJoe Perches			ERROR("DATE_TIME",
642462ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
642562ec818fSJoe Perches		}
642662ec818fSJoe Perches
64272c92488aSJoe Perches# check for use of yield()
64282c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
64292c92488aSJoe Perches			WARN("YIELD",
64302c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
64312c92488aSJoe Perches		}
64322c92488aSJoe Perches
6433179f8f40SJoe Perches# check for comparisons against true and false
6434179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6435179f8f40SJoe Perches			my $lead = $1;
6436179f8f40SJoe Perches			my $arg = $2;
6437179f8f40SJoe Perches			my $test = $3;
6438179f8f40SJoe Perches			my $otype = $4;
6439179f8f40SJoe Perches			my $trail = $5;
6440179f8f40SJoe Perches			my $op = "!";
6441179f8f40SJoe Perches
6442179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6443179f8f40SJoe Perches
6444179f8f40SJoe Perches			my $type = lc($otype);
6445179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6446179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6447179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6448179f8f40SJoe Perches					$op = "";
6449179f8f40SJoe Perches				}
6450179f8f40SJoe Perches
6451179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6452179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6453179f8f40SJoe Perches
6454179f8f40SJoe Perches## maybe suggesting a correct construct would better
6455179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6456179f8f40SJoe Perches
6457179f8f40SJoe Perches			}
6458179f8f40SJoe Perches		}
6459179f8f40SJoe Perches
64604882720bSThomas Gleixner# check for semaphores initialized locked
64614882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6462000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6463000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6464773647a0SAndy Whitcroft		}
64656712d858SJoe Perches
646667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
646767d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6468000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
646967d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6470773647a0SAndy Whitcroft		}
64716712d858SJoe Perches
6472ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6473f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6474000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6475ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6476f3db6639SMichael Ellerman		}
64776712d858SJoe Perches
64783d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
64793d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
64803d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
64813d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
64823d709ab5SPaul E. McKenney		}
64833d709ab5SPaul E. McKenney
64849189c7e7SJoe Perches# check for deprecated apis
64859189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
64869189c7e7SJoe Perches			my $deprecated_api = $1;
64879189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
64889189c7e7SJoe Perches			WARN("DEPRECATED_API",
64899189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
64909189c7e7SJoe Perches		}
64919189c7e7SJoe Perches
64920f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6493d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
64946903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6495d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6496000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6497d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
64982b6db5cbSAndy Whitcroft		}
6499773647a0SAndy Whitcroft
6500773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6501773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6502773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6503c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6504c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6505171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6506171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6507171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6508773647a0SAndy Whitcroft		{
6509000d1cc1SJoe Perches			WARN("NR_CPUS",
6510000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6511773647a0SAndy Whitcroft		}
65129c9ba34eSAndy Whitcroft
651352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
651452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
651552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
651652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
651752ea8506SJoe Perches		}
651852ea8506SJoe Perches
6519acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
65205b57980dSJoe Perches		if ($perl_version_ok &&
6521acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6522acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6523acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6524acd9362cSJoe Perches		}
6525acd9362cSJoe Perches
6526de3f186fSDenis Efremov# nested likely/unlikely calls
6527de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6528de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6529de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6530de3f186fSDenis Efremov		}
6531de3f186fSDenis Efremov
6532691d77b6SAndy Whitcroft# whine mightly about in_atomic
6533691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6534691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6535000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6536000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6537f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6538000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6539000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6540691d77b6SAndy Whitcroft			}
6541691d77b6SAndy Whitcroft		}
65421704f47bSPeter Zijlstra
65430f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
65440f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
65450f5225b0SPeter Zijlstra			ERROR("LOCKING",
65460f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
65470f5225b0SPeter Zijlstra		}
65480f5225b0SPeter Zijlstra
65491704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
65501704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
65511704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
65521704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
65531704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
65541704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6555000d1cc1SJoe Perches				ERROR("LOCKDEP",
6556000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
65571704f47bSPeter Zijlstra			}
65581704f47bSPeter Zijlstra		}
655988f8831cSDave Jones
6560b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6561b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6562000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6563000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
656488f8831cSDave Jones		}
65652435880fSJoe Perches
656600180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
656700180468SJoe Perches# and whether or not function naming is typical and if
656800180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
65695b57980dSJoe Perches		if ($perl_version_ok &&
657000180468SJoe Perches		    defined $stat &&
657100180468SJoe 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*\)/) {
657200180468SJoe Perches			my $var = $1;
657300180468SJoe Perches			my $perms = $2;
657400180468SJoe Perches			my $show = $3;
657500180468SJoe Perches			my $store = $4;
657600180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
657700180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
657800180468SJoe Perches			    $store =~ /^${var}_store$/ &&
657900180468SJoe Perches			    $octal_perms eq "0644") {
658000180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
658100180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
658200180468SJoe Perches				    $fix) {
658300180468SJoe 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})/;
658400180468SJoe Perches				}
658500180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
658600180468SJoe Perches				 $store =~ /^NULL$/ &&
658700180468SJoe Perches				 $octal_perms eq "0444") {
658800180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
658900180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
659000180468SJoe Perches				    $fix) {
659100180468SJoe 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})/;
659200180468SJoe Perches				}
659300180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
659400180468SJoe Perches				 $store =~ /^${var}_store$/ &&
659500180468SJoe Perches				 $octal_perms eq "0200") {
659600180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
659700180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
659800180468SJoe Perches				    $fix) {
659900180468SJoe 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})/;
660000180468SJoe Perches				}
660100180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
660200180468SJoe Perches				 $octal_perms eq "0444" ||
660300180468SJoe Perches				 $octal_perms eq "0200") {
660400180468SJoe Perches				my $newshow = "$show";
660500180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
660600180468SJoe Perches				my $newstore = $store;
660700180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
660800180468SJoe Perches				my $rename = "";
660900180468SJoe Perches				if ($show ne $newshow) {
661000180468SJoe Perches					$rename .= " '$show' to '$newshow'";
661100180468SJoe Perches				}
661200180468SJoe Perches				if ($store ne $newstore) {
661300180468SJoe Perches					$rename .= " '$store' to '$newstore'";
661400180468SJoe Perches				}
661500180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
661600180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
661700180468SJoe Perches			} else {
661800180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
661900180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
662000180468SJoe Perches			}
662100180468SJoe Perches		}
662200180468SJoe Perches
6623515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6624515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
662573121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
662673121534SJoe Perches#   specific definition of not visible in sysfs.
662773121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
662873121534SJoe Perches#   use the default permissions
66295b57980dSJoe Perches		if ($perl_version_ok &&
6630459cf0aeSJoe Perches		    defined $stat &&
6631515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
66322435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
66332435880fSJoe Perches				my $func = $entry->[0];
66342435880fSJoe Perches				my $arg_pos = $entry->[1];
66352435880fSJoe Perches
6636459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6637459cf0aeSJoe Perches				$lc = $lc + $linenr;
66382a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6639459cf0aeSJoe Perches
66402435880fSJoe Perches				my $skip_args = "";
66412435880fSJoe Perches				if ($arg_pos > 1) {
66422435880fSJoe Perches					$arg_pos--;
66432435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
66442435880fSJoe Perches				}
6645f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6646459cf0aeSJoe Perches				if ($stat =~ /$test/) {
66472435880fSJoe Perches					my $val = $1;
66482435880fSJoe Perches					$val = $6 if ($skip_args ne "");
664973121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
665073121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
665173121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
66522435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6653459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6654f90774e1SJoe Perches					}
6655f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6656c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6657459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
66582435880fSJoe Perches					}
6659459cf0aeSJoe Perches				}
6660459cf0aeSJoe Perches			}
6661459cf0aeSJoe Perches		}
6662459cf0aeSJoe Perches
6663459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6664bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
666500180468SJoe Perches			my $oval = $1;
666600180468SJoe Perches			my $octal = perms_to_octal($oval);
6667f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6668459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6669f90774e1SJoe Perches			    $fix) {
667000180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
66712435880fSJoe Perches			}
667213214adfSAndy Whitcroft		}
66735a6d20ceSBjorn Andersson
66745a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
66755a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
66765a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
66775a6d20ceSBjorn Andersson			my $valid_licenses = qr{
66785a6d20ceSBjorn Andersson						GPL|
66795a6d20ceSBjorn Andersson						GPL\ v2|
66805a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
66815a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
66825a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
66835a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
66845a6d20ceSBjorn Andersson						Proprietary
66855a6d20ceSBjorn Andersson					}x;
66865a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
66875a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
66885a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
66895a6d20ceSBjorn Andersson			}
66905a6d20ceSBjorn Andersson		}
66916a8d76cbSMatteo Croce
66926a8d76cbSMatteo Croce# check for sysctl duplicate constants
66936a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
66946a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
66956a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
66966a8d76cbSMatteo Croce		}
6697515a235eSJoe Perches	}
669813214adfSAndy Whitcroft
669913214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
670013214adfSAndy Whitcroft	# so just keep quiet.
670113214adfSAndy Whitcroft	if ($#rawlines == -1) {
670213214adfSAndy Whitcroft		exit(0);
67030a920b5bSAndy Whitcroft	}
67040a920b5bSAndy Whitcroft
67058905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
67068905a67cSAndy Whitcroft	# things that appear to be patches.
67078905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
67088905a67cSAndy Whitcroft		exit(0);
67098905a67cSAndy Whitcroft	}
67108905a67cSAndy Whitcroft
67118905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
67128905a67cSAndy Whitcroft	# just keep quiet.
67138905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
67148905a67cSAndy Whitcroft		exit(0);
67158905a67cSAndy Whitcroft	}
67168905a67cSAndy Whitcroft
6717a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6718000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6719000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
67200a920b5bSAndy Whitcroft	}
6721cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6722cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6723000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6724000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6725cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6726cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6727cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6728cd261496SGeert Uytterhoeven		}
67290a920b5bSAndy Whitcroft	}
67300a920b5bSAndy Whitcroft
6731f0a594c1SAndy Whitcroft	print report_dump();
673213214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
673313214adfSAndy Whitcroft		print "$filename " if ($summary_file);
67346c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
67356c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
67366c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
67376c72ffaaSAndy Whitcroft	}
67388905a67cSAndy Whitcroft
6739d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6740ef212196SJoe Perches		# If there were any defects found and not already fixing them
6741ef212196SJoe Perches		if (!$clean and !$fix) {
6742ef212196SJoe Perches			print << "EOM"
6743ef212196SJoe Perches
6744ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6745ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6746ef212196SJoe PerchesEOM
6747ef212196SJoe Perches		}
6748d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6749d2c0a235SAndy Whitcroft		# then suggest that.
6750d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6751b0781216SMike Frysinger			$rpt_cleaners = 0;
6752d8469f16SJoe Perches			print << "EOM"
6753d8469f16SJoe Perches
6754d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6755d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6756d8469f16SJoe PerchesEOM
6757d2c0a235SAndy Whitcroft		}
6758d2c0a235SAndy Whitcroft	}
6759d2c0a235SAndy Whitcroft
6760d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6761d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6762d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
67639624b8d6SJoe Perches		my $newfile = $filename;
67649624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
67653705ce5bSJoe Perches		my $linecount = 0;
67663705ce5bSJoe Perches		my $f;
67673705ce5bSJoe Perches
6768d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6769d752fcc8SJoe Perches
67703705ce5bSJoe Perches		open($f, '>', $newfile)
67713705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
67723705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
67733705ce5bSJoe Perches			$linecount++;
67743705ce5bSJoe Perches			if ($file) {
67753705ce5bSJoe Perches				if ($linecount > 3) {
67763705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
67773705ce5bSJoe Perches					print $f $fixed_line . "\n";
67783705ce5bSJoe Perches				}
67793705ce5bSJoe Perches			} else {
67803705ce5bSJoe Perches				print $f $fixed_line . "\n";
67813705ce5bSJoe Perches			}
67823705ce5bSJoe Perches		}
67833705ce5bSJoe Perches		close($f);
67843705ce5bSJoe Perches
67853705ce5bSJoe Perches		if (!$quiet) {
67863705ce5bSJoe Perches			print << "EOM";
6787d8469f16SJoe Perches
67883705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
67893705ce5bSJoe Perches
67903705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
67913705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
67923705ce5bSJoe Perches
67933705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
67943705ce5bSJoe PerchesNo warranties, expressed or implied...
67953705ce5bSJoe PerchesEOM
67963705ce5bSJoe Perches		}
67973705ce5bSJoe Perches	}
67983705ce5bSJoe Perches
6799d8469f16SJoe Perches	if ($quiet == 0) {
6800d8469f16SJoe Perches		print "\n";
6801d8469f16SJoe Perches		if ($clean == 1) {
6802d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6803d8469f16SJoe Perches		} else {
6804d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
68050a920b5bSAndy Whitcroft		}
68060a920b5bSAndy Whitcroft	}
68070a920b5bSAndy Whitcroft	return $clean;
68080a920b5bSAndy Whitcroft}
6809