xref: /linux-6.15/scripts/checkpatch.pl (revision c5967e98)
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|
383*c5967e98SJoe 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:|
47120112475SJoe Perches	Acked-by:|
47220112475SJoe Perches	Tested-by:|
47320112475SJoe Perches	Reviewed-by:|
47420112475SJoe Perches	Reported-by:|
4758543ae12SMugunthan V N	Suggested-by:|
47620112475SJoe Perches	To:|
47720112475SJoe Perches	Cc:
47820112475SJoe Perches)};
47920112475SJoe Perches
4801813087dSJoe Perchesour @typeListMisordered = (
4811813087dSJoe Perches	qr{char\s+(?:un)?signed},
4821813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4831813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4841813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4851813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4861813087dSJoe Perches	qr{short\s+(?:un)?signed},
4871813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4881813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4891813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4901813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4911813087dSJoe Perches	qr{int\s+(?:un)?signed},
4921813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4931813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4941813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4951813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4961813087dSJoe Perches	qr{long\s+(?:un)?signed},
4971813087dSJoe Perches);
4981813087dSJoe Perches
4998905a67cSAndy Whitcroftour @typeList = (
5008905a67cSAndy Whitcroft	qr{void},
5010c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5020c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5030c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5040c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5050c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5060c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5070c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5080c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5090c773d9dSJoe Perches	qr{(?:un)?signed},
5108905a67cSAndy Whitcroft	qr{float},
5118905a67cSAndy Whitcroft	qr{double},
5128905a67cSAndy Whitcroft	qr{bool},
5138905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5148905a67cSAndy Whitcroft	qr{union\s+$Ident},
5158905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5168905a67cSAndy Whitcroft	qr{${Ident}_t},
5178905a67cSAndy Whitcroft	qr{${Ident}_handler},
5188905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5191813087dSJoe Perches	@typeListMisordered,
5208905a67cSAndy Whitcroft);
521938224b5SJoe Perches
522938224b5SJoe Perchesour $C90_int_types = qr{(?x:
523938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
524938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
525938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
526938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
527938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
528938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
529938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
530938224b5SJoe Perches
531938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
532938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
533938224b5SJoe Perches	long\s+(?:un)?signed|
534938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
535938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
536938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
537938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
538938224b5SJoe Perches
539938224b5SJoe Perches	int\s+(?:un)?signed|
540938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
541938224b5SJoe Perches)};
542938224b5SJoe Perches
543485ff23eSAlex Dowadour @typeListFile = ();
5448716de38SJoe Perchesour @typeListWithAttr = (
5458716de38SJoe Perches	@typeList,
5468716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5478716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5488716de38SJoe Perches);
5498716de38SJoe Perches
550c45dcabdSAndy Whitcroftour @modifierList = (
551c45dcabdSAndy Whitcroft	qr{fastcall},
552c45dcabdSAndy Whitcroft);
553485ff23eSAlex Dowadour @modifierListFile = ();
5548905a67cSAndy Whitcroft
5552435880fSJoe Perchesour @mode_permission_funcs = (
5562435880fSJoe Perches	["module_param", 3],
5572435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5582435880fSJoe Perches	["module_param_array_named", 5],
5592435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5602435880fSJoe Perches	["proc_create(?:_data|)", 2],
561459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
562459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
563459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
564459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
565459cf0aeSJoe Perches	["__ATTR", 2],
5662435880fSJoe Perches);
5672435880fSJoe Perches
568515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
569515a235eSJoe Perchesour $mode_perms_search = "";
570515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
571515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
572515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
573515a235eSJoe Perches}
57400180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
575515a235eSJoe Perches
576b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
577b392c64fSJoe Perches	S_IWUGO		|
578b392c64fSJoe Perches	S_IWOTH		|
579b392c64fSJoe Perches	S_IRWXUGO	|
580b392c64fSJoe Perches	S_IALLUGO	|
581b392c64fSJoe Perches	0[0-7][0-7][2367]
582b392c64fSJoe Perches}x;
583b392c64fSJoe Perches
584f90774e1SJoe Perchesour %mode_permission_string_types = (
585f90774e1SJoe Perches	"S_IRWXU" => 0700,
586f90774e1SJoe Perches	"S_IRUSR" => 0400,
587f90774e1SJoe Perches	"S_IWUSR" => 0200,
588f90774e1SJoe Perches	"S_IXUSR" => 0100,
589f90774e1SJoe Perches	"S_IRWXG" => 0070,
590f90774e1SJoe Perches	"S_IRGRP" => 0040,
591f90774e1SJoe Perches	"S_IWGRP" => 0020,
592f90774e1SJoe Perches	"S_IXGRP" => 0010,
593f90774e1SJoe Perches	"S_IRWXO" => 0007,
594f90774e1SJoe Perches	"S_IROTH" => 0004,
595f90774e1SJoe Perches	"S_IWOTH" => 0002,
596f90774e1SJoe Perches	"S_IXOTH" => 0001,
597f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
598f90774e1SJoe Perches	"S_IRUGO" => 0444,
599f90774e1SJoe Perches	"S_IWUGO" => 0222,
600f90774e1SJoe Perches	"S_IXUGO" => 0111,
601f90774e1SJoe Perches);
602f90774e1SJoe Perches
603f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
604f90774e1SJoe Perchesour $mode_perms_string_search = "";
605f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
606f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
607f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
608f90774e1SJoe Perches}
60900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
61000180468SJoe Perchesour $multi_mode_perms_string_search = qr{
61100180468SJoe Perches	${single_mode_perms_string_search}
61200180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
61300180468SJoe Perches}x;
61400180468SJoe Perches
61500180468SJoe Perchessub perms_to_octal {
61600180468SJoe Perches	my ($string) = @_;
61700180468SJoe Perches
61800180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
61900180468SJoe Perches
62000180468SJoe Perches	my $val = "";
62100180468SJoe Perches	my $oval = "";
62200180468SJoe Perches	my $to = 0;
62300180468SJoe Perches	my $curpos = 0;
62400180468SJoe Perches	my $lastpos = 0;
62500180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
62600180468SJoe Perches		$curpos = pos($string);
62700180468SJoe Perches		my $match = $2;
62800180468SJoe Perches		my $omatch = $1;
62900180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
63000180468SJoe Perches		$lastpos = $curpos;
63100180468SJoe Perches		$to |= $mode_permission_string_types{$match};
63200180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
63300180468SJoe Perches		$val .= $match;
63400180468SJoe Perches		$oval .= $omatch;
63500180468SJoe Perches	}
63600180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
63700180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
63800180468SJoe Perches	return sprintf("%04o", $to);
63900180468SJoe Perches}
640f90774e1SJoe Perches
6417840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6427840a94cSWolfram Sang	irq|
643cdcee686SSergey Ryazanov	memory|
644cdcee686SSergey Ryazanov	time|
645cdcee686SSergey Ryazanov	reboot
6467840a94cSWolfram Sang)};
6477840a94cSWolfram Sang# memory.h: ARM has a custom one
6487840a94cSWolfram Sang
64966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
65066b47b4aSKees Cookmy $misspellings;
65166b47b4aSKees Cookmy %spelling_fix;
65236061e38SJoe Perches
65336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
65466b47b4aSKees Cook	while (<$spelling>) {
65566b47b4aSKees Cook		my $line = $_;
65666b47b4aSKees Cook
65766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
65866b47b4aSKees Cook		$line =~ s/^\s*//g;
65966b47b4aSKees Cook
66066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
66166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
66266b47b4aSKees Cook
66366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
66466b47b4aSKees Cook
66566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
66666b47b4aSKees Cook	}
66766b47b4aSKees Cook	close($spelling);
66836061e38SJoe Perches} else {
66936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
67036061e38SJoe Perches}
67166b47b4aSKees Cook
672ebfd7d62SJoe Perchesif ($codespell) {
673ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
674ebfd7d62SJoe Perches		while (<$spelling>) {
675ebfd7d62SJoe Perches			my $line = $_;
676ebfd7d62SJoe Perches
677ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
678ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
679ebfd7d62SJoe Perches
680ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
681ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
682ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
683ebfd7d62SJoe Perches
684ebfd7d62SJoe Perches			$line =~ s/,.*$//;
685ebfd7d62SJoe Perches
686ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
687ebfd7d62SJoe Perches
688ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
689ebfd7d62SJoe Perches		}
690ebfd7d62SJoe Perches		close($spelling);
691ebfd7d62SJoe Perches	} else {
692ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
693ebfd7d62SJoe Perches	}
694ebfd7d62SJoe Perches}
695ebfd7d62SJoe Perches
696ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
697ebfd7d62SJoe Perches
69875ad8c57SJerome Forissiersub read_words {
69975ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
70075ad8c57SJerome Forissier
70175ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
70275ad8c57SJerome Forissier		while (<$words>) {
703bf1fa1daSJoe Perches			my $line = $_;
704bf1fa1daSJoe Perches
705bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
706bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
707bf1fa1daSJoe Perches
708bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
709bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
710bf1fa1daSJoe Perches			if ($line =~ /\s/) {
71175ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
712bf1fa1daSJoe Perches				next;
713bf1fa1daSJoe Perches			}
714bf1fa1daSJoe Perches
71575ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
71675ad8c57SJerome Forissier			$$wordsRef .= $line;
717bf1fa1daSJoe Perches		}
71875ad8c57SJerome Forissier		close($file);
71975ad8c57SJerome Forissier		return 1;
720bf1fa1daSJoe Perches	}
721bf1fa1daSJoe Perches
72275ad8c57SJerome Forissier	return 0;
72375ad8c57SJerome Forissier}
72475ad8c57SJerome Forissier
72575ad8c57SJerome Forissiermy $const_structs = "";
72675ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
72775ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
72875ad8c57SJerome Forissier
72975ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
73075ad8c57SJerome Forissierif (length($typedefsfile)) {
73175ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
73275ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
73375ad8c57SJerome Forissier}
73475ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
73575ad8c57SJerome Forissier
7368905a67cSAndy Whitcroftsub build_types {
737485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
738485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7391813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7408716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
741c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
742ab7e23f3SJoe Perches	$BasicType	= qr{
743ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
744ab7e23f3SJoe Perches				(?:${all}\b)
745ab7e23f3SJoe Perches		}x;
7468905a67cSAndy Whitcroft	$NonptrType	= qr{
747d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
748cf655043SAndy Whitcroft			(?:
7496b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7508ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
751c45dcabdSAndy Whitcroft				(?:${all}\b)
752cf655043SAndy Whitcroft			)
753c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7548905a67cSAndy Whitcroft		  }x;
7551813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7561813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7571813087dSJoe Perches			(?:
7581813087dSJoe Perches				(?:${Misordered}\b)
7591813087dSJoe Perches			)
7601813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7611813087dSJoe Perches		  }x;
7628716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7638716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7648716de38SJoe Perches			(?:
7658716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
7668716de38SJoe Perches				(?:$typeTypedefs\b)|
7678716de38SJoe Perches				(?:${allWithAttr}\b)
7688716de38SJoe Perches			)
7698716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
7708716de38SJoe Perches		  }x;
7718905a67cSAndy Whitcroft	$Type	= qr{
772c45dcabdSAndy Whitcroft			$NonptrType
7731574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
774c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
7758905a67cSAndy Whitcroft		  }x;
7761813087dSJoe Perches	$TypeMisordered	= qr{
7771813087dSJoe Perches			$NonptrTypeMisordered
7781813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
7791813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
7801813087dSJoe Perches		  }x;
78191cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
7821813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
7838905a67cSAndy Whitcroft}
7848905a67cSAndy Whitcroftbuild_types();
7856c72ffaaSAndy Whitcroft
7867d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
787d1fe9c09SJoe Perches
788d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
789d1fe9c09SJoe Perches# requires at least perl version v5.10.0
790d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
791d1fe9c09SJoe Perches
792d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
7932435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
794c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
7957d2367afSJoe Perches
796f8422308SJoe Perchesour $declaration_macros = qr{(?x:
7973e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
798fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
7993d102fc0SGilad Ben-Yossef	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
8003d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
801f8422308SJoe Perches)};
802f8422308SJoe Perches
8037d2367afSJoe Perchessub deparenthesize {
8047d2367afSJoe Perches	my ($string) = @_;
8057d2367afSJoe Perches	return "" if (!defined($string));
8065b9553abSJoe Perches
8075b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8085b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8095b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8105b9553abSJoe Perches	}
8115b9553abSJoe Perches
8127d2367afSJoe Perches	$string =~ s@\s+@ @g;
8135b9553abSJoe Perches
8147d2367afSJoe Perches	return $string;
8157d2367afSJoe Perches}
8167d2367afSJoe Perches
8173445686aSJoe Perchessub seed_camelcase_file {
8183445686aSJoe Perches	my ($file) = @_;
8193445686aSJoe Perches
8203445686aSJoe Perches	return if (!(-f $file));
8213445686aSJoe Perches
8223445686aSJoe Perches	local $/;
8233445686aSJoe Perches
8243445686aSJoe Perches	open(my $include_file, '<', "$file")
8253445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8263445686aSJoe Perches	my $text = <$include_file>;
8273445686aSJoe Perches	close($include_file);
8283445686aSJoe Perches
8293445686aSJoe Perches	my @lines = split('\n', $text);
8303445686aSJoe Perches
8313445686aSJoe Perches	foreach my $line (@lines) {
8323445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8333445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8343445686aSJoe Perches			$camelcase{$1} = 1;
83511ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
83611ea516aSJoe Perches			$camelcase{$1} = 1;
83711ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8383445686aSJoe Perches			$camelcase{$1} = 1;
8393445686aSJoe Perches		}
8403445686aSJoe Perches	}
8413445686aSJoe Perches}
8423445686aSJoe Perches
84385b0ee18SJoe Perchessub is_maintained_obsolete {
84485b0ee18SJoe Perches	my ($filename) = @_;
84585b0ee18SJoe Perches
846f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
84785b0ee18SJoe Perches
8480616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
84985b0ee18SJoe Perches
85085b0ee18SJoe Perches	return $status =~ /obsolete/i;
85185b0ee18SJoe Perches}
85285b0ee18SJoe Perches
8533b6e8ac9SJoe Perchessub is_SPDX_License_valid {
8543b6e8ac9SJoe Perches	my ($license) = @_;
8553b6e8ac9SJoe Perches
85656294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
8573b6e8ac9SJoe Perches
85856294112SJoe Perches	my $root_path = abs_path($root);
85956294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
8603b6e8ac9SJoe Perches	return 0 if ($status ne "");
8613b6e8ac9SJoe Perches	return 1;
8623b6e8ac9SJoe Perches}
8633b6e8ac9SJoe Perches
8643445686aSJoe Perchesmy $camelcase_seeded = 0;
8653445686aSJoe Perchessub seed_camelcase_includes {
8663445686aSJoe Perches	return if ($camelcase_seeded);
8673445686aSJoe Perches
8683445686aSJoe Perches	my $files;
869c707a81dSJoe Perches	my $camelcase_cache = "";
870c707a81dSJoe Perches	my @include_files = ();
871c707a81dSJoe Perches
872c707a81dSJoe Perches	$camelcase_seeded = 1;
873351b2a1fSJoe Perches
8743645e328SRichard Genoud	if (-e ".git") {
875351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
876351b2a1fSJoe Perches		chomp $git_last_include_commit;
877c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
878c707a81dSJoe Perches	} else {
879c707a81dSJoe Perches		my $last_mod_date = 0;
880c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
881c707a81dSJoe Perches		@include_files = split('\n', $files);
882c707a81dSJoe Perches		foreach my $file (@include_files) {
883c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
884c707a81dSJoe Perches						   localtime((stat $file)[9]));
885c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
886c707a81dSJoe Perches		}
887c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
888c707a81dSJoe Perches	}
889c707a81dSJoe Perches
890c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
891c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
892c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
893351b2a1fSJoe Perches		while (<$camelcase_file>) {
894351b2a1fSJoe Perches			chomp;
895351b2a1fSJoe Perches			$camelcase{$_} = 1;
896351b2a1fSJoe Perches		}
897351b2a1fSJoe Perches		close($camelcase_file);
898351b2a1fSJoe Perches
899351b2a1fSJoe Perches		return;
900351b2a1fSJoe Perches	}
901c707a81dSJoe Perches
9023645e328SRichard Genoud	if (-e ".git") {
903c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
904c707a81dSJoe Perches		@include_files = split('\n', $files);
9053445686aSJoe Perches	}
906c707a81dSJoe Perches
9073445686aSJoe Perches	foreach my $file (@include_files) {
9083445686aSJoe Perches		seed_camelcase_file($file);
9093445686aSJoe Perches	}
910351b2a1fSJoe Perches
911c707a81dSJoe Perches	if ($camelcase_cache ne "") {
912351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
913c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
914c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
915351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
916351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
917351b2a1fSJoe Perches		}
918351b2a1fSJoe Perches		close($camelcase_file);
919351b2a1fSJoe Perches	}
9203445686aSJoe Perches}
9213445686aSJoe Perches
922d311cd44SJoe Perchessub git_commit_info {
923d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
924d311cd44SJoe Perches
925d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
926d311cd44SJoe Perches
927d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
928d311cd44SJoe Perches	$output =~ s/^\s*//gm;
929d311cd44SJoe Perches	my @lines = split("\n", $output);
930d311cd44SJoe Perches
9310d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9320d7835fcSJoe Perches
933d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
934d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
935d311cd44SJoe Perches# all matching commit ids, but it's very slow...
936d311cd44SJoe Perches#
937d311cd44SJoe Perches#		echo "checking commits $1..."
938d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
939d311cd44SJoe Perches#		while read line ; do
940d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
941d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
942d311cd44SJoe Perches#		done
943d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
944948b133aSHeinrich Schuchardt		$id = undef;
945d311cd44SJoe Perches	} else {
946d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
947d311cd44SJoe Perches		$desc = substr($lines[0], 41);
948d311cd44SJoe Perches	}
949d311cd44SJoe Perches
950d311cd44SJoe Perches	return ($id, $desc);
951d311cd44SJoe Perches}
952d311cd44SJoe Perches
9536c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9540a920b5bSAndy Whitcroft
95500df344fSAndy Whitcroftmy @rawlines = ();
956c2fdda0dSAndy Whitcroftmy @lines = ();
9573705ce5bSJoe Perchesmy @fixed = ();
958d752fcc8SJoe Perchesmy @fixed_inserted = ();
959d752fcc8SJoe Perchesmy @fixed_deleted = ();
960194f66fcSJoe Perchesmy $fixlinenr = -1;
961194f66fcSJoe Perches
9624a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9634a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9644a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9654a593c34SDu, Changbin
9664a593c34SDu, Changbinif ($git) {
9674a593c34SDu, Changbin	my @commits = ();
9680dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
9694a593c34SDu, Changbin		my $git_range;
97028898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
97128898fd1SJoe Perches			$git_range = "-$2 $1";
9724a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
9734a593c34SDu, Changbin			$git_range = "$commit_expr";
9744a593c34SDu, Changbin		} else {
9750dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
9760dea9f1eSJoe Perches		}
9770dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
9780dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
97928898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
98028898fd1SJoe Perches			next if (!defined($1) || !defined($2));
9810dea9f1eSJoe Perches			my $sha1 = $1;
9820dea9f1eSJoe Perches			my $subject = $2;
9830dea9f1eSJoe Perches			unshift(@commits, $sha1);
9840dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
9854a593c34SDu, Changbin		}
9864a593c34SDu, Changbin	}
9874a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
9884a593c34SDu, Changbin	@ARGV = @commits;
9894a593c34SDu, Changbin}
9904a593c34SDu, Changbin
991c2fdda0dSAndy Whitcroftmy $vname;
9926c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
99321caa13cSAndy Whitcroft	my $FILE;
9944a593c34SDu, Changbin	if ($git) {
9954a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
9964a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
9974a593c34SDu, Changbin	} elsif ($file) {
99821caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
9996c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
100021caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
100121caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10026c72ffaaSAndy Whitcroft	} else {
100321caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10046c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10056c72ffaaSAndy Whitcroft	}
1006c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1007c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10084a593c34SDu, Changbin	} elsif ($git) {
10090dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1010c2fdda0dSAndy Whitcroft	} else {
1011c2fdda0dSAndy Whitcroft		$vname = $filename;
1012c2fdda0dSAndy Whitcroft	}
101321caa13cSAndy Whitcroft	while (<$FILE>) {
10140a920b5bSAndy Whitcroft		chomp;
101500df344fSAndy Whitcroft		push(@rawlines, $_);
10166c72ffaaSAndy Whitcroft	}
101721caa13cSAndy Whitcroft	close($FILE);
1018d8469f16SJoe Perches
1019d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1020d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1021d8469f16SJoe Perches		print "$vname\n";
1022d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1023d8469f16SJoe Perches	}
1024d8469f16SJoe Perches
1025c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10260a920b5bSAndy Whitcroft		$exit = 1;
10270a920b5bSAndy Whitcroft	}
102800df344fSAndy Whitcroft	@rawlines = ();
102913214adfSAndy Whitcroft	@lines = ();
10303705ce5bSJoe Perches	@fixed = ();
1031d752fcc8SJoe Perches	@fixed_inserted = ();
1032d752fcc8SJoe Perches	@fixed_deleted = ();
1033194f66fcSJoe Perches	$fixlinenr = -1;
1034485ff23eSAlex Dowad	@modifierListFile = ();
1035485ff23eSAlex Dowad	@typeListFile = ();
1036485ff23eSAlex Dowad	build_types();
10370a920b5bSAndy Whitcroft}
10380a920b5bSAndy Whitcroft
1039d8469f16SJoe Perchesif (!$quiet) {
10403c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10413c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10423c816e49SJoe Perches
10435b57980dSJoe Perches	if (!$perl_version_ok) {
1044d8469f16SJoe Perches		print << "EOM"
1045d8469f16SJoe Perches
1046d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
10475b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1048d8469f16SJoe PerchesEOM
1049d8469f16SJoe Perches	}
1050d8469f16SJoe Perches	if ($exit) {
1051d8469f16SJoe Perches		print << "EOM"
1052d8469f16SJoe Perches
1053d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1054d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1055d8469f16SJoe PerchesEOM
1056d8469f16SJoe Perches	}
1057d8469f16SJoe Perches}
1058d8469f16SJoe Perches
10590a920b5bSAndy Whitcroftexit($exit);
10600a920b5bSAndy Whitcroft
10610a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10626c72ffaaSAndy Whitcroft	my ($root) = @_;
10636c72ffaaSAndy Whitcroft
10646c72ffaaSAndy Whitcroft	my @tree_check = (
10656c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
10666c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
10676c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
10686c72ffaaSAndy Whitcroft	);
10696c72ffaaSAndy Whitcroft
10706c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
10716c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
10720a920b5bSAndy Whitcroft			return 0;
10730a920b5bSAndy Whitcroft		}
10746c72ffaaSAndy Whitcroft	}
10756c72ffaaSAndy Whitcroft	return 1;
10766c72ffaaSAndy Whitcroft}
10770a920b5bSAndy Whitcroft
107820112475SJoe Perchessub parse_email {
107920112475SJoe Perches	my ($formatted_email) = @_;
108020112475SJoe Perches
108120112475SJoe Perches	my $name = "";
108220112475SJoe Perches	my $address = "";
108320112475SJoe Perches	my $comment = "";
108420112475SJoe Perches
108520112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
108620112475SJoe Perches		$name = $1;
108720112475SJoe Perches		$address = $2;
108820112475SJoe Perches		$comment = $3 if defined $3;
108920112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
109020112475SJoe Perches		$address = $1;
109120112475SJoe Perches		$comment = $2 if defined $2;
109220112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
109320112475SJoe Perches		$address = $1;
109420112475SJoe Perches		$comment = $2 if defined $2;
109585e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
109620112475SJoe Perches		$name = $formatted_email;
10973705ce5bSJoe Perches		$name = trim($name);
109820112475SJoe Perches		$name =~ s/^\"|\"$//g;
109920112475SJoe Perches		# If there's a name left after stripping spaces and
110020112475SJoe Perches		# leading quotes, and the address doesn't have both
110120112475SJoe Perches		# leading and trailing angle brackets, the address
110220112475SJoe Perches		# is invalid. ie:
110320112475SJoe Perches		#   "joe smith [email protected]" bad
110420112475SJoe Perches		#   "joe smith <[email protected]" bad
110520112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
110620112475SJoe Perches			$name = "";
110720112475SJoe Perches			$address = "";
110820112475SJoe Perches			$comment = "";
110920112475SJoe Perches		}
111020112475SJoe Perches	}
111120112475SJoe Perches
11123705ce5bSJoe Perches	$name = trim($name);
111320112475SJoe Perches	$name =~ s/^\"|\"$//g;
11143705ce5bSJoe Perches	$address = trim($address);
111520112475SJoe Perches	$address =~ s/^\<|\>$//g;
111620112475SJoe Perches
111720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
111820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
111920112475SJoe Perches		$name = "\"$name\"";
112020112475SJoe Perches	}
112120112475SJoe Perches
112220112475SJoe Perches	return ($name, $address, $comment);
112320112475SJoe Perches}
112420112475SJoe Perches
112520112475SJoe Perchessub format_email {
112620112475SJoe Perches	my ($name, $address) = @_;
112720112475SJoe Perches
112820112475SJoe Perches	my $formatted_email;
112920112475SJoe Perches
11303705ce5bSJoe Perches	$name = trim($name);
113120112475SJoe Perches	$name =~ s/^\"|\"$//g;
11323705ce5bSJoe Perches	$address = trim($address);
113320112475SJoe Perches
113420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
113520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
113620112475SJoe Perches		$name = "\"$name\"";
113720112475SJoe Perches	}
113820112475SJoe Perches
113920112475SJoe Perches	if ("$name" eq "") {
114020112475SJoe Perches		$formatted_email = "$address";
114120112475SJoe Perches	} else {
114220112475SJoe Perches		$formatted_email = "$name <$address>";
114320112475SJoe Perches	}
114420112475SJoe Perches
114520112475SJoe Perches	return $formatted_email;
114620112475SJoe Perches}
114720112475SJoe Perches
1148d311cd44SJoe Perchessub which {
1149d311cd44SJoe Perches	my ($bin) = @_;
1150d311cd44SJoe Perches
1151d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1152d311cd44SJoe Perches		if (-e "$path/$bin") {
1153d311cd44SJoe Perches			return "$path/$bin";
1154d311cd44SJoe Perches		}
1155d311cd44SJoe Perches	}
1156d311cd44SJoe Perches
1157d311cd44SJoe Perches	return "";
1158d311cd44SJoe Perches}
1159d311cd44SJoe Perches
1160000d1cc1SJoe Perchessub which_conf {
1161000d1cc1SJoe Perches	my ($conf) = @_;
1162000d1cc1SJoe Perches
1163000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1164000d1cc1SJoe Perches		if (-e "$path/$conf") {
1165000d1cc1SJoe Perches			return "$path/$conf";
1166000d1cc1SJoe Perches		}
1167000d1cc1SJoe Perches	}
1168000d1cc1SJoe Perches
1169000d1cc1SJoe Perches	return "";
1170000d1cc1SJoe Perches}
1171000d1cc1SJoe Perches
11720a920b5bSAndy Whitcroftsub expand_tabs {
11730a920b5bSAndy Whitcroft	my ($str) = @_;
11740a920b5bSAndy Whitcroft
11750a920b5bSAndy Whitcroft	my $res = '';
11760a920b5bSAndy Whitcroft	my $n = 0;
11770a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
11780a920b5bSAndy Whitcroft		if ($c eq "\t") {
11790a920b5bSAndy Whitcroft			$res .= ' ';
11800a920b5bSAndy Whitcroft			$n++;
11810a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
11820a920b5bSAndy Whitcroft				$res .= ' ';
11830a920b5bSAndy Whitcroft			}
11840a920b5bSAndy Whitcroft			next;
11850a920b5bSAndy Whitcroft		}
11860a920b5bSAndy Whitcroft		$res .= $c;
11870a920b5bSAndy Whitcroft		$n++;
11880a920b5bSAndy Whitcroft	}
11890a920b5bSAndy Whitcroft
11900a920b5bSAndy Whitcroft	return $res;
11910a920b5bSAndy Whitcroft}
11926c72ffaaSAndy Whitcroftsub copy_spacing {
1193773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
11946c72ffaaSAndy Whitcroft	return $res;
11956c72ffaaSAndy Whitcroft}
11960a920b5bSAndy Whitcroft
11974a0df2efSAndy Whitcroftsub line_stats {
11984a0df2efSAndy Whitcroft	my ($line) = @_;
11994a0df2efSAndy Whitcroft
12004a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12014a0df2efSAndy Whitcroft	$line =~ s/^.//;
12024a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12034a0df2efSAndy Whitcroft
12044a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12054a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12064a0df2efSAndy Whitcroft
12074a0df2efSAndy Whitcroft	return (length($line), length($white));
12084a0df2efSAndy Whitcroft}
12094a0df2efSAndy Whitcroft
1210773647a0SAndy Whitcroftmy $sanitise_quote = '';
1211773647a0SAndy Whitcroft
1212773647a0SAndy Whitcroftsub sanitise_line_reset {
1213773647a0SAndy Whitcroft	my ($in_comment) = @_;
1214773647a0SAndy Whitcroft
1215773647a0SAndy Whitcroft	if ($in_comment) {
1216773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1217773647a0SAndy Whitcroft	} else {
1218773647a0SAndy Whitcroft		$sanitise_quote = '';
1219773647a0SAndy Whitcroft	}
1220773647a0SAndy Whitcroft}
122100df344fSAndy Whitcroftsub sanitise_line {
122200df344fSAndy Whitcroft	my ($line) = @_;
122300df344fSAndy Whitcroft
122400df344fSAndy Whitcroft	my $res = '';
122500df344fSAndy Whitcroft	my $l = '';
122600df344fSAndy Whitcroft
1227c2fdda0dSAndy Whitcroft	my $qlen = 0;
1228773647a0SAndy Whitcroft	my $off = 0;
1229773647a0SAndy Whitcroft	my $c;
123000df344fSAndy Whitcroft
1231773647a0SAndy Whitcroft	# Always copy over the diff marker.
1232773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1233773647a0SAndy Whitcroft
1234773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1235773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1236773647a0SAndy Whitcroft
12378d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1238773647a0SAndy Whitcroft		# and end, all to $;.
1239773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1240773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1241773647a0SAndy Whitcroft
1242773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1243773647a0SAndy Whitcroft			$off++;
124400df344fSAndy Whitcroft			next;
1245773647a0SAndy Whitcroft		}
124681bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1247773647a0SAndy Whitcroft			$sanitise_quote = '';
1248773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1249773647a0SAndy Whitcroft			$off++;
1250773647a0SAndy Whitcroft			next;
1251773647a0SAndy Whitcroft		}
1252113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1253113f04a8SDaniel Walker			$sanitise_quote = '//';
1254113f04a8SDaniel Walker
1255113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1256113f04a8SDaniel Walker			$off++;
1257113f04a8SDaniel Walker			next;
1258113f04a8SDaniel Walker		}
1259773647a0SAndy Whitcroft
1260773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1261773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1262773647a0SAndy Whitcroft		    $c eq "\\") {
1263773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1264773647a0SAndy Whitcroft			$off++;
1265773647a0SAndy Whitcroft			next;
1266773647a0SAndy Whitcroft		}
1267773647a0SAndy Whitcroft		# Regular quotes.
1268773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1269773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1270773647a0SAndy Whitcroft				$sanitise_quote = $c;
1271773647a0SAndy Whitcroft
1272773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1273773647a0SAndy Whitcroft				next;
1274773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1275773647a0SAndy Whitcroft				$sanitise_quote = '';
127600df344fSAndy Whitcroft			}
127700df344fSAndy Whitcroft		}
1278773647a0SAndy Whitcroft
1279fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1280773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1281773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1282113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1283113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1284773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1285773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
128600df344fSAndy Whitcroft		} else {
1287773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
128800df344fSAndy Whitcroft		}
1289c2fdda0dSAndy Whitcroft	}
1290c2fdda0dSAndy Whitcroft
1291113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1292113f04a8SDaniel Walker		$sanitise_quote = '';
1293113f04a8SDaniel Walker	}
1294113f04a8SDaniel Walker
1295c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1296c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1297c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1298c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1299c2fdda0dSAndy Whitcroft
1300c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1301c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1302c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1303c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1304c2fdda0dSAndy Whitcroft	}
1305c2fdda0dSAndy Whitcroft
1306dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1307dadf680dSJoe Perches		my $match = $1;
1308dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1309dadf680dSJoe Perches	}
1310dadf680dSJoe Perches
131100df344fSAndy Whitcroft	return $res;
131200df344fSAndy Whitcroft}
131300df344fSAndy Whitcroft
1314a6962d72SJoe Perchessub get_quoted_string {
1315a6962d72SJoe Perches	my ($line, $rawline) = @_;
1316a6962d72SJoe Perches
1317478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
131833acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1319a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1320a6962d72SJoe Perches}
1321a6962d72SJoe Perches
13228905a67cSAndy Whitcroftsub ctx_statement_block {
13238905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13248905a67cSAndy Whitcroft	my $line = $linenr - 1;
13258905a67cSAndy Whitcroft	my $blk = '';
13268905a67cSAndy Whitcroft	my $soff = $off;
13278905a67cSAndy Whitcroft	my $coff = $off - 1;
1328773647a0SAndy Whitcroft	my $coff_set = 0;
13298905a67cSAndy Whitcroft
133013214adfSAndy Whitcroft	my $loff = 0;
133113214adfSAndy Whitcroft
13328905a67cSAndy Whitcroft	my $type = '';
13338905a67cSAndy Whitcroft	my $level = 0;
1334a2750645SAndy Whitcroft	my @stack = ();
1335cf655043SAndy Whitcroft	my $p;
13368905a67cSAndy Whitcroft	my $c;
13378905a67cSAndy Whitcroft	my $len = 0;
133813214adfSAndy Whitcroft
133913214adfSAndy Whitcroft	my $remainder;
13408905a67cSAndy Whitcroft	while (1) {
1341a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1342a2750645SAndy Whitcroft
1343773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
13448905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
13458905a67cSAndy Whitcroft		# context.
13468905a67cSAndy Whitcroft		if ($off >= $len) {
13478905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1348dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1349c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13508905a67cSAndy Whitcroft				$remain--;
135113214adfSAndy Whitcroft				$loff = $len;
1352c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13538905a67cSAndy Whitcroft				$len = length($blk);
13548905a67cSAndy Whitcroft				$line++;
13558905a67cSAndy Whitcroft				last;
13568905a67cSAndy Whitcroft			}
13578905a67cSAndy Whitcroft			# Bail if there is no further context.
13588905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
135913214adfSAndy Whitcroft			if ($off >= $len) {
13608905a67cSAndy Whitcroft				last;
13618905a67cSAndy Whitcroft			}
1362f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1363f74bd194SAndy Whitcroft				$level++;
1364f74bd194SAndy Whitcroft				$type = '#';
1365f74bd194SAndy Whitcroft			}
13668905a67cSAndy Whitcroft		}
1367cf655043SAndy Whitcroft		$p = $c;
13688905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
136913214adfSAndy Whitcroft		$remainder = substr($blk, $off);
13708905a67cSAndy Whitcroft
1371773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
13724635f4fbSAndy Whitcroft
13734635f4fbSAndy Whitcroft		# Handle nested #if/#else.
13744635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
13754635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
13764635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
13774635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
13784635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
13794635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
13804635f4fbSAndy Whitcroft		}
13814635f4fbSAndy Whitcroft
13828905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
13838905a67cSAndy Whitcroft		# outermost level.
13848905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
13858905a67cSAndy Whitcroft			last;
13868905a67cSAndy Whitcroft		}
13878905a67cSAndy Whitcroft
138813214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1389773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1390773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1391773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1392773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1393773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1394773647a0SAndy Whitcroft			$coff_set = 1;
1395773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1396773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
139713214adfSAndy Whitcroft		}
139813214adfSAndy Whitcroft
13998905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14008905a67cSAndy Whitcroft			$level++;
14018905a67cSAndy Whitcroft			$type = '(';
14028905a67cSAndy Whitcroft		}
14038905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14048905a67cSAndy Whitcroft			$level--;
14058905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14068905a67cSAndy Whitcroft
14078905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
14088905a67cSAndy Whitcroft				$coff = $off;
1409773647a0SAndy Whitcroft				$coff_set = 1;
1410773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
14118905a67cSAndy Whitcroft			}
14128905a67cSAndy Whitcroft		}
14138905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14148905a67cSAndy Whitcroft			$level++;
14158905a67cSAndy Whitcroft			$type = '{';
14168905a67cSAndy Whitcroft		}
14178905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14188905a67cSAndy Whitcroft			$level--;
14198905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14208905a67cSAndy Whitcroft
14218905a67cSAndy Whitcroft			if ($level == 0) {
1422b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1423b998e001SPatrick Pannuto					$off++;
1424b998e001SPatrick Pannuto				}
14258905a67cSAndy Whitcroft				last;
14268905a67cSAndy Whitcroft			}
14278905a67cSAndy Whitcroft		}
1428f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1429f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1430f74bd194SAndy Whitcroft			$level--;
1431f74bd194SAndy Whitcroft			$type = '';
1432f74bd194SAndy Whitcroft			$off++;
1433f74bd194SAndy Whitcroft			last;
1434f74bd194SAndy Whitcroft		}
14358905a67cSAndy Whitcroft		$off++;
14368905a67cSAndy Whitcroft	}
1437a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
143813214adfSAndy Whitcroft	if ($off == $len) {
1439a3bb97a7SAndy Whitcroft		$loff = $len + 1;
144013214adfSAndy Whitcroft		$line++;
144113214adfSAndy Whitcroft		$remain--;
144213214adfSAndy Whitcroft	}
14438905a67cSAndy Whitcroft
14448905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
14458905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
14468905a67cSAndy Whitcroft
14478905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
14488905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
14498905a67cSAndy Whitcroft
1450773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
145113214adfSAndy Whitcroft
145213214adfSAndy Whitcroft	return ($statement, $condition,
145313214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
145413214adfSAndy Whitcroft}
145513214adfSAndy Whitcroft
1456cf655043SAndy Whitcroftsub statement_lines {
1457cf655043SAndy Whitcroft	my ($stmt) = @_;
1458cf655043SAndy Whitcroft
1459cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1460cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1461cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1462cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1463cf655043SAndy Whitcroft
1464cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1465cf655043SAndy Whitcroft
1466cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1467cf655043SAndy Whitcroft}
1468cf655043SAndy Whitcroft
1469cf655043SAndy Whitcroftsub statement_rawlines {
1470cf655043SAndy Whitcroft	my ($stmt) = @_;
1471cf655043SAndy Whitcroft
1472cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1473cf655043SAndy Whitcroft
1474cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1475cf655043SAndy Whitcroft}
1476cf655043SAndy Whitcroft
1477cf655043SAndy Whitcroftsub statement_block_size {
1478cf655043SAndy Whitcroft	my ($stmt) = @_;
1479cf655043SAndy Whitcroft
1480cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1481cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1482cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1483cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1484cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1485cf655043SAndy Whitcroft
1486cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1487cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1488cf655043SAndy Whitcroft
1489cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1490cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1491cf655043SAndy Whitcroft
1492cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1493cf655043SAndy Whitcroft		return $stmt_lines;
1494cf655043SAndy Whitcroft	} else {
1495cf655043SAndy Whitcroft		return $stmt_statements;
1496cf655043SAndy Whitcroft	}
1497cf655043SAndy Whitcroft}
1498cf655043SAndy Whitcroft
149913214adfSAndy Whitcroftsub ctx_statement_full {
150013214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
150113214adfSAndy Whitcroft	my ($statement, $condition, $level);
150213214adfSAndy Whitcroft
150313214adfSAndy Whitcroft	my (@chunks);
150413214adfSAndy Whitcroft
1505cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
150613214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
150713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1508773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
150913214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1510cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1511cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1512cf655043SAndy Whitcroft	}
1513cf655043SAndy Whitcroft
1514cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1515cf655043SAndy Whitcroft	# could continue the statement.
1516cf655043SAndy Whitcroft	for (;;) {
151713214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
151813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1519cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1520773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1521cf655043SAndy Whitcroft		#print "C: push\n";
1522cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
152313214adfSAndy Whitcroft	}
152413214adfSAndy Whitcroft
152513214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15268905a67cSAndy Whitcroft}
15278905a67cSAndy Whitcroft
15284a0df2efSAndy Whitcroftsub ctx_block_get {
1529f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15304a0df2efSAndy Whitcroft	my $line;
15314a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15324a0df2efSAndy Whitcroft	my $blk = '';
15334a0df2efSAndy Whitcroft	my @o;
15344a0df2efSAndy Whitcroft	my @c;
15354a0df2efSAndy Whitcroft	my @res = ();
15364a0df2efSAndy Whitcroft
1537f0a594c1SAndy Whitcroft	my $level = 0;
15384635f4fbSAndy Whitcroft	my @stack = ($level);
153900df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
154000df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
154100df344fSAndy Whitcroft		$remain--;
154200df344fSAndy Whitcroft
154300df344fSAndy Whitcroft		$blk .= $rawlines[$line];
15444635f4fbSAndy Whitcroft
15454635f4fbSAndy Whitcroft		# Handle nested #if/#else.
154601464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
15474635f4fbSAndy Whitcroft			push(@stack, $level);
154801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
15494635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
155001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15514635f4fbSAndy Whitcroft			$level = pop(@stack);
15524635f4fbSAndy Whitcroft		}
15534635f4fbSAndy Whitcroft
155401464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1555f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1556f0a594c1SAndy Whitcroft			if ($off > 0) {
1557f0a594c1SAndy Whitcroft				$off--;
1558f0a594c1SAndy Whitcroft				next;
1559f0a594c1SAndy Whitcroft			}
15604a0df2efSAndy Whitcroft
1561f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1562f0a594c1SAndy Whitcroft				$level--;
1563f0a594c1SAndy Whitcroft				last if ($level == 0);
1564f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1565f0a594c1SAndy Whitcroft				$level++;
1566f0a594c1SAndy Whitcroft			}
1567f0a594c1SAndy Whitcroft		}
15684a0df2efSAndy Whitcroft
1569f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
157000df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
15714a0df2efSAndy Whitcroft		}
15724a0df2efSAndy Whitcroft
1573f0a594c1SAndy Whitcroft		last if ($level == 0);
15744a0df2efSAndy Whitcroft	}
15754a0df2efSAndy Whitcroft
1576f0a594c1SAndy Whitcroft	return ($level, @res);
15774a0df2efSAndy Whitcroft}
15784a0df2efSAndy Whitcroftsub ctx_block_outer {
15794a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15804a0df2efSAndy Whitcroft
1581f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1582f0a594c1SAndy Whitcroft	return @r;
15834a0df2efSAndy Whitcroft}
15844a0df2efSAndy Whitcroftsub ctx_block {
15854a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15864a0df2efSAndy Whitcroft
1587f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1588f0a594c1SAndy Whitcroft	return @r;
1589653d4876SAndy Whitcroft}
1590653d4876SAndy Whitcroftsub ctx_statement {
1591f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1592f0a594c1SAndy Whitcroft
1593f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1594f0a594c1SAndy Whitcroft	return @r;
1595f0a594c1SAndy Whitcroft}
1596f0a594c1SAndy Whitcroftsub ctx_block_level {
1597653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1598653d4876SAndy Whitcroft
1599f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16004a0df2efSAndy Whitcroft}
16019c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16029c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16039c0ca6f9SAndy Whitcroft
16049c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16059c0ca6f9SAndy Whitcroft}
16064a0df2efSAndy Whitcroft
16074a0df2efSAndy Whitcroftsub ctx_locate_comment {
16084a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16094a0df2efSAndy Whitcroft
16104a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1611beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
16124a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16134a0df2efSAndy Whitcroft
16144a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16154a0df2efSAndy Whitcroft	# comment.
16164a0df2efSAndy Whitcroft	my $in_comment = 0;
16174a0df2efSAndy Whitcroft	$current_comment = '';
16184a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
161900df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
162000df344fSAndy Whitcroft		#warn "           $line\n";
16214a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16224a0df2efSAndy Whitcroft			$in_comment = 1;
16234a0df2efSAndy Whitcroft		}
16244a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16254a0df2efSAndy Whitcroft			$in_comment = 1;
16264a0df2efSAndy Whitcroft		}
16274a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16284a0df2efSAndy Whitcroft			$current_comment = '';
16294a0df2efSAndy Whitcroft		}
16304a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16314a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16324a0df2efSAndy Whitcroft			$in_comment = 0;
16334a0df2efSAndy Whitcroft		}
16344a0df2efSAndy Whitcroft	}
16354a0df2efSAndy Whitcroft
16364a0df2efSAndy Whitcroft	chomp($current_comment);
16374a0df2efSAndy Whitcroft	return($current_comment);
16384a0df2efSAndy Whitcroft}
16394a0df2efSAndy Whitcroftsub ctx_has_comment {
16404a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16414a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
16424a0df2efSAndy Whitcroft
164300df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
16444a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
16454a0df2efSAndy Whitcroft
16464a0df2efSAndy Whitcroft	return ($cmt ne '');
16474a0df2efSAndy Whitcroft}
16484a0df2efSAndy Whitcroft
16494d001e4dSAndy Whitcroftsub raw_line {
16504d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16514d001e4dSAndy Whitcroft
16524d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16534d001e4dSAndy Whitcroft	$cnt++;
16544d001e4dSAndy Whitcroft
16554d001e4dSAndy Whitcroft	my $line;
16564d001e4dSAndy Whitcroft	while ($cnt) {
16574d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16584d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16594d001e4dSAndy Whitcroft		$cnt--;
16604d001e4dSAndy Whitcroft	}
16614d001e4dSAndy Whitcroft
16624d001e4dSAndy Whitcroft	return $line;
16634d001e4dSAndy Whitcroft}
16644d001e4dSAndy Whitcroft
16652a9f9d85STobin C. Hardingsub get_stat_real {
16662a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
16672a9f9d85STobin C. Harding
16682a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
16692a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
16702a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
16712a9f9d85STobin C. Harding	}
16722a9f9d85STobin C. Harding
16732a9f9d85STobin C. Harding	return $stat_real;
16742a9f9d85STobin C. Harding}
16752a9f9d85STobin C. Harding
1676e3d95a2aSTobin C. Hardingsub get_stat_here {
1677e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1678e3d95a2aSTobin C. Harding
1679e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1680e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1681e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1682e3d95a2aSTobin C. Harding	}
1683e3d95a2aSTobin C. Harding
1684e3d95a2aSTobin C. Harding	return $herectx;
1685e3d95a2aSTobin C. Harding}
1686e3d95a2aSTobin C. Harding
16870a920b5bSAndy Whitcroftsub cat_vet {
16880a920b5bSAndy Whitcroft	my ($vet) = @_;
16899c0ca6f9SAndy Whitcroft	my ($res, $coded);
16900a920b5bSAndy Whitcroft
16919c0ca6f9SAndy Whitcroft	$res = '';
16926c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
16936c72ffaaSAndy Whitcroft		$res .= $1;
16946c72ffaaSAndy Whitcroft		if ($2 ne '') {
16959c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
16966c72ffaaSAndy Whitcroft			$res .= $coded;
16976c72ffaaSAndy Whitcroft		}
16989c0ca6f9SAndy Whitcroft	}
16999c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
17000a920b5bSAndy Whitcroft
17019c0ca6f9SAndy Whitcroft	return $res;
17020a920b5bSAndy Whitcroft}
17030a920b5bSAndy Whitcroft
1704c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1705cf655043SAndy Whitcroftmy $av_pending;
1706c2fdda0dSAndy Whitcroftmy @av_paren_type;
17071f65f947SAndy Whitcroftmy $av_pend_colon;
1708c2fdda0dSAndy Whitcroft
1709c2fdda0dSAndy Whitcroftsub annotate_reset {
1710c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1711cf655043SAndy Whitcroft	$av_pending = '_';
1712cf655043SAndy Whitcroft	@av_paren_type = ('E');
17131f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1714c2fdda0dSAndy Whitcroft}
1715c2fdda0dSAndy Whitcroft
17166c72ffaaSAndy Whitcroftsub annotate_values {
17176c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
17186c72ffaaSAndy Whitcroft
17196c72ffaaSAndy Whitcroft	my $res;
17201f65f947SAndy Whitcroft	my $var = '_' x length($stream);
17216c72ffaaSAndy Whitcroft	my $cur = $stream;
17226c72ffaaSAndy Whitcroft
1723c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
17246c72ffaaSAndy Whitcroft
17256c72ffaaSAndy Whitcroft	while (length($cur)) {
1726773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1727cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1728171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
17296c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1730c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1731c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1732cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1733c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
17346c72ffaaSAndy Whitcroft			}
17356c72ffaaSAndy Whitcroft
1736c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
17379446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
17389446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1739addcdceaSAndy Whitcroft			$type = 'c';
17409446ef56SAndy Whitcroft
1741e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1742c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
17436c72ffaaSAndy Whitcroft			$type = 'T';
17446c72ffaaSAndy Whitcroft
1745389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1746389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1747389a2fe5SAndy Whitcroft			$type = 'T';
1748389a2fe5SAndy Whitcroft
1749c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1750171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1751c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1752171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1753171ae1a4SAndy Whitcroft			if ($2 ne '') {
1754cf655043SAndy Whitcroft				$av_pending = 'N';
1755171ae1a4SAndy Whitcroft			}
1756171ae1a4SAndy Whitcroft			$type = 'E';
1757171ae1a4SAndy Whitcroft
1758c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1759171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1760171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1761171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
17626c72ffaaSAndy Whitcroft
1763c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1764cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1765c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1766cf655043SAndy Whitcroft
1767cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1768cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1769171ae1a4SAndy Whitcroft			$type = 'E';
1770cf655043SAndy Whitcroft
1771c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1772cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1773cf655043SAndy Whitcroft			$av_preprocessor = 1;
1774cf655043SAndy Whitcroft
1775cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1776cf655043SAndy Whitcroft
1777171ae1a4SAndy Whitcroft			$type = 'E';
1778cf655043SAndy Whitcroft
1779c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1780cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1781cf655043SAndy Whitcroft
1782cf655043SAndy Whitcroft			$av_preprocessor = 1;
1783cf655043SAndy Whitcroft
1784cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1785cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1786cf655043SAndy Whitcroft			pop(@av_paren_type);
1787cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1788171ae1a4SAndy Whitcroft			$type = 'E';
17896c72ffaaSAndy Whitcroft
17906c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1791c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
17926c72ffaaSAndy Whitcroft
1793171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1794171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1795171ae1a4SAndy Whitcroft			$av_pending = $type;
1796171ae1a4SAndy Whitcroft			$type = 'N';
1797171ae1a4SAndy Whitcroft
17986c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1799c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
18006c72ffaaSAndy Whitcroft			if (defined $2) {
1801cf655043SAndy Whitcroft				$av_pending = 'V';
18026c72ffaaSAndy Whitcroft			}
18036c72ffaaSAndy Whitcroft			$type = 'N';
18046c72ffaaSAndy Whitcroft
180514b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1806c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
180714b111c1SAndy Whitcroft			$av_pending = 'E';
18086c72ffaaSAndy Whitcroft			$type = 'N';
18096c72ffaaSAndy Whitcroft
18101f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
18111f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
18121f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18131f65f947SAndy Whitcroft			$type = 'N';
18141f65f947SAndy Whitcroft
181514b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1816c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
18176c72ffaaSAndy Whitcroft			$type = 'N';
18186c72ffaaSAndy Whitcroft
18196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1820c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1821cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1822cf655043SAndy Whitcroft			$av_pending = '_';
18236c72ffaaSAndy Whitcroft			$type = 'N';
18246c72ffaaSAndy Whitcroft
18256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1826cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1827cf655043SAndy Whitcroft			if ($new_type ne '_') {
1828cf655043SAndy Whitcroft				$type = $new_type;
1829c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1830c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
18316c72ffaaSAndy Whitcroft			} else {
1832c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
18336c72ffaaSAndy Whitcroft			}
18346c72ffaaSAndy Whitcroft
1835c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1836c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1837c8cb2ca3SAndy Whitcroft			$type = 'V';
1838cf655043SAndy Whitcroft			$av_pending = 'V';
18396c72ffaaSAndy Whitcroft
18408e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
18418e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
18421f65f947SAndy Whitcroft				$av_pend_colon = 'B';
18438e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
18448e761b04SAndy Whitcroft				$av_pend_colon = 'L';
18451f65f947SAndy Whitcroft			}
18461f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
18471f65f947SAndy Whitcroft			$type = 'V';
18481f65f947SAndy Whitcroft
18496c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1850c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
18516c72ffaaSAndy Whitcroft			$type = 'V';
18526c72ffaaSAndy Whitcroft
18536c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1854c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
18556c72ffaaSAndy Whitcroft			$type = 'N';
18566c72ffaaSAndy Whitcroft
1857cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1858c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
185913214adfSAndy Whitcroft			$type = 'E';
18601f65f947SAndy Whitcroft			$av_pend_colon = 'O';
186113214adfSAndy Whitcroft
18628e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
18638e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
18648e761b04SAndy Whitcroft			$type = 'C';
18658e761b04SAndy Whitcroft
18661f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
18671f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
18681f65f947SAndy Whitcroft			$type = 'N';
18691f65f947SAndy Whitcroft
18701f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
18711f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
18721f65f947SAndy Whitcroft
18731f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
18741f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
18751f65f947SAndy Whitcroft				$type = 'E';
18761f65f947SAndy Whitcroft			} else {
18771f65f947SAndy Whitcroft				$type = 'N';
18781f65f947SAndy Whitcroft			}
18791f65f947SAndy Whitcroft			$av_pend_colon = 'O';
18801f65f947SAndy Whitcroft
18818e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
188213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
18836c72ffaaSAndy Whitcroft			$type = 'N';
18846c72ffaaSAndy Whitcroft
18850d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
188674048ed8SAndy Whitcroft			my $variant;
188774048ed8SAndy Whitcroft
188874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
188974048ed8SAndy Whitcroft			if ($type eq 'V') {
189074048ed8SAndy Whitcroft				$variant = 'B';
189174048ed8SAndy Whitcroft			} else {
189274048ed8SAndy Whitcroft				$variant = 'U';
189374048ed8SAndy Whitcroft			}
189474048ed8SAndy Whitcroft
189574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
189674048ed8SAndy Whitcroft			$type = 'N';
189774048ed8SAndy Whitcroft
18986c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1899c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
19006c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
19016c72ffaaSAndy Whitcroft				$type = 'N';
19026c72ffaaSAndy Whitcroft			}
19036c72ffaaSAndy Whitcroft
19046c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1905c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
19066c72ffaaSAndy Whitcroft		}
19076c72ffaaSAndy Whitcroft		if (defined $1) {
19086c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
19096c72ffaaSAndy Whitcroft			$res .= $type x length($1);
19106c72ffaaSAndy Whitcroft		}
19116c72ffaaSAndy Whitcroft	}
19126c72ffaaSAndy Whitcroft
19131f65f947SAndy Whitcroft	return ($res, $var);
19146c72ffaaSAndy Whitcroft}
19156c72ffaaSAndy Whitcroft
19168905a67cSAndy Whitcroftsub possible {
191713214adfSAndy Whitcroft	my ($possible, $line) = @_;
19189a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
19190776e594SAndy Whitcroft		^(?:
19200776e594SAndy Whitcroft			$Modifier|
19210776e594SAndy Whitcroft			$Storage|
19220776e594SAndy Whitcroft			$Type|
19239a974fdbSAndy Whitcroft			DEFINE_\S+
19249a974fdbSAndy Whitcroft		)$|
19259a974fdbSAndy Whitcroft		^(?:
19260776e594SAndy Whitcroft			goto|
19270776e594SAndy Whitcroft			return|
19280776e594SAndy Whitcroft			case|
19290776e594SAndy Whitcroft			else|
19300776e594SAndy Whitcroft			asm|__asm__|
193189a88353SAndy Whitcroft			do|
193289a88353SAndy Whitcroft			\#|
193389a88353SAndy Whitcroft			\#\#|
19349a974fdbSAndy Whitcroft		)(?:\s|$)|
19350776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
19369a974fdbSAndy Whitcroft	    )}x;
19379a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
19389a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1939c45dcabdSAndy Whitcroft		# Check for modifiers.
1940c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1941c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1942c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1943c45dcabdSAndy Whitcroft
1944c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1945c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1946d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
19479a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1948d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1949485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1950d2506586SAndy Whitcroft				}
19519a974fdbSAndy Whitcroft			}
1952c45dcabdSAndy Whitcroft
1953c45dcabdSAndy Whitcroft		} else {
195413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1955485ff23eSAlex Dowad			push(@typeListFile, $possible);
1956c45dcabdSAndy Whitcroft		}
19578905a67cSAndy Whitcroft		build_types();
19580776e594SAndy Whitcroft	} else {
19590776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
19608905a67cSAndy Whitcroft	}
19618905a67cSAndy Whitcroft}
19628905a67cSAndy Whitcroft
19636c72ffaaSAndy Whitcroftmy $prefix = '';
19646c72ffaaSAndy Whitcroft
1965000d1cc1SJoe Perchessub show_type {
1966cbec18afSJoe Perches	my ($type) = @_;
196791bfe484SJoe Perches
1968522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
1969522b837cSAlexey Dobriyan
1970cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1971cbec18afSJoe Perches
1972cbec18afSJoe Perches	return !defined $ignore_type{$type};
1973000d1cc1SJoe Perches}
1974000d1cc1SJoe Perches
1975f0a594c1SAndy Whitcroftsub report {
1976cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1977cbec18afSJoe Perches
1978cbec18afSJoe Perches	if (!show_type($type) ||
1979cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1980773647a0SAndy Whitcroft		return 0;
1981773647a0SAndy Whitcroft	}
198257230297SJoe Perches	my $output = '';
1983737c0767SJohn Brooks	if ($color) {
198457230297SJoe Perches		if ($level eq 'ERROR') {
198557230297SJoe Perches			$output .= RED;
198657230297SJoe Perches		} elsif ($level eq 'WARNING') {
198757230297SJoe Perches			$output .= YELLOW;
1988000d1cc1SJoe Perches		} else {
198957230297SJoe Perches			$output .= GREEN;
1990000d1cc1SJoe Perches		}
199157230297SJoe Perches	}
199257230297SJoe Perches	$output .= $prefix . $level . ':';
199357230297SJoe Perches	if ($show_types) {
1994737c0767SJohn Brooks		$output .= BLUE if ($color);
199557230297SJoe Perches		$output .= "$type:";
199657230297SJoe Perches	}
1997737c0767SJohn Brooks	$output .= RESET if ($color);
199857230297SJoe Perches	$output .= ' ' . $msg . "\n";
199934d8815fSJoe Perches
200034d8815fSJoe Perches	if ($showfile) {
200134d8815fSJoe Perches		my @lines = split("\n", $output, -1);
200234d8815fSJoe Perches		splice(@lines, 1, 1);
200334d8815fSJoe Perches		$output = join("\n", @lines);
200434d8815fSJoe Perches	}
200557230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
20068905a67cSAndy Whitcroft
200757230297SJoe Perches	push(our @report, $output);
2008773647a0SAndy Whitcroft
2009773647a0SAndy Whitcroft	return 1;
2010f0a594c1SAndy Whitcroft}
2011cbec18afSJoe Perches
2012f0a594c1SAndy Whitcroftsub report_dump {
201313214adfSAndy Whitcroft	our @report;
2014f0a594c1SAndy Whitcroft}
2015000d1cc1SJoe Perches
2016d752fcc8SJoe Perchessub fixup_current_range {
2017d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2018d752fcc8SJoe Perches
2019d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2020d752fcc8SJoe Perches		my $o = $1;
2021d752fcc8SJoe Perches		my $l = $2;
2022d752fcc8SJoe Perches		my $no = $o + $offset;
2023d752fcc8SJoe Perches		my $nl = $l + $length;
2024d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2025d752fcc8SJoe Perches	}
2026d752fcc8SJoe Perches}
2027d752fcc8SJoe Perches
2028d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2029d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2030d752fcc8SJoe Perches
2031d752fcc8SJoe Perches	my $range_last_linenr = 0;
2032d752fcc8SJoe Perches	my $delta_offset = 0;
2033d752fcc8SJoe Perches
2034d752fcc8SJoe Perches	my $old_linenr = 0;
2035d752fcc8SJoe Perches	my $new_linenr = 0;
2036d752fcc8SJoe Perches
2037d752fcc8SJoe Perches	my $next_insert = 0;
2038d752fcc8SJoe Perches	my $next_delete = 0;
2039d752fcc8SJoe Perches
2040d752fcc8SJoe Perches	my @lines = ();
2041d752fcc8SJoe Perches
2042d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2043d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2044d752fcc8SJoe Perches
2045d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2046d752fcc8SJoe Perches		my $save_line = 1;
2047d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2048323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2049d752fcc8SJoe Perches			$delta_offset = 0;
2050d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2051d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2052d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2053d752fcc8SJoe Perches		}
2054d752fcc8SJoe Perches
2055d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2056d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2057d752fcc8SJoe Perches			$save_line = 0;
2058d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2059d752fcc8SJoe Perches		}
2060d752fcc8SJoe Perches
2061d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2062d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2063d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2064d752fcc8SJoe Perches			$new_linenr++;
2065d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2066d752fcc8SJoe Perches		}
2067d752fcc8SJoe Perches
2068d752fcc8SJoe Perches		if ($save_line) {
2069d752fcc8SJoe Perches			push(@lines, $line);
2070d752fcc8SJoe Perches			$new_linenr++;
2071d752fcc8SJoe Perches		}
2072d752fcc8SJoe Perches
2073d752fcc8SJoe Perches		$old_linenr++;
2074d752fcc8SJoe Perches	}
2075d752fcc8SJoe Perches
2076d752fcc8SJoe Perches	return @lines;
2077d752fcc8SJoe Perches}
2078d752fcc8SJoe Perches
2079f2d7e4d4SJoe Perchessub fix_insert_line {
2080f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2081f2d7e4d4SJoe Perches
2082f2d7e4d4SJoe Perches	my $inserted = {
2083f2d7e4d4SJoe Perches		LINENR => $linenr,
2084f2d7e4d4SJoe Perches		LINE => $line,
2085f2d7e4d4SJoe Perches	};
2086f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2087f2d7e4d4SJoe Perches}
2088f2d7e4d4SJoe Perches
2089f2d7e4d4SJoe Perchessub fix_delete_line {
2090f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2091f2d7e4d4SJoe Perches
2092f2d7e4d4SJoe Perches	my $deleted = {
2093f2d7e4d4SJoe Perches		LINENR => $linenr,
2094f2d7e4d4SJoe Perches		LINE => $line,
2095f2d7e4d4SJoe Perches	};
2096f2d7e4d4SJoe Perches
2097f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2098f2d7e4d4SJoe Perches}
2099f2d7e4d4SJoe Perches
2100de7d4f0eSAndy Whitcroftsub ERROR {
2101cbec18afSJoe Perches	my ($type, $msg) = @_;
2102cbec18afSJoe Perches
2103cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2104de7d4f0eSAndy Whitcroft		our $clean = 0;
21056c72ffaaSAndy Whitcroft		our $cnt_error++;
21063705ce5bSJoe Perches		return 1;
2107de7d4f0eSAndy Whitcroft	}
21083705ce5bSJoe Perches	return 0;
2109773647a0SAndy Whitcroft}
2110de7d4f0eSAndy Whitcroftsub WARN {
2111cbec18afSJoe Perches	my ($type, $msg) = @_;
2112cbec18afSJoe Perches
2113cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2114de7d4f0eSAndy Whitcroft		our $clean = 0;
21156c72ffaaSAndy Whitcroft		our $cnt_warn++;
21163705ce5bSJoe Perches		return 1;
2117de7d4f0eSAndy Whitcroft	}
21183705ce5bSJoe Perches	return 0;
2119773647a0SAndy Whitcroft}
2120de7d4f0eSAndy Whitcroftsub CHK {
2121cbec18afSJoe Perches	my ($type, $msg) = @_;
2122cbec18afSJoe Perches
2123cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2124de7d4f0eSAndy Whitcroft		our $clean = 0;
21256c72ffaaSAndy Whitcroft		our $cnt_chk++;
21263705ce5bSJoe Perches		return 1;
21276c72ffaaSAndy Whitcroft	}
21283705ce5bSJoe Perches	return 0;
2129de7d4f0eSAndy Whitcroft}
2130de7d4f0eSAndy Whitcroft
21316ecd9674SAndy Whitcroftsub check_absolute_file {
21326ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
21336ecd9674SAndy Whitcroft	my $file = $absolute;
21346ecd9674SAndy Whitcroft
21356ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
21366ecd9674SAndy Whitcroft
21376ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
21386ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
21396ecd9674SAndy Whitcroft		if (-f "$root/$file") {
21406ecd9674SAndy Whitcroft			##print "file<$file>\n";
21416ecd9674SAndy Whitcroft			last;
21426ecd9674SAndy Whitcroft		}
21436ecd9674SAndy Whitcroft	}
21446ecd9674SAndy Whitcroft	if (! -f _)  {
21456ecd9674SAndy Whitcroft		return 0;
21466ecd9674SAndy Whitcroft	}
21476ecd9674SAndy Whitcroft
21486ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
21496ecd9674SAndy Whitcroft	my $prefix = $absolute;
21506ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
21516ecd9674SAndy Whitcroft
21526ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
21536ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2154000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2155000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
21566ecd9674SAndy Whitcroft	}
21576ecd9674SAndy Whitcroft}
21586ecd9674SAndy Whitcroft
21593705ce5bSJoe Perchessub trim {
21603705ce5bSJoe Perches	my ($string) = @_;
21613705ce5bSJoe Perches
2162b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2163b34c648bSJoe Perches
2164b34c648bSJoe Perches	return $string;
2165b34c648bSJoe Perches}
2166b34c648bSJoe Perches
2167b34c648bSJoe Perchessub ltrim {
2168b34c648bSJoe Perches	my ($string) = @_;
2169b34c648bSJoe Perches
2170b34c648bSJoe Perches	$string =~ s/^\s+//;
2171b34c648bSJoe Perches
2172b34c648bSJoe Perches	return $string;
2173b34c648bSJoe Perches}
2174b34c648bSJoe Perches
2175b34c648bSJoe Perchessub rtrim {
2176b34c648bSJoe Perches	my ($string) = @_;
2177b34c648bSJoe Perches
2178b34c648bSJoe Perches	$string =~ s/\s+$//;
21793705ce5bSJoe Perches
21803705ce5bSJoe Perches	return $string;
21813705ce5bSJoe Perches}
21823705ce5bSJoe Perches
218352ea8506SJoe Perchessub string_find_replace {
218452ea8506SJoe Perches	my ($string, $find, $replace) = @_;
218552ea8506SJoe Perches
218652ea8506SJoe Perches	$string =~ s/$find/$replace/g;
218752ea8506SJoe Perches
218852ea8506SJoe Perches	return $string;
218952ea8506SJoe Perches}
219052ea8506SJoe Perches
21913705ce5bSJoe Perchessub tabify {
21923705ce5bSJoe Perches	my ($leading) = @_;
21933705ce5bSJoe Perches
21943705ce5bSJoe Perches	my $source_indent = 8;
21953705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
21963705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
21973705ce5bSJoe Perches
21983705ce5bSJoe Perches	#convert leading spaces to tabs
21993705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
22003705ce5bSJoe Perches	#Remove spaces before a tab
22013705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
22023705ce5bSJoe Perches
22033705ce5bSJoe Perches	return "$leading";
22043705ce5bSJoe Perches}
22053705ce5bSJoe Perches
2206d1fe9c09SJoe Perchessub pos_last_openparen {
2207d1fe9c09SJoe Perches	my ($line) = @_;
2208d1fe9c09SJoe Perches
2209d1fe9c09SJoe Perches	my $pos = 0;
2210d1fe9c09SJoe Perches
2211d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2212d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2213d1fe9c09SJoe Perches
2214d1fe9c09SJoe Perches	my $last_openparen = 0;
2215d1fe9c09SJoe Perches
2216d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2217d1fe9c09SJoe Perches		return -1;
2218d1fe9c09SJoe Perches	}
2219d1fe9c09SJoe Perches
2220d1fe9c09SJoe Perches	my $len = length($line);
2221d1fe9c09SJoe Perches
2222d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2223d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2224d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2225d1fe9c09SJoe Perches			$pos += length($1) - 1;
2226d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2227d1fe9c09SJoe Perches			$last_openparen = $pos;
2228d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2229d1fe9c09SJoe Perches			last;
2230d1fe9c09SJoe Perches		}
2231d1fe9c09SJoe Perches	}
2232d1fe9c09SJoe Perches
223391cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2234d1fe9c09SJoe Perches}
2235d1fe9c09SJoe Perches
22360a920b5bSAndy Whitcroftsub process {
22370a920b5bSAndy Whitcroft	my $filename = shift;
22380a920b5bSAndy Whitcroft
22390a920b5bSAndy Whitcroft	my $linenr=0;
22400a920b5bSAndy Whitcroft	my $prevline="";
2241c2fdda0dSAndy Whitcroft	my $prevrawline="";
22420a920b5bSAndy Whitcroft	my $stashline="";
2243c2fdda0dSAndy Whitcroft	my $stashrawline="";
22440a920b5bSAndy Whitcroft
22454a0df2efSAndy Whitcroft	my $length;
22460a920b5bSAndy Whitcroft	my $indent;
22470a920b5bSAndy Whitcroft	my $previndent=0;
22480a920b5bSAndy Whitcroft	my $stashindent=0;
22490a920b5bSAndy Whitcroft
2250de7d4f0eSAndy Whitcroft	our $clean = 1;
22510a920b5bSAndy Whitcroft	my $signoff = 0;
2252cd261496SGeert Uytterhoeven	my $author = '';
2253cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
22540a920b5bSAndy Whitcroft	my $is_patch = 0;
2255133712a2SRob Herring	my $is_binding_patch = -1;
225629ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
225715662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2258ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2259490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2260bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
22612a076f40SJoe Perches	my $commit_log_long_line = 0;
2262e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
226313f1937eSJoe Perches	my $reported_maintainer_file = 0;
2264fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2265fa64205dSPasi Savanainen
2266365dd4eaSJoe Perches	my $last_blank_line = 0;
22675e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2268365dd4eaSJoe Perches
226913214adfSAndy Whitcroft	our @report = ();
22706c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
22716c72ffaaSAndy Whitcroft	our $cnt_error = 0;
22726c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
22736c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
22746c72ffaaSAndy Whitcroft
22750a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
22760a920b5bSAndy Whitcroft	my $realfile = '';
22770a920b5bSAndy Whitcroft	my $realline = 0;
22780a920b5bSAndy Whitcroft	my $realcnt = 0;
22790a920b5bSAndy Whitcroft	my $here = '';
228077cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
22810a920b5bSAndy Whitcroft	my $in_comment = 0;
2282c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
22830a920b5bSAndy Whitcroft	my $first_line = 0;
22841e855726SWolfram Sang	my $p1_prefix = '';
22850a920b5bSAndy Whitcroft
228613214adfSAndy Whitcroft	my $prev_values = 'E';
228713214adfSAndy Whitcroft
228813214adfSAndy Whitcroft	# suppression flags
2289773647a0SAndy Whitcroft	my %suppress_ifbraces;
2290170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
22912b474a1aSAndy Whitcroft	my %suppress_export;
22923e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2293653d4876SAndy Whitcroft
22947e51f197SJoe Perches	my %signatures = ();
2295323c1260SJoe Perches
2296c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2297de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2298c2fdda0dSAndy Whitcroft	#
2299de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2300de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2301773647a0SAndy Whitcroft
2302d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2303d8b07710SJoe Perches
23049f3a8992SRob Herring	my $checklicenseline = 1;
23059f3a8992SRob Herring
2306773647a0SAndy Whitcroft	sanitise_line_reset();
2307c2fdda0dSAndy Whitcroft	my $line;
2308c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2309773647a0SAndy Whitcroft		$linenr++;
2310773647a0SAndy Whitcroft		$line = $rawline;
2311c2fdda0dSAndy Whitcroft
23123705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
23133705ce5bSJoe Perches
2314773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2315de7d4f0eSAndy Whitcroft			$setup_docs = 0;
23168c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2317de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2318de7d4f0eSAndy Whitcroft			}
2319773647a0SAndy Whitcroft			#next;
2320de7d4f0eSAndy Whitcroft		}
232174fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2322773647a0SAndy Whitcroft			$realline=$1-1;
2323773647a0SAndy Whitcroft			if (defined $2) {
2324773647a0SAndy Whitcroft				$realcnt=$3+1;
2325773647a0SAndy Whitcroft			} else {
2326773647a0SAndy Whitcroft				$realcnt=1+1;
2327773647a0SAndy Whitcroft			}
2328c45dcabdSAndy Whitcroft			$in_comment = 0;
2329773647a0SAndy Whitcroft
2330773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2331773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2332773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2333773647a0SAndy Whitcroft			# at context start.
2334773647a0SAndy Whitcroft			my $edge;
233501fa9147SAndy Whitcroft			my $cnt = $realcnt;
233601fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
233701fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
233801fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
233901fa9147SAndy Whitcroft				$cnt--;
234001fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2341721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2342fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2343fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2344fae17daeSAndy Whitcroft					($edge) = $1;
2345fae17daeSAndy Whitcroft					last;
2346fae17daeSAndy Whitcroft				}
2347773647a0SAndy Whitcroft			}
2348773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2349773647a0SAndy Whitcroft				$in_comment = 1;
2350773647a0SAndy Whitcroft			}
2351773647a0SAndy Whitcroft
2352773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2353773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2354773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2355773647a0SAndy Whitcroft			if (!defined $edge &&
235683242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2357773647a0SAndy Whitcroft			{
2358773647a0SAndy Whitcroft				$in_comment = 1;
2359773647a0SAndy Whitcroft			}
2360773647a0SAndy Whitcroft
2361773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2362773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2363773647a0SAndy Whitcroft
2364171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2365773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2366171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2367773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2368773647a0SAndy Whitcroft		}
2369773647a0SAndy Whitcroft		push(@lines, $line);
2370773647a0SAndy Whitcroft
2371773647a0SAndy Whitcroft		if ($realcnt > 1) {
2372773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2373773647a0SAndy Whitcroft		} else {
2374773647a0SAndy Whitcroft			$realcnt = 0;
2375773647a0SAndy Whitcroft		}
2376773647a0SAndy Whitcroft
2377773647a0SAndy Whitcroft		#print "==>$rawline\n";
2378773647a0SAndy Whitcroft		#print "-->$line\n";
2379de7d4f0eSAndy Whitcroft
2380de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2381de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2382de7d4f0eSAndy Whitcroft		}
2383de7d4f0eSAndy Whitcroft	}
2384de7d4f0eSAndy Whitcroft
23856c72ffaaSAndy Whitcroft	$prefix = '';
23866c72ffaaSAndy Whitcroft
2387773647a0SAndy Whitcroft	$realcnt = 0;
2388773647a0SAndy Whitcroft	$linenr = 0;
2389194f66fcSJoe Perches	$fixlinenr = -1;
23900a920b5bSAndy Whitcroft	foreach my $line (@lines) {
23910a920b5bSAndy Whitcroft		$linenr++;
2392194f66fcSJoe Perches		$fixlinenr++;
23931b5539b1SJoe Perches		my $sline = $line;	#copy of $line
23941b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
23950a920b5bSAndy Whitcroft
2396c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
23976c72ffaaSAndy Whitcroft
239812c253abSJoe Perches# check if it's a mode change, rename or start of a patch
239912c253abSJoe Perches		if (!$in_commit_log &&
240012c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
240112c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
240212c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
240312c253abSJoe Perches			$is_patch = 1;
240412c253abSJoe Perches		}
240512c253abSJoe Perches
24060a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2407e518e9a5SJoe Perches		if (!$in_commit_log &&
240874fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
240974fd4f34SJoe Perches			my $context = $4;
24100a920b5bSAndy Whitcroft			$is_patch = 1;
24114a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
24120a920b5bSAndy Whitcroft			$realline=$1-1;
24130a920b5bSAndy Whitcroft			if (defined $2) {
24140a920b5bSAndy Whitcroft				$realcnt=$3+1;
24150a920b5bSAndy Whitcroft			} else {
24160a920b5bSAndy Whitcroft				$realcnt=1+1;
24170a920b5bSAndy Whitcroft			}
2418c2fdda0dSAndy Whitcroft			annotate_reset();
241913214adfSAndy Whitcroft			$prev_values = 'E';
242013214adfSAndy Whitcroft
2421773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2422170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
24232b474a1aSAndy Whitcroft			%suppress_export = ();
24243e469cdcSAndy Whitcroft			$suppress_statement = 0;
242574fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
242674fd4f34SJoe Perches				$context_function = $1;
242774fd4f34SJoe Perches			} else {
242874fd4f34SJoe Perches				undef $context_function;
242974fd4f34SJoe Perches			}
24300a920b5bSAndy Whitcroft			next;
24310a920b5bSAndy Whitcroft
24324a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
24334a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
24344a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2435773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
24360a920b5bSAndy Whitcroft			$realline++;
2437d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
24380a920b5bSAndy Whitcroft
24394a0df2efSAndy Whitcroft			# Measure the line length and indent.
2440c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
24410a920b5bSAndy Whitcroft
24420a920b5bSAndy Whitcroft			# Track the previous line.
24430a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
24440a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2445c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2446c2fdda0dSAndy Whitcroft
2447773647a0SAndy Whitcroft			#warn "line<$line>\n";
24486c72ffaaSAndy Whitcroft
2449d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2450d8aaf121SAndy Whitcroft			$realcnt--;
24510a920b5bSAndy Whitcroft		}
24520a920b5bSAndy Whitcroft
2453cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2454cc77cdcaSAndy Whitcroft
24556c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
24566c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2457773647a0SAndy Whitcroft
24582ac73b4fSJoe Perches		my $found_file = 0;
2459773647a0SAndy Whitcroft		# extract the filename as it passes
24603bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
24613bf9a009SRabin Vincent			$realfile = $1;
24622b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2463270c49a0SJoe Perches			$in_commit_log = 0;
24642ac73b4fSJoe Perches			$found_file = 1;
24653bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2466773647a0SAndy Whitcroft			$realfile = $1;
24672b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2468270c49a0SJoe Perches			$in_commit_log = 0;
24691e855726SWolfram Sang
24701e855726SWolfram Sang			$p1_prefix = $1;
2471e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2472e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2473000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2474000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
24751e855726SWolfram Sang			}
2476773647a0SAndy Whitcroft
2477c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2478000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2479000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2480773647a0SAndy Whitcroft			}
24812ac73b4fSJoe Perches			$found_file = 1;
24822ac73b4fSJoe Perches		}
24832ac73b4fSJoe Perches
248434d8815fSJoe Perches#make up the handle for any error we report on this line
248534d8815fSJoe Perches		if ($showfile) {
248634d8815fSJoe Perches			$prefix = "$realfile:$realline: "
248734d8815fSJoe Perches		} elsif ($emacs) {
24887d3a9f67SJoe Perches			if ($file) {
24897d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
24907d3a9f67SJoe Perches			} else {
249134d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
249234d8815fSJoe Perches			}
24937d3a9f67SJoe Perches		}
249434d8815fSJoe Perches
24952ac73b4fSJoe Perches		if ($found_file) {
249685b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
249785b0ee18SJoe Perches				WARN("OBSOLETE",
249885b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
249985b0ee18SJoe Perches			}
25007bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25012ac73b4fSJoe Perches				$check = 1;
25022ac73b4fSJoe Perches			} else {
25032ac73b4fSJoe Perches				$check = $check_orig;
25042ac73b4fSJoe Perches			}
25059f3a8992SRob Herring			$checklicenseline = 1;
2506133712a2SRob Herring
2507133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2508133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2509133712a2SRob Herring
2510133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2511133712a2SRob Herring
2512133712a2SRob Herring				if (($last_binding_patch != -1) &&
2513133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2514133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2515133712a2SRob Herring					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2516133712a2SRob Herring				}
2517133712a2SRob Herring			}
2518133712a2SRob Herring
2519773647a0SAndy Whitcroft			next;
2520773647a0SAndy Whitcroft		}
2521773647a0SAndy Whitcroft
2522389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
25230a920b5bSAndy Whitcroft
2524c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2525c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2526c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
25270a920b5bSAndy Whitcroft
25286c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
25296c72ffaaSAndy Whitcroft
2530490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2531490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2532490b292cSJoe Perches		if ($in_commit_log) {
2533490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2534490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2535490b292cSJoe Perches			}
2536490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2537490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2538490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2539490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2540490b292cSJoe Perches		}
2541490b292cSJoe Perches
2542e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2543e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2544e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2545e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2546e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2547e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2548e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2549e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2550e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2551e518e9a5SJoe Perches		}
2552e518e9a5SJoe Perches
25533bf9a009SRabin Vincent# Check for incorrect file permissions
25543bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
25553bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
255604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
255704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2558000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2559000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
25603bf9a009SRabin Vincent			}
25613bf9a009SRabin Vincent		}
25623bf9a009SRabin Vincent
2563cd261496SGeert Uytterhoeven# Check the patch for a From:
2564cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2565cd261496SGeert Uytterhoeven			$author = $1;
2566cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2567cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2568cd261496SGeert Uytterhoeven		}
2569cd261496SGeert Uytterhoeven
257020112475SJoe Perches# Check the patch for a signoff:
2571d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
25724a0df2efSAndy Whitcroft			$signoff++;
257315662b3eSJoe Perches			$in_commit_log = 0;
2574cd261496SGeert Uytterhoeven			if ($author ne '') {
2575cd261496SGeert Uytterhoeven				my $l = $line;
2576cd261496SGeert Uytterhoeven				$l =~ s/"//g;
2577cd261496SGeert Uytterhoeven				if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2578cd261496SGeert Uytterhoeven				    $authorsignoff = 1;
2579cd261496SGeert Uytterhoeven				}
2580cd261496SGeert Uytterhoeven			}
25810a920b5bSAndy Whitcroft		}
258220112475SJoe Perches
2583e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2584e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2585e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2586e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2587e0d975b1SJoe Perches		}
2588e0d975b1SJoe Perches
258920112475SJoe Perches# Check signature styles
2590270c49a0SJoe Perches		if (!$in_header_lines &&
2591ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
259220112475SJoe Perches			my $space_before = $1;
259320112475SJoe Perches			my $sign_off = $2;
259420112475SJoe Perches			my $space_after = $3;
259520112475SJoe Perches			my $email = $4;
259620112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
259720112475SJoe Perches
2598ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2599ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2600ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2601ce0338dfSJoe Perches			}
260220112475SJoe Perches			if (defined $space_before && $space_before ne "") {
26033705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26043705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
26053705ce5bSJoe Perches				    $fix) {
2606194f66fcSJoe Perches					$fixed[$fixlinenr] =
26073705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26083705ce5bSJoe Perches				}
260920112475SJoe Perches			}
261020112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
26113705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26123705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
26133705ce5bSJoe Perches				    $fix) {
2614194f66fcSJoe Perches					$fixed[$fixlinenr] =
26153705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26163705ce5bSJoe Perches				}
26173705ce5bSJoe Perches
261820112475SJoe Perches			}
261920112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
26203705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26213705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
26223705ce5bSJoe Perches				    $fix) {
2623194f66fcSJoe Perches					$fixed[$fixlinenr] =
26243705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26253705ce5bSJoe Perches				}
262620112475SJoe Perches			}
262720112475SJoe Perches
262820112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
262920112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
263020112475SJoe Perches			if ($suggested_email eq "") {
2631000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2632000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
263320112475SJoe Perches			} else {
263420112475SJoe Perches				my $dequoted = $suggested_email;
263520112475SJoe Perches				$dequoted =~ s/^"//;
263620112475SJoe Perches				$dequoted =~ s/" </ </;
263720112475SJoe Perches				# Don't force email to have quotes
263820112475SJoe Perches				# Allow just an angle bracketed address
263920112475SJoe Perches				if ("$dequoted$comment" ne $email &&
264020112475SJoe Perches				    "<$email_address>$comment" ne $email &&
264120112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2642000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2643000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
264420112475SJoe Perches				}
26450a920b5bSAndy Whitcroft			}
26467e51f197SJoe Perches
26477e51f197SJoe Perches# Check for duplicate signatures
26487e51f197SJoe Perches			my $sig_nospace = $line;
26497e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
26507e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
26517e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
26527e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
26537e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
26547e51f197SJoe Perches			} else {
26557e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
26567e51f197SJoe Perches			}
26570a920b5bSAndy Whitcroft		}
26580a920b5bSAndy Whitcroft
2659a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2660a2fe16b9SJoe Perches		if ($in_header_lines &&
2661a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2662a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2663a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2664a2fe16b9SJoe Perches		}
2665a2fe16b9SJoe Perches
26667ebd05efSChristopher Covington# Check for unwanted Gerrit info
26677ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
26687ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
26697ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
26707ebd05efSChristopher Covington		}
26717ebd05efSChristopher Covington
2672369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2673369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2674369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2675369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2676369c8dd3SJoe Perches					# timestamp
2677369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2678369c8dd3SJoe Perches					# stack dump address
2679369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2680369c8dd3SJoe Perches		}
2681369c8dd3SJoe Perches
26822a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
26832a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2684bf4daf12SJoe Perches		    length($line) > 75 &&
2685bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2686bf4daf12SJoe Perches					# file delta changes
2687bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2688bf4daf12SJoe Perches					# filename then :
2689bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2690bf4daf12SJoe Perches					# A Fixes: or Link: line
2691bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
26922a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
26932a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
26942a076f40SJoe Perches			$commit_log_long_line = 1;
26952a076f40SJoe Perches		}
26962a076f40SJoe Perches
2697bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2698bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2699bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2700bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2701bf4daf12SJoe Perches		}
2702bf4daf12SJoe Perches
27030d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2704369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2705aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2706e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2707fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2708aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2709369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2710bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2711fe043ea1SJoe Perches			my $init_char = "c";
2712fe043ea1SJoe Perches			my $orig_commit = "";
27130d7835fcSJoe Perches			my $short = 1;
27140d7835fcSJoe Perches			my $long = 0;
27150d7835fcSJoe Perches			my $case = 1;
27160d7835fcSJoe Perches			my $space = 1;
27170d7835fcSJoe Perches			my $hasdesc = 0;
271819c146a6SJoe Perches			my $hasparens = 0;
27190d7835fcSJoe Perches			my $id = '0123456789ab';
27200d7835fcSJoe Perches			my $orig_desc = "commit description";
27210d7835fcSJoe Perches			my $description = "";
27220d7835fcSJoe Perches
2723fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2724fe043ea1SJoe Perches				$init_char = $1;
2725fe043ea1SJoe Perches				$orig_commit = lc($2);
2726fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2727fe043ea1SJoe Perches				$orig_commit = lc($1);
2728fe043ea1SJoe Perches			}
2729fe043ea1SJoe Perches
27300d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
27310d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
27320d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
27330d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
27340d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
27350d7835fcSJoe Perches				$orig_desc = $1;
273619c146a6SJoe Perches				$hasparens = 1;
27370d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
27380d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
27390d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
27400d7835fcSJoe Perches				$orig_desc = $1;
274119c146a6SJoe Perches				$hasparens = 1;
2742b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2743b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2744b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2745b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2746b671fde0SJoe Perches				$orig_desc = $1;
2747b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2748b671fde0SJoe Perches				$orig_desc .= " " . $1;
274919c146a6SJoe Perches				$hasparens = 1;
27500d7835fcSJoe Perches			}
27510d7835fcSJoe Perches
27520d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
27530d7835fcSJoe Perches							      $id, $orig_desc);
27540d7835fcSJoe Perches
2755948b133aSHeinrich Schuchardt			if (defined($id) &&
2756948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2757d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
27580d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
27590d7835fcSJoe Perches			}
2760d311cd44SJoe Perches		}
2761d311cd44SJoe Perches
276213f1937eSJoe Perches# Check for added, moved or deleted files
276313f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
276413f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
276513f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
276613f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
276713f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2768a82603a8SAndrew Jeffery			$is_patch = 1;
276913f1937eSJoe Perches			$reported_maintainer_file = 1;
277013f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
277113f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
277213f1937eSJoe Perches		}
277313f1937eSJoe Perches
277400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
27758905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2776000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2777000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
27786c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2779de7d4f0eSAndy Whitcroft		}
2780de7d4f0eSAndy Whitcroft
2781de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2782de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2783171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2784171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2785171ae1a4SAndy Whitcroft
2786171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2787171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2788171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2789171ae1a4SAndy Whitcroft
279034d99219SJoe Perches			CHK("INVALID_UTF8",
2791000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
279200df344fSAndy Whitcroft		}
27930a920b5bSAndy Whitcroft
279415662b3eSJoe Perches# Check if it's the start of a commit log
279515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
279615662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2797eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2798eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
279915662b3eSJoe Perches			$in_header_lines = 0;
280015662b3eSJoe Perches			$in_commit_log = 1;
2801ed43c4e5SAllen Hubbe			$has_commit_log = 1;
280215662b3eSJoe Perches		}
280315662b3eSJoe Perches
2804fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2805fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2806fa64205dSPasi Savanainen		if ($in_header_lines &&
2807fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2808fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2809fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2810fa64205dSPasi Savanainen		}
2811fa64205dSPasi Savanainen
2812fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
281315662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2814fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
281515662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
281615662b3eSJoe Perches		}
281715662b3eSJoe Perches
2818d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2819d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2820d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2821d6430f71SJoe Perches				my $file = $1;
2822d6430f71SJoe Perches
2823d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2824d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2825d6430f71SJoe Perches					#
2826d6430f71SJoe Perches				} else {
2827d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2828d6430f71SJoe Perches				}
2829d6430f71SJoe Perches			}
2830d6430f71SJoe Perches		}
2831d6430f71SJoe Perches
283266b47b4aSKees Cook# Check for various typo / spelling mistakes
283366d7a382SJoe Perches		if (defined($misspellings) &&
283466d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2835ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
283666b47b4aSKees Cook				my $typo = $1;
283766b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
283866b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
283966b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
28400675a8fbSJean Delvare				my $msg_level = \&WARN;
28410675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
28420675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
284366b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
284466b47b4aSKees Cook				    $fix) {
284566b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
284666b47b4aSKees Cook				}
284766b47b4aSKees Cook			}
284866b47b4aSKees Cook		}
284966b47b4aSKees Cook
285030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
285130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
285200df344fSAndy Whitcroft
28530a920b5bSAndy Whitcroft#trailing whitespace
28549c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2855c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2856d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2857d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2858d5e616fcSJoe Perches			    $fix) {
2859194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2860d5e616fcSJoe Perches			}
2861c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2862c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28633705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
28643705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
28653705ce5bSJoe Perches			    $fix) {
2866194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
28673705ce5bSJoe Perches			}
28683705ce5bSJoe Perches
2869d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
28700a920b5bSAndy Whitcroft		}
28715368df20SAndy Whitcroft
28724783f894SJosh Triplett# Check for FSF mailing addresses.
2873109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
28741bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
28753e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
28763e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
28774783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28780675a8fbSJean Delvare			my $msg_level = \&ERROR;
28790675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
28800675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
28814783f894SJosh 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)
28824783f894SJosh Triplett		}
28834783f894SJosh Triplett
28843354957aSAndi Kleen# check for Kconfig help text having a real description
28859fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
28869fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
28873354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2888678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
2889678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
2890678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
2891678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
28923354957aSAndi Kleen			my $length = 0;
28939fe287d7SAndy Whitcroft			my $cnt = $realcnt;
28949fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
28959fe287d7SAndy Whitcroft			my $f;
2896a1385803SAndy Whitcroft			my $is_start = 0;
28979fe287d7SAndy Whitcroft			my $is_end = 0;
2898a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
28999fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
29009fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
29019fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
29029fe287d7SAndy Whitcroft
29039fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
29048d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2905a1385803SAndy Whitcroft
290686adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
2907a1385803SAndy Whitcroft					$is_start = 1;
290884af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
290984af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
291084af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
291184af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
291284af7a61SUlf Magnusson					}
2913a1385803SAndy Whitcroft					$length = -1;
2914a1385803SAndy Whitcroft				}
2915a1385803SAndy Whitcroft
29169fe287d7SAndy Whitcroft				$f =~ s/^.//;
29173354957aSAndi Kleen				$f =~ s/#.*//;
29183354957aSAndi Kleen				$f =~ s/^\s+//;
29193354957aSAndi Kleen				next if ($f =~ /^$/);
2920678ae162SUlf Magnusson
2921678ae162SUlf Magnusson				# This only checks context lines in the patch
2922678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
2923678ae162SUlf Magnusson				# positives, even though some of these are
2924678ae162SUlf Magnusson				# common words in help texts
2925678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2926678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
29279fe287d7SAndy Whitcroft					$is_end = 1;
29289fe287d7SAndy Whitcroft					last;
29299fe287d7SAndy Whitcroft				}
29303354957aSAndi Kleen				$length++;
29313354957aSAndi Kleen			}
293256193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2933000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
293456193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
293556193274SVadim Bendebury			}
2936a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
29373354957aSAndi Kleen		}
29383354957aSAndi Kleen
2939628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
2940628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
2941628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
2942628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
2943628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
2944628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2945628f91a2SJoe Perches			    $fix) {
2946628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2947628f91a2SJoe Perches			}
2948628f91a2SJoe Perches		}
2949628f91a2SJoe Perches
2950327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2951327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2952327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2953327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2954327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2955327953e9SChristoph Jaeger		}
2956327953e9SChristoph Jaeger
2957c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2958c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2959c68e5878SArnaud Lacombe			my $flag = $1;
2960c68e5878SArnaud Lacombe			my $replacement = {
2961c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2962c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2963c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2964c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2965c68e5878SArnaud Lacombe			};
2966c68e5878SArnaud Lacombe
2967c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2968c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2969c68e5878SArnaud Lacombe		}
2970c68e5878SArnaud Lacombe
2971bff5da43SRob Herring# check for DT compatible documentation
29727dd05b38SFlorian Vaussard		if (defined $root &&
29737dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
29747dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
29757dd05b38SFlorian Vaussard
2976bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2977bff5da43SRob Herring
2978cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2979cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2980cc93319bSFlorian Vaussard
2981bff5da43SRob Herring			foreach my $compat (@compats) {
2982bff5da43SRob Herring				my $compat2 = $compat;
2983185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2984185d566bSRob Herring				my $compat3 = $compat;
2985185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2986185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2987bff5da43SRob Herring				if ( $? >> 8 ) {
2988bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2989bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2990bff5da43SRob Herring				}
2991bff5da43SRob Herring
29924fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
29934fbf32a6SFlorian Vaussard				my $vendor = $1;
2994cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2995bff5da43SRob Herring				if ( $? >> 8 ) {
2996bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2997cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2998bff5da43SRob Herring				}
2999bff5da43SRob Herring			}
3000bff5da43SRob Herring		}
3001bff5da43SRob Herring
30029f3a8992SRob Herring# check for using SPDX license tag at beginning of files
30039f3a8992SRob Herring		if ($realline == $checklicenseline) {
30049f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
30059f3a8992SRob Herring				$checklicenseline = 2;
30069f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
30079f3a8992SRob Herring				my $comment = "";
30089f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
30099f3a8992SRob Herring					$comment = '/*';
30109f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
30119f3a8992SRob Herring					$comment = '//';
30129f3a8992SRob Herring				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
30139f3a8992SRob Herring					$comment = '#';
30149f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
30159f3a8992SRob Herring					$comment = '..';
30169f3a8992SRob Herring				}
30179f3a8992SRob Herring
30189f3a8992SRob Herring				if ($comment !~ /^$/ &&
30199f3a8992SRob Herring				    $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
30209f3a8992SRob Herring					 WARN("SPDX_LICENSE_TAG",
30219f3a8992SRob Herring					      "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
30223b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
30233b6e8ac9SJoe Perches					 my $spdx_license = $1;
30243b6e8ac9SJoe Perches					 if (!is_SPDX_License_valid($spdx_license)) {
30253b6e8ac9SJoe Perches						  WARN("SPDX_LICENSE_TAG",
30263b6e8ac9SJoe Perches						       "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
30273b6e8ac9SJoe Perches					 }
30289f3a8992SRob Herring				}
30299f3a8992SRob Herring			}
30309f3a8992SRob Herring		}
30319f3a8992SRob Herring
30325368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3033d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
30345368df20SAndy Whitcroft
303547e0c88bSJoe Perches# line length limit (with some exclusions)
303647e0c88bSJoe Perches#
303747e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
303847e0c88bSJoe Perches#	logging functions like pr_info that end in a string
303947e0c88bSJoe Perches#	lines with a single string
304047e0c88bSJoe Perches#	#defines that are a single string
30412e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
304247e0c88bSJoe Perches#
304347e0c88bSJoe Perches# There are 3 different line length message types:
3044ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
304547e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
304647e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
304747e0c88bSJoe Perches#
304847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
304947e0c88bSJoe Perches#
305047e0c88bSJoe Perches
3051b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
305247e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
305347e0c88bSJoe Perches
305447e0c88bSJoe Perches			# Check the allowed long line types first
305547e0c88bSJoe Perches
305647e0c88bSJoe Perches			# logging functions that end in a string that starts
305747e0c88bSJoe Perches			# before $max_line_length
305847e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
305947e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
306047e0c88bSJoe Perches				$msg_type = "";
306147e0c88bSJoe Perches
306247e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
306347e0c88bSJoe Perches			# #defines with only strings
306447e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
306547e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
306647e0c88bSJoe Perches				$msg_type = "";
306747e0c88bSJoe Perches
3068cc147506SJoe Perches			# More special cases
3069cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3070cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3071d560a5f8SJoe Perches				$msg_type = "";
3072d560a5f8SJoe Perches
30732e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
30742e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
30752e4bbbc5SAndreas Brauchli				$msg_type = "";
30762e4bbbc5SAndreas Brauchli
307747e0c88bSJoe Perches			# Otherwise set the alternate message types
307847e0c88bSJoe Perches
307947e0c88bSJoe Perches			# a comment starts before $max_line_length
308047e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
308147e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
308247e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
308347e0c88bSJoe Perches
308447e0c88bSJoe Perches			# a quoted string starts before $max_line_length
308547e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
308647e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
308747e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
308847e0c88bSJoe Perches			}
308947e0c88bSJoe Perches
309047e0c88bSJoe Perches			if ($msg_type ne "" &&
309147e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
309247e0c88bSJoe Perches				WARN($msg_type,
30936cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
30940a920b5bSAndy Whitcroft			}
309547e0c88bSJoe Perches		}
30960a920b5bSAndy Whitcroft
30978905a67cSAndy Whitcroft# check for adding lines without a newline.
30988905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3099000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3100000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
31018905a67cSAndy Whitcroft		}
31028905a67cSAndy Whitcroft
3103b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3104de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
31050a920b5bSAndy Whitcroft
31060a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
31070a920b5bSAndy Whitcroft# more than 8 must use tabs.
3108c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3109c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3110c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3111d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
31123705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
31133705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
31143705ce5bSJoe Perches			    $fix) {
3115194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
31163705ce5bSJoe Perches			}
31170a920b5bSAndy Whitcroft		}
31180a920b5bSAndy Whitcroft
311908e44365SAlberto Panizzo# check for space before tabs.
312008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
312108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
31223705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
31233705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
31243705ce5bSJoe Perches			    $fix) {
3125194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3126d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
3127194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3128c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
31293705ce5bSJoe Perches			}
313008e44365SAlberto Panizzo		}
313108e44365SAlberto Panizzo
31326a487211SJoe Perches# check for assignments on the start of a line
31336a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
31346a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
31356a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
31366a487211SJoe Perches		}
31376a487211SJoe Perches
3138d1fe9c09SJoe Perches# check for && or || at the start of a line
3139d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3140d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3141d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3142d1fe9c09SJoe Perches		}
3143d1fe9c09SJoe Perches
3144a91e8994SJoe Perches# check indentation starts on a tab stop
31455b57980dSJoe Perches		if ($perl_version_ok &&
3146bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3147a91e8994SJoe Perches			my $indent = length($1);
3148a91e8994SJoe Perches			if ($indent % 8) {
3149a91e8994SJoe Perches				if (WARN("TABSTOP",
3150a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3151a91e8994SJoe Perches				    $fix) {
3152a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3153a91e8994SJoe Perches				}
3154a91e8994SJoe Perches			}
3155a91e8994SJoe Perches		}
3156a91e8994SJoe Perches
3157d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
31585b57980dSJoe Perches		if ($perl_version_ok &&
3159fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3160d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3161d1fe9c09SJoe Perches			my $oldindent = $1;
3162d1fe9c09SJoe Perches			my $rest = $2;
3163d1fe9c09SJoe Perches
3164d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3165d1fe9c09SJoe Perches			if ($pos >= 0) {
3166b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3167b34a26f3SJoe Perches				my $newindent = $2;
3168d1fe9c09SJoe Perches
3169d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3170d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3171d1fe9c09SJoe Perches					" "  x ($pos % 8);
3172d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3173d1fe9c09SJoe Perches
3174d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3175d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
31763705ce5bSJoe Perches
31773705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
31783705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
31793705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3180194f66fcSJoe Perches						$fixed[$fixlinenr] =~
31813705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
31823705ce5bSJoe Perches					}
3183d1fe9c09SJoe Perches				}
3184d1fe9c09SJoe Perches			}
3185d1fe9c09SJoe Perches		}
3186d1fe9c09SJoe Perches
31876ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
31886ab3a970SJoe Perches# avoid checking a few false positives:
31896ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
31906ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
31916ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
31926ab3a970SJoe Perches#   multiline macros that define functions
31936ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
31946ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
31956ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
31963705ce5bSJoe Perches			if (CHK("SPACING",
3197f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
31983705ce5bSJoe Perches			    $fix) {
3199194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3200f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
32013705ce5bSJoe Perches			}
3202aad4f614SJoe Perches		}
3203aad4f614SJoe Perches
320486406b1cSJoe Perches# Block comment styles
320586406b1cSJoe Perches# Networking with an initial /*
320605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3207fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
320885ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
320985ad978cSJoe Perches		    $realline > 2) {
321005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
321105880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
321205880600SJoe Perches		}
321305880600SJoe Perches
321486406b1cSJoe Perches# Block comments use * on subsequent lines
321586406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
321686406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3217a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
321861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3219a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
322086406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
322186406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3222a605e32eSJoe Perches		}
3223a605e32eSJoe Perches
322486406b1cSJoe Perches# Block comments use */ on trailing lines
322586406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3226c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3227c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3228c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
322986406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
323086406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
323105880600SJoe Perches		}
323205880600SJoe Perches
323308eb9b80SJoe Perches# Block comment * alignment
323408eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3235af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3236af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3237af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
323808eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3239af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3240af207524SJoe Perches			my $oldindent;
324108eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3242af207524SJoe Perches			if (defined($1)) {
3243af207524SJoe Perches				$oldindent = expand_tabs($1);
3244af207524SJoe Perches			} else {
3245af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3246af207524SJoe Perches				$oldindent = expand_tabs($1);
3247af207524SJoe Perches			}
324808eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
324908eb9b80SJoe Perches			my $newindent = $1;
325008eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3251af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
325208eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
325308eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
325408eb9b80SJoe Perches			}
325508eb9b80SJoe Perches		}
325608eb9b80SJoe Perches
32577f619191SJoe Perches# check for missing blank lines after struct/union declarations
32587f619191SJoe Perches# with exceptions for various attributes and macros
32597f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
32607f619191SJoe Perches		    $line =~ /^\+/ &&
32617f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
32627f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
32637f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
32647f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
32657f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
32667f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
32677f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
32680bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
32697f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3270d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3271d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3272d752fcc8SJoe Perches			    $fix) {
3273f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3274d752fcc8SJoe Perches			}
32757f619191SJoe Perches		}
32767f619191SJoe Perches
3277365dd4eaSJoe Perches# check for multiple consecutive blank lines
3278365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3279365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3280365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3281d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3282d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3283d752fcc8SJoe Perches			    $fix) {
3284f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3285d752fcc8SJoe Perches			}
3286d752fcc8SJoe Perches
3287365dd4eaSJoe Perches			$last_blank_line = $linenr;
3288365dd4eaSJoe Perches		}
3289365dd4eaSJoe Perches
32903b617e3bSJoe Perches# check for missing blank lines after declarations
32913f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
32923f7bac03SJoe Perches			# actual declarations
32933f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
32945a4e1fd3SJoe Perches			# function pointer declarations
32955a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
32963f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
32973f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
32983f7bac03SJoe Perches			# known declaration macros
32993f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
33003f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
33013f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
33023f7bac03SJoe Perches			# other possible extensions of declaration lines
33033f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
33043f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
33053f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
33063f7bac03SJoe Perches			# looks like a declaration
33073f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33085a4e1fd3SJoe Perches			# function pointer declarations
33095a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
33103f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
33113f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
33123f7bac03SJoe Perches			# known declaration macros
33133f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
33143f7bac03SJoe Perches			# start of struct or union or enum
3315328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
33163f7bac03SJoe Perches			# start or end of block or continuation of declaration
33173f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
33183f7bac03SJoe Perches			# bitfield continuation
33193f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
33203f7bac03SJoe Perches			# other possible extensions of declaration lines
33213f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
33223f7bac03SJoe Perches			# indentation of previous and current line are the same
33233f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3324d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3325d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3326d752fcc8SJoe Perches			    $fix) {
3327f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3328d752fcc8SJoe Perches			}
33293b617e3bSJoe Perches		}
33303b617e3bSJoe Perches
33315f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
33326b4c5bebSAndy Whitcroft# Exceptions:
33336b4c5bebSAndy Whitcroft#  1) within comments
33346b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
33356b4c5bebSAndy Whitcroft#  3) hanging labels
33363705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
33375f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
33383705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
33393705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
33403705ce5bSJoe Perches			    $fix) {
3341194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
33423705ce5bSJoe Perches			}
33435f7ddae6SRaffaele Recalcati		}
33445f7ddae6SRaffaele Recalcati
3345b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3346b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3347b9ea10d6SAndy Whitcroft
33485751a24eSJoe Perches# check for unusual line ending [ or (
33495751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
33505751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
33515751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
33525751a24eSJoe Perches		}
33535751a24eSJoe Perches
33544dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
33554dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
33564dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
33574dbed76fSJoe Perches			$context_function = $1;
33584dbed76fSJoe Perches		}
33594dbed76fSJoe Perches
33604dbed76fSJoe Perches# check if this appears to be the end of function declaration
33614dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
33624dbed76fSJoe Perches			undef $context_function;
33634dbed76fSJoe Perches		}
33644dbed76fSJoe Perches
3365032a4c0fSJoe Perches# check indentation of any line with a bare else
3366840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3367032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3368032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3369032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3370840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3371840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3372840080a0SJoe Perches			     defined $lines[$linenr] &&
3373840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3374032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3375032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3376032a4c0fSJoe Perches			}
3377032a4c0fSJoe Perches		}
3378032a4c0fSJoe Perches
3379c00df19aSJoe Perches# check indentation of a line with a break;
3380c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3381c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3382c00df19aSJoe Perches			my $tabs = $1;
3383c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3384c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3385c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3386c00df19aSJoe Perches			}
3387c00df19aSJoe Perches		}
3388c00df19aSJoe Perches
3389c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3390cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3391000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3392000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3393c2fdda0dSAndy Whitcroft		}
339422f2a2efSAndy Whitcroft
339556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
339656e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
339756e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
339856e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
339956e77d70SJoe Perches		}
340056e77d70SJoe Perches
34019c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
34022b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
34032b474a1aSAndy Whitcroft		    $realline_next);
34043e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3405ca819864SJoe Perches		if ($linenr > $suppress_statement &&
34061b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3407170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3408f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3409171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3410171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3411171ae1a4SAndy Whitcroft
34123e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
34133e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
34143e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
34153e469cdcSAndy Whitcroft			# until we hit end of it.
34163e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
34173e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
34183e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
34193e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
34203e469cdcSAndy Whitcroft			}
3421f74bd194SAndy Whitcroft
34222b474a1aSAndy Whitcroft			# Find the real next line.
34232b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
34242b474a1aSAndy Whitcroft			if (defined $realline_next &&
34252b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
34262b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
34272b474a1aSAndy Whitcroft				$realline_next++;
34282b474a1aSAndy Whitcroft			}
34292b474a1aSAndy Whitcroft
3430171ae1a4SAndy Whitcroft			my $s = $stat;
3431171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3432cf655043SAndy Whitcroft
3433c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3434171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3435c2fdda0dSAndy Whitcroft
3436c2fdda0dSAndy Whitcroft			# Ignore functions being called
3437171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3438c2fdda0dSAndy Whitcroft
3439463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3440463f2864SAndy Whitcroft
3441c45dcabdSAndy Whitcroft			# declarations always start with types
3442d2506586SAndy 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) {
3443c45dcabdSAndy Whitcroft				my $type = $1;
3444c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3445c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3446c45dcabdSAndy Whitcroft
34476c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3448a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3449c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3450c2fdda0dSAndy Whitcroft			}
34518905a67cSAndy Whitcroft
34526c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
345365863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3454c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
34559c0ca6f9SAndy Whitcroft			}
34568905a67cSAndy Whitcroft
34578905a67cSAndy Whitcroft			# Check for any sort of function declaration.
34588905a67cSAndy Whitcroft			# int foo(something bar, other baz);
34598905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3460171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
34618905a67cSAndy Whitcroft				my ($name_len) = length($1);
34628905a67cSAndy Whitcroft
3463cf655043SAndy Whitcroft				my $ctx = $s;
3464773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
34658905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3466cf655043SAndy Whitcroft
34678905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3468c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
34698905a67cSAndy Whitcroft
3470c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
34718905a67cSAndy Whitcroft					}
34728905a67cSAndy Whitcroft				}
34738905a67cSAndy Whitcroft			}
34748905a67cSAndy Whitcroft
34759c0ca6f9SAndy Whitcroft		}
34769c0ca6f9SAndy Whitcroft
347700df344fSAndy Whitcroft#
347800df344fSAndy Whitcroft# Checks which may be anchored in the context.
347900df344fSAndy Whitcroft#
348000df344fSAndy Whitcroft
348100df344fSAndy Whitcroft# Check for switch () and associated case and default
348200df344fSAndy Whitcroft# statements should be at the same indent.
348300df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
348400df344fSAndy Whitcroft			my $err = '';
348500df344fSAndy Whitcroft			my $sep = '';
348600df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
348700df344fSAndy Whitcroft			shift(@ctx);
348800df344fSAndy Whitcroft			for my $ctx (@ctx) {
348900df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
349000df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
349100df344fSAndy Whitcroft							$indent != $cindent) {
349200df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
349300df344fSAndy Whitcroft					$sep = '';
349400df344fSAndy Whitcroft				} else {
349500df344fSAndy Whitcroft					$sep = "[...]\n";
349600df344fSAndy Whitcroft				}
349700df344fSAndy Whitcroft			}
349800df344fSAndy Whitcroft			if ($err ne '') {
3499000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3500000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3501de7d4f0eSAndy Whitcroft			}
3502de7d4f0eSAndy Whitcroft		}
3503de7d4f0eSAndy Whitcroft
3504de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3505de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
35060fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3507773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3508773647a0SAndy Whitcroft
35099c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
35108eef05ddSJoe Perches
35118eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
35128eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
35138eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
35148eef05ddSJoe Perches			}
35158eef05ddSJoe Perches
3516de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3517de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3518de7d4f0eSAndy Whitcroft
3519548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3520548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3521de7d4f0eSAndy Whitcroft
3522548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3523548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3524548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3525548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3526548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3527773647a0SAndy Whitcroft				$ctx_ln++;
3528773647a0SAndy Whitcroft			}
3529548596d5SAndy Whitcroft
353053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
353153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3532773647a0SAndy Whitcroft
3533773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3534000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3535000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
353601464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
353700df344fSAndy Whitcroft			}
3538773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3539773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3540773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3541773647a0SAndy Whitcroft			{
35429c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
35439c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3544000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3545000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
354601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
35479c0ca6f9SAndy Whitcroft				}
35489c0ca6f9SAndy Whitcroft			}
354900df344fSAndy Whitcroft		}
355000df344fSAndy Whitcroft
35514d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3552f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
35533e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
35543e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
35553e469cdcSAndy Whitcroft					if (!defined $stat);
35564d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
35574d001e4dSAndy Whitcroft
35584d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
35594d001e4dSAndy Whitcroft
35609f5af480SJoe Perches			# remove inline comments
35619f5af480SJoe Perches			$s =~ s/$;/ /g;
35629f5af480SJoe Perches			$c =~ s/$;/ /g;
35634d001e4dSAndy Whitcroft
35644d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
35656f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
35666f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
35674d001e4dSAndy Whitcroft
35689f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
35699f5af480SJoe Perches			# none on the first line, and are going to readd them
35709f5af480SJoe Perches			# where necessary.
35719f5af480SJoe Perches			$s =~ s/\n./\n/gs;
35729f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
35739f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
35749f5af480SJoe Perches			}
35759f5af480SJoe Perches
35764d001e4dSAndy Whitcroft			# We want to check the first line inside the block
35774d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
35784d001e4dSAndy Whitcroft			#  1) any blank line termination
35794d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
35804d001e4dSAndy Whitcroft			#  3) any do (...) {
35814d001e4dSAndy Whitcroft			my $continuation = 0;
35824d001e4dSAndy Whitcroft			my $check = 0;
35834d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
35844d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
35854d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
35864d001e4dSAndy Whitcroft				$continuation = 1;
35874d001e4dSAndy Whitcroft			}
35889bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
35894d001e4dSAndy Whitcroft				$check = 1;
35904d001e4dSAndy Whitcroft				$cond_lines++;
35914d001e4dSAndy Whitcroft			}
35924d001e4dSAndy Whitcroft
35934d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
35944d001e4dSAndy Whitcroft			# preprocessor statement.
35954d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
35964d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
35974d001e4dSAndy Whitcroft				$check = 0;
35984d001e4dSAndy Whitcroft			}
35994d001e4dSAndy Whitcroft
36009bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3601740504c6SAndy Whitcroft			$continuation = 0;
36029bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
36039bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
36044d001e4dSAndy Whitcroft
3605f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3606f16fa28fSAndy Whitcroft				# is not linear.
3607f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3608f16fa28fSAndy Whitcroft					$check = 0;
3609f16fa28fSAndy Whitcroft				}
3610f16fa28fSAndy Whitcroft
36119bd49efeSAndy Whitcroft				# Ignore:
36129bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
36139bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
36149bd49efeSAndy Whitcroft				#  3) labels.
3615740504c6SAndy Whitcroft				if ($continuation ||
3616740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
36179bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
36189bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3619740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
362030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
36219bd49efeSAndy Whitcroft						$cond_lines++;
36229bd49efeSAndy Whitcroft					}
36234d001e4dSAndy Whitcroft				}
362430dad6ebSAndy Whitcroft			}
36254d001e4dSAndy Whitcroft
36264d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
36274d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
36284d001e4dSAndy Whitcroft
36294d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
36304d001e4dSAndy Whitcroft			# this is not this patch's fault.
36314d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
36324d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
36334d001e4dSAndy Whitcroft				$check = 0;
36344d001e4dSAndy Whitcroft			}
36354d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
36364d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
36374d001e4dSAndy Whitcroft			}
36384d001e4dSAndy Whitcroft
36399bd49efeSAndy 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";
36404d001e4dSAndy Whitcroft
36419f5af480SJoe Perches			if ($check && $s ne '' &&
36429f5af480SJoe Perches			    (($sindent % 8) != 0 ||
36439f5af480SJoe Perches			     ($sindent < $indent) ||
3644f6950a73SJoe Perches			     ($sindent == $indent &&
3645f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
36469f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3647000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3648000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
36494d001e4dSAndy Whitcroft			}
36504d001e4dSAndy Whitcroft		}
36514d001e4dSAndy Whitcroft
36526c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
36536c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
36541f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
36551f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
36566c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3657c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3658c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3659cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3660cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
36611f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3662c2fdda0dSAndy Whitcroft		}
36636c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
36646c72ffaaSAndy Whitcroft
366500df344fSAndy Whitcroft#ignore lines not being added
36663705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
366700df344fSAndy Whitcroft
366811ca40a0SJoe Perches# check for dereferences that span multiple lines
366911ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
367011ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
367111ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
367211ca40a0SJoe Perches			my $ref = $1;
367311ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
367411ca40a0SJoe Perches			$ref .= $1;
367511ca40a0SJoe Perches			$ref =~ s/\s//g;
367611ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
367711ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
367811ca40a0SJoe Perches		}
367911ca40a0SJoe Perches
3680a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3681c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3682a1ce18e4SJoe Perches			my $type = $1;
3683a1ce18e4SJoe Perches			my $var = $2;
3684207a8e84SJoe Perches			$var = "" if (!defined $var);
3685207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3686a1ce18e4SJoe Perches				my $sign = $1;
3687a1ce18e4SJoe Perches				my $pointer = $2;
3688a1ce18e4SJoe Perches
3689a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3690a1ce18e4SJoe Perches
3691a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3692a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3693a1ce18e4SJoe Perches				    $fix) {
3694a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3695207a8e84SJoe Perches					my $comp_pointer = $pointer;
3696207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3697207a8e84SJoe Perches					$decl .= $comp_pointer;
3698207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3699207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3700a1ce18e4SJoe Perches				}
3701a1ce18e4SJoe Perches			}
3702a1ce18e4SJoe Perches		}
3703a1ce18e4SJoe Perches
3704653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
37057429c690SAndy Whitcroft		if ($dbg_type) {
37067429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3707000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3708000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
37097429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3710000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3711000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
37127429c690SAndy Whitcroft			}
3713653d4876SAndy Whitcroft			next;
3714653d4876SAndy Whitcroft		}
3715a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3716a1ef277eSAndy Whitcroft		if ($dbg_attr) {
37179360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3718000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3719000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
37209360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3721000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3722000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3723a1ef277eSAndy Whitcroft			}
3724a1ef277eSAndy Whitcroft			next;
3725a1ef277eSAndy Whitcroft		}
3726653d4876SAndy Whitcroft
3727f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
372899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
372999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3730d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3731d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3732f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3733f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3734f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3735d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3736d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3737f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3738d752fcc8SJoe Perches				$fixedline = $line;
37398d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3740f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3741d752fcc8SJoe Perches			}
3742f0a594c1SAndy Whitcroft		}
3743f0a594c1SAndy Whitcroft
374400df344fSAndy Whitcroft#
374500df344fSAndy Whitcroft# Checks which are anchored on the added line.
374600df344fSAndy Whitcroft#
374700df344fSAndy Whitcroft
3748653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3749c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3750653d4876SAndy Whitcroft			my $path = $1;
3751653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3752000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3753495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3754495e9d84SJoe Perches			}
3755495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3756495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3757495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3758653d4876SAndy Whitcroft			}
3759653d4876SAndy Whitcroft		}
3760653d4876SAndy Whitcroft
376100df344fSAndy Whitcroft# no C99 // comments
376200df344fSAndy Whitcroft		if ($line =~ m{//}) {
37633705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
37643705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
37653705ce5bSJoe Perches			    $fix) {
3766194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
37673705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
37683705ce5bSJoe Perches					my $comment = trim($1);
3769194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
37703705ce5bSJoe Perches				}
37713705ce5bSJoe Perches			}
377200df344fSAndy Whitcroft		}
377300df344fSAndy Whitcroft		# Remove C99 comments.
37740a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
37756c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
37760a920b5bSAndy Whitcroft
37772b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
37782b474a1aSAndy Whitcroft# the whole statement.
37792b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
37802b474a1aSAndy Whitcroft		if (defined $realline_next &&
37812b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
37822b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
37832b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
37842b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
37853cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
37863cbf62dfSAndy Whitcroft			# a prefix:
37873cbf62dfSAndy Whitcroft			#   XXX(foo);
37883cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3789653d4876SAndy Whitcroft			my $name = $1;
379087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
37913cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
37923cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
37933cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
37943cbf62dfSAndy Whitcroft
37953cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
37962b474a1aSAndy Whitcroft				\n.}\s*$|
379748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
379848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
379948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
38002b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
38012b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
380248012058SAndy Whitcroft			    )/x) {
38032b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
38042b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
38052b474a1aSAndy Whitcroft			} else {
38062b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38070a920b5bSAndy Whitcroft			}
38080a920b5bSAndy Whitcroft		}
38092b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
38102b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
38112b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
38122b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
38132b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
38142b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
38152b474a1aSAndy Whitcroft		}
38162b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
38172b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3818000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3819000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
38202b474a1aSAndy Whitcroft		}
38210a920b5bSAndy Whitcroft
38225150bda4SJoe Eloff# check for global initialisers.
38236d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3824d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
38256d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3826d5e616fcSJoe Perches			    $fix) {
38276d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3828d5e616fcSJoe Perches			}
3829f0a594c1SAndy Whitcroft		}
38300a920b5bSAndy Whitcroft# check for static initialisers.
38316d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3832d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
38336d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3834d5e616fcSJoe Perches				      $herecurr) &&
3835d5e616fcSJoe Perches			    $fix) {
38366d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3837d5e616fcSJoe Perches			}
38380a920b5bSAndy Whitcroft		}
38390a920b5bSAndy Whitcroft
38401813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
38411813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
38421813087dSJoe Perches			my $tmp = trim($1);
38431813087dSJoe Perches			WARN("MISORDERED_TYPE",
38441813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
38451813087dSJoe Perches		}
38461813087dSJoe Perches
3847809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3848809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3849809e082eSJoe Perches			my $type = trim($1);
3850809e082eSJoe Perches			next if ($type !~ /\bint\b/);
3851809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3852809e082eSJoe Perches			my $new_type = $type;
3853809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
3854809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3855809e082eSJoe Perches			$new_type =~ s/^const\s+//;
3856809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3857809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
3858809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
3859809e082eSJoe Perches			$new_type = trim($new_type);
3860809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
3861809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3862809e082eSJoe Perches			    $fix) {
3863809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3864809e082eSJoe Perches			}
3865809e082eSJoe Perches		}
3866809e082eSJoe Perches
3867cb710ecaSJoe Perches# check for static const char * arrays.
3868cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3869000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3870000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3871cb710ecaSJoe Perches				$herecurr);
3872cb710ecaSJoe Perches               }
3873cb710ecaSJoe Perches
3874cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3875cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3876000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3877000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3878cb710ecaSJoe Perches				$herecurr);
3879cb710ecaSJoe Perches               }
3880cb710ecaSJoe Perches
3881ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3882ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3883ab7e23f3SJoe Perches			my $found = $1;
3884ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3885ab7e23f3SJoe Perches				WARN("CONST_CONST",
3886ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3887ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3888ab7e23f3SJoe Perches				WARN("CONST_CONST",
3889ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3890ab7e23f3SJoe Perches			}
3891ab7e23f3SJoe Perches		}
3892ab7e23f3SJoe Perches
38939b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
38949b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
38959b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
38969b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
38979b0fa60dSJoe Perches				$herecurr);
38989b0fa60dSJoe Perches               }
38999b0fa60dSJoe Perches
3900b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3901b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3902b598b670SJoe Perches			my $array = $1;
3903b598b670SJoe 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*\))@) {
3904b598b670SJoe Perches				my $array_div = $1;
3905b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3906b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3907b598b670SJoe Perches				    $fix) {
3908b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3909b598b670SJoe Perches				}
3910b598b670SJoe Perches			}
3911b598b670SJoe Perches		}
3912b598b670SJoe Perches
3913b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3914b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3915b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3916b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3917b36190c5SJoe Perches			    $fix) {
3918194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3919b36190c5SJoe Perches			}
3920b36190c5SJoe Perches		}
3921b36190c5SJoe Perches
3922653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3923653d4876SAndy Whitcroft# make sense.
3924653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
39258054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3926c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
39278ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
392846d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
3929000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3930000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
39310a920b5bSAndy Whitcroft		}
39320a920b5bSAndy Whitcroft
39330a920b5bSAndy Whitcroft# * goes on variable not on type
393465863862SAndy Whitcroft		# (char*[ const])
3935bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3936bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
39373705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3938d8aaf121SAndy Whitcroft
393965863862SAndy Whitcroft			# Should start with a space.
394065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
394165863862SAndy Whitcroft			# Should not end with a space.
394265863862SAndy Whitcroft			$to =~ s/\s+$//;
394365863862SAndy Whitcroft			# '*'s should not have spaces between.
3944f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
394565863862SAndy Whitcroft			}
3946d8aaf121SAndy Whitcroft
39473705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
394865863862SAndy Whitcroft			if ($from ne $to) {
39493705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
39503705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
39513705ce5bSJoe Perches				    $fix) {
39523705ce5bSJoe Perches					my $sub_from = $ident;
39533705ce5bSJoe Perches					my $sub_to = $ident;
39543705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3955194f66fcSJoe Perches					$fixed[$fixlinenr] =~
39563705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
39573705ce5bSJoe Perches				}
395865863862SAndy Whitcroft			}
3959bfcb2cc7SAndy Whitcroft		}
3960bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3961bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
39623705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3963d8aaf121SAndy Whitcroft
396465863862SAndy Whitcroft			# Should start with a space.
396565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
396665863862SAndy Whitcroft			# Should not end with a space.
396765863862SAndy Whitcroft			$to =~ s/\s+$//;
396865863862SAndy Whitcroft			# '*'s should not have spaces between.
3969f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
397065863862SAndy Whitcroft			}
397165863862SAndy Whitcroft			# Modifiers should have spaces.
397265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
397365863862SAndy Whitcroft
39743705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3975667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
39763705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
39773705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
39783705ce5bSJoe Perches				    $fix) {
39793705ce5bSJoe Perches
39803705ce5bSJoe Perches					my $sub_from = $match;
39813705ce5bSJoe Perches					my $sub_to = $match;
39823705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3983194f66fcSJoe Perches					$fixed[$fixlinenr] =~
39843705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
39853705ce5bSJoe Perches				}
398665863862SAndy Whitcroft			}
39870a920b5bSAndy Whitcroft		}
39880a920b5bSAndy Whitcroft
39899d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
39909d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
39910675a8fbSJean Delvare			my $msg_level = \&WARN;
39920675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
39930675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
39949d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
39959d3e3c70SJoe Perches		}
39960a920b5bSAndy Whitcroft
39979d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
39988905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3999000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4000000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
40018905a67cSAndy Whitcroft		}
40028905a67cSAndy Whitcroft
400317441227SJoe Perches# check for uses of printk_ratelimit
400417441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4005000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4006000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
400717441227SJoe Perches		}
400817441227SJoe Perches
4009eeef5733SJoe Perches# printk should use KERN_* levels
4010eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4011000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4012eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
401300df344fSAndy Whitcroft		}
40140a920b5bSAndy Whitcroft
4015243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4016243f3803SJoe Perches			my $orig = $1;
4017243f3803SJoe Perches			my $level = lc($orig);
4018243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
40198f26b837SJoe Perches			my $level2 = $level;
40208f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4021243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4022daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4023243f3803SJoe Perches		}
4024243f3803SJoe Perches
4025243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
4026d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
4027d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4028d5e616fcSJoe Perches			    $fix) {
4029194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4030d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
4031d5e616fcSJoe Perches			}
4032243f3803SJoe Perches		}
4033243f3803SJoe Perches
4034dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4035dc139313SJoe Perches			my $orig = $1;
4036dc139313SJoe Perches			my $level = lc($orig);
4037dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4038dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4039dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4040dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4041dc139313SJoe Perches		}
4042dc139313SJoe Perches
404391c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
404491c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
404591c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
404691c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
404791c9afafSAndy Lutomirski			WARN("ENOSYS",
404891c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
404991c9afafSAndy Lutomirski		}
405091c9afafSAndy Lutomirski
4051653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4052653d4876SAndy Whitcroft# or if closed on same line
40535b57980dSJoe Perches		if ($perl_version_ok &&
40542d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
40552d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
40562d453e3bSJoe Perches		    $sline !~ /}/) {
40578d182478SJoe Perches			if (ERROR("OPEN_BRACE",
40582d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
40598d182478SJoe Perches			    $fix) {
40608d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
40618d182478SJoe Perches				my $fixed_line = $rawline;
40628d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
40638d182478SJoe Perches				my $line1 = $1;
40648d182478SJoe Perches				my $line2 = $2;
40658d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
40668d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
40678d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
40688d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
40698d182478SJoe Perches				}
40708d182478SJoe Perches			}
40710a920b5bSAndy Whitcroft		}
4072653d4876SAndy Whitcroft
40738905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
40748905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
40758905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
40768d182478SJoe Perches			if (ERROR("OPEN_BRACE",
40778d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
40788d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
40798d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
40808d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
40818d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
40828d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
40838d182478SJoe Perches				$fixedline = $rawline;
40848d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
40858d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
40868d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
40878d182478SJoe Perches				}
40888d182478SJoe Perches			}
40898905a67cSAndy Whitcroft		}
40908905a67cSAndy Whitcroft
40910c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
40923705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
40933705ce5bSJoe Perches			if (WARN("SPACING",
40943705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
40953705ce5bSJoe Perches			    $fix) {
4096194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40973705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
40983705ce5bSJoe Perches			}
40990c73b4ebSAndy Whitcroft		}
41000c73b4ebSAndy Whitcroft
410131070b5dSJoe Perches# Function pointer declarations
410231070b5dSJoe Perches# check spacing between type, funcptr, and args
410331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
410491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
410531070b5dSJoe Perches			my $declare = $1;
410631070b5dSJoe Perches			my $pre_pointer_space = $2;
410731070b5dSJoe Perches			my $post_pointer_space = $3;
410831070b5dSJoe Perches			my $funcname = $4;
410931070b5dSJoe Perches			my $post_funcname_space = $5;
411031070b5dSJoe Perches			my $pre_args_space = $6;
411131070b5dSJoe Perches
411291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
411391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
411491f72e9cSJoe Perches# don't need a space so don't warn for those.
411591f72e9cSJoe Perches			my $post_declare_space = "";
411691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
411791f72e9cSJoe Perches				$post_declare_space = $1;
411891f72e9cSJoe Perches				$declare = rtrim($declare);
411991f72e9cSJoe Perches			}
412091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
412131070b5dSJoe Perches				WARN("SPACING",
412231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
412391f72e9cSJoe Perches				$post_declare_space = " ";
412431070b5dSJoe Perches			}
412531070b5dSJoe Perches
412631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
412791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
412891f72e9cSJoe Perches# equivalent to
412991f72e9cSJoe Perches#	int  foo(int bar, ...)
413091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
413191f72e9cSJoe Perches#
413291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
413391f72e9cSJoe Perches#				WARN("SPACING",
413491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
413591f72e9cSJoe Perches#			}
413631070b5dSJoe Perches
413731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
413831070b5dSJoe Perches			if (defined $pre_pointer_space &&
413931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
414031070b5dSJoe Perches				WARN("SPACING",
414131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
414231070b5dSJoe Perches			}
414331070b5dSJoe Perches
414431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
414531070b5dSJoe Perches			if (defined $post_pointer_space &&
414631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
414731070b5dSJoe Perches				WARN("SPACING",
414831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
414931070b5dSJoe Perches			}
415031070b5dSJoe Perches
415131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
415231070b5dSJoe Perches			if (defined $post_funcname_space &&
415331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
415431070b5dSJoe Perches				WARN("SPACING",
415531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
415631070b5dSJoe Perches			}
415731070b5dSJoe Perches
415831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
415931070b5dSJoe Perches			if (defined $pre_args_space &&
416031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
416131070b5dSJoe Perches				WARN("SPACING",
416231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
416331070b5dSJoe Perches			}
416431070b5dSJoe Perches
416531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4166194f66fcSJoe Perches				$fixed[$fixlinenr] =~
416791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
416831070b5dSJoe Perches			}
416931070b5dSJoe Perches		}
417031070b5dSJoe Perches
41718d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
41728d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4173fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4174fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
41758d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
41768d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
41778d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4178fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
417938dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
41803705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
41813705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
41823705ce5bSJoe Perches				    $fix) {
4183194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
41843705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
41853705ce5bSJoe Perches				}
41868d31cfceSAndy Whitcroft			}
41878d31cfceSAndy Whitcroft		}
41888d31cfceSAndy Whitcroft
4189f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
41906c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4191c2fdda0dSAndy Whitcroft			my $name = $1;
4192773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4193773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4194c2fdda0dSAndy Whitcroft
4195c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4196773647a0SAndy Whitcroft			if ($name =~ /^(?:
4197773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4198773647a0SAndy Whitcroft				volatile|__volatile__|
4199773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4200773647a0SAndy Whitcroft				asm|__asm__)$/x)
4201773647a0SAndy Whitcroft			{
4202c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4203c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4204c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4205c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4206773647a0SAndy Whitcroft
4207773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4208c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4209c2fdda0dSAndy Whitcroft
4210c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4211c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4212773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4213c2fdda0dSAndy Whitcroft
4214c2fdda0dSAndy Whitcroft			} else {
42153705ce5bSJoe Perches				if (WARN("SPACING",
42163705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
42173705ce5bSJoe Perches					     $fix) {
4218194f66fcSJoe Perches					$fixed[$fixlinenr] =~
42193705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
42203705ce5bSJoe Perches				}
4221f0a594c1SAndy Whitcroft			}
42226c72ffaaSAndy Whitcroft		}
42239a4cad4eSEric Nelson
4224653d4876SAndy Whitcroft# Check operator spacing.
42250a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
42263705ce5bSJoe Perches			my $fixed_line = "";
42273705ce5bSJoe Perches			my $line_fixed = 0;
42283705ce5bSJoe Perches
42299c0ca6f9SAndy Whitcroft			my $ops = qr{
42309c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
42319c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
42329c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
42331f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
423484731623SJoe Perches				\?:|\?|:
42359c0ca6f9SAndy Whitcroft			}x;
4236cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
42373705ce5bSJoe Perches
42383705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
42393705ce5bSJoe Perches##			foreach my $el (@elements) {
42403705ce5bSJoe Perches##				print("el: <$el>\n");
42413705ce5bSJoe Perches##			}
42423705ce5bSJoe Perches
42433705ce5bSJoe Perches			my @fix_elements = ();
424400df344fSAndy Whitcroft			my $off = 0;
42456c72ffaaSAndy Whitcroft
42463705ce5bSJoe Perches			foreach my $el (@elements) {
42473705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
42483705ce5bSJoe Perches				$off += length($el);
42493705ce5bSJoe Perches			}
42503705ce5bSJoe Perches
42513705ce5bSJoe Perches			$off = 0;
42523705ce5bSJoe Perches
42536c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4254b34c648bSJoe Perches			my $last_after = -1;
42556c72ffaaSAndy Whitcroft
42560a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
42573705ce5bSJoe Perches
42583705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
42593705ce5bSJoe Perches
42603705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
42613705ce5bSJoe Perches
42624a0df2efSAndy Whitcroft				$off += length($elements[$n]);
42634a0df2efSAndy Whitcroft
426425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4265773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4266773647a0SAndy Whitcroft				my $cc = '';
4267773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4268773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4269773647a0SAndy Whitcroft				}
4270773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4271773647a0SAndy Whitcroft
42724a0df2efSAndy Whitcroft				my $a = '';
42734a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
42744a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4275cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
42764a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
42774a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4278773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
42794a0df2efSAndy Whitcroft
42800a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
42814a0df2efSAndy Whitcroft
42824a0df2efSAndy Whitcroft				my $c = '';
42830a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
42844a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
42854a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4286cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
42874a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
42884a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
42898b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
42904a0df2efSAndy Whitcroft				} else {
42914a0df2efSAndy Whitcroft					$c = 'E';
42920a920b5bSAndy Whitcroft				}
42930a920b5bSAndy Whitcroft
42944a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
42954a0df2efSAndy Whitcroft
42964a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
42974a0df2efSAndy Whitcroft
42986c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4299de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
43000a920b5bSAndy Whitcroft
430174048ed8SAndy Whitcroft				# Pull out the value of this operator.
43026c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
43030a920b5bSAndy Whitcroft
43041f65f947SAndy Whitcroft				# Get the full operator variant.
43051f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
43061f65f947SAndy Whitcroft
430713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
430813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4309d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
431013214adfSAndy Whitcroft
4311cf655043SAndy Whitcroft#				# Ignore comments
4312cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
431313214adfSAndy Whitcroft
4314d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
431513214adfSAndy Whitcroft				} elsif ($op eq ';') {
4316cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4317cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
43183705ce5bSJoe Perches						if (ERROR("SPACING",
43193705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4320b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
43213705ce5bSJoe Perches							$line_fixed = 1;
43223705ce5bSJoe Perches						}
4323d8aaf121SAndy Whitcroft					}
4324d8aaf121SAndy Whitcroft
4325d8aaf121SAndy Whitcroft				# // is a comment
4326d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
43270a920b5bSAndy Whitcroft
4328b00e4814SJoe Perches				#   :   when part of a bitfield
4329b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4330b00e4814SJoe Perches					# skip the bitfield test for now
4331b00e4814SJoe Perches
43321f65f947SAndy Whitcroft				# No spaces for:
43331f65f947SAndy Whitcroft				#   ->
4334b00e4814SJoe Perches				} elsif ($op eq '->') {
43354a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
43363705ce5bSJoe Perches						if (ERROR("SPACING",
43373705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4338b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43393705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
43403705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
43413705ce5bSJoe Perches							}
4342b34c648bSJoe Perches							$line_fixed = 1;
43433705ce5bSJoe Perches						}
43440a920b5bSAndy Whitcroft					}
43450a920b5bSAndy Whitcroft
43462381097bSJoe Perches				# , must not have a space before and must have a space on the right.
43470a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
43482381097bSJoe Perches					my $rtrim_before = 0;
43492381097bSJoe Perches					my $space_after = 0;
43502381097bSJoe Perches					if ($ctx =~ /Wx./) {
43512381097bSJoe Perches						if (ERROR("SPACING",
43522381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
43532381097bSJoe Perches							$line_fixed = 1;
43542381097bSJoe Perches							$rtrim_before = 1;
43552381097bSJoe Perches						}
43562381097bSJoe Perches					}
4357cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
43583705ce5bSJoe Perches						if (ERROR("SPACING",
43593705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
43603705ce5bSJoe Perches							$line_fixed = 1;
4361b34c648bSJoe Perches							$last_after = $n;
43622381097bSJoe Perches							$space_after = 1;
43632381097bSJoe Perches						}
43642381097bSJoe Perches					}
43652381097bSJoe Perches					if ($rtrim_before || $space_after) {
43662381097bSJoe Perches						if ($rtrim_before) {
43672381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43682381097bSJoe Perches						} else {
43692381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
43702381097bSJoe Perches						}
43712381097bSJoe Perches						if ($space_after) {
43722381097bSJoe Perches							$good .= " ";
43733705ce5bSJoe Perches						}
43740a920b5bSAndy Whitcroft					}
43750a920b5bSAndy Whitcroft
43769c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
437774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
43789c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
43799c0ca6f9SAndy Whitcroft
43809c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
43819c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
43829c0ca6f9SAndy Whitcroft				# unary operator, or a cast
43839c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
438474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
43850d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4386cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
43873705ce5bSJoe Perches						if (ERROR("SPACING",
43883705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4389b34c648bSJoe Perches							if ($n != $last_after + 2) {
4390b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
43913705ce5bSJoe Perches								$line_fixed = 1;
43923705ce5bSJoe Perches							}
43930a920b5bSAndy Whitcroft						}
4394b34c648bSJoe Perches					}
4395a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4396171ae1a4SAndy Whitcroft						# A unary '*' may be const
4397171ae1a4SAndy Whitcroft
4398171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
43993705ce5bSJoe Perches						if (ERROR("SPACING",
44003705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4401b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
44023705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44033705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44043705ce5bSJoe Perches							}
4405b34c648bSJoe Perches							$line_fixed = 1;
44063705ce5bSJoe Perches						}
44070a920b5bSAndy Whitcroft					}
44080a920b5bSAndy Whitcroft
44090a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
44100a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4411773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
44123705ce5bSJoe Perches						if (ERROR("SPACING",
44133705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4414b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44153705ce5bSJoe Perches							$line_fixed = 1;
44163705ce5bSJoe Perches						}
44170a920b5bSAndy Whitcroft					}
4418773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4419773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
44203705ce5bSJoe Perches						if (ERROR("SPACING",
44213705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4422b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44233705ce5bSJoe Perches							$line_fixed = 1;
44243705ce5bSJoe Perches						}
4425653d4876SAndy Whitcroft					}
4426773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
44273705ce5bSJoe Perches						if (ERROR("SPACING",
44283705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4429b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
44303705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44313705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4432773647a0SAndy Whitcroft							}
4433b34c648bSJoe Perches							$line_fixed = 1;
44343705ce5bSJoe Perches						}
44353705ce5bSJoe Perches					}
44360a920b5bSAndy Whitcroft
44370a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
44389c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
44399c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
44409c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4441c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4442c2fdda0dSAndy Whitcroft					 $op eq '%')
44430a920b5bSAndy Whitcroft				{
4444d2e025f3SJoe Perches					if ($check) {
4445d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4446d2e025f3SJoe Perches							if (CHK("SPACING",
4447d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4448d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4449d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4450d2e025f3SJoe Perches								$line_fixed = 1;
4451d2e025f3SJoe Perches							}
4452d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4453d2e025f3SJoe Perches							if (CHK("SPACING",
4454d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4455d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4456d2e025f3SJoe Perches								$line_fixed = 1;
4457d2e025f3SJoe Perches							}
4458d2e025f3SJoe Perches						}
4459d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
44603705ce5bSJoe Perches						if (ERROR("SPACING",
44613705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4462b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4463b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4464b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4465b34c648bSJoe Perches							}
44663705ce5bSJoe Perches							$line_fixed = 1;
44673705ce5bSJoe Perches						}
44680a920b5bSAndy Whitcroft					}
44690a920b5bSAndy Whitcroft
44701f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
44711f65f947SAndy Whitcroft				# terminating a case value or a label.
44721f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
44731f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
44743705ce5bSJoe Perches						if (ERROR("SPACING",
44753705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4476b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44773705ce5bSJoe Perches							$line_fixed = 1;
44783705ce5bSJoe Perches						}
44791f65f947SAndy Whitcroft					}
44801f65f947SAndy Whitcroft
44810a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4482cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
44831f65f947SAndy Whitcroft					my $ok = 0;
44841f65f947SAndy Whitcroft
448522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
44861f65f947SAndy Whitcroft					if (($op eq '<' &&
44871f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
44881f65f947SAndy Whitcroft					    ($op eq '>' &&
44891f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
44901f65f947SAndy Whitcroft					{
44911f65f947SAndy Whitcroft					    	$ok = 1;
44921f65f947SAndy Whitcroft					}
44931f65f947SAndy Whitcroft
4494e0df7e1fSJoe Perches					# for asm volatile statements
4495e0df7e1fSJoe Perches					# ignore a colon with another
4496e0df7e1fSJoe Perches					# colon immediately before or after
4497e0df7e1fSJoe Perches					if (($op eq ':') &&
4498e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4499e0df7e1fSJoe Perches						$ok = 1;
4500e0df7e1fSJoe Perches					}
4501e0df7e1fSJoe Perches
450284731623SJoe Perches					# messages are ERROR, but ?: are CHK
45031f65f947SAndy Whitcroft					if ($ok == 0) {
45040675a8fbSJean Delvare						my $msg_level = \&ERROR;
45050675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
450684731623SJoe Perches
45070675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
45083705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4509b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4510b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4511b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4512b34c648bSJoe Perches							}
45133705ce5bSJoe Perches							$line_fixed = 1;
45143705ce5bSJoe Perches						}
45150a920b5bSAndy Whitcroft					}
451622f2a2efSAndy Whitcroft				}
45174a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
45183705ce5bSJoe Perches
45193705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
45203705ce5bSJoe Perches
45213705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
45220a920b5bSAndy Whitcroft			}
45233705ce5bSJoe Perches
45243705ce5bSJoe Perches			if (($#elements % 2) == 0) {
45253705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
45263705ce5bSJoe Perches			}
45273705ce5bSJoe Perches
4528194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4529194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
45303705ce5bSJoe Perches			}
45313705ce5bSJoe Perches
45323705ce5bSJoe Perches
45330a920b5bSAndy Whitcroft		}
45340a920b5bSAndy Whitcroft
4535786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4536d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4537786b6326SJoe Perches			if (WARN("SPACING",
4538786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4539786b6326SJoe Perches			    $fix) {
4540194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4541786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4542786b6326SJoe Perches			}
4543786b6326SJoe Perches		}
4544786b6326SJoe Perches
4545f0a594c1SAndy Whitcroft# check for multiple assignments
4546f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4547000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4548000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4549f0a594c1SAndy Whitcroft		}
4550f0a594c1SAndy Whitcroft
455122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
455222f2a2efSAndy Whitcroft## # continuation.
455322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
455422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
455522f2a2efSAndy Whitcroft##
455622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
455722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
455822f2a2efSAndy Whitcroft## 			my $ln = $line;
455922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
456022f2a2efSAndy Whitcroft## 			}
456122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4562000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4563000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
456422f2a2efSAndy Whitcroft## 			}
456522f2a2efSAndy Whitcroft## 		}
4566f0a594c1SAndy Whitcroft
45670a920b5bSAndy Whitcroft#need space before brace following if, while, etc
45686b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
45696ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
45703705ce5bSJoe Perches			if (ERROR("SPACING",
45713705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
45723705ce5bSJoe Perches			    $fix) {
45736ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
45743705ce5bSJoe Perches			}
4575de7d4f0eSAndy Whitcroft		}
4576de7d4f0eSAndy Whitcroft
4577c4a62ef9SJoe Perches## # check for blank lines before declarations
4578c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4579c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4580c4a62ef9SJoe Perches##			WARN("SPACING",
4581c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4582c4a62ef9SJoe Perches##		}
4583c4a62ef9SJoe Perches##
4584c4a62ef9SJoe Perches
4585de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4586de7d4f0eSAndy Whitcroft# on the line
4587de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4588d5e616fcSJoe Perches			if (ERROR("SPACING",
4589d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4590d5e616fcSJoe Perches			    $fix) {
4591194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4592d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4593d5e616fcSJoe Perches			}
45940a920b5bSAndy Whitcroft		}
45950a920b5bSAndy Whitcroft
459622f2a2efSAndy Whitcroft# check spacing on square brackets
459722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
45983705ce5bSJoe Perches			if (ERROR("SPACING",
45993705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
46003705ce5bSJoe Perches			    $fix) {
4601194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46023705ce5bSJoe Perches				    s/\[\s+/\[/;
46033705ce5bSJoe Perches			}
460422f2a2efSAndy Whitcroft		}
460522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
46063705ce5bSJoe Perches			if (ERROR("SPACING",
46073705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
46083705ce5bSJoe Perches			    $fix) {
4609194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46103705ce5bSJoe Perches				    s/\s+\]/\]/;
46113705ce5bSJoe Perches			}
461222f2a2efSAndy Whitcroft		}
461322f2a2efSAndy Whitcroft
4614c45dcabdSAndy Whitcroft# check spacing on parentheses
46159c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
46169c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
46173705ce5bSJoe Perches			if (ERROR("SPACING",
46183705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
46193705ce5bSJoe Perches			    $fix) {
4620194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46213705ce5bSJoe Perches				    s/\(\s+/\(/;
46223705ce5bSJoe Perches			}
462322f2a2efSAndy Whitcroft		}
462413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4625c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4626c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
46273705ce5bSJoe Perches			if (ERROR("SPACING",
46283705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
46293705ce5bSJoe Perches			    $fix) {
4630194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46313705ce5bSJoe Perches				    s/\s+\)/\)/;
46323705ce5bSJoe Perches			}
463322f2a2efSAndy Whitcroft		}
463422f2a2efSAndy Whitcroft
4635e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4636e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4637e2826fd0SJoe Perches
4638e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4639ea4acbb1SJoe Perches			my $var = $1;
4640ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4641ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4642ea4acbb1SJoe Perches			    $fix) {
4643ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4644ea4acbb1SJoe Perches			}
4645ea4acbb1SJoe Perches		}
4646ea4acbb1SJoe Perches
4647ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4648ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4649ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4650ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4651ea4acbb1SJoe Perches			my $var = $2;
4652ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4653ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4654ea4acbb1SJoe Perches			    $fix) {
4655ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4656ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4657ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4658ea4acbb1SJoe Perches			}
4659e2826fd0SJoe Perches		}
4660e2826fd0SJoe Perches
466163b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4662a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4663a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
46645b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
466563b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
466663b7c73eSJoe Perches			my $if_stat = $1;
466763b7c73eSJoe Perches			my $test = substr($2, 1, -1);
466863b7c73eSJoe Perches			my $herectx;
466963b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
467063b7c73eSJoe Perches				my $match = $1;
467163b7c73eSJoe Perches				# avoid parentheses around potential macro args
467263b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
467363b7c73eSJoe Perches				if (!defined($herectx)) {
467463b7c73eSJoe Perches					$herectx = $here . "\n";
467563b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
467663b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
467763b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
467863b7c73eSJoe Perches						$herectx .=  $rl . "\n";
467963b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
468063b7c73eSJoe Perches					}
468163b7c73eSJoe Perches				}
468263b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
468363b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
468463b7c73eSJoe Perches			}
468563b7c73eSJoe Perches		}
468663b7c73eSJoe Perches
46870a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
46884a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
46890a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
46903705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
46913705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
46923705ce5bSJoe Perches			    $fix) {
4693194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46943705ce5bSJoe Perches				    s/^(.)\s+/$1/;
46953705ce5bSJoe Perches			}
46960a920b5bSAndy Whitcroft		}
46970a920b5bSAndy Whitcroft
46985b9553abSJoe Perches# return is not a function
4699507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4700c45dcabdSAndy Whitcroft			my $spacing = $1;
47015b57980dSJoe Perches			if ($perl_version_ok &&
47025b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
47035b9553abSJoe Perches				my $value = $1;
47045b9553abSJoe Perches				$value = deparenthesize($value);
47055b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4706000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4707000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
47085b9553abSJoe Perches				}
4709c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4710000d1cc1SJoe Perches				ERROR("SPACING",
4711000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4712c45dcabdSAndy Whitcroft			}
4713c45dcabdSAndy Whitcroft		}
4714507e5141SJoe Perches
4715b43ae21bSJoe Perches# unnecessary return in a void function
4716b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4717b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4718b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4719b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4720b43ae21bSJoe Perches		    $linenr >= 3 &&
4721b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4722b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
47239819cf25SJoe Perches			WARN("RETURN_VOID",
4724b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
47259819cf25SJoe Perches               }
47269819cf25SJoe Perches
4727189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
47285b57980dSJoe Perches		if ($perl_version_ok &&
4729189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4730189248d8SJoe Perches			my $openparens = $1;
4731189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4732189248d8SJoe Perches			my $msg = "";
4733189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4734189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4735189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4736189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4737189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4738189248d8SJoe Perches			}
4739189248d8SJoe Perches		}
4740189248d8SJoe Perches
4741c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4742c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4743c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4744c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
47455b57980dSJoe Perches		if ($perl_version_ok &&
4746c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4747c5595fa2SJoe Perches			my $lead = $1;
4748c5595fa2SJoe Perches			my $const = $2;
4749c5595fa2SJoe Perches			my $comp = $3;
4750c5595fa2SJoe Perches			my $to = $4;
4751c5595fa2SJoe Perches			my $newcomp = $comp;
4752f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4753c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4754c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4755c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4756c5595fa2SJoe Perches			    $fix) {
4757c5595fa2SJoe Perches				if ($comp eq "<") {
4758c5595fa2SJoe Perches					$newcomp = ">";
4759c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4760c5595fa2SJoe Perches					$newcomp = ">=";
4761c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4762c5595fa2SJoe Perches					$newcomp = "<";
4763c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4764c5595fa2SJoe Perches					$newcomp = "<=";
4765c5595fa2SJoe Perches				}
4766c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4767c5595fa2SJoe Perches			}
4768c5595fa2SJoe Perches		}
4769c5595fa2SJoe Perches
4770f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4771f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
477253a3c448SAndy Whitcroft			my $name = $1;
477353a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4774000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4775f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
477653a3c448SAndy Whitcroft			}
477753a3c448SAndy Whitcroft		}
4778c45dcabdSAndy Whitcroft
47790a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
47804a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
47813705ce5bSJoe Perches			if (ERROR("SPACING",
47823705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
47833705ce5bSJoe Perches			    $fix) {
4784194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47853705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
47863705ce5bSJoe Perches			}
47870a920b5bSAndy Whitcroft		}
47880a920b5bSAndy Whitcroft
4789f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4790f5fe35ddSAndy Whitcroft# statements after the conditional.
4791170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
47923e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
47933e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
47943e469cdcSAndy Whitcroft					if (!defined $stat);
4795170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4796170d3a22SAndy Whitcroft						$remain_next, $off_next);
4797170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4798170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4799170d3a22SAndy Whitcroft
4800170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4801170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4802170d3a22SAndy Whitcroft				# then count those as offsets.
4803170d3a22SAndy Whitcroft				my ($whitespace) =
4804170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4805170d3a22SAndy Whitcroft				my $offset =
4806170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4807170d3a22SAndy Whitcroft
4808170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4809170d3a22SAndy Whitcroft								$offset} = 1;
4810170d3a22SAndy Whitcroft			}
4811170d3a22SAndy Whitcroft		}
4812170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4813c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4814170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4815171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
48168905a67cSAndy Whitcroft
4817b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4818000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4819000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
48208905a67cSAndy Whitcroft			}
48218905a67cSAndy Whitcroft
48228905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
48238905a67cSAndy Whitcroft			# conditional.
4824773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
48258905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
482613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
482753210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
482853210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4829773647a0SAndy Whitcroft			{
4830bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4831bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4832bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
483342bdf74cSHidetoshi Seto				my $stat_real = '';
4834bb44ad39SAndy Whitcroft
483542bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
483642bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4837bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4838bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4839bb44ad39SAndy Whitcroft				}
4840bb44ad39SAndy Whitcroft
4841000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4842000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
48438905a67cSAndy Whitcroft			}
48448905a67cSAndy Whitcroft		}
48458905a67cSAndy Whitcroft
484613214adfSAndy Whitcroft# Check for bitwise tests written as boolean
484713214adfSAndy Whitcroft		if ($line =~ /
484813214adfSAndy Whitcroft			(?:
484913214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
485013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
485113214adfSAndy Whitcroft				(?:\&\&|\|\|)
485213214adfSAndy Whitcroft			|
485313214adfSAndy Whitcroft				(?:\&\&|\|\|)
485413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
485513214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
485613214adfSAndy Whitcroft			)/x)
485713214adfSAndy Whitcroft		{
4858000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4859000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
486013214adfSAndy Whitcroft		}
486113214adfSAndy Whitcroft
48628905a67cSAndy Whitcroft# if and else should not have general statements after it
486313214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
486413214adfSAndy Whitcroft			my $s = $1;
486513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
486613214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4867000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4868000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
48690a920b5bSAndy Whitcroft			}
487013214adfSAndy Whitcroft		}
487139667782SAndy Whitcroft# if should not continue a brace
487239667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4873000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4874048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
487539667782SAndy Whitcroft				$herecurr);
487639667782SAndy Whitcroft		}
4877a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4878a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4879a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
48803fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4881a1080bf8SAndy Whitcroft			\s*return\s+
4882a1080bf8SAndy Whitcroft		    )/xg)
4883a1080bf8SAndy Whitcroft		{
4884000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4885000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4886a1080bf8SAndy Whitcroft		}
48870a920b5bSAndy Whitcroft
48880a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
48890a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
48908b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
48910a920b5bSAndy Whitcroft		    $previndent == $indent) {
48928b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
48938b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
48948b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
48958b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
48968b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
48978b8856f4SJoe Perches				my $fixedline = $prevrawline;
48988b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
48998b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
49008b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49018b8856f4SJoe Perches				}
49028b8856f4SJoe Perches				$fixedline = $rawline;
49038b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
49048b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
49058b8856f4SJoe Perches			}
49060a920b5bSAndy Whitcroft		}
49070a920b5bSAndy Whitcroft
49088b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4909c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4910c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4911c2fdda0dSAndy Whitcroft
4912c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4913c2fdda0dSAndy Whitcroft			# conditional.
4914773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4915c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4916c2fdda0dSAndy Whitcroft
4917c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
49188b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
49198b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
49208b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49218b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
49228b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
49238b8856f4SJoe Perches					my $fixedline = $prevrawline;
49248b8856f4SJoe Perches					my $trailing = $rawline;
49258b8856f4SJoe Perches					$trailing =~ s/^\+//;
49268b8856f4SJoe Perches					$trailing = trim($trailing);
49278b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
49288b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49298b8856f4SJoe Perches				}
4930c2fdda0dSAndy Whitcroft			}
4931c2fdda0dSAndy Whitcroft		}
4932c2fdda0dSAndy Whitcroft
493395e2c602SJoe Perches#Specific variable tests
4934323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4935323c1260SJoe Perches			my $var = $1;
493695e2c602SJoe Perches
493795e2c602SJoe Perches#gcc binary extension
493895e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4939d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4940d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4941d5e616fcSJoe Perches				    $fix) {
4942d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4943194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4944d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4945d5e616fcSJoe Perches				}
494695e2c602SJoe Perches			}
494795e2c602SJoe Perches
494895e2c602SJoe Perches#CamelCase
4949807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4950be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
495122735ce8SJoe Perches#Ignore Page<foo> variants
4952807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
495322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4954f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4955f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4956f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
49577e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
49587e781f67SJoe Perches					my $word = $1;
49597e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4960d8b07710SJoe Perches					if ($check) {
4961d8b07710SJoe Perches						seed_camelcase_includes();
4962d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4963d8b07710SJoe Perches							seed_camelcase_file($realfile);
4964d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4965d8b07710SJoe Perches						}
4966d8b07710SJoe Perches					}
49677e781f67SJoe Perches					if (!defined $camelcase{$word}) {
49687e781f67SJoe Perches						$camelcase{$word} = 1;
4969be79794bSJoe Perches						CHK("CAMELCASE",
49707e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
49717e781f67SJoe Perches					}
4972323c1260SJoe Perches				}
4973323c1260SJoe Perches			}
49743445686aSJoe Perches		}
49750a920b5bSAndy Whitcroft
49760a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4977d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4978d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4979d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4980d5e616fcSJoe Perches			    $fix) {
4981194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4982d5e616fcSJoe Perches			}
49830a920b5bSAndy Whitcroft		}
49840a920b5bSAndy Whitcroft
49850e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
49860e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4987c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4988e09dec48SAndy Whitcroft			my $file = "$1.h";
4989e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4990e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4991e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
49927840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4993c45dcabdSAndy Whitcroft			{
49940e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
49950e212e0aSFabian Frederick				if ($asminclude > 0) {
4996e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4997000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4998000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4999e09dec48SAndy Whitcroft					} else {
5000000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5001000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5002e09dec48SAndy Whitcroft					}
50030a920b5bSAndy Whitcroft				}
50040a920b5bSAndy Whitcroft			}
50050e212e0aSFabian Frederick		}
50060a920b5bSAndy Whitcroft
5007653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5008653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5009cf655043SAndy Whitcroft# in a known good container
5010b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5011b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5012d8aaf121SAndy Whitcroft			my $ln = $linenr;
5013d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5014c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5015c45dcabdSAndy Whitcroft			my $ctx = '';
501608a2843eSJoe Perches			my $has_flow_statement = 0;
501708a2843eSJoe Perches			my $has_arg_concat = 0;
5018c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5019f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5020f74bd194SAndy Whitcroft			$ctx = $dstat;
5021c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5022a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5023c45dcabdSAndy Whitcroft
502408a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
502562e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
502608a2843eSJoe Perches
5027f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5028f59b64bfSJoe Perches			my $define_args = $1;
5029f59b64bfSJoe Perches			my $define_stmt = $dstat;
5030f59b64bfSJoe Perches			my @def_args = ();
5031f59b64bfSJoe Perches
5032f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5033f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5034f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
50358c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5036f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5037f59b64bfSJoe Perches			}
5038f59b64bfSJoe Perches
5039292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5040c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5041c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5042c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5043c45dcabdSAndy Whitcroft
5044c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5045bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5046bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
50476b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5048bf30d6edSAndy Whitcroft			{
5049c45dcabdSAndy Whitcroft			}
5050c45dcabdSAndy Whitcroft
5051e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
505233acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
505333acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5054e45bab8eSAndy Whitcroft			{
5055e45bab8eSAndy Whitcroft			}
5056e45bab8eSAndy Whitcroft
505742e15293SJoe Perches			# Make asm volatile uses seem like a generic function
505842e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
505942e15293SJoe Perches
5060c45dcabdSAndy Whitcroft			my $exceptions = qr{
5061c45dcabdSAndy Whitcroft				$Declare|
5062c45dcabdSAndy Whitcroft				module_param_named|
5063a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5064c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5065c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5066383099fdSAndy Whitcroft				__typeof__\(|
506722fd2d3eSStefani Seibold				union|
506822fd2d3eSStefani Seibold				struct|
5069ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
50706b10df42SVladimir Zapolskiy				^\"|\"$|
50716b10df42SVladimir Zapolskiy				^\[
5072c45dcabdSAndy Whitcroft			}x;
50735eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5074f59b64bfSJoe Perches
5075f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5076f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5077e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5078f59b64bfSJoe Perches
5079f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5080f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5081f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
50823cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5083356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5084f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5085f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5086e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
508772f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5088f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5089f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5090f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
50914e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5092f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5093c45dcabdSAndy Whitcroft			{
5094e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5095e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5096e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5097e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5098f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5099f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5100f74bd194SAndy Whitcroft				} else {
5101000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5102388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5103d8aaf121SAndy Whitcroft				}
5104f59b64bfSJoe Perches
5105f59b64bfSJoe Perches			}
51065207649bSJoe Perches
51075207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
51085207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
51095207649bSJoe Perches			my $first = 1;
51105207649bSJoe Perches			$define_stmt = "";
51115207649bSJoe Perches			foreach my $l (@stmt_array) {
51125207649bSJoe Perches				$l =~ s/\\$//;
51135207649bSJoe Perches				if ($first) {
51145207649bSJoe Perches					$define_stmt = $l;
51155207649bSJoe Perches					$first = 0;
51165207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
51175207649bSJoe Perches					$define_stmt .= substr($l, 1);
51185207649bSJoe Perches				}
51195207649bSJoe Perches			}
51205207649bSJoe Perches			$define_stmt =~ s/$;//g;
51215207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
51225207649bSJoe Perches			$define_stmt = trim($define_stmt);
51235207649bSJoe Perches
5124f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5125f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5126f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
51279192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
51287fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
51297fe528a2SJoe Perches				$tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
51307fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
51317fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5132d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5133f59b64bfSJoe Perches				if ($use_cnt > 1) {
5134f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5135f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5136f59b64bfSJoe Perches				    }
51379192d41aSJoe Perches# check if any macro arguments may have other precedence issues
51387fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
51399192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
51409192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
51419192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
51429192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
51439192d41aSJoe Perches				}
51440a920b5bSAndy Whitcroft			}
51455023d347SJoe Perches
514608a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
514708a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
514808a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
514908a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5150e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
515108a2843eSJoe Perches
515208a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
515308a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
515408a2843eSJoe Perches			}
515508a2843eSJoe Perches
5156481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
51575023d347SJoe Perches
51585023d347SJoe Perches		} else {
51595023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5160481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5161481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
51625023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
51635023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
51645023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
51655023d347SJoe Perches			}
5166653d4876SAndy Whitcroft		}
51670a920b5bSAndy Whitcroft
5168b13edf7fSJoe Perches# do {} while (0) macro tests:
5169b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5170b13edf7fSJoe Perches# macro should not end with a semicolon
51715b57980dSJoe Perches		if ($perl_version_ok &&
5172b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5173b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5174b13edf7fSJoe Perches			my $ln = $linenr;
5175b13edf7fSJoe Perches			my $cnt = $realcnt;
5176b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5177b13edf7fSJoe Perches			my $ctx = '';
5178b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5179b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5180b13edf7fSJoe Perches			$ctx = $dstat;
5181b13edf7fSJoe Perches
5182b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
51831b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5184b13edf7fSJoe Perches
5185b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5186b13edf7fSJoe Perches				my $stmts = $2;
5187b13edf7fSJoe Perches				my $semis = $3;
5188b13edf7fSJoe Perches
5189b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5190b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5191e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5192b13edf7fSJoe Perches
5193ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5194ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5195b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5196b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5197b13edf7fSJoe Perches				}
5198b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5199b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5200b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5201b13edf7fSJoe Perches				}
5202f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5203f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5204f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5205e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5206f5ef95b1SJoe Perches
5207f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5208f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5209b13edf7fSJoe Perches			}
5210b13edf7fSJoe Perches		}
5211b13edf7fSJoe Perches
5212f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
521313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
521413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5215cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
521613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5217cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5218cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5219aad4f614SJoe Perches				my @allowed = ();
5220aad4f614SJoe Perches				my $allow = 0;
522113214adfSAndy Whitcroft				my $seen = 0;
5222773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5223cf655043SAndy Whitcroft				my $ln = $linenr - 1;
522413214adfSAndy Whitcroft				for my $chunk (@chunks) {
522513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
522613214adfSAndy Whitcroft
5227773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5228773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5229773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5230773647a0SAndy Whitcroft
5231aad4f614SJoe Perches					$allowed[$allow] = 0;
5232773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5233773647a0SAndy Whitcroft
5234773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5235773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5236773647a0SAndy Whitcroft
5237773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5238cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5239cf655043SAndy Whitcroft
5240773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
524113214adfSAndy Whitcroft
524213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
524313214adfSAndy Whitcroft
5244aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5245cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5246cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5247aad4f614SJoe Perches						$allowed[$allow] = 1;
524813214adfSAndy Whitcroft					}
524913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5250cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5251aad4f614SJoe Perches						$allowed[$allow] = 1;
525213214adfSAndy Whitcroft					}
5253cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5254cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5255aad4f614SJoe Perches						$allowed[$allow] = 1;
525613214adfSAndy Whitcroft					}
5257aad4f614SJoe Perches					$allow++;
525813214adfSAndy Whitcroft				}
5259aad4f614SJoe Perches				if ($seen) {
5260aad4f614SJoe Perches					my $sum_allowed = 0;
5261aad4f614SJoe Perches					foreach (@allowed) {
5262aad4f614SJoe Perches						$sum_allowed += $_;
5263aad4f614SJoe Perches					}
5264aad4f614SJoe Perches					if ($sum_allowed == 0) {
5265000d1cc1SJoe Perches						WARN("BRACES",
5266000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5267aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5268aad4f614SJoe Perches						 $seen != $allow) {
5269aad4f614SJoe Perches						CHK("BRACES",
5270aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5271aad4f614SJoe Perches					}
527213214adfSAndy Whitcroft				}
527313214adfSAndy Whitcroft			}
527413214adfSAndy Whitcroft		}
5275773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
527613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5277cf655043SAndy Whitcroft			my $allowed = 0;
5278f0a594c1SAndy Whitcroft
5279cf655043SAndy Whitcroft			# Check the pre-context.
5280cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5281cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5282cf655043SAndy Whitcroft				$allowed = 1;
5283f0a594c1SAndy Whitcroft			}
5284773647a0SAndy Whitcroft
5285773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5286773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5287773647a0SAndy Whitcroft
5288cf655043SAndy Whitcroft			# Check the condition.
5289cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5290773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5291cf655043SAndy Whitcroft			if (defined $cond) {
5292773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5293cf655043SAndy Whitcroft			}
5294cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5295cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5296cf655043SAndy Whitcroft				$allowed = 1;
5297cf655043SAndy Whitcroft			}
5298cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5299cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5300cf655043SAndy Whitcroft				$allowed = 1;
5301cf655043SAndy Whitcroft			}
5302cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5303cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5304cf655043SAndy Whitcroft				$allowed = 1;
5305cf655043SAndy Whitcroft			}
5306cf655043SAndy Whitcroft			# Check the post-context.
5307cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5308cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5309cf655043SAndy Whitcroft				if (defined $cond) {
5310773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5311cf655043SAndy Whitcroft				}
5312cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5313cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5314cf655043SAndy Whitcroft					$allowed = 1;
5315cf655043SAndy Whitcroft				}
5316cf655043SAndy Whitcroft			}
5317cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5318f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5319e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5320cf655043SAndy Whitcroft
5321000d1cc1SJoe Perches				WARN("BRACES",
5322000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5323f0a594c1SAndy Whitcroft			}
5324f0a594c1SAndy Whitcroft		}
5325f0a594c1SAndy Whitcroft
5326e4c5babdSJoe Perches# check for single line unbalanced braces
532795330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
532895330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5329e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5330e4c5babdSJoe Perches		}
5331e4c5babdSJoe Perches
53320979ae66SJoe Perches# check for unnecessary blank lines around braces
533377b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5334f8e58219SJoe Perches			if (CHK("BRACES",
5335f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5336f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5337f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5338f8e58219SJoe Perches			}
53390979ae66SJoe Perches		}
534077b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5341f8e58219SJoe Perches			if (CHK("BRACES",
5342f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5343f8e58219SJoe Perches			    $fix) {
5344f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5345f8e58219SJoe Perches			}
53460979ae66SJoe Perches		}
53470979ae66SJoe Perches
53484a0df2efSAndy Whitcroft# no volatiles please
53496c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
53506c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5351000d1cc1SJoe Perches			WARN("VOLATILE",
53528c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
53534a0df2efSAndy Whitcroft		}
53544a0df2efSAndy Whitcroft
53555e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
53565e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
53575e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
53585e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
535933acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
53605e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
53615e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
53625e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
53635e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
53645e4f6ba5SJoe Perches				     $fix &&
53655e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
53665e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
53675e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
53685e4f6ba5SJoe Perches				my $comma_close = "";
53695e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
53705e4f6ba5SJoe Perches					$comma_close = $1;
53715e4f6ba5SJoe Perches				}
53725e4f6ba5SJoe Perches
53735e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
53745e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
53755e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
53765e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
53775e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
53785e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
53795e4f6ba5SJoe Perches				$fixedline = $rawline;
53805e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
53815e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
53825e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
53835e4f6ba5SJoe Perches				}
53845e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
53855e4f6ba5SJoe Perches			}
53865e4f6ba5SJoe Perches		}
53875e4f6ba5SJoe Perches
53885e4f6ba5SJoe Perches# check for missing a space in a string concatenation
53895e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
53905e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
53915e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
53925e4f6ba5SJoe Perches		}
53935e4f6ba5SJoe Perches
539477cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5395e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5396e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5397e4b7d309SJoe Perches# function declarations
539877cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
539977cb8546SJoe Perches		    defined($context_function) &&
5400e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5401e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
540277cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5403e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
540477cb8546SJoe Perches		}
540577cb8546SJoe Perches
54065e4f6ba5SJoe Perches# check for spaces before a quoted newline
54075e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
54085e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
54095e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
54105e4f6ba5SJoe Perches			    $fix) {
54115e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
54125e4f6ba5SJoe Perches			}
54135e4f6ba5SJoe Perches
54145e4f6ba5SJoe Perches		}
54155e4f6ba5SJoe Perches
5416f17dba4fSJoe Perches# concatenated string without spaces between elements
541779682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
541879682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
541979682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
542079682c0cSJoe Perches			    $fix) {
542179682c0cSJoe Perches				while ($line =~ /($String)/g) {
542279682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
542379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
542479682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
542579682c0cSJoe Perches				}
542679682c0cSJoe Perches			}
5427f17dba4fSJoe Perches		}
5428f17dba4fSJoe Perches
542990ad30e5SJoe Perches# uncoalesced string fragments
543033acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
543179682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
543279682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
543379682c0cSJoe Perches			    $fix) {
543479682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
543579682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
543679682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
543779682c0cSJoe Perches				}
543879682c0cSJoe Perches			}
543990ad30e5SJoe Perches		}
544090ad30e5SJoe Perches
5441522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5442522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5443522b837cSAlexey Dobriyan		my $show_Z = 1;
54445e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5445522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
54465e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5447522b837cSAlexey Dobriyan			# check for %L
5448522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
54495e4f6ba5SJoe Perches				WARN("PRINTF_L",
5450522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5451522b837cSAlexey Dobriyan				$show_L = 0;
54525e4f6ba5SJoe Perches			}
5453522b837cSAlexey Dobriyan			# check for %Z
5454522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5455522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5456522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5457522b837cSAlexey Dobriyan				$show_Z = 0;
5458522b837cSAlexey Dobriyan			}
5459522b837cSAlexey Dobriyan			# check for 0x<decimal>
5460522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5461522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
54626e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
54636e300757SJoe Perches			}
54645e4f6ba5SJoe Perches		}
54655e4f6ba5SJoe Perches
54665e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
54673f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
54685e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
54695e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
54705e4f6ba5SJoe Perches		}
54715e4f6ba5SJoe Perches
547200df344fSAndy Whitcroft# warn about #if 0
5473c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
547460f89010SPrakruthi Deepak Heragu			WARN("IF_0",
547560f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
547660f89010SPrakruthi Deepak Heragu		}
547760f89010SPrakruthi Deepak Heragu
547860f89010SPrakruthi Deepak Heragu# warn about #if 1
547960f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
548060f89010SPrakruthi Deepak Heragu			WARN("IF_1",
548160f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
54824a0df2efSAndy Whitcroft		}
54834a0df2efSAndy Whitcroft
548403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
548503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5486100425deSJoe Perches			my $tested = quotemeta($1);
5487100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5488100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5489100425deSJoe Perches				my $func = $1;
5490100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5491100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5492100425deSJoe Perches				    $fix) {
5493100425deSJoe Perches					my $do_fix = 1;
5494100425deSJoe Perches					my $leading_tabs = "";
5495100425deSJoe Perches					my $new_leading_tabs = "";
5496100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5497100425deSJoe Perches						$leading_tabs = $1;
5498100425deSJoe Perches					} else {
5499100425deSJoe Perches						$do_fix = 0;
5500100425deSJoe Perches					}
5501100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5502100425deSJoe Perches						$new_leading_tabs = $1;
5503100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5504100425deSJoe Perches							$do_fix = 0;
5505100425deSJoe Perches						}
5506100425deSJoe Perches					} else {
5507100425deSJoe Perches						$do_fix = 0;
5508100425deSJoe Perches					}
5509100425deSJoe Perches					if ($do_fix) {
5510100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5511100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5512100425deSJoe Perches					}
5513100425deSJoe Perches				}
55144c432a8fSGreg Kroah-Hartman			}
55154c432a8fSGreg Kroah-Hartman		}
5516f0a594c1SAndy Whitcroft
5517ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5518ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5519ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5520ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5521ebfdc409SJoe Perches		    $linenr > 3) {
5522ebfdc409SJoe Perches			my $testval = $2;
5523ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5524ebfdc409SJoe Perches
5525ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5526ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5527ebfdc409SJoe Perches
5528fb0d0e08SJoe 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)/) {
5529ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5530ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5531ebfdc409SJoe Perches			}
5532ebfdc409SJoe Perches		}
5533ebfdc409SJoe Perches
5534f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5535dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5536f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5537f78d98f6SJoe Perches			my $level = $1;
5538f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5539f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5540f78d98f6SJoe Perches			    $fix) {
5541f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5542f78d98f6SJoe Perches			}
5543f78d98f6SJoe Perches		}
5544f78d98f6SJoe Perches
554545c55e92SJoe Perches# check for logging continuations
554645c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
554745c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
554845c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
554945c55e92SJoe Perches		}
555045c55e92SJoe Perches
5551abb08a53SJoe Perches# check for mask then right shift without a parentheses
55525b57980dSJoe Perches		if ($perl_version_ok &&
5553abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5554abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5555abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5556abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5557abb08a53SJoe Perches		}
5558abb08a53SJoe Perches
5559b75ac618SJoe Perches# check for pointer comparisons to NULL
55605b57980dSJoe Perches		if ($perl_version_ok) {
5561b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5562b75ac618SJoe Perches				my $val = $1;
5563b75ac618SJoe Perches				my $equal = "!";
5564b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5565b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5566b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5567b75ac618SJoe Perches					    $fix) {
5568b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5569b75ac618SJoe Perches				}
5570b75ac618SJoe Perches			}
5571b75ac618SJoe Perches		}
5572b75ac618SJoe Perches
55738716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
55748716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
55758716de38SJoe Perches			my $attr = $1;
55768716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
55778716de38SJoe Perches				my $ptr = $1;
55788716de38SJoe Perches				my $var = $2;
55798716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
55808716de38SJoe Perches				      ERROR("MISPLACED_INIT",
55818716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
55828716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
55838716de38SJoe Perches				      WARN("MISPLACED_INIT",
55848716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
55858716de38SJoe Perches				    $fix) {
5586194f66fcSJoe 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;
55878716de38SJoe Perches				}
55888716de38SJoe Perches			}
55898716de38SJoe Perches		}
55908716de38SJoe Perches
5591e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5592e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5593e970b884SJoe Perches			my $attr = $1;
5594e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5595e970b884SJoe Perches			my $attr_prefix = $1;
5596e970b884SJoe Perches			my $attr_type = $2;
5597e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5598e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5599e970b884SJoe Perches			    $fix) {
5600194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5601e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5602e970b884SJoe Perches			}
5603e970b884SJoe Perches		}
5604e970b884SJoe Perches
5605e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5606e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5607e970b884SJoe Perches			my $attr = $1;
5608e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5609e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5610e970b884SJoe Perches			    $fix) {
5611194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5612e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5613e970b884SJoe Perches				$lead = rtrim($1);
5614e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5615e970b884SJoe Perches				$lead = "${lead}const ";
5616194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5617e970b884SJoe Perches			}
5618e970b884SJoe Perches		}
5619e970b884SJoe Perches
5620c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5621c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5622c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5623c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5624c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5625c17893c7SJoe Perches			    $fix) {
5626c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5627c17893c7SJoe Perches			}
5628c17893c7SJoe Perches		}
5629c17893c7SJoe Perches
5630fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5631fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5632fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5633fbdb8138SJoe Perches			my $constant_func = $1;
5634fbdb8138SJoe Perches			my $func = $constant_func;
5635fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5636fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5637fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5638fbdb8138SJoe Perches			    $fix) {
5639194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5640fbdb8138SJoe Perches			}
5641fbdb8138SJoe Perches		}
5642fbdb8138SJoe Perches
56431a15a250SPatrick Pannuto# prefer usleep_range over udelay
564437581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
564543c1d77cSJoe Perches			my $delay = $1;
56461a15a250SPatrick Pannuto			# ignore udelay's < 10, however
564743c1d77cSJoe Perches			if (! ($delay < 10) ) {
5648000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
564943c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
565043c1d77cSJoe Perches			}
565143c1d77cSJoe Perches			if ($delay > 2000) {
565243c1d77cSJoe Perches				WARN("LONG_UDELAY",
565343c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
56541a15a250SPatrick Pannuto			}
56551a15a250SPatrick Pannuto		}
56561a15a250SPatrick Pannuto
565709ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
565809ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
565909ef8725SPatrick Pannuto			if ($1 < 20) {
5660000d1cc1SJoe Perches				WARN("MSLEEP",
566143c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
566209ef8725SPatrick Pannuto			}
566309ef8725SPatrick Pannuto		}
566409ef8725SPatrick Pannuto
566536ec1939SJoe Perches# check for comparisons of jiffies
566636ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
566736ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
566836ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
566936ec1939SJoe Perches		}
567036ec1939SJoe Perches
56719d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
56729d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
56739d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
56749d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
56759d7a34a5SJoe Perches		}
56769d7a34a5SJoe Perches
567700df344fSAndy Whitcroft# warn about #ifdefs in C files
5678c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
567900df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
568000df344fSAndy Whitcroft#			print "$herecurr";
568100df344fSAndy Whitcroft#			$clean = 0;
568200df344fSAndy Whitcroft#		}
568300df344fSAndy Whitcroft
568422f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5685c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
56863705ce5bSJoe Perches			if (ERROR("SPACING",
56873705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
56883705ce5bSJoe Perches			    $fix) {
5689194f66fcSJoe Perches				$fixed[$fixlinenr] =~
56903705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
56913705ce5bSJoe Perches			}
56923705ce5bSJoe Perches
569322f2a2efSAndy Whitcroft		}
569422f2a2efSAndy Whitcroft
56954a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5696171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5697171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
56984a0df2efSAndy Whitcroft			my $which = $1;
56994a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5700000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5701000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
57024a0df2efSAndy Whitcroft			}
57034a0df2efSAndy Whitcroft		}
57044a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5705402c2553SMichael S. Tsirkin
5706402c2553SMichael S. Tsirkin		my $barriers = qr{
5707402c2553SMichael S. Tsirkin			mb|
5708402c2553SMichael S. Tsirkin			rmb|
5709402c2553SMichael S. Tsirkin			wmb|
5710402c2553SMichael S. Tsirkin			read_barrier_depends
5711402c2553SMichael S. Tsirkin		}x;
5712402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5713402c2553SMichael S. Tsirkin			mb__before_atomic|
5714402c2553SMichael S. Tsirkin			mb__after_atomic|
5715402c2553SMichael S. Tsirkin			store_release|
5716402c2553SMichael S. Tsirkin			load_acquire|
5717402c2553SMichael S. Tsirkin			store_mb|
5718402c2553SMichael S. Tsirkin			(?:$barriers)
5719402c2553SMichael S. Tsirkin		}x;
5720402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5721402c2553SMichael S. Tsirkin			(?:$barriers)|
572243e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
572343e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5724402c2553SMichael S. Tsirkin		}x;
5725402c2553SMichael S. Tsirkin
5726402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
57274a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5728c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5729000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
57304a0df2efSAndy Whitcroft			}
57314a0df2efSAndy Whitcroft		}
57323ad81779SPaul E. McKenney
5733f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5734f4073b0fSMichael S. Tsirkin
5735f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5736f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5737f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5738f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5739f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5740f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5741f4073b0fSMichael S. Tsirkin		}
5742f4073b0fSMichael S. Tsirkin
5743cb426e99SJoe Perches# check for waitqueue_active without a comment.
5744cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5745cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5746cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5747cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5748cb426e99SJoe Perches			}
5749cb426e99SJoe Perches		}
57503ad81779SPaul E. McKenney
575191db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
575291db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
575391db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
575491db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
575591db2592SPaul E. McKenney		}
575691db2592SPaul E. McKenney
57574a0df2efSAndy Whitcroft# check of hardware specific defines
5758c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5759000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5760000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
57610a920b5bSAndy Whitcroft		}
5762653d4876SAndy Whitcroft
5763596ed45bSJoe Perches# check that the storage class is not after a type
5764596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5765000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5766596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5767596ed45bSJoe Perches		}
5768596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5769596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5770596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5771596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5772596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5773596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5774596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5775d4977c78STobias Klauser		}
5776d4977c78STobias Klauser
5777de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5778de7d4f0eSAndy Whitcroft# storage class and type.
57799c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
57809c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5781000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5782000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5783de7d4f0eSAndy Whitcroft		}
5784de7d4f0eSAndy Whitcroft
57858905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
57862b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
57872b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5788d5e616fcSJoe Perches			if (WARN("INLINE",
5789d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5790d5e616fcSJoe Perches			    $fix) {
5791194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5792d5e616fcSJoe Perches
5793d5e616fcSJoe Perches			}
57948905a67cSAndy Whitcroft		}
57958905a67cSAndy Whitcroft
57963d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
57972b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
57982b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5799000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5800000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
58013d130fd0SJoe Perches		}
58023d130fd0SJoe Perches
580339b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
58042b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58052b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5806000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5807000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
580839b7e287SJoe Perches		}
580939b7e287SJoe Perches
58105f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
58112b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58122b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5813d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5814d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5815d5e616fcSJoe Perches			    $fix) {
5816194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5817d5e616fcSJoe Perches
5818d5e616fcSJoe Perches			}
58195f14d3bdSJoe Perches		}
58205f14d3bdSJoe Perches
58216061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
58222b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58232b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5824d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5825d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5826d5e616fcSJoe Perches			    $fix) {
5827194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5828d5e616fcSJoe Perches			}
58296061d949SJoe Perches		}
58306061d949SJoe Perches
5831619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
58325b57980dSJoe Perches		if ($perl_version_ok &&
5833619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5834619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5835619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5836619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5837619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5838619a908aSJoe Perches		}
5839619a908aSJoe Perches
5840fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5841e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5842fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5843e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5844e6176fa4SJoe Perches			my $type = $1;
5845e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5846e6176fa4SJoe Perches				$type = $1;
5847e6176fa4SJoe Perches				my $kernel_type = 'u';
5848e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5849e6176fa4SJoe Perches				$type =~ /(\d+)/;
5850e6176fa4SJoe Perches				$kernel_type .= $1;
5851e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5852e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5853e6176fa4SJoe Perches				    $fix) {
5854e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5855e6176fa4SJoe Perches				}
5856e6176fa4SJoe Perches			}
5857e6176fa4SJoe Perches		}
5858e6176fa4SJoe Perches
5859938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5860938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5861938224b5SJoe Perches			my $cast = $1;
5862938224b5SJoe Perches			my $const = $2;
5863938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5864938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5865938224b5SJoe Perches			    $fix) {
5866938224b5SJoe Perches				my $suffix = "";
5867938224b5SJoe Perches				my $newconst = $const;
5868938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5869938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5870938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5871938224b5SJoe Perches					$suffix .= 'LL';
5872938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5873938224b5SJoe Perches					$suffix .= 'L';
5874938224b5SJoe Perches				}
5875938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5876938224b5SJoe Perches			}
5877938224b5SJoe Perches		}
5878938224b5SJoe Perches
58798f53a9b8SJoe Perches# check for sizeof(&)
58808f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5881000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5882000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
58838f53a9b8SJoe Perches		}
58848f53a9b8SJoe Perches
588566c80b60SJoe Perches# check for sizeof without parenthesis
588666c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5887d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5888d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5889d5e616fcSJoe Perches			    $fix) {
5890194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5891d5e616fcSJoe Perches			}
589266c80b60SJoe Perches		}
589366c80b60SJoe Perches
589488982feaSJoe Perches# check for struct spinlock declarations
589588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
589688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
589788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
589888982feaSJoe Perches		}
589988982feaSJoe Perches
5900a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
590106668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5902a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5903caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5904caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5905d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5906d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5907d5e616fcSJoe Perches				    $fix) {
5908194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5909d5e616fcSJoe Perches				}
5910a6962d72SJoe Perches			}
5911a6962d72SJoe Perches		}
5912a6962d72SJoe Perches
59130b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
59145b57980dSJoe Perches		if ($perl_version_ok &&
59150b523769SJoe Perches		    defined $stat &&
59160b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
59170b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
5918e3c6bc95STobin C. Harding			my $stat_real;
5919e3c6bc95STobin C. Harding
59200b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
59210b523769SJoe Perches			$lc = $lc + $linenr;
59220b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
5923ffe07513SJoe Perches				my $specifier;
5924ffe07513SJoe Perches				my $extension;
5925ffe07513SJoe Perches				my $bad_specifier = "";
59260b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
59270b523769SJoe Perches				$fmt =~ s/%%//g;
5928e3c6bc95STobin C. Harding
5929e3c6bc95STobin C. Harding				while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5930e3c6bc95STobin C. Harding					$specifier = $1;
5931e3c6bc95STobin C. Harding					$extension = $2;
5932e3c6bc95STobin C. Harding					if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5933e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
59340b523769SJoe Perches						last;
59350b523769SJoe Perches					}
5936e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
5937e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
5938e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
59390b523769SJoe Perches						}
5940e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
5941e3c6bc95STobin 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");
5942e3c6bc95STobin C. Harding					}
5943e3c6bc95STobin C. Harding				}
5944e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
59452a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
59461df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
59471df7338aSSergey Senozhatsky					my $use = "";
5948e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
59491df7338aSSergey Senozhatsky						$ext_type = "Deprecated";
59501df7338aSSergey Senozhatsky						$use = " - use %pS instead";
5951e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
59521df7338aSSergey Senozhatsky					}
59532a9f9d85STobin C. Harding
59540b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
5955e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5956e3c6bc95STobin C. Harding				}
59570b523769SJoe Perches			}
59580b523769SJoe Perches		}
59590b523769SJoe Perches
5960554e165cSAndy Whitcroft# Check for misused memsets
59615b57980dSJoe Perches		if ($perl_version_ok &&
5962d1fe9c09SJoe Perches		    defined $stat &&
59639e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5964554e165cSAndy Whitcroft
5965d7c76ba7SJoe Perches			my $ms_addr = $2;
5966d1fe9c09SJoe Perches			my $ms_val = $7;
5967d1fe9c09SJoe Perches			my $ms_size = $12;
5968d7c76ba7SJoe Perches
5969554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5970554e165cSAndy Whitcroft				ERROR("MEMSET",
5971d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5972554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5973554e165cSAndy Whitcroft				WARN("MEMSET",
5974d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5975d7c76ba7SJoe Perches			}
5976d7c76ba7SJoe Perches		}
5977d7c76ba7SJoe Perches
597898a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
59795b57980dSJoe Perches#		if ($perl_version_ok &&
5980f333195dSJoe Perches#		    defined $stat &&
5981f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5982f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
5983f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5984f333195dSJoe Perches#			    $fix) {
5985f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5986f333195dSJoe Perches#			}
5987f333195dSJoe Perches#		}
598898a9bba5SJoe Perches
5989b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
59905b57980dSJoe Perches#		if ($perl_version_ok &&
5991f333195dSJoe Perches#		    defined $stat &&
5992f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5993f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
5994f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5995f333195dSJoe Perches#		}
5996b6117d17SMateusz Kulikowski
59978617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
59988617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
59995b57980dSJoe Perches#		if ($perl_version_ok &&
6000f333195dSJoe Perches#		    defined $stat &&
6001f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6002f333195dSJoe Perches#
6003f333195dSJoe Perches#			my $ms_val = $7;
6004f333195dSJoe Perches#
6005f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6006f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6007f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6008f333195dSJoe Perches#				    $fix) {
6009f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6010f333195dSJoe Perches#				}
6011f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6012f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6013f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6014f333195dSJoe Perches#				    $fix) {
6015f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6016f333195dSJoe Perches#				}
6017f333195dSJoe Perches#			}
6018f333195dSJoe Perches#		}
60198617cd09SMateusz Kulikowski
6020d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
60215b57980dSJoe Perches		if ($perl_version_ok &&
6022d1fe9c09SJoe Perches		    defined $stat &&
6023d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6024d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6025d7c76ba7SJoe Perches				my $call = $1;
6026d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6027d7c76ba7SJoe Perches				my $arg1 = $3;
6028d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6029d1fe9c09SJoe Perches				my $arg2 = $8;
6030d7c76ba7SJoe Perches				my $cast;
6031d7c76ba7SJoe Perches
6032d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6033d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6034d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6035d7c76ba7SJoe Perches					$cast = $cast1;
6036d7c76ba7SJoe Perches				} else {
6037d7c76ba7SJoe Perches					$cast = $cast2;
6038d7c76ba7SJoe Perches				}
6039d7c76ba7SJoe Perches				WARN("MINMAX",
6040d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6041554e165cSAndy Whitcroft			}
6042554e165cSAndy Whitcroft		}
6043554e165cSAndy Whitcroft
60444a273195SJoe Perches# check usleep_range arguments
60455b57980dSJoe Perches		if ($perl_version_ok &&
60464a273195SJoe Perches		    defined $stat &&
60474a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
60484a273195SJoe Perches			my $min = $1;
60494a273195SJoe Perches			my $max = $7;
60504a273195SJoe Perches			if ($min eq $max) {
60514a273195SJoe Perches				WARN("USLEEP_RANGE",
60524a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
60534a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
60544a273195SJoe Perches				 $min > $max) {
60554a273195SJoe Perches				WARN("USLEEP_RANGE",
60564a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
60574a273195SJoe Perches			}
60584a273195SJoe Perches		}
60594a273195SJoe Perches
6060823b794cSJoe Perches# check for naked sscanf
60615b57980dSJoe Perches		if ($perl_version_ok &&
6062823b794cSJoe Perches		    defined $stat &&
60636c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6064823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6065823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6066823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6067823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6068823b794cSJoe Perches			$lc = $lc + $linenr;
60692a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6070823b794cSJoe Perches			WARN("NAKED_SSCANF",
6071823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6072823b794cSJoe Perches		}
6073823b794cSJoe Perches
6074afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
60755b57980dSJoe Perches		if ($perl_version_ok &&
6076afc819abSJoe Perches		    defined $stat &&
6077afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6078afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6079afc819abSJoe Perches			$lc = $lc + $linenr;
60802a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6081afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6082afc819abSJoe Perches				my $format = $6;
6083afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6084afc819abSJoe Perches				if ($count == 1 &&
6085afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6086afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6087afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6088afc819abSJoe Perches				}
6089afc819abSJoe Perches			}
6090afc819abSJoe Perches		}
6091afc819abSJoe Perches
609270dc8a48SJoe Perches# check for new externs in .h files.
609370dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
609470dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6095d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
609670dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
609770dc8a48SJoe Perches			    $fix) {
6098194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
609970dc8a48SJoe Perches			}
610070dc8a48SJoe Perches		}
610170dc8a48SJoe Perches
6102de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6103171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6104c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6105171ae1a4SAndy Whitcroft		{
6106c45dcabdSAndy Whitcroft			my $function_name = $1;
6107c45dcabdSAndy Whitcroft			my $paren_space = $2;
6108171ae1a4SAndy Whitcroft
6109171ae1a4SAndy Whitcroft			my $s = $stat;
6110171ae1a4SAndy Whitcroft			if (defined $cond) {
6111171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6112171ae1a4SAndy Whitcroft			}
6113c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6114c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6115c45dcabdSAndy Whitcroft			{
6116000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6117000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6118de7d4f0eSAndy Whitcroft			}
6119de7d4f0eSAndy Whitcroft
6120171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6121000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6122000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6123171ae1a4SAndy Whitcroft			}
61249c9ba34eSAndy Whitcroft
61259c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
61269c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
61279c9ba34eSAndy Whitcroft		{
6128000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6129000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6130171ae1a4SAndy Whitcroft		}
6131171ae1a4SAndy Whitcroft
6132a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6133a0ad7596SJoe Perches		if (defined $stat &&
613425bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6135ca0d8929SJoe Perches		    $1 ne "void") {
6136ca0d8929SJoe Perches			my $args = trim($1);
6137ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6138ca0d8929SJoe Perches				my $arg = trim($1);
6139ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6140ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6141ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6142ca0d8929SJoe Perches				}
6143ca0d8929SJoe Perches			}
6144ca0d8929SJoe Perches		}
6145ca0d8929SJoe Perches
6146a0ad7596SJoe Perches# check for function definitions
61475b57980dSJoe Perches		if ($perl_version_ok &&
6148a0ad7596SJoe Perches		    defined $stat &&
6149a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6150a0ad7596SJoe Perches			$context_function = $1;
6151a0ad7596SJoe Perches
6152a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6153a0ad7596SJoe Perches			my $ok = 0;
6154a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6155a0ad7596SJoe Perches			my $herectx = $here . "\n";
6156a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6157a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6158a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6159a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6160a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6161a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6162a0ad7596SJoe Perches			}
6163a0ad7596SJoe Perches			if (!$ok) {
6164a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6165a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6166a0ad7596SJoe Perches			}
6167a0ad7596SJoe Perches		}
6168a0ad7596SJoe Perches
6169de7d4f0eSAndy Whitcroft# checks for new __setup's
6170de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6171de7d4f0eSAndy Whitcroft			my $name = $1;
6172de7d4f0eSAndy Whitcroft
6173de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6174000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
61758c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6176de7d4f0eSAndy Whitcroft			}
6177653d4876SAndy Whitcroft		}
61789c0ca6f9SAndy Whitcroft
61799c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
6180caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
6181000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6182000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
61839c0ca6f9SAndy Whitcroft		}
618413214adfSAndy Whitcroft
6185a640d25cSJoe Perches# alloc style
6186a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
61875b57980dSJoe Perches		if ($perl_version_ok &&
6188a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6189a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6190a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6191a640d25cSJoe Perches		}
6192a640d25cSJoe Perches
619360a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
61945b57980dSJoe Perches		if ($perl_version_ok &&
61951b4a2ed4SJoe Perches		    defined $stat &&
61961b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
619760a55369SJoe Perches			my $oldfunc = $3;
619860a55369SJoe Perches			my $a1 = $4;
619960a55369SJoe Perches			my $a2 = $10;
620060a55369SJoe Perches			my $newfunc = "kmalloc_array";
620160a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
620260a55369SJoe Perches			my $r1 = $a1;
620360a55369SJoe Perches			my $r2 = $a2;
620460a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
620560a55369SJoe Perches				$r1 = $a2;
620660a55369SJoe Perches				$r2 = $a1;
620760a55369SJoe Perches			}
6208e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6209e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
62101b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6211e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6212e3d95a2aSTobin C. Harding
6213e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
62141b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
62151b4a2ed4SJoe Perches				    $cnt == 1 &&
6216e367455aSJoe Perches				    $fix) {
6217194f66fcSJoe 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;
621860a55369SJoe Perches				}
621960a55369SJoe Perches			}
622060a55369SJoe Perches		}
622160a55369SJoe Perches
6222972fdea2SJoe Perches# check for krealloc arg reuse
62235b57980dSJoe Perches		if ($perl_version_ok &&
62244cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
62254cab63ceSJoe Perches		    $1 eq $3) {
6226972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6227972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6228972fdea2SJoe Perches		}
6229972fdea2SJoe Perches
62305ce59ae0SJoe Perches# check for alloc argument mismatch
62315ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
62325ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
62335ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
62345ce59ae0SJoe Perches		}
62355ce59ae0SJoe Perches
6236caf2a54fSJoe Perches# check for multiple semicolons
6237caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6238d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6239d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6240d5e616fcSJoe Perches			    $fix) {
6241194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6242d5e616fcSJoe Perches			}
6243d1e2ad07SJoe Perches		}
6244d1e2ad07SJoe Perches
6245cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6246cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6247cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
62480ab90191SJoe Perches			my $ull = "";
62490ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
62500ab90191SJoe Perches			if (CHK("BIT_MACRO",
62510ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
62520ab90191SJoe Perches			    $fix) {
62530ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
62540ab90191SJoe Perches			}
62550ab90191SJoe Perches		}
62560ab90191SJoe Perches
62572d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
62582d632745SJoe 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*$/) {
62592d632745SJoe Perches			my $config = $1;
62602d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
62612d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
62622d632745SJoe Perches			    $fix) {
62632d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
62642d632745SJoe Perches			}
62652d632745SJoe Perches		}
62662d632745SJoe Perches
6267e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6268c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6269c34c09a8SJoe Perches			my $has_break = 0;
6270c34c09a8SJoe Perches			my $has_statement = 0;
6271c34c09a8SJoe Perches			my $count = 0;
6272c34c09a8SJoe Perches			my $prevline = $linenr;
6273e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6274c34c09a8SJoe Perches				$prevline--;
6275c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6276c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6277c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6278c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6279c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6280c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6281c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6282c34c09a8SJoe Perches				$has_statement = 1;
6283c34c09a8SJoe Perches				$count++;
6284258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6285c34c09a8SJoe Perches			}
6286c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6287c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6288224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6289c34c09a8SJoe Perches			}
6290c34c09a8SJoe Perches		}
6291c34c09a8SJoe Perches
6292d1e2ad07SJoe Perches# check for switch/default statements without a break;
62935b57980dSJoe Perches		if ($perl_version_ok &&
6294d1e2ad07SJoe Perches		    defined $stat &&
6295d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6296d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6297e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6298e3d95a2aSTobin C. Harding
6299d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6300d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6301caf2a54fSJoe Perches		}
6302caf2a54fSJoe Perches
630313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6304d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6305d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6306d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6307d5e616fcSJoe Perches			    $fix) {
6308194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6309d5e616fcSJoe Perches			}
631013214adfSAndy Whitcroft		}
6311773647a0SAndy Whitcroft
631262ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
631362ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
631462ec818fSJoe Perches			ERROR("DATE_TIME",
631562ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
631662ec818fSJoe Perches		}
631762ec818fSJoe Perches
63182c92488aSJoe Perches# check for use of yield()
63192c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
63202c92488aSJoe Perches			WARN("YIELD",
63212c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
63222c92488aSJoe Perches		}
63232c92488aSJoe Perches
6324179f8f40SJoe Perches# check for comparisons against true and false
6325179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6326179f8f40SJoe Perches			my $lead = $1;
6327179f8f40SJoe Perches			my $arg = $2;
6328179f8f40SJoe Perches			my $test = $3;
6329179f8f40SJoe Perches			my $otype = $4;
6330179f8f40SJoe Perches			my $trail = $5;
6331179f8f40SJoe Perches			my $op = "!";
6332179f8f40SJoe Perches
6333179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6334179f8f40SJoe Perches
6335179f8f40SJoe Perches			my $type = lc($otype);
6336179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6337179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6338179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6339179f8f40SJoe Perches					$op = "";
6340179f8f40SJoe Perches				}
6341179f8f40SJoe Perches
6342179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6343179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6344179f8f40SJoe Perches
6345179f8f40SJoe Perches## maybe suggesting a correct construct would better
6346179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6347179f8f40SJoe Perches
6348179f8f40SJoe Perches			}
6349179f8f40SJoe Perches		}
6350179f8f40SJoe Perches
63515d430902SJoe Perches# check for bool bitfields
63525d430902SJoe Perches		if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
63535d430902SJoe Perches			WARN("BOOL_BITFIELD",
63545d430902SJoe Perches			     "Avoid using bool as bitfield.  Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
63555d430902SJoe Perches		}
63565d430902SJoe Perches
6357d729593eSJoe Perches# check for bool use in .h files
6358d729593eSJoe Perches		if ($realfile =~ /\.h$/ &&
6359d729593eSJoe Perches		    $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6360d729593eSJoe Perches			CHK("BOOL_MEMBER",
6361d729593eSJoe Perches			    "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6362d729593eSJoe Perches		}
6363d729593eSJoe Perches
63644882720bSThomas Gleixner# check for semaphores initialized locked
63654882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6366000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6367000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6368773647a0SAndy Whitcroft		}
63696712d858SJoe Perches
637067d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
637167d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6372000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
637367d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6374773647a0SAndy Whitcroft		}
63756712d858SJoe Perches
6376ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6377f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6378000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6379ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6380f3db6639SMichael Ellerman		}
63816712d858SJoe Perches
63820f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6383d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
63846903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6385d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6386000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6387d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
63882b6db5cbSAndy Whitcroft		}
6389773647a0SAndy Whitcroft
6390773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6391773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6392773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6393c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6394c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6395171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6396171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6397171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6398773647a0SAndy Whitcroft		{
6399000d1cc1SJoe Perches			WARN("NR_CPUS",
6400000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6401773647a0SAndy Whitcroft		}
64029c9ba34eSAndy Whitcroft
640352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
640452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
640552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
640652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
640752ea8506SJoe Perches		}
640852ea8506SJoe Perches
6409acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
64105b57980dSJoe Perches		if ($perl_version_ok &&
6411acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6412acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6413acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6414acd9362cSJoe Perches		}
6415acd9362cSJoe Perches
6416691d77b6SAndy Whitcroft# whine mightly about in_atomic
6417691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6418691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6419000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6420000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6421f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6422000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6423000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6424691d77b6SAndy Whitcroft			}
6425691d77b6SAndy Whitcroft		}
64261704f47bSPeter Zijlstra
64270f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
64280f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
64290f5225b0SPeter Zijlstra			ERROR("LOCKING",
64300f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
64310f5225b0SPeter Zijlstra		}
64320f5225b0SPeter Zijlstra
64331704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
64341704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
64351704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
64361704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
64371704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
64381704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6439000d1cc1SJoe Perches				ERROR("LOCKDEP",
6440000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
64411704f47bSPeter Zijlstra			}
64421704f47bSPeter Zijlstra		}
644388f8831cSDave Jones
6444b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6445b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6446000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6447000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
644888f8831cSDave Jones		}
64492435880fSJoe Perches
645000180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
645100180468SJoe Perches# and whether or not function naming is typical and if
645200180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
64535b57980dSJoe Perches		if ($perl_version_ok &&
645400180468SJoe Perches		    defined $stat &&
645500180468SJoe 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*\)/) {
645600180468SJoe Perches			my $var = $1;
645700180468SJoe Perches			my $perms = $2;
645800180468SJoe Perches			my $show = $3;
645900180468SJoe Perches			my $store = $4;
646000180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
646100180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
646200180468SJoe Perches			    $store =~ /^${var}_store$/ &&
646300180468SJoe Perches			    $octal_perms eq "0644") {
646400180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
646500180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
646600180468SJoe Perches				    $fix) {
646700180468SJoe 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})/;
646800180468SJoe Perches				}
646900180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
647000180468SJoe Perches				 $store =~ /^NULL$/ &&
647100180468SJoe Perches				 $octal_perms eq "0444") {
647200180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
647300180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
647400180468SJoe Perches				    $fix) {
647500180468SJoe 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})/;
647600180468SJoe Perches				}
647700180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
647800180468SJoe Perches				 $store =~ /^${var}_store$/ &&
647900180468SJoe Perches				 $octal_perms eq "0200") {
648000180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
648100180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
648200180468SJoe Perches				    $fix) {
648300180468SJoe 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})/;
648400180468SJoe Perches				}
648500180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
648600180468SJoe Perches				 $octal_perms eq "0444" ||
648700180468SJoe Perches				 $octal_perms eq "0200") {
648800180468SJoe Perches				my $newshow = "$show";
648900180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
649000180468SJoe Perches				my $newstore = $store;
649100180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
649200180468SJoe Perches				my $rename = "";
649300180468SJoe Perches				if ($show ne $newshow) {
649400180468SJoe Perches					$rename .= " '$show' to '$newshow'";
649500180468SJoe Perches				}
649600180468SJoe Perches				if ($store ne $newstore) {
649700180468SJoe Perches					$rename .= " '$store' to '$newstore'";
649800180468SJoe Perches				}
649900180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
650000180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
650100180468SJoe Perches			} else {
650200180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
650300180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
650400180468SJoe Perches			}
650500180468SJoe Perches		}
650600180468SJoe Perches
6507515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6508515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
650973121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
651073121534SJoe Perches#   specific definition of not visible in sysfs.
651173121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
651273121534SJoe Perches#   use the default permissions
65135b57980dSJoe Perches		if ($perl_version_ok &&
6514459cf0aeSJoe Perches		    defined $stat &&
6515515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
65162435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
65172435880fSJoe Perches				my $func = $entry->[0];
65182435880fSJoe Perches				my $arg_pos = $entry->[1];
65192435880fSJoe Perches
6520459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6521459cf0aeSJoe Perches				$lc = $lc + $linenr;
65222a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6523459cf0aeSJoe Perches
65242435880fSJoe Perches				my $skip_args = "";
65252435880fSJoe Perches				if ($arg_pos > 1) {
65262435880fSJoe Perches					$arg_pos--;
65272435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
65282435880fSJoe Perches				}
6529f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6530459cf0aeSJoe Perches				if ($stat =~ /$test/) {
65312435880fSJoe Perches					my $val = $1;
65322435880fSJoe Perches					$val = $6 if ($skip_args ne "");
653373121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
653473121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
653573121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
65362435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6537459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6538f90774e1SJoe Perches					}
6539f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6540c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6541459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
65422435880fSJoe Perches					}
6543459cf0aeSJoe Perches				}
6544459cf0aeSJoe Perches			}
6545459cf0aeSJoe Perches		}
6546459cf0aeSJoe Perches
6547459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6548bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
654900180468SJoe Perches			my $oval = $1;
655000180468SJoe Perches			my $octal = perms_to_octal($oval);
6551f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6552459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6553f90774e1SJoe Perches			    $fix) {
655400180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
65552435880fSJoe Perches			}
655613214adfSAndy Whitcroft		}
65575a6d20ceSBjorn Andersson
65585a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
65595a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
65605a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
65615a6d20ceSBjorn Andersson			my $valid_licenses = qr{
65625a6d20ceSBjorn Andersson						GPL|
65635a6d20ceSBjorn Andersson						GPL\ v2|
65645a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
65655a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
65665a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
65675a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
65685a6d20ceSBjorn Andersson						Proprietary
65695a6d20ceSBjorn Andersson					}x;
65705a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
65715a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
65725a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
65735a6d20ceSBjorn Andersson			}
65745a6d20ceSBjorn Andersson		}
6575515a235eSJoe Perches	}
657613214adfSAndy Whitcroft
657713214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
657813214adfSAndy Whitcroft	# so just keep quiet.
657913214adfSAndy Whitcroft	if ($#rawlines == -1) {
658013214adfSAndy Whitcroft		exit(0);
65810a920b5bSAndy Whitcroft	}
65820a920b5bSAndy Whitcroft
65838905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
65848905a67cSAndy Whitcroft	# things that appear to be patches.
65858905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
65868905a67cSAndy Whitcroft		exit(0);
65878905a67cSAndy Whitcroft	}
65888905a67cSAndy Whitcroft
65898905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
65908905a67cSAndy Whitcroft	# just keep quiet.
65918905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
65928905a67cSAndy Whitcroft		exit(0);
65938905a67cSAndy Whitcroft	}
65948905a67cSAndy Whitcroft
6595a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6596000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6597000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
65980a920b5bSAndy Whitcroft	}
6599cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6600cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6601000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6602000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6603cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6604cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6605cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6606cd261496SGeert Uytterhoeven		}
66070a920b5bSAndy Whitcroft	}
66080a920b5bSAndy Whitcroft
6609f0a594c1SAndy Whitcroft	print report_dump();
661013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
661113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
66126c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
66136c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
66146c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
66156c72ffaaSAndy Whitcroft	}
66168905a67cSAndy Whitcroft
6617d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6618ef212196SJoe Perches		# If there were any defects found and not already fixing them
6619ef212196SJoe Perches		if (!$clean and !$fix) {
6620ef212196SJoe Perches			print << "EOM"
6621ef212196SJoe Perches
6622ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6623ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6624ef212196SJoe PerchesEOM
6625ef212196SJoe Perches		}
6626d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6627d2c0a235SAndy Whitcroft		# then suggest that.
6628d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6629b0781216SMike Frysinger			$rpt_cleaners = 0;
6630d8469f16SJoe Perches			print << "EOM"
6631d8469f16SJoe Perches
6632d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6633d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6634d8469f16SJoe PerchesEOM
6635d2c0a235SAndy Whitcroft		}
6636d2c0a235SAndy Whitcroft	}
6637d2c0a235SAndy Whitcroft
6638d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6639d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6640d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
66419624b8d6SJoe Perches		my $newfile = $filename;
66429624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
66433705ce5bSJoe Perches		my $linecount = 0;
66443705ce5bSJoe Perches		my $f;
66453705ce5bSJoe Perches
6646d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6647d752fcc8SJoe Perches
66483705ce5bSJoe Perches		open($f, '>', $newfile)
66493705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
66503705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
66513705ce5bSJoe Perches			$linecount++;
66523705ce5bSJoe Perches			if ($file) {
66533705ce5bSJoe Perches				if ($linecount > 3) {
66543705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
66553705ce5bSJoe Perches					print $f $fixed_line . "\n";
66563705ce5bSJoe Perches				}
66573705ce5bSJoe Perches			} else {
66583705ce5bSJoe Perches				print $f $fixed_line . "\n";
66593705ce5bSJoe Perches			}
66603705ce5bSJoe Perches		}
66613705ce5bSJoe Perches		close($f);
66623705ce5bSJoe Perches
66633705ce5bSJoe Perches		if (!$quiet) {
66643705ce5bSJoe Perches			print << "EOM";
6665d8469f16SJoe Perches
66663705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
66673705ce5bSJoe Perches
66683705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
66693705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
66703705ce5bSJoe Perches
66713705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
66723705ce5bSJoe PerchesNo warranties, expressed or implied...
66733705ce5bSJoe PerchesEOM
66743705ce5bSJoe Perches		}
66753705ce5bSJoe Perches	}
66763705ce5bSJoe Perches
6677d8469f16SJoe Perches	if ($quiet == 0) {
6678d8469f16SJoe Perches		print "\n";
6679d8469f16SJoe Perches		if ($clean == 1) {
6680d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6681d8469f16SJoe Perches		} else {
6682d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
66830a920b5bSAndy Whitcroft		}
66840a920b5bSAndy Whitcroft	}
66850a920b5bSAndy Whitcroft	return $clean;
66860a920b5bSAndy Whitcroft}
6687