xref: /linux-6.15/scripts/checkpatch.pl (revision d499480c)
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";
64dadf680dSJoe Perchesmy $allow_c99_comments = 1;
6577f5b10aSHannes Eder
6677f5b10aSHannes Edersub help {
6777f5b10aSHannes Eder	my ($exitcode) = @_;
6877f5b10aSHannes Eder
6977f5b10aSHannes Eder	print << "EOM";
7077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7177f5b10aSHannes EderVersion: $V
7277f5b10aSHannes Eder
7377f5b10aSHannes EderOptions:
7477f5b10aSHannes Eder  -q, --quiet                quiet
7577f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7677f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7777f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
7877f5b10aSHannes Eder  --emacs                    emacs compile window format
7977f5b10aSHannes Eder  --terse                    one line per report
8034d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
814a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
824a593c34SDu, Changbin                             single git commit with:
834a593c34SDu, Changbin                               <rev>
844a593c34SDu, Changbin                               <rev>^
854a593c34SDu, Changbin                               <rev>~n
864a593c34SDu, Changbin                             multiple git commits with:
874a593c34SDu, Changbin                               <rev1>..<rev2>
884a593c34SDu, Changbin                               <rev1>...<rev2>
894a593c34SDu, Changbin                               <rev>-<count>
904a593c34SDu, Changbin                             git merges are ignored
9177f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9277f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
933beb42ecSJoe Perches  --list-types               list the possible message types
9491bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
95000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
963beb42ecSJoe Perches  --show-types               show the specific message type in the output
976cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
9856193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
9977f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10077f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10177f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10277f5b10aSHannes Eder  --summary-file             include the filename in summary
10377f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
10477f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10577f5b10aSHannes Eder                             is all off)
10677f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10777f5b10aSHannes Eder                             literally
1083705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1093705ce5bSJoe Perches                             If correctable single-line errors exist, create
1103705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1113705ce5bSJoe Perches                             with potential errors corrected to the preferred
1123705ce5bSJoe Perches                             checkpatch style
1139624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1149624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1159624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
116d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
117d62a201fSDave Hansen                             runtime errors.
118ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
119f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
120ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12175ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
122737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
123737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
12477f5b10aSHannes Eder  -h, --help, --version      display this help and exit
12577f5b10aSHannes Eder
12677f5b10aSHannes EderWhen FILE is - read standard input.
12777f5b10aSHannes EderEOM
12877f5b10aSHannes Eder
12977f5b10aSHannes Eder	exit($exitcode);
13077f5b10aSHannes Eder}
13177f5b10aSHannes Eder
1323beb42ecSJoe Perchessub uniq {
1333beb42ecSJoe Perches	my %seen;
1343beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1353beb42ecSJoe Perches}
1363beb42ecSJoe Perches
1373beb42ecSJoe Perchessub list_types {
1383beb42ecSJoe Perches	my ($exitcode) = @_;
1393beb42ecSJoe Perches
1403beb42ecSJoe Perches	my $count = 0;
1413beb42ecSJoe Perches
1423beb42ecSJoe Perches	local $/ = undef;
1433beb42ecSJoe Perches
1443beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1453beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1463beb42ecSJoe Perches
1473beb42ecSJoe Perches	my $text = <$script>;
1483beb42ecSJoe Perches	close($script);
1493beb42ecSJoe Perches
1503beb42ecSJoe Perches	my @types = ();
1510547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1520547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1533beb42ecSJoe Perches		push (@types, $_);
1543beb42ecSJoe Perches	}
1553beb42ecSJoe Perches	@types = sort(uniq(@types));
1563beb42ecSJoe Perches	print("#\tMessage type\n\n");
1573beb42ecSJoe Perches	foreach my $type (@types) {
1583beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1593beb42ecSJoe Perches	}
1603beb42ecSJoe Perches
1613beb42ecSJoe Perches	exit($exitcode);
1623beb42ecSJoe Perches}
1633beb42ecSJoe Perches
164000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
165000d1cc1SJoe Perchesif (-f $conf) {
166000d1cc1SJoe Perches	my @conf_args;
167000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
168000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
169000d1cc1SJoe Perches
170000d1cc1SJoe Perches	while (<$conffile>) {
171000d1cc1SJoe Perches		my $line = $_;
172000d1cc1SJoe Perches
173000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
174000d1cc1SJoe Perches		$line =~ s/^\s*//g;
175000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
176000d1cc1SJoe Perches
177000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
178000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
179000d1cc1SJoe Perches
180000d1cc1SJoe Perches		my @words = split(" ", $line);
181000d1cc1SJoe Perches		foreach my $word (@words) {
182000d1cc1SJoe Perches			last if ($word =~ m/^#/);
183000d1cc1SJoe Perches			push (@conf_args, $word);
184000d1cc1SJoe Perches		}
185000d1cc1SJoe Perches	}
186000d1cc1SJoe Perches	close($conffile);
187000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
188000d1cc1SJoe Perches}
189000d1cc1SJoe Perches
190737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
191737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
192737c0767SJohn Brooksforeach (@ARGV) {
193737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
194737c0767SJohn Brooks		$_ = "--color=$color";
195737c0767SJohn Brooks	}
196737c0767SJohn Brooks}
197737c0767SJohn Brooks
1980a920b5bSAndy WhitcroftGetOptions(
1996c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2000a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2010a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2020a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2036c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2048905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20534d8815fSJoe Perches	'showfile!'	=> \$showfile,
20677f5b10aSHannes Eder	'f|file!'	=> \$file,
2074a593c34SDu, Changbin	'g|git!'	=> \$git,
2086c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2096c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
210000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
21191bfe484SJoe Perches	'types=s'	=> \@use,
212000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2133beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2146cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21556193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2166c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2178905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2188905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
21913214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2203705ce5bSJoe Perches	'fix!'		=> \$fix,
2219624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
222d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
223c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
224773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
225ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
226ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
22775ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
228737c0767SJohn Brooks	'color=s'	=> \$color,
229737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
230737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
23177f5b10aSHannes Eder	'h|help'	=> \$help,
23277f5b10aSHannes Eder	'version'	=> \$help
23377f5b10aSHannes Eder) or help(1);
23477f5b10aSHannes Eder
23577f5b10aSHannes Ederhelp(0) if ($help);
2360a920b5bSAndy Whitcroft
2373beb42ecSJoe Percheslist_types(0) if ($list_types);
2383beb42ecSJoe Perches
2399624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2402ac73b4fSJoe Perches$check_orig = $check;
2419624b8d6SJoe Perches
2420a920b5bSAndy Whitcroftmy $exit = 0;
2430a920b5bSAndy Whitcroft
2445b57980dSJoe Perchesmy $perl_version_ok = 1;
245d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2465b57980dSJoe Perches	$perl_version_ok = 0;
247d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2485b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
249d62a201fSDave Hansen}
250d62a201fSDave Hansen
25145107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2520a920b5bSAndy Whitcroftif ($#ARGV < 0) {
25345107ff6SAllen Hubbe	push(@ARGV, '-');
2540a920b5bSAndy Whitcroft}
2550a920b5bSAndy Whitcroft
256737c0767SJohn Brooksif ($color =~ /^[01]$/) {
257737c0767SJohn Brooks	$color = !$color;
258737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
259737c0767SJohn Brooks	$color = 1;
260737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
261737c0767SJohn Brooks	$color = 0;
262737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
263737c0767SJohn Brooks	$color = (-t STDOUT);
264737c0767SJohn Brooks} else {
265737c0767SJohn Brooks	die "Invalid color mode: $color\n";
266737c0767SJohn Brooks}
267737c0767SJohn Brooks
26891bfe484SJoe Perchessub hash_save_array_words {
26991bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
27091bfe484SJoe Perches
27191bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
27291bfe484SJoe Perches	foreach my $word (@array) {
273000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
274000d1cc1SJoe Perches		$word =~ s/^\s*//g;
275000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
276000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
277000d1cc1SJoe Perches
278000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
279000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
280000d1cc1SJoe Perches
28191bfe484SJoe Perches		$hashRef->{$word}++;
282000d1cc1SJoe Perches	}
28391bfe484SJoe Perches}
28491bfe484SJoe Perches
28591bfe484SJoe Perchessub hash_show_words {
28691bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
28791bfe484SJoe Perches
2883c816e49SJoe Perches	if (keys %$hashRef) {
289d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
29058cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
29191bfe484SJoe Perches			print " $word";
29291bfe484SJoe Perches		}
293d8469f16SJoe Perches		print "\n";
29491bfe484SJoe Perches	}
29591bfe484SJoe Perches}
29691bfe484SJoe Perches
29791bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
29891bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
299000d1cc1SJoe Perches
300c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
301c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3027429c690SAndy Whitcroftmy $dbg_type = 0;
303a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
304c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
30521caa13cSAndy Whitcroft	## no critic
30621caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
30721caa13cSAndy Whitcroft	die "$@" if ($@);
308c2fdda0dSAndy Whitcroft}
309c2fdda0dSAndy Whitcroft
310d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
311d2c0a235SAndy Whitcroft
3128905a67cSAndy Whitcroftif ($terse) {
3138905a67cSAndy Whitcroft	$emacs = 1;
3148905a67cSAndy Whitcroft	$quiet++;
3158905a67cSAndy Whitcroft}
3168905a67cSAndy Whitcroft
3176c72ffaaSAndy Whitcroftif ($tree) {
3186c72ffaaSAndy Whitcroft	if (defined $root) {
3196c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3206c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3216c72ffaaSAndy Whitcroft		}
3226c72ffaaSAndy Whitcroft	} else {
3236c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3246c72ffaaSAndy Whitcroft			$root = '.';
3256c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3266c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3276c72ffaaSAndy Whitcroft			$root = $1;
3286c72ffaaSAndy Whitcroft		}
3296c72ffaaSAndy Whitcroft	}
3306c72ffaaSAndy Whitcroft
3316c72ffaaSAndy Whitcroft	if (!defined $root) {
3320a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3330a920b5bSAndy Whitcroft		exit(2);
3340a920b5bSAndy Whitcroft	}
3356c72ffaaSAndy Whitcroft}
3366c72ffaaSAndy Whitcroft
3376c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3386c72ffaaSAndy Whitcroft
3392ceb532bSAndy Whitcroftour $Ident	= qr{
3402ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3412ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3422ceb532bSAndy Whitcroft		}x;
3436c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3446c72ffaaSAndy Whitcroftour $Sparse	= qr{
3456c72ffaaSAndy Whitcroft			__user|
3466c72ffaaSAndy Whitcroft			__kernel|
3476c72ffaaSAndy Whitcroft			__force|
3486c72ffaaSAndy Whitcroft			__iomem|
3496c72ffaaSAndy Whitcroft			__must_check|
350417495edSAndy Whitcroft			__kprobes|
351165e72a6SSven Eckelmann			__ref|
35233aa4597SGeert Uytterhoeven			__refconst|
35333aa4597SGeert Uytterhoeven			__refdata|
354ad315455SBoqun Feng			__rcu|
355ad315455SBoqun Feng			__private
3566c72ffaaSAndy Whitcroft		}x;
357e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
358e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
359e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
360e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
361e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3628716de38SJoe Perches
36352131292SWolfram Sang# Notes to $Attribute:
36452131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3656c72ffaaSAndy Whitcroftour $Attribute	= qr{
3666c72ffaaSAndy Whitcroft			const|
36703f1df7dSJoe Perches			__percpu|
36803f1df7dSJoe Perches			__nocast|
36903f1df7dSJoe Perches			__safe|
37046d832f5SMichael S. Tsirkin			__bitwise|
37103f1df7dSJoe Perches			__packed__|
37203f1df7dSJoe Perches			__packed2__|
37303f1df7dSJoe Perches			__naked|
37403f1df7dSJoe Perches			__maybe_unused|
37503f1df7dSJoe Perches			__always_unused|
37603f1df7dSJoe Perches			__noreturn|
37703f1df7dSJoe Perches			__used|
37803f1df7dSJoe Perches			__cold|
379e23ef1f3SJoe Perches			__pure|
38003f1df7dSJoe Perches			__noclone|
38103f1df7dSJoe Perches			__deprecated|
3826c72ffaaSAndy Whitcroft			__read_mostly|
383c5967e98SJoe Perches			__ro_after_init|
3846c72ffaaSAndy Whitcroft			__kprobes|
3858716de38SJoe Perches			$InitAttribute|
38624e1d81aSAndy Whitcroft			____cacheline_aligned|
38724e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3885fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3895fe3af11SAndy Whitcroft			__weak
3906c72ffaaSAndy Whitcroft		  }x;
391c45dcabdSAndy Whitcroftour $Modifier;
39291cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3936c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3946c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3956c72ffaaSAndy Whitcroft
39695e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
39795e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
39895e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
39995e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4002435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
401c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
402326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
403326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
404326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
40574349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4062435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
407326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
408447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
40923f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4106c72ffaaSAndy Whitcroftour $Operators	= qr{
4116c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4126c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
41323f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4146c72ffaaSAndy Whitcroft		  }x;
4156c72ffaaSAndy Whitcroft
41691cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
41791cb5195SJoe Perches
418ab7e23f3SJoe Perchesour $BasicType;
4198905a67cSAndy Whitcroftour $NonptrType;
4201813087dSJoe Perchesour $NonptrTypeMisordered;
4218716de38SJoe Perchesour $NonptrTypeWithAttr;
4228905a67cSAndy Whitcroftour $Type;
4231813087dSJoe Perchesour $TypeMisordered;
4248905a67cSAndy Whitcroftour $Declare;
4251813087dSJoe Perchesour $DeclareMisordered;
4268905a67cSAndy Whitcroft
42715662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
42815662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
429171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
430171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
431171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
432171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
433171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
434171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
435171ae1a4SAndy Whitcroft}x;
436171ae1a4SAndy Whitcroft
43715662b3eSJoe Perchesour $UTF8	= qr{
43815662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
43915662b3eSJoe Perches	| $NON_ASCII_UTF8
44015662b3eSJoe Perches}x;
44115662b3eSJoe Perches
442e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
443021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
444021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
445021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
446021158b4SJoe Perches)};
447e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
448fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4498ed22cadSAndy Whitcroft	atomic_t
4508ed22cadSAndy Whitcroft)};
451e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
452e6176fa4SJoe Perches	$typeC99Typedefs\b|
453e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
454e6176fa4SJoe Perches	$typeKernelTypedefs\b
455e6176fa4SJoe Perches)};
4568ed22cadSAndy Whitcroft
4576d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4586d32f7a3SJoe Perches
459691e669bSJoe Perchesour $logFunctions = qr{(?x:
460758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4617d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
46287bd499aSJoe Perches	TP_printk|
4636e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
464b0531722SJoe Perches	panic|
46506668727SJoe Perches	MODULE_[A-Z_]+|
46606668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
467691e669bSJoe Perches)};
468691e669bSJoe Perches
46920112475SJoe Perchesour $signature_tags = qr{(?xi:
47020112475SJoe Perches	Signed-off-by:|
471*d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
47220112475SJoe Perches	Acked-by:|
47320112475SJoe Perches	Tested-by:|
47420112475SJoe Perches	Reviewed-by:|
47520112475SJoe Perches	Reported-by:|
4768543ae12SMugunthan V N	Suggested-by:|
47720112475SJoe Perches	To:|
47820112475SJoe Perches	Cc:
47920112475SJoe Perches)};
48020112475SJoe Perches
4811813087dSJoe Perchesour @typeListMisordered = (
4821813087dSJoe Perches	qr{char\s+(?:un)?signed},
4831813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4841813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4851813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4861813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4871813087dSJoe Perches	qr{short\s+(?:un)?signed},
4881813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4891813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4901813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4911813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4921813087dSJoe Perches	qr{int\s+(?:un)?signed},
4931813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4941813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4951813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4961813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4971813087dSJoe Perches	qr{long\s+(?:un)?signed},
4981813087dSJoe Perches);
4991813087dSJoe Perches
5008905a67cSAndy Whitcroftour @typeList = (
5018905a67cSAndy Whitcroft	qr{void},
5020c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5030c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5040c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5050c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5060c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5070c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5080c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5090c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5100c773d9dSJoe Perches	qr{(?:un)?signed},
5118905a67cSAndy Whitcroft	qr{float},
5128905a67cSAndy Whitcroft	qr{double},
5138905a67cSAndy Whitcroft	qr{bool},
5148905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5158905a67cSAndy Whitcroft	qr{union\s+$Ident},
5168905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5178905a67cSAndy Whitcroft	qr{${Ident}_t},
5188905a67cSAndy Whitcroft	qr{${Ident}_handler},
5198905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5201813087dSJoe Perches	@typeListMisordered,
5218905a67cSAndy Whitcroft);
522938224b5SJoe Perches
523938224b5SJoe Perchesour $C90_int_types = qr{(?x:
524938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
525938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
526938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
527938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
528938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
529938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
530938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
531938224b5SJoe Perches
532938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
533938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
534938224b5SJoe Perches	long\s+(?:un)?signed|
535938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
536938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
537938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
538938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
539938224b5SJoe Perches
540938224b5SJoe Perches	int\s+(?:un)?signed|
541938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
542938224b5SJoe Perches)};
543938224b5SJoe Perches
544485ff23eSAlex Dowadour @typeListFile = ();
5458716de38SJoe Perchesour @typeListWithAttr = (
5468716de38SJoe Perches	@typeList,
5478716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5488716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5498716de38SJoe Perches);
5508716de38SJoe Perches
551c45dcabdSAndy Whitcroftour @modifierList = (
552c45dcabdSAndy Whitcroft	qr{fastcall},
553c45dcabdSAndy Whitcroft);
554485ff23eSAlex Dowadour @modifierListFile = ();
5558905a67cSAndy Whitcroft
5562435880fSJoe Perchesour @mode_permission_funcs = (
5572435880fSJoe Perches	["module_param", 3],
5582435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5592435880fSJoe Perches	["module_param_array_named", 5],
5602435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5612435880fSJoe Perches	["proc_create(?:_data|)", 2],
562459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
563459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
564459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
565459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
566459cf0aeSJoe Perches	["__ATTR", 2],
5672435880fSJoe Perches);
5682435880fSJoe Perches
569515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
570515a235eSJoe Perchesour $mode_perms_search = "";
571515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
572515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
573515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
574515a235eSJoe Perches}
57500180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
576515a235eSJoe Perches
5779189c7e7SJoe Perchesour %deprecated_apis = (
5789189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
5799189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
5809189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
5819189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
5829189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
5839189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
5849189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
5859189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
5869189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
5879189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
5889189c7e7SJoe Perches);
5899189c7e7SJoe Perches
5909189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
5919189c7e7SJoe Perchesour $deprecated_apis_search = "";
5929189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
5939189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
5949189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
5959189c7e7SJoe Perches}
5969189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
5979189c7e7SJoe Perches
598b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
599b392c64fSJoe Perches	S_IWUGO		|
600b392c64fSJoe Perches	S_IWOTH		|
601b392c64fSJoe Perches	S_IRWXUGO	|
602b392c64fSJoe Perches	S_IALLUGO	|
603b392c64fSJoe Perches	0[0-7][0-7][2367]
604b392c64fSJoe Perches}x;
605b392c64fSJoe Perches
606f90774e1SJoe Perchesour %mode_permission_string_types = (
607f90774e1SJoe Perches	"S_IRWXU" => 0700,
608f90774e1SJoe Perches	"S_IRUSR" => 0400,
609f90774e1SJoe Perches	"S_IWUSR" => 0200,
610f90774e1SJoe Perches	"S_IXUSR" => 0100,
611f90774e1SJoe Perches	"S_IRWXG" => 0070,
612f90774e1SJoe Perches	"S_IRGRP" => 0040,
613f90774e1SJoe Perches	"S_IWGRP" => 0020,
614f90774e1SJoe Perches	"S_IXGRP" => 0010,
615f90774e1SJoe Perches	"S_IRWXO" => 0007,
616f90774e1SJoe Perches	"S_IROTH" => 0004,
617f90774e1SJoe Perches	"S_IWOTH" => 0002,
618f90774e1SJoe Perches	"S_IXOTH" => 0001,
619f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
620f90774e1SJoe Perches	"S_IRUGO" => 0444,
621f90774e1SJoe Perches	"S_IWUGO" => 0222,
622f90774e1SJoe Perches	"S_IXUGO" => 0111,
623f90774e1SJoe Perches);
624f90774e1SJoe Perches
625f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
626f90774e1SJoe Perchesour $mode_perms_string_search = "";
627f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
628f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
629f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
630f90774e1SJoe Perches}
63100180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
63200180468SJoe Perchesour $multi_mode_perms_string_search = qr{
63300180468SJoe Perches	${single_mode_perms_string_search}
63400180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
63500180468SJoe Perches}x;
63600180468SJoe Perches
63700180468SJoe Perchessub perms_to_octal {
63800180468SJoe Perches	my ($string) = @_;
63900180468SJoe Perches
64000180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
64100180468SJoe Perches
64200180468SJoe Perches	my $val = "";
64300180468SJoe Perches	my $oval = "";
64400180468SJoe Perches	my $to = 0;
64500180468SJoe Perches	my $curpos = 0;
64600180468SJoe Perches	my $lastpos = 0;
64700180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
64800180468SJoe Perches		$curpos = pos($string);
64900180468SJoe Perches		my $match = $2;
65000180468SJoe Perches		my $omatch = $1;
65100180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
65200180468SJoe Perches		$lastpos = $curpos;
65300180468SJoe Perches		$to |= $mode_permission_string_types{$match};
65400180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
65500180468SJoe Perches		$val .= $match;
65600180468SJoe Perches		$oval .= $omatch;
65700180468SJoe Perches	}
65800180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
65900180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
66000180468SJoe Perches	return sprintf("%04o", $to);
66100180468SJoe Perches}
662f90774e1SJoe Perches
6637840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6647840a94cSWolfram Sang	irq|
665cdcee686SSergey Ryazanov	memory|
666cdcee686SSergey Ryazanov	time|
667cdcee686SSergey Ryazanov	reboot
6687840a94cSWolfram Sang)};
6697840a94cSWolfram Sang# memory.h: ARM has a custom one
6707840a94cSWolfram Sang
67166b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
67266b47b4aSKees Cookmy $misspellings;
67366b47b4aSKees Cookmy %spelling_fix;
67436061e38SJoe Perches
67536061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
67666b47b4aSKees Cook	while (<$spelling>) {
67766b47b4aSKees Cook		my $line = $_;
67866b47b4aSKees Cook
67966b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
68066b47b4aSKees Cook		$line =~ s/^\s*//g;
68166b47b4aSKees Cook
68266b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
68366b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
68466b47b4aSKees Cook
68566b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
68666b47b4aSKees Cook
68766b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
68866b47b4aSKees Cook	}
68966b47b4aSKees Cook	close($spelling);
69036061e38SJoe Perches} else {
69136061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
69236061e38SJoe Perches}
69366b47b4aSKees Cook
694ebfd7d62SJoe Perchesif ($codespell) {
695ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
696ebfd7d62SJoe Perches		while (<$spelling>) {
697ebfd7d62SJoe Perches			my $line = $_;
698ebfd7d62SJoe Perches
699ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
700ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
701ebfd7d62SJoe Perches
702ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
703ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
704ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
705ebfd7d62SJoe Perches
706ebfd7d62SJoe Perches			$line =~ s/,.*$//;
707ebfd7d62SJoe Perches
708ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
709ebfd7d62SJoe Perches
710ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
711ebfd7d62SJoe Perches		}
712ebfd7d62SJoe Perches		close($spelling);
713ebfd7d62SJoe Perches	} else {
714ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
715ebfd7d62SJoe Perches	}
716ebfd7d62SJoe Perches}
717ebfd7d62SJoe Perches
718ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
719ebfd7d62SJoe Perches
72075ad8c57SJerome Forissiersub read_words {
72175ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
72275ad8c57SJerome Forissier
72375ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
72475ad8c57SJerome Forissier		while (<$words>) {
725bf1fa1daSJoe Perches			my $line = $_;
726bf1fa1daSJoe Perches
727bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
728bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
729bf1fa1daSJoe Perches
730bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
731bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
732bf1fa1daSJoe Perches			if ($line =~ /\s/) {
73375ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
734bf1fa1daSJoe Perches				next;
735bf1fa1daSJoe Perches			}
736bf1fa1daSJoe Perches
73775ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
73875ad8c57SJerome Forissier			$$wordsRef .= $line;
739bf1fa1daSJoe Perches		}
74075ad8c57SJerome Forissier		close($file);
74175ad8c57SJerome Forissier		return 1;
742bf1fa1daSJoe Perches	}
743bf1fa1daSJoe Perches
74475ad8c57SJerome Forissier	return 0;
74575ad8c57SJerome Forissier}
74675ad8c57SJerome Forissier
74775ad8c57SJerome Forissiermy $const_structs = "";
74875ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
74975ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
75075ad8c57SJerome Forissier
75175ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
75275ad8c57SJerome Forissierif (length($typedefsfile)) {
75375ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
75475ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
75575ad8c57SJerome Forissier}
75675ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
75775ad8c57SJerome Forissier
7588905a67cSAndy Whitcroftsub build_types {
759485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
760485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7611813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7628716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
763c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
764ab7e23f3SJoe Perches	$BasicType	= qr{
765ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
766ab7e23f3SJoe Perches				(?:${all}\b)
767ab7e23f3SJoe Perches		}x;
7688905a67cSAndy Whitcroft	$NonptrType	= qr{
769d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
770cf655043SAndy Whitcroft			(?:
7716b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7728ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
773c45dcabdSAndy Whitcroft				(?:${all}\b)
774cf655043SAndy Whitcroft			)
775c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7768905a67cSAndy Whitcroft		  }x;
7771813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7781813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7791813087dSJoe Perches			(?:
7801813087dSJoe Perches				(?:${Misordered}\b)
7811813087dSJoe Perches			)
7821813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7831813087dSJoe Perches		  }x;
7848716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7858716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7868716de38SJoe Perches			(?:
7878716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
7888716de38SJoe Perches				(?:$typeTypedefs\b)|
7898716de38SJoe Perches				(?:${allWithAttr}\b)
7908716de38SJoe Perches			)
7918716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
7928716de38SJoe Perches		  }x;
7938905a67cSAndy Whitcroft	$Type	= qr{
794c45dcabdSAndy Whitcroft			$NonptrType
7951574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
796c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
7978905a67cSAndy Whitcroft		  }x;
7981813087dSJoe Perches	$TypeMisordered	= qr{
7991813087dSJoe Perches			$NonptrTypeMisordered
8001813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
8011813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8021813087dSJoe Perches		  }x;
80391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8041813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8058905a67cSAndy Whitcroft}
8068905a67cSAndy Whitcroftbuild_types();
8076c72ffaaSAndy Whitcroft
8087d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
809d1fe9c09SJoe Perches
810d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
811d1fe9c09SJoe Perches# requires at least perl version v5.10.0
812d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
813d1fe9c09SJoe Perches
814d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8152435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
816c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8177d2367afSJoe Perches
818f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8193e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
820fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8213d102fc0SGilad Ben-Yossef	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
8223d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
823f8422308SJoe Perches)};
824f8422308SJoe Perches
8257d2367afSJoe Perchessub deparenthesize {
8267d2367afSJoe Perches	my ($string) = @_;
8277d2367afSJoe Perches	return "" if (!defined($string));
8285b9553abSJoe Perches
8295b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8305b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8315b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8325b9553abSJoe Perches	}
8335b9553abSJoe Perches
8347d2367afSJoe Perches	$string =~ s@\s+@ @g;
8355b9553abSJoe Perches
8367d2367afSJoe Perches	return $string;
8377d2367afSJoe Perches}
8387d2367afSJoe Perches
8393445686aSJoe Perchessub seed_camelcase_file {
8403445686aSJoe Perches	my ($file) = @_;
8413445686aSJoe Perches
8423445686aSJoe Perches	return if (!(-f $file));
8433445686aSJoe Perches
8443445686aSJoe Perches	local $/;
8453445686aSJoe Perches
8463445686aSJoe Perches	open(my $include_file, '<', "$file")
8473445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8483445686aSJoe Perches	my $text = <$include_file>;
8493445686aSJoe Perches	close($include_file);
8503445686aSJoe Perches
8513445686aSJoe Perches	my @lines = split('\n', $text);
8523445686aSJoe Perches
8533445686aSJoe Perches	foreach my $line (@lines) {
8543445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8553445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8563445686aSJoe Perches			$camelcase{$1} = 1;
85711ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
85811ea516aSJoe Perches			$camelcase{$1} = 1;
85911ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8603445686aSJoe Perches			$camelcase{$1} = 1;
8613445686aSJoe Perches		}
8623445686aSJoe Perches	}
8633445686aSJoe Perches}
8643445686aSJoe Perches
86585b0ee18SJoe Perchessub is_maintained_obsolete {
86685b0ee18SJoe Perches	my ($filename) = @_;
86785b0ee18SJoe Perches
868f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
86985b0ee18SJoe Perches
8700616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
87185b0ee18SJoe Perches
87285b0ee18SJoe Perches	return $status =~ /obsolete/i;
87385b0ee18SJoe Perches}
87485b0ee18SJoe Perches
8753b6e8ac9SJoe Perchessub is_SPDX_License_valid {
8763b6e8ac9SJoe Perches	my ($license) = @_;
8773b6e8ac9SJoe Perches
87856294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
8793b6e8ac9SJoe Perches
88056294112SJoe Perches	my $root_path = abs_path($root);
88156294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
8823b6e8ac9SJoe Perches	return 0 if ($status ne "");
8833b6e8ac9SJoe Perches	return 1;
8843b6e8ac9SJoe Perches}
8853b6e8ac9SJoe Perches
8863445686aSJoe Perchesmy $camelcase_seeded = 0;
8873445686aSJoe Perchessub seed_camelcase_includes {
8883445686aSJoe Perches	return if ($camelcase_seeded);
8893445686aSJoe Perches
8903445686aSJoe Perches	my $files;
891c707a81dSJoe Perches	my $camelcase_cache = "";
892c707a81dSJoe Perches	my @include_files = ();
893c707a81dSJoe Perches
894c707a81dSJoe Perches	$camelcase_seeded = 1;
895351b2a1fSJoe Perches
8963645e328SRichard Genoud	if (-e ".git") {
897351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
898351b2a1fSJoe Perches		chomp $git_last_include_commit;
899c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
900c707a81dSJoe Perches	} else {
901c707a81dSJoe Perches		my $last_mod_date = 0;
902c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
903c707a81dSJoe Perches		@include_files = split('\n', $files);
904c707a81dSJoe Perches		foreach my $file (@include_files) {
905c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
906c707a81dSJoe Perches						   localtime((stat $file)[9]));
907c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
908c707a81dSJoe Perches		}
909c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
910c707a81dSJoe Perches	}
911c707a81dSJoe Perches
912c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
913c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
914c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
915351b2a1fSJoe Perches		while (<$camelcase_file>) {
916351b2a1fSJoe Perches			chomp;
917351b2a1fSJoe Perches			$camelcase{$_} = 1;
918351b2a1fSJoe Perches		}
919351b2a1fSJoe Perches		close($camelcase_file);
920351b2a1fSJoe Perches
921351b2a1fSJoe Perches		return;
922351b2a1fSJoe Perches	}
923c707a81dSJoe Perches
9243645e328SRichard Genoud	if (-e ".git") {
925c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
926c707a81dSJoe Perches		@include_files = split('\n', $files);
9273445686aSJoe Perches	}
928c707a81dSJoe Perches
9293445686aSJoe Perches	foreach my $file (@include_files) {
9303445686aSJoe Perches		seed_camelcase_file($file);
9313445686aSJoe Perches	}
932351b2a1fSJoe Perches
933c707a81dSJoe Perches	if ($camelcase_cache ne "") {
934351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
935c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
936c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
937351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
938351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
939351b2a1fSJoe Perches		}
940351b2a1fSJoe Perches		close($camelcase_file);
941351b2a1fSJoe Perches	}
9423445686aSJoe Perches}
9433445686aSJoe Perches
944d311cd44SJoe Perchessub git_commit_info {
945d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
946d311cd44SJoe Perches
947d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
948d311cd44SJoe Perches
949d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
950d311cd44SJoe Perches	$output =~ s/^\s*//gm;
951d311cd44SJoe Perches	my @lines = split("\n", $output);
952d311cd44SJoe Perches
9530d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9540d7835fcSJoe Perches
955d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
956d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
957d311cd44SJoe Perches# all matching commit ids, but it's very slow...
958d311cd44SJoe Perches#
959d311cd44SJoe Perches#		echo "checking commits $1..."
960d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
961d311cd44SJoe Perches#		while read line ; do
962d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
963d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
964d311cd44SJoe Perches#		done
965d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
966948b133aSHeinrich Schuchardt		$id = undef;
967d311cd44SJoe Perches	} else {
968d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
969d311cd44SJoe Perches		$desc = substr($lines[0], 41);
970d311cd44SJoe Perches	}
971d311cd44SJoe Perches
972d311cd44SJoe Perches	return ($id, $desc);
973d311cd44SJoe Perches}
974d311cd44SJoe Perches
9756c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9760a920b5bSAndy Whitcroft
97700df344fSAndy Whitcroftmy @rawlines = ();
978c2fdda0dSAndy Whitcroftmy @lines = ();
9793705ce5bSJoe Perchesmy @fixed = ();
980d752fcc8SJoe Perchesmy @fixed_inserted = ();
981d752fcc8SJoe Perchesmy @fixed_deleted = ();
982194f66fcSJoe Perchesmy $fixlinenr = -1;
983194f66fcSJoe Perches
9844a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9854a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9864a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9874a593c34SDu, Changbin
9884a593c34SDu, Changbinif ($git) {
9894a593c34SDu, Changbin	my @commits = ();
9900dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
9914a593c34SDu, Changbin		my $git_range;
99228898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
99328898fd1SJoe Perches			$git_range = "-$2 $1";
9944a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
9954a593c34SDu, Changbin			$git_range = "$commit_expr";
9964a593c34SDu, Changbin		} else {
9970dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
9980dea9f1eSJoe Perches		}
9990dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10000dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
100128898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
100228898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10030dea9f1eSJoe Perches			my $sha1 = $1;
10040dea9f1eSJoe Perches			my $subject = $2;
10050dea9f1eSJoe Perches			unshift(@commits, $sha1);
10060dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10074a593c34SDu, Changbin		}
10084a593c34SDu, Changbin	}
10094a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10104a593c34SDu, Changbin	@ARGV = @commits;
10114a593c34SDu, Changbin}
10124a593c34SDu, Changbin
1013c2fdda0dSAndy Whitcroftmy $vname;
10146c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
101521caa13cSAndy Whitcroft	my $FILE;
10164a593c34SDu, Changbin	if ($git) {
10174a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10184a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10194a593c34SDu, Changbin	} elsif ($file) {
102021caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10216c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
102221caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
102321caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10246c72ffaaSAndy Whitcroft	} else {
102521caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10266c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10276c72ffaaSAndy Whitcroft	}
1028c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1029c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10304a593c34SDu, Changbin	} elsif ($git) {
10310dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1032c2fdda0dSAndy Whitcroft	} else {
1033c2fdda0dSAndy Whitcroft		$vname = $filename;
1034c2fdda0dSAndy Whitcroft	}
103521caa13cSAndy Whitcroft	while (<$FILE>) {
10360a920b5bSAndy Whitcroft		chomp;
103700df344fSAndy Whitcroft		push(@rawlines, $_);
10386c72ffaaSAndy Whitcroft	}
103921caa13cSAndy Whitcroft	close($FILE);
1040d8469f16SJoe Perches
1041d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1042d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1043d8469f16SJoe Perches		print "$vname\n";
1044d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1045d8469f16SJoe Perches	}
1046d8469f16SJoe Perches
1047c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10480a920b5bSAndy Whitcroft		$exit = 1;
10490a920b5bSAndy Whitcroft	}
105000df344fSAndy Whitcroft	@rawlines = ();
105113214adfSAndy Whitcroft	@lines = ();
10523705ce5bSJoe Perches	@fixed = ();
1053d752fcc8SJoe Perches	@fixed_inserted = ();
1054d752fcc8SJoe Perches	@fixed_deleted = ();
1055194f66fcSJoe Perches	$fixlinenr = -1;
1056485ff23eSAlex Dowad	@modifierListFile = ();
1057485ff23eSAlex Dowad	@typeListFile = ();
1058485ff23eSAlex Dowad	build_types();
10590a920b5bSAndy Whitcroft}
10600a920b5bSAndy Whitcroft
1061d8469f16SJoe Perchesif (!$quiet) {
10623c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10633c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10643c816e49SJoe Perches
10655b57980dSJoe Perches	if (!$perl_version_ok) {
1066d8469f16SJoe Perches		print << "EOM"
1067d8469f16SJoe Perches
1068d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
10695b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1070d8469f16SJoe PerchesEOM
1071d8469f16SJoe Perches	}
1072d8469f16SJoe Perches	if ($exit) {
1073d8469f16SJoe Perches		print << "EOM"
1074d8469f16SJoe Perches
1075d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1076d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1077d8469f16SJoe PerchesEOM
1078d8469f16SJoe Perches	}
1079d8469f16SJoe Perches}
1080d8469f16SJoe Perches
10810a920b5bSAndy Whitcroftexit($exit);
10820a920b5bSAndy Whitcroft
10830a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10846c72ffaaSAndy Whitcroft	my ($root) = @_;
10856c72ffaaSAndy Whitcroft
10866c72ffaaSAndy Whitcroft	my @tree_check = (
10876c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
10886c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
10896c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
10906c72ffaaSAndy Whitcroft	);
10916c72ffaaSAndy Whitcroft
10926c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
10936c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
10940a920b5bSAndy Whitcroft			return 0;
10950a920b5bSAndy Whitcroft		}
10966c72ffaaSAndy Whitcroft	}
10976c72ffaaSAndy Whitcroft	return 1;
10986c72ffaaSAndy Whitcroft}
10990a920b5bSAndy Whitcroft
110020112475SJoe Perchessub parse_email {
110120112475SJoe Perches	my ($formatted_email) = @_;
110220112475SJoe Perches
110320112475SJoe Perches	my $name = "";
110420112475SJoe Perches	my $address = "";
110520112475SJoe Perches	my $comment = "";
110620112475SJoe Perches
110720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
110820112475SJoe Perches		$name = $1;
110920112475SJoe Perches		$address = $2;
111020112475SJoe Perches		$comment = $3 if defined $3;
111120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
111220112475SJoe Perches		$address = $1;
111320112475SJoe Perches		$comment = $2 if defined $2;
111420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
111520112475SJoe Perches		$address = $1;
111620112475SJoe Perches		$comment = $2 if defined $2;
111785e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
111820112475SJoe Perches		$name = $formatted_email;
11193705ce5bSJoe Perches		$name = trim($name);
112020112475SJoe Perches		$name =~ s/^\"|\"$//g;
112120112475SJoe Perches		# If there's a name left after stripping spaces and
112220112475SJoe Perches		# leading quotes, and the address doesn't have both
112320112475SJoe Perches		# leading and trailing angle brackets, the address
112420112475SJoe Perches		# is invalid. ie:
112520112475SJoe Perches		#   "joe smith [email protected]" bad
112620112475SJoe Perches		#   "joe smith <[email protected]" bad
112720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
112820112475SJoe Perches			$name = "";
112920112475SJoe Perches			$address = "";
113020112475SJoe Perches			$comment = "";
113120112475SJoe Perches		}
113220112475SJoe Perches	}
113320112475SJoe Perches
11343705ce5bSJoe Perches	$name = trim($name);
113520112475SJoe Perches	$name =~ s/^\"|\"$//g;
11363705ce5bSJoe Perches	$address = trim($address);
113720112475SJoe Perches	$address =~ s/^\<|\>$//g;
113820112475SJoe Perches
113920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
114020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
114120112475SJoe Perches		$name = "\"$name\"";
114220112475SJoe Perches	}
114320112475SJoe Perches
114420112475SJoe Perches	return ($name, $address, $comment);
114520112475SJoe Perches}
114620112475SJoe Perches
114720112475SJoe Perchessub format_email {
114820112475SJoe Perches	my ($name, $address) = @_;
114920112475SJoe Perches
115020112475SJoe Perches	my $formatted_email;
115120112475SJoe Perches
11523705ce5bSJoe Perches	$name = trim($name);
115320112475SJoe Perches	$name =~ s/^\"|\"$//g;
11543705ce5bSJoe Perches	$address = trim($address);
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	if ("$name" eq "") {
116220112475SJoe Perches		$formatted_email = "$address";
116320112475SJoe Perches	} else {
116420112475SJoe Perches		$formatted_email = "$name <$address>";
116520112475SJoe Perches	}
116620112475SJoe Perches
116720112475SJoe Perches	return $formatted_email;
116820112475SJoe Perches}
116920112475SJoe Perches
1170d311cd44SJoe Perchessub which {
1171d311cd44SJoe Perches	my ($bin) = @_;
1172d311cd44SJoe Perches
1173d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1174d311cd44SJoe Perches		if (-e "$path/$bin") {
1175d311cd44SJoe Perches			return "$path/$bin";
1176d311cd44SJoe Perches		}
1177d311cd44SJoe Perches	}
1178d311cd44SJoe Perches
1179d311cd44SJoe Perches	return "";
1180d311cd44SJoe Perches}
1181d311cd44SJoe Perches
1182000d1cc1SJoe Perchessub which_conf {
1183000d1cc1SJoe Perches	my ($conf) = @_;
1184000d1cc1SJoe Perches
1185000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1186000d1cc1SJoe Perches		if (-e "$path/$conf") {
1187000d1cc1SJoe Perches			return "$path/$conf";
1188000d1cc1SJoe Perches		}
1189000d1cc1SJoe Perches	}
1190000d1cc1SJoe Perches
1191000d1cc1SJoe Perches	return "";
1192000d1cc1SJoe Perches}
1193000d1cc1SJoe Perches
11940a920b5bSAndy Whitcroftsub expand_tabs {
11950a920b5bSAndy Whitcroft	my ($str) = @_;
11960a920b5bSAndy Whitcroft
11970a920b5bSAndy Whitcroft	my $res = '';
11980a920b5bSAndy Whitcroft	my $n = 0;
11990a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12000a920b5bSAndy Whitcroft		if ($c eq "\t") {
12010a920b5bSAndy Whitcroft			$res .= ' ';
12020a920b5bSAndy Whitcroft			$n++;
12030a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
12040a920b5bSAndy Whitcroft				$res .= ' ';
12050a920b5bSAndy Whitcroft			}
12060a920b5bSAndy Whitcroft			next;
12070a920b5bSAndy Whitcroft		}
12080a920b5bSAndy Whitcroft		$res .= $c;
12090a920b5bSAndy Whitcroft		$n++;
12100a920b5bSAndy Whitcroft	}
12110a920b5bSAndy Whitcroft
12120a920b5bSAndy Whitcroft	return $res;
12130a920b5bSAndy Whitcroft}
12146c72ffaaSAndy Whitcroftsub copy_spacing {
1215773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
12166c72ffaaSAndy Whitcroft	return $res;
12176c72ffaaSAndy Whitcroft}
12180a920b5bSAndy Whitcroft
12194a0df2efSAndy Whitcroftsub line_stats {
12204a0df2efSAndy Whitcroft	my ($line) = @_;
12214a0df2efSAndy Whitcroft
12224a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12234a0df2efSAndy Whitcroft	$line =~ s/^.//;
12244a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12254a0df2efSAndy Whitcroft
12264a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12274a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12284a0df2efSAndy Whitcroft
12294a0df2efSAndy Whitcroft	return (length($line), length($white));
12304a0df2efSAndy Whitcroft}
12314a0df2efSAndy Whitcroft
1232773647a0SAndy Whitcroftmy $sanitise_quote = '';
1233773647a0SAndy Whitcroft
1234773647a0SAndy Whitcroftsub sanitise_line_reset {
1235773647a0SAndy Whitcroft	my ($in_comment) = @_;
1236773647a0SAndy Whitcroft
1237773647a0SAndy Whitcroft	if ($in_comment) {
1238773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1239773647a0SAndy Whitcroft	} else {
1240773647a0SAndy Whitcroft		$sanitise_quote = '';
1241773647a0SAndy Whitcroft	}
1242773647a0SAndy Whitcroft}
124300df344fSAndy Whitcroftsub sanitise_line {
124400df344fSAndy Whitcroft	my ($line) = @_;
124500df344fSAndy Whitcroft
124600df344fSAndy Whitcroft	my $res = '';
124700df344fSAndy Whitcroft	my $l = '';
124800df344fSAndy Whitcroft
1249c2fdda0dSAndy Whitcroft	my $qlen = 0;
1250773647a0SAndy Whitcroft	my $off = 0;
1251773647a0SAndy Whitcroft	my $c;
125200df344fSAndy Whitcroft
1253773647a0SAndy Whitcroft	# Always copy over the diff marker.
1254773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1255773647a0SAndy Whitcroft
1256773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1257773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1258773647a0SAndy Whitcroft
12598d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1260773647a0SAndy Whitcroft		# and end, all to $;.
1261773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1262773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1263773647a0SAndy Whitcroft
1264773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1265773647a0SAndy Whitcroft			$off++;
126600df344fSAndy Whitcroft			next;
1267773647a0SAndy Whitcroft		}
126881bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1269773647a0SAndy Whitcroft			$sanitise_quote = '';
1270773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1271773647a0SAndy Whitcroft			$off++;
1272773647a0SAndy Whitcroft			next;
1273773647a0SAndy Whitcroft		}
1274113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1275113f04a8SDaniel Walker			$sanitise_quote = '//';
1276113f04a8SDaniel Walker
1277113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1278113f04a8SDaniel Walker			$off++;
1279113f04a8SDaniel Walker			next;
1280113f04a8SDaniel Walker		}
1281773647a0SAndy Whitcroft
1282773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1283773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1284773647a0SAndy Whitcroft		    $c eq "\\") {
1285773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1286773647a0SAndy Whitcroft			$off++;
1287773647a0SAndy Whitcroft			next;
1288773647a0SAndy Whitcroft		}
1289773647a0SAndy Whitcroft		# Regular quotes.
1290773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1291773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1292773647a0SAndy Whitcroft				$sanitise_quote = $c;
1293773647a0SAndy Whitcroft
1294773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1295773647a0SAndy Whitcroft				next;
1296773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1297773647a0SAndy Whitcroft				$sanitise_quote = '';
129800df344fSAndy Whitcroft			}
129900df344fSAndy Whitcroft		}
1300773647a0SAndy Whitcroft
1301fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1302773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1303773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1304113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1305113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1306773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1307773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
130800df344fSAndy Whitcroft		} else {
1309773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
131000df344fSAndy Whitcroft		}
1311c2fdda0dSAndy Whitcroft	}
1312c2fdda0dSAndy Whitcroft
1313113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1314113f04a8SDaniel Walker		$sanitise_quote = '';
1315113f04a8SDaniel Walker	}
1316113f04a8SDaniel Walker
1317c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1318c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1319c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1320c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1321c2fdda0dSAndy Whitcroft
1322c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1323c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1324c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1325c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1326c2fdda0dSAndy Whitcroft	}
1327c2fdda0dSAndy Whitcroft
1328dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1329dadf680dSJoe Perches		my $match = $1;
1330dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1331dadf680dSJoe Perches	}
1332dadf680dSJoe Perches
133300df344fSAndy Whitcroft	return $res;
133400df344fSAndy Whitcroft}
133500df344fSAndy Whitcroft
1336a6962d72SJoe Perchessub get_quoted_string {
1337a6962d72SJoe Perches	my ($line, $rawline) = @_;
1338a6962d72SJoe Perches
1339478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
134033acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1341a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1342a6962d72SJoe Perches}
1343a6962d72SJoe Perches
13448905a67cSAndy Whitcroftsub ctx_statement_block {
13458905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13468905a67cSAndy Whitcroft	my $line = $linenr - 1;
13478905a67cSAndy Whitcroft	my $blk = '';
13488905a67cSAndy Whitcroft	my $soff = $off;
13498905a67cSAndy Whitcroft	my $coff = $off - 1;
1350773647a0SAndy Whitcroft	my $coff_set = 0;
13518905a67cSAndy Whitcroft
135213214adfSAndy Whitcroft	my $loff = 0;
135313214adfSAndy Whitcroft
13548905a67cSAndy Whitcroft	my $type = '';
13558905a67cSAndy Whitcroft	my $level = 0;
1356a2750645SAndy Whitcroft	my @stack = ();
1357cf655043SAndy Whitcroft	my $p;
13588905a67cSAndy Whitcroft	my $c;
13598905a67cSAndy Whitcroft	my $len = 0;
136013214adfSAndy Whitcroft
136113214adfSAndy Whitcroft	my $remainder;
13628905a67cSAndy Whitcroft	while (1) {
1363a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1364a2750645SAndy Whitcroft
1365773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
13668905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
13678905a67cSAndy Whitcroft		# context.
13688905a67cSAndy Whitcroft		if ($off >= $len) {
13698905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1370dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1371c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13728905a67cSAndy Whitcroft				$remain--;
137313214adfSAndy Whitcroft				$loff = $len;
1374c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13758905a67cSAndy Whitcroft				$len = length($blk);
13768905a67cSAndy Whitcroft				$line++;
13778905a67cSAndy Whitcroft				last;
13788905a67cSAndy Whitcroft			}
13798905a67cSAndy Whitcroft			# Bail if there is no further context.
13808905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
138113214adfSAndy Whitcroft			if ($off >= $len) {
13828905a67cSAndy Whitcroft				last;
13838905a67cSAndy Whitcroft			}
1384f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1385f74bd194SAndy Whitcroft				$level++;
1386f74bd194SAndy Whitcroft				$type = '#';
1387f74bd194SAndy Whitcroft			}
13888905a67cSAndy Whitcroft		}
1389cf655043SAndy Whitcroft		$p = $c;
13908905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
139113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
13928905a67cSAndy Whitcroft
1393773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
13944635f4fbSAndy Whitcroft
13954635f4fbSAndy Whitcroft		# Handle nested #if/#else.
13964635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
13974635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
13984635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
13994635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14004635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14014635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14024635f4fbSAndy Whitcroft		}
14034635f4fbSAndy Whitcroft
14048905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14058905a67cSAndy Whitcroft		# outermost level.
14068905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
14078905a67cSAndy Whitcroft			last;
14088905a67cSAndy Whitcroft		}
14098905a67cSAndy Whitcroft
141013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1411773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1412773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1413773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1414773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1415773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1416773647a0SAndy Whitcroft			$coff_set = 1;
1417773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1418773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
141913214adfSAndy Whitcroft		}
142013214adfSAndy Whitcroft
14218905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14228905a67cSAndy Whitcroft			$level++;
14238905a67cSAndy Whitcroft			$type = '(';
14248905a67cSAndy Whitcroft		}
14258905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14268905a67cSAndy Whitcroft			$level--;
14278905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14288905a67cSAndy Whitcroft
14298905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
14308905a67cSAndy Whitcroft				$coff = $off;
1431773647a0SAndy Whitcroft				$coff_set = 1;
1432773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
14338905a67cSAndy Whitcroft			}
14348905a67cSAndy Whitcroft		}
14358905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14368905a67cSAndy Whitcroft			$level++;
14378905a67cSAndy Whitcroft			$type = '{';
14388905a67cSAndy Whitcroft		}
14398905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14408905a67cSAndy Whitcroft			$level--;
14418905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14428905a67cSAndy Whitcroft
14438905a67cSAndy Whitcroft			if ($level == 0) {
1444b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1445b998e001SPatrick Pannuto					$off++;
1446b998e001SPatrick Pannuto				}
14478905a67cSAndy Whitcroft				last;
14488905a67cSAndy Whitcroft			}
14498905a67cSAndy Whitcroft		}
1450f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1451f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1452f74bd194SAndy Whitcroft			$level--;
1453f74bd194SAndy Whitcroft			$type = '';
1454f74bd194SAndy Whitcroft			$off++;
1455f74bd194SAndy Whitcroft			last;
1456f74bd194SAndy Whitcroft		}
14578905a67cSAndy Whitcroft		$off++;
14588905a67cSAndy Whitcroft	}
1459a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
146013214adfSAndy Whitcroft	if ($off == $len) {
1461a3bb97a7SAndy Whitcroft		$loff = $len + 1;
146213214adfSAndy Whitcroft		$line++;
146313214adfSAndy Whitcroft		$remain--;
146413214adfSAndy Whitcroft	}
14658905a67cSAndy Whitcroft
14668905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
14678905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
14688905a67cSAndy Whitcroft
14698905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
14708905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
14718905a67cSAndy Whitcroft
1472773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
147313214adfSAndy Whitcroft
147413214adfSAndy Whitcroft	return ($statement, $condition,
147513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
147613214adfSAndy Whitcroft}
147713214adfSAndy Whitcroft
1478cf655043SAndy Whitcroftsub statement_lines {
1479cf655043SAndy Whitcroft	my ($stmt) = @_;
1480cf655043SAndy Whitcroft
1481cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1482cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1483cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1484cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1485cf655043SAndy Whitcroft
1486cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1487cf655043SAndy Whitcroft
1488cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1489cf655043SAndy Whitcroft}
1490cf655043SAndy Whitcroft
1491cf655043SAndy Whitcroftsub statement_rawlines {
1492cf655043SAndy Whitcroft	my ($stmt) = @_;
1493cf655043SAndy Whitcroft
1494cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1495cf655043SAndy Whitcroft
1496cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1497cf655043SAndy Whitcroft}
1498cf655043SAndy Whitcroft
1499cf655043SAndy Whitcroftsub statement_block_size {
1500cf655043SAndy Whitcroft	my ($stmt) = @_;
1501cf655043SAndy Whitcroft
1502cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1503cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1504cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1505cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1506cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1507cf655043SAndy Whitcroft
1508cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1509cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1510cf655043SAndy Whitcroft
1511cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1512cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1513cf655043SAndy Whitcroft
1514cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1515cf655043SAndy Whitcroft		return $stmt_lines;
1516cf655043SAndy Whitcroft	} else {
1517cf655043SAndy Whitcroft		return $stmt_statements;
1518cf655043SAndy Whitcroft	}
1519cf655043SAndy Whitcroft}
1520cf655043SAndy Whitcroft
152113214adfSAndy Whitcroftsub ctx_statement_full {
152213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
152313214adfSAndy Whitcroft	my ($statement, $condition, $level);
152413214adfSAndy Whitcroft
152513214adfSAndy Whitcroft	my (@chunks);
152613214adfSAndy Whitcroft
1527cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
152813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
152913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1530773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
153113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1532cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1533cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1534cf655043SAndy Whitcroft	}
1535cf655043SAndy Whitcroft
1536cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1537cf655043SAndy Whitcroft	# could continue the statement.
1538cf655043SAndy Whitcroft	for (;;) {
153913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
154013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1541cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1542773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1543cf655043SAndy Whitcroft		#print "C: push\n";
1544cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
154513214adfSAndy Whitcroft	}
154613214adfSAndy Whitcroft
154713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15488905a67cSAndy Whitcroft}
15498905a67cSAndy Whitcroft
15504a0df2efSAndy Whitcroftsub ctx_block_get {
1551f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15524a0df2efSAndy Whitcroft	my $line;
15534a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15544a0df2efSAndy Whitcroft	my $blk = '';
15554a0df2efSAndy Whitcroft	my @o;
15564a0df2efSAndy Whitcroft	my @c;
15574a0df2efSAndy Whitcroft	my @res = ();
15584a0df2efSAndy Whitcroft
1559f0a594c1SAndy Whitcroft	my $level = 0;
15604635f4fbSAndy Whitcroft	my @stack = ($level);
156100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
156200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
156300df344fSAndy Whitcroft		$remain--;
156400df344fSAndy Whitcroft
156500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
15664635f4fbSAndy Whitcroft
15674635f4fbSAndy Whitcroft		# Handle nested #if/#else.
156801464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
15694635f4fbSAndy Whitcroft			push(@stack, $level);
157001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
15714635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
157201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15734635f4fbSAndy Whitcroft			$level = pop(@stack);
15744635f4fbSAndy Whitcroft		}
15754635f4fbSAndy Whitcroft
157601464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1577f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1578f0a594c1SAndy Whitcroft			if ($off > 0) {
1579f0a594c1SAndy Whitcroft				$off--;
1580f0a594c1SAndy Whitcroft				next;
1581f0a594c1SAndy Whitcroft			}
15824a0df2efSAndy Whitcroft
1583f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1584f0a594c1SAndy Whitcroft				$level--;
1585f0a594c1SAndy Whitcroft				last if ($level == 0);
1586f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1587f0a594c1SAndy Whitcroft				$level++;
1588f0a594c1SAndy Whitcroft			}
1589f0a594c1SAndy Whitcroft		}
15904a0df2efSAndy Whitcroft
1591f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
159200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
15934a0df2efSAndy Whitcroft		}
15944a0df2efSAndy Whitcroft
1595f0a594c1SAndy Whitcroft		last if ($level == 0);
15964a0df2efSAndy Whitcroft	}
15974a0df2efSAndy Whitcroft
1598f0a594c1SAndy Whitcroft	return ($level, @res);
15994a0df2efSAndy Whitcroft}
16004a0df2efSAndy Whitcroftsub ctx_block_outer {
16014a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16024a0df2efSAndy Whitcroft
1603f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1604f0a594c1SAndy Whitcroft	return @r;
16054a0df2efSAndy Whitcroft}
16064a0df2efSAndy Whitcroftsub ctx_block {
16074a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16084a0df2efSAndy Whitcroft
1609f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1610f0a594c1SAndy Whitcroft	return @r;
1611653d4876SAndy Whitcroft}
1612653d4876SAndy Whitcroftsub ctx_statement {
1613f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1614f0a594c1SAndy Whitcroft
1615f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1616f0a594c1SAndy Whitcroft	return @r;
1617f0a594c1SAndy Whitcroft}
1618f0a594c1SAndy Whitcroftsub ctx_block_level {
1619653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1620653d4876SAndy Whitcroft
1621f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16224a0df2efSAndy Whitcroft}
16239c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16249c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16259c0ca6f9SAndy Whitcroft
16269c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16279c0ca6f9SAndy Whitcroft}
16284a0df2efSAndy Whitcroft
16294a0df2efSAndy Whitcroftsub ctx_locate_comment {
16304a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16314a0df2efSAndy Whitcroft
16324a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1633beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
16344a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16354a0df2efSAndy Whitcroft
16364a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16374a0df2efSAndy Whitcroft	# comment.
16384a0df2efSAndy Whitcroft	my $in_comment = 0;
16394a0df2efSAndy Whitcroft	$current_comment = '';
16404a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
164100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
164200df344fSAndy Whitcroft		#warn "           $line\n";
16434a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16444a0df2efSAndy Whitcroft			$in_comment = 1;
16454a0df2efSAndy Whitcroft		}
16464a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16474a0df2efSAndy Whitcroft			$in_comment = 1;
16484a0df2efSAndy Whitcroft		}
16494a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16504a0df2efSAndy Whitcroft			$current_comment = '';
16514a0df2efSAndy Whitcroft		}
16524a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16534a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16544a0df2efSAndy Whitcroft			$in_comment = 0;
16554a0df2efSAndy Whitcroft		}
16564a0df2efSAndy Whitcroft	}
16574a0df2efSAndy Whitcroft
16584a0df2efSAndy Whitcroft	chomp($current_comment);
16594a0df2efSAndy Whitcroft	return($current_comment);
16604a0df2efSAndy Whitcroft}
16614a0df2efSAndy Whitcroftsub ctx_has_comment {
16624a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16634a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
16644a0df2efSAndy Whitcroft
166500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
16664a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
16674a0df2efSAndy Whitcroft
16684a0df2efSAndy Whitcroft	return ($cmt ne '');
16694a0df2efSAndy Whitcroft}
16704a0df2efSAndy Whitcroft
16714d001e4dSAndy Whitcroftsub raw_line {
16724d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16734d001e4dSAndy Whitcroft
16744d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16754d001e4dSAndy Whitcroft	$cnt++;
16764d001e4dSAndy Whitcroft
16774d001e4dSAndy Whitcroft	my $line;
16784d001e4dSAndy Whitcroft	while ($cnt) {
16794d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16804d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16814d001e4dSAndy Whitcroft		$cnt--;
16824d001e4dSAndy Whitcroft	}
16834d001e4dSAndy Whitcroft
16844d001e4dSAndy Whitcroft	return $line;
16854d001e4dSAndy Whitcroft}
16864d001e4dSAndy Whitcroft
16872a9f9d85STobin C. Hardingsub get_stat_real {
16882a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
16892a9f9d85STobin C. Harding
16902a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
16912a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
16922a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
16932a9f9d85STobin C. Harding	}
16942a9f9d85STobin C. Harding
16952a9f9d85STobin C. Harding	return $stat_real;
16962a9f9d85STobin C. Harding}
16972a9f9d85STobin C. Harding
1698e3d95a2aSTobin C. Hardingsub get_stat_here {
1699e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1700e3d95a2aSTobin C. Harding
1701e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1702e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1703e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1704e3d95a2aSTobin C. Harding	}
1705e3d95a2aSTobin C. Harding
1706e3d95a2aSTobin C. Harding	return $herectx;
1707e3d95a2aSTobin C. Harding}
1708e3d95a2aSTobin C. Harding
17090a920b5bSAndy Whitcroftsub cat_vet {
17100a920b5bSAndy Whitcroft	my ($vet) = @_;
17119c0ca6f9SAndy Whitcroft	my ($res, $coded);
17120a920b5bSAndy Whitcroft
17139c0ca6f9SAndy Whitcroft	$res = '';
17146c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
17156c72ffaaSAndy Whitcroft		$res .= $1;
17166c72ffaaSAndy Whitcroft		if ($2 ne '') {
17179c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
17186c72ffaaSAndy Whitcroft			$res .= $coded;
17196c72ffaaSAndy Whitcroft		}
17209c0ca6f9SAndy Whitcroft	}
17219c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
17220a920b5bSAndy Whitcroft
17239c0ca6f9SAndy Whitcroft	return $res;
17240a920b5bSAndy Whitcroft}
17250a920b5bSAndy Whitcroft
1726c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1727cf655043SAndy Whitcroftmy $av_pending;
1728c2fdda0dSAndy Whitcroftmy @av_paren_type;
17291f65f947SAndy Whitcroftmy $av_pend_colon;
1730c2fdda0dSAndy Whitcroft
1731c2fdda0dSAndy Whitcroftsub annotate_reset {
1732c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1733cf655043SAndy Whitcroft	$av_pending = '_';
1734cf655043SAndy Whitcroft	@av_paren_type = ('E');
17351f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1736c2fdda0dSAndy Whitcroft}
1737c2fdda0dSAndy Whitcroft
17386c72ffaaSAndy Whitcroftsub annotate_values {
17396c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
17406c72ffaaSAndy Whitcroft
17416c72ffaaSAndy Whitcroft	my $res;
17421f65f947SAndy Whitcroft	my $var = '_' x length($stream);
17436c72ffaaSAndy Whitcroft	my $cur = $stream;
17446c72ffaaSAndy Whitcroft
1745c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
17466c72ffaaSAndy Whitcroft
17476c72ffaaSAndy Whitcroft	while (length($cur)) {
1748773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1749cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1750171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
17516c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1752c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1753c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1754cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1755c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
17566c72ffaaSAndy Whitcroft			}
17576c72ffaaSAndy Whitcroft
1758c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
17599446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
17609446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1761addcdceaSAndy Whitcroft			$type = 'c';
17629446ef56SAndy Whitcroft
1763e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1764c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
17656c72ffaaSAndy Whitcroft			$type = 'T';
17666c72ffaaSAndy Whitcroft
1767389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1768389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1769389a2fe5SAndy Whitcroft			$type = 'T';
1770389a2fe5SAndy Whitcroft
1771c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1772171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1773c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1774171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1775171ae1a4SAndy Whitcroft			if ($2 ne '') {
1776cf655043SAndy Whitcroft				$av_pending = 'N';
1777171ae1a4SAndy Whitcroft			}
1778171ae1a4SAndy Whitcroft			$type = 'E';
1779171ae1a4SAndy Whitcroft
1780c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1781171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1782171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1783171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
17846c72ffaaSAndy Whitcroft
1785c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1786cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1787c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1788cf655043SAndy Whitcroft
1789cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1790cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1791171ae1a4SAndy Whitcroft			$type = 'E';
1792cf655043SAndy Whitcroft
1793c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1794cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1795cf655043SAndy Whitcroft			$av_preprocessor = 1;
1796cf655043SAndy Whitcroft
1797cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1798cf655043SAndy Whitcroft
1799171ae1a4SAndy Whitcroft			$type = 'E';
1800cf655043SAndy Whitcroft
1801c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1802cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1803cf655043SAndy Whitcroft
1804cf655043SAndy Whitcroft			$av_preprocessor = 1;
1805cf655043SAndy Whitcroft
1806cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1807cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1808cf655043SAndy Whitcroft			pop(@av_paren_type);
1809cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1810171ae1a4SAndy Whitcroft			$type = 'E';
18116c72ffaaSAndy Whitcroft
18126c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1813c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
18146c72ffaaSAndy Whitcroft
1815171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1816171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1817171ae1a4SAndy Whitcroft			$av_pending = $type;
1818171ae1a4SAndy Whitcroft			$type = 'N';
1819171ae1a4SAndy Whitcroft
18206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1821c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
18226c72ffaaSAndy Whitcroft			if (defined $2) {
1823cf655043SAndy Whitcroft				$av_pending = 'V';
18246c72ffaaSAndy Whitcroft			}
18256c72ffaaSAndy Whitcroft			$type = 'N';
18266c72ffaaSAndy Whitcroft
182714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1828c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
182914b111c1SAndy Whitcroft			$av_pending = 'E';
18306c72ffaaSAndy Whitcroft			$type = 'N';
18316c72ffaaSAndy Whitcroft
18321f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
18331f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
18341f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18351f65f947SAndy Whitcroft			$type = 'N';
18361f65f947SAndy Whitcroft
183714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1838c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
18396c72ffaaSAndy Whitcroft			$type = 'N';
18406c72ffaaSAndy Whitcroft
18416c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1842c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1843cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1844cf655043SAndy Whitcroft			$av_pending = '_';
18456c72ffaaSAndy Whitcroft			$type = 'N';
18466c72ffaaSAndy Whitcroft
18476c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1848cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1849cf655043SAndy Whitcroft			if ($new_type ne '_') {
1850cf655043SAndy Whitcroft				$type = $new_type;
1851c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1852c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
18536c72ffaaSAndy Whitcroft			} else {
1854c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
18556c72ffaaSAndy Whitcroft			}
18566c72ffaaSAndy Whitcroft
1857c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1858c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1859c8cb2ca3SAndy Whitcroft			$type = 'V';
1860cf655043SAndy Whitcroft			$av_pending = 'V';
18616c72ffaaSAndy Whitcroft
18628e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
18638e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
18641f65f947SAndy Whitcroft				$av_pend_colon = 'B';
18658e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
18668e761b04SAndy Whitcroft				$av_pend_colon = 'L';
18671f65f947SAndy Whitcroft			}
18681f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
18691f65f947SAndy Whitcroft			$type = 'V';
18701f65f947SAndy Whitcroft
18716c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1872c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
18736c72ffaaSAndy Whitcroft			$type = 'V';
18746c72ffaaSAndy Whitcroft
18756c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1876c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
18776c72ffaaSAndy Whitcroft			$type = 'N';
18786c72ffaaSAndy Whitcroft
1879cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1880c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
188113214adfSAndy Whitcroft			$type = 'E';
18821f65f947SAndy Whitcroft			$av_pend_colon = 'O';
188313214adfSAndy Whitcroft
18848e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
18858e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
18868e761b04SAndy Whitcroft			$type = 'C';
18878e761b04SAndy Whitcroft
18881f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
18891f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
18901f65f947SAndy Whitcroft			$type = 'N';
18911f65f947SAndy Whitcroft
18921f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
18931f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
18941f65f947SAndy Whitcroft
18951f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
18961f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
18971f65f947SAndy Whitcroft				$type = 'E';
18981f65f947SAndy Whitcroft			} else {
18991f65f947SAndy Whitcroft				$type = 'N';
19001f65f947SAndy Whitcroft			}
19011f65f947SAndy Whitcroft			$av_pend_colon = 'O';
19021f65f947SAndy Whitcroft
19038e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
190413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
19056c72ffaaSAndy Whitcroft			$type = 'N';
19066c72ffaaSAndy Whitcroft
19070d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
190874048ed8SAndy Whitcroft			my $variant;
190974048ed8SAndy Whitcroft
191074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
191174048ed8SAndy Whitcroft			if ($type eq 'V') {
191274048ed8SAndy Whitcroft				$variant = 'B';
191374048ed8SAndy Whitcroft			} else {
191474048ed8SAndy Whitcroft				$variant = 'U';
191574048ed8SAndy Whitcroft			}
191674048ed8SAndy Whitcroft
191774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
191874048ed8SAndy Whitcroft			$type = 'N';
191974048ed8SAndy Whitcroft
19206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1921c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
19226c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
19236c72ffaaSAndy Whitcroft				$type = 'N';
19246c72ffaaSAndy Whitcroft			}
19256c72ffaaSAndy Whitcroft
19266c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1927c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
19286c72ffaaSAndy Whitcroft		}
19296c72ffaaSAndy Whitcroft		if (defined $1) {
19306c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
19316c72ffaaSAndy Whitcroft			$res .= $type x length($1);
19326c72ffaaSAndy Whitcroft		}
19336c72ffaaSAndy Whitcroft	}
19346c72ffaaSAndy Whitcroft
19351f65f947SAndy Whitcroft	return ($res, $var);
19366c72ffaaSAndy Whitcroft}
19376c72ffaaSAndy Whitcroft
19388905a67cSAndy Whitcroftsub possible {
193913214adfSAndy Whitcroft	my ($possible, $line) = @_;
19409a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
19410776e594SAndy Whitcroft		^(?:
19420776e594SAndy Whitcroft			$Modifier|
19430776e594SAndy Whitcroft			$Storage|
19440776e594SAndy Whitcroft			$Type|
19459a974fdbSAndy Whitcroft			DEFINE_\S+
19469a974fdbSAndy Whitcroft		)$|
19479a974fdbSAndy Whitcroft		^(?:
19480776e594SAndy Whitcroft			goto|
19490776e594SAndy Whitcroft			return|
19500776e594SAndy Whitcroft			case|
19510776e594SAndy Whitcroft			else|
19520776e594SAndy Whitcroft			asm|__asm__|
195389a88353SAndy Whitcroft			do|
195489a88353SAndy Whitcroft			\#|
195589a88353SAndy Whitcroft			\#\#|
19569a974fdbSAndy Whitcroft		)(?:\s|$)|
19570776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
19589a974fdbSAndy Whitcroft	    )}x;
19599a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
19609a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1961c45dcabdSAndy Whitcroft		# Check for modifiers.
1962c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1963c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1964c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1965c45dcabdSAndy Whitcroft
1966c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1967c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1968d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
19699a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1970d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1971485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1972d2506586SAndy Whitcroft				}
19739a974fdbSAndy Whitcroft			}
1974c45dcabdSAndy Whitcroft
1975c45dcabdSAndy Whitcroft		} else {
197613214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1977485ff23eSAlex Dowad			push(@typeListFile, $possible);
1978c45dcabdSAndy Whitcroft		}
19798905a67cSAndy Whitcroft		build_types();
19800776e594SAndy Whitcroft	} else {
19810776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
19828905a67cSAndy Whitcroft	}
19838905a67cSAndy Whitcroft}
19848905a67cSAndy Whitcroft
19856c72ffaaSAndy Whitcroftmy $prefix = '';
19866c72ffaaSAndy Whitcroft
1987000d1cc1SJoe Perchessub show_type {
1988cbec18afSJoe Perches	my ($type) = @_;
198991bfe484SJoe Perches
1990522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
1991522b837cSAlexey Dobriyan
1992cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1993cbec18afSJoe Perches
1994cbec18afSJoe Perches	return !defined $ignore_type{$type};
1995000d1cc1SJoe Perches}
1996000d1cc1SJoe Perches
1997f0a594c1SAndy Whitcroftsub report {
1998cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1999cbec18afSJoe Perches
2000cbec18afSJoe Perches	if (!show_type($type) ||
2001cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2002773647a0SAndy Whitcroft		return 0;
2003773647a0SAndy Whitcroft	}
200457230297SJoe Perches	my $output = '';
2005737c0767SJohn Brooks	if ($color) {
200657230297SJoe Perches		if ($level eq 'ERROR') {
200757230297SJoe Perches			$output .= RED;
200857230297SJoe Perches		} elsif ($level eq 'WARNING') {
200957230297SJoe Perches			$output .= YELLOW;
2010000d1cc1SJoe Perches		} else {
201157230297SJoe Perches			$output .= GREEN;
2012000d1cc1SJoe Perches		}
201357230297SJoe Perches	}
201457230297SJoe Perches	$output .= $prefix . $level . ':';
201557230297SJoe Perches	if ($show_types) {
2016737c0767SJohn Brooks		$output .= BLUE if ($color);
201757230297SJoe Perches		$output .= "$type:";
201857230297SJoe Perches	}
2019737c0767SJohn Brooks	$output .= RESET if ($color);
202057230297SJoe Perches	$output .= ' ' . $msg . "\n";
202134d8815fSJoe Perches
202234d8815fSJoe Perches	if ($showfile) {
202334d8815fSJoe Perches		my @lines = split("\n", $output, -1);
202434d8815fSJoe Perches		splice(@lines, 1, 1);
202534d8815fSJoe Perches		$output = join("\n", @lines);
202634d8815fSJoe Perches	}
202757230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
20288905a67cSAndy Whitcroft
202957230297SJoe Perches	push(our @report, $output);
2030773647a0SAndy Whitcroft
2031773647a0SAndy Whitcroft	return 1;
2032f0a594c1SAndy Whitcroft}
2033cbec18afSJoe Perches
2034f0a594c1SAndy Whitcroftsub report_dump {
203513214adfSAndy Whitcroft	our @report;
2036f0a594c1SAndy Whitcroft}
2037000d1cc1SJoe Perches
2038d752fcc8SJoe Perchessub fixup_current_range {
2039d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2040d752fcc8SJoe Perches
2041d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2042d752fcc8SJoe Perches		my $o = $1;
2043d752fcc8SJoe Perches		my $l = $2;
2044d752fcc8SJoe Perches		my $no = $o + $offset;
2045d752fcc8SJoe Perches		my $nl = $l + $length;
2046d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2047d752fcc8SJoe Perches	}
2048d752fcc8SJoe Perches}
2049d752fcc8SJoe Perches
2050d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2051d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2052d752fcc8SJoe Perches
2053d752fcc8SJoe Perches	my $range_last_linenr = 0;
2054d752fcc8SJoe Perches	my $delta_offset = 0;
2055d752fcc8SJoe Perches
2056d752fcc8SJoe Perches	my $old_linenr = 0;
2057d752fcc8SJoe Perches	my $new_linenr = 0;
2058d752fcc8SJoe Perches
2059d752fcc8SJoe Perches	my $next_insert = 0;
2060d752fcc8SJoe Perches	my $next_delete = 0;
2061d752fcc8SJoe Perches
2062d752fcc8SJoe Perches	my @lines = ();
2063d752fcc8SJoe Perches
2064d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2065d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2066d752fcc8SJoe Perches
2067d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2068d752fcc8SJoe Perches		my $save_line = 1;
2069d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2070323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2071d752fcc8SJoe Perches			$delta_offset = 0;
2072d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2073d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2074d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2075d752fcc8SJoe Perches		}
2076d752fcc8SJoe Perches
2077d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2078d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2079d752fcc8SJoe Perches			$save_line = 0;
2080d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2081d752fcc8SJoe Perches		}
2082d752fcc8SJoe Perches
2083d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2084d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2085d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2086d752fcc8SJoe Perches			$new_linenr++;
2087d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2088d752fcc8SJoe Perches		}
2089d752fcc8SJoe Perches
2090d752fcc8SJoe Perches		if ($save_line) {
2091d752fcc8SJoe Perches			push(@lines, $line);
2092d752fcc8SJoe Perches			$new_linenr++;
2093d752fcc8SJoe Perches		}
2094d752fcc8SJoe Perches
2095d752fcc8SJoe Perches		$old_linenr++;
2096d752fcc8SJoe Perches	}
2097d752fcc8SJoe Perches
2098d752fcc8SJoe Perches	return @lines;
2099d752fcc8SJoe Perches}
2100d752fcc8SJoe Perches
2101f2d7e4d4SJoe Perchessub fix_insert_line {
2102f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2103f2d7e4d4SJoe Perches
2104f2d7e4d4SJoe Perches	my $inserted = {
2105f2d7e4d4SJoe Perches		LINENR => $linenr,
2106f2d7e4d4SJoe Perches		LINE => $line,
2107f2d7e4d4SJoe Perches	};
2108f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2109f2d7e4d4SJoe Perches}
2110f2d7e4d4SJoe Perches
2111f2d7e4d4SJoe Perchessub fix_delete_line {
2112f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2113f2d7e4d4SJoe Perches
2114f2d7e4d4SJoe Perches	my $deleted = {
2115f2d7e4d4SJoe Perches		LINENR => $linenr,
2116f2d7e4d4SJoe Perches		LINE => $line,
2117f2d7e4d4SJoe Perches	};
2118f2d7e4d4SJoe Perches
2119f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2120f2d7e4d4SJoe Perches}
2121f2d7e4d4SJoe Perches
2122de7d4f0eSAndy Whitcroftsub ERROR {
2123cbec18afSJoe Perches	my ($type, $msg) = @_;
2124cbec18afSJoe Perches
2125cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2126de7d4f0eSAndy Whitcroft		our $clean = 0;
21276c72ffaaSAndy Whitcroft		our $cnt_error++;
21283705ce5bSJoe Perches		return 1;
2129de7d4f0eSAndy Whitcroft	}
21303705ce5bSJoe Perches	return 0;
2131773647a0SAndy Whitcroft}
2132de7d4f0eSAndy Whitcroftsub WARN {
2133cbec18afSJoe Perches	my ($type, $msg) = @_;
2134cbec18afSJoe Perches
2135cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2136de7d4f0eSAndy Whitcroft		our $clean = 0;
21376c72ffaaSAndy Whitcroft		our $cnt_warn++;
21383705ce5bSJoe Perches		return 1;
2139de7d4f0eSAndy Whitcroft	}
21403705ce5bSJoe Perches	return 0;
2141773647a0SAndy Whitcroft}
2142de7d4f0eSAndy Whitcroftsub CHK {
2143cbec18afSJoe Perches	my ($type, $msg) = @_;
2144cbec18afSJoe Perches
2145cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2146de7d4f0eSAndy Whitcroft		our $clean = 0;
21476c72ffaaSAndy Whitcroft		our $cnt_chk++;
21483705ce5bSJoe Perches		return 1;
21496c72ffaaSAndy Whitcroft	}
21503705ce5bSJoe Perches	return 0;
2151de7d4f0eSAndy Whitcroft}
2152de7d4f0eSAndy Whitcroft
21536ecd9674SAndy Whitcroftsub check_absolute_file {
21546ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
21556ecd9674SAndy Whitcroft	my $file = $absolute;
21566ecd9674SAndy Whitcroft
21576ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
21586ecd9674SAndy Whitcroft
21596ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
21606ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
21616ecd9674SAndy Whitcroft		if (-f "$root/$file") {
21626ecd9674SAndy Whitcroft			##print "file<$file>\n";
21636ecd9674SAndy Whitcroft			last;
21646ecd9674SAndy Whitcroft		}
21656ecd9674SAndy Whitcroft	}
21666ecd9674SAndy Whitcroft	if (! -f _)  {
21676ecd9674SAndy Whitcroft		return 0;
21686ecd9674SAndy Whitcroft	}
21696ecd9674SAndy Whitcroft
21706ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
21716ecd9674SAndy Whitcroft	my $prefix = $absolute;
21726ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
21736ecd9674SAndy Whitcroft
21746ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
21756ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2176000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2177000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
21786ecd9674SAndy Whitcroft	}
21796ecd9674SAndy Whitcroft}
21806ecd9674SAndy Whitcroft
21813705ce5bSJoe Perchessub trim {
21823705ce5bSJoe Perches	my ($string) = @_;
21833705ce5bSJoe Perches
2184b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2185b34c648bSJoe Perches
2186b34c648bSJoe Perches	return $string;
2187b34c648bSJoe Perches}
2188b34c648bSJoe Perches
2189b34c648bSJoe Perchessub ltrim {
2190b34c648bSJoe Perches	my ($string) = @_;
2191b34c648bSJoe Perches
2192b34c648bSJoe Perches	$string =~ s/^\s+//;
2193b34c648bSJoe Perches
2194b34c648bSJoe Perches	return $string;
2195b34c648bSJoe Perches}
2196b34c648bSJoe Perches
2197b34c648bSJoe Perchessub rtrim {
2198b34c648bSJoe Perches	my ($string) = @_;
2199b34c648bSJoe Perches
2200b34c648bSJoe Perches	$string =~ s/\s+$//;
22013705ce5bSJoe Perches
22023705ce5bSJoe Perches	return $string;
22033705ce5bSJoe Perches}
22043705ce5bSJoe Perches
220552ea8506SJoe Perchessub string_find_replace {
220652ea8506SJoe Perches	my ($string, $find, $replace) = @_;
220752ea8506SJoe Perches
220852ea8506SJoe Perches	$string =~ s/$find/$replace/g;
220952ea8506SJoe Perches
221052ea8506SJoe Perches	return $string;
221152ea8506SJoe Perches}
221252ea8506SJoe Perches
22133705ce5bSJoe Perchessub tabify {
22143705ce5bSJoe Perches	my ($leading) = @_;
22153705ce5bSJoe Perches
22163705ce5bSJoe Perches	my $source_indent = 8;
22173705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
22183705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
22193705ce5bSJoe Perches
22203705ce5bSJoe Perches	#convert leading spaces to tabs
22213705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
22223705ce5bSJoe Perches	#Remove spaces before a tab
22233705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
22243705ce5bSJoe Perches
22253705ce5bSJoe Perches	return "$leading";
22263705ce5bSJoe Perches}
22273705ce5bSJoe Perches
2228d1fe9c09SJoe Perchessub pos_last_openparen {
2229d1fe9c09SJoe Perches	my ($line) = @_;
2230d1fe9c09SJoe Perches
2231d1fe9c09SJoe Perches	my $pos = 0;
2232d1fe9c09SJoe Perches
2233d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2234d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2235d1fe9c09SJoe Perches
2236d1fe9c09SJoe Perches	my $last_openparen = 0;
2237d1fe9c09SJoe Perches
2238d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2239d1fe9c09SJoe Perches		return -1;
2240d1fe9c09SJoe Perches	}
2241d1fe9c09SJoe Perches
2242d1fe9c09SJoe Perches	my $len = length($line);
2243d1fe9c09SJoe Perches
2244d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2245d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2246d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2247d1fe9c09SJoe Perches			$pos += length($1) - 1;
2248d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2249d1fe9c09SJoe Perches			$last_openparen = $pos;
2250d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2251d1fe9c09SJoe Perches			last;
2252d1fe9c09SJoe Perches		}
2253d1fe9c09SJoe Perches	}
2254d1fe9c09SJoe Perches
225591cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2256d1fe9c09SJoe Perches}
2257d1fe9c09SJoe Perches
22580a920b5bSAndy Whitcroftsub process {
22590a920b5bSAndy Whitcroft	my $filename = shift;
22600a920b5bSAndy Whitcroft
22610a920b5bSAndy Whitcroft	my $linenr=0;
22620a920b5bSAndy Whitcroft	my $prevline="";
2263c2fdda0dSAndy Whitcroft	my $prevrawline="";
22640a920b5bSAndy Whitcroft	my $stashline="";
2265c2fdda0dSAndy Whitcroft	my $stashrawline="";
22660a920b5bSAndy Whitcroft
22674a0df2efSAndy Whitcroft	my $length;
22680a920b5bSAndy Whitcroft	my $indent;
22690a920b5bSAndy Whitcroft	my $previndent=0;
22700a920b5bSAndy Whitcroft	my $stashindent=0;
22710a920b5bSAndy Whitcroft
2272de7d4f0eSAndy Whitcroft	our $clean = 1;
22730a920b5bSAndy Whitcroft	my $signoff = 0;
2274cd261496SGeert Uytterhoeven	my $author = '';
2275cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
22760a920b5bSAndy Whitcroft	my $is_patch = 0;
2277133712a2SRob Herring	my $is_binding_patch = -1;
227829ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
227915662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2280ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2281490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2282bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
22832a076f40SJoe Perches	my $commit_log_long_line = 0;
2284e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
228513f1937eSJoe Perches	my $reported_maintainer_file = 0;
2286fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2287fa64205dSPasi Savanainen
2288365dd4eaSJoe Perches	my $last_blank_line = 0;
22895e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2290365dd4eaSJoe Perches
229113214adfSAndy Whitcroft	our @report = ();
22926c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
22936c72ffaaSAndy Whitcroft	our $cnt_error = 0;
22946c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
22956c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
22966c72ffaaSAndy Whitcroft
22970a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
22980a920b5bSAndy Whitcroft	my $realfile = '';
22990a920b5bSAndy Whitcroft	my $realline = 0;
23000a920b5bSAndy Whitcroft	my $realcnt = 0;
23010a920b5bSAndy Whitcroft	my $here = '';
230277cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23030a920b5bSAndy Whitcroft	my $in_comment = 0;
2304c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23050a920b5bSAndy Whitcroft	my $first_line = 0;
23061e855726SWolfram Sang	my $p1_prefix = '';
23070a920b5bSAndy Whitcroft
230813214adfSAndy Whitcroft	my $prev_values = 'E';
230913214adfSAndy Whitcroft
231013214adfSAndy Whitcroft	# suppression flags
2311773647a0SAndy Whitcroft	my %suppress_ifbraces;
2312170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
23132b474a1aSAndy Whitcroft	my %suppress_export;
23143e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2315653d4876SAndy Whitcroft
23167e51f197SJoe Perches	my %signatures = ();
2317323c1260SJoe Perches
2318c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2319de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2320c2fdda0dSAndy Whitcroft	#
2321de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2322de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2323773647a0SAndy Whitcroft
2324d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2325d8b07710SJoe Perches
23269f3a8992SRob Herring	my $checklicenseline = 1;
23279f3a8992SRob Herring
2328773647a0SAndy Whitcroft	sanitise_line_reset();
2329c2fdda0dSAndy Whitcroft	my $line;
2330c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2331773647a0SAndy Whitcroft		$linenr++;
2332773647a0SAndy Whitcroft		$line = $rawline;
2333c2fdda0dSAndy Whitcroft
23343705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
23353705ce5bSJoe Perches
2336773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2337de7d4f0eSAndy Whitcroft			$setup_docs = 0;
23388c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2339de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2340de7d4f0eSAndy Whitcroft			}
2341773647a0SAndy Whitcroft			#next;
2342de7d4f0eSAndy Whitcroft		}
234374fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2344773647a0SAndy Whitcroft			$realline=$1-1;
2345773647a0SAndy Whitcroft			if (defined $2) {
2346773647a0SAndy Whitcroft				$realcnt=$3+1;
2347773647a0SAndy Whitcroft			} else {
2348773647a0SAndy Whitcroft				$realcnt=1+1;
2349773647a0SAndy Whitcroft			}
2350c45dcabdSAndy Whitcroft			$in_comment = 0;
2351773647a0SAndy Whitcroft
2352773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2353773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2354773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2355773647a0SAndy Whitcroft			# at context start.
2356773647a0SAndy Whitcroft			my $edge;
235701fa9147SAndy Whitcroft			my $cnt = $realcnt;
235801fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
235901fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
236001fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
236101fa9147SAndy Whitcroft				$cnt--;
236201fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2363721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2364fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2365fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2366fae17daeSAndy Whitcroft					($edge) = $1;
2367fae17daeSAndy Whitcroft					last;
2368fae17daeSAndy Whitcroft				}
2369773647a0SAndy Whitcroft			}
2370773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2371773647a0SAndy Whitcroft				$in_comment = 1;
2372773647a0SAndy Whitcroft			}
2373773647a0SAndy Whitcroft
2374773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2375773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2376773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2377773647a0SAndy Whitcroft			if (!defined $edge &&
237883242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2379773647a0SAndy Whitcroft			{
2380773647a0SAndy Whitcroft				$in_comment = 1;
2381773647a0SAndy Whitcroft			}
2382773647a0SAndy Whitcroft
2383773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2384773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2385773647a0SAndy Whitcroft
2386171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2387773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2388171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2389773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2390773647a0SAndy Whitcroft		}
2391773647a0SAndy Whitcroft		push(@lines, $line);
2392773647a0SAndy Whitcroft
2393773647a0SAndy Whitcroft		if ($realcnt > 1) {
2394773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2395773647a0SAndy Whitcroft		} else {
2396773647a0SAndy Whitcroft			$realcnt = 0;
2397773647a0SAndy Whitcroft		}
2398773647a0SAndy Whitcroft
2399773647a0SAndy Whitcroft		#print "==>$rawline\n";
2400773647a0SAndy Whitcroft		#print "-->$line\n";
2401de7d4f0eSAndy Whitcroft
2402de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2403de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2404de7d4f0eSAndy Whitcroft		}
2405de7d4f0eSAndy Whitcroft	}
2406de7d4f0eSAndy Whitcroft
24076c72ffaaSAndy Whitcroft	$prefix = '';
24086c72ffaaSAndy Whitcroft
2409773647a0SAndy Whitcroft	$realcnt = 0;
2410773647a0SAndy Whitcroft	$linenr = 0;
2411194f66fcSJoe Perches	$fixlinenr = -1;
24120a920b5bSAndy Whitcroft	foreach my $line (@lines) {
24130a920b5bSAndy Whitcroft		$linenr++;
2414194f66fcSJoe Perches		$fixlinenr++;
24151b5539b1SJoe Perches		my $sline = $line;	#copy of $line
24161b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
24170a920b5bSAndy Whitcroft
2418c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
24196c72ffaaSAndy Whitcroft
242012c253abSJoe Perches# check if it's a mode change, rename or start of a patch
242112c253abSJoe Perches		if (!$in_commit_log &&
242212c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
242312c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
242412c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
242512c253abSJoe Perches			$is_patch = 1;
242612c253abSJoe Perches		}
242712c253abSJoe Perches
24280a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2429e518e9a5SJoe Perches		if (!$in_commit_log &&
243074fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
243174fd4f34SJoe Perches			my $context = $4;
24320a920b5bSAndy Whitcroft			$is_patch = 1;
24334a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
24340a920b5bSAndy Whitcroft			$realline=$1-1;
24350a920b5bSAndy Whitcroft			if (defined $2) {
24360a920b5bSAndy Whitcroft				$realcnt=$3+1;
24370a920b5bSAndy Whitcroft			} else {
24380a920b5bSAndy Whitcroft				$realcnt=1+1;
24390a920b5bSAndy Whitcroft			}
2440c2fdda0dSAndy Whitcroft			annotate_reset();
244113214adfSAndy Whitcroft			$prev_values = 'E';
244213214adfSAndy Whitcroft
2443773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2444170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
24452b474a1aSAndy Whitcroft			%suppress_export = ();
24463e469cdcSAndy Whitcroft			$suppress_statement = 0;
244774fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
244874fd4f34SJoe Perches				$context_function = $1;
244974fd4f34SJoe Perches			} else {
245074fd4f34SJoe Perches				undef $context_function;
245174fd4f34SJoe Perches			}
24520a920b5bSAndy Whitcroft			next;
24530a920b5bSAndy Whitcroft
24544a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
24554a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
24564a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2457773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
24580a920b5bSAndy Whitcroft			$realline++;
2459d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
24600a920b5bSAndy Whitcroft
24614a0df2efSAndy Whitcroft			# Measure the line length and indent.
2462c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
24630a920b5bSAndy Whitcroft
24640a920b5bSAndy Whitcroft			# Track the previous line.
24650a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
24660a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2467c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2468c2fdda0dSAndy Whitcroft
2469773647a0SAndy Whitcroft			#warn "line<$line>\n";
24706c72ffaaSAndy Whitcroft
2471d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2472d8aaf121SAndy Whitcroft			$realcnt--;
24730a920b5bSAndy Whitcroft		}
24740a920b5bSAndy Whitcroft
2475cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2476cc77cdcaSAndy Whitcroft
24776c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
24786c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2479773647a0SAndy Whitcroft
24802ac73b4fSJoe Perches		my $found_file = 0;
2481773647a0SAndy Whitcroft		# extract the filename as it passes
24823bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
24833bf9a009SRabin Vincent			$realfile = $1;
24842b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2485270c49a0SJoe Perches			$in_commit_log = 0;
24862ac73b4fSJoe Perches			$found_file = 1;
24873bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2488773647a0SAndy Whitcroft			$realfile = $1;
24892b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2490270c49a0SJoe Perches			$in_commit_log = 0;
24911e855726SWolfram Sang
24921e855726SWolfram Sang			$p1_prefix = $1;
2493e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2494e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2495000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2496000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
24971e855726SWolfram Sang			}
2498773647a0SAndy Whitcroft
2499c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2500000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2501000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2502773647a0SAndy Whitcroft			}
25032ac73b4fSJoe Perches			$found_file = 1;
25042ac73b4fSJoe Perches		}
25052ac73b4fSJoe Perches
250634d8815fSJoe Perches#make up the handle for any error we report on this line
250734d8815fSJoe Perches		if ($showfile) {
250834d8815fSJoe Perches			$prefix = "$realfile:$realline: "
250934d8815fSJoe Perches		} elsif ($emacs) {
25107d3a9f67SJoe Perches			if ($file) {
25117d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
25127d3a9f67SJoe Perches			} else {
251334d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
251434d8815fSJoe Perches			}
25157d3a9f67SJoe Perches		}
251634d8815fSJoe Perches
25172ac73b4fSJoe Perches		if ($found_file) {
251885b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
251985b0ee18SJoe Perches				WARN("OBSOLETE",
252085b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
252185b0ee18SJoe Perches			}
25227bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25232ac73b4fSJoe Perches				$check = 1;
25242ac73b4fSJoe Perches			} else {
25252ac73b4fSJoe Perches				$check = $check_orig;
25262ac73b4fSJoe Perches			}
25279f3a8992SRob Herring			$checklicenseline = 1;
2528133712a2SRob Herring
2529133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2530133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2531133712a2SRob Herring
2532133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2533133712a2SRob Herring
2534133712a2SRob Herring				if (($last_binding_patch != -1) &&
2535133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2536133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2537133712a2SRob Herring					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2538133712a2SRob Herring				}
2539133712a2SRob Herring			}
2540133712a2SRob Herring
2541773647a0SAndy Whitcroft			next;
2542773647a0SAndy Whitcroft		}
2543773647a0SAndy Whitcroft
2544389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
25450a920b5bSAndy Whitcroft
2546c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2547c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2548c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
25490a920b5bSAndy Whitcroft
25506c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
25516c72ffaaSAndy Whitcroft
2552490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2553490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2554490b292cSJoe Perches		if ($in_commit_log) {
2555490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2556490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2557490b292cSJoe Perches			}
2558490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2559490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2560490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2561490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2562490b292cSJoe Perches		}
2563490b292cSJoe Perches
2564e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2565e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2566e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2567e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2568e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2569e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2570e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2571e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2572e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2573e518e9a5SJoe Perches		}
2574e518e9a5SJoe Perches
25753bf9a009SRabin Vincent# Check for incorrect file permissions
25763bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
25773bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
257804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
257904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2580000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2581000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
25823bf9a009SRabin Vincent			}
25833bf9a009SRabin Vincent		}
25843bf9a009SRabin Vincent
2585cd261496SGeert Uytterhoeven# Check the patch for a From:
2586cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2587cd261496SGeert Uytterhoeven			$author = $1;
2588cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2589cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2590cd261496SGeert Uytterhoeven		}
2591cd261496SGeert Uytterhoeven
259220112475SJoe Perches# Check the patch for a signoff:
2593d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
25944a0df2efSAndy Whitcroft			$signoff++;
259515662b3eSJoe Perches			$in_commit_log = 0;
2596cd261496SGeert Uytterhoeven			if ($author ne '') {
2597cd261496SGeert Uytterhoeven				my $l = $line;
2598cd261496SGeert Uytterhoeven				$l =~ s/"//g;
2599cd261496SGeert Uytterhoeven				if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2600cd261496SGeert Uytterhoeven				    $authorsignoff = 1;
2601cd261496SGeert Uytterhoeven				}
2602cd261496SGeert Uytterhoeven			}
26030a920b5bSAndy Whitcroft		}
260420112475SJoe Perches
2605e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2606e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2607e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2608e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2609e0d975b1SJoe Perches		}
2610e0d975b1SJoe Perches
261120112475SJoe Perches# Check signature styles
2612270c49a0SJoe Perches		if (!$in_header_lines &&
2613ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
261420112475SJoe Perches			my $space_before = $1;
261520112475SJoe Perches			my $sign_off = $2;
261620112475SJoe Perches			my $space_after = $3;
261720112475SJoe Perches			my $email = $4;
261820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
261920112475SJoe Perches
2620ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2621ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2622ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2623ce0338dfSJoe Perches			}
262420112475SJoe Perches			if (defined $space_before && $space_before ne "") {
26253705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26263705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
26273705ce5bSJoe Perches				    $fix) {
2628194f66fcSJoe Perches					$fixed[$fixlinenr] =
26293705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26303705ce5bSJoe Perches				}
263120112475SJoe Perches			}
263220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
26333705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26343705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
26353705ce5bSJoe Perches				    $fix) {
2636194f66fcSJoe Perches					$fixed[$fixlinenr] =
26373705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26383705ce5bSJoe Perches				}
26393705ce5bSJoe Perches
264020112475SJoe Perches			}
264120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
26423705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26433705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
26443705ce5bSJoe Perches				    $fix) {
2645194f66fcSJoe Perches					$fixed[$fixlinenr] =
26463705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26473705ce5bSJoe Perches				}
264820112475SJoe Perches			}
264920112475SJoe Perches
265020112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
265120112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
265220112475SJoe Perches			if ($suggested_email eq "") {
2653000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2654000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
265520112475SJoe Perches			} else {
265620112475SJoe Perches				my $dequoted = $suggested_email;
265720112475SJoe Perches				$dequoted =~ s/^"//;
265820112475SJoe Perches				$dequoted =~ s/" </ </;
265920112475SJoe Perches				# Don't force email to have quotes
266020112475SJoe Perches				# Allow just an angle bracketed address
266120112475SJoe Perches				if ("$dequoted$comment" ne $email &&
266220112475SJoe Perches				    "<$email_address>$comment" ne $email &&
266320112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2664000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2665000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
266620112475SJoe Perches				}
26670a920b5bSAndy Whitcroft			}
26687e51f197SJoe Perches
26697e51f197SJoe Perches# Check for duplicate signatures
26707e51f197SJoe Perches			my $sig_nospace = $line;
26717e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
26727e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
26737e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
26747e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
26757e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
26767e51f197SJoe Perches			} else {
26777e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
26787e51f197SJoe Perches			}
26790a920b5bSAndy Whitcroft		}
26800a920b5bSAndy Whitcroft
2681a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2682a2fe16b9SJoe Perches		if ($in_header_lines &&
2683a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2684a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2685a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2686a2fe16b9SJoe Perches		}
2687a2fe16b9SJoe Perches
26887ebd05efSChristopher Covington# Check for unwanted Gerrit info
26897ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
26907ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
26917ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
26927ebd05efSChristopher Covington		}
26937ebd05efSChristopher Covington
2694369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2695369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2696369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2697369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2698369c8dd3SJoe Perches					# timestamp
2699369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2700369c8dd3SJoe Perches					# stack dump address
2701369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2702369c8dd3SJoe Perches		}
2703369c8dd3SJoe Perches
27042a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
27052a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2706bf4daf12SJoe Perches		    length($line) > 75 &&
2707bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2708bf4daf12SJoe Perches					# file delta changes
2709bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2710bf4daf12SJoe Perches					# filename then :
2711bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2712bf4daf12SJoe Perches					# A Fixes: or Link: line
2713bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
27142a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
27152a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
27162a076f40SJoe Perches			$commit_log_long_line = 1;
27172a076f40SJoe Perches		}
27182a076f40SJoe Perches
2719bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2720bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2721bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2722bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2723bf4daf12SJoe Perches		}
2724bf4daf12SJoe Perches
27250d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2726369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2727aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2728e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2729fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2730aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2731369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2732bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2733fe043ea1SJoe Perches			my $init_char = "c";
2734fe043ea1SJoe Perches			my $orig_commit = "";
27350d7835fcSJoe Perches			my $short = 1;
27360d7835fcSJoe Perches			my $long = 0;
27370d7835fcSJoe Perches			my $case = 1;
27380d7835fcSJoe Perches			my $space = 1;
27390d7835fcSJoe Perches			my $hasdesc = 0;
274019c146a6SJoe Perches			my $hasparens = 0;
27410d7835fcSJoe Perches			my $id = '0123456789ab';
27420d7835fcSJoe Perches			my $orig_desc = "commit description";
27430d7835fcSJoe Perches			my $description = "";
27440d7835fcSJoe Perches
2745fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2746fe043ea1SJoe Perches				$init_char = $1;
2747fe043ea1SJoe Perches				$orig_commit = lc($2);
2748fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2749fe043ea1SJoe Perches				$orig_commit = lc($1);
2750fe043ea1SJoe Perches			}
2751fe043ea1SJoe Perches
27520d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
27530d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
27540d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
27550d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
27560d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
27570d7835fcSJoe Perches				$orig_desc = $1;
275819c146a6SJoe Perches				$hasparens = 1;
27590d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
27600d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
27610d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
27620d7835fcSJoe Perches				$orig_desc = $1;
276319c146a6SJoe Perches				$hasparens = 1;
2764b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2765b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2766b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2767b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2768b671fde0SJoe Perches				$orig_desc = $1;
2769b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2770b671fde0SJoe Perches				$orig_desc .= " " . $1;
277119c146a6SJoe Perches				$hasparens = 1;
27720d7835fcSJoe Perches			}
27730d7835fcSJoe Perches
27740d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
27750d7835fcSJoe Perches							      $id, $orig_desc);
27760d7835fcSJoe Perches
2777948b133aSHeinrich Schuchardt			if (defined($id) &&
2778948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2779d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
27800d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
27810d7835fcSJoe Perches			}
2782d311cd44SJoe Perches		}
2783d311cd44SJoe Perches
278413f1937eSJoe Perches# Check for added, moved or deleted files
278513f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
278613f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
278713f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
278813f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
278913f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2790a82603a8SAndrew Jeffery			$is_patch = 1;
279113f1937eSJoe Perches			$reported_maintainer_file = 1;
279213f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
279313f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
279413f1937eSJoe Perches		}
279513f1937eSJoe Perches
279600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
27978905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2798000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2799000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
28006c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2801de7d4f0eSAndy Whitcroft		}
2802de7d4f0eSAndy Whitcroft
2803de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2804de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2805171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2806171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2807171ae1a4SAndy Whitcroft
2808171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2809171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2810171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2811171ae1a4SAndy Whitcroft
281234d99219SJoe Perches			CHK("INVALID_UTF8",
2813000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
281400df344fSAndy Whitcroft		}
28150a920b5bSAndy Whitcroft
281615662b3eSJoe Perches# Check if it's the start of a commit log
281715662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
281815662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2819eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2820eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
282115662b3eSJoe Perches			$in_header_lines = 0;
282215662b3eSJoe Perches			$in_commit_log = 1;
2823ed43c4e5SAllen Hubbe			$has_commit_log = 1;
282415662b3eSJoe Perches		}
282515662b3eSJoe Perches
2826fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2827fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2828fa64205dSPasi Savanainen		if ($in_header_lines &&
2829fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2830fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2831fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2832fa64205dSPasi Savanainen		}
2833fa64205dSPasi Savanainen
2834fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
283515662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2836fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
283715662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
283815662b3eSJoe Perches		}
283915662b3eSJoe Perches
2840d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2841d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2842d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2843d6430f71SJoe Perches				my $file = $1;
2844d6430f71SJoe Perches
2845d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2846d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2847d6430f71SJoe Perches					#
2848d6430f71SJoe Perches				} else {
2849d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2850d6430f71SJoe Perches				}
2851d6430f71SJoe Perches			}
2852d6430f71SJoe Perches		}
2853d6430f71SJoe Perches
285466b47b4aSKees Cook# Check for various typo / spelling mistakes
285566d7a382SJoe Perches		if (defined($misspellings) &&
285666d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2857ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
285866b47b4aSKees Cook				my $typo = $1;
285966b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
286066b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
286166b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
28620675a8fbSJean Delvare				my $msg_level = \&WARN;
28630675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
28640675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
286566b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
286666b47b4aSKees Cook				    $fix) {
286766b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
286866b47b4aSKees Cook				}
286966b47b4aSKees Cook			}
287066b47b4aSKees Cook		}
287166b47b4aSKees Cook
287230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
287330670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
287400df344fSAndy Whitcroft
28750a920b5bSAndy Whitcroft#trailing whitespace
28769c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2877c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2878d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2879d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2880d5e616fcSJoe Perches			    $fix) {
2881194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2882d5e616fcSJoe Perches			}
2883c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2884c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28853705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
28863705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
28873705ce5bSJoe Perches			    $fix) {
2888194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
28893705ce5bSJoe Perches			}
28903705ce5bSJoe Perches
2891d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
28920a920b5bSAndy Whitcroft		}
28935368df20SAndy Whitcroft
28944783f894SJosh Triplett# Check for FSF mailing addresses.
2895109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
28961bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
28973e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
28983e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
28994783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29000675a8fbSJean Delvare			my $msg_level = \&ERROR;
29010675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
29020675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
29034783f894SJosh 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)
29044783f894SJosh Triplett		}
29054783f894SJosh Triplett
29063354957aSAndi Kleen# check for Kconfig help text having a real description
29079fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
29089fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
29093354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2910678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
2911678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
2912678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
2913678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
29143354957aSAndi Kleen			my $length = 0;
29159fe287d7SAndy Whitcroft			my $cnt = $realcnt;
29169fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
29179fe287d7SAndy Whitcroft			my $f;
2918a1385803SAndy Whitcroft			my $is_start = 0;
29199fe287d7SAndy Whitcroft			my $is_end = 0;
2920a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
29219fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
29229fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
29239fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
29249fe287d7SAndy Whitcroft
29259fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
29268d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2927a1385803SAndy Whitcroft
292886adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
2929a1385803SAndy Whitcroft					$is_start = 1;
293084af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
293184af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
293284af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
293384af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
293484af7a61SUlf Magnusson					}
2935a1385803SAndy Whitcroft					$length = -1;
2936a1385803SAndy Whitcroft				}
2937a1385803SAndy Whitcroft
29389fe287d7SAndy Whitcroft				$f =~ s/^.//;
29393354957aSAndi Kleen				$f =~ s/#.*//;
29403354957aSAndi Kleen				$f =~ s/^\s+//;
29413354957aSAndi Kleen				next if ($f =~ /^$/);
2942678ae162SUlf Magnusson
2943678ae162SUlf Magnusson				# This only checks context lines in the patch
2944678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
2945678ae162SUlf Magnusson				# positives, even though some of these are
2946678ae162SUlf Magnusson				# common words in help texts
2947678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2948678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
29499fe287d7SAndy Whitcroft					$is_end = 1;
29509fe287d7SAndy Whitcroft					last;
29519fe287d7SAndy Whitcroft				}
29523354957aSAndi Kleen				$length++;
29533354957aSAndi Kleen			}
295456193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2955000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
295656193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
295756193274SVadim Bendebury			}
2958a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
29593354957aSAndi Kleen		}
29603354957aSAndi Kleen
2961628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
2962628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
2963628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
2964628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
2965628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
2966628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2967628f91a2SJoe Perches			    $fix) {
2968628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2969628f91a2SJoe Perches			}
2970628f91a2SJoe Perches		}
2971628f91a2SJoe Perches
2972327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2973327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2974327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2975327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2976327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2977327953e9SChristoph Jaeger		}
2978327953e9SChristoph Jaeger
2979c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2980c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2981c68e5878SArnaud Lacombe			my $flag = $1;
2982c68e5878SArnaud Lacombe			my $replacement = {
2983c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2984c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2985c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2986c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2987c68e5878SArnaud Lacombe			};
2988c68e5878SArnaud Lacombe
2989c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2990c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2991c68e5878SArnaud Lacombe		}
2992c68e5878SArnaud Lacombe
2993bff5da43SRob Herring# check for DT compatible documentation
29947dd05b38SFlorian Vaussard		if (defined $root &&
29957dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
29967dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
29977dd05b38SFlorian Vaussard
2998bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2999bff5da43SRob Herring
3000cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3001cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
3002cc93319bSFlorian Vaussard
3003bff5da43SRob Herring			foreach my $compat (@compats) {
3004bff5da43SRob Herring				my $compat2 = $compat;
3005185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3006185d566bSRob Herring				my $compat3 = $compat;
3007185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3008185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3009bff5da43SRob Herring				if ( $? >> 8 ) {
3010bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3011bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3012bff5da43SRob Herring				}
3013bff5da43SRob Herring
30144fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
30154fbf32a6SFlorian Vaussard				my $vendor = $1;
3016cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
3017bff5da43SRob Herring				if ( $? >> 8 ) {
3018bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3019cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3020bff5da43SRob Herring				}
3021bff5da43SRob Herring			}
3022bff5da43SRob Herring		}
3023bff5da43SRob Herring
30249f3a8992SRob Herring# check for using SPDX license tag at beginning of files
30259f3a8992SRob Herring		if ($realline == $checklicenseline) {
30269f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
30279f3a8992SRob Herring				$checklicenseline = 2;
30289f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
30299f3a8992SRob Herring				my $comment = "";
30309f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
30319f3a8992SRob Herring					$comment = '/*';
30329f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
30339f3a8992SRob Herring					$comment = '//';
30349f3a8992SRob Herring				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
30359f3a8992SRob Herring					$comment = '#';
30369f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
30379f3a8992SRob Herring					$comment = '..';
30389f3a8992SRob Herring				}
30399f3a8992SRob Herring
30409f3a8992SRob Herring				if ($comment !~ /^$/ &&
30419f3a8992SRob Herring				    $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
30429f3a8992SRob Herring					 WARN("SPDX_LICENSE_TAG",
30439f3a8992SRob Herring					      "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
30443b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
30453b6e8ac9SJoe Perches					 my $spdx_license = $1;
30463b6e8ac9SJoe Perches					 if (!is_SPDX_License_valid($spdx_license)) {
30473b6e8ac9SJoe Perches						  WARN("SPDX_LICENSE_TAG",
30483b6e8ac9SJoe Perches						       "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
30493b6e8ac9SJoe Perches					 }
30509f3a8992SRob Herring				}
30519f3a8992SRob Herring			}
30529f3a8992SRob Herring		}
30539f3a8992SRob Herring
30545368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3055d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
30565368df20SAndy Whitcroft
305747e0c88bSJoe Perches# line length limit (with some exclusions)
305847e0c88bSJoe Perches#
305947e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
306047e0c88bSJoe Perches#	logging functions like pr_info that end in a string
306147e0c88bSJoe Perches#	lines with a single string
306247e0c88bSJoe Perches#	#defines that are a single string
30632e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
306447e0c88bSJoe Perches#
306547e0c88bSJoe Perches# There are 3 different line length message types:
3066ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
306747e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
306847e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
306947e0c88bSJoe Perches#
307047e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
307147e0c88bSJoe Perches#
307247e0c88bSJoe Perches
3073b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
307447e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
307547e0c88bSJoe Perches
307647e0c88bSJoe Perches			# Check the allowed long line types first
307747e0c88bSJoe Perches
307847e0c88bSJoe Perches			# logging functions that end in a string that starts
307947e0c88bSJoe Perches			# before $max_line_length
308047e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
308147e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
308247e0c88bSJoe Perches				$msg_type = "";
308347e0c88bSJoe Perches
308447e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
308547e0c88bSJoe Perches			# #defines with only strings
308647e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
308747e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
308847e0c88bSJoe Perches				$msg_type = "";
308947e0c88bSJoe Perches
3090cc147506SJoe Perches			# More special cases
3091cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3092cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3093d560a5f8SJoe Perches				$msg_type = "";
3094d560a5f8SJoe Perches
30952e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
30962e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
30972e4bbbc5SAndreas Brauchli				$msg_type = "";
30982e4bbbc5SAndreas Brauchli
309947e0c88bSJoe Perches			# Otherwise set the alternate message types
310047e0c88bSJoe Perches
310147e0c88bSJoe Perches			# a comment starts before $max_line_length
310247e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
310347e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
310447e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
310547e0c88bSJoe Perches
310647e0c88bSJoe Perches			# a quoted string starts before $max_line_length
310747e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
310847e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
310947e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
311047e0c88bSJoe Perches			}
311147e0c88bSJoe Perches
311247e0c88bSJoe Perches			if ($msg_type ne "" &&
311347e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
311447e0c88bSJoe Perches				WARN($msg_type,
31156cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
31160a920b5bSAndy Whitcroft			}
311747e0c88bSJoe Perches		}
31180a920b5bSAndy Whitcroft
31198905a67cSAndy Whitcroft# check for adding lines without a newline.
31208905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3121000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3122000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
31238905a67cSAndy Whitcroft		}
31248905a67cSAndy Whitcroft
3125b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3126de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
31270a920b5bSAndy Whitcroft
31280a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
31290a920b5bSAndy Whitcroft# more than 8 must use tabs.
3130c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3131c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3132c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3133d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
31343705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
31353705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
31363705ce5bSJoe Perches			    $fix) {
3137194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
31383705ce5bSJoe Perches			}
31390a920b5bSAndy Whitcroft		}
31400a920b5bSAndy Whitcroft
314108e44365SAlberto Panizzo# check for space before tabs.
314208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
314308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
31443705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
31453705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
31463705ce5bSJoe Perches			    $fix) {
3147194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3148d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
3149194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3150c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
31513705ce5bSJoe Perches			}
315208e44365SAlberto Panizzo		}
315308e44365SAlberto Panizzo
31546a487211SJoe Perches# check for assignments on the start of a line
31556a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
31566a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
31576a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
31586a487211SJoe Perches		}
31596a487211SJoe Perches
3160d1fe9c09SJoe Perches# check for && or || at the start of a line
3161d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3162d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3163d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3164d1fe9c09SJoe Perches		}
3165d1fe9c09SJoe Perches
3166a91e8994SJoe Perches# check indentation starts on a tab stop
31675b57980dSJoe Perches		if ($perl_version_ok &&
3168bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3169a91e8994SJoe Perches			my $indent = length($1);
3170a91e8994SJoe Perches			if ($indent % 8) {
3171a91e8994SJoe Perches				if (WARN("TABSTOP",
3172a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3173a91e8994SJoe Perches				    $fix) {
3174a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3175a91e8994SJoe Perches				}
3176a91e8994SJoe Perches			}
3177a91e8994SJoe Perches		}
3178a91e8994SJoe Perches
3179d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
31805b57980dSJoe Perches		if ($perl_version_ok &&
3181fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3182d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3183d1fe9c09SJoe Perches			my $oldindent = $1;
3184d1fe9c09SJoe Perches			my $rest = $2;
3185d1fe9c09SJoe Perches
3186d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3187d1fe9c09SJoe Perches			if ($pos >= 0) {
3188b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3189b34a26f3SJoe Perches				my $newindent = $2;
3190d1fe9c09SJoe Perches
3191d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3192d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3193d1fe9c09SJoe Perches					" "  x ($pos % 8);
3194d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3195d1fe9c09SJoe Perches
3196d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3197d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
31983705ce5bSJoe Perches
31993705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
32003705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
32013705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3202194f66fcSJoe Perches						$fixed[$fixlinenr] =~
32033705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
32043705ce5bSJoe Perches					}
3205d1fe9c09SJoe Perches				}
3206d1fe9c09SJoe Perches			}
3207d1fe9c09SJoe Perches		}
3208d1fe9c09SJoe Perches
32096ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
32106ab3a970SJoe Perches# avoid checking a few false positives:
32116ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
32126ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
32136ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
32146ab3a970SJoe Perches#   multiline macros that define functions
32156ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
32166ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
32176ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
32183705ce5bSJoe Perches			if (CHK("SPACING",
3219f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
32203705ce5bSJoe Perches			    $fix) {
3221194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3222f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
32233705ce5bSJoe Perches			}
3224aad4f614SJoe Perches		}
3225aad4f614SJoe Perches
322686406b1cSJoe Perches# Block comment styles
322786406b1cSJoe Perches# Networking with an initial /*
322805880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3229fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
323085ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
323185ad978cSJoe Perches		    $realline > 2) {
323205880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
323305880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
323405880600SJoe Perches		}
323505880600SJoe Perches
323686406b1cSJoe Perches# Block comments use * on subsequent lines
323786406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
323886406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3239a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
324061135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3241a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
324286406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
324386406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3244a605e32eSJoe Perches		}
3245a605e32eSJoe Perches
324686406b1cSJoe Perches# Block comments use */ on trailing lines
324786406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3248c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3249c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3250c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
325186406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
325286406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
325305880600SJoe Perches		}
325405880600SJoe Perches
325508eb9b80SJoe Perches# Block comment * alignment
325608eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3257af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3258af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3259af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
326008eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3261af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3262af207524SJoe Perches			my $oldindent;
326308eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3264af207524SJoe Perches			if (defined($1)) {
3265af207524SJoe Perches				$oldindent = expand_tabs($1);
3266af207524SJoe Perches			} else {
3267af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3268af207524SJoe Perches				$oldindent = expand_tabs($1);
3269af207524SJoe Perches			}
327008eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
327108eb9b80SJoe Perches			my $newindent = $1;
327208eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3273af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
327408eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
327508eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
327608eb9b80SJoe Perches			}
327708eb9b80SJoe Perches		}
327808eb9b80SJoe Perches
32797f619191SJoe Perches# check for missing blank lines after struct/union declarations
32807f619191SJoe Perches# with exceptions for various attributes and macros
32817f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
32827f619191SJoe Perches		    $line =~ /^\+/ &&
32837f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
32847f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
32857f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
32867f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
32877f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
32887f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
32897f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
32900bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
32917f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3292d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3293d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3294d752fcc8SJoe Perches			    $fix) {
3295f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3296d752fcc8SJoe Perches			}
32977f619191SJoe Perches		}
32987f619191SJoe Perches
3299365dd4eaSJoe Perches# check for multiple consecutive blank lines
3300365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3301365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3302365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3303d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3304d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3305d752fcc8SJoe Perches			    $fix) {
3306f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3307d752fcc8SJoe Perches			}
3308d752fcc8SJoe Perches
3309365dd4eaSJoe Perches			$last_blank_line = $linenr;
3310365dd4eaSJoe Perches		}
3311365dd4eaSJoe Perches
33123b617e3bSJoe Perches# check for missing blank lines after declarations
33133f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
33143f7bac03SJoe Perches			# actual declarations
33153f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33165a4e1fd3SJoe Perches			# function pointer declarations
33175a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
33183f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
33193f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
33203f7bac03SJoe Perches			# known declaration macros
33213f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
33223f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
33233f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
33243f7bac03SJoe Perches			# other possible extensions of declaration lines
33253f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
33263f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
33273f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
33283f7bac03SJoe Perches			# looks like a declaration
33293f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33305a4e1fd3SJoe Perches			# function pointer declarations
33315a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
33323f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
33333f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
33343f7bac03SJoe Perches			# known declaration macros
33353f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
33363f7bac03SJoe Perches			# start of struct or union or enum
3337328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
33383f7bac03SJoe Perches			# start or end of block or continuation of declaration
33393f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
33403f7bac03SJoe Perches			# bitfield continuation
33413f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
33423f7bac03SJoe Perches			# other possible extensions of declaration lines
33433f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
33443f7bac03SJoe Perches			# indentation of previous and current line are the same
33453f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3346d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3347d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3348d752fcc8SJoe Perches			    $fix) {
3349f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3350d752fcc8SJoe Perches			}
33513b617e3bSJoe Perches		}
33523b617e3bSJoe Perches
33535f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
33546b4c5bebSAndy Whitcroft# Exceptions:
33556b4c5bebSAndy Whitcroft#  1) within comments
33566b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
33576b4c5bebSAndy Whitcroft#  3) hanging labels
33583705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
33595f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
33603705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
33613705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
33623705ce5bSJoe Perches			    $fix) {
3363194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
33643705ce5bSJoe Perches			}
33655f7ddae6SRaffaele Recalcati		}
33665f7ddae6SRaffaele Recalcati
3367b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3368b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3369b9ea10d6SAndy Whitcroft
33705751a24eSJoe Perches# check for unusual line ending [ or (
33715751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
33725751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
33735751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
33745751a24eSJoe Perches		}
33755751a24eSJoe Perches
33764dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
33774dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
33784dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
33794dbed76fSJoe Perches			$context_function = $1;
33804dbed76fSJoe Perches		}
33814dbed76fSJoe Perches
33824dbed76fSJoe Perches# check if this appears to be the end of function declaration
33834dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
33844dbed76fSJoe Perches			undef $context_function;
33854dbed76fSJoe Perches		}
33864dbed76fSJoe Perches
3387032a4c0fSJoe Perches# check indentation of any line with a bare else
3388840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3389032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3390032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3391032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3392840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3393840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3394840080a0SJoe Perches			     defined $lines[$linenr] &&
3395840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3396032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3397032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3398032a4c0fSJoe Perches			}
3399032a4c0fSJoe Perches		}
3400032a4c0fSJoe Perches
3401c00df19aSJoe Perches# check indentation of a line with a break;
3402c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3403c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3404c00df19aSJoe Perches			my $tabs = $1;
3405c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3406c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3407c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3408c00df19aSJoe Perches			}
3409c00df19aSJoe Perches		}
3410c00df19aSJoe Perches
3411c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3412cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3413000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3414000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3415c2fdda0dSAndy Whitcroft		}
341622f2a2efSAndy Whitcroft
341756e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
341856e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
341956e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
342056e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
342156e77d70SJoe Perches		}
342256e77d70SJoe Perches
34239c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
34242b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
34252b474a1aSAndy Whitcroft		    $realline_next);
34263e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3427ca819864SJoe Perches		if ($linenr > $suppress_statement &&
34281b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3429170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3430f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3431171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3432171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3433171ae1a4SAndy Whitcroft
34343e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
34353e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
34363e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
34373e469cdcSAndy Whitcroft			# until we hit end of it.
34383e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
34393e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
34403e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
34413e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
34423e469cdcSAndy Whitcroft			}
3443f74bd194SAndy Whitcroft
34442b474a1aSAndy Whitcroft			# Find the real next line.
34452b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
34462b474a1aSAndy Whitcroft			if (defined $realline_next &&
34472b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
34482b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
34492b474a1aSAndy Whitcroft				$realline_next++;
34502b474a1aSAndy Whitcroft			}
34512b474a1aSAndy Whitcroft
3452171ae1a4SAndy Whitcroft			my $s = $stat;
3453171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3454cf655043SAndy Whitcroft
3455c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3456171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3457c2fdda0dSAndy Whitcroft
3458c2fdda0dSAndy Whitcroft			# Ignore functions being called
3459171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3460c2fdda0dSAndy Whitcroft
3461463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3462463f2864SAndy Whitcroft
3463c45dcabdSAndy Whitcroft			# declarations always start with types
3464d2506586SAndy 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) {
3465c45dcabdSAndy Whitcroft				my $type = $1;
3466c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3467c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3468c45dcabdSAndy Whitcroft
34696c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3470a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3471c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3472c2fdda0dSAndy Whitcroft			}
34738905a67cSAndy Whitcroft
34746c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
347565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3476c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
34779c0ca6f9SAndy Whitcroft			}
34788905a67cSAndy Whitcroft
34798905a67cSAndy Whitcroft			# Check for any sort of function declaration.
34808905a67cSAndy Whitcroft			# int foo(something bar, other baz);
34818905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3482171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
34838905a67cSAndy Whitcroft				my ($name_len) = length($1);
34848905a67cSAndy Whitcroft
3485cf655043SAndy Whitcroft				my $ctx = $s;
3486773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
34878905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3488cf655043SAndy Whitcroft
34898905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3490c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
34918905a67cSAndy Whitcroft
3492c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
34938905a67cSAndy Whitcroft					}
34948905a67cSAndy Whitcroft				}
34958905a67cSAndy Whitcroft			}
34968905a67cSAndy Whitcroft
34979c0ca6f9SAndy Whitcroft		}
34989c0ca6f9SAndy Whitcroft
349900df344fSAndy Whitcroft#
350000df344fSAndy Whitcroft# Checks which may be anchored in the context.
350100df344fSAndy Whitcroft#
350200df344fSAndy Whitcroft
350300df344fSAndy Whitcroft# Check for switch () and associated case and default
350400df344fSAndy Whitcroft# statements should be at the same indent.
350500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
350600df344fSAndy Whitcroft			my $err = '';
350700df344fSAndy Whitcroft			my $sep = '';
350800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
350900df344fSAndy Whitcroft			shift(@ctx);
351000df344fSAndy Whitcroft			for my $ctx (@ctx) {
351100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
351200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
351300df344fSAndy Whitcroft							$indent != $cindent) {
351400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
351500df344fSAndy Whitcroft					$sep = '';
351600df344fSAndy Whitcroft				} else {
351700df344fSAndy Whitcroft					$sep = "[...]\n";
351800df344fSAndy Whitcroft				}
351900df344fSAndy Whitcroft			}
352000df344fSAndy Whitcroft			if ($err ne '') {
3521000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3522000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3523de7d4f0eSAndy Whitcroft			}
3524de7d4f0eSAndy Whitcroft		}
3525de7d4f0eSAndy Whitcroft
3526de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3527de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
35280fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3529773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3530773647a0SAndy Whitcroft
35319c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
35328eef05ddSJoe Perches
35338eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
35348eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
35358eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
35368eef05ddSJoe Perches			}
35378eef05ddSJoe Perches
3538de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3539de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3540de7d4f0eSAndy Whitcroft
3541548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3542548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3543de7d4f0eSAndy Whitcroft
3544548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3545548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3546548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3547548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3548548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3549773647a0SAndy Whitcroft				$ctx_ln++;
3550773647a0SAndy Whitcroft			}
3551548596d5SAndy Whitcroft
355253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
355353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3554773647a0SAndy Whitcroft
3555773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3556000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3557000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
355801464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
355900df344fSAndy Whitcroft			}
3560773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3561773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3562773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3563773647a0SAndy Whitcroft			{
35649c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
35659c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3566000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3567000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
356801464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
35699c0ca6f9SAndy Whitcroft				}
35709c0ca6f9SAndy Whitcroft			}
357100df344fSAndy Whitcroft		}
357200df344fSAndy Whitcroft
35734d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3574f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
35753e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
35763e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
35773e469cdcSAndy Whitcroft					if (!defined $stat);
35784d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
35794d001e4dSAndy Whitcroft
35804d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
35814d001e4dSAndy Whitcroft
35829f5af480SJoe Perches			# remove inline comments
35839f5af480SJoe Perches			$s =~ s/$;/ /g;
35849f5af480SJoe Perches			$c =~ s/$;/ /g;
35854d001e4dSAndy Whitcroft
35864d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
35876f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
35886f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
35894d001e4dSAndy Whitcroft
35909f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
35919f5af480SJoe Perches			# none on the first line, and are going to readd them
35929f5af480SJoe Perches			# where necessary.
35939f5af480SJoe Perches			$s =~ s/\n./\n/gs;
35949f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
35959f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
35969f5af480SJoe Perches			}
35979f5af480SJoe Perches
35984d001e4dSAndy Whitcroft			# We want to check the first line inside the block
35994d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
36004d001e4dSAndy Whitcroft			#  1) any blank line termination
36014d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
36024d001e4dSAndy Whitcroft			#  3) any do (...) {
36034d001e4dSAndy Whitcroft			my $continuation = 0;
36044d001e4dSAndy Whitcroft			my $check = 0;
36054d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
36064d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
36074d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
36084d001e4dSAndy Whitcroft				$continuation = 1;
36094d001e4dSAndy Whitcroft			}
36109bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
36114d001e4dSAndy Whitcroft				$check = 1;
36124d001e4dSAndy Whitcroft				$cond_lines++;
36134d001e4dSAndy Whitcroft			}
36144d001e4dSAndy Whitcroft
36154d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
36164d001e4dSAndy Whitcroft			# preprocessor statement.
36174d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
36184d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
36194d001e4dSAndy Whitcroft				$check = 0;
36204d001e4dSAndy Whitcroft			}
36214d001e4dSAndy Whitcroft
36229bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3623740504c6SAndy Whitcroft			$continuation = 0;
36249bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
36259bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
36264d001e4dSAndy Whitcroft
3627f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3628f16fa28fSAndy Whitcroft				# is not linear.
3629f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3630f16fa28fSAndy Whitcroft					$check = 0;
3631f16fa28fSAndy Whitcroft				}
3632f16fa28fSAndy Whitcroft
36339bd49efeSAndy Whitcroft				# Ignore:
36349bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
36359bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
36369bd49efeSAndy Whitcroft				#  3) labels.
3637740504c6SAndy Whitcroft				if ($continuation ||
3638740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
36399bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
36409bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3641740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
364230dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
36439bd49efeSAndy Whitcroft						$cond_lines++;
36449bd49efeSAndy Whitcroft					}
36454d001e4dSAndy Whitcroft				}
364630dad6ebSAndy Whitcroft			}
36474d001e4dSAndy Whitcroft
36484d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
36494d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
36504d001e4dSAndy Whitcroft
36514d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
36524d001e4dSAndy Whitcroft			# this is not this patch's fault.
36534d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
36544d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
36554d001e4dSAndy Whitcroft				$check = 0;
36564d001e4dSAndy Whitcroft			}
36574d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
36584d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
36594d001e4dSAndy Whitcroft			}
36604d001e4dSAndy Whitcroft
36619bd49efeSAndy 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";
36624d001e4dSAndy Whitcroft
36639f5af480SJoe Perches			if ($check && $s ne '' &&
36649f5af480SJoe Perches			    (($sindent % 8) != 0 ||
36659f5af480SJoe Perches			     ($sindent < $indent) ||
3666f6950a73SJoe Perches			     ($sindent == $indent &&
3667f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
36689f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3669000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3670000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
36714d001e4dSAndy Whitcroft			}
36724d001e4dSAndy Whitcroft		}
36734d001e4dSAndy Whitcroft
36746c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
36756c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
36761f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
36771f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
36786c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3679c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3680c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3681cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3682cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
36831f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3684c2fdda0dSAndy Whitcroft		}
36856c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
36866c72ffaaSAndy Whitcroft
368700df344fSAndy Whitcroft#ignore lines not being added
36883705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
368900df344fSAndy Whitcroft
369011ca40a0SJoe Perches# check for dereferences that span multiple lines
369111ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
369211ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
369311ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
369411ca40a0SJoe Perches			my $ref = $1;
369511ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
369611ca40a0SJoe Perches			$ref .= $1;
369711ca40a0SJoe Perches			$ref =~ s/\s//g;
369811ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
369911ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
370011ca40a0SJoe Perches		}
370111ca40a0SJoe Perches
3702a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3703c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3704a1ce18e4SJoe Perches			my $type = $1;
3705a1ce18e4SJoe Perches			my $var = $2;
3706207a8e84SJoe Perches			$var = "" if (!defined $var);
3707207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3708a1ce18e4SJoe Perches				my $sign = $1;
3709a1ce18e4SJoe Perches				my $pointer = $2;
3710a1ce18e4SJoe Perches
3711a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3712a1ce18e4SJoe Perches
3713a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3714a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3715a1ce18e4SJoe Perches				    $fix) {
3716a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3717207a8e84SJoe Perches					my $comp_pointer = $pointer;
3718207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3719207a8e84SJoe Perches					$decl .= $comp_pointer;
3720207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3721207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3722a1ce18e4SJoe Perches				}
3723a1ce18e4SJoe Perches			}
3724a1ce18e4SJoe Perches		}
3725a1ce18e4SJoe Perches
3726653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
37277429c690SAndy Whitcroft		if ($dbg_type) {
37287429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3729000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3730000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
37317429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3732000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3733000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
37347429c690SAndy Whitcroft			}
3735653d4876SAndy Whitcroft			next;
3736653d4876SAndy Whitcroft		}
3737a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3738a1ef277eSAndy Whitcroft		if ($dbg_attr) {
37399360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3740000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3741000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
37429360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3743000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3744000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3745a1ef277eSAndy Whitcroft			}
3746a1ef277eSAndy Whitcroft			next;
3747a1ef277eSAndy Whitcroft		}
3748653d4876SAndy Whitcroft
3749f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
375099423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
375199423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3752d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3753d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3754f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3755f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3756f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3757d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3758d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3759f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3760d752fcc8SJoe Perches				$fixedline = $line;
37618d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3762f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3763d752fcc8SJoe Perches			}
3764f0a594c1SAndy Whitcroft		}
3765f0a594c1SAndy Whitcroft
376600df344fSAndy Whitcroft#
376700df344fSAndy Whitcroft# Checks which are anchored on the added line.
376800df344fSAndy Whitcroft#
376900df344fSAndy Whitcroft
3770653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3771c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3772653d4876SAndy Whitcroft			my $path = $1;
3773653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3774000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3775495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3776495e9d84SJoe Perches			}
3777495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3778495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3779495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3780653d4876SAndy Whitcroft			}
3781653d4876SAndy Whitcroft		}
3782653d4876SAndy Whitcroft
378300df344fSAndy Whitcroft# no C99 // comments
378400df344fSAndy Whitcroft		if ($line =~ m{//}) {
37853705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
37863705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
37873705ce5bSJoe Perches			    $fix) {
3788194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
37893705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
37903705ce5bSJoe Perches					my $comment = trim($1);
3791194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
37923705ce5bSJoe Perches				}
37933705ce5bSJoe Perches			}
379400df344fSAndy Whitcroft		}
379500df344fSAndy Whitcroft		# Remove C99 comments.
37960a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
37976c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
37980a920b5bSAndy Whitcroft
37992b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
38002b474a1aSAndy Whitcroft# the whole statement.
38012b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
38022b474a1aSAndy Whitcroft		if (defined $realline_next &&
38032b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
38042b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
38052b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
38062b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
38073cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
38083cbf62dfSAndy Whitcroft			# a prefix:
38093cbf62dfSAndy Whitcroft			#   XXX(foo);
38103cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3811653d4876SAndy Whitcroft			my $name = $1;
381287a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
38133cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
38143cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
38153cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38163cbf62dfSAndy Whitcroft
38173cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
38182b474a1aSAndy Whitcroft				\n.}\s*$|
381948012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
382048012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
382148012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
38222b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
38232b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
382448012058SAndy Whitcroft			    )/x) {
38252b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
38262b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
38272b474a1aSAndy Whitcroft			} else {
38282b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38290a920b5bSAndy Whitcroft			}
38300a920b5bSAndy Whitcroft		}
38312b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
38322b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
38332b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
38342b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
38352b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
38362b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
38372b474a1aSAndy Whitcroft		}
38382b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
38392b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3840000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3841000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
38422b474a1aSAndy Whitcroft		}
38430a920b5bSAndy Whitcroft
38445150bda4SJoe Eloff# check for global initialisers.
38456d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3846d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
38476d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3848d5e616fcSJoe Perches			    $fix) {
38496d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3850d5e616fcSJoe Perches			}
3851f0a594c1SAndy Whitcroft		}
38520a920b5bSAndy Whitcroft# check for static initialisers.
38536d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3854d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
38556d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3856d5e616fcSJoe Perches				      $herecurr) &&
3857d5e616fcSJoe Perches			    $fix) {
38586d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3859d5e616fcSJoe Perches			}
38600a920b5bSAndy Whitcroft		}
38610a920b5bSAndy Whitcroft
38621813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
38631813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
38641813087dSJoe Perches			my $tmp = trim($1);
38651813087dSJoe Perches			WARN("MISORDERED_TYPE",
38661813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
38671813087dSJoe Perches		}
38681813087dSJoe Perches
3869809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3870809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3871809e082eSJoe Perches			my $type = trim($1);
3872809e082eSJoe Perches			next if ($type !~ /\bint\b/);
3873809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3874809e082eSJoe Perches			my $new_type = $type;
3875809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
3876809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3877809e082eSJoe Perches			$new_type =~ s/^const\s+//;
3878809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3879809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
3880809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
3881809e082eSJoe Perches			$new_type = trim($new_type);
3882809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
3883809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3884809e082eSJoe Perches			    $fix) {
3885809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3886809e082eSJoe Perches			}
3887809e082eSJoe Perches		}
3888809e082eSJoe Perches
3889cb710ecaSJoe Perches# check for static const char * arrays.
3890cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3891000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3892000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3893cb710ecaSJoe Perches				$herecurr);
3894cb710ecaSJoe Perches		}
3895cb710ecaSJoe Perches
389677b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
389777b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
389877b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
389977b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
390077b8c0a8SJoe Perches			    $fix) {
390177b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
390277b8c0a8SJoe Perches			}
390377b8c0a8SJoe Perches		}
390477b8c0a8SJoe Perches
3905cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3906cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3907000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3908000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3909cb710ecaSJoe Perches				$herecurr);
3910cb710ecaSJoe Perches		}
3911cb710ecaSJoe Perches
3912ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3913ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3914ab7e23f3SJoe Perches			my $found = $1;
3915ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3916ab7e23f3SJoe Perches				WARN("CONST_CONST",
3917ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3918ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3919ab7e23f3SJoe Perches				WARN("CONST_CONST",
3920ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3921ab7e23f3SJoe Perches			}
3922ab7e23f3SJoe Perches		}
3923ab7e23f3SJoe Perches
39249b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
39259b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
39269b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
39279b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
39289b0fa60dSJoe Perches				$herecurr);
39299b0fa60dSJoe Perches               }
39309b0fa60dSJoe Perches
3931b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3932b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3933b598b670SJoe Perches			my $array = $1;
3934b598b670SJoe 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*\))@) {
3935b598b670SJoe Perches				my $array_div = $1;
3936b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3937b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3938b598b670SJoe Perches				    $fix) {
3939b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3940b598b670SJoe Perches				}
3941b598b670SJoe Perches			}
3942b598b670SJoe Perches		}
3943b598b670SJoe Perches
3944b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3945b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3946b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3947b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3948b36190c5SJoe Perches			    $fix) {
3949194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3950b36190c5SJoe Perches			}
3951b36190c5SJoe Perches		}
3952b36190c5SJoe Perches
3953653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3954653d4876SAndy Whitcroft# make sense.
3955653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
39568054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3957c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
39588ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
395946d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
3960000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3961000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
39620a920b5bSAndy Whitcroft		}
39630a920b5bSAndy Whitcroft
39640a920b5bSAndy Whitcroft# * goes on variable not on type
396565863862SAndy Whitcroft		# (char*[ const])
3966bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3967bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
39683705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3969d8aaf121SAndy Whitcroft
397065863862SAndy Whitcroft			# Should start with a space.
397165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
397265863862SAndy Whitcroft			# Should not end with a space.
397365863862SAndy Whitcroft			$to =~ s/\s+$//;
397465863862SAndy Whitcroft			# '*'s should not have spaces between.
3975f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
397665863862SAndy Whitcroft			}
3977d8aaf121SAndy Whitcroft
39783705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
397965863862SAndy Whitcroft			if ($from ne $to) {
39803705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
39813705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
39823705ce5bSJoe Perches				    $fix) {
39833705ce5bSJoe Perches					my $sub_from = $ident;
39843705ce5bSJoe Perches					my $sub_to = $ident;
39853705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3986194f66fcSJoe Perches					$fixed[$fixlinenr] =~
39873705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
39883705ce5bSJoe Perches				}
398965863862SAndy Whitcroft			}
3990bfcb2cc7SAndy Whitcroft		}
3991bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3992bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
39933705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3994d8aaf121SAndy Whitcroft
399565863862SAndy Whitcroft			# Should start with a space.
399665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
399765863862SAndy Whitcroft			# Should not end with a space.
399865863862SAndy Whitcroft			$to =~ s/\s+$//;
399965863862SAndy Whitcroft			# '*'s should not have spaces between.
4000f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
400165863862SAndy Whitcroft			}
400265863862SAndy Whitcroft			# Modifiers should have spaces.
400365863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
400465863862SAndy Whitcroft
40053705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4006667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
40073705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40083705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
40093705ce5bSJoe Perches				    $fix) {
40103705ce5bSJoe Perches
40113705ce5bSJoe Perches					my $sub_from = $match;
40123705ce5bSJoe Perches					my $sub_to = $match;
40133705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4014194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40153705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40163705ce5bSJoe Perches				}
401765863862SAndy Whitcroft			}
40180a920b5bSAndy Whitcroft		}
40190a920b5bSAndy Whitcroft
40209d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
40219d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
40220675a8fbSJean Delvare			my $msg_level = \&WARN;
40230675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
40240675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
40259d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
40269d3e3c70SJoe Perches		}
40270a920b5bSAndy Whitcroft
40289d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
40298905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4030000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4031000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
40328905a67cSAndy Whitcroft		}
40338905a67cSAndy Whitcroft
403417441227SJoe Perches# check for uses of printk_ratelimit
403517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4036000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4037000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
403817441227SJoe Perches		}
403917441227SJoe Perches
4040eeef5733SJoe Perches# printk should use KERN_* levels
4041eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4042000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4043eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
404400df344fSAndy Whitcroft		}
40450a920b5bSAndy Whitcroft
4046243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4047243f3803SJoe Perches			my $orig = $1;
4048243f3803SJoe Perches			my $level = lc($orig);
4049243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
40508f26b837SJoe Perches			my $level2 = $level;
40518f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4052243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4053daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4054243f3803SJoe Perches		}
4055243f3803SJoe Perches
4056243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
4057d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
4058d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4059d5e616fcSJoe Perches			    $fix) {
4060194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4061d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
4062d5e616fcSJoe Perches			}
4063243f3803SJoe Perches		}
4064243f3803SJoe Perches
4065dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4066dc139313SJoe Perches			my $orig = $1;
4067dc139313SJoe Perches			my $level = lc($orig);
4068dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4069dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4070dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4071dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4072dc139313SJoe Perches		}
4073dc139313SJoe Perches
407491c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
407591c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
407691c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
407791c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
407891c9afafSAndy Lutomirski			WARN("ENOSYS",
407991c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
408091c9afafSAndy Lutomirski		}
408191c9afafSAndy Lutomirski
4082653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4083653d4876SAndy Whitcroft# or if closed on same line
40845b57980dSJoe Perches		if ($perl_version_ok &&
40852d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
40862d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
40872d453e3bSJoe Perches		    $sline !~ /}/) {
40888d182478SJoe Perches			if (ERROR("OPEN_BRACE",
40892d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
40908d182478SJoe Perches			    $fix) {
40918d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
40928d182478SJoe Perches				my $fixed_line = $rawline;
40938d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
40948d182478SJoe Perches				my $line1 = $1;
40958d182478SJoe Perches				my $line2 = $2;
40968d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
40978d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
40988d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
40998d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
41008d182478SJoe Perches				}
41018d182478SJoe Perches			}
41020a920b5bSAndy Whitcroft		}
4103653d4876SAndy Whitcroft
41048905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
41058905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
41068905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
41078d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41088d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
41098d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41108d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
41118d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41128d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
41138d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
41148d182478SJoe Perches				$fixedline = $rawline;
41158d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
41168d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
41178d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41188d182478SJoe Perches				}
41198d182478SJoe Perches			}
41208905a67cSAndy Whitcroft		}
41218905a67cSAndy Whitcroft
41220c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
41233705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
41243705ce5bSJoe Perches			if (WARN("SPACING",
41253705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
41263705ce5bSJoe Perches			    $fix) {
4127194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41283705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
41293705ce5bSJoe Perches			}
41300c73b4ebSAndy Whitcroft		}
41310c73b4ebSAndy Whitcroft
413231070b5dSJoe Perches# Function pointer declarations
413331070b5dSJoe Perches# check spacing between type, funcptr, and args
413431070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
413591f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
413631070b5dSJoe Perches			my $declare = $1;
413731070b5dSJoe Perches			my $pre_pointer_space = $2;
413831070b5dSJoe Perches			my $post_pointer_space = $3;
413931070b5dSJoe Perches			my $funcname = $4;
414031070b5dSJoe Perches			my $post_funcname_space = $5;
414131070b5dSJoe Perches			my $pre_args_space = $6;
414231070b5dSJoe Perches
414391f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
414491f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
414591f72e9cSJoe Perches# don't need a space so don't warn for those.
414691f72e9cSJoe Perches			my $post_declare_space = "";
414791f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
414891f72e9cSJoe Perches				$post_declare_space = $1;
414991f72e9cSJoe Perches				$declare = rtrim($declare);
415091f72e9cSJoe Perches			}
415191f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
415231070b5dSJoe Perches				WARN("SPACING",
415331070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
415491f72e9cSJoe Perches				$post_declare_space = " ";
415531070b5dSJoe Perches			}
415631070b5dSJoe Perches
415731070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
415891f72e9cSJoe Perches# This test is not currently implemented because these declarations are
415991f72e9cSJoe Perches# equivalent to
416091f72e9cSJoe Perches#	int  foo(int bar, ...)
416191f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
416291f72e9cSJoe Perches#
416391f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
416491f72e9cSJoe Perches#				WARN("SPACING",
416591f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
416691f72e9cSJoe Perches#			}
416731070b5dSJoe Perches
416831070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
416931070b5dSJoe Perches			if (defined $pre_pointer_space &&
417031070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
417131070b5dSJoe Perches				WARN("SPACING",
417231070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
417331070b5dSJoe Perches			}
417431070b5dSJoe Perches
417531070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
417631070b5dSJoe Perches			if (defined $post_pointer_space &&
417731070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
417831070b5dSJoe Perches				WARN("SPACING",
417931070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
418031070b5dSJoe Perches			}
418131070b5dSJoe Perches
418231070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
418331070b5dSJoe Perches			if (defined $post_funcname_space &&
418431070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
418531070b5dSJoe Perches				WARN("SPACING",
418631070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
418731070b5dSJoe Perches			}
418831070b5dSJoe Perches
418931070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
419031070b5dSJoe Perches			if (defined $pre_args_space &&
419131070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
419231070b5dSJoe Perches				WARN("SPACING",
419331070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
419431070b5dSJoe Perches			}
419531070b5dSJoe Perches
419631070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4197194f66fcSJoe Perches				$fixed[$fixlinenr] =~
419891f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
419931070b5dSJoe Perches			}
420031070b5dSJoe Perches		}
420131070b5dSJoe Perches
42028d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
42038d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4204fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4205fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
42068d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
42078d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
42088d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4209fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
421038dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
42113705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
42123705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
42133705ce5bSJoe Perches				    $fix) {
4214194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
42153705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
42163705ce5bSJoe Perches				}
42178d31cfceSAndy Whitcroft			}
42188d31cfceSAndy Whitcroft		}
42198d31cfceSAndy Whitcroft
4220f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
42216c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4222c2fdda0dSAndy Whitcroft			my $name = $1;
4223773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4224773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4225c2fdda0dSAndy Whitcroft
4226c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4227773647a0SAndy Whitcroft			if ($name =~ /^(?:
4228773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4229773647a0SAndy Whitcroft				volatile|__volatile__|
4230773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4231773647a0SAndy Whitcroft				asm|__asm__)$/x)
4232773647a0SAndy Whitcroft			{
4233c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4234c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4235c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4236c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4237773647a0SAndy Whitcroft
4238773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4239c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4240c2fdda0dSAndy Whitcroft
4241c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4242c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4243773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4244c2fdda0dSAndy Whitcroft
4245c2fdda0dSAndy Whitcroft			} else {
42463705ce5bSJoe Perches				if (WARN("SPACING",
42473705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
42483705ce5bSJoe Perches					     $fix) {
4249194f66fcSJoe Perches					$fixed[$fixlinenr] =~
42503705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
42513705ce5bSJoe Perches				}
4252f0a594c1SAndy Whitcroft			}
42536c72ffaaSAndy Whitcroft		}
42549a4cad4eSEric Nelson
4255653d4876SAndy Whitcroft# Check operator spacing.
42560a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
42573705ce5bSJoe Perches			my $fixed_line = "";
42583705ce5bSJoe Perches			my $line_fixed = 0;
42593705ce5bSJoe Perches
42609c0ca6f9SAndy Whitcroft			my $ops = qr{
42619c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
42629c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
42639c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
42641f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
426584731623SJoe Perches				\?:|\?|:
42669c0ca6f9SAndy Whitcroft			}x;
4267cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
42683705ce5bSJoe Perches
42693705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
42703705ce5bSJoe Perches##			foreach my $el (@elements) {
42713705ce5bSJoe Perches##				print("el: <$el>\n");
42723705ce5bSJoe Perches##			}
42733705ce5bSJoe Perches
42743705ce5bSJoe Perches			my @fix_elements = ();
427500df344fSAndy Whitcroft			my $off = 0;
42766c72ffaaSAndy Whitcroft
42773705ce5bSJoe Perches			foreach my $el (@elements) {
42783705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
42793705ce5bSJoe Perches				$off += length($el);
42803705ce5bSJoe Perches			}
42813705ce5bSJoe Perches
42823705ce5bSJoe Perches			$off = 0;
42833705ce5bSJoe Perches
42846c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4285b34c648bSJoe Perches			my $last_after = -1;
42866c72ffaaSAndy Whitcroft
42870a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
42883705ce5bSJoe Perches
42893705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
42903705ce5bSJoe Perches
42913705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
42923705ce5bSJoe Perches
42934a0df2efSAndy Whitcroft				$off += length($elements[$n]);
42944a0df2efSAndy Whitcroft
429525985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4296773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4297773647a0SAndy Whitcroft				my $cc = '';
4298773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4299773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4300773647a0SAndy Whitcroft				}
4301773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4302773647a0SAndy Whitcroft
43034a0df2efSAndy Whitcroft				my $a = '';
43044a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
43054a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4306cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
43074a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
43084a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4309773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
43104a0df2efSAndy Whitcroft
43110a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
43124a0df2efSAndy Whitcroft
43134a0df2efSAndy Whitcroft				my $c = '';
43140a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
43154a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
43164a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4317cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
43184a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
43194a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
43208b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
43214a0df2efSAndy Whitcroft				} else {
43224a0df2efSAndy Whitcroft					$c = 'E';
43230a920b5bSAndy Whitcroft				}
43240a920b5bSAndy Whitcroft
43254a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
43264a0df2efSAndy Whitcroft
43274a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
43284a0df2efSAndy Whitcroft
43296c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4330de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
43310a920b5bSAndy Whitcroft
433274048ed8SAndy Whitcroft				# Pull out the value of this operator.
43336c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
43340a920b5bSAndy Whitcroft
43351f65f947SAndy Whitcroft				# Get the full operator variant.
43361f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
43371f65f947SAndy Whitcroft
433813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
433913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4340d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
434113214adfSAndy Whitcroft
4342cf655043SAndy Whitcroft#				# Ignore comments
4343cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
434413214adfSAndy Whitcroft
4345d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
434613214adfSAndy Whitcroft				} elsif ($op eq ';') {
4347cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4348cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
43493705ce5bSJoe Perches						if (ERROR("SPACING",
43503705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4351b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
43523705ce5bSJoe Perches							$line_fixed = 1;
43533705ce5bSJoe Perches						}
4354d8aaf121SAndy Whitcroft					}
4355d8aaf121SAndy Whitcroft
4356d8aaf121SAndy Whitcroft				# // is a comment
4357d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
43580a920b5bSAndy Whitcroft
4359b00e4814SJoe Perches				#   :   when part of a bitfield
4360b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4361b00e4814SJoe Perches					# skip the bitfield test for now
4362b00e4814SJoe Perches
43631f65f947SAndy Whitcroft				# No spaces for:
43641f65f947SAndy Whitcroft				#   ->
4365b00e4814SJoe Perches				} elsif ($op eq '->') {
43664a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
43673705ce5bSJoe Perches						if (ERROR("SPACING",
43683705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4369b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43703705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
43713705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
43723705ce5bSJoe Perches							}
4373b34c648bSJoe Perches							$line_fixed = 1;
43743705ce5bSJoe Perches						}
43750a920b5bSAndy Whitcroft					}
43760a920b5bSAndy Whitcroft
43772381097bSJoe Perches				# , must not have a space before and must have a space on the right.
43780a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
43792381097bSJoe Perches					my $rtrim_before = 0;
43802381097bSJoe Perches					my $space_after = 0;
43812381097bSJoe Perches					if ($ctx =~ /Wx./) {
43822381097bSJoe Perches						if (ERROR("SPACING",
43832381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
43842381097bSJoe Perches							$line_fixed = 1;
43852381097bSJoe Perches							$rtrim_before = 1;
43862381097bSJoe Perches						}
43872381097bSJoe Perches					}
4388cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
43893705ce5bSJoe Perches						if (ERROR("SPACING",
43903705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
43913705ce5bSJoe Perches							$line_fixed = 1;
4392b34c648bSJoe Perches							$last_after = $n;
43932381097bSJoe Perches							$space_after = 1;
43942381097bSJoe Perches						}
43952381097bSJoe Perches					}
43962381097bSJoe Perches					if ($rtrim_before || $space_after) {
43972381097bSJoe Perches						if ($rtrim_before) {
43982381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43992381097bSJoe Perches						} else {
44002381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
44012381097bSJoe Perches						}
44022381097bSJoe Perches						if ($space_after) {
44032381097bSJoe Perches							$good .= " ";
44043705ce5bSJoe Perches						}
44050a920b5bSAndy Whitcroft					}
44060a920b5bSAndy Whitcroft
44079c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
440874048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
44099c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
44109c0ca6f9SAndy Whitcroft
44119c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
44129c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
44139c0ca6f9SAndy Whitcroft				# unary operator, or a cast
44149c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
441574048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
44160d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4417cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
44183705ce5bSJoe Perches						if (ERROR("SPACING",
44193705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4420b34c648bSJoe Perches							if ($n != $last_after + 2) {
4421b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
44223705ce5bSJoe Perches								$line_fixed = 1;
44233705ce5bSJoe Perches							}
44240a920b5bSAndy Whitcroft						}
4425b34c648bSJoe Perches					}
4426a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4427171ae1a4SAndy Whitcroft						# A unary '*' may be const
4428171ae1a4SAndy Whitcroft
4429171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
44303705ce5bSJoe Perches						if (ERROR("SPACING",
44313705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4432b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
44333705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44343705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44353705ce5bSJoe Perches							}
4436b34c648bSJoe Perches							$line_fixed = 1;
44373705ce5bSJoe Perches						}
44380a920b5bSAndy Whitcroft					}
44390a920b5bSAndy Whitcroft
44400a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
44410a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4442773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
44433705ce5bSJoe Perches						if (ERROR("SPACING",
44443705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4445b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44463705ce5bSJoe Perches							$line_fixed = 1;
44473705ce5bSJoe Perches						}
44480a920b5bSAndy Whitcroft					}
4449773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4450773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
44513705ce5bSJoe Perches						if (ERROR("SPACING",
44523705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4453b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44543705ce5bSJoe Perches							$line_fixed = 1;
44553705ce5bSJoe Perches						}
4456653d4876SAndy Whitcroft					}
4457773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
44583705ce5bSJoe Perches						if (ERROR("SPACING",
44593705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4460b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
44613705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44623705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4463773647a0SAndy Whitcroft							}
4464b34c648bSJoe Perches							$line_fixed = 1;
44653705ce5bSJoe Perches						}
44663705ce5bSJoe Perches					}
44670a920b5bSAndy Whitcroft
44680a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
44699c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
44709c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
44719c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4472c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4473c2fdda0dSAndy Whitcroft					 $op eq '%')
44740a920b5bSAndy Whitcroft				{
4475d2e025f3SJoe Perches					if ($check) {
4476d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4477d2e025f3SJoe Perches							if (CHK("SPACING",
4478d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4479d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4480d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4481d2e025f3SJoe Perches								$line_fixed = 1;
4482d2e025f3SJoe Perches							}
4483d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4484d2e025f3SJoe Perches							if (CHK("SPACING",
4485d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4486d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4487d2e025f3SJoe Perches								$line_fixed = 1;
4488d2e025f3SJoe Perches							}
4489d2e025f3SJoe Perches						}
4490d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
44913705ce5bSJoe Perches						if (ERROR("SPACING",
44923705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4493b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4494b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4495b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4496b34c648bSJoe Perches							}
44973705ce5bSJoe Perches							$line_fixed = 1;
44983705ce5bSJoe Perches						}
44990a920b5bSAndy Whitcroft					}
45000a920b5bSAndy Whitcroft
45011f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
45021f65f947SAndy Whitcroft				# terminating a case value or a label.
45031f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
45041f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
45053705ce5bSJoe Perches						if (ERROR("SPACING",
45063705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4507b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45083705ce5bSJoe Perches							$line_fixed = 1;
45093705ce5bSJoe Perches						}
45101f65f947SAndy Whitcroft					}
45111f65f947SAndy Whitcroft
45120a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4513cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
45141f65f947SAndy Whitcroft					my $ok = 0;
45151f65f947SAndy Whitcroft
451622f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
45171f65f947SAndy Whitcroft					if (($op eq '<' &&
45181f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
45191f65f947SAndy Whitcroft					    ($op eq '>' &&
45201f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
45211f65f947SAndy Whitcroft					{
45221f65f947SAndy Whitcroft					    	$ok = 1;
45231f65f947SAndy Whitcroft					}
45241f65f947SAndy Whitcroft
4525e0df7e1fSJoe Perches					# for asm volatile statements
4526e0df7e1fSJoe Perches					# ignore a colon with another
4527e0df7e1fSJoe Perches					# colon immediately before or after
4528e0df7e1fSJoe Perches					if (($op eq ':') &&
4529e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4530e0df7e1fSJoe Perches						$ok = 1;
4531e0df7e1fSJoe Perches					}
4532e0df7e1fSJoe Perches
453384731623SJoe Perches					# messages are ERROR, but ?: are CHK
45341f65f947SAndy Whitcroft					if ($ok == 0) {
45350675a8fbSJean Delvare						my $msg_level = \&ERROR;
45360675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
453784731623SJoe Perches
45380675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
45393705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4540b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4541b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4542b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4543b34c648bSJoe Perches							}
45443705ce5bSJoe Perches							$line_fixed = 1;
45453705ce5bSJoe Perches						}
45460a920b5bSAndy Whitcroft					}
454722f2a2efSAndy Whitcroft				}
45484a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
45493705ce5bSJoe Perches
45503705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
45513705ce5bSJoe Perches
45523705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
45530a920b5bSAndy Whitcroft			}
45543705ce5bSJoe Perches
45553705ce5bSJoe Perches			if (($#elements % 2) == 0) {
45563705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
45573705ce5bSJoe Perches			}
45583705ce5bSJoe Perches
4559194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4560194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
45613705ce5bSJoe Perches			}
45623705ce5bSJoe Perches
45633705ce5bSJoe Perches
45640a920b5bSAndy Whitcroft		}
45650a920b5bSAndy Whitcroft
4566786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4567d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4568786b6326SJoe Perches			if (WARN("SPACING",
4569786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4570786b6326SJoe Perches			    $fix) {
4571194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4572786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4573786b6326SJoe Perches			}
4574786b6326SJoe Perches		}
4575786b6326SJoe Perches
4576f0a594c1SAndy Whitcroft# check for multiple assignments
4577f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4578000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4579000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4580f0a594c1SAndy Whitcroft		}
4581f0a594c1SAndy Whitcroft
458222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
458322f2a2efSAndy Whitcroft## # continuation.
458422f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
458522f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
458622f2a2efSAndy Whitcroft##
458722f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
458822f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
458922f2a2efSAndy Whitcroft## 			my $ln = $line;
459022f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
459122f2a2efSAndy Whitcroft## 			}
459222f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4593000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4594000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
459522f2a2efSAndy Whitcroft## 			}
459622f2a2efSAndy Whitcroft## 		}
4597f0a594c1SAndy Whitcroft
45980a920b5bSAndy Whitcroft#need space before brace following if, while, etc
45996b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
46006ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
46013705ce5bSJoe Perches			if (ERROR("SPACING",
46023705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
46033705ce5bSJoe Perches			    $fix) {
46046ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
46053705ce5bSJoe Perches			}
4606de7d4f0eSAndy Whitcroft		}
4607de7d4f0eSAndy Whitcroft
4608c4a62ef9SJoe Perches## # check for blank lines before declarations
4609c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4610c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4611c4a62ef9SJoe Perches##			WARN("SPACING",
4612c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4613c4a62ef9SJoe Perches##		}
4614c4a62ef9SJoe Perches##
4615c4a62ef9SJoe Perches
4616de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4617de7d4f0eSAndy Whitcroft# on the line
4618de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4619d5e616fcSJoe Perches			if (ERROR("SPACING",
4620d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4621d5e616fcSJoe Perches			    $fix) {
4622194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4623d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4624d5e616fcSJoe Perches			}
46250a920b5bSAndy Whitcroft		}
46260a920b5bSAndy Whitcroft
462722f2a2efSAndy Whitcroft# check spacing on square brackets
462822f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
46293705ce5bSJoe Perches			if (ERROR("SPACING",
46303705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
46313705ce5bSJoe Perches			    $fix) {
4632194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46333705ce5bSJoe Perches				    s/\[\s+/\[/;
46343705ce5bSJoe Perches			}
463522f2a2efSAndy Whitcroft		}
463622f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
46373705ce5bSJoe Perches			if (ERROR("SPACING",
46383705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
46393705ce5bSJoe Perches			    $fix) {
4640194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46413705ce5bSJoe Perches				    s/\s+\]/\]/;
46423705ce5bSJoe Perches			}
464322f2a2efSAndy Whitcroft		}
464422f2a2efSAndy Whitcroft
4645c45dcabdSAndy Whitcroft# check spacing on parentheses
46469c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
46479c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
46483705ce5bSJoe Perches			if (ERROR("SPACING",
46493705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
46503705ce5bSJoe Perches			    $fix) {
4651194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46523705ce5bSJoe Perches				    s/\(\s+/\(/;
46533705ce5bSJoe Perches			}
465422f2a2efSAndy Whitcroft		}
465513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4656c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4657c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
46583705ce5bSJoe Perches			if (ERROR("SPACING",
46593705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
46603705ce5bSJoe Perches			    $fix) {
4661194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46623705ce5bSJoe Perches				    s/\s+\)/\)/;
46633705ce5bSJoe Perches			}
466422f2a2efSAndy Whitcroft		}
466522f2a2efSAndy Whitcroft
4666e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4667e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4668e2826fd0SJoe Perches
4669e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4670ea4acbb1SJoe Perches			my $var = $1;
4671ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4672ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4673ea4acbb1SJoe Perches			    $fix) {
4674ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4675ea4acbb1SJoe Perches			}
4676ea4acbb1SJoe Perches		}
4677ea4acbb1SJoe Perches
4678ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4679ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4680ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4681ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4682ea4acbb1SJoe Perches			my $var = $2;
4683ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4684ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4685ea4acbb1SJoe Perches			    $fix) {
4686ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4687ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4688ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4689ea4acbb1SJoe Perches			}
4690e2826fd0SJoe Perches		}
4691e2826fd0SJoe Perches
469263b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4693a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4694a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
46955b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
469663b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
469763b7c73eSJoe Perches			my $if_stat = $1;
469863b7c73eSJoe Perches			my $test = substr($2, 1, -1);
469963b7c73eSJoe Perches			my $herectx;
470063b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
470163b7c73eSJoe Perches				my $match = $1;
470263b7c73eSJoe Perches				# avoid parentheses around potential macro args
470363b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
470463b7c73eSJoe Perches				if (!defined($herectx)) {
470563b7c73eSJoe Perches					$herectx = $here . "\n";
470663b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
470763b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
470863b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
470963b7c73eSJoe Perches						$herectx .=  $rl . "\n";
471063b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
471163b7c73eSJoe Perches					}
471263b7c73eSJoe Perches				}
471363b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
471463b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
471563b7c73eSJoe Perches			}
471663b7c73eSJoe Perches		}
471763b7c73eSJoe Perches
47180a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
47194a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
47200a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
47213705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
47223705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
47233705ce5bSJoe Perches			    $fix) {
4724194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47253705ce5bSJoe Perches				    s/^(.)\s+/$1/;
47263705ce5bSJoe Perches			}
47270a920b5bSAndy Whitcroft		}
47280a920b5bSAndy Whitcroft
47295b9553abSJoe Perches# return is not a function
4730507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4731c45dcabdSAndy Whitcroft			my $spacing = $1;
47325b57980dSJoe Perches			if ($perl_version_ok &&
47335b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
47345b9553abSJoe Perches				my $value = $1;
47355b9553abSJoe Perches				$value = deparenthesize($value);
47365b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4737000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4738000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
47395b9553abSJoe Perches				}
4740c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4741000d1cc1SJoe Perches				ERROR("SPACING",
4742000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4743c45dcabdSAndy Whitcroft			}
4744c45dcabdSAndy Whitcroft		}
4745507e5141SJoe Perches
4746b43ae21bSJoe Perches# unnecessary return in a void function
4747b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4748b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4749b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4750b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4751b43ae21bSJoe Perches		    $linenr >= 3 &&
4752b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4753b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
47549819cf25SJoe Perches			WARN("RETURN_VOID",
4755b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
47569819cf25SJoe Perches               }
47579819cf25SJoe Perches
4758189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
47595b57980dSJoe Perches		if ($perl_version_ok &&
4760189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4761189248d8SJoe Perches			my $openparens = $1;
4762189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4763189248d8SJoe Perches			my $msg = "";
4764189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4765189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4766189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4767189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4768189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4769189248d8SJoe Perches			}
4770189248d8SJoe Perches		}
4771189248d8SJoe Perches
4772c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4773c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4774c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4775c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
47765b57980dSJoe Perches		if ($perl_version_ok &&
4777c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4778c5595fa2SJoe Perches			my $lead = $1;
4779c5595fa2SJoe Perches			my $const = $2;
4780c5595fa2SJoe Perches			my $comp = $3;
4781c5595fa2SJoe Perches			my $to = $4;
4782c5595fa2SJoe Perches			my $newcomp = $comp;
4783f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4784c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4785c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4786c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4787c5595fa2SJoe Perches			    $fix) {
4788c5595fa2SJoe Perches				if ($comp eq "<") {
4789c5595fa2SJoe Perches					$newcomp = ">";
4790c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4791c5595fa2SJoe Perches					$newcomp = ">=";
4792c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4793c5595fa2SJoe Perches					$newcomp = "<";
4794c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4795c5595fa2SJoe Perches					$newcomp = "<=";
4796c5595fa2SJoe Perches				}
4797c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4798c5595fa2SJoe Perches			}
4799c5595fa2SJoe Perches		}
4800c5595fa2SJoe Perches
4801f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4802f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
480353a3c448SAndy Whitcroft			my $name = $1;
480453a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4805000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4806f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
480753a3c448SAndy Whitcroft			}
480853a3c448SAndy Whitcroft		}
4809c45dcabdSAndy Whitcroft
48100a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
48114a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
48123705ce5bSJoe Perches			if (ERROR("SPACING",
48133705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
48143705ce5bSJoe Perches			    $fix) {
4815194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48163705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
48173705ce5bSJoe Perches			}
48180a920b5bSAndy Whitcroft		}
48190a920b5bSAndy Whitcroft
4820f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4821f5fe35ddSAndy Whitcroft# statements after the conditional.
4822170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
48233e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
48243e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
48253e469cdcSAndy Whitcroft					if (!defined $stat);
4826170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4827170d3a22SAndy Whitcroft						$remain_next, $off_next);
4828170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4829170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4830170d3a22SAndy Whitcroft
4831170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4832170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4833170d3a22SAndy Whitcroft				# then count those as offsets.
4834170d3a22SAndy Whitcroft				my ($whitespace) =
4835170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4836170d3a22SAndy Whitcroft				my $offset =
4837170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4838170d3a22SAndy Whitcroft
4839170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4840170d3a22SAndy Whitcroft								$offset} = 1;
4841170d3a22SAndy Whitcroft			}
4842170d3a22SAndy Whitcroft		}
4843170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4844c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4845170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4846171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
48478905a67cSAndy Whitcroft
4848b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4849000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4850000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
48518905a67cSAndy Whitcroft			}
48528905a67cSAndy Whitcroft
48538905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
48548905a67cSAndy Whitcroft			# conditional.
4855773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
48568905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
485713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
485853210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
485953210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4860773647a0SAndy Whitcroft			{
4861bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4862bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4863bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
486442bdf74cSHidetoshi Seto				my $stat_real = '';
4865bb44ad39SAndy Whitcroft
486642bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
486742bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4868bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4869bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4870bb44ad39SAndy Whitcroft				}
4871bb44ad39SAndy Whitcroft
4872000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4873000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
48748905a67cSAndy Whitcroft			}
48758905a67cSAndy Whitcroft		}
48768905a67cSAndy Whitcroft
487713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
487813214adfSAndy Whitcroft		if ($line =~ /
487913214adfSAndy Whitcroft			(?:
488013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
488113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
488213214adfSAndy Whitcroft				(?:\&\&|\|\|)
488313214adfSAndy Whitcroft			|
488413214adfSAndy Whitcroft				(?:\&\&|\|\|)
488513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
488613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
488713214adfSAndy Whitcroft			)/x)
488813214adfSAndy Whitcroft		{
4889000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4890000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
489113214adfSAndy Whitcroft		}
489213214adfSAndy Whitcroft
48938905a67cSAndy Whitcroft# if and else should not have general statements after it
489413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
489513214adfSAndy Whitcroft			my $s = $1;
489613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
489713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4898000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4899000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
49000a920b5bSAndy Whitcroft			}
490113214adfSAndy Whitcroft		}
490239667782SAndy Whitcroft# if should not continue a brace
490339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4904000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4905048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
490639667782SAndy Whitcroft				$herecurr);
490739667782SAndy Whitcroft		}
4908a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4909a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4910a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
49113fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4912a1080bf8SAndy Whitcroft			\s*return\s+
4913a1080bf8SAndy Whitcroft		    )/xg)
4914a1080bf8SAndy Whitcroft		{
4915000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4916000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4917a1080bf8SAndy Whitcroft		}
49180a920b5bSAndy Whitcroft
49190a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
49200a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
49218b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
49220a920b5bSAndy Whitcroft		    $previndent == $indent) {
49238b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
49248b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
49258b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49268b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49278b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49288b8856f4SJoe Perches				my $fixedline = $prevrawline;
49298b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
49308b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
49318b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49328b8856f4SJoe Perches				}
49338b8856f4SJoe Perches				$fixedline = $rawline;
49348b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
49358b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
49368b8856f4SJoe Perches			}
49370a920b5bSAndy Whitcroft		}
49380a920b5bSAndy Whitcroft
49398b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4940c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4941c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4942c2fdda0dSAndy Whitcroft
4943c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4944c2fdda0dSAndy Whitcroft			# conditional.
4945773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4946c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4947c2fdda0dSAndy Whitcroft
4948c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
49498b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
49508b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
49518b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49528b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
49538b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
49548b8856f4SJoe Perches					my $fixedline = $prevrawline;
49558b8856f4SJoe Perches					my $trailing = $rawline;
49568b8856f4SJoe Perches					$trailing =~ s/^\+//;
49578b8856f4SJoe Perches					$trailing = trim($trailing);
49588b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
49598b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49608b8856f4SJoe Perches				}
4961c2fdda0dSAndy Whitcroft			}
4962c2fdda0dSAndy Whitcroft		}
4963c2fdda0dSAndy Whitcroft
496495e2c602SJoe Perches#Specific variable tests
4965323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4966323c1260SJoe Perches			my $var = $1;
496795e2c602SJoe Perches
496895e2c602SJoe Perches#CamelCase
4969807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4970be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
497122735ce8SJoe Perches#Ignore Page<foo> variants
4972807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
497322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4974f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4975f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4976f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
49777e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
49787e781f67SJoe Perches					my $word = $1;
49797e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4980d8b07710SJoe Perches					if ($check) {
4981d8b07710SJoe Perches						seed_camelcase_includes();
4982d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4983d8b07710SJoe Perches							seed_camelcase_file($realfile);
4984d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4985d8b07710SJoe Perches						}
4986d8b07710SJoe Perches					}
49877e781f67SJoe Perches					if (!defined $camelcase{$word}) {
49887e781f67SJoe Perches						$camelcase{$word} = 1;
4989be79794bSJoe Perches						CHK("CAMELCASE",
49907e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
49917e781f67SJoe Perches					}
4992323c1260SJoe Perches				}
4993323c1260SJoe Perches			}
49943445686aSJoe Perches		}
49950a920b5bSAndy Whitcroft
49960a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4997d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4998d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4999d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5000d5e616fcSJoe Perches			    $fix) {
5001194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5002d5e616fcSJoe Perches			}
50030a920b5bSAndy Whitcroft		}
50040a920b5bSAndy Whitcroft
50050e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
50060e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5007c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5008e09dec48SAndy Whitcroft			my $file = "$1.h";
5009e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5010e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5011e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
50127840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5013c45dcabdSAndy Whitcroft			{
50140e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
50150e212e0aSFabian Frederick				if ($asminclude > 0) {
5016e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5017000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5018000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5019e09dec48SAndy Whitcroft					} else {
5020000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5021000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5022e09dec48SAndy Whitcroft					}
50230a920b5bSAndy Whitcroft				}
50240a920b5bSAndy Whitcroft			}
50250e212e0aSFabian Frederick		}
50260a920b5bSAndy Whitcroft
5027653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5028653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5029cf655043SAndy Whitcroft# in a known good container
5030b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5031b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5032d8aaf121SAndy Whitcroft			my $ln = $linenr;
5033d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5034c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5035c45dcabdSAndy Whitcroft			my $ctx = '';
503608a2843eSJoe Perches			my $has_flow_statement = 0;
503708a2843eSJoe Perches			my $has_arg_concat = 0;
5038c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5039f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5040f74bd194SAndy Whitcroft			$ctx = $dstat;
5041c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5042a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5043c45dcabdSAndy Whitcroft
504408a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
504562e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
504608a2843eSJoe Perches
5047f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5048f59b64bfSJoe Perches			my $define_args = $1;
5049f59b64bfSJoe Perches			my $define_stmt = $dstat;
5050f59b64bfSJoe Perches			my @def_args = ();
5051f59b64bfSJoe Perches
5052f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5053f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5054f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
50558c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5056f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5057f59b64bfSJoe Perches			}
5058f59b64bfSJoe Perches
5059292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5060c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5061c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5062c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5063c45dcabdSAndy Whitcroft
5064c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5065bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5066bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
50676b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5068bf30d6edSAndy Whitcroft			{
5069c45dcabdSAndy Whitcroft			}
5070c45dcabdSAndy Whitcroft
5071e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
507233acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
507333acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5074e45bab8eSAndy Whitcroft			{
5075e45bab8eSAndy Whitcroft			}
5076e45bab8eSAndy Whitcroft
507742e15293SJoe Perches			# Make asm volatile uses seem like a generic function
507842e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
507942e15293SJoe Perches
5080c45dcabdSAndy Whitcroft			my $exceptions = qr{
5081c45dcabdSAndy Whitcroft				$Declare|
5082c45dcabdSAndy Whitcroft				module_param_named|
5083a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5084c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5085c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5086383099fdSAndy Whitcroft				__typeof__\(|
508722fd2d3eSStefani Seibold				union|
508822fd2d3eSStefani Seibold				struct|
5089ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
50906b10df42SVladimir Zapolskiy				^\"|\"$|
50916b10df42SVladimir Zapolskiy				^\[
5092c45dcabdSAndy Whitcroft			}x;
50935eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5094f59b64bfSJoe Perches
5095f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5096f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5097e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5098f59b64bfSJoe Perches
5099f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5100f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5101f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
51023cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5103356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5104f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5105f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5106e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
510772f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5108f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5109f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5110f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
51114e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5112f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5113c45dcabdSAndy Whitcroft			{
5114e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5115e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5116e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5117e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5118f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5119f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5120f74bd194SAndy Whitcroft				} else {
5121000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5122388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5123d8aaf121SAndy Whitcroft				}
5124f59b64bfSJoe Perches
5125f59b64bfSJoe Perches			}
51265207649bSJoe Perches
51275207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
51285207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
51295207649bSJoe Perches			my $first = 1;
51305207649bSJoe Perches			$define_stmt = "";
51315207649bSJoe Perches			foreach my $l (@stmt_array) {
51325207649bSJoe Perches				$l =~ s/\\$//;
51335207649bSJoe Perches				if ($first) {
51345207649bSJoe Perches					$define_stmt = $l;
51355207649bSJoe Perches					$first = 0;
51365207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
51375207649bSJoe Perches					$define_stmt .= substr($l, 1);
51385207649bSJoe Perches				}
51395207649bSJoe Perches			}
51405207649bSJoe Perches			$define_stmt =~ s/$;//g;
51415207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
51425207649bSJoe Perches			$define_stmt = trim($define_stmt);
51435207649bSJoe Perches
5144f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5145f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5146f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
51479192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
51487fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
51497fe528a2SJoe Perches				$tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
51507fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
51517fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5152d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5153f59b64bfSJoe Perches				if ($use_cnt > 1) {
5154f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5155f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5156f59b64bfSJoe Perches				    }
51579192d41aSJoe Perches# check if any macro arguments may have other precedence issues
51587fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
51599192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
51609192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
51619192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
51629192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
51639192d41aSJoe Perches				}
51640a920b5bSAndy Whitcroft			}
51655023d347SJoe Perches
516608a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
516708a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
516808a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
516908a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5170e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
517108a2843eSJoe Perches
517208a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
517308a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
517408a2843eSJoe Perches			}
517508a2843eSJoe Perches
5176481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
51775023d347SJoe Perches
51785023d347SJoe Perches		} else {
51795023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5180481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5181481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
51825023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
51835023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
51845023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
51855023d347SJoe Perches			}
5186653d4876SAndy Whitcroft		}
51870a920b5bSAndy Whitcroft
5188b13edf7fSJoe Perches# do {} while (0) macro tests:
5189b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5190b13edf7fSJoe Perches# macro should not end with a semicolon
51915b57980dSJoe Perches		if ($perl_version_ok &&
5192b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5193b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5194b13edf7fSJoe Perches			my $ln = $linenr;
5195b13edf7fSJoe Perches			my $cnt = $realcnt;
5196b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5197b13edf7fSJoe Perches			my $ctx = '';
5198b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5199b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5200b13edf7fSJoe Perches			$ctx = $dstat;
5201b13edf7fSJoe Perches
5202b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
52031b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5204b13edf7fSJoe Perches
5205b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5206b13edf7fSJoe Perches				my $stmts = $2;
5207b13edf7fSJoe Perches				my $semis = $3;
5208b13edf7fSJoe Perches
5209b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5210b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5211e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5212b13edf7fSJoe Perches
5213ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5214ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5215b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5216b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5217b13edf7fSJoe Perches				}
5218b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5219b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5220b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5221b13edf7fSJoe Perches				}
5222f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5223f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5224f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5225e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5226f5ef95b1SJoe Perches
5227f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5228f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5229b13edf7fSJoe Perches			}
5230b13edf7fSJoe Perches		}
5231b13edf7fSJoe Perches
5232f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
523313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
523413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5235cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
523613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5237cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5238cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5239aad4f614SJoe Perches				my @allowed = ();
5240aad4f614SJoe Perches				my $allow = 0;
524113214adfSAndy Whitcroft				my $seen = 0;
5242773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5243cf655043SAndy Whitcroft				my $ln = $linenr - 1;
524413214adfSAndy Whitcroft				for my $chunk (@chunks) {
524513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
524613214adfSAndy Whitcroft
5247773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5248773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5249773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5250773647a0SAndy Whitcroft
5251aad4f614SJoe Perches					$allowed[$allow] = 0;
5252773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5253773647a0SAndy Whitcroft
5254773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5255773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5256773647a0SAndy Whitcroft
5257773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5258cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5259cf655043SAndy Whitcroft
5260773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
526113214adfSAndy Whitcroft
526213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
526313214adfSAndy Whitcroft
5264aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5265cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5266cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5267aad4f614SJoe Perches						$allowed[$allow] = 1;
526813214adfSAndy Whitcroft					}
526913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5270cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5271aad4f614SJoe Perches						$allowed[$allow] = 1;
527213214adfSAndy Whitcroft					}
5273cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5274cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5275aad4f614SJoe Perches						$allowed[$allow] = 1;
527613214adfSAndy Whitcroft					}
5277aad4f614SJoe Perches					$allow++;
527813214adfSAndy Whitcroft				}
5279aad4f614SJoe Perches				if ($seen) {
5280aad4f614SJoe Perches					my $sum_allowed = 0;
5281aad4f614SJoe Perches					foreach (@allowed) {
5282aad4f614SJoe Perches						$sum_allowed += $_;
5283aad4f614SJoe Perches					}
5284aad4f614SJoe Perches					if ($sum_allowed == 0) {
5285000d1cc1SJoe Perches						WARN("BRACES",
5286000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5287aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5288aad4f614SJoe Perches						 $seen != $allow) {
5289aad4f614SJoe Perches						CHK("BRACES",
5290aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5291aad4f614SJoe Perches					}
529213214adfSAndy Whitcroft				}
529313214adfSAndy Whitcroft			}
529413214adfSAndy Whitcroft		}
5295773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
529613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5297cf655043SAndy Whitcroft			my $allowed = 0;
5298f0a594c1SAndy Whitcroft
5299cf655043SAndy Whitcroft			# Check the pre-context.
5300cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5301cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5302cf655043SAndy Whitcroft				$allowed = 1;
5303f0a594c1SAndy Whitcroft			}
5304773647a0SAndy Whitcroft
5305773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5306773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5307773647a0SAndy Whitcroft
5308cf655043SAndy Whitcroft			# Check the condition.
5309cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5310773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5311cf655043SAndy Whitcroft			if (defined $cond) {
5312773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5313cf655043SAndy Whitcroft			}
5314cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5315cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5316cf655043SAndy Whitcroft				$allowed = 1;
5317cf655043SAndy Whitcroft			}
5318cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5319cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5320cf655043SAndy Whitcroft				$allowed = 1;
5321cf655043SAndy Whitcroft			}
5322cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5323cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5324cf655043SAndy Whitcroft				$allowed = 1;
5325cf655043SAndy Whitcroft			}
5326cf655043SAndy Whitcroft			# Check the post-context.
5327cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5328cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5329cf655043SAndy Whitcroft				if (defined $cond) {
5330773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5331cf655043SAndy Whitcroft				}
5332cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5333cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5334cf655043SAndy Whitcroft					$allowed = 1;
5335cf655043SAndy Whitcroft				}
5336cf655043SAndy Whitcroft			}
5337cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5338f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5339e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5340cf655043SAndy Whitcroft
5341000d1cc1SJoe Perches				WARN("BRACES",
5342000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5343f0a594c1SAndy Whitcroft			}
5344f0a594c1SAndy Whitcroft		}
5345f0a594c1SAndy Whitcroft
5346e4c5babdSJoe Perches# check for single line unbalanced braces
534795330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
534895330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5349e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5350e4c5babdSJoe Perches		}
5351e4c5babdSJoe Perches
53520979ae66SJoe Perches# check for unnecessary blank lines around braces
535377b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5354f8e58219SJoe Perches			if (CHK("BRACES",
5355f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5356f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5357f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5358f8e58219SJoe Perches			}
53590979ae66SJoe Perches		}
536077b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5361f8e58219SJoe Perches			if (CHK("BRACES",
5362f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5363f8e58219SJoe Perches			    $fix) {
5364f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5365f8e58219SJoe Perches			}
53660979ae66SJoe Perches		}
53670979ae66SJoe Perches
53684a0df2efSAndy Whitcroft# no volatiles please
53696c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
53706c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5371000d1cc1SJoe Perches			WARN("VOLATILE",
53728c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
53734a0df2efSAndy Whitcroft		}
53744a0df2efSAndy Whitcroft
53755e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
53765e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
53775e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
53785e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
537933acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
53805e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
53815e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
53825e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
53835e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
53845e4f6ba5SJoe Perches				     $fix &&
53855e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
53865e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
53875e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
53885e4f6ba5SJoe Perches				my $comma_close = "";
53895e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
53905e4f6ba5SJoe Perches					$comma_close = $1;
53915e4f6ba5SJoe Perches				}
53925e4f6ba5SJoe Perches
53935e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
53945e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
53955e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
53965e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
53975e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
53985e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
53995e4f6ba5SJoe Perches				$fixedline = $rawline;
54005e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
54015e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
54025e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54035e4f6ba5SJoe Perches				}
54045e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
54055e4f6ba5SJoe Perches			}
54065e4f6ba5SJoe Perches		}
54075e4f6ba5SJoe Perches
54085e4f6ba5SJoe Perches# check for missing a space in a string concatenation
54095e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
54105e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
54115e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
54125e4f6ba5SJoe Perches		}
54135e4f6ba5SJoe Perches
541477cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5415e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5416e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5417e4b7d309SJoe Perches# function declarations
541877cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
541977cb8546SJoe Perches		    defined($context_function) &&
5420e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5421e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
542277cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5423e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
542477cb8546SJoe Perches		}
542577cb8546SJoe Perches
54265e4f6ba5SJoe Perches# check for spaces before a quoted newline
54275e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
54285e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
54295e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
54305e4f6ba5SJoe Perches			    $fix) {
54315e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
54325e4f6ba5SJoe Perches			}
54335e4f6ba5SJoe Perches
54345e4f6ba5SJoe Perches		}
54355e4f6ba5SJoe Perches
5436f17dba4fSJoe Perches# concatenated string without spaces between elements
543779682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
543879682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
543979682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
544079682c0cSJoe Perches			    $fix) {
544179682c0cSJoe Perches				while ($line =~ /($String)/g) {
544279682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
544379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
544479682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
544579682c0cSJoe Perches				}
544679682c0cSJoe Perches			}
5447f17dba4fSJoe Perches		}
5448f17dba4fSJoe Perches
544990ad30e5SJoe Perches# uncoalesced string fragments
545033acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
545179682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
545279682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
545379682c0cSJoe Perches			    $fix) {
545479682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
545579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
545679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
545779682c0cSJoe Perches				}
545879682c0cSJoe Perches			}
545990ad30e5SJoe Perches		}
546090ad30e5SJoe Perches
5461522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5462522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5463522b837cSAlexey Dobriyan		my $show_Z = 1;
54645e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5465522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
54665e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5467522b837cSAlexey Dobriyan			# check for %L
5468522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
54695e4f6ba5SJoe Perches				WARN("PRINTF_L",
5470522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5471522b837cSAlexey Dobriyan				$show_L = 0;
54725e4f6ba5SJoe Perches			}
5473522b837cSAlexey Dobriyan			# check for %Z
5474522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5475522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5476522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5477522b837cSAlexey Dobriyan				$show_Z = 0;
5478522b837cSAlexey Dobriyan			}
5479522b837cSAlexey Dobriyan			# check for 0x<decimal>
5480522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5481522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
54826e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
54836e300757SJoe Perches			}
54845e4f6ba5SJoe Perches		}
54855e4f6ba5SJoe Perches
54865e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
54873f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
54885e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
54895e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
54905e4f6ba5SJoe Perches		}
54915e4f6ba5SJoe Perches
549200df344fSAndy Whitcroft# warn about #if 0
5493c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
549460f89010SPrakruthi Deepak Heragu			WARN("IF_0",
549560f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
549660f89010SPrakruthi Deepak Heragu		}
549760f89010SPrakruthi Deepak Heragu
549860f89010SPrakruthi Deepak Heragu# warn about #if 1
549960f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
550060f89010SPrakruthi Deepak Heragu			WARN("IF_1",
550160f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
55024a0df2efSAndy Whitcroft		}
55034a0df2efSAndy Whitcroft
550403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
550503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5506100425deSJoe Perches			my $tested = quotemeta($1);
5507100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5508100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5509100425deSJoe Perches				my $func = $1;
5510100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5511100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5512100425deSJoe Perches				    $fix) {
5513100425deSJoe Perches					my $do_fix = 1;
5514100425deSJoe Perches					my $leading_tabs = "";
5515100425deSJoe Perches					my $new_leading_tabs = "";
5516100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5517100425deSJoe Perches						$leading_tabs = $1;
5518100425deSJoe Perches					} else {
5519100425deSJoe Perches						$do_fix = 0;
5520100425deSJoe Perches					}
5521100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5522100425deSJoe Perches						$new_leading_tabs = $1;
5523100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5524100425deSJoe Perches							$do_fix = 0;
5525100425deSJoe Perches						}
5526100425deSJoe Perches					} else {
5527100425deSJoe Perches						$do_fix = 0;
5528100425deSJoe Perches					}
5529100425deSJoe Perches					if ($do_fix) {
5530100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5531100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5532100425deSJoe Perches					}
5533100425deSJoe Perches				}
55344c432a8fSGreg Kroah-Hartman			}
55354c432a8fSGreg Kroah-Hartman		}
5536f0a594c1SAndy Whitcroft
5537ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5538ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5539ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5540ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5541ebfdc409SJoe Perches		    $linenr > 3) {
5542ebfdc409SJoe Perches			my $testval = $2;
5543ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5544ebfdc409SJoe Perches
5545ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5546ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5547ebfdc409SJoe Perches
5548fb0d0e08SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
5549ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5550ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5551ebfdc409SJoe Perches			}
5552ebfdc409SJoe Perches		}
5553ebfdc409SJoe Perches
5554f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5555dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5556f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5557f78d98f6SJoe Perches			my $level = $1;
5558f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5559f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5560f78d98f6SJoe Perches			    $fix) {
5561f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5562f78d98f6SJoe Perches			}
5563f78d98f6SJoe Perches		}
5564f78d98f6SJoe Perches
556545c55e92SJoe Perches# check for logging continuations
556645c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
556745c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
556845c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
556945c55e92SJoe Perches		}
557045c55e92SJoe Perches
5571abb08a53SJoe Perches# check for mask then right shift without a parentheses
55725b57980dSJoe Perches		if ($perl_version_ok &&
5573abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5574abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5575abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5576abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5577abb08a53SJoe Perches		}
5578abb08a53SJoe Perches
5579b75ac618SJoe Perches# check for pointer comparisons to NULL
55805b57980dSJoe Perches		if ($perl_version_ok) {
5581b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5582b75ac618SJoe Perches				my $val = $1;
5583b75ac618SJoe Perches				my $equal = "!";
5584b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5585b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5586b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5587b75ac618SJoe Perches					    $fix) {
5588b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5589b75ac618SJoe Perches				}
5590b75ac618SJoe Perches			}
5591b75ac618SJoe Perches		}
5592b75ac618SJoe Perches
55938716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
55948716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
55958716de38SJoe Perches			my $attr = $1;
55968716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
55978716de38SJoe Perches				my $ptr = $1;
55988716de38SJoe Perches				my $var = $2;
55998716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
56008716de38SJoe Perches				      ERROR("MISPLACED_INIT",
56018716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
56028716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
56038716de38SJoe Perches				      WARN("MISPLACED_INIT",
56048716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
56058716de38SJoe Perches				    $fix) {
5606194f66fcSJoe 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;
56078716de38SJoe Perches				}
56088716de38SJoe Perches			}
56098716de38SJoe Perches		}
56108716de38SJoe Perches
5611e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5612e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5613e970b884SJoe Perches			my $attr = $1;
5614e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5615e970b884SJoe Perches			my $attr_prefix = $1;
5616e970b884SJoe Perches			my $attr_type = $2;
5617e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5618e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5619e970b884SJoe Perches			    $fix) {
5620194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5621e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5622e970b884SJoe Perches			}
5623e970b884SJoe Perches		}
5624e970b884SJoe Perches
5625e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5626e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5627e970b884SJoe Perches			my $attr = $1;
5628e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5629e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5630e970b884SJoe Perches			    $fix) {
5631194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5632e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5633e970b884SJoe Perches				$lead = rtrim($1);
5634e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5635e970b884SJoe Perches				$lead = "${lead}const ";
5636194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5637e970b884SJoe Perches			}
5638e970b884SJoe Perches		}
5639e970b884SJoe Perches
5640c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5641c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5642c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5643c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5644c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5645c17893c7SJoe Perches			    $fix) {
5646c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5647c17893c7SJoe Perches			}
5648c17893c7SJoe Perches		}
5649c17893c7SJoe Perches
5650fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5651fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5652fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5653fbdb8138SJoe Perches			my $constant_func = $1;
5654fbdb8138SJoe Perches			my $func = $constant_func;
5655fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5656fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5657fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5658fbdb8138SJoe Perches			    $fix) {
5659194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5660fbdb8138SJoe Perches			}
5661fbdb8138SJoe Perches		}
5662fbdb8138SJoe Perches
56631a15a250SPatrick Pannuto# prefer usleep_range over udelay
566437581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
566543c1d77cSJoe Perches			my $delay = $1;
56661a15a250SPatrick Pannuto			# ignore udelay's < 10, however
566743c1d77cSJoe Perches			if (! ($delay < 10) ) {
5668000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
566943c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
567043c1d77cSJoe Perches			}
567143c1d77cSJoe Perches			if ($delay > 2000) {
567243c1d77cSJoe Perches				WARN("LONG_UDELAY",
567343c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
56741a15a250SPatrick Pannuto			}
56751a15a250SPatrick Pannuto		}
56761a15a250SPatrick Pannuto
567709ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
567809ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
567909ef8725SPatrick Pannuto			if ($1 < 20) {
5680000d1cc1SJoe Perches				WARN("MSLEEP",
568143c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
568209ef8725SPatrick Pannuto			}
568309ef8725SPatrick Pannuto		}
568409ef8725SPatrick Pannuto
568536ec1939SJoe Perches# check for comparisons of jiffies
568636ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
568736ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
568836ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
568936ec1939SJoe Perches		}
569036ec1939SJoe Perches
56919d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
56929d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
56939d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
56949d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
56959d7a34a5SJoe Perches		}
56969d7a34a5SJoe Perches
569700df344fSAndy Whitcroft# warn about #ifdefs in C files
5698c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
569900df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
570000df344fSAndy Whitcroft#			print "$herecurr";
570100df344fSAndy Whitcroft#			$clean = 0;
570200df344fSAndy Whitcroft#		}
570300df344fSAndy Whitcroft
570422f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5705c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
57063705ce5bSJoe Perches			if (ERROR("SPACING",
57073705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
57083705ce5bSJoe Perches			    $fix) {
5709194f66fcSJoe Perches				$fixed[$fixlinenr] =~
57103705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
57113705ce5bSJoe Perches			}
57123705ce5bSJoe Perches
571322f2a2efSAndy Whitcroft		}
571422f2a2efSAndy Whitcroft
57154a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5716171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5717171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
57184a0df2efSAndy Whitcroft			my $which = $1;
57194a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5720000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5721000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
57224a0df2efSAndy Whitcroft			}
57234a0df2efSAndy Whitcroft		}
57244a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5725402c2553SMichael S. Tsirkin
5726402c2553SMichael S. Tsirkin		my $barriers = qr{
5727402c2553SMichael S. Tsirkin			mb|
5728402c2553SMichael S. Tsirkin			rmb|
5729402c2553SMichael S. Tsirkin			wmb|
5730402c2553SMichael S. Tsirkin			read_barrier_depends
5731402c2553SMichael S. Tsirkin		}x;
5732402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5733402c2553SMichael S. Tsirkin			mb__before_atomic|
5734402c2553SMichael S. Tsirkin			mb__after_atomic|
5735402c2553SMichael S. Tsirkin			store_release|
5736402c2553SMichael S. Tsirkin			load_acquire|
5737402c2553SMichael S. Tsirkin			store_mb|
5738402c2553SMichael S. Tsirkin			(?:$barriers)
5739402c2553SMichael S. Tsirkin		}x;
5740402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5741402c2553SMichael S. Tsirkin			(?:$barriers)|
574243e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
574343e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5744402c2553SMichael S. Tsirkin		}x;
5745402c2553SMichael S. Tsirkin
5746402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
57474a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5748c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5749000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
57504a0df2efSAndy Whitcroft			}
57514a0df2efSAndy Whitcroft		}
57523ad81779SPaul E. McKenney
5753f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5754f4073b0fSMichael S. Tsirkin
5755f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5756f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5757f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5758f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5759f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5760f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5761f4073b0fSMichael S. Tsirkin		}
5762f4073b0fSMichael S. Tsirkin
5763cb426e99SJoe Perches# check for waitqueue_active without a comment.
5764cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5765cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5766cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5767cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5768cb426e99SJoe Perches			}
5769cb426e99SJoe Perches		}
57703ad81779SPaul E. McKenney
577191db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
577291db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
577391db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
577491db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
577591db2592SPaul E. McKenney		}
577691db2592SPaul E. McKenney
57774a0df2efSAndy Whitcroft# check of hardware specific defines
5778c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5779000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5780000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
57810a920b5bSAndy Whitcroft		}
5782653d4876SAndy Whitcroft
5783596ed45bSJoe Perches# check that the storage class is not after a type
5784596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5785000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5786596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5787596ed45bSJoe Perches		}
5788596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5789596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5790596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5791596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5792596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5793596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5794596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5795d4977c78STobias Klauser		}
5796d4977c78STobias Klauser
5797de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5798de7d4f0eSAndy Whitcroft# storage class and type.
57999c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
58009c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5801000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5802000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5803de7d4f0eSAndy Whitcroft		}
5804de7d4f0eSAndy Whitcroft
58058905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
58062b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58072b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5808d5e616fcSJoe Perches			if (WARN("INLINE",
5809d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5810d5e616fcSJoe Perches			    $fix) {
5811194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5812d5e616fcSJoe Perches
5813d5e616fcSJoe Perches			}
58148905a67cSAndy Whitcroft		}
58158905a67cSAndy Whitcroft
58163d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
58172b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58182b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5819000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5820000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
58213d130fd0SJoe Perches		}
58223d130fd0SJoe Perches
582339b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
58242b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58252b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5826000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5827000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
582839b7e287SJoe Perches		}
582939b7e287SJoe Perches
58305f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
58312b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58322b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5833d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5834d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5835d5e616fcSJoe Perches			    $fix) {
5836194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5837d5e616fcSJoe Perches
5838d5e616fcSJoe Perches			}
58395f14d3bdSJoe Perches		}
58405f14d3bdSJoe Perches
58416061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
58422b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58432b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5844d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5845d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5846d5e616fcSJoe Perches			    $fix) {
5847194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5848d5e616fcSJoe Perches			}
58496061d949SJoe Perches		}
58506061d949SJoe Perches
5851619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
58525b57980dSJoe Perches		if ($perl_version_ok &&
5853619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5854619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5855619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5856619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5857619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5858619a908aSJoe Perches		}
5859619a908aSJoe Perches
5860fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5861e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5862fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5863e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5864e6176fa4SJoe Perches			my $type = $1;
5865e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5866e6176fa4SJoe Perches				$type = $1;
5867e6176fa4SJoe Perches				my $kernel_type = 'u';
5868e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5869e6176fa4SJoe Perches				$type =~ /(\d+)/;
5870e6176fa4SJoe Perches				$kernel_type .= $1;
5871e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5872e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5873e6176fa4SJoe Perches				    $fix) {
5874e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5875e6176fa4SJoe Perches				}
5876e6176fa4SJoe Perches			}
5877e6176fa4SJoe Perches		}
5878e6176fa4SJoe Perches
5879938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5880938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5881938224b5SJoe Perches			my $cast = $1;
5882938224b5SJoe Perches			my $const = $2;
5883938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5884938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5885938224b5SJoe Perches			    $fix) {
5886938224b5SJoe Perches				my $suffix = "";
5887938224b5SJoe Perches				my $newconst = $const;
5888938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5889938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5890938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5891938224b5SJoe Perches					$suffix .= 'LL';
5892938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5893938224b5SJoe Perches					$suffix .= 'L';
5894938224b5SJoe Perches				}
5895938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5896938224b5SJoe Perches			}
5897938224b5SJoe Perches		}
5898938224b5SJoe Perches
58998f53a9b8SJoe Perches# check for sizeof(&)
59008f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5901000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5902000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
59038f53a9b8SJoe Perches		}
59048f53a9b8SJoe Perches
590566c80b60SJoe Perches# check for sizeof without parenthesis
590666c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5907d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5908d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5909d5e616fcSJoe Perches			    $fix) {
5910194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5911d5e616fcSJoe Perches			}
591266c80b60SJoe Perches		}
591366c80b60SJoe Perches
591488982feaSJoe Perches# check for struct spinlock declarations
591588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
591688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
591788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
591888982feaSJoe Perches		}
591988982feaSJoe Perches
5920a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
592106668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5922a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5923caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5924caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5925d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5926d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5927d5e616fcSJoe Perches				    $fix) {
5928194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5929d5e616fcSJoe Perches				}
5930a6962d72SJoe Perches			}
5931a6962d72SJoe Perches		}
5932a6962d72SJoe Perches
59330b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
59345b57980dSJoe Perches		if ($perl_version_ok &&
59350b523769SJoe Perches		    defined $stat &&
59360b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
59370b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
5938e3c6bc95STobin C. Harding			my $stat_real;
5939e3c6bc95STobin C. Harding
59400b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
59410b523769SJoe Perches			$lc = $lc + $linenr;
59420b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
5943ffe07513SJoe Perches				my $specifier;
5944ffe07513SJoe Perches				my $extension;
5945ffe07513SJoe Perches				my $bad_specifier = "";
59460b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
59470b523769SJoe Perches				$fmt =~ s/%%//g;
5948e3c6bc95STobin C. Harding
5949e3c6bc95STobin C. Harding				while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5950e3c6bc95STobin C. Harding					$specifier = $1;
5951e3c6bc95STobin C. Harding					$extension = $2;
5952e3c6bc95STobin C. Harding					if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5953e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
59540b523769SJoe Perches						last;
59550b523769SJoe Perches					}
5956e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
5957e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
5958e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
59590b523769SJoe Perches						}
5960e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
5961e3c6bc95STobin 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");
5962e3c6bc95STobin C. Harding					}
5963e3c6bc95STobin C. Harding				}
5964e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
59652a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
59661df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
59671df7338aSSergey Senozhatsky					my $use = "";
5968e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
59691df7338aSSergey Senozhatsky						$ext_type = "Deprecated";
59701df7338aSSergey Senozhatsky						$use = " - use %pS instead";
5971e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
59721df7338aSSergey Senozhatsky					}
59732a9f9d85STobin C. Harding
59740b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
5975e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5976e3c6bc95STobin C. Harding				}
59770b523769SJoe Perches			}
59780b523769SJoe Perches		}
59790b523769SJoe Perches
5980554e165cSAndy Whitcroft# Check for misused memsets
59815b57980dSJoe Perches		if ($perl_version_ok &&
5982d1fe9c09SJoe Perches		    defined $stat &&
59839e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5984554e165cSAndy Whitcroft
5985d7c76ba7SJoe Perches			my $ms_addr = $2;
5986d1fe9c09SJoe Perches			my $ms_val = $7;
5987d1fe9c09SJoe Perches			my $ms_size = $12;
5988d7c76ba7SJoe Perches
5989554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5990554e165cSAndy Whitcroft				ERROR("MEMSET",
5991d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5992554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5993554e165cSAndy Whitcroft				WARN("MEMSET",
5994d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5995d7c76ba7SJoe Perches			}
5996d7c76ba7SJoe Perches		}
5997d7c76ba7SJoe Perches
599898a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
59995b57980dSJoe Perches#		if ($perl_version_ok &&
6000f333195dSJoe Perches#		    defined $stat &&
6001f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6002f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6003f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6004f333195dSJoe Perches#			    $fix) {
6005f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6006f333195dSJoe Perches#			}
6007f333195dSJoe Perches#		}
600898a9bba5SJoe Perches
6009b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
60105b57980dSJoe Perches#		if ($perl_version_ok &&
6011f333195dSJoe Perches#		    defined $stat &&
6012f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6013f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6014f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6015f333195dSJoe Perches#		}
6016b6117d17SMateusz Kulikowski
60178617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
60188617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
60195b57980dSJoe Perches#		if ($perl_version_ok &&
6020f333195dSJoe Perches#		    defined $stat &&
6021f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6022f333195dSJoe Perches#
6023f333195dSJoe Perches#			my $ms_val = $7;
6024f333195dSJoe Perches#
6025f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6026f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6027f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6028f333195dSJoe Perches#				    $fix) {
6029f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6030f333195dSJoe Perches#				}
6031f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6032f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6033f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6034f333195dSJoe Perches#				    $fix) {
6035f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6036f333195dSJoe Perches#				}
6037f333195dSJoe Perches#			}
6038f333195dSJoe Perches#		}
60398617cd09SMateusz Kulikowski
6040d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
60415b57980dSJoe Perches		if ($perl_version_ok &&
6042d1fe9c09SJoe Perches		    defined $stat &&
6043d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6044d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6045d7c76ba7SJoe Perches				my $call = $1;
6046d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6047d7c76ba7SJoe Perches				my $arg1 = $3;
6048d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6049d1fe9c09SJoe Perches				my $arg2 = $8;
6050d7c76ba7SJoe Perches				my $cast;
6051d7c76ba7SJoe Perches
6052d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6053d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6054d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6055d7c76ba7SJoe Perches					$cast = $cast1;
6056d7c76ba7SJoe Perches				} else {
6057d7c76ba7SJoe Perches					$cast = $cast2;
6058d7c76ba7SJoe Perches				}
6059d7c76ba7SJoe Perches				WARN("MINMAX",
6060d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6061554e165cSAndy Whitcroft			}
6062554e165cSAndy Whitcroft		}
6063554e165cSAndy Whitcroft
60644a273195SJoe Perches# check usleep_range arguments
60655b57980dSJoe Perches		if ($perl_version_ok &&
60664a273195SJoe Perches		    defined $stat &&
60674a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
60684a273195SJoe Perches			my $min = $1;
60694a273195SJoe Perches			my $max = $7;
60704a273195SJoe Perches			if ($min eq $max) {
60714a273195SJoe Perches				WARN("USLEEP_RANGE",
60724a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
60734a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
60744a273195SJoe Perches				 $min > $max) {
60754a273195SJoe Perches				WARN("USLEEP_RANGE",
60764a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
60774a273195SJoe Perches			}
60784a273195SJoe Perches		}
60794a273195SJoe Perches
6080823b794cSJoe Perches# check for naked sscanf
60815b57980dSJoe Perches		if ($perl_version_ok &&
6082823b794cSJoe Perches		    defined $stat &&
60836c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6084823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6085823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6086823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6087823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6088823b794cSJoe Perches			$lc = $lc + $linenr;
60892a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6090823b794cSJoe Perches			WARN("NAKED_SSCANF",
6091823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6092823b794cSJoe Perches		}
6093823b794cSJoe Perches
6094afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
60955b57980dSJoe Perches		if ($perl_version_ok &&
6096afc819abSJoe Perches		    defined $stat &&
6097afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6098afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6099afc819abSJoe Perches			$lc = $lc + $linenr;
61002a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6101afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6102afc819abSJoe Perches				my $format = $6;
6103afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6104afc819abSJoe Perches				if ($count == 1 &&
6105afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6106afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6107afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6108afc819abSJoe Perches				}
6109afc819abSJoe Perches			}
6110afc819abSJoe Perches		}
6111afc819abSJoe Perches
611270dc8a48SJoe Perches# check for new externs in .h files.
611370dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
611470dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6115d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
611670dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
611770dc8a48SJoe Perches			    $fix) {
6118194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
611970dc8a48SJoe Perches			}
612070dc8a48SJoe Perches		}
612170dc8a48SJoe Perches
6122de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6123171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6124c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6125171ae1a4SAndy Whitcroft		{
6126c45dcabdSAndy Whitcroft			my $function_name = $1;
6127c45dcabdSAndy Whitcroft			my $paren_space = $2;
6128171ae1a4SAndy Whitcroft
6129171ae1a4SAndy Whitcroft			my $s = $stat;
6130171ae1a4SAndy Whitcroft			if (defined $cond) {
6131171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6132171ae1a4SAndy Whitcroft			}
6133c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6134c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6135c45dcabdSAndy Whitcroft			{
6136000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6137000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6138de7d4f0eSAndy Whitcroft			}
6139de7d4f0eSAndy Whitcroft
6140171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6141000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6142000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6143171ae1a4SAndy Whitcroft			}
61449c9ba34eSAndy Whitcroft
61459c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
61469c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
61479c9ba34eSAndy Whitcroft		{
6148000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6149000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6150171ae1a4SAndy Whitcroft		}
6151171ae1a4SAndy Whitcroft
6152a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6153a0ad7596SJoe Perches		if (defined $stat &&
615425bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6155ca0d8929SJoe Perches		    $1 ne "void") {
6156ca0d8929SJoe Perches			my $args = trim($1);
6157ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6158ca0d8929SJoe Perches				my $arg = trim($1);
6159ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6160ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6161ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6162ca0d8929SJoe Perches				}
6163ca0d8929SJoe Perches			}
6164ca0d8929SJoe Perches		}
6165ca0d8929SJoe Perches
6166a0ad7596SJoe Perches# check for function definitions
61675b57980dSJoe Perches		if ($perl_version_ok &&
6168a0ad7596SJoe Perches		    defined $stat &&
6169a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6170a0ad7596SJoe Perches			$context_function = $1;
6171a0ad7596SJoe Perches
6172a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6173a0ad7596SJoe Perches			my $ok = 0;
6174a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6175a0ad7596SJoe Perches			my $herectx = $here . "\n";
6176a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6177a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6178a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6179a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6180a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6181a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6182a0ad7596SJoe Perches			}
6183a0ad7596SJoe Perches			if (!$ok) {
6184a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6185a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6186a0ad7596SJoe Perches			}
6187a0ad7596SJoe Perches		}
6188a0ad7596SJoe Perches
6189de7d4f0eSAndy Whitcroft# checks for new __setup's
6190de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6191de7d4f0eSAndy Whitcroft			my $name = $1;
6192de7d4f0eSAndy Whitcroft
6193de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6194000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
61958c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6196de7d4f0eSAndy Whitcroft			}
6197653d4876SAndy Whitcroft		}
61989c0ca6f9SAndy Whitcroft
61999c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
6200caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
6201000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6202000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
62039c0ca6f9SAndy Whitcroft		}
620413214adfSAndy Whitcroft
6205a640d25cSJoe Perches# alloc style
6206a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
62075b57980dSJoe Perches		if ($perl_version_ok &&
6208a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6209a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6210a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6211a640d25cSJoe Perches		}
6212a640d25cSJoe Perches
621360a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
62145b57980dSJoe Perches		if ($perl_version_ok &&
62151b4a2ed4SJoe Perches		    defined $stat &&
62161b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
621760a55369SJoe Perches			my $oldfunc = $3;
621860a55369SJoe Perches			my $a1 = $4;
621960a55369SJoe Perches			my $a2 = $10;
622060a55369SJoe Perches			my $newfunc = "kmalloc_array";
622160a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
622260a55369SJoe Perches			my $r1 = $a1;
622360a55369SJoe Perches			my $r2 = $a2;
622460a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
622560a55369SJoe Perches				$r1 = $a2;
622660a55369SJoe Perches				$r2 = $a1;
622760a55369SJoe Perches			}
6228e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6229e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
62301b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6231e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6232e3d95a2aSTobin C. Harding
6233e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
62341b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
62351b4a2ed4SJoe Perches				    $cnt == 1 &&
6236e367455aSJoe Perches				    $fix) {
6237194f66fcSJoe 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;
623860a55369SJoe Perches				}
623960a55369SJoe Perches			}
624060a55369SJoe Perches		}
624160a55369SJoe Perches
6242972fdea2SJoe Perches# check for krealloc arg reuse
62435b57980dSJoe Perches		if ($perl_version_ok &&
62444cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
62454cab63ceSJoe Perches		    $1 eq $3) {
6246972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6247972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6248972fdea2SJoe Perches		}
6249972fdea2SJoe Perches
62505ce59ae0SJoe Perches# check for alloc argument mismatch
62515ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
62525ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
62535ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
62545ce59ae0SJoe Perches		}
62555ce59ae0SJoe Perches
6256caf2a54fSJoe Perches# check for multiple semicolons
6257caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6258d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6259d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6260d5e616fcSJoe Perches			    $fix) {
6261194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6262d5e616fcSJoe Perches			}
6263d1e2ad07SJoe Perches		}
6264d1e2ad07SJoe Perches
6265cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6266cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6267cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
62680ab90191SJoe Perches			my $ull = "";
62690ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
62700ab90191SJoe Perches			if (CHK("BIT_MACRO",
62710ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
62720ab90191SJoe Perches			    $fix) {
62730ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
62740ab90191SJoe Perches			}
62750ab90191SJoe Perches		}
62760ab90191SJoe Perches
62772d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
62782d632745SJoe 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*$/) {
62792d632745SJoe Perches			my $config = $1;
62802d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
62812d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
62822d632745SJoe Perches			    $fix) {
62832d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
62842d632745SJoe Perches			}
62852d632745SJoe Perches		}
62862d632745SJoe Perches
6287e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6288c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6289c34c09a8SJoe Perches			my $has_break = 0;
6290c34c09a8SJoe Perches			my $has_statement = 0;
6291c34c09a8SJoe Perches			my $count = 0;
6292c34c09a8SJoe Perches			my $prevline = $linenr;
6293e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6294c34c09a8SJoe Perches				$prevline--;
6295c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6296c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6297c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6298c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6299c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6300c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6301c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6302c34c09a8SJoe Perches				$has_statement = 1;
6303c34c09a8SJoe Perches				$count++;
6304258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6305c34c09a8SJoe Perches			}
6306c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6307c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6308224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6309c34c09a8SJoe Perches			}
6310c34c09a8SJoe Perches		}
6311c34c09a8SJoe Perches
6312d1e2ad07SJoe Perches# check for switch/default statements without a break;
63135b57980dSJoe Perches		if ($perl_version_ok &&
6314d1e2ad07SJoe Perches		    defined $stat &&
6315d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6316d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6317e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6318e3d95a2aSTobin C. Harding
6319d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6320d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6321caf2a54fSJoe Perches		}
6322caf2a54fSJoe Perches
632313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6324d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6325d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6326d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6327d5e616fcSJoe Perches			    $fix) {
6328194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6329d5e616fcSJoe Perches			}
633013214adfSAndy Whitcroft		}
6331773647a0SAndy Whitcroft
633262ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
633362ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
633462ec818fSJoe Perches			ERROR("DATE_TIME",
633562ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
633662ec818fSJoe Perches		}
633762ec818fSJoe Perches
63382c92488aSJoe Perches# check for use of yield()
63392c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
63402c92488aSJoe Perches			WARN("YIELD",
63412c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
63422c92488aSJoe Perches		}
63432c92488aSJoe Perches
6344179f8f40SJoe Perches# check for comparisons against true and false
6345179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6346179f8f40SJoe Perches			my $lead = $1;
6347179f8f40SJoe Perches			my $arg = $2;
6348179f8f40SJoe Perches			my $test = $3;
6349179f8f40SJoe Perches			my $otype = $4;
6350179f8f40SJoe Perches			my $trail = $5;
6351179f8f40SJoe Perches			my $op = "!";
6352179f8f40SJoe Perches
6353179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6354179f8f40SJoe Perches
6355179f8f40SJoe Perches			my $type = lc($otype);
6356179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6357179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6358179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6359179f8f40SJoe Perches					$op = "";
6360179f8f40SJoe Perches				}
6361179f8f40SJoe Perches
6362179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6363179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6364179f8f40SJoe Perches
6365179f8f40SJoe Perches## maybe suggesting a correct construct would better
6366179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6367179f8f40SJoe Perches
6368179f8f40SJoe Perches			}
6369179f8f40SJoe Perches		}
6370179f8f40SJoe Perches
63715d430902SJoe Perches# check for bool bitfields
63725d430902SJoe Perches		if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
63735d430902SJoe Perches			WARN("BOOL_BITFIELD",
63745d430902SJoe Perches			     "Avoid using bool as bitfield.  Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
63755d430902SJoe Perches		}
63765d430902SJoe Perches
6377d729593eSJoe Perches# check for bool use in .h files
6378d729593eSJoe Perches		if ($realfile =~ /\.h$/ &&
6379d729593eSJoe Perches		    $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6380d729593eSJoe Perches			CHK("BOOL_MEMBER",
6381d729593eSJoe Perches			    "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6382d729593eSJoe Perches		}
6383d729593eSJoe Perches
63844882720bSThomas Gleixner# check for semaphores initialized locked
63854882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6386000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6387000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6388773647a0SAndy Whitcroft		}
63896712d858SJoe Perches
639067d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
639167d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6392000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
639367d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6394773647a0SAndy Whitcroft		}
63956712d858SJoe Perches
6396ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6397f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6398000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6399ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6400f3db6639SMichael Ellerman		}
64016712d858SJoe Perches
64023d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
64033d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
64043d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
64053d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
64063d709ab5SPaul E. McKenney		}
64073d709ab5SPaul E. McKenney
64089189c7e7SJoe Perches# check for deprecated apis
64099189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
64109189c7e7SJoe Perches			my $deprecated_api = $1;
64119189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
64129189c7e7SJoe Perches			WARN("DEPRECATED_API",
64139189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
64149189c7e7SJoe Perches		}
64159189c7e7SJoe Perches
64160f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6417d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
64186903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6419d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6420000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6421d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
64222b6db5cbSAndy Whitcroft		}
6423773647a0SAndy Whitcroft
6424773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6425773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6426773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6427c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6428c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6429171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6430171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6431171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6432773647a0SAndy Whitcroft		{
6433000d1cc1SJoe Perches			WARN("NR_CPUS",
6434000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6435773647a0SAndy Whitcroft		}
64369c9ba34eSAndy Whitcroft
643752ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
643852ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
643952ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
644052ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
644152ea8506SJoe Perches		}
644252ea8506SJoe Perches
6443acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
64445b57980dSJoe Perches		if ($perl_version_ok &&
6445acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6446acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6447acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6448acd9362cSJoe Perches		}
6449acd9362cSJoe Perches
6450691d77b6SAndy Whitcroft# whine mightly about in_atomic
6451691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6452691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6453000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6454000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6455f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6456000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6457000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6458691d77b6SAndy Whitcroft			}
6459691d77b6SAndy Whitcroft		}
64601704f47bSPeter Zijlstra
64610f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
64620f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
64630f5225b0SPeter Zijlstra			ERROR("LOCKING",
64640f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
64650f5225b0SPeter Zijlstra		}
64660f5225b0SPeter Zijlstra
64671704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
64681704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
64691704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
64701704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
64711704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
64721704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6473000d1cc1SJoe Perches				ERROR("LOCKDEP",
6474000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
64751704f47bSPeter Zijlstra			}
64761704f47bSPeter Zijlstra		}
647788f8831cSDave Jones
6478b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6479b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6480000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6481000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
648288f8831cSDave Jones		}
64832435880fSJoe Perches
648400180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
648500180468SJoe Perches# and whether or not function naming is typical and if
648600180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
64875b57980dSJoe Perches		if ($perl_version_ok &&
648800180468SJoe Perches		    defined $stat &&
648900180468SJoe 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*\)/) {
649000180468SJoe Perches			my $var = $1;
649100180468SJoe Perches			my $perms = $2;
649200180468SJoe Perches			my $show = $3;
649300180468SJoe Perches			my $store = $4;
649400180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
649500180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
649600180468SJoe Perches			    $store =~ /^${var}_store$/ &&
649700180468SJoe Perches			    $octal_perms eq "0644") {
649800180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
649900180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
650000180468SJoe Perches				    $fix) {
650100180468SJoe 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})/;
650200180468SJoe Perches				}
650300180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
650400180468SJoe Perches				 $store =~ /^NULL$/ &&
650500180468SJoe Perches				 $octal_perms eq "0444") {
650600180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
650700180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
650800180468SJoe Perches				    $fix) {
650900180468SJoe 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})/;
651000180468SJoe Perches				}
651100180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
651200180468SJoe Perches				 $store =~ /^${var}_store$/ &&
651300180468SJoe Perches				 $octal_perms eq "0200") {
651400180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
651500180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
651600180468SJoe Perches				    $fix) {
651700180468SJoe 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})/;
651800180468SJoe Perches				}
651900180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
652000180468SJoe Perches				 $octal_perms eq "0444" ||
652100180468SJoe Perches				 $octal_perms eq "0200") {
652200180468SJoe Perches				my $newshow = "$show";
652300180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
652400180468SJoe Perches				my $newstore = $store;
652500180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
652600180468SJoe Perches				my $rename = "";
652700180468SJoe Perches				if ($show ne $newshow) {
652800180468SJoe Perches					$rename .= " '$show' to '$newshow'";
652900180468SJoe Perches				}
653000180468SJoe Perches				if ($store ne $newstore) {
653100180468SJoe Perches					$rename .= " '$store' to '$newstore'";
653200180468SJoe Perches				}
653300180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
653400180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
653500180468SJoe Perches			} else {
653600180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
653700180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
653800180468SJoe Perches			}
653900180468SJoe Perches		}
654000180468SJoe Perches
6541515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6542515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
654373121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
654473121534SJoe Perches#   specific definition of not visible in sysfs.
654573121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
654673121534SJoe Perches#   use the default permissions
65475b57980dSJoe Perches		if ($perl_version_ok &&
6548459cf0aeSJoe Perches		    defined $stat &&
6549515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
65502435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
65512435880fSJoe Perches				my $func = $entry->[0];
65522435880fSJoe Perches				my $arg_pos = $entry->[1];
65532435880fSJoe Perches
6554459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6555459cf0aeSJoe Perches				$lc = $lc + $linenr;
65562a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6557459cf0aeSJoe Perches
65582435880fSJoe Perches				my $skip_args = "";
65592435880fSJoe Perches				if ($arg_pos > 1) {
65602435880fSJoe Perches					$arg_pos--;
65612435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
65622435880fSJoe Perches				}
6563f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6564459cf0aeSJoe Perches				if ($stat =~ /$test/) {
65652435880fSJoe Perches					my $val = $1;
65662435880fSJoe Perches					$val = $6 if ($skip_args ne "");
656773121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
656873121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
656973121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
65702435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6571459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6572f90774e1SJoe Perches					}
6573f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6574c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6575459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
65762435880fSJoe Perches					}
6577459cf0aeSJoe Perches				}
6578459cf0aeSJoe Perches			}
6579459cf0aeSJoe Perches		}
6580459cf0aeSJoe Perches
6581459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6582bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
658300180468SJoe Perches			my $oval = $1;
658400180468SJoe Perches			my $octal = perms_to_octal($oval);
6585f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6586459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6587f90774e1SJoe Perches			    $fix) {
658800180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
65892435880fSJoe Perches			}
659013214adfSAndy Whitcroft		}
65915a6d20ceSBjorn Andersson
65925a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
65935a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
65945a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
65955a6d20ceSBjorn Andersson			my $valid_licenses = qr{
65965a6d20ceSBjorn Andersson						GPL|
65975a6d20ceSBjorn Andersson						GPL\ v2|
65985a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
65995a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
66005a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
66015a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
66025a6d20ceSBjorn Andersson						Proprietary
66035a6d20ceSBjorn Andersson					}x;
66045a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
66055a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
66065a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
66075a6d20ceSBjorn Andersson			}
66085a6d20ceSBjorn Andersson		}
6609515a235eSJoe Perches	}
661013214adfSAndy Whitcroft
661113214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
661213214adfSAndy Whitcroft	# so just keep quiet.
661313214adfSAndy Whitcroft	if ($#rawlines == -1) {
661413214adfSAndy Whitcroft		exit(0);
66150a920b5bSAndy Whitcroft	}
66160a920b5bSAndy Whitcroft
66178905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
66188905a67cSAndy Whitcroft	# things that appear to be patches.
66198905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
66208905a67cSAndy Whitcroft		exit(0);
66218905a67cSAndy Whitcroft	}
66228905a67cSAndy Whitcroft
66238905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
66248905a67cSAndy Whitcroft	# just keep quiet.
66258905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
66268905a67cSAndy Whitcroft		exit(0);
66278905a67cSAndy Whitcroft	}
66288905a67cSAndy Whitcroft
6629a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6630000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6631000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
66320a920b5bSAndy Whitcroft	}
6633cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6634cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6635000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6636000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6637cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6638cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6639cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6640cd261496SGeert Uytterhoeven		}
66410a920b5bSAndy Whitcroft	}
66420a920b5bSAndy Whitcroft
6643f0a594c1SAndy Whitcroft	print report_dump();
664413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
664513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
66466c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
66476c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
66486c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
66496c72ffaaSAndy Whitcroft	}
66508905a67cSAndy Whitcroft
6651d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6652ef212196SJoe Perches		# If there were any defects found and not already fixing them
6653ef212196SJoe Perches		if (!$clean and !$fix) {
6654ef212196SJoe Perches			print << "EOM"
6655ef212196SJoe Perches
6656ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6657ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6658ef212196SJoe PerchesEOM
6659ef212196SJoe Perches		}
6660d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6661d2c0a235SAndy Whitcroft		# then suggest that.
6662d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6663b0781216SMike Frysinger			$rpt_cleaners = 0;
6664d8469f16SJoe Perches			print << "EOM"
6665d8469f16SJoe Perches
6666d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6667d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6668d8469f16SJoe PerchesEOM
6669d2c0a235SAndy Whitcroft		}
6670d2c0a235SAndy Whitcroft	}
6671d2c0a235SAndy Whitcroft
6672d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6673d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6674d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
66759624b8d6SJoe Perches		my $newfile = $filename;
66769624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
66773705ce5bSJoe Perches		my $linecount = 0;
66783705ce5bSJoe Perches		my $f;
66793705ce5bSJoe Perches
6680d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6681d752fcc8SJoe Perches
66823705ce5bSJoe Perches		open($f, '>', $newfile)
66833705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
66843705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
66853705ce5bSJoe Perches			$linecount++;
66863705ce5bSJoe Perches			if ($file) {
66873705ce5bSJoe Perches				if ($linecount > 3) {
66883705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
66893705ce5bSJoe Perches					print $f $fixed_line . "\n";
66903705ce5bSJoe Perches				}
66913705ce5bSJoe Perches			} else {
66923705ce5bSJoe Perches				print $f $fixed_line . "\n";
66933705ce5bSJoe Perches			}
66943705ce5bSJoe Perches		}
66953705ce5bSJoe Perches		close($f);
66963705ce5bSJoe Perches
66973705ce5bSJoe Perches		if (!$quiet) {
66983705ce5bSJoe Perches			print << "EOM";
6699d8469f16SJoe Perches
67003705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
67013705ce5bSJoe Perches
67023705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
67033705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
67043705ce5bSJoe Perches
67053705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
67063705ce5bSJoe PerchesNo warranties, expressed or implied...
67073705ce5bSJoe PerchesEOM
67083705ce5bSJoe Perches		}
67093705ce5bSJoe Perches	}
67103705ce5bSJoe Perches
6711d8469f16SJoe Perches	if ($quiet == 0) {
6712d8469f16SJoe Perches		print "\n";
6713d8469f16SJoe Perches		if ($clean == 1) {
6714d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6715d8469f16SJoe Perches		} else {
6716d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
67170a920b5bSAndy Whitcroft		}
67180a920b5bSAndy Whitcroft	}
67190a920b5bSAndy Whitcroft	return $clean;
67200a920b5bSAndy Whitcroft}
6721