xref: /linux-6.15/scripts/checkpatch.pl (revision 33aa4597)
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);
160a920b5bSAndy Whitcroft
170a920b5bSAndy Whitcroftmy $P = $0;
1836061e38SJoe Perchesmy $D = dirname(abs_path($P));
190a920b5bSAndy Whitcroft
20000d1cc1SJoe Perchesmy $V = '0.32';
210a920b5bSAndy Whitcroft
220a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
230a920b5bSAndy Whitcroft
240a920b5bSAndy Whitcroftmy $quiet = 0;
250a920b5bSAndy Whitcroftmy $tree = 1;
260a920b5bSAndy Whitcroftmy $chk_signoff = 1;
270a920b5bSAndy Whitcroftmy $chk_patch = 1;
28773647a0SAndy Whitcroftmy $tst_only;
296c72ffaaSAndy Whitcroftmy $emacs = 0;
308905a67cSAndy Whitcroftmy $terse = 0;
3134d8815fSJoe Perchesmy $showfile = 0;
326c72ffaaSAndy Whitcroftmy $file = 0;
334a593c34SDu, Changbinmy $git = 0;
340dea9f1eSJoe Perchesmy %git_commits = ();
356c72ffaaSAndy Whitcroftmy $check = 0;
362ac73b4fSJoe Perchesmy $check_orig = 0;
378905a67cSAndy Whitcroftmy $summary = 1;
388905a67cSAndy Whitcroftmy $mailback = 0;
3913214adfSAndy Whitcroftmy $summary_file = 0;
40000d1cc1SJoe Perchesmy $show_types = 0;
413beb42ecSJoe Perchesmy $list_types = 0;
423705ce5bSJoe Perchesmy $fix = 0;
439624b8d6SJoe Perchesmy $fix_inplace = 0;
446c72ffaaSAndy Whitcroftmy $root;
45c2fdda0dSAndy Whitcroftmy %debug;
463445686aSJoe Perchesmy %camelcase = ();
4791bfe484SJoe Perchesmy %use_type = ();
4891bfe484SJoe Perchesmy @use = ();
4991bfe484SJoe Perchesmy %ignore_type = ();
50000d1cc1SJoe Perchesmy @ignore = ();
5177f5b10aSHannes Edermy $help = 0;
52000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
536cd7f386SJoe Perchesmy $max_line_length = 80;
54d62a201fSDave Hansenmy $ignore_perl_version = 0;
55d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5656193274SVadim Bendeburymy $min_conf_desc_length = 4;
5766b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
58ebfd7d62SJoe Perchesmy $codespell = 0;
59f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
60bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
6175ad8c57SJerome Forissiermy $typedefsfile = "";
62737c0767SJohn Brooksmy $color = "auto";
63dadf680dSJoe Perchesmy $allow_c99_comments = 1;
6477f5b10aSHannes Eder
6577f5b10aSHannes Edersub help {
6677f5b10aSHannes Eder	my ($exitcode) = @_;
6777f5b10aSHannes Eder
6877f5b10aSHannes Eder	print << "EOM";
6977f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7077f5b10aSHannes EderVersion: $V
7177f5b10aSHannes Eder
7277f5b10aSHannes EderOptions:
7377f5b10aSHannes Eder  -q, --quiet                quiet
7477f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7577f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7677f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
7777f5b10aSHannes Eder  --emacs                    emacs compile window format
7877f5b10aSHannes Eder  --terse                    one line per report
7934d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
804a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
814a593c34SDu, Changbin                             single git commit with:
824a593c34SDu, Changbin                               <rev>
834a593c34SDu, Changbin                               <rev>^
844a593c34SDu, Changbin                               <rev>~n
854a593c34SDu, Changbin                             multiple git commits with:
864a593c34SDu, Changbin                               <rev1>..<rev2>
874a593c34SDu, Changbin                               <rev1>...<rev2>
884a593c34SDu, Changbin                               <rev>-<count>
894a593c34SDu, Changbin                             git merges are ignored
9077f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9177f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
923beb42ecSJoe Perches  --list-types               list the possible message types
9391bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
94000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
953beb42ecSJoe Perches  --show-types               show the specific message type in the output
966cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
9756193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
9877f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
9977f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10077f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10177f5b10aSHannes Eder  --summary-file             include the filename in summary
10277f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
10377f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10477f5b10aSHannes Eder                             is all off)
10577f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10677f5b10aSHannes Eder                             literally
1073705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1083705ce5bSJoe Perches                             If correctable single-line errors exist, create
1093705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1103705ce5bSJoe Perches                             with potential errors corrected to the preferred
1113705ce5bSJoe Perches                             checkpatch style
1129624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1139624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1149624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
115d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
116d62a201fSDave Hansen                             runtime errors.
117ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
118f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
119ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12075ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
121737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
122737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
12377f5b10aSHannes Eder  -h, --help, --version      display this help and exit
12477f5b10aSHannes Eder
12577f5b10aSHannes EderWhen FILE is - read standard input.
12677f5b10aSHannes EderEOM
12777f5b10aSHannes Eder
12877f5b10aSHannes Eder	exit($exitcode);
12977f5b10aSHannes Eder}
13077f5b10aSHannes Eder
1313beb42ecSJoe Perchessub uniq {
1323beb42ecSJoe Perches	my %seen;
1333beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1343beb42ecSJoe Perches}
1353beb42ecSJoe Perches
1363beb42ecSJoe Perchessub list_types {
1373beb42ecSJoe Perches	my ($exitcode) = @_;
1383beb42ecSJoe Perches
1393beb42ecSJoe Perches	my $count = 0;
1403beb42ecSJoe Perches
1413beb42ecSJoe Perches	local $/ = undef;
1423beb42ecSJoe Perches
1433beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1443beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1453beb42ecSJoe Perches
1463beb42ecSJoe Perches	my $text = <$script>;
1473beb42ecSJoe Perches	close($script);
1483beb42ecSJoe Perches
1493beb42ecSJoe Perches	my @types = ();
1500547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1510547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1523beb42ecSJoe Perches		push (@types, $_);
1533beb42ecSJoe Perches	}
1543beb42ecSJoe Perches	@types = sort(uniq(@types));
1553beb42ecSJoe Perches	print("#\tMessage type\n\n");
1563beb42ecSJoe Perches	foreach my $type (@types) {
1573beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1583beb42ecSJoe Perches	}
1593beb42ecSJoe Perches
1603beb42ecSJoe Perches	exit($exitcode);
1613beb42ecSJoe Perches}
1623beb42ecSJoe Perches
163000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
164000d1cc1SJoe Perchesif (-f $conf) {
165000d1cc1SJoe Perches	my @conf_args;
166000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
167000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
168000d1cc1SJoe Perches
169000d1cc1SJoe Perches	while (<$conffile>) {
170000d1cc1SJoe Perches		my $line = $_;
171000d1cc1SJoe Perches
172000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
173000d1cc1SJoe Perches		$line =~ s/^\s*//g;
174000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
175000d1cc1SJoe Perches
176000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
177000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
178000d1cc1SJoe Perches
179000d1cc1SJoe Perches		my @words = split(" ", $line);
180000d1cc1SJoe Perches		foreach my $word (@words) {
181000d1cc1SJoe Perches			last if ($word =~ m/^#/);
182000d1cc1SJoe Perches			push (@conf_args, $word);
183000d1cc1SJoe Perches		}
184000d1cc1SJoe Perches	}
185000d1cc1SJoe Perches	close($conffile);
186000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
187000d1cc1SJoe Perches}
188000d1cc1SJoe Perches
189737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
190737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
191737c0767SJohn Brooksforeach (@ARGV) {
192737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
193737c0767SJohn Brooks		$_ = "--color=$color";
194737c0767SJohn Brooks	}
195737c0767SJohn Brooks}
196737c0767SJohn Brooks
1970a920b5bSAndy WhitcroftGetOptions(
1986c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1990a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2000a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2010a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2026c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2038905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20434d8815fSJoe Perches	'showfile!'	=> \$showfile,
20577f5b10aSHannes Eder	'f|file!'	=> \$file,
2064a593c34SDu, Changbin	'g|git!'	=> \$git,
2076c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2086c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
209000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
21091bfe484SJoe Perches	'types=s'	=> \@use,
211000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2123beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2136cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21456193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2156c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2168905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2178905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
21813214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2193705ce5bSJoe Perches	'fix!'		=> \$fix,
2209624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
221d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
222c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
223773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
224ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
225ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
22675ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
227737c0767SJohn Brooks	'color=s'	=> \$color,
228737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
229737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
23077f5b10aSHannes Eder	'h|help'	=> \$help,
23177f5b10aSHannes Eder	'version'	=> \$help
23277f5b10aSHannes Eder) or help(1);
23377f5b10aSHannes Eder
23477f5b10aSHannes Ederhelp(0) if ($help);
2350a920b5bSAndy Whitcroft
2363beb42ecSJoe Percheslist_types(0) if ($list_types);
2373beb42ecSJoe Perches
2389624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2392ac73b4fSJoe Perches$check_orig = $check;
2409624b8d6SJoe Perches
2410a920b5bSAndy Whitcroftmy $exit = 0;
2420a920b5bSAndy Whitcroft
2435b57980dSJoe Perchesmy $perl_version_ok = 1;
244d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2455b57980dSJoe Perches	$perl_version_ok = 0;
246d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2475b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
248d62a201fSDave Hansen}
249d62a201fSDave Hansen
25045107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2510a920b5bSAndy Whitcroftif ($#ARGV < 0) {
25245107ff6SAllen Hubbe	push(@ARGV, '-');
2530a920b5bSAndy Whitcroft}
2540a920b5bSAndy Whitcroft
255737c0767SJohn Brooksif ($color =~ /^[01]$/) {
256737c0767SJohn Brooks	$color = !$color;
257737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
258737c0767SJohn Brooks	$color = 1;
259737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
260737c0767SJohn Brooks	$color = 0;
261737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
262737c0767SJohn Brooks	$color = (-t STDOUT);
263737c0767SJohn Brooks} else {
264737c0767SJohn Brooks	die "Invalid color mode: $color\n";
265737c0767SJohn Brooks}
266737c0767SJohn Brooks
26791bfe484SJoe Perchessub hash_save_array_words {
26891bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
26991bfe484SJoe Perches
27091bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
27191bfe484SJoe Perches	foreach my $word (@array) {
272000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
273000d1cc1SJoe Perches		$word =~ s/^\s*//g;
274000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
275000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
276000d1cc1SJoe Perches
277000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
278000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
279000d1cc1SJoe Perches
28091bfe484SJoe Perches		$hashRef->{$word}++;
281000d1cc1SJoe Perches	}
28291bfe484SJoe Perches}
28391bfe484SJoe Perches
28491bfe484SJoe Perchessub hash_show_words {
28591bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
28691bfe484SJoe Perches
2873c816e49SJoe Perches	if (keys %$hashRef) {
288d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
28958cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
29091bfe484SJoe Perches			print " $word";
29191bfe484SJoe Perches		}
292d8469f16SJoe Perches		print "\n";
29391bfe484SJoe Perches	}
29491bfe484SJoe Perches}
29591bfe484SJoe Perches
29691bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
29791bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
298000d1cc1SJoe Perches
299c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
300c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3017429c690SAndy Whitcroftmy $dbg_type = 0;
302a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
303c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
30421caa13cSAndy Whitcroft	## no critic
30521caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
30621caa13cSAndy Whitcroft	die "$@" if ($@);
307c2fdda0dSAndy Whitcroft}
308c2fdda0dSAndy Whitcroft
309d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
310d2c0a235SAndy Whitcroft
3118905a67cSAndy Whitcroftif ($terse) {
3128905a67cSAndy Whitcroft	$emacs = 1;
3138905a67cSAndy Whitcroft	$quiet++;
3148905a67cSAndy Whitcroft}
3158905a67cSAndy Whitcroft
3166c72ffaaSAndy Whitcroftif ($tree) {
3176c72ffaaSAndy Whitcroft	if (defined $root) {
3186c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3196c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3206c72ffaaSAndy Whitcroft		}
3216c72ffaaSAndy Whitcroft	} else {
3226c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3236c72ffaaSAndy Whitcroft			$root = '.';
3246c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3256c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3266c72ffaaSAndy Whitcroft			$root = $1;
3276c72ffaaSAndy Whitcroft		}
3286c72ffaaSAndy Whitcroft	}
3296c72ffaaSAndy Whitcroft
3306c72ffaaSAndy Whitcroft	if (!defined $root) {
3310a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3320a920b5bSAndy Whitcroft		exit(2);
3330a920b5bSAndy Whitcroft	}
3346c72ffaaSAndy Whitcroft}
3356c72ffaaSAndy Whitcroft
3366c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3376c72ffaaSAndy Whitcroft
3382ceb532bSAndy Whitcroftour $Ident	= qr{
3392ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3402ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3412ceb532bSAndy Whitcroft		}x;
3426c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3436c72ffaaSAndy Whitcroftour $Sparse	= qr{
3446c72ffaaSAndy Whitcroft			__user|
3456c72ffaaSAndy Whitcroft			__kernel|
3466c72ffaaSAndy Whitcroft			__force|
3476c72ffaaSAndy Whitcroft			__iomem|
3486c72ffaaSAndy Whitcroft			__must_check|
349417495edSAndy Whitcroft			__kprobes|
350165e72a6SSven Eckelmann			__ref|
351*33aa4597SGeert Uytterhoeven			__refconst|
352*33aa4597SGeert Uytterhoeven			__refdata|
353ad315455SBoqun Feng			__rcu|
354ad315455SBoqun Feng			__private
3556c72ffaaSAndy Whitcroft		}x;
356e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
357e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
358e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
359e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
360e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3618716de38SJoe Perches
36252131292SWolfram Sang# Notes to $Attribute:
36352131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3646c72ffaaSAndy Whitcroftour $Attribute	= qr{
3656c72ffaaSAndy Whitcroft			const|
36603f1df7dSJoe Perches			__percpu|
36703f1df7dSJoe Perches			__nocast|
36803f1df7dSJoe Perches			__safe|
36946d832f5SMichael S. Tsirkin			__bitwise|
37003f1df7dSJoe Perches			__packed__|
37103f1df7dSJoe Perches			__packed2__|
37203f1df7dSJoe Perches			__naked|
37303f1df7dSJoe Perches			__maybe_unused|
37403f1df7dSJoe Perches			__always_unused|
37503f1df7dSJoe Perches			__noreturn|
37603f1df7dSJoe Perches			__used|
37703f1df7dSJoe Perches			__cold|
378e23ef1f3SJoe Perches			__pure|
37903f1df7dSJoe Perches			__noclone|
38003f1df7dSJoe Perches			__deprecated|
3816c72ffaaSAndy Whitcroft			__read_mostly|
3826c72ffaaSAndy Whitcroft			__kprobes|
3838716de38SJoe Perches			$InitAttribute|
38424e1d81aSAndy Whitcroft			____cacheline_aligned|
38524e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3865fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3875fe3af11SAndy Whitcroft			__weak
3886c72ffaaSAndy Whitcroft		  }x;
389c45dcabdSAndy Whitcroftour $Modifier;
39091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3916c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3926c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3936c72ffaaSAndy Whitcroft
39495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
39595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
39695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
39795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3982435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
399c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
400326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
401326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
402326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
40374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4042435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
405326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
406447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
40723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4086c72ffaaSAndy Whitcroftour $Operators	= qr{
4096c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4106c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
41123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4126c72ffaaSAndy Whitcroft		  }x;
4136c72ffaaSAndy Whitcroft
41491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
41591cb5195SJoe Perches
416ab7e23f3SJoe Perchesour $BasicType;
4178905a67cSAndy Whitcroftour $NonptrType;
4181813087dSJoe Perchesour $NonptrTypeMisordered;
4198716de38SJoe Perchesour $NonptrTypeWithAttr;
4208905a67cSAndy Whitcroftour $Type;
4211813087dSJoe Perchesour $TypeMisordered;
4228905a67cSAndy Whitcroftour $Declare;
4231813087dSJoe Perchesour $DeclareMisordered;
4248905a67cSAndy Whitcroft
42515662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
42615662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
427171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
428171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
429171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
430171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
431171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
432171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
433171ae1a4SAndy Whitcroft}x;
434171ae1a4SAndy Whitcroft
43515662b3eSJoe Perchesour $UTF8	= qr{
43615662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
43715662b3eSJoe Perches	| $NON_ASCII_UTF8
43815662b3eSJoe Perches}x;
43915662b3eSJoe Perches
440e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
441021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
442021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
443021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
444021158b4SJoe Perches)};
445e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
446fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4478ed22cadSAndy Whitcroft	atomic_t
4488ed22cadSAndy Whitcroft)};
449e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
450e6176fa4SJoe Perches	$typeC99Typedefs\b|
451e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
452e6176fa4SJoe Perches	$typeKernelTypedefs\b
453e6176fa4SJoe Perches)};
4548ed22cadSAndy Whitcroft
4556d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4566d32f7a3SJoe Perches
457691e669bSJoe Perchesour $logFunctions = qr{(?x:
458758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4597d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
46087bd499aSJoe Perches	TP_printk|
4616e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
462b0531722SJoe Perches	panic|
46306668727SJoe Perches	MODULE_[A-Z_]+|
46406668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
465691e669bSJoe Perches)};
466691e669bSJoe Perches
46720112475SJoe Perchesour $signature_tags = qr{(?xi:
46820112475SJoe Perches	Signed-off-by:|
46920112475SJoe Perches	Acked-by:|
47020112475SJoe Perches	Tested-by:|
47120112475SJoe Perches	Reviewed-by:|
47220112475SJoe Perches	Reported-by:|
4738543ae12SMugunthan V N	Suggested-by:|
47420112475SJoe Perches	To:|
47520112475SJoe Perches	Cc:
47620112475SJoe Perches)};
47720112475SJoe Perches
4781813087dSJoe Perchesour @typeListMisordered = (
4791813087dSJoe Perches	qr{char\s+(?:un)?signed},
4801813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4811813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4821813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4831813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4841813087dSJoe Perches	qr{short\s+(?:un)?signed},
4851813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4861813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4871813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4881813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4891813087dSJoe Perches	qr{int\s+(?:un)?signed},
4901813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4911813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4921813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4931813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4941813087dSJoe Perches	qr{long\s+(?:un)?signed},
4951813087dSJoe Perches);
4961813087dSJoe Perches
4978905a67cSAndy Whitcroftour @typeList = (
4988905a67cSAndy Whitcroft	qr{void},
4990c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5000c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5010c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5020c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5030c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5040c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5050c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5060c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5070c773d9dSJoe Perches	qr{(?:un)?signed},
5088905a67cSAndy Whitcroft	qr{float},
5098905a67cSAndy Whitcroft	qr{double},
5108905a67cSAndy Whitcroft	qr{bool},
5118905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5128905a67cSAndy Whitcroft	qr{union\s+$Ident},
5138905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5148905a67cSAndy Whitcroft	qr{${Ident}_t},
5158905a67cSAndy Whitcroft	qr{${Ident}_handler},
5168905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5171813087dSJoe Perches	@typeListMisordered,
5188905a67cSAndy Whitcroft);
519938224b5SJoe Perches
520938224b5SJoe Perchesour $C90_int_types = qr{(?x:
521938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
522938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
523938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
524938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
525938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
526938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
527938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
528938224b5SJoe Perches
529938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
530938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
531938224b5SJoe Perches	long\s+(?:un)?signed|
532938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
533938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
534938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
535938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
536938224b5SJoe Perches
537938224b5SJoe Perches	int\s+(?:un)?signed|
538938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
539938224b5SJoe Perches)};
540938224b5SJoe Perches
541485ff23eSAlex Dowadour @typeListFile = ();
5428716de38SJoe Perchesour @typeListWithAttr = (
5438716de38SJoe Perches	@typeList,
5448716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5458716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5468716de38SJoe Perches);
5478716de38SJoe Perches
548c45dcabdSAndy Whitcroftour @modifierList = (
549c45dcabdSAndy Whitcroft	qr{fastcall},
550c45dcabdSAndy Whitcroft);
551485ff23eSAlex Dowadour @modifierListFile = ();
5528905a67cSAndy Whitcroft
5532435880fSJoe Perchesour @mode_permission_funcs = (
5542435880fSJoe Perches	["module_param", 3],
5552435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5562435880fSJoe Perches	["module_param_array_named", 5],
5572435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5582435880fSJoe Perches	["proc_create(?:_data|)", 2],
559459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
560459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
561459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
562459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
563459cf0aeSJoe Perches	["__ATTR", 2],
5642435880fSJoe Perches);
5652435880fSJoe Perches
566515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
567515a235eSJoe Perchesour $mode_perms_search = "";
568515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
569515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
570515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
571515a235eSJoe Perches}
57200180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
573515a235eSJoe Perches
574b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
575b392c64fSJoe Perches	S_IWUGO		|
576b392c64fSJoe Perches	S_IWOTH		|
577b392c64fSJoe Perches	S_IRWXUGO	|
578b392c64fSJoe Perches	S_IALLUGO	|
579b392c64fSJoe Perches	0[0-7][0-7][2367]
580b392c64fSJoe Perches}x;
581b392c64fSJoe Perches
582f90774e1SJoe Perchesour %mode_permission_string_types = (
583f90774e1SJoe Perches	"S_IRWXU" => 0700,
584f90774e1SJoe Perches	"S_IRUSR" => 0400,
585f90774e1SJoe Perches	"S_IWUSR" => 0200,
586f90774e1SJoe Perches	"S_IXUSR" => 0100,
587f90774e1SJoe Perches	"S_IRWXG" => 0070,
588f90774e1SJoe Perches	"S_IRGRP" => 0040,
589f90774e1SJoe Perches	"S_IWGRP" => 0020,
590f90774e1SJoe Perches	"S_IXGRP" => 0010,
591f90774e1SJoe Perches	"S_IRWXO" => 0007,
592f90774e1SJoe Perches	"S_IROTH" => 0004,
593f90774e1SJoe Perches	"S_IWOTH" => 0002,
594f90774e1SJoe Perches	"S_IXOTH" => 0001,
595f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
596f90774e1SJoe Perches	"S_IRUGO" => 0444,
597f90774e1SJoe Perches	"S_IWUGO" => 0222,
598f90774e1SJoe Perches	"S_IXUGO" => 0111,
599f90774e1SJoe Perches);
600f90774e1SJoe Perches
601f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
602f90774e1SJoe Perchesour $mode_perms_string_search = "";
603f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
604f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
605f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
606f90774e1SJoe Perches}
60700180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
60800180468SJoe Perchesour $multi_mode_perms_string_search = qr{
60900180468SJoe Perches	${single_mode_perms_string_search}
61000180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
61100180468SJoe Perches}x;
61200180468SJoe Perches
61300180468SJoe Perchessub perms_to_octal {
61400180468SJoe Perches	my ($string) = @_;
61500180468SJoe Perches
61600180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
61700180468SJoe Perches
61800180468SJoe Perches	my $val = "";
61900180468SJoe Perches	my $oval = "";
62000180468SJoe Perches	my $to = 0;
62100180468SJoe Perches	my $curpos = 0;
62200180468SJoe Perches	my $lastpos = 0;
62300180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
62400180468SJoe Perches		$curpos = pos($string);
62500180468SJoe Perches		my $match = $2;
62600180468SJoe Perches		my $omatch = $1;
62700180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
62800180468SJoe Perches		$lastpos = $curpos;
62900180468SJoe Perches		$to |= $mode_permission_string_types{$match};
63000180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
63100180468SJoe Perches		$val .= $match;
63200180468SJoe Perches		$oval .= $omatch;
63300180468SJoe Perches	}
63400180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
63500180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
63600180468SJoe Perches	return sprintf("%04o", $to);
63700180468SJoe Perches}
638f90774e1SJoe Perches
6397840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6407840a94cSWolfram Sang	irq|
641cdcee686SSergey Ryazanov	memory|
642cdcee686SSergey Ryazanov	time|
643cdcee686SSergey Ryazanov	reboot
6447840a94cSWolfram Sang)};
6457840a94cSWolfram Sang# memory.h: ARM has a custom one
6467840a94cSWolfram Sang
64766b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
64866b47b4aSKees Cookmy $misspellings;
64966b47b4aSKees Cookmy %spelling_fix;
65036061e38SJoe Perches
65136061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
65266b47b4aSKees Cook	while (<$spelling>) {
65366b47b4aSKees Cook		my $line = $_;
65466b47b4aSKees Cook
65566b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
65666b47b4aSKees Cook		$line =~ s/^\s*//g;
65766b47b4aSKees Cook
65866b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
65966b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
66066b47b4aSKees Cook
66166b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
66266b47b4aSKees Cook
66366b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
66466b47b4aSKees Cook	}
66566b47b4aSKees Cook	close($spelling);
66636061e38SJoe Perches} else {
66736061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
66836061e38SJoe Perches}
66966b47b4aSKees Cook
670ebfd7d62SJoe Perchesif ($codespell) {
671ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
672ebfd7d62SJoe Perches		while (<$spelling>) {
673ebfd7d62SJoe Perches			my $line = $_;
674ebfd7d62SJoe Perches
675ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
676ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
677ebfd7d62SJoe Perches
678ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
679ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
680ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
681ebfd7d62SJoe Perches
682ebfd7d62SJoe Perches			$line =~ s/,.*$//;
683ebfd7d62SJoe Perches
684ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
685ebfd7d62SJoe Perches
686ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
687ebfd7d62SJoe Perches		}
688ebfd7d62SJoe Perches		close($spelling);
689ebfd7d62SJoe Perches	} else {
690ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
691ebfd7d62SJoe Perches	}
692ebfd7d62SJoe Perches}
693ebfd7d62SJoe Perches
694ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
695ebfd7d62SJoe Perches
69675ad8c57SJerome Forissiersub read_words {
69775ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
69875ad8c57SJerome Forissier
69975ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
70075ad8c57SJerome Forissier		while (<$words>) {
701bf1fa1daSJoe Perches			my $line = $_;
702bf1fa1daSJoe Perches
703bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
704bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
705bf1fa1daSJoe Perches
706bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
707bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
708bf1fa1daSJoe Perches			if ($line =~ /\s/) {
70975ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
710bf1fa1daSJoe Perches				next;
711bf1fa1daSJoe Perches			}
712bf1fa1daSJoe Perches
71375ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
71475ad8c57SJerome Forissier			$$wordsRef .= $line;
715bf1fa1daSJoe Perches		}
71675ad8c57SJerome Forissier		close($file);
71775ad8c57SJerome Forissier		return 1;
718bf1fa1daSJoe Perches	}
719bf1fa1daSJoe Perches
72075ad8c57SJerome Forissier	return 0;
72175ad8c57SJerome Forissier}
72275ad8c57SJerome Forissier
72375ad8c57SJerome Forissiermy $const_structs = "";
72475ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
72575ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
72675ad8c57SJerome Forissier
72775ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
72875ad8c57SJerome Forissierif (length($typedefsfile)) {
72975ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
73075ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
73175ad8c57SJerome Forissier}
73275ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
73375ad8c57SJerome Forissier
7348905a67cSAndy Whitcroftsub build_types {
735485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
736485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7371813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7388716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
739c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
740ab7e23f3SJoe Perches	$BasicType	= qr{
741ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
742ab7e23f3SJoe Perches				(?:${all}\b)
743ab7e23f3SJoe Perches		}x;
7448905a67cSAndy Whitcroft	$NonptrType	= qr{
745d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
746cf655043SAndy Whitcroft			(?:
7476b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7488ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
749c45dcabdSAndy Whitcroft				(?:${all}\b)
750cf655043SAndy Whitcroft			)
751c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7528905a67cSAndy Whitcroft		  }x;
7531813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7541813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7551813087dSJoe Perches			(?:
7561813087dSJoe Perches				(?:${Misordered}\b)
7571813087dSJoe Perches			)
7581813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7591813087dSJoe Perches		  }x;
7608716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7618716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7628716de38SJoe Perches			(?:
7638716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
7648716de38SJoe Perches				(?:$typeTypedefs\b)|
7658716de38SJoe Perches				(?:${allWithAttr}\b)
7668716de38SJoe Perches			)
7678716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
7688716de38SJoe Perches		  }x;
7698905a67cSAndy Whitcroft	$Type	= qr{
770c45dcabdSAndy Whitcroft			$NonptrType
7711574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
772c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
7738905a67cSAndy Whitcroft		  }x;
7741813087dSJoe Perches	$TypeMisordered	= qr{
7751813087dSJoe Perches			$NonptrTypeMisordered
7761813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
7771813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
7781813087dSJoe Perches		  }x;
77991cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
7801813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
7818905a67cSAndy Whitcroft}
7828905a67cSAndy Whitcroftbuild_types();
7836c72ffaaSAndy Whitcroft
7847d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
785d1fe9c09SJoe Perches
786d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
787d1fe9c09SJoe Perches# requires at least perl version v5.10.0
788d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
789d1fe9c09SJoe Perches
790d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
7912435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
792c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
7937d2367afSJoe Perches
794f8422308SJoe Perchesour $declaration_macros = qr{(?x:
7953e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
796fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
7973d102fc0SGilad Ben-Yossef	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
7983d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
799f8422308SJoe Perches)};
800f8422308SJoe Perches
8017d2367afSJoe Perchessub deparenthesize {
8027d2367afSJoe Perches	my ($string) = @_;
8037d2367afSJoe Perches	return "" if (!defined($string));
8045b9553abSJoe Perches
8055b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8065b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8075b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8085b9553abSJoe Perches	}
8095b9553abSJoe Perches
8107d2367afSJoe Perches	$string =~ s@\s+@ @g;
8115b9553abSJoe Perches
8127d2367afSJoe Perches	return $string;
8137d2367afSJoe Perches}
8147d2367afSJoe Perches
8153445686aSJoe Perchessub seed_camelcase_file {
8163445686aSJoe Perches	my ($file) = @_;
8173445686aSJoe Perches
8183445686aSJoe Perches	return if (!(-f $file));
8193445686aSJoe Perches
8203445686aSJoe Perches	local $/;
8213445686aSJoe Perches
8223445686aSJoe Perches	open(my $include_file, '<', "$file")
8233445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8243445686aSJoe Perches	my $text = <$include_file>;
8253445686aSJoe Perches	close($include_file);
8263445686aSJoe Perches
8273445686aSJoe Perches	my @lines = split('\n', $text);
8283445686aSJoe Perches
8293445686aSJoe Perches	foreach my $line (@lines) {
8303445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8313445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8323445686aSJoe Perches			$camelcase{$1} = 1;
83311ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
83411ea516aSJoe Perches			$camelcase{$1} = 1;
83511ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8363445686aSJoe Perches			$camelcase{$1} = 1;
8373445686aSJoe Perches		}
8383445686aSJoe Perches	}
8393445686aSJoe Perches}
8403445686aSJoe Perches
84185b0ee18SJoe Perchessub is_maintained_obsolete {
84285b0ee18SJoe Perches	my ($filename) = @_;
84385b0ee18SJoe Perches
844f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
84585b0ee18SJoe Perches
8460616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
84785b0ee18SJoe Perches
84885b0ee18SJoe Perches	return $status =~ /obsolete/i;
84985b0ee18SJoe Perches}
85085b0ee18SJoe Perches
8513445686aSJoe Perchesmy $camelcase_seeded = 0;
8523445686aSJoe Perchessub seed_camelcase_includes {
8533445686aSJoe Perches	return if ($camelcase_seeded);
8543445686aSJoe Perches
8553445686aSJoe Perches	my $files;
856c707a81dSJoe Perches	my $camelcase_cache = "";
857c707a81dSJoe Perches	my @include_files = ();
858c707a81dSJoe Perches
859c707a81dSJoe Perches	$camelcase_seeded = 1;
860351b2a1fSJoe Perches
8613645e328SRichard Genoud	if (-e ".git") {
862351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
863351b2a1fSJoe Perches		chomp $git_last_include_commit;
864c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
865c707a81dSJoe Perches	} else {
866c707a81dSJoe Perches		my $last_mod_date = 0;
867c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
868c707a81dSJoe Perches		@include_files = split('\n', $files);
869c707a81dSJoe Perches		foreach my $file (@include_files) {
870c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
871c707a81dSJoe Perches						   localtime((stat $file)[9]));
872c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
873c707a81dSJoe Perches		}
874c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
875c707a81dSJoe Perches	}
876c707a81dSJoe Perches
877c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
878c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
879c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
880351b2a1fSJoe Perches		while (<$camelcase_file>) {
881351b2a1fSJoe Perches			chomp;
882351b2a1fSJoe Perches			$camelcase{$_} = 1;
883351b2a1fSJoe Perches		}
884351b2a1fSJoe Perches		close($camelcase_file);
885351b2a1fSJoe Perches
886351b2a1fSJoe Perches		return;
887351b2a1fSJoe Perches	}
888c707a81dSJoe Perches
8893645e328SRichard Genoud	if (-e ".git") {
890c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
891c707a81dSJoe Perches		@include_files = split('\n', $files);
8923445686aSJoe Perches	}
893c707a81dSJoe Perches
8943445686aSJoe Perches	foreach my $file (@include_files) {
8953445686aSJoe Perches		seed_camelcase_file($file);
8963445686aSJoe Perches	}
897351b2a1fSJoe Perches
898c707a81dSJoe Perches	if ($camelcase_cache ne "") {
899351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
900c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
901c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
902351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
903351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
904351b2a1fSJoe Perches		}
905351b2a1fSJoe Perches		close($camelcase_file);
906351b2a1fSJoe Perches	}
9073445686aSJoe Perches}
9083445686aSJoe Perches
909d311cd44SJoe Perchessub git_commit_info {
910d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
911d311cd44SJoe Perches
912d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
913d311cd44SJoe Perches
914d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
915d311cd44SJoe Perches	$output =~ s/^\s*//gm;
916d311cd44SJoe Perches	my @lines = split("\n", $output);
917d311cd44SJoe Perches
9180d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9190d7835fcSJoe Perches
920d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
921d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
922d311cd44SJoe Perches# all matching commit ids, but it's very slow...
923d311cd44SJoe Perches#
924d311cd44SJoe Perches#		echo "checking commits $1..."
925d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
926d311cd44SJoe Perches#		while read line ; do
927d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
928d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
929d311cd44SJoe Perches#		done
930d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
931948b133aSHeinrich Schuchardt		$id = undef;
932d311cd44SJoe Perches	} else {
933d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
934d311cd44SJoe Perches		$desc = substr($lines[0], 41);
935d311cd44SJoe Perches	}
936d311cd44SJoe Perches
937d311cd44SJoe Perches	return ($id, $desc);
938d311cd44SJoe Perches}
939d311cd44SJoe Perches
9406c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9410a920b5bSAndy Whitcroft
94200df344fSAndy Whitcroftmy @rawlines = ();
943c2fdda0dSAndy Whitcroftmy @lines = ();
9443705ce5bSJoe Perchesmy @fixed = ();
945d752fcc8SJoe Perchesmy @fixed_inserted = ();
946d752fcc8SJoe Perchesmy @fixed_deleted = ();
947194f66fcSJoe Perchesmy $fixlinenr = -1;
948194f66fcSJoe Perches
9494a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9504a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9514a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9524a593c34SDu, Changbin
9534a593c34SDu, Changbinif ($git) {
9544a593c34SDu, Changbin	my @commits = ();
9550dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
9564a593c34SDu, Changbin		my $git_range;
95728898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
95828898fd1SJoe Perches			$git_range = "-$2 $1";
9594a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
9604a593c34SDu, Changbin			$git_range = "$commit_expr";
9614a593c34SDu, Changbin		} else {
9620dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
9630dea9f1eSJoe Perches		}
9640dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
9650dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
96628898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
96728898fd1SJoe Perches			next if (!defined($1) || !defined($2));
9680dea9f1eSJoe Perches			my $sha1 = $1;
9690dea9f1eSJoe Perches			my $subject = $2;
9700dea9f1eSJoe Perches			unshift(@commits, $sha1);
9710dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
9724a593c34SDu, Changbin		}
9734a593c34SDu, Changbin	}
9744a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
9754a593c34SDu, Changbin	@ARGV = @commits;
9764a593c34SDu, Changbin}
9774a593c34SDu, Changbin
978c2fdda0dSAndy Whitcroftmy $vname;
9796c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
98021caa13cSAndy Whitcroft	my $FILE;
9814a593c34SDu, Changbin	if ($git) {
9824a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
9834a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
9844a593c34SDu, Changbin	} elsif ($file) {
98521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
9866c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
98721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
98821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
9896c72ffaaSAndy Whitcroft	} else {
99021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
9916c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
9926c72ffaaSAndy Whitcroft	}
993c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
994c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
9954a593c34SDu, Changbin	} elsif ($git) {
9960dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
997c2fdda0dSAndy Whitcroft	} else {
998c2fdda0dSAndy Whitcroft		$vname = $filename;
999c2fdda0dSAndy Whitcroft	}
100021caa13cSAndy Whitcroft	while (<$FILE>) {
10010a920b5bSAndy Whitcroft		chomp;
100200df344fSAndy Whitcroft		push(@rawlines, $_);
10036c72ffaaSAndy Whitcroft	}
100421caa13cSAndy Whitcroft	close($FILE);
1005d8469f16SJoe Perches
1006d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1007d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1008d8469f16SJoe Perches		print "$vname\n";
1009d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1010d8469f16SJoe Perches	}
1011d8469f16SJoe Perches
1012c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10130a920b5bSAndy Whitcroft		$exit = 1;
10140a920b5bSAndy Whitcroft	}
101500df344fSAndy Whitcroft	@rawlines = ();
101613214adfSAndy Whitcroft	@lines = ();
10173705ce5bSJoe Perches	@fixed = ();
1018d752fcc8SJoe Perches	@fixed_inserted = ();
1019d752fcc8SJoe Perches	@fixed_deleted = ();
1020194f66fcSJoe Perches	$fixlinenr = -1;
1021485ff23eSAlex Dowad	@modifierListFile = ();
1022485ff23eSAlex Dowad	@typeListFile = ();
1023485ff23eSAlex Dowad	build_types();
10240a920b5bSAndy Whitcroft}
10250a920b5bSAndy Whitcroft
1026d8469f16SJoe Perchesif (!$quiet) {
10273c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10283c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10293c816e49SJoe Perches
10305b57980dSJoe Perches	if (!$perl_version_ok) {
1031d8469f16SJoe Perches		print << "EOM"
1032d8469f16SJoe Perches
1033d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
10345b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1035d8469f16SJoe PerchesEOM
1036d8469f16SJoe Perches	}
1037d8469f16SJoe Perches	if ($exit) {
1038d8469f16SJoe Perches		print << "EOM"
1039d8469f16SJoe Perches
1040d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1041d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1042d8469f16SJoe PerchesEOM
1043d8469f16SJoe Perches	}
1044d8469f16SJoe Perches}
1045d8469f16SJoe Perches
10460a920b5bSAndy Whitcroftexit($exit);
10470a920b5bSAndy Whitcroft
10480a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10496c72ffaaSAndy Whitcroft	my ($root) = @_;
10506c72ffaaSAndy Whitcroft
10516c72ffaaSAndy Whitcroft	my @tree_check = (
10526c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
10536c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
10546c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
10556c72ffaaSAndy Whitcroft	);
10566c72ffaaSAndy Whitcroft
10576c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
10586c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
10590a920b5bSAndy Whitcroft			return 0;
10600a920b5bSAndy Whitcroft		}
10616c72ffaaSAndy Whitcroft	}
10626c72ffaaSAndy Whitcroft	return 1;
10636c72ffaaSAndy Whitcroft}
10640a920b5bSAndy Whitcroft
106520112475SJoe Perchessub parse_email {
106620112475SJoe Perches	my ($formatted_email) = @_;
106720112475SJoe Perches
106820112475SJoe Perches	my $name = "";
106920112475SJoe Perches	my $address = "";
107020112475SJoe Perches	my $comment = "";
107120112475SJoe Perches
107220112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
107320112475SJoe Perches		$name = $1;
107420112475SJoe Perches		$address = $2;
107520112475SJoe Perches		$comment = $3 if defined $3;
107620112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
107720112475SJoe Perches		$address = $1;
107820112475SJoe Perches		$comment = $2 if defined $2;
107920112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
108020112475SJoe Perches		$address = $1;
108120112475SJoe Perches		$comment = $2 if defined $2;
108285e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
108320112475SJoe Perches		$name = $formatted_email;
10843705ce5bSJoe Perches		$name = trim($name);
108520112475SJoe Perches		$name =~ s/^\"|\"$//g;
108620112475SJoe Perches		# If there's a name left after stripping spaces and
108720112475SJoe Perches		# leading quotes, and the address doesn't have both
108820112475SJoe Perches		# leading and trailing angle brackets, the address
108920112475SJoe Perches		# is invalid. ie:
109020112475SJoe Perches		#   "joe smith [email protected]" bad
109120112475SJoe Perches		#   "joe smith <[email protected]" bad
109220112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
109320112475SJoe Perches			$name = "";
109420112475SJoe Perches			$address = "";
109520112475SJoe Perches			$comment = "";
109620112475SJoe Perches		}
109720112475SJoe Perches	}
109820112475SJoe Perches
10993705ce5bSJoe Perches	$name = trim($name);
110020112475SJoe Perches	$name =~ s/^\"|\"$//g;
11013705ce5bSJoe Perches	$address = trim($address);
110220112475SJoe Perches	$address =~ s/^\<|\>$//g;
110320112475SJoe Perches
110420112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
110520112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
110620112475SJoe Perches		$name = "\"$name\"";
110720112475SJoe Perches	}
110820112475SJoe Perches
110920112475SJoe Perches	return ($name, $address, $comment);
111020112475SJoe Perches}
111120112475SJoe Perches
111220112475SJoe Perchessub format_email {
111320112475SJoe Perches	my ($name, $address) = @_;
111420112475SJoe Perches
111520112475SJoe Perches	my $formatted_email;
111620112475SJoe Perches
11173705ce5bSJoe Perches	$name = trim($name);
111820112475SJoe Perches	$name =~ s/^\"|\"$//g;
11193705ce5bSJoe Perches	$address = trim($address);
112020112475SJoe Perches
112120112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
112220112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
112320112475SJoe Perches		$name = "\"$name\"";
112420112475SJoe Perches	}
112520112475SJoe Perches
112620112475SJoe Perches	if ("$name" eq "") {
112720112475SJoe Perches		$formatted_email = "$address";
112820112475SJoe Perches	} else {
112920112475SJoe Perches		$formatted_email = "$name <$address>";
113020112475SJoe Perches	}
113120112475SJoe Perches
113220112475SJoe Perches	return $formatted_email;
113320112475SJoe Perches}
113420112475SJoe Perches
1135d311cd44SJoe Perchessub which {
1136d311cd44SJoe Perches	my ($bin) = @_;
1137d311cd44SJoe Perches
1138d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1139d311cd44SJoe Perches		if (-e "$path/$bin") {
1140d311cd44SJoe Perches			return "$path/$bin";
1141d311cd44SJoe Perches		}
1142d311cd44SJoe Perches	}
1143d311cd44SJoe Perches
1144d311cd44SJoe Perches	return "";
1145d311cd44SJoe Perches}
1146d311cd44SJoe Perches
1147000d1cc1SJoe Perchessub which_conf {
1148000d1cc1SJoe Perches	my ($conf) = @_;
1149000d1cc1SJoe Perches
1150000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1151000d1cc1SJoe Perches		if (-e "$path/$conf") {
1152000d1cc1SJoe Perches			return "$path/$conf";
1153000d1cc1SJoe Perches		}
1154000d1cc1SJoe Perches	}
1155000d1cc1SJoe Perches
1156000d1cc1SJoe Perches	return "";
1157000d1cc1SJoe Perches}
1158000d1cc1SJoe Perches
11590a920b5bSAndy Whitcroftsub expand_tabs {
11600a920b5bSAndy Whitcroft	my ($str) = @_;
11610a920b5bSAndy Whitcroft
11620a920b5bSAndy Whitcroft	my $res = '';
11630a920b5bSAndy Whitcroft	my $n = 0;
11640a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
11650a920b5bSAndy Whitcroft		if ($c eq "\t") {
11660a920b5bSAndy Whitcroft			$res .= ' ';
11670a920b5bSAndy Whitcroft			$n++;
11680a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
11690a920b5bSAndy Whitcroft				$res .= ' ';
11700a920b5bSAndy Whitcroft			}
11710a920b5bSAndy Whitcroft			next;
11720a920b5bSAndy Whitcroft		}
11730a920b5bSAndy Whitcroft		$res .= $c;
11740a920b5bSAndy Whitcroft		$n++;
11750a920b5bSAndy Whitcroft	}
11760a920b5bSAndy Whitcroft
11770a920b5bSAndy Whitcroft	return $res;
11780a920b5bSAndy Whitcroft}
11796c72ffaaSAndy Whitcroftsub copy_spacing {
1180773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
11816c72ffaaSAndy Whitcroft	return $res;
11826c72ffaaSAndy Whitcroft}
11830a920b5bSAndy Whitcroft
11844a0df2efSAndy Whitcroftsub line_stats {
11854a0df2efSAndy Whitcroft	my ($line) = @_;
11864a0df2efSAndy Whitcroft
11874a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
11884a0df2efSAndy Whitcroft	$line =~ s/^.//;
11894a0df2efSAndy Whitcroft	$line = expand_tabs($line);
11904a0df2efSAndy Whitcroft
11914a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
11924a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
11934a0df2efSAndy Whitcroft
11944a0df2efSAndy Whitcroft	return (length($line), length($white));
11954a0df2efSAndy Whitcroft}
11964a0df2efSAndy Whitcroft
1197773647a0SAndy Whitcroftmy $sanitise_quote = '';
1198773647a0SAndy Whitcroft
1199773647a0SAndy Whitcroftsub sanitise_line_reset {
1200773647a0SAndy Whitcroft	my ($in_comment) = @_;
1201773647a0SAndy Whitcroft
1202773647a0SAndy Whitcroft	if ($in_comment) {
1203773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1204773647a0SAndy Whitcroft	} else {
1205773647a0SAndy Whitcroft		$sanitise_quote = '';
1206773647a0SAndy Whitcroft	}
1207773647a0SAndy Whitcroft}
120800df344fSAndy Whitcroftsub sanitise_line {
120900df344fSAndy Whitcroft	my ($line) = @_;
121000df344fSAndy Whitcroft
121100df344fSAndy Whitcroft	my $res = '';
121200df344fSAndy Whitcroft	my $l = '';
121300df344fSAndy Whitcroft
1214c2fdda0dSAndy Whitcroft	my $qlen = 0;
1215773647a0SAndy Whitcroft	my $off = 0;
1216773647a0SAndy Whitcroft	my $c;
121700df344fSAndy Whitcroft
1218773647a0SAndy Whitcroft	# Always copy over the diff marker.
1219773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1220773647a0SAndy Whitcroft
1221773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1222773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1223773647a0SAndy Whitcroft
12248d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1225773647a0SAndy Whitcroft		# and end, all to $;.
1226773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1227773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1228773647a0SAndy Whitcroft
1229773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1230773647a0SAndy Whitcroft			$off++;
123100df344fSAndy Whitcroft			next;
1232773647a0SAndy Whitcroft		}
123381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1234773647a0SAndy Whitcroft			$sanitise_quote = '';
1235773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1236773647a0SAndy Whitcroft			$off++;
1237773647a0SAndy Whitcroft			next;
1238773647a0SAndy Whitcroft		}
1239113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1240113f04a8SDaniel Walker			$sanitise_quote = '//';
1241113f04a8SDaniel Walker
1242113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1243113f04a8SDaniel Walker			$off++;
1244113f04a8SDaniel Walker			next;
1245113f04a8SDaniel Walker		}
1246773647a0SAndy Whitcroft
1247773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1248773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1249773647a0SAndy Whitcroft		    $c eq "\\") {
1250773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1251773647a0SAndy Whitcroft			$off++;
1252773647a0SAndy Whitcroft			next;
1253773647a0SAndy Whitcroft		}
1254773647a0SAndy Whitcroft		# Regular quotes.
1255773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1256773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1257773647a0SAndy Whitcroft				$sanitise_quote = $c;
1258773647a0SAndy Whitcroft
1259773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1260773647a0SAndy Whitcroft				next;
1261773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1262773647a0SAndy Whitcroft				$sanitise_quote = '';
126300df344fSAndy Whitcroft			}
126400df344fSAndy Whitcroft		}
1265773647a0SAndy Whitcroft
1266fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1267773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1268773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1269113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1270113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1271773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1272773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
127300df344fSAndy Whitcroft		} else {
1274773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
127500df344fSAndy Whitcroft		}
1276c2fdda0dSAndy Whitcroft	}
1277c2fdda0dSAndy Whitcroft
1278113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1279113f04a8SDaniel Walker		$sanitise_quote = '';
1280113f04a8SDaniel Walker	}
1281113f04a8SDaniel Walker
1282c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1283c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1284c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1285c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1286c2fdda0dSAndy Whitcroft
1287c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1288c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1289c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1290c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1291c2fdda0dSAndy Whitcroft	}
1292c2fdda0dSAndy Whitcroft
1293dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1294dadf680dSJoe Perches		my $match = $1;
1295dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1296dadf680dSJoe Perches	}
1297dadf680dSJoe Perches
129800df344fSAndy Whitcroft	return $res;
129900df344fSAndy Whitcroft}
130000df344fSAndy Whitcroft
1301a6962d72SJoe Perchessub get_quoted_string {
1302a6962d72SJoe Perches	my ($line, $rawline) = @_;
1303a6962d72SJoe Perches
1304478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
130533acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1306a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1307a6962d72SJoe Perches}
1308a6962d72SJoe Perches
13098905a67cSAndy Whitcroftsub ctx_statement_block {
13108905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13118905a67cSAndy Whitcroft	my $line = $linenr - 1;
13128905a67cSAndy Whitcroft	my $blk = '';
13138905a67cSAndy Whitcroft	my $soff = $off;
13148905a67cSAndy Whitcroft	my $coff = $off - 1;
1315773647a0SAndy Whitcroft	my $coff_set = 0;
13168905a67cSAndy Whitcroft
131713214adfSAndy Whitcroft	my $loff = 0;
131813214adfSAndy Whitcroft
13198905a67cSAndy Whitcroft	my $type = '';
13208905a67cSAndy Whitcroft	my $level = 0;
1321a2750645SAndy Whitcroft	my @stack = ();
1322cf655043SAndy Whitcroft	my $p;
13238905a67cSAndy Whitcroft	my $c;
13248905a67cSAndy Whitcroft	my $len = 0;
132513214adfSAndy Whitcroft
132613214adfSAndy Whitcroft	my $remainder;
13278905a67cSAndy Whitcroft	while (1) {
1328a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1329a2750645SAndy Whitcroft
1330773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
13318905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
13328905a67cSAndy Whitcroft		# context.
13338905a67cSAndy Whitcroft		if ($off >= $len) {
13348905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1335dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1336c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13378905a67cSAndy Whitcroft				$remain--;
133813214adfSAndy Whitcroft				$loff = $len;
1339c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13408905a67cSAndy Whitcroft				$len = length($blk);
13418905a67cSAndy Whitcroft				$line++;
13428905a67cSAndy Whitcroft				last;
13438905a67cSAndy Whitcroft			}
13448905a67cSAndy Whitcroft			# Bail if there is no further context.
13458905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
134613214adfSAndy Whitcroft			if ($off >= $len) {
13478905a67cSAndy Whitcroft				last;
13488905a67cSAndy Whitcroft			}
1349f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1350f74bd194SAndy Whitcroft				$level++;
1351f74bd194SAndy Whitcroft				$type = '#';
1352f74bd194SAndy Whitcroft			}
13538905a67cSAndy Whitcroft		}
1354cf655043SAndy Whitcroft		$p = $c;
13558905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
135613214adfSAndy Whitcroft		$remainder = substr($blk, $off);
13578905a67cSAndy Whitcroft
1358773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
13594635f4fbSAndy Whitcroft
13604635f4fbSAndy Whitcroft		# Handle nested #if/#else.
13614635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
13624635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
13634635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
13644635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
13654635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
13664635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
13674635f4fbSAndy Whitcroft		}
13684635f4fbSAndy Whitcroft
13698905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
13708905a67cSAndy Whitcroft		# outermost level.
13718905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
13728905a67cSAndy Whitcroft			last;
13738905a67cSAndy Whitcroft		}
13748905a67cSAndy Whitcroft
137513214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1376773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1377773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1378773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1379773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1380773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1381773647a0SAndy Whitcroft			$coff_set = 1;
1382773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1383773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
138413214adfSAndy Whitcroft		}
138513214adfSAndy Whitcroft
13868905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
13878905a67cSAndy Whitcroft			$level++;
13888905a67cSAndy Whitcroft			$type = '(';
13898905a67cSAndy Whitcroft		}
13908905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
13918905a67cSAndy Whitcroft			$level--;
13928905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
13938905a67cSAndy Whitcroft
13948905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
13958905a67cSAndy Whitcroft				$coff = $off;
1396773647a0SAndy Whitcroft				$coff_set = 1;
1397773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
13988905a67cSAndy Whitcroft			}
13998905a67cSAndy Whitcroft		}
14008905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14018905a67cSAndy Whitcroft			$level++;
14028905a67cSAndy Whitcroft			$type = '{';
14038905a67cSAndy Whitcroft		}
14048905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14058905a67cSAndy Whitcroft			$level--;
14068905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14078905a67cSAndy Whitcroft
14088905a67cSAndy Whitcroft			if ($level == 0) {
1409b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1410b998e001SPatrick Pannuto					$off++;
1411b998e001SPatrick Pannuto				}
14128905a67cSAndy Whitcroft				last;
14138905a67cSAndy Whitcroft			}
14148905a67cSAndy Whitcroft		}
1415f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1416f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1417f74bd194SAndy Whitcroft			$level--;
1418f74bd194SAndy Whitcroft			$type = '';
1419f74bd194SAndy Whitcroft			$off++;
1420f74bd194SAndy Whitcroft			last;
1421f74bd194SAndy Whitcroft		}
14228905a67cSAndy Whitcroft		$off++;
14238905a67cSAndy Whitcroft	}
1424a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
142513214adfSAndy Whitcroft	if ($off == $len) {
1426a3bb97a7SAndy Whitcroft		$loff = $len + 1;
142713214adfSAndy Whitcroft		$line++;
142813214adfSAndy Whitcroft		$remain--;
142913214adfSAndy Whitcroft	}
14308905a67cSAndy Whitcroft
14318905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
14328905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
14338905a67cSAndy Whitcroft
14348905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
14358905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
14368905a67cSAndy Whitcroft
1437773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
143813214adfSAndy Whitcroft
143913214adfSAndy Whitcroft	return ($statement, $condition,
144013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
144113214adfSAndy Whitcroft}
144213214adfSAndy Whitcroft
1443cf655043SAndy Whitcroftsub statement_lines {
1444cf655043SAndy Whitcroft	my ($stmt) = @_;
1445cf655043SAndy Whitcroft
1446cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1447cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1448cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1449cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1450cf655043SAndy Whitcroft
1451cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1452cf655043SAndy Whitcroft
1453cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1454cf655043SAndy Whitcroft}
1455cf655043SAndy Whitcroft
1456cf655043SAndy Whitcroftsub statement_rawlines {
1457cf655043SAndy Whitcroft	my ($stmt) = @_;
1458cf655043SAndy Whitcroft
1459cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1460cf655043SAndy Whitcroft
1461cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1462cf655043SAndy Whitcroft}
1463cf655043SAndy Whitcroft
1464cf655043SAndy Whitcroftsub statement_block_size {
1465cf655043SAndy Whitcroft	my ($stmt) = @_;
1466cf655043SAndy Whitcroft
1467cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1468cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1469cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1470cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1471cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1472cf655043SAndy Whitcroft
1473cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1474cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1475cf655043SAndy Whitcroft
1476cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1477cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1478cf655043SAndy Whitcroft
1479cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1480cf655043SAndy Whitcroft		return $stmt_lines;
1481cf655043SAndy Whitcroft	} else {
1482cf655043SAndy Whitcroft		return $stmt_statements;
1483cf655043SAndy Whitcroft	}
1484cf655043SAndy Whitcroft}
1485cf655043SAndy Whitcroft
148613214adfSAndy Whitcroftsub ctx_statement_full {
148713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
148813214adfSAndy Whitcroft	my ($statement, $condition, $level);
148913214adfSAndy Whitcroft
149013214adfSAndy Whitcroft	my (@chunks);
149113214adfSAndy Whitcroft
1492cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
149313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
149413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1495773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
149613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1497cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1498cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1499cf655043SAndy Whitcroft	}
1500cf655043SAndy Whitcroft
1501cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1502cf655043SAndy Whitcroft	# could continue the statement.
1503cf655043SAndy Whitcroft	for (;;) {
150413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
150513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1506cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1507773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1508cf655043SAndy Whitcroft		#print "C: push\n";
1509cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
151013214adfSAndy Whitcroft	}
151113214adfSAndy Whitcroft
151213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15138905a67cSAndy Whitcroft}
15148905a67cSAndy Whitcroft
15154a0df2efSAndy Whitcroftsub ctx_block_get {
1516f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15174a0df2efSAndy Whitcroft	my $line;
15184a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15194a0df2efSAndy Whitcroft	my $blk = '';
15204a0df2efSAndy Whitcroft	my @o;
15214a0df2efSAndy Whitcroft	my @c;
15224a0df2efSAndy Whitcroft	my @res = ();
15234a0df2efSAndy Whitcroft
1524f0a594c1SAndy Whitcroft	my $level = 0;
15254635f4fbSAndy Whitcroft	my @stack = ($level);
152600df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
152700df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
152800df344fSAndy Whitcroft		$remain--;
152900df344fSAndy Whitcroft
153000df344fSAndy Whitcroft		$blk .= $rawlines[$line];
15314635f4fbSAndy Whitcroft
15324635f4fbSAndy Whitcroft		# Handle nested #if/#else.
153301464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
15344635f4fbSAndy Whitcroft			push(@stack, $level);
153501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
15364635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
153701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15384635f4fbSAndy Whitcroft			$level = pop(@stack);
15394635f4fbSAndy Whitcroft		}
15404635f4fbSAndy Whitcroft
154101464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1542f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1543f0a594c1SAndy Whitcroft			if ($off > 0) {
1544f0a594c1SAndy Whitcroft				$off--;
1545f0a594c1SAndy Whitcroft				next;
1546f0a594c1SAndy Whitcroft			}
15474a0df2efSAndy Whitcroft
1548f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1549f0a594c1SAndy Whitcroft				$level--;
1550f0a594c1SAndy Whitcroft				last if ($level == 0);
1551f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1552f0a594c1SAndy Whitcroft				$level++;
1553f0a594c1SAndy Whitcroft			}
1554f0a594c1SAndy Whitcroft		}
15554a0df2efSAndy Whitcroft
1556f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
155700df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
15584a0df2efSAndy Whitcroft		}
15594a0df2efSAndy Whitcroft
1560f0a594c1SAndy Whitcroft		last if ($level == 0);
15614a0df2efSAndy Whitcroft	}
15624a0df2efSAndy Whitcroft
1563f0a594c1SAndy Whitcroft	return ($level, @res);
15644a0df2efSAndy Whitcroft}
15654a0df2efSAndy Whitcroftsub ctx_block_outer {
15664a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15674a0df2efSAndy Whitcroft
1568f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1569f0a594c1SAndy Whitcroft	return @r;
15704a0df2efSAndy Whitcroft}
15714a0df2efSAndy Whitcroftsub ctx_block {
15724a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15734a0df2efSAndy Whitcroft
1574f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1575f0a594c1SAndy Whitcroft	return @r;
1576653d4876SAndy Whitcroft}
1577653d4876SAndy Whitcroftsub ctx_statement {
1578f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1579f0a594c1SAndy Whitcroft
1580f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1581f0a594c1SAndy Whitcroft	return @r;
1582f0a594c1SAndy Whitcroft}
1583f0a594c1SAndy Whitcroftsub ctx_block_level {
1584653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1585653d4876SAndy Whitcroft
1586f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
15874a0df2efSAndy Whitcroft}
15889c0ca6f9SAndy Whitcroftsub ctx_statement_level {
15899c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
15909c0ca6f9SAndy Whitcroft
15919c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
15929c0ca6f9SAndy Whitcroft}
15934a0df2efSAndy Whitcroft
15944a0df2efSAndy Whitcroftsub ctx_locate_comment {
15954a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15964a0df2efSAndy Whitcroft
15974a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1598beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
15994a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16004a0df2efSAndy Whitcroft
16014a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16024a0df2efSAndy Whitcroft	# comment.
16034a0df2efSAndy Whitcroft	my $in_comment = 0;
16044a0df2efSAndy Whitcroft	$current_comment = '';
16054a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
160600df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
160700df344fSAndy Whitcroft		#warn "           $line\n";
16084a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16094a0df2efSAndy Whitcroft			$in_comment = 1;
16104a0df2efSAndy Whitcroft		}
16114a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16124a0df2efSAndy Whitcroft			$in_comment = 1;
16134a0df2efSAndy Whitcroft		}
16144a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16154a0df2efSAndy Whitcroft			$current_comment = '';
16164a0df2efSAndy Whitcroft		}
16174a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16184a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16194a0df2efSAndy Whitcroft			$in_comment = 0;
16204a0df2efSAndy Whitcroft		}
16214a0df2efSAndy Whitcroft	}
16224a0df2efSAndy Whitcroft
16234a0df2efSAndy Whitcroft	chomp($current_comment);
16244a0df2efSAndy Whitcroft	return($current_comment);
16254a0df2efSAndy Whitcroft}
16264a0df2efSAndy Whitcroftsub ctx_has_comment {
16274a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16284a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
16294a0df2efSAndy Whitcroft
163000df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
16314a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
16324a0df2efSAndy Whitcroft
16334a0df2efSAndy Whitcroft	return ($cmt ne '');
16344a0df2efSAndy Whitcroft}
16354a0df2efSAndy Whitcroft
16364d001e4dSAndy Whitcroftsub raw_line {
16374d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16384d001e4dSAndy Whitcroft
16394d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16404d001e4dSAndy Whitcroft	$cnt++;
16414d001e4dSAndy Whitcroft
16424d001e4dSAndy Whitcroft	my $line;
16434d001e4dSAndy Whitcroft	while ($cnt) {
16444d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16454d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16464d001e4dSAndy Whitcroft		$cnt--;
16474d001e4dSAndy Whitcroft	}
16484d001e4dSAndy Whitcroft
16494d001e4dSAndy Whitcroft	return $line;
16504d001e4dSAndy Whitcroft}
16514d001e4dSAndy Whitcroft
16522a9f9d85STobin C. Hardingsub get_stat_real {
16532a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
16542a9f9d85STobin C. Harding
16552a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
16562a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
16572a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
16582a9f9d85STobin C. Harding	}
16592a9f9d85STobin C. Harding
16602a9f9d85STobin C. Harding	return $stat_real;
16612a9f9d85STobin C. Harding}
16622a9f9d85STobin C. Harding
1663e3d95a2aSTobin C. Hardingsub get_stat_here {
1664e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1665e3d95a2aSTobin C. Harding
1666e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1667e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1668e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1669e3d95a2aSTobin C. Harding	}
1670e3d95a2aSTobin C. Harding
1671e3d95a2aSTobin C. Harding	return $herectx;
1672e3d95a2aSTobin C. Harding}
1673e3d95a2aSTobin C. Harding
16740a920b5bSAndy Whitcroftsub cat_vet {
16750a920b5bSAndy Whitcroft	my ($vet) = @_;
16769c0ca6f9SAndy Whitcroft	my ($res, $coded);
16770a920b5bSAndy Whitcroft
16789c0ca6f9SAndy Whitcroft	$res = '';
16796c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
16806c72ffaaSAndy Whitcroft		$res .= $1;
16816c72ffaaSAndy Whitcroft		if ($2 ne '') {
16829c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
16836c72ffaaSAndy Whitcroft			$res .= $coded;
16846c72ffaaSAndy Whitcroft		}
16859c0ca6f9SAndy Whitcroft	}
16869c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
16870a920b5bSAndy Whitcroft
16889c0ca6f9SAndy Whitcroft	return $res;
16890a920b5bSAndy Whitcroft}
16900a920b5bSAndy Whitcroft
1691c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1692cf655043SAndy Whitcroftmy $av_pending;
1693c2fdda0dSAndy Whitcroftmy @av_paren_type;
16941f65f947SAndy Whitcroftmy $av_pend_colon;
1695c2fdda0dSAndy Whitcroft
1696c2fdda0dSAndy Whitcroftsub annotate_reset {
1697c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1698cf655043SAndy Whitcroft	$av_pending = '_';
1699cf655043SAndy Whitcroft	@av_paren_type = ('E');
17001f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1701c2fdda0dSAndy Whitcroft}
1702c2fdda0dSAndy Whitcroft
17036c72ffaaSAndy Whitcroftsub annotate_values {
17046c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
17056c72ffaaSAndy Whitcroft
17066c72ffaaSAndy Whitcroft	my $res;
17071f65f947SAndy Whitcroft	my $var = '_' x length($stream);
17086c72ffaaSAndy Whitcroft	my $cur = $stream;
17096c72ffaaSAndy Whitcroft
1710c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
17116c72ffaaSAndy Whitcroft
17126c72ffaaSAndy Whitcroft	while (length($cur)) {
1713773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1714cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1715171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
17166c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1717c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1718c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1719cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1720c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
17216c72ffaaSAndy Whitcroft			}
17226c72ffaaSAndy Whitcroft
1723c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
17249446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
17259446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1726addcdceaSAndy Whitcroft			$type = 'c';
17279446ef56SAndy Whitcroft
1728e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1729c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
17306c72ffaaSAndy Whitcroft			$type = 'T';
17316c72ffaaSAndy Whitcroft
1732389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1733389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1734389a2fe5SAndy Whitcroft			$type = 'T';
1735389a2fe5SAndy Whitcroft
1736c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1737171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1738c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1739171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1740171ae1a4SAndy Whitcroft			if ($2 ne '') {
1741cf655043SAndy Whitcroft				$av_pending = 'N';
1742171ae1a4SAndy Whitcroft			}
1743171ae1a4SAndy Whitcroft			$type = 'E';
1744171ae1a4SAndy Whitcroft
1745c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1746171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1747171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1748171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
17496c72ffaaSAndy Whitcroft
1750c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1751cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1752c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1753cf655043SAndy Whitcroft
1754cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1755cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1756171ae1a4SAndy Whitcroft			$type = 'E';
1757cf655043SAndy Whitcroft
1758c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1759cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1760cf655043SAndy Whitcroft			$av_preprocessor = 1;
1761cf655043SAndy Whitcroft
1762cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1763cf655043SAndy Whitcroft
1764171ae1a4SAndy Whitcroft			$type = 'E';
1765cf655043SAndy Whitcroft
1766c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1767cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1768cf655043SAndy Whitcroft
1769cf655043SAndy Whitcroft			$av_preprocessor = 1;
1770cf655043SAndy Whitcroft
1771cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1772cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1773cf655043SAndy Whitcroft			pop(@av_paren_type);
1774cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1775171ae1a4SAndy Whitcroft			$type = 'E';
17766c72ffaaSAndy Whitcroft
17776c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1778c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
17796c72ffaaSAndy Whitcroft
1780171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1781171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1782171ae1a4SAndy Whitcroft			$av_pending = $type;
1783171ae1a4SAndy Whitcroft			$type = 'N';
1784171ae1a4SAndy Whitcroft
17856c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1786c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
17876c72ffaaSAndy Whitcroft			if (defined $2) {
1788cf655043SAndy Whitcroft				$av_pending = 'V';
17896c72ffaaSAndy Whitcroft			}
17906c72ffaaSAndy Whitcroft			$type = 'N';
17916c72ffaaSAndy Whitcroft
179214b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1793c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
179414b111c1SAndy Whitcroft			$av_pending = 'E';
17956c72ffaaSAndy Whitcroft			$type = 'N';
17966c72ffaaSAndy Whitcroft
17971f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
17981f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
17991f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18001f65f947SAndy Whitcroft			$type = 'N';
18011f65f947SAndy Whitcroft
180214b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1803c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
18046c72ffaaSAndy Whitcroft			$type = 'N';
18056c72ffaaSAndy Whitcroft
18066c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1807c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1808cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1809cf655043SAndy Whitcroft			$av_pending = '_';
18106c72ffaaSAndy Whitcroft			$type = 'N';
18116c72ffaaSAndy Whitcroft
18126c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1813cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1814cf655043SAndy Whitcroft			if ($new_type ne '_') {
1815cf655043SAndy Whitcroft				$type = $new_type;
1816c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1817c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
18186c72ffaaSAndy Whitcroft			} else {
1819c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
18206c72ffaaSAndy Whitcroft			}
18216c72ffaaSAndy Whitcroft
1822c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1823c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1824c8cb2ca3SAndy Whitcroft			$type = 'V';
1825cf655043SAndy Whitcroft			$av_pending = 'V';
18266c72ffaaSAndy Whitcroft
18278e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
18288e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
18291f65f947SAndy Whitcroft				$av_pend_colon = 'B';
18308e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
18318e761b04SAndy Whitcroft				$av_pend_colon = 'L';
18321f65f947SAndy Whitcroft			}
18331f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
18341f65f947SAndy Whitcroft			$type = 'V';
18351f65f947SAndy Whitcroft
18366c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1837c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
18386c72ffaaSAndy Whitcroft			$type = 'V';
18396c72ffaaSAndy Whitcroft
18406c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1841c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
18426c72ffaaSAndy Whitcroft			$type = 'N';
18436c72ffaaSAndy Whitcroft
1844cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1845c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
184613214adfSAndy Whitcroft			$type = 'E';
18471f65f947SAndy Whitcroft			$av_pend_colon = 'O';
184813214adfSAndy Whitcroft
18498e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
18508e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
18518e761b04SAndy Whitcroft			$type = 'C';
18528e761b04SAndy Whitcroft
18531f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
18541f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
18551f65f947SAndy Whitcroft			$type = 'N';
18561f65f947SAndy Whitcroft
18571f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
18581f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
18591f65f947SAndy Whitcroft
18601f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
18611f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
18621f65f947SAndy Whitcroft				$type = 'E';
18631f65f947SAndy Whitcroft			} else {
18641f65f947SAndy Whitcroft				$type = 'N';
18651f65f947SAndy Whitcroft			}
18661f65f947SAndy Whitcroft			$av_pend_colon = 'O';
18671f65f947SAndy Whitcroft
18688e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
186913214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
18706c72ffaaSAndy Whitcroft			$type = 'N';
18716c72ffaaSAndy Whitcroft
18720d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
187374048ed8SAndy Whitcroft			my $variant;
187474048ed8SAndy Whitcroft
187574048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
187674048ed8SAndy Whitcroft			if ($type eq 'V') {
187774048ed8SAndy Whitcroft				$variant = 'B';
187874048ed8SAndy Whitcroft			} else {
187974048ed8SAndy Whitcroft				$variant = 'U';
188074048ed8SAndy Whitcroft			}
188174048ed8SAndy Whitcroft
188274048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
188374048ed8SAndy Whitcroft			$type = 'N';
188474048ed8SAndy Whitcroft
18856c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1886c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
18876c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
18886c72ffaaSAndy Whitcroft				$type = 'N';
18896c72ffaaSAndy Whitcroft			}
18906c72ffaaSAndy Whitcroft
18916c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1892c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
18936c72ffaaSAndy Whitcroft		}
18946c72ffaaSAndy Whitcroft		if (defined $1) {
18956c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
18966c72ffaaSAndy Whitcroft			$res .= $type x length($1);
18976c72ffaaSAndy Whitcroft		}
18986c72ffaaSAndy Whitcroft	}
18996c72ffaaSAndy Whitcroft
19001f65f947SAndy Whitcroft	return ($res, $var);
19016c72ffaaSAndy Whitcroft}
19026c72ffaaSAndy Whitcroft
19038905a67cSAndy Whitcroftsub possible {
190413214adfSAndy Whitcroft	my ($possible, $line) = @_;
19059a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
19060776e594SAndy Whitcroft		^(?:
19070776e594SAndy Whitcroft			$Modifier|
19080776e594SAndy Whitcroft			$Storage|
19090776e594SAndy Whitcroft			$Type|
19109a974fdbSAndy Whitcroft			DEFINE_\S+
19119a974fdbSAndy Whitcroft		)$|
19129a974fdbSAndy Whitcroft		^(?:
19130776e594SAndy Whitcroft			goto|
19140776e594SAndy Whitcroft			return|
19150776e594SAndy Whitcroft			case|
19160776e594SAndy Whitcroft			else|
19170776e594SAndy Whitcroft			asm|__asm__|
191889a88353SAndy Whitcroft			do|
191989a88353SAndy Whitcroft			\#|
192089a88353SAndy Whitcroft			\#\#|
19219a974fdbSAndy Whitcroft		)(?:\s|$)|
19220776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
19239a974fdbSAndy Whitcroft	    )}x;
19249a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
19259a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1926c45dcabdSAndy Whitcroft		# Check for modifiers.
1927c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1928c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1929c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1930c45dcabdSAndy Whitcroft
1931c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1932c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1933d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
19349a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1935d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1936485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1937d2506586SAndy Whitcroft				}
19389a974fdbSAndy Whitcroft			}
1939c45dcabdSAndy Whitcroft
1940c45dcabdSAndy Whitcroft		} else {
194113214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1942485ff23eSAlex Dowad			push(@typeListFile, $possible);
1943c45dcabdSAndy Whitcroft		}
19448905a67cSAndy Whitcroft		build_types();
19450776e594SAndy Whitcroft	} else {
19460776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
19478905a67cSAndy Whitcroft	}
19488905a67cSAndy Whitcroft}
19498905a67cSAndy Whitcroft
19506c72ffaaSAndy Whitcroftmy $prefix = '';
19516c72ffaaSAndy Whitcroft
1952000d1cc1SJoe Perchessub show_type {
1953cbec18afSJoe Perches	my ($type) = @_;
195491bfe484SJoe Perches
1955522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
1956522b837cSAlexey Dobriyan
1957cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1958cbec18afSJoe Perches
1959cbec18afSJoe Perches	return !defined $ignore_type{$type};
1960000d1cc1SJoe Perches}
1961000d1cc1SJoe Perches
1962f0a594c1SAndy Whitcroftsub report {
1963cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1964cbec18afSJoe Perches
1965cbec18afSJoe Perches	if (!show_type($type) ||
1966cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1967773647a0SAndy Whitcroft		return 0;
1968773647a0SAndy Whitcroft	}
196957230297SJoe Perches	my $output = '';
1970737c0767SJohn Brooks	if ($color) {
197157230297SJoe Perches		if ($level eq 'ERROR') {
197257230297SJoe Perches			$output .= RED;
197357230297SJoe Perches		} elsif ($level eq 'WARNING') {
197457230297SJoe Perches			$output .= YELLOW;
1975000d1cc1SJoe Perches		} else {
197657230297SJoe Perches			$output .= GREEN;
1977000d1cc1SJoe Perches		}
197857230297SJoe Perches	}
197957230297SJoe Perches	$output .= $prefix . $level . ':';
198057230297SJoe Perches	if ($show_types) {
1981737c0767SJohn Brooks		$output .= BLUE if ($color);
198257230297SJoe Perches		$output .= "$type:";
198357230297SJoe Perches	}
1984737c0767SJohn Brooks	$output .= RESET if ($color);
198557230297SJoe Perches	$output .= ' ' . $msg . "\n";
198634d8815fSJoe Perches
198734d8815fSJoe Perches	if ($showfile) {
198834d8815fSJoe Perches		my @lines = split("\n", $output, -1);
198934d8815fSJoe Perches		splice(@lines, 1, 1);
199034d8815fSJoe Perches		$output = join("\n", @lines);
199134d8815fSJoe Perches	}
199257230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
19938905a67cSAndy Whitcroft
199457230297SJoe Perches	push(our @report, $output);
1995773647a0SAndy Whitcroft
1996773647a0SAndy Whitcroft	return 1;
1997f0a594c1SAndy Whitcroft}
1998cbec18afSJoe Perches
1999f0a594c1SAndy Whitcroftsub report_dump {
200013214adfSAndy Whitcroft	our @report;
2001f0a594c1SAndy Whitcroft}
2002000d1cc1SJoe Perches
2003d752fcc8SJoe Perchessub fixup_current_range {
2004d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2005d752fcc8SJoe Perches
2006d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2007d752fcc8SJoe Perches		my $o = $1;
2008d752fcc8SJoe Perches		my $l = $2;
2009d752fcc8SJoe Perches		my $no = $o + $offset;
2010d752fcc8SJoe Perches		my $nl = $l + $length;
2011d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2012d752fcc8SJoe Perches	}
2013d752fcc8SJoe Perches}
2014d752fcc8SJoe Perches
2015d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2016d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2017d752fcc8SJoe Perches
2018d752fcc8SJoe Perches	my $range_last_linenr = 0;
2019d752fcc8SJoe Perches	my $delta_offset = 0;
2020d752fcc8SJoe Perches
2021d752fcc8SJoe Perches	my $old_linenr = 0;
2022d752fcc8SJoe Perches	my $new_linenr = 0;
2023d752fcc8SJoe Perches
2024d752fcc8SJoe Perches	my $next_insert = 0;
2025d752fcc8SJoe Perches	my $next_delete = 0;
2026d752fcc8SJoe Perches
2027d752fcc8SJoe Perches	my @lines = ();
2028d752fcc8SJoe Perches
2029d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2030d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2031d752fcc8SJoe Perches
2032d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2033d752fcc8SJoe Perches		my $save_line = 1;
2034d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2035323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2036d752fcc8SJoe Perches			$delta_offset = 0;
2037d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2038d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2039d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2040d752fcc8SJoe Perches		}
2041d752fcc8SJoe Perches
2042d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2043d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2044d752fcc8SJoe Perches			$save_line = 0;
2045d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2046d752fcc8SJoe Perches		}
2047d752fcc8SJoe Perches
2048d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2049d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2050d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2051d752fcc8SJoe Perches			$new_linenr++;
2052d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2053d752fcc8SJoe Perches		}
2054d752fcc8SJoe Perches
2055d752fcc8SJoe Perches		if ($save_line) {
2056d752fcc8SJoe Perches			push(@lines, $line);
2057d752fcc8SJoe Perches			$new_linenr++;
2058d752fcc8SJoe Perches		}
2059d752fcc8SJoe Perches
2060d752fcc8SJoe Perches		$old_linenr++;
2061d752fcc8SJoe Perches	}
2062d752fcc8SJoe Perches
2063d752fcc8SJoe Perches	return @lines;
2064d752fcc8SJoe Perches}
2065d752fcc8SJoe Perches
2066f2d7e4d4SJoe Perchessub fix_insert_line {
2067f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2068f2d7e4d4SJoe Perches
2069f2d7e4d4SJoe Perches	my $inserted = {
2070f2d7e4d4SJoe Perches		LINENR => $linenr,
2071f2d7e4d4SJoe Perches		LINE => $line,
2072f2d7e4d4SJoe Perches	};
2073f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2074f2d7e4d4SJoe Perches}
2075f2d7e4d4SJoe Perches
2076f2d7e4d4SJoe Perchessub fix_delete_line {
2077f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2078f2d7e4d4SJoe Perches
2079f2d7e4d4SJoe Perches	my $deleted = {
2080f2d7e4d4SJoe Perches		LINENR => $linenr,
2081f2d7e4d4SJoe Perches		LINE => $line,
2082f2d7e4d4SJoe Perches	};
2083f2d7e4d4SJoe Perches
2084f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2085f2d7e4d4SJoe Perches}
2086f2d7e4d4SJoe Perches
2087de7d4f0eSAndy Whitcroftsub ERROR {
2088cbec18afSJoe Perches	my ($type, $msg) = @_;
2089cbec18afSJoe Perches
2090cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2091de7d4f0eSAndy Whitcroft		our $clean = 0;
20926c72ffaaSAndy Whitcroft		our $cnt_error++;
20933705ce5bSJoe Perches		return 1;
2094de7d4f0eSAndy Whitcroft	}
20953705ce5bSJoe Perches	return 0;
2096773647a0SAndy Whitcroft}
2097de7d4f0eSAndy Whitcroftsub WARN {
2098cbec18afSJoe Perches	my ($type, $msg) = @_;
2099cbec18afSJoe Perches
2100cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2101de7d4f0eSAndy Whitcroft		our $clean = 0;
21026c72ffaaSAndy Whitcroft		our $cnt_warn++;
21033705ce5bSJoe Perches		return 1;
2104de7d4f0eSAndy Whitcroft	}
21053705ce5bSJoe Perches	return 0;
2106773647a0SAndy Whitcroft}
2107de7d4f0eSAndy Whitcroftsub CHK {
2108cbec18afSJoe Perches	my ($type, $msg) = @_;
2109cbec18afSJoe Perches
2110cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2111de7d4f0eSAndy Whitcroft		our $clean = 0;
21126c72ffaaSAndy Whitcroft		our $cnt_chk++;
21133705ce5bSJoe Perches		return 1;
21146c72ffaaSAndy Whitcroft	}
21153705ce5bSJoe Perches	return 0;
2116de7d4f0eSAndy Whitcroft}
2117de7d4f0eSAndy Whitcroft
21186ecd9674SAndy Whitcroftsub check_absolute_file {
21196ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
21206ecd9674SAndy Whitcroft	my $file = $absolute;
21216ecd9674SAndy Whitcroft
21226ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
21236ecd9674SAndy Whitcroft
21246ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
21256ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
21266ecd9674SAndy Whitcroft		if (-f "$root/$file") {
21276ecd9674SAndy Whitcroft			##print "file<$file>\n";
21286ecd9674SAndy Whitcroft			last;
21296ecd9674SAndy Whitcroft		}
21306ecd9674SAndy Whitcroft	}
21316ecd9674SAndy Whitcroft	if (! -f _)  {
21326ecd9674SAndy Whitcroft		return 0;
21336ecd9674SAndy Whitcroft	}
21346ecd9674SAndy Whitcroft
21356ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
21366ecd9674SAndy Whitcroft	my $prefix = $absolute;
21376ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
21386ecd9674SAndy Whitcroft
21396ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
21406ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2141000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2142000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
21436ecd9674SAndy Whitcroft	}
21446ecd9674SAndy Whitcroft}
21456ecd9674SAndy Whitcroft
21463705ce5bSJoe Perchessub trim {
21473705ce5bSJoe Perches	my ($string) = @_;
21483705ce5bSJoe Perches
2149b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2150b34c648bSJoe Perches
2151b34c648bSJoe Perches	return $string;
2152b34c648bSJoe Perches}
2153b34c648bSJoe Perches
2154b34c648bSJoe Perchessub ltrim {
2155b34c648bSJoe Perches	my ($string) = @_;
2156b34c648bSJoe Perches
2157b34c648bSJoe Perches	$string =~ s/^\s+//;
2158b34c648bSJoe Perches
2159b34c648bSJoe Perches	return $string;
2160b34c648bSJoe Perches}
2161b34c648bSJoe Perches
2162b34c648bSJoe Perchessub rtrim {
2163b34c648bSJoe Perches	my ($string) = @_;
2164b34c648bSJoe Perches
2165b34c648bSJoe Perches	$string =~ s/\s+$//;
21663705ce5bSJoe Perches
21673705ce5bSJoe Perches	return $string;
21683705ce5bSJoe Perches}
21693705ce5bSJoe Perches
217052ea8506SJoe Perchessub string_find_replace {
217152ea8506SJoe Perches	my ($string, $find, $replace) = @_;
217252ea8506SJoe Perches
217352ea8506SJoe Perches	$string =~ s/$find/$replace/g;
217452ea8506SJoe Perches
217552ea8506SJoe Perches	return $string;
217652ea8506SJoe Perches}
217752ea8506SJoe Perches
21783705ce5bSJoe Perchessub tabify {
21793705ce5bSJoe Perches	my ($leading) = @_;
21803705ce5bSJoe Perches
21813705ce5bSJoe Perches	my $source_indent = 8;
21823705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
21833705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
21843705ce5bSJoe Perches
21853705ce5bSJoe Perches	#convert leading spaces to tabs
21863705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
21873705ce5bSJoe Perches	#Remove spaces before a tab
21883705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
21893705ce5bSJoe Perches
21903705ce5bSJoe Perches	return "$leading";
21913705ce5bSJoe Perches}
21923705ce5bSJoe Perches
2193d1fe9c09SJoe Perchessub pos_last_openparen {
2194d1fe9c09SJoe Perches	my ($line) = @_;
2195d1fe9c09SJoe Perches
2196d1fe9c09SJoe Perches	my $pos = 0;
2197d1fe9c09SJoe Perches
2198d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2199d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2200d1fe9c09SJoe Perches
2201d1fe9c09SJoe Perches	my $last_openparen = 0;
2202d1fe9c09SJoe Perches
2203d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2204d1fe9c09SJoe Perches		return -1;
2205d1fe9c09SJoe Perches	}
2206d1fe9c09SJoe Perches
2207d1fe9c09SJoe Perches	my $len = length($line);
2208d1fe9c09SJoe Perches
2209d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2210d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2211d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2212d1fe9c09SJoe Perches			$pos += length($1) - 1;
2213d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2214d1fe9c09SJoe Perches			$last_openparen = $pos;
2215d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2216d1fe9c09SJoe Perches			last;
2217d1fe9c09SJoe Perches		}
2218d1fe9c09SJoe Perches	}
2219d1fe9c09SJoe Perches
222091cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2221d1fe9c09SJoe Perches}
2222d1fe9c09SJoe Perches
22230a920b5bSAndy Whitcroftsub process {
22240a920b5bSAndy Whitcroft	my $filename = shift;
22250a920b5bSAndy Whitcroft
22260a920b5bSAndy Whitcroft	my $linenr=0;
22270a920b5bSAndy Whitcroft	my $prevline="";
2228c2fdda0dSAndy Whitcroft	my $prevrawline="";
22290a920b5bSAndy Whitcroft	my $stashline="";
2230c2fdda0dSAndy Whitcroft	my $stashrawline="";
22310a920b5bSAndy Whitcroft
22324a0df2efSAndy Whitcroft	my $length;
22330a920b5bSAndy Whitcroft	my $indent;
22340a920b5bSAndy Whitcroft	my $previndent=0;
22350a920b5bSAndy Whitcroft	my $stashindent=0;
22360a920b5bSAndy Whitcroft
2237de7d4f0eSAndy Whitcroft	our $clean = 1;
22380a920b5bSAndy Whitcroft	my $signoff = 0;
22390a920b5bSAndy Whitcroft	my $is_patch = 0;
224029ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
224115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2242ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2243bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
22442a076f40SJoe Perches	my $commit_log_long_line = 0;
2245e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
224613f1937eSJoe Perches	my $reported_maintainer_file = 0;
2247fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2248fa64205dSPasi Savanainen
2249365dd4eaSJoe Perches	my $last_blank_line = 0;
22505e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2251365dd4eaSJoe Perches
225213214adfSAndy Whitcroft	our @report = ();
22536c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
22546c72ffaaSAndy Whitcroft	our $cnt_error = 0;
22556c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
22566c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
22576c72ffaaSAndy Whitcroft
22580a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
22590a920b5bSAndy Whitcroft	my $realfile = '';
22600a920b5bSAndy Whitcroft	my $realline = 0;
22610a920b5bSAndy Whitcroft	my $realcnt = 0;
22620a920b5bSAndy Whitcroft	my $here = '';
226377cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
22640a920b5bSAndy Whitcroft	my $in_comment = 0;
2265c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
22660a920b5bSAndy Whitcroft	my $first_line = 0;
22671e855726SWolfram Sang	my $p1_prefix = '';
22680a920b5bSAndy Whitcroft
226913214adfSAndy Whitcroft	my $prev_values = 'E';
227013214adfSAndy Whitcroft
227113214adfSAndy Whitcroft	# suppression flags
2272773647a0SAndy Whitcroft	my %suppress_ifbraces;
2273170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
22742b474a1aSAndy Whitcroft	my %suppress_export;
22753e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2276653d4876SAndy Whitcroft
22777e51f197SJoe Perches	my %signatures = ();
2278323c1260SJoe Perches
2279c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2280de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2281c2fdda0dSAndy Whitcroft	#
2282de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2283de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2284773647a0SAndy Whitcroft
2285d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2286d8b07710SJoe Perches
22879f3a8992SRob Herring	my $checklicenseline = 1;
22889f3a8992SRob Herring
2289773647a0SAndy Whitcroft	sanitise_line_reset();
2290c2fdda0dSAndy Whitcroft	my $line;
2291c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2292773647a0SAndy Whitcroft		$linenr++;
2293773647a0SAndy Whitcroft		$line = $rawline;
2294c2fdda0dSAndy Whitcroft
22953705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
22963705ce5bSJoe Perches
2297773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2298de7d4f0eSAndy Whitcroft			$setup_docs = 0;
22998c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2300de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2301de7d4f0eSAndy Whitcroft			}
2302773647a0SAndy Whitcroft			#next;
2303de7d4f0eSAndy Whitcroft		}
230474fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2305773647a0SAndy Whitcroft			$realline=$1-1;
2306773647a0SAndy Whitcroft			if (defined $2) {
2307773647a0SAndy Whitcroft				$realcnt=$3+1;
2308773647a0SAndy Whitcroft			} else {
2309773647a0SAndy Whitcroft				$realcnt=1+1;
2310773647a0SAndy Whitcroft			}
2311c45dcabdSAndy Whitcroft			$in_comment = 0;
2312773647a0SAndy Whitcroft
2313773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2314773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2315773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2316773647a0SAndy Whitcroft			# at context start.
2317773647a0SAndy Whitcroft			my $edge;
231801fa9147SAndy Whitcroft			my $cnt = $realcnt;
231901fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
232001fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
232101fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
232201fa9147SAndy Whitcroft				$cnt--;
232301fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2324721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2325fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2326fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2327fae17daeSAndy Whitcroft					($edge) = $1;
2328fae17daeSAndy Whitcroft					last;
2329fae17daeSAndy Whitcroft				}
2330773647a0SAndy Whitcroft			}
2331773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2332773647a0SAndy Whitcroft				$in_comment = 1;
2333773647a0SAndy Whitcroft			}
2334773647a0SAndy Whitcroft
2335773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2336773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2337773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2338773647a0SAndy Whitcroft			if (!defined $edge &&
233983242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2340773647a0SAndy Whitcroft			{
2341773647a0SAndy Whitcroft				$in_comment = 1;
2342773647a0SAndy Whitcroft			}
2343773647a0SAndy Whitcroft
2344773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2345773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2346773647a0SAndy Whitcroft
2347171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2348773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2349171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2350773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2351773647a0SAndy Whitcroft		}
2352773647a0SAndy Whitcroft		push(@lines, $line);
2353773647a0SAndy Whitcroft
2354773647a0SAndy Whitcroft		if ($realcnt > 1) {
2355773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2356773647a0SAndy Whitcroft		} else {
2357773647a0SAndy Whitcroft			$realcnt = 0;
2358773647a0SAndy Whitcroft		}
2359773647a0SAndy Whitcroft
2360773647a0SAndy Whitcroft		#print "==>$rawline\n";
2361773647a0SAndy Whitcroft		#print "-->$line\n";
2362de7d4f0eSAndy Whitcroft
2363de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2364de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2365de7d4f0eSAndy Whitcroft		}
2366de7d4f0eSAndy Whitcroft	}
2367de7d4f0eSAndy Whitcroft
23686c72ffaaSAndy Whitcroft	$prefix = '';
23696c72ffaaSAndy Whitcroft
2370773647a0SAndy Whitcroft	$realcnt = 0;
2371773647a0SAndy Whitcroft	$linenr = 0;
2372194f66fcSJoe Perches	$fixlinenr = -1;
23730a920b5bSAndy Whitcroft	foreach my $line (@lines) {
23740a920b5bSAndy Whitcroft		$linenr++;
2375194f66fcSJoe Perches		$fixlinenr++;
23761b5539b1SJoe Perches		my $sline = $line;	#copy of $line
23771b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
23780a920b5bSAndy Whitcroft
2379c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
23806c72ffaaSAndy Whitcroft
238112c253abSJoe Perches# check if it's a mode change, rename or start of a patch
238212c253abSJoe Perches		if (!$in_commit_log &&
238312c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
238412c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
238512c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
238612c253abSJoe Perches			$is_patch = 1;
238712c253abSJoe Perches		}
238812c253abSJoe Perches
23890a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2390e518e9a5SJoe Perches		if (!$in_commit_log &&
239174fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
239274fd4f34SJoe Perches			my $context = $4;
23930a920b5bSAndy Whitcroft			$is_patch = 1;
23944a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
23950a920b5bSAndy Whitcroft			$realline=$1-1;
23960a920b5bSAndy Whitcroft			if (defined $2) {
23970a920b5bSAndy Whitcroft				$realcnt=$3+1;
23980a920b5bSAndy Whitcroft			} else {
23990a920b5bSAndy Whitcroft				$realcnt=1+1;
24000a920b5bSAndy Whitcroft			}
2401c2fdda0dSAndy Whitcroft			annotate_reset();
240213214adfSAndy Whitcroft			$prev_values = 'E';
240313214adfSAndy Whitcroft
2404773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2405170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
24062b474a1aSAndy Whitcroft			%suppress_export = ();
24073e469cdcSAndy Whitcroft			$suppress_statement = 0;
240874fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
240974fd4f34SJoe Perches				$context_function = $1;
241074fd4f34SJoe Perches			} else {
241174fd4f34SJoe Perches				undef $context_function;
241274fd4f34SJoe Perches			}
24130a920b5bSAndy Whitcroft			next;
24140a920b5bSAndy Whitcroft
24154a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
24164a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
24174a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2418773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
24190a920b5bSAndy Whitcroft			$realline++;
2420d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
24210a920b5bSAndy Whitcroft
24224a0df2efSAndy Whitcroft			# Measure the line length and indent.
2423c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
24240a920b5bSAndy Whitcroft
24250a920b5bSAndy Whitcroft			# Track the previous line.
24260a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
24270a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2428c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2429c2fdda0dSAndy Whitcroft
2430773647a0SAndy Whitcroft			#warn "line<$line>\n";
24316c72ffaaSAndy Whitcroft
2432d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2433d8aaf121SAndy Whitcroft			$realcnt--;
24340a920b5bSAndy Whitcroft		}
24350a920b5bSAndy Whitcroft
2436cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2437cc77cdcaSAndy Whitcroft
24386c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
24396c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2440773647a0SAndy Whitcroft
24412ac73b4fSJoe Perches		my $found_file = 0;
2442773647a0SAndy Whitcroft		# extract the filename as it passes
24433bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
24443bf9a009SRabin Vincent			$realfile = $1;
24452b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2446270c49a0SJoe Perches			$in_commit_log = 0;
24472ac73b4fSJoe Perches			$found_file = 1;
24483bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2449773647a0SAndy Whitcroft			$realfile = $1;
24502b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2451270c49a0SJoe Perches			$in_commit_log = 0;
24521e855726SWolfram Sang
24531e855726SWolfram Sang			$p1_prefix = $1;
2454e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2455e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2456000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2457000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
24581e855726SWolfram Sang			}
2459773647a0SAndy Whitcroft
2460c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2461000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2462000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2463773647a0SAndy Whitcroft			}
24642ac73b4fSJoe Perches			$found_file = 1;
24652ac73b4fSJoe Perches		}
24662ac73b4fSJoe Perches
246734d8815fSJoe Perches#make up the handle for any error we report on this line
246834d8815fSJoe Perches		if ($showfile) {
246934d8815fSJoe Perches			$prefix = "$realfile:$realline: "
247034d8815fSJoe Perches		} elsif ($emacs) {
24717d3a9f67SJoe Perches			if ($file) {
24727d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
24737d3a9f67SJoe Perches			} else {
247434d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
247534d8815fSJoe Perches			}
24767d3a9f67SJoe Perches		}
247734d8815fSJoe Perches
24782ac73b4fSJoe Perches		if ($found_file) {
247985b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
248085b0ee18SJoe Perches				WARN("OBSOLETE",
248185b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
248285b0ee18SJoe Perches			}
24837bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
24842ac73b4fSJoe Perches				$check = 1;
24852ac73b4fSJoe Perches			} else {
24862ac73b4fSJoe Perches				$check = $check_orig;
24872ac73b4fSJoe Perches			}
24889f3a8992SRob Herring			$checklicenseline = 1;
2489773647a0SAndy Whitcroft			next;
2490773647a0SAndy Whitcroft		}
2491773647a0SAndy Whitcroft
2492389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
24930a920b5bSAndy Whitcroft
2494c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2495c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2496c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
24970a920b5bSAndy Whitcroft
24986c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
24996c72ffaaSAndy Whitcroft
2500e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2501e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2502e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2503e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2504e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2505e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2506e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2507e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2508e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2509e518e9a5SJoe Perches		}
2510e518e9a5SJoe Perches
25113bf9a009SRabin Vincent# Check for incorrect file permissions
25123bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
25133bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
251404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
251504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2516000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2517000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
25183bf9a009SRabin Vincent			}
25193bf9a009SRabin Vincent		}
25203bf9a009SRabin Vincent
252120112475SJoe Perches# Check the patch for a signoff:
2522d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
25234a0df2efSAndy Whitcroft			$signoff++;
252415662b3eSJoe Perches			$in_commit_log = 0;
25250a920b5bSAndy Whitcroft		}
252620112475SJoe Perches
2527e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2528e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2529e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2530e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2531e0d975b1SJoe Perches		}
2532e0d975b1SJoe Perches
253320112475SJoe Perches# Check signature styles
2534270c49a0SJoe Perches		if (!$in_header_lines &&
2535ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
253620112475SJoe Perches			my $space_before = $1;
253720112475SJoe Perches			my $sign_off = $2;
253820112475SJoe Perches			my $space_after = $3;
253920112475SJoe Perches			my $email = $4;
254020112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
254120112475SJoe Perches
2542ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2543ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2544ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2545ce0338dfSJoe Perches			}
254620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
25473705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
25483705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
25493705ce5bSJoe Perches				    $fix) {
2550194f66fcSJoe Perches					$fixed[$fixlinenr] =
25513705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
25523705ce5bSJoe Perches				}
255320112475SJoe Perches			}
255420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
25553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
25563705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
25573705ce5bSJoe Perches				    $fix) {
2558194f66fcSJoe Perches					$fixed[$fixlinenr] =
25593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
25603705ce5bSJoe Perches				}
25613705ce5bSJoe Perches
256220112475SJoe Perches			}
256320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
25643705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
25653705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
25663705ce5bSJoe Perches				    $fix) {
2567194f66fcSJoe Perches					$fixed[$fixlinenr] =
25683705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
25693705ce5bSJoe Perches				}
257020112475SJoe Perches			}
257120112475SJoe Perches
257220112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
257320112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
257420112475SJoe Perches			if ($suggested_email eq "") {
2575000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2576000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
257720112475SJoe Perches			} else {
257820112475SJoe Perches				my $dequoted = $suggested_email;
257920112475SJoe Perches				$dequoted =~ s/^"//;
258020112475SJoe Perches				$dequoted =~ s/" </ </;
258120112475SJoe Perches				# Don't force email to have quotes
258220112475SJoe Perches				# Allow just an angle bracketed address
258320112475SJoe Perches				if ("$dequoted$comment" ne $email &&
258420112475SJoe Perches				    "<$email_address>$comment" ne $email &&
258520112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2586000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2587000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
258820112475SJoe Perches				}
25890a920b5bSAndy Whitcroft			}
25907e51f197SJoe Perches
25917e51f197SJoe Perches# Check for duplicate signatures
25927e51f197SJoe Perches			my $sig_nospace = $line;
25937e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
25947e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
25957e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
25967e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
25977e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
25987e51f197SJoe Perches			} else {
25997e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
26007e51f197SJoe Perches			}
26010a920b5bSAndy Whitcroft		}
26020a920b5bSAndy Whitcroft
2603a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2604a2fe16b9SJoe Perches		if ($in_header_lines &&
2605a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2606a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2607a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2608a2fe16b9SJoe Perches		}
2609a2fe16b9SJoe Perches
26107ebd05efSChristopher Covington# Check for unwanted Gerrit info
26117ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
26127ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
26137ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
26147ebd05efSChristopher Covington		}
26157ebd05efSChristopher Covington
2616369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2617369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2618369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2619369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2620369c8dd3SJoe Perches					# timestamp
2621369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2622369c8dd3SJoe Perches					# stack dump address
2623369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2624369c8dd3SJoe Perches		}
2625369c8dd3SJoe Perches
26262a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
26272a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2628bf4daf12SJoe Perches		    length($line) > 75 &&
2629bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2630bf4daf12SJoe Perches					# file delta changes
2631bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2632bf4daf12SJoe Perches					# filename then :
2633bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2634bf4daf12SJoe Perches					# A Fixes: or Link: line
2635bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
26362a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
26372a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
26382a076f40SJoe Perches			$commit_log_long_line = 1;
26392a076f40SJoe Perches		}
26402a076f40SJoe Perches
2641bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2642bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2643bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2644bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2645bf4daf12SJoe Perches		}
2646bf4daf12SJoe Perches
26470d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2648369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2649aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2650e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2651fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2652aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2653369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2654bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2655fe043ea1SJoe Perches			my $init_char = "c";
2656fe043ea1SJoe Perches			my $orig_commit = "";
26570d7835fcSJoe Perches			my $short = 1;
26580d7835fcSJoe Perches			my $long = 0;
26590d7835fcSJoe Perches			my $case = 1;
26600d7835fcSJoe Perches			my $space = 1;
26610d7835fcSJoe Perches			my $hasdesc = 0;
266219c146a6SJoe Perches			my $hasparens = 0;
26630d7835fcSJoe Perches			my $id = '0123456789ab';
26640d7835fcSJoe Perches			my $orig_desc = "commit description";
26650d7835fcSJoe Perches			my $description = "";
26660d7835fcSJoe Perches
2667fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2668fe043ea1SJoe Perches				$init_char = $1;
2669fe043ea1SJoe Perches				$orig_commit = lc($2);
2670fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2671fe043ea1SJoe Perches				$orig_commit = lc($1);
2672fe043ea1SJoe Perches			}
2673fe043ea1SJoe Perches
26740d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
26750d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
26760d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
26770d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
26780d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
26790d7835fcSJoe Perches				$orig_desc = $1;
268019c146a6SJoe Perches				$hasparens = 1;
26810d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
26820d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
26830d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
26840d7835fcSJoe Perches				$orig_desc = $1;
268519c146a6SJoe Perches				$hasparens = 1;
2686b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2687b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2688b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2689b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2690b671fde0SJoe Perches				$orig_desc = $1;
2691b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2692b671fde0SJoe Perches				$orig_desc .= " " . $1;
269319c146a6SJoe Perches				$hasparens = 1;
26940d7835fcSJoe Perches			}
26950d7835fcSJoe Perches
26960d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
26970d7835fcSJoe Perches							      $id, $orig_desc);
26980d7835fcSJoe Perches
2699948b133aSHeinrich Schuchardt			if (defined($id) &&
2700948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2701d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
27020d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
27030d7835fcSJoe Perches			}
2704d311cd44SJoe Perches		}
2705d311cd44SJoe Perches
270613f1937eSJoe Perches# Check for added, moved or deleted files
270713f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
270813f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
270913f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
271013f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
271113f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2712a82603a8SAndrew Jeffery			$is_patch = 1;
271313f1937eSJoe Perches			$reported_maintainer_file = 1;
271413f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
271513f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
271613f1937eSJoe Perches		}
271713f1937eSJoe Perches
271800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
27198905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2720000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2721000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
27226c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2723de7d4f0eSAndy Whitcroft		}
2724de7d4f0eSAndy Whitcroft
2725de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2726de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2727171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2728171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2729171ae1a4SAndy Whitcroft
2730171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2731171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2732171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2733171ae1a4SAndy Whitcroft
273434d99219SJoe Perches			CHK("INVALID_UTF8",
2735000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
273600df344fSAndy Whitcroft		}
27370a920b5bSAndy Whitcroft
273815662b3eSJoe Perches# Check if it's the start of a commit log
273915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
274015662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2741eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2742eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
274315662b3eSJoe Perches			$in_header_lines = 0;
274415662b3eSJoe Perches			$in_commit_log = 1;
2745ed43c4e5SAllen Hubbe			$has_commit_log = 1;
274615662b3eSJoe Perches		}
274715662b3eSJoe Perches
2748fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2749fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2750fa64205dSPasi Savanainen		if ($in_header_lines &&
2751fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2752fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2753fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2754fa64205dSPasi Savanainen		}
2755fa64205dSPasi Savanainen
2756fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
275715662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2758fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
275915662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
276015662b3eSJoe Perches		}
276115662b3eSJoe Perches
2762d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2763d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2764d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2765d6430f71SJoe Perches				my $file = $1;
2766d6430f71SJoe Perches
2767d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2768d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2769d6430f71SJoe Perches					#
2770d6430f71SJoe Perches				} else {
2771d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2772d6430f71SJoe Perches				}
2773d6430f71SJoe Perches			}
2774d6430f71SJoe Perches		}
2775d6430f71SJoe Perches
277666b47b4aSKees Cook# Check for various typo / spelling mistakes
277766d7a382SJoe Perches		if (defined($misspellings) &&
277866d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2779ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
278066b47b4aSKees Cook				my $typo = $1;
278166b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
278266b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
278366b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
27840675a8fbSJean Delvare				my $msg_level = \&WARN;
27850675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
27860675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
278766b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
278866b47b4aSKees Cook				    $fix) {
278966b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
279066b47b4aSKees Cook				}
279166b47b4aSKees Cook			}
279266b47b4aSKees Cook		}
279366b47b4aSKees Cook
279430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
279530670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
279600df344fSAndy Whitcroft
27970a920b5bSAndy Whitcroft#trailing whitespace
27989c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2799c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2800d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2801d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2802d5e616fcSJoe Perches			    $fix) {
2803194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2804d5e616fcSJoe Perches			}
2805c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2806c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28073705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
28083705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
28093705ce5bSJoe Perches			    $fix) {
2810194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
28113705ce5bSJoe Perches			}
28123705ce5bSJoe Perches
2813d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
28140a920b5bSAndy Whitcroft		}
28155368df20SAndy Whitcroft
28164783f894SJosh Triplett# Check for FSF mailing addresses.
2817109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
28181bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
28193e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
28203e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
28214783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28220675a8fbSJean Delvare			my $msg_level = \&ERROR;
28230675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
28240675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
28254783f894SJosh 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)
28264783f894SJosh Triplett		}
28274783f894SJosh Triplett
28283354957aSAndi Kleen# check for Kconfig help text having a real description
28299fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
28309fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
28313354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2832678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
2833678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
2834678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
2835678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
28363354957aSAndi Kleen			my $length = 0;
28379fe287d7SAndy Whitcroft			my $cnt = $realcnt;
28389fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
28399fe287d7SAndy Whitcroft			my $f;
2840a1385803SAndy Whitcroft			my $is_start = 0;
28419fe287d7SAndy Whitcroft			my $is_end = 0;
2842a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
28439fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
28449fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
28459fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
28469fe287d7SAndy Whitcroft
28479fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
28488d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2849a1385803SAndy Whitcroft
285086adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
2851a1385803SAndy Whitcroft					$is_start = 1;
285284af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
285384af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
285484af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
285584af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
285684af7a61SUlf Magnusson					}
2857a1385803SAndy Whitcroft					$length = -1;
2858a1385803SAndy Whitcroft				}
2859a1385803SAndy Whitcroft
28609fe287d7SAndy Whitcroft				$f =~ s/^.//;
28613354957aSAndi Kleen				$f =~ s/#.*//;
28623354957aSAndi Kleen				$f =~ s/^\s+//;
28633354957aSAndi Kleen				next if ($f =~ /^$/);
2864678ae162SUlf Magnusson
2865678ae162SUlf Magnusson				# This only checks context lines in the patch
2866678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
2867678ae162SUlf Magnusson				# positives, even though some of these are
2868678ae162SUlf Magnusson				# common words in help texts
2869678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2870678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
28719fe287d7SAndy Whitcroft					$is_end = 1;
28729fe287d7SAndy Whitcroft					last;
28739fe287d7SAndy Whitcroft				}
28743354957aSAndi Kleen				$length++;
28753354957aSAndi Kleen			}
287656193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2877000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
287856193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
287956193274SVadim Bendebury			}
2880a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
28813354957aSAndi Kleen		}
28823354957aSAndi Kleen
2883628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
2884628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
2885628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
2886628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
2887628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
2888628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2889628f91a2SJoe Perches			    $fix) {
2890628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2891628f91a2SJoe Perches			}
2892628f91a2SJoe Perches		}
2893628f91a2SJoe Perches
2894327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2895327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2896327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2897327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2898327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2899327953e9SChristoph Jaeger		}
2900327953e9SChristoph Jaeger
2901c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2902c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2903c68e5878SArnaud Lacombe			my $flag = $1;
2904c68e5878SArnaud Lacombe			my $replacement = {
2905c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2906c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2907c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2908c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2909c68e5878SArnaud Lacombe			};
2910c68e5878SArnaud Lacombe
2911c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2912c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2913c68e5878SArnaud Lacombe		}
2914c68e5878SArnaud Lacombe
2915bff5da43SRob Herring# check for DT compatible documentation
29167dd05b38SFlorian Vaussard		if (defined $root &&
29177dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
29187dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
29197dd05b38SFlorian Vaussard
2920bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2921bff5da43SRob Herring
2922cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2923cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2924cc93319bSFlorian Vaussard
2925bff5da43SRob Herring			foreach my $compat (@compats) {
2926bff5da43SRob Herring				my $compat2 = $compat;
2927185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2928185d566bSRob Herring				my $compat3 = $compat;
2929185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2930185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2931bff5da43SRob Herring				if ( $? >> 8 ) {
2932bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2933bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2934bff5da43SRob Herring				}
2935bff5da43SRob Herring
29364fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
29374fbf32a6SFlorian Vaussard				my $vendor = $1;
2938cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2939bff5da43SRob Herring				if ( $? >> 8 ) {
2940bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2941cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2942bff5da43SRob Herring				}
2943bff5da43SRob Herring			}
2944bff5da43SRob Herring		}
2945bff5da43SRob Herring
29469f3a8992SRob Herring# check for using SPDX license tag at beginning of files
29479f3a8992SRob Herring		if ($realline == $checklicenseline) {
29489f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
29499f3a8992SRob Herring				$checklicenseline = 2;
29509f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
29519f3a8992SRob Herring				my $comment = "";
29529f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
29539f3a8992SRob Herring					$comment = '/*';
29549f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
29559f3a8992SRob Herring					$comment = '//';
29569f3a8992SRob Herring				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
29579f3a8992SRob Herring					$comment = '#';
29589f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
29599f3a8992SRob Herring					$comment = '..';
29609f3a8992SRob Herring				}
29619f3a8992SRob Herring
29629f3a8992SRob Herring				if ($comment !~ /^$/ &&
29639f3a8992SRob Herring				    $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
29649f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
29659f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
29669f3a8992SRob Herring				}
29679f3a8992SRob Herring			}
29689f3a8992SRob Herring		}
29699f3a8992SRob Herring
29705368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2971d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
29725368df20SAndy Whitcroft
297347e0c88bSJoe Perches# line length limit (with some exclusions)
297447e0c88bSJoe Perches#
297547e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
297647e0c88bSJoe Perches#	logging functions like pr_info that end in a string
297747e0c88bSJoe Perches#	lines with a single string
297847e0c88bSJoe Perches#	#defines that are a single string
29792e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
298047e0c88bSJoe Perches#
298147e0c88bSJoe Perches# There are 3 different line length message types:
2982ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
298347e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
298447e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
298547e0c88bSJoe Perches#
298647e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
298747e0c88bSJoe Perches#
298847e0c88bSJoe Perches
2989b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
299047e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
299147e0c88bSJoe Perches
299247e0c88bSJoe Perches			# Check the allowed long line types first
299347e0c88bSJoe Perches
299447e0c88bSJoe Perches			# logging functions that end in a string that starts
299547e0c88bSJoe Perches			# before $max_line_length
299647e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
299747e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
299847e0c88bSJoe Perches				$msg_type = "";
299947e0c88bSJoe Perches
300047e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
300147e0c88bSJoe Perches			# #defines with only strings
300247e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
300347e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
300447e0c88bSJoe Perches				$msg_type = "";
300547e0c88bSJoe Perches
3006cc147506SJoe Perches			# More special cases
3007cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3008cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3009d560a5f8SJoe Perches				$msg_type = "";
3010d560a5f8SJoe Perches
30112e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
30122e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
30132e4bbbc5SAndreas Brauchli				$msg_type = "";
30142e4bbbc5SAndreas Brauchli
301547e0c88bSJoe Perches			# Otherwise set the alternate message types
301647e0c88bSJoe Perches
301747e0c88bSJoe Perches			# a comment starts before $max_line_length
301847e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
301947e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
302047e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
302147e0c88bSJoe Perches
302247e0c88bSJoe Perches			# a quoted string starts before $max_line_length
302347e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
302447e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
302547e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
302647e0c88bSJoe Perches			}
302747e0c88bSJoe Perches
302847e0c88bSJoe Perches			if ($msg_type ne "" &&
302947e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
303047e0c88bSJoe Perches				WARN($msg_type,
30316cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
30320a920b5bSAndy Whitcroft			}
303347e0c88bSJoe Perches		}
30340a920b5bSAndy Whitcroft
30358905a67cSAndy Whitcroft# check for adding lines without a newline.
30368905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3037000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3038000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
30398905a67cSAndy Whitcroft		}
30408905a67cSAndy Whitcroft
3041b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3042de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
30430a920b5bSAndy Whitcroft
30440a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
30450a920b5bSAndy Whitcroft# more than 8 must use tabs.
3046c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3047c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3048c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3049d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
30503705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
30513705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
30523705ce5bSJoe Perches			    $fix) {
3053194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
30543705ce5bSJoe Perches			}
30550a920b5bSAndy Whitcroft		}
30560a920b5bSAndy Whitcroft
305708e44365SAlberto Panizzo# check for space before tabs.
305808e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
305908e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30603705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
30613705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
30623705ce5bSJoe Perches			    $fix) {
3063194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3064d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
3065194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3066c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
30673705ce5bSJoe Perches			}
306808e44365SAlberto Panizzo		}
306908e44365SAlberto Panizzo
30706a487211SJoe Perches# check for assignments on the start of a line
30716a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
30726a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
30736a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
30746a487211SJoe Perches		}
30756a487211SJoe Perches
3076d1fe9c09SJoe Perches# check for && or || at the start of a line
3077d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3078d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3079d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3080d1fe9c09SJoe Perches		}
3081d1fe9c09SJoe Perches
3082a91e8994SJoe Perches# check indentation starts on a tab stop
30835b57980dSJoe Perches		if ($perl_version_ok &&
3084bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3085a91e8994SJoe Perches			my $indent = length($1);
3086a91e8994SJoe Perches			if ($indent % 8) {
3087a91e8994SJoe Perches				if (WARN("TABSTOP",
3088a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3089a91e8994SJoe Perches				    $fix) {
3090a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3091a91e8994SJoe Perches				}
3092a91e8994SJoe Perches			}
3093a91e8994SJoe Perches		}
3094a91e8994SJoe Perches
3095d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
30965b57980dSJoe Perches		if ($perl_version_ok &&
3097fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3098d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3099d1fe9c09SJoe Perches			my $oldindent = $1;
3100d1fe9c09SJoe Perches			my $rest = $2;
3101d1fe9c09SJoe Perches
3102d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3103d1fe9c09SJoe Perches			if ($pos >= 0) {
3104b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3105b34a26f3SJoe Perches				my $newindent = $2;
3106d1fe9c09SJoe Perches
3107d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3108d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3109d1fe9c09SJoe Perches					" "  x ($pos % 8);
3110d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3111d1fe9c09SJoe Perches
3112d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3113d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
31143705ce5bSJoe Perches
31153705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
31163705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
31173705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3118194f66fcSJoe Perches						$fixed[$fixlinenr] =~
31193705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
31203705ce5bSJoe Perches					}
3121d1fe9c09SJoe Perches				}
3122d1fe9c09SJoe Perches			}
3123d1fe9c09SJoe Perches		}
3124d1fe9c09SJoe Perches
31256ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
31266ab3a970SJoe Perches# avoid checking a few false positives:
31276ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
31286ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
31296ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
31306ab3a970SJoe Perches#   multiline macros that define functions
31316ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
31326ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
31336ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
31343705ce5bSJoe Perches			if (CHK("SPACING",
3135f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
31363705ce5bSJoe Perches			    $fix) {
3137194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3138f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
31393705ce5bSJoe Perches			}
3140aad4f614SJoe Perches		}
3141aad4f614SJoe Perches
314286406b1cSJoe Perches# Block comment styles
314386406b1cSJoe Perches# Networking with an initial /*
314405880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3145fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
314685ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
314785ad978cSJoe Perches		    $realline > 2) {
314805880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
314905880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
315005880600SJoe Perches		}
315105880600SJoe Perches
315286406b1cSJoe Perches# Block comments use * on subsequent lines
315386406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
315486406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3155a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
315661135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3157a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
315886406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
315986406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3160a605e32eSJoe Perches		}
3161a605e32eSJoe Perches
316286406b1cSJoe Perches# Block comments use */ on trailing lines
316386406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3164c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3165c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3166c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
316786406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
316886406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
316905880600SJoe Perches		}
317005880600SJoe Perches
317108eb9b80SJoe Perches# Block comment * alignment
317208eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3173af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3174af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3175af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
317608eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3177af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3178af207524SJoe Perches			my $oldindent;
317908eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3180af207524SJoe Perches			if (defined($1)) {
3181af207524SJoe Perches				$oldindent = expand_tabs($1);
3182af207524SJoe Perches			} else {
3183af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3184af207524SJoe Perches				$oldindent = expand_tabs($1);
3185af207524SJoe Perches			}
318608eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
318708eb9b80SJoe Perches			my $newindent = $1;
318808eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3189af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
319008eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
319108eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
319208eb9b80SJoe Perches			}
319308eb9b80SJoe Perches		}
319408eb9b80SJoe Perches
31957f619191SJoe Perches# check for missing blank lines after struct/union declarations
31967f619191SJoe Perches# with exceptions for various attributes and macros
31977f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
31987f619191SJoe Perches		    $line =~ /^\+/ &&
31997f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
32007f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
32017f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
32027f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
32037f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
32047f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
32057f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
32060bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
32077f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3208d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3209d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3210d752fcc8SJoe Perches			    $fix) {
3211f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3212d752fcc8SJoe Perches			}
32137f619191SJoe Perches		}
32147f619191SJoe Perches
3215365dd4eaSJoe Perches# check for multiple consecutive blank lines
3216365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3217365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3218365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3219d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3220d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3221d752fcc8SJoe Perches			    $fix) {
3222f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3223d752fcc8SJoe Perches			}
3224d752fcc8SJoe Perches
3225365dd4eaSJoe Perches			$last_blank_line = $linenr;
3226365dd4eaSJoe Perches		}
3227365dd4eaSJoe Perches
32283b617e3bSJoe Perches# check for missing blank lines after declarations
32293f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
32303f7bac03SJoe Perches			# actual declarations
32313f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
32325a4e1fd3SJoe Perches			# function pointer declarations
32335a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
32343f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
32353f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
32363f7bac03SJoe Perches			# known declaration macros
32373f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
32383f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
32393f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
32403f7bac03SJoe Perches			# other possible extensions of declaration lines
32413f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
32423f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
32433f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
32443f7bac03SJoe Perches			# looks like a declaration
32453f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
32465a4e1fd3SJoe Perches			# function pointer declarations
32475a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
32483f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
32493f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
32503f7bac03SJoe Perches			# known declaration macros
32513f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
32523f7bac03SJoe Perches			# start of struct or union or enum
32533b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
32543f7bac03SJoe Perches			# start or end of block or continuation of declaration
32553f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
32563f7bac03SJoe Perches			# bitfield continuation
32573f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
32583f7bac03SJoe Perches			# other possible extensions of declaration lines
32593f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
32603f7bac03SJoe Perches			# indentation of previous and current line are the same
32613f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3262d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3263d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3264d752fcc8SJoe Perches			    $fix) {
3265f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3266d752fcc8SJoe Perches			}
32673b617e3bSJoe Perches		}
32683b617e3bSJoe Perches
32695f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
32706b4c5bebSAndy Whitcroft# Exceptions:
32716b4c5bebSAndy Whitcroft#  1) within comments
32726b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
32736b4c5bebSAndy Whitcroft#  3) hanging labels
32743705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
32755f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32763705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
32773705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
32783705ce5bSJoe Perches			    $fix) {
3279194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
32803705ce5bSJoe Perches			}
32815f7ddae6SRaffaele Recalcati		}
32825f7ddae6SRaffaele Recalcati
3283b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3284b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3285b9ea10d6SAndy Whitcroft
32865751a24eSJoe Perches# check for unusual line ending [ or (
32875751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
32885751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
32895751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
32905751a24eSJoe Perches		}
32915751a24eSJoe Perches
32924dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
32934dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
32944dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
32954dbed76fSJoe Perches			$context_function = $1;
32964dbed76fSJoe Perches		}
32974dbed76fSJoe Perches
32984dbed76fSJoe Perches# check if this appears to be the end of function declaration
32994dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
33004dbed76fSJoe Perches			undef $context_function;
33014dbed76fSJoe Perches		}
33024dbed76fSJoe Perches
3303032a4c0fSJoe Perches# check indentation of any line with a bare else
3304840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3305032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3306032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3307032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3308840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3309840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3310840080a0SJoe Perches			     defined $lines[$linenr] &&
3311840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3312032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3313032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3314032a4c0fSJoe Perches			}
3315032a4c0fSJoe Perches		}
3316032a4c0fSJoe Perches
3317c00df19aSJoe Perches# check indentation of a line with a break;
3318c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3319c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3320c00df19aSJoe Perches			my $tabs = $1;
3321c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3322c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3323c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3324c00df19aSJoe Perches			}
3325c00df19aSJoe Perches		}
3326c00df19aSJoe Perches
3327c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3328cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3329000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3330000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3331c2fdda0dSAndy Whitcroft		}
333222f2a2efSAndy Whitcroft
333356e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
333456e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
333556e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
333656e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
333756e77d70SJoe Perches		}
333856e77d70SJoe Perches
33399c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
33402b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
33412b474a1aSAndy Whitcroft		    $realline_next);
33423e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3343ca819864SJoe Perches		if ($linenr > $suppress_statement &&
33441b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3345170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3346f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3347171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3348171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3349171ae1a4SAndy Whitcroft
33503e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
33513e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
33523e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
33533e469cdcSAndy Whitcroft			# until we hit end of it.
33543e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
33553e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
33563e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
33573e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
33583e469cdcSAndy Whitcroft			}
3359f74bd194SAndy Whitcroft
33602b474a1aSAndy Whitcroft			# Find the real next line.
33612b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
33622b474a1aSAndy Whitcroft			if (defined $realline_next &&
33632b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
33642b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
33652b474a1aSAndy Whitcroft				$realline_next++;
33662b474a1aSAndy Whitcroft			}
33672b474a1aSAndy Whitcroft
3368171ae1a4SAndy Whitcroft			my $s = $stat;
3369171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3370cf655043SAndy Whitcroft
3371c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3372171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3373c2fdda0dSAndy Whitcroft
3374c2fdda0dSAndy Whitcroft			# Ignore functions being called
3375171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3376c2fdda0dSAndy Whitcroft
3377463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3378463f2864SAndy Whitcroft
3379c45dcabdSAndy Whitcroft			# declarations always start with types
3380d2506586SAndy 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) {
3381c45dcabdSAndy Whitcroft				my $type = $1;
3382c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3383c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3384c45dcabdSAndy Whitcroft
33856c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3386a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3387c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3388c2fdda0dSAndy Whitcroft			}
33898905a67cSAndy Whitcroft
33906c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
339165863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3392c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
33939c0ca6f9SAndy Whitcroft			}
33948905a67cSAndy Whitcroft
33958905a67cSAndy Whitcroft			# Check for any sort of function declaration.
33968905a67cSAndy Whitcroft			# int foo(something bar, other baz);
33978905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3398171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
33998905a67cSAndy Whitcroft				my ($name_len) = length($1);
34008905a67cSAndy Whitcroft
3401cf655043SAndy Whitcroft				my $ctx = $s;
3402773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
34038905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3404cf655043SAndy Whitcroft
34058905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3406c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
34078905a67cSAndy Whitcroft
3408c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
34098905a67cSAndy Whitcroft					}
34108905a67cSAndy Whitcroft				}
34118905a67cSAndy Whitcroft			}
34128905a67cSAndy Whitcroft
34139c0ca6f9SAndy Whitcroft		}
34149c0ca6f9SAndy Whitcroft
341500df344fSAndy Whitcroft#
341600df344fSAndy Whitcroft# Checks which may be anchored in the context.
341700df344fSAndy Whitcroft#
341800df344fSAndy Whitcroft
341900df344fSAndy Whitcroft# Check for switch () and associated case and default
342000df344fSAndy Whitcroft# statements should be at the same indent.
342100df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
342200df344fSAndy Whitcroft			my $err = '';
342300df344fSAndy Whitcroft			my $sep = '';
342400df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
342500df344fSAndy Whitcroft			shift(@ctx);
342600df344fSAndy Whitcroft			for my $ctx (@ctx) {
342700df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
342800df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
342900df344fSAndy Whitcroft							$indent != $cindent) {
343000df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
343100df344fSAndy Whitcroft					$sep = '';
343200df344fSAndy Whitcroft				} else {
343300df344fSAndy Whitcroft					$sep = "[...]\n";
343400df344fSAndy Whitcroft				}
343500df344fSAndy Whitcroft			}
343600df344fSAndy Whitcroft			if ($err ne '') {
3437000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3438000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3439de7d4f0eSAndy Whitcroft			}
3440de7d4f0eSAndy Whitcroft		}
3441de7d4f0eSAndy Whitcroft
3442de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3443de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
34440fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3445773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3446773647a0SAndy Whitcroft
34479c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
34488eef05ddSJoe Perches
34498eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
34508eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
34518eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
34528eef05ddSJoe Perches			}
34538eef05ddSJoe Perches
3454de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3455de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3456de7d4f0eSAndy Whitcroft
3457548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3458548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3459de7d4f0eSAndy Whitcroft
3460548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3461548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3462548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3463548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3464548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3465773647a0SAndy Whitcroft				$ctx_ln++;
3466773647a0SAndy Whitcroft			}
3467548596d5SAndy Whitcroft
346853210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
346953210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3470773647a0SAndy Whitcroft
3471773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3472000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3473000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
347401464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
347500df344fSAndy Whitcroft			}
3476773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3477773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3478773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3479773647a0SAndy Whitcroft			{
34809c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
34819c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3482000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3483000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
348401464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
34859c0ca6f9SAndy Whitcroft				}
34869c0ca6f9SAndy Whitcroft			}
348700df344fSAndy Whitcroft		}
348800df344fSAndy Whitcroft
34894d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3490f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
34913e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
34923e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
34933e469cdcSAndy Whitcroft					if (!defined $stat);
34944d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
34954d001e4dSAndy Whitcroft
34964d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
34974d001e4dSAndy Whitcroft
34989f5af480SJoe Perches			# remove inline comments
34999f5af480SJoe Perches			$s =~ s/$;/ /g;
35009f5af480SJoe Perches			$c =~ s/$;/ /g;
35014d001e4dSAndy Whitcroft
35024d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
35036f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
35046f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
35054d001e4dSAndy Whitcroft
35069f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
35079f5af480SJoe Perches			# none on the first line, and are going to readd them
35089f5af480SJoe Perches			# where necessary.
35099f5af480SJoe Perches			$s =~ s/\n./\n/gs;
35109f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
35119f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
35129f5af480SJoe Perches			}
35139f5af480SJoe Perches
35144d001e4dSAndy Whitcroft			# We want to check the first line inside the block
35154d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
35164d001e4dSAndy Whitcroft			#  1) any blank line termination
35174d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
35184d001e4dSAndy Whitcroft			#  3) any do (...) {
35194d001e4dSAndy Whitcroft			my $continuation = 0;
35204d001e4dSAndy Whitcroft			my $check = 0;
35214d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
35224d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
35234d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
35244d001e4dSAndy Whitcroft				$continuation = 1;
35254d001e4dSAndy Whitcroft			}
35269bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
35274d001e4dSAndy Whitcroft				$check = 1;
35284d001e4dSAndy Whitcroft				$cond_lines++;
35294d001e4dSAndy Whitcroft			}
35304d001e4dSAndy Whitcroft
35314d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
35324d001e4dSAndy Whitcroft			# preprocessor statement.
35334d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
35344d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
35354d001e4dSAndy Whitcroft				$check = 0;
35364d001e4dSAndy Whitcroft			}
35374d001e4dSAndy Whitcroft
35389bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3539740504c6SAndy Whitcroft			$continuation = 0;
35409bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
35419bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
35424d001e4dSAndy Whitcroft
3543f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3544f16fa28fSAndy Whitcroft				# is not linear.
3545f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3546f16fa28fSAndy Whitcroft					$check = 0;
3547f16fa28fSAndy Whitcroft				}
3548f16fa28fSAndy Whitcroft
35499bd49efeSAndy Whitcroft				# Ignore:
35509bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
35519bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
35529bd49efeSAndy Whitcroft				#  3) labels.
3553740504c6SAndy Whitcroft				if ($continuation ||
3554740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
35559bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
35569bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3557740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
355830dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
35599bd49efeSAndy Whitcroft						$cond_lines++;
35609bd49efeSAndy Whitcroft					}
35614d001e4dSAndy Whitcroft				}
356230dad6ebSAndy Whitcroft			}
35634d001e4dSAndy Whitcroft
35644d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
35654d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
35664d001e4dSAndy Whitcroft
35674d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
35684d001e4dSAndy Whitcroft			# this is not this patch's fault.
35694d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
35704d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
35714d001e4dSAndy Whitcroft				$check = 0;
35724d001e4dSAndy Whitcroft			}
35734d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
35744d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
35754d001e4dSAndy Whitcroft			}
35764d001e4dSAndy Whitcroft
35779bd49efeSAndy 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";
35784d001e4dSAndy Whitcroft
35799f5af480SJoe Perches			if ($check && $s ne '' &&
35809f5af480SJoe Perches			    (($sindent % 8) != 0 ||
35819f5af480SJoe Perches			     ($sindent < $indent) ||
3582f6950a73SJoe Perches			     ($sindent == $indent &&
3583f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
35849f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3585000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3586000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
35874d001e4dSAndy Whitcroft			}
35884d001e4dSAndy Whitcroft		}
35894d001e4dSAndy Whitcroft
35906c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
35916c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
35921f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
35931f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
35946c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3595c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3596c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3597cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3598cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
35991f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3600c2fdda0dSAndy Whitcroft		}
36016c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
36026c72ffaaSAndy Whitcroft
360300df344fSAndy Whitcroft#ignore lines not being added
36043705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
360500df344fSAndy Whitcroft
360611ca40a0SJoe Perches# check for dereferences that span multiple lines
360711ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
360811ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
360911ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
361011ca40a0SJoe Perches			my $ref = $1;
361111ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
361211ca40a0SJoe Perches			$ref .= $1;
361311ca40a0SJoe Perches			$ref =~ s/\s//g;
361411ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
361511ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
361611ca40a0SJoe Perches		}
361711ca40a0SJoe Perches
3618a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3619c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3620a1ce18e4SJoe Perches			my $type = $1;
3621a1ce18e4SJoe Perches			my $var = $2;
3622207a8e84SJoe Perches			$var = "" if (!defined $var);
3623207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3624a1ce18e4SJoe Perches				my $sign = $1;
3625a1ce18e4SJoe Perches				my $pointer = $2;
3626a1ce18e4SJoe Perches
3627a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3628a1ce18e4SJoe Perches
3629a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3630a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3631a1ce18e4SJoe Perches				    $fix) {
3632a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3633207a8e84SJoe Perches					my $comp_pointer = $pointer;
3634207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3635207a8e84SJoe Perches					$decl .= $comp_pointer;
3636207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3637207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3638a1ce18e4SJoe Perches				}
3639a1ce18e4SJoe Perches			}
3640a1ce18e4SJoe Perches		}
3641a1ce18e4SJoe Perches
3642653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
36437429c690SAndy Whitcroft		if ($dbg_type) {
36447429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3645000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3646000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
36477429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3648000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3649000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
36507429c690SAndy Whitcroft			}
3651653d4876SAndy Whitcroft			next;
3652653d4876SAndy Whitcroft		}
3653a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3654a1ef277eSAndy Whitcroft		if ($dbg_attr) {
36559360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3656000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3657000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
36589360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3659000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3660000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3661a1ef277eSAndy Whitcroft			}
3662a1ef277eSAndy Whitcroft			next;
3663a1ef277eSAndy Whitcroft		}
3664653d4876SAndy Whitcroft
3665f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
366699423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
366799423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3668d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3669d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3670f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3671f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3672f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3673d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3674d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3675f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3676d752fcc8SJoe Perches				$fixedline = $line;
36778d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3678f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3679d752fcc8SJoe Perches			}
3680f0a594c1SAndy Whitcroft		}
3681f0a594c1SAndy Whitcroft
368200df344fSAndy Whitcroft#
368300df344fSAndy Whitcroft# Checks which are anchored on the added line.
368400df344fSAndy Whitcroft#
368500df344fSAndy Whitcroft
3686653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3687c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3688653d4876SAndy Whitcroft			my $path = $1;
3689653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3690000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3691495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3692495e9d84SJoe Perches			}
3693495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3694495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3695495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3696653d4876SAndy Whitcroft			}
3697653d4876SAndy Whitcroft		}
3698653d4876SAndy Whitcroft
369900df344fSAndy Whitcroft# no C99 // comments
370000df344fSAndy Whitcroft		if ($line =~ m{//}) {
37013705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
37023705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
37033705ce5bSJoe Perches			    $fix) {
3704194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
37053705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
37063705ce5bSJoe Perches					my $comment = trim($1);
3707194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
37083705ce5bSJoe Perches				}
37093705ce5bSJoe Perches			}
371000df344fSAndy Whitcroft		}
371100df344fSAndy Whitcroft		# Remove C99 comments.
37120a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
37136c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
37140a920b5bSAndy Whitcroft
37152b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
37162b474a1aSAndy Whitcroft# the whole statement.
37172b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
37182b474a1aSAndy Whitcroft		if (defined $realline_next &&
37192b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
37202b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
37212b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
37222b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
37233cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
37243cbf62dfSAndy Whitcroft			# a prefix:
37253cbf62dfSAndy Whitcroft			#   XXX(foo);
37263cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3727653d4876SAndy Whitcroft			my $name = $1;
372887a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
37293cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
37303cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
37313cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
37323cbf62dfSAndy Whitcroft
37333cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
37342b474a1aSAndy Whitcroft				\n.}\s*$|
373548012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
373648012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
373748012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
37382b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
37392b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
374048012058SAndy Whitcroft			    )/x) {
37412b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
37422b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
37432b474a1aSAndy Whitcroft			} else {
37442b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
37450a920b5bSAndy Whitcroft			}
37460a920b5bSAndy Whitcroft		}
37472b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
37482b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
37492b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
37502b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
37512b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
37522b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
37532b474a1aSAndy Whitcroft		}
37542b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
37552b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3756000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3757000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
37582b474a1aSAndy Whitcroft		}
37590a920b5bSAndy Whitcroft
37605150bda4SJoe Eloff# check for global initialisers.
37616d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3762d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
37636d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3764d5e616fcSJoe Perches			    $fix) {
37656d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3766d5e616fcSJoe Perches			}
3767f0a594c1SAndy Whitcroft		}
37680a920b5bSAndy Whitcroft# check for static initialisers.
37696d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3770d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
37716d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3772d5e616fcSJoe Perches				      $herecurr) &&
3773d5e616fcSJoe Perches			    $fix) {
37746d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3775d5e616fcSJoe Perches			}
37760a920b5bSAndy Whitcroft		}
37770a920b5bSAndy Whitcroft
37781813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
37791813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
37801813087dSJoe Perches			my $tmp = trim($1);
37811813087dSJoe Perches			WARN("MISORDERED_TYPE",
37821813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
37831813087dSJoe Perches		}
37841813087dSJoe Perches
3785cb710ecaSJoe Perches# check for static const char * arrays.
3786cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3787000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3788000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3789cb710ecaSJoe Perches				$herecurr);
3790cb710ecaSJoe Perches               }
3791cb710ecaSJoe Perches
3792cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3793cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3794000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3795000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3796cb710ecaSJoe Perches				$herecurr);
3797cb710ecaSJoe Perches               }
3798cb710ecaSJoe Perches
3799ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3800ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3801ab7e23f3SJoe Perches			my $found = $1;
3802ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3803ab7e23f3SJoe Perches				WARN("CONST_CONST",
3804ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3805ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3806ab7e23f3SJoe Perches				WARN("CONST_CONST",
3807ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3808ab7e23f3SJoe Perches			}
3809ab7e23f3SJoe Perches		}
3810ab7e23f3SJoe Perches
38119b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
38129b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
38139b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
38149b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
38159b0fa60dSJoe Perches				$herecurr);
38169b0fa60dSJoe Perches               }
38179b0fa60dSJoe Perches
3818b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3819b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3820b598b670SJoe Perches			my $array = $1;
3821b598b670SJoe 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*\))@) {
3822b598b670SJoe Perches				my $array_div = $1;
3823b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3824b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3825b598b670SJoe Perches				    $fix) {
3826b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3827b598b670SJoe Perches				}
3828b598b670SJoe Perches			}
3829b598b670SJoe Perches		}
3830b598b670SJoe Perches
3831b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3832b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3833b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3834b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3835b36190c5SJoe Perches			    $fix) {
3836194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3837b36190c5SJoe Perches			}
3838b36190c5SJoe Perches		}
3839b36190c5SJoe Perches
3840653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3841653d4876SAndy Whitcroft# make sense.
3842653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
38438054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3844c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
38458ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
384646d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
3847000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3848000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
38490a920b5bSAndy Whitcroft		}
38500a920b5bSAndy Whitcroft
38510a920b5bSAndy Whitcroft# * goes on variable not on type
385265863862SAndy Whitcroft		# (char*[ const])
3853bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3854bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
38553705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3856d8aaf121SAndy Whitcroft
385765863862SAndy Whitcroft			# Should start with a space.
385865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
385965863862SAndy Whitcroft			# Should not end with a space.
386065863862SAndy Whitcroft			$to =~ s/\s+$//;
386165863862SAndy Whitcroft			# '*'s should not have spaces between.
3862f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
386365863862SAndy Whitcroft			}
3864d8aaf121SAndy Whitcroft
38653705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
386665863862SAndy Whitcroft			if ($from ne $to) {
38673705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
38683705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
38693705ce5bSJoe Perches				    $fix) {
38703705ce5bSJoe Perches					my $sub_from = $ident;
38713705ce5bSJoe Perches					my $sub_to = $ident;
38723705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3873194f66fcSJoe Perches					$fixed[$fixlinenr] =~
38743705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
38753705ce5bSJoe Perches				}
387665863862SAndy Whitcroft			}
3877bfcb2cc7SAndy Whitcroft		}
3878bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3879bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
38803705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3881d8aaf121SAndy Whitcroft
388265863862SAndy Whitcroft			# Should start with a space.
388365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
388465863862SAndy Whitcroft			# Should not end with a space.
388565863862SAndy Whitcroft			$to =~ s/\s+$//;
388665863862SAndy Whitcroft			# '*'s should not have spaces between.
3887f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
388865863862SAndy Whitcroft			}
388965863862SAndy Whitcroft			# Modifiers should have spaces.
389065863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
389165863862SAndy Whitcroft
38923705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3893667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
38943705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
38953705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
38963705ce5bSJoe Perches				    $fix) {
38973705ce5bSJoe Perches
38983705ce5bSJoe Perches					my $sub_from = $match;
38993705ce5bSJoe Perches					my $sub_to = $match;
39003705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3901194f66fcSJoe Perches					$fixed[$fixlinenr] =~
39023705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
39033705ce5bSJoe Perches				}
390465863862SAndy Whitcroft			}
39050a920b5bSAndy Whitcroft		}
39060a920b5bSAndy Whitcroft
39079d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
39089d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
39090675a8fbSJean Delvare			my $msg_level = \&WARN;
39100675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
39110675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
39129d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
39139d3e3c70SJoe Perches		}
39140a920b5bSAndy Whitcroft
39159d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
39168905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3917000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3918000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
39198905a67cSAndy Whitcroft		}
39208905a67cSAndy Whitcroft
392117441227SJoe Perches# check for uses of printk_ratelimit
392217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3923000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3924000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
392517441227SJoe Perches		}
392617441227SJoe Perches
3927eeef5733SJoe Perches# printk should use KERN_* levels
3928eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3929000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
3930eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
393100df344fSAndy Whitcroft		}
39320a920b5bSAndy Whitcroft
3933243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3934243f3803SJoe Perches			my $orig = $1;
3935243f3803SJoe Perches			my $level = lc($orig);
3936243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
39378f26b837SJoe Perches			my $level2 = $level;
39388f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3939243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3940daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3941243f3803SJoe Perches		}
3942243f3803SJoe Perches
3943243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3944d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3945d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3946d5e616fcSJoe Perches			    $fix) {
3947194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3948d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3949d5e616fcSJoe Perches			}
3950243f3803SJoe Perches		}
3951243f3803SJoe Perches
3952dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3953dc139313SJoe Perches			my $orig = $1;
3954dc139313SJoe Perches			my $level = lc($orig);
3955dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3956dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3957dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3958dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3959dc139313SJoe Perches		}
3960dc139313SJoe Perches
396191c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
396291c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
396391c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
396491c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
396591c9afafSAndy Lutomirski			WARN("ENOSYS",
396691c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
396791c9afafSAndy Lutomirski		}
396891c9afafSAndy Lutomirski
3969653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3970653d4876SAndy Whitcroft# or if closed on same line
39715b57980dSJoe Perches		if ($perl_version_ok &&
39722d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
39732d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
39742d453e3bSJoe Perches		    $sline !~ /}/) {
39758d182478SJoe Perches			if (ERROR("OPEN_BRACE",
39762d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
39778d182478SJoe Perches			    $fix) {
39788d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39798d182478SJoe Perches				my $fixed_line = $rawline;
39808d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
39818d182478SJoe Perches				my $line1 = $1;
39828d182478SJoe Perches				my $line2 = $2;
39838d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
39848d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
39858d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
39868d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
39878d182478SJoe Perches				}
39888d182478SJoe Perches			}
39890a920b5bSAndy Whitcroft		}
3990653d4876SAndy Whitcroft
39918905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
39928905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
39938905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
39948d182478SJoe Perches			if (ERROR("OPEN_BRACE",
39958d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
39968d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39978d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39988d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39998d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
40008d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
40018d182478SJoe Perches				$fixedline = $rawline;
40028d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
40038d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
40048d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
40058d182478SJoe Perches				}
40068d182478SJoe Perches			}
40078905a67cSAndy Whitcroft		}
40088905a67cSAndy Whitcroft
40090c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
40103705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
40113705ce5bSJoe Perches			if (WARN("SPACING",
40123705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
40133705ce5bSJoe Perches			    $fix) {
4014194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40153705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
40163705ce5bSJoe Perches			}
40170c73b4ebSAndy Whitcroft		}
40180c73b4ebSAndy Whitcroft
401931070b5dSJoe Perches# Function pointer declarations
402031070b5dSJoe Perches# check spacing between type, funcptr, and args
402131070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
402291f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
402331070b5dSJoe Perches			my $declare = $1;
402431070b5dSJoe Perches			my $pre_pointer_space = $2;
402531070b5dSJoe Perches			my $post_pointer_space = $3;
402631070b5dSJoe Perches			my $funcname = $4;
402731070b5dSJoe Perches			my $post_funcname_space = $5;
402831070b5dSJoe Perches			my $pre_args_space = $6;
402931070b5dSJoe Perches
403091f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
403191f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
403291f72e9cSJoe Perches# don't need a space so don't warn for those.
403391f72e9cSJoe Perches			my $post_declare_space = "";
403491f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
403591f72e9cSJoe Perches				$post_declare_space = $1;
403691f72e9cSJoe Perches				$declare = rtrim($declare);
403791f72e9cSJoe Perches			}
403891f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
403931070b5dSJoe Perches				WARN("SPACING",
404031070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
404191f72e9cSJoe Perches				$post_declare_space = " ";
404231070b5dSJoe Perches			}
404331070b5dSJoe Perches
404431070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
404591f72e9cSJoe Perches# This test is not currently implemented because these declarations are
404691f72e9cSJoe Perches# equivalent to
404791f72e9cSJoe Perches#	int  foo(int bar, ...)
404891f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
404991f72e9cSJoe Perches#
405091f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
405191f72e9cSJoe Perches#				WARN("SPACING",
405291f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
405391f72e9cSJoe Perches#			}
405431070b5dSJoe Perches
405531070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
405631070b5dSJoe Perches			if (defined $pre_pointer_space &&
405731070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
405831070b5dSJoe Perches				WARN("SPACING",
405931070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
406031070b5dSJoe Perches			}
406131070b5dSJoe Perches
406231070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
406331070b5dSJoe Perches			if (defined $post_pointer_space &&
406431070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
406531070b5dSJoe Perches				WARN("SPACING",
406631070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
406731070b5dSJoe Perches			}
406831070b5dSJoe Perches
406931070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
407031070b5dSJoe Perches			if (defined $post_funcname_space &&
407131070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
407231070b5dSJoe Perches				WARN("SPACING",
407331070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
407431070b5dSJoe Perches			}
407531070b5dSJoe Perches
407631070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
407731070b5dSJoe Perches			if (defined $pre_args_space &&
407831070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
407931070b5dSJoe Perches				WARN("SPACING",
408031070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
408131070b5dSJoe Perches			}
408231070b5dSJoe Perches
408331070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4084194f66fcSJoe Perches				$fixed[$fixlinenr] =~
408591f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
408631070b5dSJoe Perches			}
408731070b5dSJoe Perches		}
408831070b5dSJoe Perches
40898d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
40908d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4091fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4092fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
40938d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
40948d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
40958d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4096fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
409738dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
40983705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
40993705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
41003705ce5bSJoe Perches				    $fix) {
4101194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
41023705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
41033705ce5bSJoe Perches				}
41048d31cfceSAndy Whitcroft			}
41058d31cfceSAndy Whitcroft		}
41068d31cfceSAndy Whitcroft
4107f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
41086c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4109c2fdda0dSAndy Whitcroft			my $name = $1;
4110773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4111773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4112c2fdda0dSAndy Whitcroft
4113c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4114773647a0SAndy Whitcroft			if ($name =~ /^(?:
4115773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4116773647a0SAndy Whitcroft				volatile|__volatile__|
4117773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4118773647a0SAndy Whitcroft				asm|__asm__)$/x)
4119773647a0SAndy Whitcroft			{
4120c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4121c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4122c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4123c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4124773647a0SAndy Whitcroft
4125773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4126c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4127c2fdda0dSAndy Whitcroft
4128c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4129c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4130773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4131c2fdda0dSAndy Whitcroft
4132c2fdda0dSAndy Whitcroft			} else {
41333705ce5bSJoe Perches				if (WARN("SPACING",
41343705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
41353705ce5bSJoe Perches					     $fix) {
4136194f66fcSJoe Perches					$fixed[$fixlinenr] =~
41373705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
41383705ce5bSJoe Perches				}
4139f0a594c1SAndy Whitcroft			}
41406c72ffaaSAndy Whitcroft		}
41419a4cad4eSEric Nelson
4142653d4876SAndy Whitcroft# Check operator spacing.
41430a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
41443705ce5bSJoe Perches			my $fixed_line = "";
41453705ce5bSJoe Perches			my $line_fixed = 0;
41463705ce5bSJoe Perches
41479c0ca6f9SAndy Whitcroft			my $ops = qr{
41489c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
41499c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
41509c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
41511f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
415284731623SJoe Perches				\?:|\?|:
41539c0ca6f9SAndy Whitcroft			}x;
4154cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
41553705ce5bSJoe Perches
41563705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
41573705ce5bSJoe Perches##			foreach my $el (@elements) {
41583705ce5bSJoe Perches##				print("el: <$el>\n");
41593705ce5bSJoe Perches##			}
41603705ce5bSJoe Perches
41613705ce5bSJoe Perches			my @fix_elements = ();
416200df344fSAndy Whitcroft			my $off = 0;
41636c72ffaaSAndy Whitcroft
41643705ce5bSJoe Perches			foreach my $el (@elements) {
41653705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
41663705ce5bSJoe Perches				$off += length($el);
41673705ce5bSJoe Perches			}
41683705ce5bSJoe Perches
41693705ce5bSJoe Perches			$off = 0;
41703705ce5bSJoe Perches
41716c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4172b34c648bSJoe Perches			my $last_after = -1;
41736c72ffaaSAndy Whitcroft
41740a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
41753705ce5bSJoe Perches
41763705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
41773705ce5bSJoe Perches
41783705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
41793705ce5bSJoe Perches
41804a0df2efSAndy Whitcroft				$off += length($elements[$n]);
41814a0df2efSAndy Whitcroft
418225985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4183773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4184773647a0SAndy Whitcroft				my $cc = '';
4185773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4186773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4187773647a0SAndy Whitcroft				}
4188773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4189773647a0SAndy Whitcroft
41904a0df2efSAndy Whitcroft				my $a = '';
41914a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
41924a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4193cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
41944a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
41954a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4196773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
41974a0df2efSAndy Whitcroft
41980a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
41994a0df2efSAndy Whitcroft
42004a0df2efSAndy Whitcroft				my $c = '';
42010a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
42024a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
42034a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4204cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
42054a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
42064a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
42078b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
42084a0df2efSAndy Whitcroft				} else {
42094a0df2efSAndy Whitcroft					$c = 'E';
42100a920b5bSAndy Whitcroft				}
42110a920b5bSAndy Whitcroft
42124a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
42134a0df2efSAndy Whitcroft
42144a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
42154a0df2efSAndy Whitcroft
42166c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4217de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
42180a920b5bSAndy Whitcroft
421974048ed8SAndy Whitcroft				# Pull out the value of this operator.
42206c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
42210a920b5bSAndy Whitcroft
42221f65f947SAndy Whitcroft				# Get the full operator variant.
42231f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
42241f65f947SAndy Whitcroft
422513214adfSAndy Whitcroft				# Ignore operators passed as parameters.
422613214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4227d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
422813214adfSAndy Whitcroft
4229cf655043SAndy Whitcroft#				# Ignore comments
4230cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
423113214adfSAndy Whitcroft
4232d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
423313214adfSAndy Whitcroft				} elsif ($op eq ';') {
4234cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4235cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
42363705ce5bSJoe Perches						if (ERROR("SPACING",
42373705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4238b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
42393705ce5bSJoe Perches							$line_fixed = 1;
42403705ce5bSJoe Perches						}
4241d8aaf121SAndy Whitcroft					}
4242d8aaf121SAndy Whitcroft
4243d8aaf121SAndy Whitcroft				# // is a comment
4244d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
42450a920b5bSAndy Whitcroft
4246b00e4814SJoe Perches				#   :   when part of a bitfield
4247b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4248b00e4814SJoe Perches					# skip the bitfield test for now
4249b00e4814SJoe Perches
42501f65f947SAndy Whitcroft				# No spaces for:
42511f65f947SAndy Whitcroft				#   ->
4252b00e4814SJoe Perches				} elsif ($op eq '->') {
42534a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
42543705ce5bSJoe Perches						if (ERROR("SPACING",
42553705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4256b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42573705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42583705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
42593705ce5bSJoe Perches							}
4260b34c648bSJoe Perches							$line_fixed = 1;
42613705ce5bSJoe Perches						}
42620a920b5bSAndy Whitcroft					}
42630a920b5bSAndy Whitcroft
42642381097bSJoe Perches				# , must not have a space before and must have a space on the right.
42650a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
42662381097bSJoe Perches					my $rtrim_before = 0;
42672381097bSJoe Perches					my $space_after = 0;
42682381097bSJoe Perches					if ($ctx =~ /Wx./) {
42692381097bSJoe Perches						if (ERROR("SPACING",
42702381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
42712381097bSJoe Perches							$line_fixed = 1;
42722381097bSJoe Perches							$rtrim_before = 1;
42732381097bSJoe Perches						}
42742381097bSJoe Perches					}
4275cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
42763705ce5bSJoe Perches						if (ERROR("SPACING",
42773705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
42783705ce5bSJoe Perches							$line_fixed = 1;
4279b34c648bSJoe Perches							$last_after = $n;
42802381097bSJoe Perches							$space_after = 1;
42812381097bSJoe Perches						}
42822381097bSJoe Perches					}
42832381097bSJoe Perches					if ($rtrim_before || $space_after) {
42842381097bSJoe Perches						if ($rtrim_before) {
42852381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42862381097bSJoe Perches						} else {
42872381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42882381097bSJoe Perches						}
42892381097bSJoe Perches						if ($space_after) {
42902381097bSJoe Perches							$good .= " ";
42913705ce5bSJoe Perches						}
42920a920b5bSAndy Whitcroft					}
42930a920b5bSAndy Whitcroft
42949c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
429574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
42969c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
42979c0ca6f9SAndy Whitcroft
42989c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
42999c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
43009c0ca6f9SAndy Whitcroft				# unary operator, or a cast
43019c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
430274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
43030d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4304cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
43053705ce5bSJoe Perches						if (ERROR("SPACING",
43063705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4307b34c648bSJoe Perches							if ($n != $last_after + 2) {
4308b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
43093705ce5bSJoe Perches								$line_fixed = 1;
43103705ce5bSJoe Perches							}
43110a920b5bSAndy Whitcroft						}
4312b34c648bSJoe Perches					}
4313a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4314171ae1a4SAndy Whitcroft						# A unary '*' may be const
4315171ae1a4SAndy Whitcroft
4316171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
43173705ce5bSJoe Perches						if (ERROR("SPACING",
43183705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4319b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
43203705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
43213705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
43223705ce5bSJoe Perches							}
4323b34c648bSJoe Perches							$line_fixed = 1;
43243705ce5bSJoe Perches						}
43250a920b5bSAndy Whitcroft					}
43260a920b5bSAndy Whitcroft
43270a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
43280a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4329773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
43303705ce5bSJoe Perches						if (ERROR("SPACING",
43313705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4332b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
43333705ce5bSJoe Perches							$line_fixed = 1;
43343705ce5bSJoe Perches						}
43350a920b5bSAndy Whitcroft					}
4336773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4337773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
43383705ce5bSJoe Perches						if (ERROR("SPACING",
43393705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4340b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43413705ce5bSJoe Perches							$line_fixed = 1;
43423705ce5bSJoe Perches						}
4343653d4876SAndy Whitcroft					}
4344773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
43453705ce5bSJoe Perches						if (ERROR("SPACING",
43463705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4347b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
43483705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
43493705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4350773647a0SAndy Whitcroft							}
4351b34c648bSJoe Perches							$line_fixed = 1;
43523705ce5bSJoe Perches						}
43533705ce5bSJoe Perches					}
43540a920b5bSAndy Whitcroft
43550a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
43569c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
43579c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
43589c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4359c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4360c2fdda0dSAndy Whitcroft					 $op eq '%')
43610a920b5bSAndy Whitcroft				{
4362d2e025f3SJoe Perches					if ($check) {
4363d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4364d2e025f3SJoe Perches							if (CHK("SPACING",
4365d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4366d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4367d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4368d2e025f3SJoe Perches								$line_fixed = 1;
4369d2e025f3SJoe Perches							}
4370d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4371d2e025f3SJoe Perches							if (CHK("SPACING",
4372d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4373d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4374d2e025f3SJoe Perches								$line_fixed = 1;
4375d2e025f3SJoe Perches							}
4376d2e025f3SJoe Perches						}
4377d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
43783705ce5bSJoe Perches						if (ERROR("SPACING",
43793705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4380b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4381b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4382b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4383b34c648bSJoe Perches							}
43843705ce5bSJoe Perches							$line_fixed = 1;
43853705ce5bSJoe Perches						}
43860a920b5bSAndy Whitcroft					}
43870a920b5bSAndy Whitcroft
43881f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
43891f65f947SAndy Whitcroft				# terminating a case value or a label.
43901f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
43911f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
43923705ce5bSJoe Perches						if (ERROR("SPACING",
43933705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4394b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43953705ce5bSJoe Perches							$line_fixed = 1;
43963705ce5bSJoe Perches						}
43971f65f947SAndy Whitcroft					}
43981f65f947SAndy Whitcroft
43990a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4400cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
44011f65f947SAndy Whitcroft					my $ok = 0;
44021f65f947SAndy Whitcroft
440322f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
44041f65f947SAndy Whitcroft					if (($op eq '<' &&
44051f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
44061f65f947SAndy Whitcroft					    ($op eq '>' &&
44071f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
44081f65f947SAndy Whitcroft					{
44091f65f947SAndy Whitcroft					    	$ok = 1;
44101f65f947SAndy Whitcroft					}
44111f65f947SAndy Whitcroft
4412e0df7e1fSJoe Perches					# for asm volatile statements
4413e0df7e1fSJoe Perches					# ignore a colon with another
4414e0df7e1fSJoe Perches					# colon immediately before or after
4415e0df7e1fSJoe Perches					if (($op eq ':') &&
4416e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4417e0df7e1fSJoe Perches						$ok = 1;
4418e0df7e1fSJoe Perches					}
4419e0df7e1fSJoe Perches
442084731623SJoe Perches					# messages are ERROR, but ?: are CHK
44211f65f947SAndy Whitcroft					if ($ok == 0) {
44220675a8fbSJean Delvare						my $msg_level = \&ERROR;
44230675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
442484731623SJoe Perches
44250675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
44263705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4427b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4428b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4429b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4430b34c648bSJoe Perches							}
44313705ce5bSJoe Perches							$line_fixed = 1;
44323705ce5bSJoe Perches						}
44330a920b5bSAndy Whitcroft					}
443422f2a2efSAndy Whitcroft				}
44354a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
44363705ce5bSJoe Perches
44373705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
44383705ce5bSJoe Perches
44393705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
44400a920b5bSAndy Whitcroft			}
44413705ce5bSJoe Perches
44423705ce5bSJoe Perches			if (($#elements % 2) == 0) {
44433705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
44443705ce5bSJoe Perches			}
44453705ce5bSJoe Perches
4446194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4447194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
44483705ce5bSJoe Perches			}
44493705ce5bSJoe Perches
44503705ce5bSJoe Perches
44510a920b5bSAndy Whitcroft		}
44520a920b5bSAndy Whitcroft
4453786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4454d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4455786b6326SJoe Perches			if (WARN("SPACING",
4456786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4457786b6326SJoe Perches			    $fix) {
4458194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4459786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4460786b6326SJoe Perches			}
4461786b6326SJoe Perches		}
4462786b6326SJoe Perches
4463f0a594c1SAndy Whitcroft# check for multiple assignments
4464f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4465000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4466000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4467f0a594c1SAndy Whitcroft		}
4468f0a594c1SAndy Whitcroft
446922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
447022f2a2efSAndy Whitcroft## # continuation.
447122f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
447222f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
447322f2a2efSAndy Whitcroft##
447422f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
447522f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
447622f2a2efSAndy Whitcroft## 			my $ln = $line;
447722f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
447822f2a2efSAndy Whitcroft## 			}
447922f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4480000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4481000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
448222f2a2efSAndy Whitcroft## 			}
448322f2a2efSAndy Whitcroft## 		}
4484f0a594c1SAndy Whitcroft
44850a920b5bSAndy Whitcroft#need space before brace following if, while, etc
44866b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
44874e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
44883705ce5bSJoe Perches			if (ERROR("SPACING",
44893705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
44903705ce5bSJoe Perches			    $fix) {
44918d81ae05SCyril Bur				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
44923705ce5bSJoe Perches			}
4493de7d4f0eSAndy Whitcroft		}
4494de7d4f0eSAndy Whitcroft
4495c4a62ef9SJoe Perches## # check for blank lines before declarations
4496c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4497c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4498c4a62ef9SJoe Perches##			WARN("SPACING",
4499c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4500c4a62ef9SJoe Perches##		}
4501c4a62ef9SJoe Perches##
4502c4a62ef9SJoe Perches
4503de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4504de7d4f0eSAndy Whitcroft# on the line
4505de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4506d5e616fcSJoe Perches			if (ERROR("SPACING",
4507d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4508d5e616fcSJoe Perches			    $fix) {
4509194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4510d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4511d5e616fcSJoe Perches			}
45120a920b5bSAndy Whitcroft		}
45130a920b5bSAndy Whitcroft
451422f2a2efSAndy Whitcroft# check spacing on square brackets
451522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
45163705ce5bSJoe Perches			if (ERROR("SPACING",
45173705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
45183705ce5bSJoe Perches			    $fix) {
4519194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45203705ce5bSJoe Perches				    s/\[\s+/\[/;
45213705ce5bSJoe Perches			}
452222f2a2efSAndy Whitcroft		}
452322f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
45243705ce5bSJoe Perches			if (ERROR("SPACING",
45253705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
45263705ce5bSJoe Perches			    $fix) {
4527194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45283705ce5bSJoe Perches				    s/\s+\]/\]/;
45293705ce5bSJoe Perches			}
453022f2a2efSAndy Whitcroft		}
453122f2a2efSAndy Whitcroft
4532c45dcabdSAndy Whitcroft# check spacing on parentheses
45339c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
45349c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
45353705ce5bSJoe Perches			if (ERROR("SPACING",
45363705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
45373705ce5bSJoe Perches			    $fix) {
4538194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45393705ce5bSJoe Perches				    s/\(\s+/\(/;
45403705ce5bSJoe Perches			}
454122f2a2efSAndy Whitcroft		}
454213214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4543c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4544c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
45453705ce5bSJoe Perches			if (ERROR("SPACING",
45463705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
45473705ce5bSJoe Perches			    $fix) {
4548194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45493705ce5bSJoe Perches				    s/\s+\)/\)/;
45503705ce5bSJoe Perches			}
455122f2a2efSAndy Whitcroft		}
455222f2a2efSAndy Whitcroft
4553e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4554e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4555e2826fd0SJoe Perches
4556e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4557ea4acbb1SJoe Perches			my $var = $1;
4558ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4559ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4560ea4acbb1SJoe Perches			    $fix) {
4561ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4562ea4acbb1SJoe Perches			}
4563ea4acbb1SJoe Perches		}
4564ea4acbb1SJoe Perches
4565ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4566ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4567ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4568ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4569ea4acbb1SJoe Perches			my $var = $2;
4570ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4571ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4572ea4acbb1SJoe Perches			    $fix) {
4573ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4574ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4575ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4576ea4acbb1SJoe Perches			}
4577e2826fd0SJoe Perches		}
4578e2826fd0SJoe Perches
457963b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4580a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4581a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
45825b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
458363b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
458463b7c73eSJoe Perches			my $if_stat = $1;
458563b7c73eSJoe Perches			my $test = substr($2, 1, -1);
458663b7c73eSJoe Perches			my $herectx;
458763b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
458863b7c73eSJoe Perches				my $match = $1;
458963b7c73eSJoe Perches				# avoid parentheses around potential macro args
459063b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
459163b7c73eSJoe Perches				if (!defined($herectx)) {
459263b7c73eSJoe Perches					$herectx = $here . "\n";
459363b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
459463b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
459563b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
459663b7c73eSJoe Perches						$herectx .=  $rl . "\n";
459763b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
459863b7c73eSJoe Perches					}
459963b7c73eSJoe Perches				}
460063b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
460163b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
460263b7c73eSJoe Perches			}
460363b7c73eSJoe Perches		}
460463b7c73eSJoe Perches
46050a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
46064a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
46070a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
46083705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
46093705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
46103705ce5bSJoe Perches			    $fix) {
4611194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46123705ce5bSJoe Perches				    s/^(.)\s+/$1/;
46133705ce5bSJoe Perches			}
46140a920b5bSAndy Whitcroft		}
46150a920b5bSAndy Whitcroft
46165b9553abSJoe Perches# return is not a function
4617507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4618c45dcabdSAndy Whitcroft			my $spacing = $1;
46195b57980dSJoe Perches			if ($perl_version_ok &&
46205b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
46215b9553abSJoe Perches				my $value = $1;
46225b9553abSJoe Perches				$value = deparenthesize($value);
46235b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4624000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4625000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
46265b9553abSJoe Perches				}
4627c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4628000d1cc1SJoe Perches				ERROR("SPACING",
4629000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4630c45dcabdSAndy Whitcroft			}
4631c45dcabdSAndy Whitcroft		}
4632507e5141SJoe Perches
4633b43ae21bSJoe Perches# unnecessary return in a void function
4634b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4635b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4636b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4637b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4638b43ae21bSJoe Perches		    $linenr >= 3 &&
4639b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4640b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
46419819cf25SJoe Perches			WARN("RETURN_VOID",
4642b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
46439819cf25SJoe Perches               }
46449819cf25SJoe Perches
4645189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
46465b57980dSJoe Perches		if ($perl_version_ok &&
4647189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4648189248d8SJoe Perches			my $openparens = $1;
4649189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4650189248d8SJoe Perches			my $msg = "";
4651189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4652189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4653189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4654189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4655189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4656189248d8SJoe Perches			}
4657189248d8SJoe Perches		}
4658189248d8SJoe Perches
4659c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4660c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4661c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4662c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
46635b57980dSJoe Perches		if ($perl_version_ok &&
4664c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4665c5595fa2SJoe Perches			my $lead = $1;
4666c5595fa2SJoe Perches			my $const = $2;
4667c5595fa2SJoe Perches			my $comp = $3;
4668c5595fa2SJoe Perches			my $to = $4;
4669c5595fa2SJoe Perches			my $newcomp = $comp;
4670f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4671c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4672c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4673c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4674c5595fa2SJoe Perches			    $fix) {
4675c5595fa2SJoe Perches				if ($comp eq "<") {
4676c5595fa2SJoe Perches					$newcomp = ">";
4677c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4678c5595fa2SJoe Perches					$newcomp = ">=";
4679c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4680c5595fa2SJoe Perches					$newcomp = "<";
4681c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4682c5595fa2SJoe Perches					$newcomp = "<=";
4683c5595fa2SJoe Perches				}
4684c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4685c5595fa2SJoe Perches			}
4686c5595fa2SJoe Perches		}
4687c5595fa2SJoe Perches
4688f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4689f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
469053a3c448SAndy Whitcroft			my $name = $1;
469153a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4692000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4693f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
469453a3c448SAndy Whitcroft			}
469553a3c448SAndy Whitcroft		}
4696c45dcabdSAndy Whitcroft
46970a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
46984a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
46993705ce5bSJoe Perches			if (ERROR("SPACING",
47003705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
47013705ce5bSJoe Perches			    $fix) {
4702194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47033705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
47043705ce5bSJoe Perches			}
47050a920b5bSAndy Whitcroft		}
47060a920b5bSAndy Whitcroft
4707f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4708f5fe35ddSAndy Whitcroft# statements after the conditional.
4709170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
47103e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
47113e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
47123e469cdcSAndy Whitcroft					if (!defined $stat);
4713170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4714170d3a22SAndy Whitcroft						$remain_next, $off_next);
4715170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4716170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4717170d3a22SAndy Whitcroft
4718170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4719170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4720170d3a22SAndy Whitcroft				# then count those as offsets.
4721170d3a22SAndy Whitcroft				my ($whitespace) =
4722170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4723170d3a22SAndy Whitcroft				my $offset =
4724170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4725170d3a22SAndy Whitcroft
4726170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4727170d3a22SAndy Whitcroft								$offset} = 1;
4728170d3a22SAndy Whitcroft			}
4729170d3a22SAndy Whitcroft		}
4730170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4731c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4732170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4733171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
47348905a67cSAndy Whitcroft
4735b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4736000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4737000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
47388905a67cSAndy Whitcroft			}
47398905a67cSAndy Whitcroft
47408905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
47418905a67cSAndy Whitcroft			# conditional.
4742773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
47438905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
474413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
474553210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
474653210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4747773647a0SAndy Whitcroft			{
4748bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4749bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4750bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
475142bdf74cSHidetoshi Seto				my $stat_real = '';
4752bb44ad39SAndy Whitcroft
475342bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
475442bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4755bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4756bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4757bb44ad39SAndy Whitcroft				}
4758bb44ad39SAndy Whitcroft
4759000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4760000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
47618905a67cSAndy Whitcroft			}
47628905a67cSAndy Whitcroft		}
47638905a67cSAndy Whitcroft
476413214adfSAndy Whitcroft# Check for bitwise tests written as boolean
476513214adfSAndy Whitcroft		if ($line =~ /
476613214adfSAndy Whitcroft			(?:
476713214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
476813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
476913214adfSAndy Whitcroft				(?:\&\&|\|\|)
477013214adfSAndy Whitcroft			|
477113214adfSAndy Whitcroft				(?:\&\&|\|\|)
477213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
477313214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
477413214adfSAndy Whitcroft			)/x)
477513214adfSAndy Whitcroft		{
4776000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4777000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
477813214adfSAndy Whitcroft		}
477913214adfSAndy Whitcroft
47808905a67cSAndy Whitcroft# if and else should not have general statements after it
478113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
478213214adfSAndy Whitcroft			my $s = $1;
478313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
478413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4785000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4786000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
47870a920b5bSAndy Whitcroft			}
478813214adfSAndy Whitcroft		}
478939667782SAndy Whitcroft# if should not continue a brace
479039667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4791000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4792048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
479339667782SAndy Whitcroft				$herecurr);
479439667782SAndy Whitcroft		}
4795a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4796a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4797a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
47983fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4799a1080bf8SAndy Whitcroft			\s*return\s+
4800a1080bf8SAndy Whitcroft		    )/xg)
4801a1080bf8SAndy Whitcroft		{
4802000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4803000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4804a1080bf8SAndy Whitcroft		}
48050a920b5bSAndy Whitcroft
48060a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
48070a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
48088b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
48090a920b5bSAndy Whitcroft		    $previndent == $indent) {
48108b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
48118b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
48128b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
48138b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
48148b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
48158b8856f4SJoe Perches				my $fixedline = $prevrawline;
48168b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
48178b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
48188b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
48198b8856f4SJoe Perches				}
48208b8856f4SJoe Perches				$fixedline = $rawline;
48218b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
48228b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
48238b8856f4SJoe Perches			}
48240a920b5bSAndy Whitcroft		}
48250a920b5bSAndy Whitcroft
48268b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4827c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4828c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4829c2fdda0dSAndy Whitcroft
4830c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4831c2fdda0dSAndy Whitcroft			# conditional.
4832773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4833c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4834c2fdda0dSAndy Whitcroft
4835c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
48368b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
48378b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
48388b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
48398b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
48408b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
48418b8856f4SJoe Perches					my $fixedline = $prevrawline;
48428b8856f4SJoe Perches					my $trailing = $rawline;
48438b8856f4SJoe Perches					$trailing =~ s/^\+//;
48448b8856f4SJoe Perches					$trailing = trim($trailing);
48458b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
48468b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
48478b8856f4SJoe Perches				}
4848c2fdda0dSAndy Whitcroft			}
4849c2fdda0dSAndy Whitcroft		}
4850c2fdda0dSAndy Whitcroft
485195e2c602SJoe Perches#Specific variable tests
4852323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4853323c1260SJoe Perches			my $var = $1;
485495e2c602SJoe Perches
485595e2c602SJoe Perches#gcc binary extension
485695e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4857d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4858d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4859d5e616fcSJoe Perches				    $fix) {
4860d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4861194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4862d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4863d5e616fcSJoe Perches				}
486495e2c602SJoe Perches			}
486595e2c602SJoe Perches
486695e2c602SJoe Perches#CamelCase
4867807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4868be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
486922735ce8SJoe Perches#Ignore Page<foo> variants
4870807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
487122735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4872f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4873f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4874f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
48757e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
48767e781f67SJoe Perches					my $word = $1;
48777e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4878d8b07710SJoe Perches					if ($check) {
4879d8b07710SJoe Perches						seed_camelcase_includes();
4880d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4881d8b07710SJoe Perches							seed_camelcase_file($realfile);
4882d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4883d8b07710SJoe Perches						}
4884d8b07710SJoe Perches					}
48857e781f67SJoe Perches					if (!defined $camelcase{$word}) {
48867e781f67SJoe Perches						$camelcase{$word} = 1;
4887be79794bSJoe Perches						CHK("CAMELCASE",
48887e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
48897e781f67SJoe Perches					}
4890323c1260SJoe Perches				}
4891323c1260SJoe Perches			}
48923445686aSJoe Perches		}
48930a920b5bSAndy Whitcroft
48940a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4895d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4896d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4897d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4898d5e616fcSJoe Perches			    $fix) {
4899194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4900d5e616fcSJoe Perches			}
49010a920b5bSAndy Whitcroft		}
49020a920b5bSAndy Whitcroft
49030e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
49040e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4905c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4906e09dec48SAndy Whitcroft			my $file = "$1.h";
4907e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4908e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4909e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
49107840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4911c45dcabdSAndy Whitcroft			{
49120e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
49130e212e0aSFabian Frederick				if ($asminclude > 0) {
4914e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4915000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4916000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4917e09dec48SAndy Whitcroft					} else {
4918000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4919000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4920e09dec48SAndy Whitcroft					}
49210a920b5bSAndy Whitcroft				}
49220a920b5bSAndy Whitcroft			}
49230e212e0aSFabian Frederick		}
49240a920b5bSAndy Whitcroft
4925653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4926653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4927cf655043SAndy Whitcroft# in a known good container
4928b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4929b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4930d8aaf121SAndy Whitcroft			my $ln = $linenr;
4931d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4932c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4933c45dcabdSAndy Whitcroft			my $ctx = '';
493408a2843eSJoe Perches			my $has_flow_statement = 0;
493508a2843eSJoe Perches			my $has_arg_concat = 0;
4936c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4937f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4938f74bd194SAndy Whitcroft			$ctx = $dstat;
4939c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4940a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4941c45dcabdSAndy Whitcroft
494208a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
494362e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
494408a2843eSJoe Perches
4945f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4946f59b64bfSJoe Perches			my $define_args = $1;
4947f59b64bfSJoe Perches			my $define_stmt = $dstat;
4948f59b64bfSJoe Perches			my @def_args = ();
4949f59b64bfSJoe Perches
4950f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
4951f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
4952f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
4953f59b64bfSJoe Perches				@def_args = split(",", $define_args);
4954f59b64bfSJoe Perches			}
4955f59b64bfSJoe Perches
4956292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4957c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4958c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4959c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4960c45dcabdSAndy Whitcroft
4961c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4962bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4963bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
49646b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4965bf30d6edSAndy Whitcroft			{
4966c45dcabdSAndy Whitcroft			}
4967c45dcabdSAndy Whitcroft
4968e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
496933acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
497033acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4971e45bab8eSAndy Whitcroft			{
4972e45bab8eSAndy Whitcroft			}
4973e45bab8eSAndy Whitcroft
497442e15293SJoe Perches			# Make asm volatile uses seem like a generic function
497542e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
497642e15293SJoe Perches
4977c45dcabdSAndy Whitcroft			my $exceptions = qr{
4978c45dcabdSAndy Whitcroft				$Declare|
4979c45dcabdSAndy Whitcroft				module_param_named|
4980a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4981c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4982c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4983383099fdSAndy Whitcroft				__typeof__\(|
498422fd2d3eSStefani Seibold				union|
498522fd2d3eSStefani Seibold				struct|
4986ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
49876b10df42SVladimir Zapolskiy				^\"|\"$|
49886b10df42SVladimir Zapolskiy				^\[
4989c45dcabdSAndy Whitcroft			}x;
49905eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4991f59b64bfSJoe Perches
4992f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
4993f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
4994e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
4995f59b64bfSJoe Perches
4996f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4997f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4998f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
49993cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5000356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5001f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5002f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5003e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
500472f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5005f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5006f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5007f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
50084e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5009f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5010c45dcabdSAndy Whitcroft			{
5011e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5012e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5013e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5014e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5015f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5016f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5017f74bd194SAndy Whitcroft				} else {
5018000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5019388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5020d8aaf121SAndy Whitcroft				}
5021f59b64bfSJoe Perches
5022f59b64bfSJoe Perches			}
50235207649bSJoe Perches
50245207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
50255207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
50265207649bSJoe Perches			my $first = 1;
50275207649bSJoe Perches			$define_stmt = "";
50285207649bSJoe Perches			foreach my $l (@stmt_array) {
50295207649bSJoe Perches				$l =~ s/\\$//;
50305207649bSJoe Perches				if ($first) {
50315207649bSJoe Perches					$define_stmt = $l;
50325207649bSJoe Perches					$first = 0;
50335207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
50345207649bSJoe Perches					$define_stmt .= substr($l, 1);
50355207649bSJoe Perches				}
50365207649bSJoe Perches			}
50375207649bSJoe Perches			$define_stmt =~ s/$;//g;
50385207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
50395207649bSJoe Perches			$define_stmt = trim($define_stmt);
50405207649bSJoe Perches
5041f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5042f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5043f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
50449192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
50457fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
50467fe528a2SJoe Perches				$tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
50477fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
50487fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5049d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5050f59b64bfSJoe Perches				if ($use_cnt > 1) {
5051f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5052f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5053f59b64bfSJoe Perches				    }
50549192d41aSJoe Perches# check if any macro arguments may have other precedence issues
50557fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
50569192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
50579192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
50589192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
50599192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
50609192d41aSJoe Perches				}
50610a920b5bSAndy Whitcroft			}
50625023d347SJoe Perches
506308a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
506408a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
506508a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
506608a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5067e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
506808a2843eSJoe Perches
506908a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
507008a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
507108a2843eSJoe Perches			}
507208a2843eSJoe Perches
5073481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
50745023d347SJoe Perches
50755023d347SJoe Perches		} else {
50765023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5077481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5078481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
50795023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
50805023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
50815023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
50825023d347SJoe Perches			}
5083653d4876SAndy Whitcroft		}
50840a920b5bSAndy Whitcroft
5085b13edf7fSJoe Perches# do {} while (0) macro tests:
5086b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5087b13edf7fSJoe Perches# macro should not end with a semicolon
50885b57980dSJoe Perches		if ($perl_version_ok &&
5089b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5090b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5091b13edf7fSJoe Perches			my $ln = $linenr;
5092b13edf7fSJoe Perches			my $cnt = $realcnt;
5093b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5094b13edf7fSJoe Perches			my $ctx = '';
5095b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5096b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5097b13edf7fSJoe Perches			$ctx = $dstat;
5098b13edf7fSJoe Perches
5099b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
51001b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5101b13edf7fSJoe Perches
5102b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5103b13edf7fSJoe Perches				my $stmts = $2;
5104b13edf7fSJoe Perches				my $semis = $3;
5105b13edf7fSJoe Perches
5106b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5107b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5108e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5109b13edf7fSJoe Perches
5110ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5111ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5112b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5113b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5114b13edf7fSJoe Perches				}
5115b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5116b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5117b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5118b13edf7fSJoe Perches				}
5119f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5120f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5121f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5122e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5123f5ef95b1SJoe Perches
5124f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5125f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5126b13edf7fSJoe Perches			}
5127b13edf7fSJoe Perches		}
5128b13edf7fSJoe Perches
5129f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
513013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
513113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5132cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
513313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5134cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5135cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5136aad4f614SJoe Perches				my @allowed = ();
5137aad4f614SJoe Perches				my $allow = 0;
513813214adfSAndy Whitcroft				my $seen = 0;
5139773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5140cf655043SAndy Whitcroft				my $ln = $linenr - 1;
514113214adfSAndy Whitcroft				for my $chunk (@chunks) {
514213214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
514313214adfSAndy Whitcroft
5144773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5145773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5146773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5147773647a0SAndy Whitcroft
5148aad4f614SJoe Perches					$allowed[$allow] = 0;
5149773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5150773647a0SAndy Whitcroft
5151773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5152773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5153773647a0SAndy Whitcroft
5154773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5155cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5156cf655043SAndy Whitcroft
5157773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
515813214adfSAndy Whitcroft
515913214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
516013214adfSAndy Whitcroft
5161aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5162cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5163cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5164aad4f614SJoe Perches						$allowed[$allow] = 1;
516513214adfSAndy Whitcroft					}
516613214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5167cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5168aad4f614SJoe Perches						$allowed[$allow] = 1;
516913214adfSAndy Whitcroft					}
5170cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5171cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5172aad4f614SJoe Perches						$allowed[$allow] = 1;
517313214adfSAndy Whitcroft					}
5174aad4f614SJoe Perches					$allow++;
517513214adfSAndy Whitcroft				}
5176aad4f614SJoe Perches				if ($seen) {
5177aad4f614SJoe Perches					my $sum_allowed = 0;
5178aad4f614SJoe Perches					foreach (@allowed) {
5179aad4f614SJoe Perches						$sum_allowed += $_;
5180aad4f614SJoe Perches					}
5181aad4f614SJoe Perches					if ($sum_allowed == 0) {
5182000d1cc1SJoe Perches						WARN("BRACES",
5183000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5184aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5185aad4f614SJoe Perches						 $seen != $allow) {
5186aad4f614SJoe Perches						CHK("BRACES",
5187aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5188aad4f614SJoe Perches					}
518913214adfSAndy Whitcroft				}
519013214adfSAndy Whitcroft			}
519113214adfSAndy Whitcroft		}
5192773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
519313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5194cf655043SAndy Whitcroft			my $allowed = 0;
5195f0a594c1SAndy Whitcroft
5196cf655043SAndy Whitcroft			# Check the pre-context.
5197cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5198cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5199cf655043SAndy Whitcroft				$allowed = 1;
5200f0a594c1SAndy Whitcroft			}
5201773647a0SAndy Whitcroft
5202773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5203773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5204773647a0SAndy Whitcroft
5205cf655043SAndy Whitcroft			# Check the condition.
5206cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5207773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5208cf655043SAndy Whitcroft			if (defined $cond) {
5209773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5210cf655043SAndy Whitcroft			}
5211cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5212cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5213cf655043SAndy Whitcroft				$allowed = 1;
5214cf655043SAndy Whitcroft			}
5215cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5216cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5217cf655043SAndy Whitcroft				$allowed = 1;
5218cf655043SAndy Whitcroft			}
5219cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5220cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5221cf655043SAndy Whitcroft				$allowed = 1;
5222cf655043SAndy Whitcroft			}
5223cf655043SAndy Whitcroft			# Check the post-context.
5224cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5225cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5226cf655043SAndy Whitcroft				if (defined $cond) {
5227773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5228cf655043SAndy Whitcroft				}
5229cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5230cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5231cf655043SAndy Whitcroft					$allowed = 1;
5232cf655043SAndy Whitcroft				}
5233cf655043SAndy Whitcroft			}
5234cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5235f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5236e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5237cf655043SAndy Whitcroft
5238000d1cc1SJoe Perches				WARN("BRACES",
5239000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5240f0a594c1SAndy Whitcroft			}
5241f0a594c1SAndy Whitcroft		}
5242f0a594c1SAndy Whitcroft
5243e4c5babdSJoe Perches# check for single line unbalanced braces
524495330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
524595330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5246e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5247e4c5babdSJoe Perches		}
5248e4c5babdSJoe Perches
52490979ae66SJoe Perches# check for unnecessary blank lines around braces
525077b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5251f8e58219SJoe Perches			if (CHK("BRACES",
5252f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5253f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5254f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5255f8e58219SJoe Perches			}
52560979ae66SJoe Perches		}
525777b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5258f8e58219SJoe Perches			if (CHK("BRACES",
5259f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5260f8e58219SJoe Perches			    $fix) {
5261f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5262f8e58219SJoe Perches			}
52630979ae66SJoe Perches		}
52640979ae66SJoe Perches
52654a0df2efSAndy Whitcroft# no volatiles please
52666c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
52676c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5268000d1cc1SJoe Perches			WARN("VOLATILE",
52698c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
52704a0df2efSAndy Whitcroft		}
52714a0df2efSAndy Whitcroft
52725e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
52735e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
52745e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
52755e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
527633acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
52775e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
52785e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
52795e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
52805e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
52815e4f6ba5SJoe Perches				     $fix &&
52825e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
52835e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
52845e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
52855e4f6ba5SJoe Perches				my $comma_close = "";
52865e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
52875e4f6ba5SJoe Perches					$comma_close = $1;
52885e4f6ba5SJoe Perches				}
52895e4f6ba5SJoe Perches
52905e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
52915e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
52925e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
52935e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
52945e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
52955e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
52965e4f6ba5SJoe Perches				$fixedline = $rawline;
52975e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
52985e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
52995e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
53005e4f6ba5SJoe Perches				}
53015e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
53025e4f6ba5SJoe Perches			}
53035e4f6ba5SJoe Perches		}
53045e4f6ba5SJoe Perches
53055e4f6ba5SJoe Perches# check for missing a space in a string concatenation
53065e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
53075e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
53085e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
53095e4f6ba5SJoe Perches		}
53105e4f6ba5SJoe Perches
531177cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5312e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5313e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5314e4b7d309SJoe Perches# function declarations
531577cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
531677cb8546SJoe Perches		    defined($context_function) &&
5317e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5318e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
531977cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5320e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
532177cb8546SJoe Perches		}
532277cb8546SJoe Perches
53235e4f6ba5SJoe Perches# check for spaces before a quoted newline
53245e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
53255e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
53265e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
53275e4f6ba5SJoe Perches			    $fix) {
53285e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
53295e4f6ba5SJoe Perches			}
53305e4f6ba5SJoe Perches
53315e4f6ba5SJoe Perches		}
53325e4f6ba5SJoe Perches
5333f17dba4fSJoe Perches# concatenated string without spaces between elements
533479682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
533579682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
533679682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
533779682c0cSJoe Perches			    $fix) {
533879682c0cSJoe Perches				while ($line =~ /($String)/g) {
533979682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
534079682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
534179682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
534279682c0cSJoe Perches				}
534379682c0cSJoe Perches			}
5344f17dba4fSJoe Perches		}
5345f17dba4fSJoe Perches
534690ad30e5SJoe Perches# uncoalesced string fragments
534733acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
534879682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
534979682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
535079682c0cSJoe Perches			    $fix) {
535179682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
535279682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
535379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
535479682c0cSJoe Perches				}
535579682c0cSJoe Perches			}
535690ad30e5SJoe Perches		}
535790ad30e5SJoe Perches
5358522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5359522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5360522b837cSAlexey Dobriyan		my $show_Z = 1;
53615e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5362522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
53635e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5364522b837cSAlexey Dobriyan			# check for %L
5365522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
53665e4f6ba5SJoe Perches				WARN("PRINTF_L",
5367522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5368522b837cSAlexey Dobriyan				$show_L = 0;
53695e4f6ba5SJoe Perches			}
5370522b837cSAlexey Dobriyan			# check for %Z
5371522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5372522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5373522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5374522b837cSAlexey Dobriyan				$show_Z = 0;
5375522b837cSAlexey Dobriyan			}
5376522b837cSAlexey Dobriyan			# check for 0x<decimal>
5377522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5378522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
53796e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
53806e300757SJoe Perches			}
53815e4f6ba5SJoe Perches		}
53825e4f6ba5SJoe Perches
53835e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
53843f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
53855e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
53865e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
53875e4f6ba5SJoe Perches		}
53885e4f6ba5SJoe Perches
538900df344fSAndy Whitcroft# warn about #if 0
5390c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5391000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5392000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5393de7d4f0eSAndy Whitcroft				$herecurr);
53944a0df2efSAndy Whitcroft		}
53954a0df2efSAndy Whitcroft
539603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
539703df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5398100425deSJoe Perches			my $tested = quotemeta($1);
5399100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5400100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5401100425deSJoe Perches				my $func = $1;
5402100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5403100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5404100425deSJoe Perches				    $fix) {
5405100425deSJoe Perches					my $do_fix = 1;
5406100425deSJoe Perches					my $leading_tabs = "";
5407100425deSJoe Perches					my $new_leading_tabs = "";
5408100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5409100425deSJoe Perches						$leading_tabs = $1;
5410100425deSJoe Perches					} else {
5411100425deSJoe Perches						$do_fix = 0;
5412100425deSJoe Perches					}
5413100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5414100425deSJoe Perches						$new_leading_tabs = $1;
5415100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5416100425deSJoe Perches							$do_fix = 0;
5417100425deSJoe Perches						}
5418100425deSJoe Perches					} else {
5419100425deSJoe Perches						$do_fix = 0;
5420100425deSJoe Perches					}
5421100425deSJoe Perches					if ($do_fix) {
5422100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5423100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5424100425deSJoe Perches					}
5425100425deSJoe Perches				}
54264c432a8fSGreg Kroah-Hartman			}
54274c432a8fSGreg Kroah-Hartman		}
5428f0a594c1SAndy Whitcroft
5429ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5430ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5431ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5432ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5433ebfdc409SJoe Perches		    $linenr > 3) {
5434ebfdc409SJoe Perches			my $testval = $2;
5435ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5436ebfdc409SJoe Perches
5437ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5438ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5439ebfdc409SJoe Perches
5440fb0d0e08SJoe 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)/) {
5441ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5442ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5443ebfdc409SJoe Perches			}
5444ebfdc409SJoe Perches		}
5445ebfdc409SJoe Perches
5446f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5447dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5448f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5449f78d98f6SJoe Perches			my $level = $1;
5450f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5451f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5452f78d98f6SJoe Perches			    $fix) {
5453f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5454f78d98f6SJoe Perches			}
5455f78d98f6SJoe Perches		}
5456f78d98f6SJoe Perches
545745c55e92SJoe Perches# check for logging continuations
545845c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
545945c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
546045c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
546145c55e92SJoe Perches		}
546245c55e92SJoe Perches
5463abb08a53SJoe Perches# check for mask then right shift without a parentheses
54645b57980dSJoe Perches		if ($perl_version_ok &&
5465abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5466abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5467abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5468abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5469abb08a53SJoe Perches		}
5470abb08a53SJoe Perches
5471b75ac618SJoe Perches# check for pointer comparisons to NULL
54725b57980dSJoe Perches		if ($perl_version_ok) {
5473b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5474b75ac618SJoe Perches				my $val = $1;
5475b75ac618SJoe Perches				my $equal = "!";
5476b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5477b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5478b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5479b75ac618SJoe Perches					    $fix) {
5480b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5481b75ac618SJoe Perches				}
5482b75ac618SJoe Perches			}
5483b75ac618SJoe Perches		}
5484b75ac618SJoe Perches
54858716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
54868716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
54878716de38SJoe Perches			my $attr = $1;
54888716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
54898716de38SJoe Perches				my $ptr = $1;
54908716de38SJoe Perches				my $var = $2;
54918716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
54928716de38SJoe Perches				      ERROR("MISPLACED_INIT",
54938716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
54948716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
54958716de38SJoe Perches				      WARN("MISPLACED_INIT",
54968716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
54978716de38SJoe Perches				    $fix) {
5498194f66fcSJoe 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;
54998716de38SJoe Perches				}
55008716de38SJoe Perches			}
55018716de38SJoe Perches		}
55028716de38SJoe Perches
5503e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5504e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5505e970b884SJoe Perches			my $attr = $1;
5506e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5507e970b884SJoe Perches			my $attr_prefix = $1;
5508e970b884SJoe Perches			my $attr_type = $2;
5509e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5510e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5511e970b884SJoe Perches			    $fix) {
5512194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5513e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5514e970b884SJoe Perches			}
5515e970b884SJoe Perches		}
5516e970b884SJoe Perches
5517e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5518e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5519e970b884SJoe Perches			my $attr = $1;
5520e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5521e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5522e970b884SJoe Perches			    $fix) {
5523194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5524e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5525e970b884SJoe Perches				$lead = rtrim($1);
5526e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5527e970b884SJoe Perches				$lead = "${lead}const ";
5528194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5529e970b884SJoe Perches			}
5530e970b884SJoe Perches		}
5531e970b884SJoe Perches
5532c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5533c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5534c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5535c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5536c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5537c17893c7SJoe Perches			    $fix) {
5538c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5539c17893c7SJoe Perches			}
5540c17893c7SJoe Perches		}
5541c17893c7SJoe Perches
5542fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5543fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5544fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5545fbdb8138SJoe Perches			my $constant_func = $1;
5546fbdb8138SJoe Perches			my $func = $constant_func;
5547fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5548fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5549fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5550fbdb8138SJoe Perches			    $fix) {
5551194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5552fbdb8138SJoe Perches			}
5553fbdb8138SJoe Perches		}
5554fbdb8138SJoe Perches
55551a15a250SPatrick Pannuto# prefer usleep_range over udelay
555637581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
555743c1d77cSJoe Perches			my $delay = $1;
55581a15a250SPatrick Pannuto			# ignore udelay's < 10, however
555943c1d77cSJoe Perches			if (! ($delay < 10) ) {
5560000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
556143c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
556243c1d77cSJoe Perches			}
556343c1d77cSJoe Perches			if ($delay > 2000) {
556443c1d77cSJoe Perches				WARN("LONG_UDELAY",
556543c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
55661a15a250SPatrick Pannuto			}
55671a15a250SPatrick Pannuto		}
55681a15a250SPatrick Pannuto
556909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
557009ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
557109ef8725SPatrick Pannuto			if ($1 < 20) {
5572000d1cc1SJoe Perches				WARN("MSLEEP",
557343c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
557409ef8725SPatrick Pannuto			}
557509ef8725SPatrick Pannuto		}
557609ef8725SPatrick Pannuto
557736ec1939SJoe Perches# check for comparisons of jiffies
557836ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
557936ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
558036ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
558136ec1939SJoe Perches		}
558236ec1939SJoe Perches
55839d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
55849d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
55859d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
55869d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
55879d7a34a5SJoe Perches		}
55889d7a34a5SJoe Perches
558900df344fSAndy Whitcroft# warn about #ifdefs in C files
5590c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
559100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
559200df344fSAndy Whitcroft#			print "$herecurr";
559300df344fSAndy Whitcroft#			$clean = 0;
559400df344fSAndy Whitcroft#		}
559500df344fSAndy Whitcroft
559622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5597c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
55983705ce5bSJoe Perches			if (ERROR("SPACING",
55993705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
56003705ce5bSJoe Perches			    $fix) {
5601194f66fcSJoe Perches				$fixed[$fixlinenr] =~
56023705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
56033705ce5bSJoe Perches			}
56043705ce5bSJoe Perches
560522f2a2efSAndy Whitcroft		}
560622f2a2efSAndy Whitcroft
56074a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5608171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5609171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
56104a0df2efSAndy Whitcroft			my $which = $1;
56114a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5612000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5613000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
56144a0df2efSAndy Whitcroft			}
56154a0df2efSAndy Whitcroft		}
56164a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5617402c2553SMichael S. Tsirkin
5618402c2553SMichael S. Tsirkin		my $barriers = qr{
5619402c2553SMichael S. Tsirkin			mb|
5620402c2553SMichael S. Tsirkin			rmb|
5621402c2553SMichael S. Tsirkin			wmb|
5622402c2553SMichael S. Tsirkin			read_barrier_depends
5623402c2553SMichael S. Tsirkin		}x;
5624402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5625402c2553SMichael S. Tsirkin			mb__before_atomic|
5626402c2553SMichael S. Tsirkin			mb__after_atomic|
5627402c2553SMichael S. Tsirkin			store_release|
5628402c2553SMichael S. Tsirkin			load_acquire|
5629402c2553SMichael S. Tsirkin			store_mb|
5630402c2553SMichael S. Tsirkin			(?:$barriers)
5631402c2553SMichael S. Tsirkin		}x;
5632402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5633402c2553SMichael S. Tsirkin			(?:$barriers)|
563443e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
563543e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5636402c2553SMichael S. Tsirkin		}x;
5637402c2553SMichael S. Tsirkin
5638402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
56394a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5640c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5641000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
56424a0df2efSAndy Whitcroft			}
56434a0df2efSAndy Whitcroft		}
56443ad81779SPaul E. McKenney
5645f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5646f4073b0fSMichael S. Tsirkin
5647f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5648f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5649f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5650f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5651f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5652f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5653f4073b0fSMichael S. Tsirkin		}
5654f4073b0fSMichael S. Tsirkin
5655cb426e99SJoe Perches# check for waitqueue_active without a comment.
5656cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5657cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5658cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5659cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5660cb426e99SJoe Perches			}
5661cb426e99SJoe Perches		}
56623ad81779SPaul E. McKenney
566391db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
566491db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
566591db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
566691db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
566791db2592SPaul E. McKenney		}
566891db2592SPaul E. McKenney
56694a0df2efSAndy Whitcroft# check of hardware specific defines
5670c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5671000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5672000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
56730a920b5bSAndy Whitcroft		}
5674653d4876SAndy Whitcroft
5675596ed45bSJoe Perches# check that the storage class is not after a type
5676596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5677000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5678596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5679596ed45bSJoe Perches		}
5680596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5681596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5682596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5683596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5684596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5685596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5686596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5687d4977c78STobias Klauser		}
5688d4977c78STobias Klauser
5689de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5690de7d4f0eSAndy Whitcroft# storage class and type.
56919c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
56929c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5693000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5694000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5695de7d4f0eSAndy Whitcroft		}
5696de7d4f0eSAndy Whitcroft
56978905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
56982b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56992b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5700d5e616fcSJoe Perches			if (WARN("INLINE",
5701d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5702d5e616fcSJoe Perches			    $fix) {
5703194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5704d5e616fcSJoe Perches
5705d5e616fcSJoe Perches			}
57068905a67cSAndy Whitcroft		}
57078905a67cSAndy Whitcroft
57083d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
57092b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
57102b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5711000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5712000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
57133d130fd0SJoe Perches		}
57143d130fd0SJoe Perches
571539b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
57162b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
57172b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5718000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5719000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
572039b7e287SJoe Perches		}
572139b7e287SJoe Perches
57225f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
57232b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
57242b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5725d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5726d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5727d5e616fcSJoe Perches			    $fix) {
5728194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5729d5e616fcSJoe Perches
5730d5e616fcSJoe Perches			}
57315f14d3bdSJoe Perches		}
57325f14d3bdSJoe Perches
57336061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
57342b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
57352b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5736d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5737d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5738d5e616fcSJoe Perches			    $fix) {
5739194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5740d5e616fcSJoe Perches			}
57416061d949SJoe Perches		}
57426061d949SJoe Perches
5743619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
57445b57980dSJoe Perches		if ($perl_version_ok &&
5745619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5746619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5747619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5748619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5749619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5750619a908aSJoe Perches		}
5751619a908aSJoe Perches
5752fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5753e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5754fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5755e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5756e6176fa4SJoe Perches			my $type = $1;
5757e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5758e6176fa4SJoe Perches				$type = $1;
5759e6176fa4SJoe Perches				my $kernel_type = 'u';
5760e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5761e6176fa4SJoe Perches				$type =~ /(\d+)/;
5762e6176fa4SJoe Perches				$kernel_type .= $1;
5763e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5764e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5765e6176fa4SJoe Perches				    $fix) {
5766e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5767e6176fa4SJoe Perches				}
5768e6176fa4SJoe Perches			}
5769e6176fa4SJoe Perches		}
5770e6176fa4SJoe Perches
5771938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5772938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5773938224b5SJoe Perches			my $cast = $1;
5774938224b5SJoe Perches			my $const = $2;
5775938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5776938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5777938224b5SJoe Perches			    $fix) {
5778938224b5SJoe Perches				my $suffix = "";
5779938224b5SJoe Perches				my $newconst = $const;
5780938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5781938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5782938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5783938224b5SJoe Perches					$suffix .= 'LL';
5784938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5785938224b5SJoe Perches					$suffix .= 'L';
5786938224b5SJoe Perches				}
5787938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5788938224b5SJoe Perches			}
5789938224b5SJoe Perches		}
5790938224b5SJoe Perches
57918f53a9b8SJoe Perches# check for sizeof(&)
57928f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5793000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5794000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
57958f53a9b8SJoe Perches		}
57968f53a9b8SJoe Perches
579766c80b60SJoe Perches# check for sizeof without parenthesis
579866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5799d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5800d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5801d5e616fcSJoe Perches			    $fix) {
5802194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5803d5e616fcSJoe Perches			}
580466c80b60SJoe Perches		}
580566c80b60SJoe Perches
580688982feaSJoe Perches# check for struct spinlock declarations
580788982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
580888982feaSJoe Perches			WARN("USE_SPINLOCK_T",
580988982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
581088982feaSJoe Perches		}
581188982feaSJoe Perches
5812a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
581306668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5814a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5815caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5816caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5817d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5818d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5819d5e616fcSJoe Perches				    $fix) {
5820194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5821d5e616fcSJoe Perches				}
5822a6962d72SJoe Perches			}
5823a6962d72SJoe Perches		}
5824a6962d72SJoe Perches
58250b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
58265b57980dSJoe Perches		if ($perl_version_ok &&
58270b523769SJoe Perches		    defined $stat &&
58280b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
58290b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
5830e3c6bc95STobin C. Harding			my $stat_real;
5831e3c6bc95STobin C. Harding
58320b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
58330b523769SJoe Perches			$lc = $lc + $linenr;
58340b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
5835ffe07513SJoe Perches				my $specifier;
5836ffe07513SJoe Perches				my $extension;
5837ffe07513SJoe Perches				my $bad_specifier = "";
58380b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
58390b523769SJoe Perches				$fmt =~ s/%%//g;
5840e3c6bc95STobin C. Harding
5841e3c6bc95STobin C. Harding				while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5842e3c6bc95STobin C. Harding					$specifier = $1;
5843e3c6bc95STobin C. Harding					$extension = $2;
5844e3c6bc95STobin C. Harding					if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5845e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
58460b523769SJoe Perches						last;
58470b523769SJoe Perches					}
5848e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
5849e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
5850e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
58510b523769SJoe Perches						}
5852e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
5853e3c6bc95STobin 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");
5854e3c6bc95STobin C. Harding					}
5855e3c6bc95STobin C. Harding				}
5856e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
58572a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
58581df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
58591df7338aSSergey Senozhatsky					my $use = "";
5860e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
58611df7338aSSergey Senozhatsky						$ext_type = "Deprecated";
58621df7338aSSergey Senozhatsky						$use = " - use %pS instead";
5863e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
58641df7338aSSergey Senozhatsky					}
58652a9f9d85STobin C. Harding
58660b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
5867e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5868e3c6bc95STobin C. Harding				}
58690b523769SJoe Perches			}
58700b523769SJoe Perches		}
58710b523769SJoe Perches
5872554e165cSAndy Whitcroft# Check for misused memsets
58735b57980dSJoe Perches		if ($perl_version_ok &&
5874d1fe9c09SJoe Perches		    defined $stat &&
58759e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5876554e165cSAndy Whitcroft
5877d7c76ba7SJoe Perches			my $ms_addr = $2;
5878d1fe9c09SJoe Perches			my $ms_val = $7;
5879d1fe9c09SJoe Perches			my $ms_size = $12;
5880d7c76ba7SJoe Perches
5881554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5882554e165cSAndy Whitcroft				ERROR("MEMSET",
5883d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5884554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5885554e165cSAndy Whitcroft				WARN("MEMSET",
5886d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5887d7c76ba7SJoe Perches			}
5888d7c76ba7SJoe Perches		}
5889d7c76ba7SJoe Perches
589098a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
58915b57980dSJoe Perches#		if ($perl_version_ok &&
5892f333195dSJoe Perches#		    defined $stat &&
5893f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5894f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
5895f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5896f333195dSJoe Perches#			    $fix) {
5897f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5898f333195dSJoe Perches#			}
5899f333195dSJoe Perches#		}
590098a9bba5SJoe Perches
5901b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
59025b57980dSJoe Perches#		if ($perl_version_ok &&
5903f333195dSJoe Perches#		    defined $stat &&
5904f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5905f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
5906f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5907f333195dSJoe Perches#		}
5908b6117d17SMateusz Kulikowski
59098617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
59108617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
59115b57980dSJoe Perches#		if ($perl_version_ok &&
5912f333195dSJoe Perches#		    defined $stat &&
5913f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5914f333195dSJoe Perches#
5915f333195dSJoe Perches#			my $ms_val = $7;
5916f333195dSJoe Perches#
5917f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
5918f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
5919f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5920f333195dSJoe Perches#				    $fix) {
5921f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5922f333195dSJoe Perches#				}
5923f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5924f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
5925f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5926f333195dSJoe Perches#				    $fix) {
5927f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5928f333195dSJoe Perches#				}
5929f333195dSJoe Perches#			}
5930f333195dSJoe Perches#		}
59318617cd09SMateusz Kulikowski
5932d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
59335b57980dSJoe Perches		if ($perl_version_ok &&
5934d1fe9c09SJoe Perches		    defined $stat &&
5935d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5936d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5937d7c76ba7SJoe Perches				my $call = $1;
5938d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5939d7c76ba7SJoe Perches				my $arg1 = $3;
5940d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5941d1fe9c09SJoe Perches				my $arg2 = $8;
5942d7c76ba7SJoe Perches				my $cast;
5943d7c76ba7SJoe Perches
5944d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5945d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5946d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5947d7c76ba7SJoe Perches					$cast = $cast1;
5948d7c76ba7SJoe Perches				} else {
5949d7c76ba7SJoe Perches					$cast = $cast2;
5950d7c76ba7SJoe Perches				}
5951d7c76ba7SJoe Perches				WARN("MINMAX",
5952d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5953554e165cSAndy Whitcroft			}
5954554e165cSAndy Whitcroft		}
5955554e165cSAndy Whitcroft
59564a273195SJoe Perches# check usleep_range arguments
59575b57980dSJoe Perches		if ($perl_version_ok &&
59584a273195SJoe Perches		    defined $stat &&
59594a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
59604a273195SJoe Perches			my $min = $1;
59614a273195SJoe Perches			my $max = $7;
59624a273195SJoe Perches			if ($min eq $max) {
59634a273195SJoe Perches				WARN("USLEEP_RANGE",
59644a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
59654a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
59664a273195SJoe Perches				 $min > $max) {
59674a273195SJoe Perches				WARN("USLEEP_RANGE",
59684a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
59694a273195SJoe Perches			}
59704a273195SJoe Perches		}
59714a273195SJoe Perches
5972823b794cSJoe Perches# check for naked sscanf
59735b57980dSJoe Perches		if ($perl_version_ok &&
5974823b794cSJoe Perches		    defined $stat &&
59756c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5976823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5977823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5978823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5979823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5980823b794cSJoe Perches			$lc = $lc + $linenr;
59812a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
5982823b794cSJoe Perches			WARN("NAKED_SSCANF",
5983823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5984823b794cSJoe Perches		}
5985823b794cSJoe Perches
5986afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
59875b57980dSJoe Perches		if ($perl_version_ok &&
5988afc819abSJoe Perches		    defined $stat &&
5989afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5990afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5991afc819abSJoe Perches			$lc = $lc + $linenr;
59922a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
5993afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5994afc819abSJoe Perches				my $format = $6;
5995afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5996afc819abSJoe Perches				if ($count == 1 &&
5997afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5998afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5999afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6000afc819abSJoe Perches				}
6001afc819abSJoe Perches			}
6002afc819abSJoe Perches		}
6003afc819abSJoe Perches
600470dc8a48SJoe Perches# check for new externs in .h files.
600570dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
600670dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6007d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
600870dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
600970dc8a48SJoe Perches			    $fix) {
6010194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
601170dc8a48SJoe Perches			}
601270dc8a48SJoe Perches		}
601370dc8a48SJoe Perches
6014de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6015171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6016c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6017171ae1a4SAndy Whitcroft		{
6018c45dcabdSAndy Whitcroft			my $function_name = $1;
6019c45dcabdSAndy Whitcroft			my $paren_space = $2;
6020171ae1a4SAndy Whitcroft
6021171ae1a4SAndy Whitcroft			my $s = $stat;
6022171ae1a4SAndy Whitcroft			if (defined $cond) {
6023171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6024171ae1a4SAndy Whitcroft			}
6025c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6026c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6027c45dcabdSAndy Whitcroft			{
6028000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6029000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6030de7d4f0eSAndy Whitcroft			}
6031de7d4f0eSAndy Whitcroft
6032171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6033000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6034000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6035171ae1a4SAndy Whitcroft			}
60369c9ba34eSAndy Whitcroft
60379c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
60389c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
60399c9ba34eSAndy Whitcroft		{
6040000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6041000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6042171ae1a4SAndy Whitcroft		}
6043171ae1a4SAndy Whitcroft
6044a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6045a0ad7596SJoe Perches		if (defined $stat &&
604625bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6047ca0d8929SJoe Perches		    $1 ne "void") {
6048ca0d8929SJoe Perches			my $args = trim($1);
6049ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6050ca0d8929SJoe Perches				my $arg = trim($1);
6051ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6052ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6053ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6054ca0d8929SJoe Perches				}
6055ca0d8929SJoe Perches			}
6056ca0d8929SJoe Perches		}
6057ca0d8929SJoe Perches
6058a0ad7596SJoe Perches# check for function definitions
60595b57980dSJoe Perches		if ($perl_version_ok &&
6060a0ad7596SJoe Perches		    defined $stat &&
6061a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6062a0ad7596SJoe Perches			$context_function = $1;
6063a0ad7596SJoe Perches
6064a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6065a0ad7596SJoe Perches			my $ok = 0;
6066a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6067a0ad7596SJoe Perches			my $herectx = $here . "\n";
6068a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6069a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6070a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6071a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6072a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6073a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6074a0ad7596SJoe Perches			}
6075a0ad7596SJoe Perches			if (!$ok) {
6076a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6077a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6078a0ad7596SJoe Perches			}
6079a0ad7596SJoe Perches		}
6080a0ad7596SJoe Perches
6081de7d4f0eSAndy Whitcroft# checks for new __setup's
6082de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6083de7d4f0eSAndy Whitcroft			my $name = $1;
6084de7d4f0eSAndy Whitcroft
6085de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6086000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
60878c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6088de7d4f0eSAndy Whitcroft			}
6089653d4876SAndy Whitcroft		}
60909c0ca6f9SAndy Whitcroft
60919c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
6092caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
6093000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6094000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
60959c0ca6f9SAndy Whitcroft		}
609613214adfSAndy Whitcroft
6097a640d25cSJoe Perches# alloc style
6098a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
60995b57980dSJoe Perches		if ($perl_version_ok &&
6100a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6101a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6102a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6103a640d25cSJoe Perches		}
6104a640d25cSJoe Perches
610560a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
61065b57980dSJoe Perches		if ($perl_version_ok &&
61071b4a2ed4SJoe Perches		    defined $stat &&
61081b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
610960a55369SJoe Perches			my $oldfunc = $3;
611060a55369SJoe Perches			my $a1 = $4;
611160a55369SJoe Perches			my $a2 = $10;
611260a55369SJoe Perches			my $newfunc = "kmalloc_array";
611360a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
611460a55369SJoe Perches			my $r1 = $a1;
611560a55369SJoe Perches			my $r2 = $a2;
611660a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
611760a55369SJoe Perches				$r1 = $a2;
611860a55369SJoe Perches				$r2 = $a1;
611960a55369SJoe Perches			}
6120e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6121e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
61221b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6123e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6124e3d95a2aSTobin C. Harding
6125e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
61261b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
61271b4a2ed4SJoe Perches				    $cnt == 1 &&
6128e367455aSJoe Perches				    $fix) {
6129194f66fcSJoe 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;
613060a55369SJoe Perches				}
613160a55369SJoe Perches			}
613260a55369SJoe Perches		}
613360a55369SJoe Perches
6134972fdea2SJoe Perches# check for krealloc arg reuse
61355b57980dSJoe Perches		if ($perl_version_ok &&
6136972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6137972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6138972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6139972fdea2SJoe Perches		}
6140972fdea2SJoe Perches
61415ce59ae0SJoe Perches# check for alloc argument mismatch
61425ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
61435ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
61445ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
61455ce59ae0SJoe Perches		}
61465ce59ae0SJoe Perches
6147caf2a54fSJoe Perches# check for multiple semicolons
6148caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6149d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6150d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6151d5e616fcSJoe Perches			    $fix) {
6152194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6153d5e616fcSJoe Perches			}
6154d1e2ad07SJoe Perches		}
6155d1e2ad07SJoe Perches
6156cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6157cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6158cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
61590ab90191SJoe Perches			my $ull = "";
61600ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
61610ab90191SJoe Perches			if (CHK("BIT_MACRO",
61620ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
61630ab90191SJoe Perches			    $fix) {
61640ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
61650ab90191SJoe Perches			}
61660ab90191SJoe Perches		}
61670ab90191SJoe Perches
61682d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
61692d632745SJoe 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*$/) {
61702d632745SJoe Perches			my $config = $1;
61712d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
61722d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
61732d632745SJoe Perches			    $fix) {
61742d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
61752d632745SJoe Perches			}
61762d632745SJoe Perches		}
61772d632745SJoe Perches
6178e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6179c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6180c34c09a8SJoe Perches			my $has_break = 0;
6181c34c09a8SJoe Perches			my $has_statement = 0;
6182c34c09a8SJoe Perches			my $count = 0;
6183c34c09a8SJoe Perches			my $prevline = $linenr;
6184e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6185c34c09a8SJoe Perches				$prevline--;
6186c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6187c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6188c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6189c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6190c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6191c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6192c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6193c34c09a8SJoe Perches				$has_statement = 1;
6194c34c09a8SJoe Perches				$count++;
6195258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6196c34c09a8SJoe Perches			}
6197c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6198c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6199224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6200c34c09a8SJoe Perches			}
6201c34c09a8SJoe Perches		}
6202c34c09a8SJoe Perches
6203d1e2ad07SJoe Perches# check for switch/default statements without a break;
62045b57980dSJoe Perches		if ($perl_version_ok &&
6205d1e2ad07SJoe Perches		    defined $stat &&
6206d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6207d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6208e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6209e3d95a2aSTobin C. Harding
6210d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6211d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6212caf2a54fSJoe Perches		}
6213caf2a54fSJoe Perches
621413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6215d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6216d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6217d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6218d5e616fcSJoe Perches			    $fix) {
6219194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6220d5e616fcSJoe Perches			}
622113214adfSAndy Whitcroft		}
6222773647a0SAndy Whitcroft
622362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
622462ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
622562ec818fSJoe Perches			ERROR("DATE_TIME",
622662ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
622762ec818fSJoe Perches		}
622862ec818fSJoe Perches
62292c92488aSJoe Perches# check for use of yield()
62302c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
62312c92488aSJoe Perches			WARN("YIELD",
62322c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
62332c92488aSJoe Perches		}
62342c92488aSJoe Perches
6235179f8f40SJoe Perches# check for comparisons against true and false
6236179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6237179f8f40SJoe Perches			my $lead = $1;
6238179f8f40SJoe Perches			my $arg = $2;
6239179f8f40SJoe Perches			my $test = $3;
6240179f8f40SJoe Perches			my $otype = $4;
6241179f8f40SJoe Perches			my $trail = $5;
6242179f8f40SJoe Perches			my $op = "!";
6243179f8f40SJoe Perches
6244179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6245179f8f40SJoe Perches
6246179f8f40SJoe Perches			my $type = lc($otype);
6247179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6248179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6249179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6250179f8f40SJoe Perches					$op = "";
6251179f8f40SJoe Perches				}
6252179f8f40SJoe Perches
6253179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6254179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6255179f8f40SJoe Perches
6256179f8f40SJoe Perches## maybe suggesting a correct construct would better
6257179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6258179f8f40SJoe Perches
6259179f8f40SJoe Perches			}
6260179f8f40SJoe Perches		}
6261179f8f40SJoe Perches
62625d430902SJoe Perches# check for bool bitfields
62635d430902SJoe Perches		if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
62645d430902SJoe Perches			WARN("BOOL_BITFIELD",
62655d430902SJoe Perches			     "Avoid using bool as bitfield.  Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
62665d430902SJoe Perches		}
62675d430902SJoe Perches
6268d729593eSJoe Perches# check for bool use in .h files
6269d729593eSJoe Perches		if ($realfile =~ /\.h$/ &&
6270d729593eSJoe Perches		    $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6271d729593eSJoe Perches			CHK("BOOL_MEMBER",
6272d729593eSJoe Perches			    "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6273d729593eSJoe Perches		}
6274d729593eSJoe Perches
62754882720bSThomas Gleixner# check for semaphores initialized locked
62764882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6277000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6278000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6279773647a0SAndy Whitcroft		}
62806712d858SJoe Perches
628167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
628267d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6283000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
628467d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6285773647a0SAndy Whitcroft		}
62866712d858SJoe Perches
6287ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6288f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6289000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6290ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6291f3db6639SMichael Ellerman		}
62926712d858SJoe Perches
62930f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6294d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
62956903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6296d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6297000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6298d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
62992b6db5cbSAndy Whitcroft		}
6300773647a0SAndy Whitcroft
6301773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6302773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6303773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6304c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6305c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6306171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6307171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6308171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6309773647a0SAndy Whitcroft		{
6310000d1cc1SJoe Perches			WARN("NR_CPUS",
6311000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6312773647a0SAndy Whitcroft		}
63139c9ba34eSAndy Whitcroft
631452ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
631552ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
631652ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
631752ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
631852ea8506SJoe Perches		}
631952ea8506SJoe Perches
6320acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
63215b57980dSJoe Perches		if ($perl_version_ok &&
6322acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6323acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6324acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6325acd9362cSJoe Perches		}
6326acd9362cSJoe Perches
6327691d77b6SAndy Whitcroft# whine mightly about in_atomic
6328691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6329691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6330000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6331000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6332f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6333000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6334000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6335691d77b6SAndy Whitcroft			}
6336691d77b6SAndy Whitcroft		}
63371704f47bSPeter Zijlstra
63380f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
63390f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
63400f5225b0SPeter Zijlstra			ERROR("LOCKING",
63410f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
63420f5225b0SPeter Zijlstra		}
63430f5225b0SPeter Zijlstra
63441704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
63451704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
63461704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
63471704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
63481704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
63491704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6350000d1cc1SJoe Perches				ERROR("LOCKDEP",
6351000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
63521704f47bSPeter Zijlstra			}
63531704f47bSPeter Zijlstra		}
635488f8831cSDave Jones
6355b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6356b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6357000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6358000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
635988f8831cSDave Jones		}
63602435880fSJoe Perches
636100180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
636200180468SJoe Perches# and whether or not function naming is typical and if
636300180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
63645b57980dSJoe Perches		if ($perl_version_ok &&
636500180468SJoe Perches		    defined $stat &&
636600180468SJoe 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*\)/) {
636700180468SJoe Perches			my $var = $1;
636800180468SJoe Perches			my $perms = $2;
636900180468SJoe Perches			my $show = $3;
637000180468SJoe Perches			my $store = $4;
637100180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
637200180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
637300180468SJoe Perches			    $store =~ /^${var}_store$/ &&
637400180468SJoe Perches			    $octal_perms eq "0644") {
637500180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
637600180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
637700180468SJoe Perches				    $fix) {
637800180468SJoe 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})/;
637900180468SJoe Perches				}
638000180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
638100180468SJoe Perches				 $store =~ /^NULL$/ &&
638200180468SJoe Perches				 $octal_perms eq "0444") {
638300180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
638400180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
638500180468SJoe Perches				    $fix) {
638600180468SJoe 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})/;
638700180468SJoe Perches				}
638800180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
638900180468SJoe Perches				 $store =~ /^${var}_store$/ &&
639000180468SJoe Perches				 $octal_perms eq "0200") {
639100180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
639200180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
639300180468SJoe Perches				    $fix) {
639400180468SJoe 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})/;
639500180468SJoe Perches				}
639600180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
639700180468SJoe Perches				 $octal_perms eq "0444" ||
639800180468SJoe Perches				 $octal_perms eq "0200") {
639900180468SJoe Perches				my $newshow = "$show";
640000180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
640100180468SJoe Perches				my $newstore = $store;
640200180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
640300180468SJoe Perches				my $rename = "";
640400180468SJoe Perches				if ($show ne $newshow) {
640500180468SJoe Perches					$rename .= " '$show' to '$newshow'";
640600180468SJoe Perches				}
640700180468SJoe Perches				if ($store ne $newstore) {
640800180468SJoe Perches					$rename .= " '$store' to '$newstore'";
640900180468SJoe Perches				}
641000180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
641100180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
641200180468SJoe Perches			} else {
641300180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
641400180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
641500180468SJoe Perches			}
641600180468SJoe Perches		}
641700180468SJoe Perches
6418515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6419515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
642073121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
642173121534SJoe Perches#   specific definition of not visible in sysfs.
642273121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
642373121534SJoe Perches#   use the default permissions
64245b57980dSJoe Perches		if ($perl_version_ok &&
6425459cf0aeSJoe Perches		    defined $stat &&
6426515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
64272435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
64282435880fSJoe Perches				my $func = $entry->[0];
64292435880fSJoe Perches				my $arg_pos = $entry->[1];
64302435880fSJoe Perches
6431459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6432459cf0aeSJoe Perches				$lc = $lc + $linenr;
64332a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6434459cf0aeSJoe Perches
64352435880fSJoe Perches				my $skip_args = "";
64362435880fSJoe Perches				if ($arg_pos > 1) {
64372435880fSJoe Perches					$arg_pos--;
64382435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
64392435880fSJoe Perches				}
6440f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6441459cf0aeSJoe Perches				if ($stat =~ /$test/) {
64422435880fSJoe Perches					my $val = $1;
64432435880fSJoe Perches					$val = $6 if ($skip_args ne "");
644473121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
644573121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
644673121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
64472435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6448459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6449f90774e1SJoe Perches					}
6450f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6451c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6452459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
64532435880fSJoe Perches					}
6454459cf0aeSJoe Perches				}
6455459cf0aeSJoe Perches			}
6456459cf0aeSJoe Perches		}
6457459cf0aeSJoe Perches
6458459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6459bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
646000180468SJoe Perches			my $oval = $1;
646100180468SJoe Perches			my $octal = perms_to_octal($oval);
6462f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6463459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6464f90774e1SJoe Perches			    $fix) {
646500180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
64662435880fSJoe Perches			}
646713214adfSAndy Whitcroft		}
64685a6d20ceSBjorn Andersson
64695a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
64705a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
64715a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
64725a6d20ceSBjorn Andersson			my $valid_licenses = qr{
64735a6d20ceSBjorn Andersson						GPL|
64745a6d20ceSBjorn Andersson						GPL\ v2|
64755a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
64765a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
64775a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
64785a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
64795a6d20ceSBjorn Andersson						Proprietary
64805a6d20ceSBjorn Andersson					}x;
64815a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
64825a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
64835a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
64845a6d20ceSBjorn Andersson			}
64855a6d20ceSBjorn Andersson		}
6486515a235eSJoe Perches	}
648713214adfSAndy Whitcroft
648813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
648913214adfSAndy Whitcroft	# so just keep quiet.
649013214adfSAndy Whitcroft	if ($#rawlines == -1) {
649113214adfSAndy Whitcroft		exit(0);
64920a920b5bSAndy Whitcroft	}
64930a920b5bSAndy Whitcroft
64948905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
64958905a67cSAndy Whitcroft	# things that appear to be patches.
64968905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
64978905a67cSAndy Whitcroft		exit(0);
64988905a67cSAndy Whitcroft	}
64998905a67cSAndy Whitcroft
65008905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
65018905a67cSAndy Whitcroft	# just keep quiet.
65028905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
65038905a67cSAndy Whitcroft		exit(0);
65048905a67cSAndy Whitcroft	}
65058905a67cSAndy Whitcroft
6506a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6507000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6508000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
65090a920b5bSAndy Whitcroft	}
6510ed43c4e5SAllen Hubbe	if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6511000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6512000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
65130a920b5bSAndy Whitcroft	}
65140a920b5bSAndy Whitcroft
6515f0a594c1SAndy Whitcroft	print report_dump();
651613214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
651713214adfSAndy Whitcroft		print "$filename " if ($summary_file);
65186c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
65196c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
65206c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
65216c72ffaaSAndy Whitcroft	}
65228905a67cSAndy Whitcroft
6523d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6524ef212196SJoe Perches		# If there were any defects found and not already fixing them
6525ef212196SJoe Perches		if (!$clean and !$fix) {
6526ef212196SJoe Perches			print << "EOM"
6527ef212196SJoe Perches
6528ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6529ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6530ef212196SJoe PerchesEOM
6531ef212196SJoe Perches		}
6532d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6533d2c0a235SAndy Whitcroft		# then suggest that.
6534d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6535b0781216SMike Frysinger			$rpt_cleaners = 0;
6536d8469f16SJoe Perches			print << "EOM"
6537d8469f16SJoe Perches
6538d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6539d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6540d8469f16SJoe PerchesEOM
6541d2c0a235SAndy Whitcroft		}
6542d2c0a235SAndy Whitcroft	}
6543d2c0a235SAndy Whitcroft
6544d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6545d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6546d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
65479624b8d6SJoe Perches		my $newfile = $filename;
65489624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
65493705ce5bSJoe Perches		my $linecount = 0;
65503705ce5bSJoe Perches		my $f;
65513705ce5bSJoe Perches
6552d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6553d752fcc8SJoe Perches
65543705ce5bSJoe Perches		open($f, '>', $newfile)
65553705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
65563705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
65573705ce5bSJoe Perches			$linecount++;
65583705ce5bSJoe Perches			if ($file) {
65593705ce5bSJoe Perches				if ($linecount > 3) {
65603705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
65613705ce5bSJoe Perches					print $f $fixed_line . "\n";
65623705ce5bSJoe Perches				}
65633705ce5bSJoe Perches			} else {
65643705ce5bSJoe Perches				print $f $fixed_line . "\n";
65653705ce5bSJoe Perches			}
65663705ce5bSJoe Perches		}
65673705ce5bSJoe Perches		close($f);
65683705ce5bSJoe Perches
65693705ce5bSJoe Perches		if (!$quiet) {
65703705ce5bSJoe Perches			print << "EOM";
6571d8469f16SJoe Perches
65723705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
65733705ce5bSJoe Perches
65743705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
65753705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
65763705ce5bSJoe Perches
65773705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
65783705ce5bSJoe PerchesNo warranties, expressed or implied...
65793705ce5bSJoe PerchesEOM
65803705ce5bSJoe Perches		}
65813705ce5bSJoe Perches	}
65823705ce5bSJoe Perches
6583d8469f16SJoe Perches	if ($quiet == 0) {
6584d8469f16SJoe Perches		print "\n";
6585d8469f16SJoe Perches		if ($clean == 1) {
6586d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6587d8469f16SJoe Perches		} else {
6588d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
65890a920b5bSAndy Whitcroft		}
65900a920b5bSAndy Whitcroft	}
65910a920b5bSAndy Whitcroft	return $clean;
65920a920b5bSAndy Whitcroft}
6593