xref: /linux-6.15/scripts/checkpatch.pl (revision f34e4a4f)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
9c707a81dSJoe Perchesuse POSIX;
1036061e38SJoe Perchesuse File::Basename;
1136061e38SJoe Perchesuse Cwd 'abs_path';
120a920b5bSAndy Whitcroft
130a920b5bSAndy Whitcroftmy $P = $0;
1436061e38SJoe Perchesmy $D = dirname(abs_path($P));
150a920b5bSAndy Whitcroft
16000d1cc1SJoe Perchesmy $V = '0.32';
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
190a920b5bSAndy Whitcroft
200a920b5bSAndy Whitcroftmy $quiet = 0;
210a920b5bSAndy Whitcroftmy $tree = 1;
220a920b5bSAndy Whitcroftmy $chk_signoff = 1;
230a920b5bSAndy Whitcroftmy $chk_patch = 1;
24773647a0SAndy Whitcroftmy $tst_only;
256c72ffaaSAndy Whitcroftmy $emacs = 0;
268905a67cSAndy Whitcroftmy $terse = 0;
276c72ffaaSAndy Whitcroftmy $file = 0;
286c72ffaaSAndy Whitcroftmy $check = 0;
292ac73b4fSJoe Perchesmy $check_orig = 0;
308905a67cSAndy Whitcroftmy $summary = 1;
318905a67cSAndy Whitcroftmy $mailback = 0;
3213214adfSAndy Whitcroftmy $summary_file = 0;
33000d1cc1SJoe Perchesmy $show_types = 0;
343705ce5bSJoe Perchesmy $fix = 0;
359624b8d6SJoe Perchesmy $fix_inplace = 0;
366c72ffaaSAndy Whitcroftmy $root;
37c2fdda0dSAndy Whitcroftmy %debug;
383445686aSJoe Perchesmy %camelcase = ();
3991bfe484SJoe Perchesmy %use_type = ();
4091bfe484SJoe Perchesmy @use = ();
4191bfe484SJoe Perchesmy %ignore_type = ();
42000d1cc1SJoe Perchesmy @ignore = ();
4377f5b10aSHannes Edermy $help = 0;
44000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
456cd7f386SJoe Perchesmy $max_line_length = 80;
46d62a201fSDave Hansenmy $ignore_perl_version = 0;
47d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
4856193274SVadim Bendeburymy $min_conf_desc_length = 4;
4966b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
50ebfd7d62SJoe Perchesmy $codespell = 0;
51ebfd7d62SJoe Perchesmy $codespellfile = "/usr/local/share/codespell/dictionary.txt";
5277f5b10aSHannes Eder
5377f5b10aSHannes Edersub help {
5477f5b10aSHannes Eder	my ($exitcode) = @_;
5577f5b10aSHannes Eder
5677f5b10aSHannes Eder	print << "EOM";
5777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5877f5b10aSHannes EderVersion: $V
5977f5b10aSHannes Eder
6077f5b10aSHannes EderOptions:
6177f5b10aSHannes Eder  -q, --quiet                quiet
6277f5b10aSHannes Eder  --no-tree                  run without a kernel tree
6377f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
6477f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
6577f5b10aSHannes Eder  --emacs                    emacs compile window format
6677f5b10aSHannes Eder  --terse                    one line per report
6777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6991bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
70000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
716cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
7256193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
73000d1cc1SJoe Perches  --show-types               show the message "types" in the output
7477f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
7577f5b10aSHannes Eder  --no-summary               suppress the per-file summary
7677f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
7777f5b10aSHannes Eder  --summary-file             include the filename in summary
7877f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7977f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
8077f5b10aSHannes Eder                             is all off)
8177f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
8277f5b10aSHannes Eder                             literally
833705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
843705ce5bSJoe Perches                             If correctable single-line errors exist, create
853705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
863705ce5bSJoe Perches                             with potential errors corrected to the preferred
873705ce5bSJoe Perches                             checkpatch style
889624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
899624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
909624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
91d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
92d62a201fSDave Hansen                             runtime errors.
93ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
94ebfd7d62SJoe Perches                             (default:/usr/local/share/codespell/dictionary.txt)
95ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
9677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
9777f5b10aSHannes Eder
9877f5b10aSHannes EderWhen FILE is - read standard input.
9977f5b10aSHannes EderEOM
10077f5b10aSHannes Eder
10177f5b10aSHannes Eder	exit($exitcode);
10277f5b10aSHannes Eder}
10377f5b10aSHannes Eder
104000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
105000d1cc1SJoe Perchesif (-f $conf) {
106000d1cc1SJoe Perches	my @conf_args;
107000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
108000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
109000d1cc1SJoe Perches
110000d1cc1SJoe Perches	while (<$conffile>) {
111000d1cc1SJoe Perches		my $line = $_;
112000d1cc1SJoe Perches
113000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
114000d1cc1SJoe Perches		$line =~ s/^\s*//g;
115000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
116000d1cc1SJoe Perches
117000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
118000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
119000d1cc1SJoe Perches
120000d1cc1SJoe Perches		my @words = split(" ", $line);
121000d1cc1SJoe Perches		foreach my $word (@words) {
122000d1cc1SJoe Perches			last if ($word =~ m/^#/);
123000d1cc1SJoe Perches			push (@conf_args, $word);
124000d1cc1SJoe Perches		}
125000d1cc1SJoe Perches	}
126000d1cc1SJoe Perches	close($conffile);
127000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
128000d1cc1SJoe Perches}
129000d1cc1SJoe Perches
1300a920b5bSAndy WhitcroftGetOptions(
1316c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1320a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1330a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1340a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1356c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1368905a67cSAndy Whitcroft	'terse!'	=> \$terse,
13777f5b10aSHannes Eder	'f|file!'	=> \$file,
1386c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1396c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
140000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
14191bfe484SJoe Perches	'types=s'	=> \@use,
142000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1436cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
14456193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1456c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1468905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1478905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
14813214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1493705ce5bSJoe Perches	'fix!'		=> \$fix,
1509624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
151d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
152c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
153773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
154ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
155ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
15677f5b10aSHannes Eder	'h|help'	=> \$help,
15777f5b10aSHannes Eder	'version'	=> \$help
15877f5b10aSHannes Eder) or help(1);
15977f5b10aSHannes Eder
16077f5b10aSHannes Ederhelp(0) if ($help);
1610a920b5bSAndy Whitcroft
1629624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1632ac73b4fSJoe Perches$check_orig = $check;
1649624b8d6SJoe Perches
1650a920b5bSAndy Whitcroftmy $exit = 0;
1660a920b5bSAndy Whitcroft
167d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
168d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
169d62a201fSDave Hansen	if (!$ignore_perl_version) {
170d62a201fSDave Hansen		exit(1);
171d62a201fSDave Hansen	}
172d62a201fSDave Hansen}
173d62a201fSDave Hansen
1740a920b5bSAndy Whitcroftif ($#ARGV < 0) {
17577f5b10aSHannes Eder	print "$P: no input files\n";
1760a920b5bSAndy Whitcroft	exit(1);
1770a920b5bSAndy Whitcroft}
1780a920b5bSAndy Whitcroft
17991bfe484SJoe Perchessub hash_save_array_words {
18091bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
18191bfe484SJoe Perches
18291bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
18391bfe484SJoe Perches	foreach my $word (@array) {
184000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
185000d1cc1SJoe Perches		$word =~ s/^\s*//g;
186000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
187000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
188000d1cc1SJoe Perches
189000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
190000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
191000d1cc1SJoe Perches
19291bfe484SJoe Perches		$hashRef->{$word}++;
193000d1cc1SJoe Perches	}
19491bfe484SJoe Perches}
19591bfe484SJoe Perches
19691bfe484SJoe Perchessub hash_show_words {
19791bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
19891bfe484SJoe Perches
19958cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
20091bfe484SJoe Perches		print "NOTE: $prefix message types:";
20158cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
20291bfe484SJoe Perches			print " $word";
20391bfe484SJoe Perches		}
20491bfe484SJoe Perches		print "\n\n";
20591bfe484SJoe Perches	}
20691bfe484SJoe Perches}
20791bfe484SJoe Perches
20891bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
20991bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
210000d1cc1SJoe Perches
211c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
212c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2137429c690SAndy Whitcroftmy $dbg_type = 0;
214a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
215c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
21621caa13cSAndy Whitcroft	## no critic
21721caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
21821caa13cSAndy Whitcroft	die "$@" if ($@);
219c2fdda0dSAndy Whitcroft}
220c2fdda0dSAndy Whitcroft
221d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
222d2c0a235SAndy Whitcroft
2238905a67cSAndy Whitcroftif ($terse) {
2248905a67cSAndy Whitcroft	$emacs = 1;
2258905a67cSAndy Whitcroft	$quiet++;
2268905a67cSAndy Whitcroft}
2278905a67cSAndy Whitcroft
2286c72ffaaSAndy Whitcroftif ($tree) {
2296c72ffaaSAndy Whitcroft	if (defined $root) {
2306c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2316c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2326c72ffaaSAndy Whitcroft		}
2336c72ffaaSAndy Whitcroft	} else {
2346c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2356c72ffaaSAndy Whitcroft			$root = '.';
2366c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2376c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2386c72ffaaSAndy Whitcroft			$root = $1;
2396c72ffaaSAndy Whitcroft		}
2406c72ffaaSAndy Whitcroft	}
2416c72ffaaSAndy Whitcroft
2426c72ffaaSAndy Whitcroft	if (!defined $root) {
2430a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2440a920b5bSAndy Whitcroft		exit(2);
2450a920b5bSAndy Whitcroft	}
2466c72ffaaSAndy Whitcroft}
2476c72ffaaSAndy Whitcroft
2486c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2496c72ffaaSAndy Whitcroft
2502ceb532bSAndy Whitcroftour $Ident	= qr{
2512ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2522ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2532ceb532bSAndy Whitcroft		}x;
2546c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2556c72ffaaSAndy Whitcroftour $Sparse	= qr{
2566c72ffaaSAndy Whitcroft			__user|
2576c72ffaaSAndy Whitcroft			__kernel|
2586c72ffaaSAndy Whitcroft			__force|
2596c72ffaaSAndy Whitcroft			__iomem|
2606c72ffaaSAndy Whitcroft			__must_check|
2616c72ffaaSAndy Whitcroft			__init_refok|
262417495edSAndy Whitcroft			__kprobes|
263165e72a6SSven Eckelmann			__ref|
264165e72a6SSven Eckelmann			__rcu
2656c72ffaaSAndy Whitcroft		}x;
266e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
267e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
268e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
269e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
270e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2718716de38SJoe Perches
27252131292SWolfram Sang# Notes to $Attribute:
27352131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2746c72ffaaSAndy Whitcroftour $Attribute	= qr{
2756c72ffaaSAndy Whitcroft			const|
27603f1df7dSJoe Perches			__percpu|
27703f1df7dSJoe Perches			__nocast|
27803f1df7dSJoe Perches			__safe|
27903f1df7dSJoe Perches			__bitwise__|
28003f1df7dSJoe Perches			__packed__|
28103f1df7dSJoe Perches			__packed2__|
28203f1df7dSJoe Perches			__naked|
28303f1df7dSJoe Perches			__maybe_unused|
28403f1df7dSJoe Perches			__always_unused|
28503f1df7dSJoe Perches			__noreturn|
28603f1df7dSJoe Perches			__used|
28703f1df7dSJoe Perches			__cold|
288e23ef1f3SJoe Perches			__pure|
28903f1df7dSJoe Perches			__noclone|
29003f1df7dSJoe Perches			__deprecated|
2916c72ffaaSAndy Whitcroft			__read_mostly|
2926c72ffaaSAndy Whitcroft			__kprobes|
2938716de38SJoe Perches			$InitAttribute|
29424e1d81aSAndy Whitcroft			____cacheline_aligned|
29524e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2965fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2975fe3af11SAndy Whitcroft			__weak
2986c72ffaaSAndy Whitcroft		  }x;
299c45dcabdSAndy Whitcroftour $Modifier;
30091cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3016c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3026c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3036c72ffaaSAndy Whitcroft
30495e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
30595e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
30695e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
30795e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3082435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
309c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
310326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
311326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
312326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
31374349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3142435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
315326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
316447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
31723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3186c72ffaaSAndy Whitcroftour $Operators	= qr{
3196c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3206c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
32123f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3226c72ffaaSAndy Whitcroft		  }x;
3236c72ffaaSAndy Whitcroft
32491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
32591cb5195SJoe Perches
3268905a67cSAndy Whitcroftour $NonptrType;
3271813087dSJoe Perchesour $NonptrTypeMisordered;
3288716de38SJoe Perchesour $NonptrTypeWithAttr;
3298905a67cSAndy Whitcroftour $Type;
3301813087dSJoe Perchesour $TypeMisordered;
3318905a67cSAndy Whitcroftour $Declare;
3321813087dSJoe Perchesour $DeclareMisordered;
3338905a67cSAndy Whitcroft
33415662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
33515662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
336171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
337171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
338171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
339171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
340171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
341171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
342171ae1a4SAndy Whitcroft}x;
343171ae1a4SAndy Whitcroft
34415662b3eSJoe Perchesour $UTF8	= qr{
34515662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
34615662b3eSJoe Perches	| $NON_ASCII_UTF8
34715662b3eSJoe Perches}x;
34815662b3eSJoe Perches
349021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
350021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
351021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
352021158b4SJoe Perches)};
353021158b4SJoe Perches
3548ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
355fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3568ed22cadSAndy Whitcroft	atomic_t
3578ed22cadSAndy Whitcroft)};
3588ed22cadSAndy Whitcroft
359691e669bSJoe Perchesour $logFunctions = qr{(?x:
3606e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3617d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3626e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
363b0531722SJoe Perches	panic|
36406668727SJoe Perches	MODULE_[A-Z_]+|
36506668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
366691e669bSJoe Perches)};
367691e669bSJoe Perches
36820112475SJoe Perchesour $signature_tags = qr{(?xi:
36920112475SJoe Perches	Signed-off-by:|
37020112475SJoe Perches	Acked-by:|
37120112475SJoe Perches	Tested-by:|
37220112475SJoe Perches	Reviewed-by:|
37320112475SJoe Perches	Reported-by:|
3748543ae12SMugunthan V N	Suggested-by:|
37520112475SJoe Perches	To:|
37620112475SJoe Perches	Cc:
37720112475SJoe Perches)};
37820112475SJoe Perches
3791813087dSJoe Perchesour @typeListMisordered = (
3801813087dSJoe Perches	qr{char\s+(?:un)?signed},
3811813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3821813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3831813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3841813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3851813087dSJoe Perches	qr{short\s+(?:un)?signed},
3861813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
3871813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
3881813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
3891813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
3901813087dSJoe Perches	qr{int\s+(?:un)?signed},
3911813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
3921813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
3931813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
3941813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
3951813087dSJoe Perches	qr{long\s+(?:un)?signed},
3961813087dSJoe Perches);
3971813087dSJoe Perches
3988905a67cSAndy Whitcroftour @typeList = (
3998905a67cSAndy Whitcroft	qr{void},
4000c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4010c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4020c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4030c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4040c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4050c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4060c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4070c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4080c773d9dSJoe Perches	qr{(?:un)?signed},
4098905a67cSAndy Whitcroft	qr{float},
4108905a67cSAndy Whitcroft	qr{double},
4118905a67cSAndy Whitcroft	qr{bool},
4128905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4138905a67cSAndy Whitcroft	qr{union\s+$Ident},
4148905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4158905a67cSAndy Whitcroft	qr{${Ident}_t},
4168905a67cSAndy Whitcroft	qr{${Ident}_handler},
4178905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4181813087dSJoe Perches	@typeListMisordered,
4198905a67cSAndy Whitcroft);
4208716de38SJoe Perchesour @typeListWithAttr = (
4218716de38SJoe Perches	@typeList,
4228716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4238716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4248716de38SJoe Perches);
4258716de38SJoe Perches
426c45dcabdSAndy Whitcroftour @modifierList = (
427c45dcabdSAndy Whitcroft	qr{fastcall},
428c45dcabdSAndy Whitcroft);
4298905a67cSAndy Whitcroft
4302435880fSJoe Perchesour @mode_permission_funcs = (
4312435880fSJoe Perches	["module_param", 3],
4322435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4332435880fSJoe Perches	["module_param_array_named", 5],
4342435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4352435880fSJoe Perches	["proc_create(?:_data|)", 2],
4362435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4372435880fSJoe Perches);
4382435880fSJoe Perches
439515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
440515a235eSJoe Perchesour $mode_perms_search = "";
441515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
442515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
443515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
444515a235eSJoe Perches}
445515a235eSJoe Perches
446b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
447b392c64fSJoe Perches	S_IWUGO		|
448b392c64fSJoe Perches	S_IWOTH		|
449b392c64fSJoe Perches	S_IRWXUGO	|
450b392c64fSJoe Perches	S_IALLUGO	|
451b392c64fSJoe Perches	0[0-7][0-7][2367]
452b392c64fSJoe Perches}x;
453b392c64fSJoe Perches
4547840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4557840a94cSWolfram Sang	irq|
456cdcee686SSergey Ryazanov	memory|
457cdcee686SSergey Ryazanov	time|
458cdcee686SSergey Ryazanov	reboot
4597840a94cSWolfram Sang)};
4607840a94cSWolfram Sang# memory.h: ARM has a custom one
4617840a94cSWolfram Sang
46266b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
46366b47b4aSKees Cookmy $misspellings;
46466b47b4aSKees Cookmy %spelling_fix;
46536061e38SJoe Perches
46636061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
46766b47b4aSKees Cook	while (<$spelling>) {
46866b47b4aSKees Cook		my $line = $_;
46966b47b4aSKees Cook
47066b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
47166b47b4aSKees Cook		$line =~ s/^\s*//g;
47266b47b4aSKees Cook
47366b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
47466b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
47566b47b4aSKees Cook
47666b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
47766b47b4aSKees Cook
47866b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
47966b47b4aSKees Cook	}
48066b47b4aSKees Cook	close($spelling);
48136061e38SJoe Perches} else {
48236061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
48336061e38SJoe Perches}
48466b47b4aSKees Cook
485ebfd7d62SJoe Perchesif ($codespell) {
486ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
487ebfd7d62SJoe Perches		while (<$spelling>) {
488ebfd7d62SJoe Perches			my $line = $_;
489ebfd7d62SJoe Perches
490ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
491ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
492ebfd7d62SJoe Perches
493ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
494ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
495ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
496ebfd7d62SJoe Perches
497ebfd7d62SJoe Perches			$line =~ s/,.*$//;
498ebfd7d62SJoe Perches
499ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
500ebfd7d62SJoe Perches
501ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
502ebfd7d62SJoe Perches		}
503ebfd7d62SJoe Perches		close($spelling);
504ebfd7d62SJoe Perches	} else {
505ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
506ebfd7d62SJoe Perches	}
507ebfd7d62SJoe Perches}
508ebfd7d62SJoe Perches
509ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
510ebfd7d62SJoe Perches
5118905a67cSAndy Whitcroftsub build_types {
512d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
513d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
5141813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
5158716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
516c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
5178905a67cSAndy Whitcroft	$NonptrType	= qr{
518d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
519cf655043SAndy Whitcroft			(?:
5206b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
521021158b4SJoe Perches				(?:$typeOtherOSTypedefs\b)|
5228ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
523c45dcabdSAndy Whitcroft				(?:${all}\b)
524cf655043SAndy Whitcroft			)
525c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
5268905a67cSAndy Whitcroft		  }x;
5271813087dSJoe Perches	$NonptrTypeMisordered	= qr{
5281813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
5291813087dSJoe Perches			(?:
5301813087dSJoe Perches				(?:${Misordered}\b)
5311813087dSJoe Perches			)
5321813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
5331813087dSJoe Perches		  }x;
5348716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
5358716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
5368716de38SJoe Perches			(?:
5378716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
5388716de38SJoe Perches				(?:$typeTypedefs\b)|
539021158b4SJoe Perches				(?:$typeOtherOSTypedefs\b)|
5408716de38SJoe Perches				(?:${allWithAttr}\b)
5418716de38SJoe Perches			)
5428716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
5438716de38SJoe Perches		  }x;
5448905a67cSAndy Whitcroft	$Type	= qr{
545c45dcabdSAndy Whitcroft			$NonptrType
5461574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
547c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
5488905a67cSAndy Whitcroft		  }x;
5491813087dSJoe Perches	$TypeMisordered	= qr{
5501813087dSJoe Perches			$NonptrTypeMisordered
5511813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5521813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5531813087dSJoe Perches		  }x;
55491cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5551813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5568905a67cSAndy Whitcroft}
5578905a67cSAndy Whitcroftbuild_types();
5586c72ffaaSAndy Whitcroft
5597d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
560d1fe9c09SJoe Perches
561d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
562d1fe9c09SJoe Perches# requires at least perl version v5.10.0
563d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
564d1fe9c09SJoe Perches
565d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
5662435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
567c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
5687d2367afSJoe Perches
569f8422308SJoe Perchesour $declaration_macros = qr{(?x:
570f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
571f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
572f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
573f8422308SJoe Perches)};
574f8422308SJoe Perches
5757d2367afSJoe Perchessub deparenthesize {
5767d2367afSJoe Perches	my ($string) = @_;
5777d2367afSJoe Perches	return "" if (!defined($string));
5785b9553abSJoe Perches
5795b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5805b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5815b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5825b9553abSJoe Perches	}
5835b9553abSJoe Perches
5847d2367afSJoe Perches	$string =~ s@\s+@ @g;
5855b9553abSJoe Perches
5867d2367afSJoe Perches	return $string;
5877d2367afSJoe Perches}
5887d2367afSJoe Perches
5893445686aSJoe Perchessub seed_camelcase_file {
5903445686aSJoe Perches	my ($file) = @_;
5913445686aSJoe Perches
5923445686aSJoe Perches	return if (!(-f $file));
5933445686aSJoe Perches
5943445686aSJoe Perches	local $/;
5953445686aSJoe Perches
5963445686aSJoe Perches	open(my $include_file, '<', "$file")
5973445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
5983445686aSJoe Perches	my $text = <$include_file>;
5993445686aSJoe Perches	close($include_file);
6003445686aSJoe Perches
6013445686aSJoe Perches	my @lines = split('\n', $text);
6023445686aSJoe Perches
6033445686aSJoe Perches	foreach my $line (@lines) {
6043445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
6053445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
6063445686aSJoe Perches			$camelcase{$1} = 1;
60711ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
60811ea516aSJoe Perches			$camelcase{$1} = 1;
60911ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
6103445686aSJoe Perches			$camelcase{$1} = 1;
6113445686aSJoe Perches		}
6123445686aSJoe Perches	}
6133445686aSJoe Perches}
6143445686aSJoe Perches
6153445686aSJoe Perchesmy $camelcase_seeded = 0;
6163445686aSJoe Perchessub seed_camelcase_includes {
6173445686aSJoe Perches	return if ($camelcase_seeded);
6183445686aSJoe Perches
6193445686aSJoe Perches	my $files;
620c707a81dSJoe Perches	my $camelcase_cache = "";
621c707a81dSJoe Perches	my @include_files = ();
622c707a81dSJoe Perches
623c707a81dSJoe Perches	$camelcase_seeded = 1;
624351b2a1fSJoe Perches
6253645e328SRichard Genoud	if (-e ".git") {
626351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
627351b2a1fSJoe Perches		chomp $git_last_include_commit;
628c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
629c707a81dSJoe Perches	} else {
630c707a81dSJoe Perches		my $last_mod_date = 0;
631c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
632c707a81dSJoe Perches		@include_files = split('\n', $files);
633c707a81dSJoe Perches		foreach my $file (@include_files) {
634c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
635c707a81dSJoe Perches						   localtime((stat $file)[9]));
636c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
637c707a81dSJoe Perches		}
638c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
639c707a81dSJoe Perches	}
640c707a81dSJoe Perches
641c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
642c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
643c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
644351b2a1fSJoe Perches		while (<$camelcase_file>) {
645351b2a1fSJoe Perches			chomp;
646351b2a1fSJoe Perches			$camelcase{$_} = 1;
647351b2a1fSJoe Perches		}
648351b2a1fSJoe Perches		close($camelcase_file);
649351b2a1fSJoe Perches
650351b2a1fSJoe Perches		return;
651351b2a1fSJoe Perches	}
652c707a81dSJoe Perches
6533645e328SRichard Genoud	if (-e ".git") {
654c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
655c707a81dSJoe Perches		@include_files = split('\n', $files);
6563445686aSJoe Perches	}
657c707a81dSJoe Perches
6583445686aSJoe Perches	foreach my $file (@include_files) {
6593445686aSJoe Perches		seed_camelcase_file($file);
6603445686aSJoe Perches	}
661351b2a1fSJoe Perches
662c707a81dSJoe Perches	if ($camelcase_cache ne "") {
663351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
664c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
665c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
666351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
667351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
668351b2a1fSJoe Perches		}
669351b2a1fSJoe Perches		close($camelcase_file);
670351b2a1fSJoe Perches	}
6713445686aSJoe Perches}
6723445686aSJoe Perches
673d311cd44SJoe Perchessub git_commit_info {
674d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
675d311cd44SJoe Perches
676d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
677d311cd44SJoe Perches
678d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
679d311cd44SJoe Perches	$output =~ s/^\s*//gm;
680d311cd44SJoe Perches	my @lines = split("\n", $output);
681d311cd44SJoe Perches
6820d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
6830d7835fcSJoe Perches
684d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
685d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
686d311cd44SJoe Perches# all matching commit ids, but it's very slow...
687d311cd44SJoe Perches#
688d311cd44SJoe Perches#		echo "checking commits $1..."
689d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
690d311cd44SJoe Perches#		while read line ; do
691d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
692d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
693d311cd44SJoe Perches#		done
694d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
695d311cd44SJoe Perches	} else {
696d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
697d311cd44SJoe Perches		$desc = substr($lines[0], 41);
698d311cd44SJoe Perches	}
699d311cd44SJoe Perches
700d311cd44SJoe Perches	return ($id, $desc);
701d311cd44SJoe Perches}
702d311cd44SJoe Perches
7036c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
7040a920b5bSAndy Whitcroft
70500df344fSAndy Whitcroftmy @rawlines = ();
706c2fdda0dSAndy Whitcroftmy @lines = ();
7073705ce5bSJoe Perchesmy @fixed = ();
708d752fcc8SJoe Perchesmy @fixed_inserted = ();
709d752fcc8SJoe Perchesmy @fixed_deleted = ();
710194f66fcSJoe Perchesmy $fixlinenr = -1;
711194f66fcSJoe Perches
712c2fdda0dSAndy Whitcroftmy $vname;
7136c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
71421caa13cSAndy Whitcroft	my $FILE;
7156c72ffaaSAndy Whitcroft	if ($file) {
71621caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
7176c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
71821caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
71921caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
7206c72ffaaSAndy Whitcroft	} else {
72121caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
7226c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
7236c72ffaaSAndy Whitcroft	}
724c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
725c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
726c2fdda0dSAndy Whitcroft	} else {
727c2fdda0dSAndy Whitcroft		$vname = $filename;
728c2fdda0dSAndy Whitcroft	}
72921caa13cSAndy Whitcroft	while (<$FILE>) {
7300a920b5bSAndy Whitcroft		chomp;
73100df344fSAndy Whitcroft		push(@rawlines, $_);
7326c72ffaaSAndy Whitcroft	}
73321caa13cSAndy Whitcroft	close($FILE);
734c2fdda0dSAndy Whitcroft	if (!process($filename)) {
7350a920b5bSAndy Whitcroft		$exit = 1;
7360a920b5bSAndy Whitcroft	}
73700df344fSAndy Whitcroft	@rawlines = ();
73813214adfSAndy Whitcroft	@lines = ();
7393705ce5bSJoe Perches	@fixed = ();
740d752fcc8SJoe Perches	@fixed_inserted = ();
741d752fcc8SJoe Perches	@fixed_deleted = ();
742194f66fcSJoe Perches	$fixlinenr = -1;
7430a920b5bSAndy Whitcroft}
7440a920b5bSAndy Whitcroft
7450a920b5bSAndy Whitcroftexit($exit);
7460a920b5bSAndy Whitcroft
7470a920b5bSAndy Whitcroftsub top_of_kernel_tree {
7486c72ffaaSAndy Whitcroft	my ($root) = @_;
7496c72ffaaSAndy Whitcroft
7506c72ffaaSAndy Whitcroft	my @tree_check = (
7516c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
7526c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
7536c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
7546c72ffaaSAndy Whitcroft	);
7556c72ffaaSAndy Whitcroft
7566c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
7576c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
7580a920b5bSAndy Whitcroft			return 0;
7590a920b5bSAndy Whitcroft		}
7606c72ffaaSAndy Whitcroft	}
7616c72ffaaSAndy Whitcroft	return 1;
7626c72ffaaSAndy Whitcroft}
7630a920b5bSAndy Whitcroft
76420112475SJoe Perchessub parse_email {
76520112475SJoe Perches	my ($formatted_email) = @_;
76620112475SJoe Perches
76720112475SJoe Perches	my $name = "";
76820112475SJoe Perches	my $address = "";
76920112475SJoe Perches	my $comment = "";
77020112475SJoe Perches
77120112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
77220112475SJoe Perches		$name = $1;
77320112475SJoe Perches		$address = $2;
77420112475SJoe Perches		$comment = $3 if defined $3;
77520112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
77620112475SJoe Perches		$address = $1;
77720112475SJoe Perches		$comment = $2 if defined $2;
77820112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
77920112475SJoe Perches		$address = $1;
78020112475SJoe Perches		$comment = $2 if defined $2;
78120112475SJoe Perches		$formatted_email =~ s/$address.*$//;
78220112475SJoe Perches		$name = $formatted_email;
7833705ce5bSJoe Perches		$name = trim($name);
78420112475SJoe Perches		$name =~ s/^\"|\"$//g;
78520112475SJoe Perches		# If there's a name left after stripping spaces and
78620112475SJoe Perches		# leading quotes, and the address doesn't have both
78720112475SJoe Perches		# leading and trailing angle brackets, the address
78820112475SJoe Perches		# is invalid. ie:
78920112475SJoe Perches		#   "joe smith [email protected]" bad
79020112475SJoe Perches		#   "joe smith <[email protected]" bad
79120112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
79220112475SJoe Perches			$name = "";
79320112475SJoe Perches			$address = "";
79420112475SJoe Perches			$comment = "";
79520112475SJoe Perches		}
79620112475SJoe Perches	}
79720112475SJoe Perches
7983705ce5bSJoe Perches	$name = trim($name);
79920112475SJoe Perches	$name =~ s/^\"|\"$//g;
8003705ce5bSJoe Perches	$address = trim($address);
80120112475SJoe Perches	$address =~ s/^\<|\>$//g;
80220112475SJoe Perches
80320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
80420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
80520112475SJoe Perches		$name = "\"$name\"";
80620112475SJoe Perches	}
80720112475SJoe Perches
80820112475SJoe Perches	return ($name, $address, $comment);
80920112475SJoe Perches}
81020112475SJoe Perches
81120112475SJoe Perchessub format_email {
81220112475SJoe Perches	my ($name, $address) = @_;
81320112475SJoe Perches
81420112475SJoe Perches	my $formatted_email;
81520112475SJoe Perches
8163705ce5bSJoe Perches	$name = trim($name);
81720112475SJoe Perches	$name =~ s/^\"|\"$//g;
8183705ce5bSJoe Perches	$address = trim($address);
81920112475SJoe Perches
82020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
82120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
82220112475SJoe Perches		$name = "\"$name\"";
82320112475SJoe Perches	}
82420112475SJoe Perches
82520112475SJoe Perches	if ("$name" eq "") {
82620112475SJoe Perches		$formatted_email = "$address";
82720112475SJoe Perches	} else {
82820112475SJoe Perches		$formatted_email = "$name <$address>";
82920112475SJoe Perches	}
83020112475SJoe Perches
83120112475SJoe Perches	return $formatted_email;
83220112475SJoe Perches}
83320112475SJoe Perches
834d311cd44SJoe Perchessub which {
835d311cd44SJoe Perches	my ($bin) = @_;
836d311cd44SJoe Perches
837d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
838d311cd44SJoe Perches		if (-e "$path/$bin") {
839d311cd44SJoe Perches			return "$path/$bin";
840d311cd44SJoe Perches		}
841d311cd44SJoe Perches	}
842d311cd44SJoe Perches
843d311cd44SJoe Perches	return "";
844d311cd44SJoe Perches}
845d311cd44SJoe Perches
846000d1cc1SJoe Perchessub which_conf {
847000d1cc1SJoe Perches	my ($conf) = @_;
848000d1cc1SJoe Perches
849000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
850000d1cc1SJoe Perches		if (-e "$path/$conf") {
851000d1cc1SJoe Perches			return "$path/$conf";
852000d1cc1SJoe Perches		}
853000d1cc1SJoe Perches	}
854000d1cc1SJoe Perches
855000d1cc1SJoe Perches	return "";
856000d1cc1SJoe Perches}
857000d1cc1SJoe Perches
8580a920b5bSAndy Whitcroftsub expand_tabs {
8590a920b5bSAndy Whitcroft	my ($str) = @_;
8600a920b5bSAndy Whitcroft
8610a920b5bSAndy Whitcroft	my $res = '';
8620a920b5bSAndy Whitcroft	my $n = 0;
8630a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
8640a920b5bSAndy Whitcroft		if ($c eq "\t") {
8650a920b5bSAndy Whitcroft			$res .= ' ';
8660a920b5bSAndy Whitcroft			$n++;
8670a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
8680a920b5bSAndy Whitcroft				$res .= ' ';
8690a920b5bSAndy Whitcroft			}
8700a920b5bSAndy Whitcroft			next;
8710a920b5bSAndy Whitcroft		}
8720a920b5bSAndy Whitcroft		$res .= $c;
8730a920b5bSAndy Whitcroft		$n++;
8740a920b5bSAndy Whitcroft	}
8750a920b5bSAndy Whitcroft
8760a920b5bSAndy Whitcroft	return $res;
8770a920b5bSAndy Whitcroft}
8786c72ffaaSAndy Whitcroftsub copy_spacing {
879773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
8806c72ffaaSAndy Whitcroft	return $res;
8816c72ffaaSAndy Whitcroft}
8820a920b5bSAndy Whitcroft
8834a0df2efSAndy Whitcroftsub line_stats {
8844a0df2efSAndy Whitcroft	my ($line) = @_;
8854a0df2efSAndy Whitcroft
8864a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8874a0df2efSAndy Whitcroft	$line =~ s/^.//;
8884a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8894a0df2efSAndy Whitcroft
8904a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8914a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8924a0df2efSAndy Whitcroft
8934a0df2efSAndy Whitcroft	return (length($line), length($white));
8944a0df2efSAndy Whitcroft}
8954a0df2efSAndy Whitcroft
896773647a0SAndy Whitcroftmy $sanitise_quote = '';
897773647a0SAndy Whitcroft
898773647a0SAndy Whitcroftsub sanitise_line_reset {
899773647a0SAndy Whitcroft	my ($in_comment) = @_;
900773647a0SAndy Whitcroft
901773647a0SAndy Whitcroft	if ($in_comment) {
902773647a0SAndy Whitcroft		$sanitise_quote = '*/';
903773647a0SAndy Whitcroft	} else {
904773647a0SAndy Whitcroft		$sanitise_quote = '';
905773647a0SAndy Whitcroft	}
906773647a0SAndy Whitcroft}
90700df344fSAndy Whitcroftsub sanitise_line {
90800df344fSAndy Whitcroft	my ($line) = @_;
90900df344fSAndy Whitcroft
91000df344fSAndy Whitcroft	my $res = '';
91100df344fSAndy Whitcroft	my $l = '';
91200df344fSAndy Whitcroft
913c2fdda0dSAndy Whitcroft	my $qlen = 0;
914773647a0SAndy Whitcroft	my $off = 0;
915773647a0SAndy Whitcroft	my $c;
91600df344fSAndy Whitcroft
917773647a0SAndy Whitcroft	# Always copy over the diff marker.
918773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
919773647a0SAndy Whitcroft
920773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
921773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
922773647a0SAndy Whitcroft
923773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
924773647a0SAndy Whitcroft		# and end, all to $;.
925773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
926773647a0SAndy Whitcroft			$sanitise_quote = '*/';
927773647a0SAndy Whitcroft
928773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
929773647a0SAndy Whitcroft			$off++;
93000df344fSAndy Whitcroft			next;
931773647a0SAndy Whitcroft		}
93281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
933773647a0SAndy Whitcroft			$sanitise_quote = '';
934773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
935773647a0SAndy Whitcroft			$off++;
936773647a0SAndy Whitcroft			next;
937773647a0SAndy Whitcroft		}
938113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
939113f04a8SDaniel Walker			$sanitise_quote = '//';
940113f04a8SDaniel Walker
941113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
942113f04a8SDaniel Walker			$off++;
943113f04a8SDaniel Walker			next;
944113f04a8SDaniel Walker		}
945773647a0SAndy Whitcroft
946773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
947773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
948773647a0SAndy Whitcroft		    $c eq "\\") {
949773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
950773647a0SAndy Whitcroft			$off++;
951773647a0SAndy Whitcroft			next;
952773647a0SAndy Whitcroft		}
953773647a0SAndy Whitcroft		# Regular quotes.
954773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
955773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
956773647a0SAndy Whitcroft				$sanitise_quote = $c;
957773647a0SAndy Whitcroft
958773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
959773647a0SAndy Whitcroft				next;
960773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
961773647a0SAndy Whitcroft				$sanitise_quote = '';
96200df344fSAndy Whitcroft			}
96300df344fSAndy Whitcroft		}
964773647a0SAndy Whitcroft
965fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
966773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
967773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
968113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
969113f04a8SDaniel Walker			substr($res, $off, 1, $;);
970773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
971773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
97200df344fSAndy Whitcroft		} else {
973773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
97400df344fSAndy Whitcroft		}
975c2fdda0dSAndy Whitcroft	}
976c2fdda0dSAndy Whitcroft
977113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
978113f04a8SDaniel Walker		$sanitise_quote = '';
979113f04a8SDaniel Walker	}
980113f04a8SDaniel Walker
981c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
982c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
983c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
984c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
985c2fdda0dSAndy Whitcroft
986c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
987c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
988c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
989c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
990c2fdda0dSAndy Whitcroft	}
991c2fdda0dSAndy Whitcroft
99200df344fSAndy Whitcroft	return $res;
99300df344fSAndy Whitcroft}
99400df344fSAndy Whitcroft
995a6962d72SJoe Perchessub get_quoted_string {
996a6962d72SJoe Perches	my ($line, $rawline) = @_;
997a6962d72SJoe Perches
9985e4f6ba5SJoe Perches	return "" if ($line !~ m/(\"[X\t]+\")/g);
999a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1000a6962d72SJoe Perches}
1001a6962d72SJoe Perches
10028905a67cSAndy Whitcroftsub ctx_statement_block {
10038905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
10048905a67cSAndy Whitcroft	my $line = $linenr - 1;
10058905a67cSAndy Whitcroft	my $blk = '';
10068905a67cSAndy Whitcroft	my $soff = $off;
10078905a67cSAndy Whitcroft	my $coff = $off - 1;
1008773647a0SAndy Whitcroft	my $coff_set = 0;
10098905a67cSAndy Whitcroft
101013214adfSAndy Whitcroft	my $loff = 0;
101113214adfSAndy Whitcroft
10128905a67cSAndy Whitcroft	my $type = '';
10138905a67cSAndy Whitcroft	my $level = 0;
1014a2750645SAndy Whitcroft	my @stack = ();
1015cf655043SAndy Whitcroft	my $p;
10168905a67cSAndy Whitcroft	my $c;
10178905a67cSAndy Whitcroft	my $len = 0;
101813214adfSAndy Whitcroft
101913214adfSAndy Whitcroft	my $remainder;
10208905a67cSAndy Whitcroft	while (1) {
1021a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1022a2750645SAndy Whitcroft
1023773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
10248905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
10258905a67cSAndy Whitcroft		# context.
10268905a67cSAndy Whitcroft		if ($off >= $len) {
10278905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1028dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1029c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
10308905a67cSAndy Whitcroft				$remain--;
103113214adfSAndy Whitcroft				$loff = $len;
1032c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
10338905a67cSAndy Whitcroft				$len = length($blk);
10348905a67cSAndy Whitcroft				$line++;
10358905a67cSAndy Whitcroft				last;
10368905a67cSAndy Whitcroft			}
10378905a67cSAndy Whitcroft			# Bail if there is no further context.
10388905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
103913214adfSAndy Whitcroft			if ($off >= $len) {
10408905a67cSAndy Whitcroft				last;
10418905a67cSAndy Whitcroft			}
1042f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1043f74bd194SAndy Whitcroft				$level++;
1044f74bd194SAndy Whitcroft				$type = '#';
1045f74bd194SAndy Whitcroft			}
10468905a67cSAndy Whitcroft		}
1047cf655043SAndy Whitcroft		$p = $c;
10488905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
104913214adfSAndy Whitcroft		$remainder = substr($blk, $off);
10508905a67cSAndy Whitcroft
1051773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
10524635f4fbSAndy Whitcroft
10534635f4fbSAndy Whitcroft		# Handle nested #if/#else.
10544635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
10554635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
10564635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
10574635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
10584635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
10594635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
10604635f4fbSAndy Whitcroft		}
10614635f4fbSAndy Whitcroft
10628905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
10638905a67cSAndy Whitcroft		# outermost level.
10648905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
10658905a67cSAndy Whitcroft			last;
10668905a67cSAndy Whitcroft		}
10678905a67cSAndy Whitcroft
106813214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1069773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1070773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1071773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1072773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1073773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1074773647a0SAndy Whitcroft			$coff_set = 1;
1075773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1076773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
107713214adfSAndy Whitcroft		}
107813214adfSAndy Whitcroft
10798905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
10808905a67cSAndy Whitcroft			$level++;
10818905a67cSAndy Whitcroft			$type = '(';
10828905a67cSAndy Whitcroft		}
10838905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10848905a67cSAndy Whitcroft			$level--;
10858905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10868905a67cSAndy Whitcroft
10878905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10888905a67cSAndy Whitcroft				$coff = $off;
1089773647a0SAndy Whitcroft				$coff_set = 1;
1090773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10918905a67cSAndy Whitcroft			}
10928905a67cSAndy Whitcroft		}
10938905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
10948905a67cSAndy Whitcroft			$level++;
10958905a67cSAndy Whitcroft			$type = '{';
10968905a67cSAndy Whitcroft		}
10978905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
10988905a67cSAndy Whitcroft			$level--;
10998905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
11008905a67cSAndy Whitcroft
11018905a67cSAndy Whitcroft			if ($level == 0) {
1102b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1103b998e001SPatrick Pannuto					$off++;
1104b998e001SPatrick Pannuto				}
11058905a67cSAndy Whitcroft				last;
11068905a67cSAndy Whitcroft			}
11078905a67cSAndy Whitcroft		}
1108f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1109f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1110f74bd194SAndy Whitcroft			$level--;
1111f74bd194SAndy Whitcroft			$type = '';
1112f74bd194SAndy Whitcroft			$off++;
1113f74bd194SAndy Whitcroft			last;
1114f74bd194SAndy Whitcroft		}
11158905a67cSAndy Whitcroft		$off++;
11168905a67cSAndy Whitcroft	}
1117a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
111813214adfSAndy Whitcroft	if ($off == $len) {
1119a3bb97a7SAndy Whitcroft		$loff = $len + 1;
112013214adfSAndy Whitcroft		$line++;
112113214adfSAndy Whitcroft		$remain--;
112213214adfSAndy Whitcroft	}
11238905a67cSAndy Whitcroft
11248905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
11258905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
11268905a67cSAndy Whitcroft
11278905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
11288905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
11298905a67cSAndy Whitcroft
1130773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
113113214adfSAndy Whitcroft
113213214adfSAndy Whitcroft	return ($statement, $condition,
113313214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
113413214adfSAndy Whitcroft}
113513214adfSAndy Whitcroft
1136cf655043SAndy Whitcroftsub statement_lines {
1137cf655043SAndy Whitcroft	my ($stmt) = @_;
1138cf655043SAndy Whitcroft
1139cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1140cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1141cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1142cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1143cf655043SAndy Whitcroft
1144cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1145cf655043SAndy Whitcroft
1146cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1147cf655043SAndy Whitcroft}
1148cf655043SAndy Whitcroft
1149cf655043SAndy Whitcroftsub statement_rawlines {
1150cf655043SAndy Whitcroft	my ($stmt) = @_;
1151cf655043SAndy Whitcroft
1152cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1153cf655043SAndy Whitcroft
1154cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1155cf655043SAndy Whitcroft}
1156cf655043SAndy Whitcroft
1157cf655043SAndy Whitcroftsub statement_block_size {
1158cf655043SAndy Whitcroft	my ($stmt) = @_;
1159cf655043SAndy Whitcroft
1160cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1161cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1162cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1163cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1164cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1165cf655043SAndy Whitcroft
1166cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1167cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1168cf655043SAndy Whitcroft
1169cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1170cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1171cf655043SAndy Whitcroft
1172cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1173cf655043SAndy Whitcroft		return $stmt_lines;
1174cf655043SAndy Whitcroft	} else {
1175cf655043SAndy Whitcroft		return $stmt_statements;
1176cf655043SAndy Whitcroft	}
1177cf655043SAndy Whitcroft}
1178cf655043SAndy Whitcroft
117913214adfSAndy Whitcroftsub ctx_statement_full {
118013214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
118113214adfSAndy Whitcroft	my ($statement, $condition, $level);
118213214adfSAndy Whitcroft
118313214adfSAndy Whitcroft	my (@chunks);
118413214adfSAndy Whitcroft
1185cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
118613214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
118713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1188773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
118913214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1190cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1191cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1192cf655043SAndy Whitcroft	}
1193cf655043SAndy Whitcroft
1194cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1195cf655043SAndy Whitcroft	# could continue the statement.
1196cf655043SAndy Whitcroft	for (;;) {
119713214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
119813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1199cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1200773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1201cf655043SAndy Whitcroft		#print "C: push\n";
1202cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
120313214adfSAndy Whitcroft	}
120413214adfSAndy Whitcroft
120513214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
12068905a67cSAndy Whitcroft}
12078905a67cSAndy Whitcroft
12084a0df2efSAndy Whitcroftsub ctx_block_get {
1209f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
12104a0df2efSAndy Whitcroft	my $line;
12114a0df2efSAndy Whitcroft	my $start = $linenr - 1;
12124a0df2efSAndy Whitcroft	my $blk = '';
12134a0df2efSAndy Whitcroft	my @o;
12144a0df2efSAndy Whitcroft	my @c;
12154a0df2efSAndy Whitcroft	my @res = ();
12164a0df2efSAndy Whitcroft
1217f0a594c1SAndy Whitcroft	my $level = 0;
12184635f4fbSAndy Whitcroft	my @stack = ($level);
121900df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
122000df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
122100df344fSAndy Whitcroft		$remain--;
122200df344fSAndy Whitcroft
122300df344fSAndy Whitcroft		$blk .= $rawlines[$line];
12244635f4fbSAndy Whitcroft
12254635f4fbSAndy Whitcroft		# Handle nested #if/#else.
122601464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
12274635f4fbSAndy Whitcroft			push(@stack, $level);
122801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
12294635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
123001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
12314635f4fbSAndy Whitcroft			$level = pop(@stack);
12324635f4fbSAndy Whitcroft		}
12334635f4fbSAndy Whitcroft
123401464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1235f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1236f0a594c1SAndy Whitcroft			if ($off > 0) {
1237f0a594c1SAndy Whitcroft				$off--;
1238f0a594c1SAndy Whitcroft				next;
1239f0a594c1SAndy Whitcroft			}
12404a0df2efSAndy Whitcroft
1241f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1242f0a594c1SAndy Whitcroft				$level--;
1243f0a594c1SAndy Whitcroft				last if ($level == 0);
1244f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1245f0a594c1SAndy Whitcroft				$level++;
1246f0a594c1SAndy Whitcroft			}
1247f0a594c1SAndy Whitcroft		}
12484a0df2efSAndy Whitcroft
1249f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
125000df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
12514a0df2efSAndy Whitcroft		}
12524a0df2efSAndy Whitcroft
1253f0a594c1SAndy Whitcroft		last if ($level == 0);
12544a0df2efSAndy Whitcroft	}
12554a0df2efSAndy Whitcroft
1256f0a594c1SAndy Whitcroft	return ($level, @res);
12574a0df2efSAndy Whitcroft}
12584a0df2efSAndy Whitcroftsub ctx_block_outer {
12594a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12604a0df2efSAndy Whitcroft
1261f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1262f0a594c1SAndy Whitcroft	return @r;
12634a0df2efSAndy Whitcroft}
12644a0df2efSAndy Whitcroftsub ctx_block {
12654a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12664a0df2efSAndy Whitcroft
1267f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1268f0a594c1SAndy Whitcroft	return @r;
1269653d4876SAndy Whitcroft}
1270653d4876SAndy Whitcroftsub ctx_statement {
1271f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1272f0a594c1SAndy Whitcroft
1273f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1274f0a594c1SAndy Whitcroft	return @r;
1275f0a594c1SAndy Whitcroft}
1276f0a594c1SAndy Whitcroftsub ctx_block_level {
1277653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1278653d4876SAndy Whitcroft
1279f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
12804a0df2efSAndy Whitcroft}
12819c0ca6f9SAndy Whitcroftsub ctx_statement_level {
12829c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12839c0ca6f9SAndy Whitcroft
12849c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12859c0ca6f9SAndy Whitcroft}
12864a0df2efSAndy Whitcroft
12874a0df2efSAndy Whitcroftsub ctx_locate_comment {
12884a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12894a0df2efSAndy Whitcroft
12904a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1291beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12924a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12934a0df2efSAndy Whitcroft
12944a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
12954a0df2efSAndy Whitcroft	# comment.
12964a0df2efSAndy Whitcroft	my $in_comment = 0;
12974a0df2efSAndy Whitcroft	$current_comment = '';
12984a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
129900df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
130000df344fSAndy Whitcroft		#warn "           $line\n";
13014a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
13024a0df2efSAndy Whitcroft			$in_comment = 1;
13034a0df2efSAndy Whitcroft		}
13044a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
13054a0df2efSAndy Whitcroft			$in_comment = 1;
13064a0df2efSAndy Whitcroft		}
13074a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
13084a0df2efSAndy Whitcroft			$current_comment = '';
13094a0df2efSAndy Whitcroft		}
13104a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
13114a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
13124a0df2efSAndy Whitcroft			$in_comment = 0;
13134a0df2efSAndy Whitcroft		}
13144a0df2efSAndy Whitcroft	}
13154a0df2efSAndy Whitcroft
13164a0df2efSAndy Whitcroft	chomp($current_comment);
13174a0df2efSAndy Whitcroft	return($current_comment);
13184a0df2efSAndy Whitcroft}
13194a0df2efSAndy Whitcroftsub ctx_has_comment {
13204a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13214a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
13224a0df2efSAndy Whitcroft
132300df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
13244a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
13254a0df2efSAndy Whitcroft
13264a0df2efSAndy Whitcroft	return ($cmt ne '');
13274a0df2efSAndy Whitcroft}
13284a0df2efSAndy Whitcroft
13294d001e4dSAndy Whitcroftsub raw_line {
13304d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
13314d001e4dSAndy Whitcroft
13324d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
13334d001e4dSAndy Whitcroft	$cnt++;
13344d001e4dSAndy Whitcroft
13354d001e4dSAndy Whitcroft	my $line;
13364d001e4dSAndy Whitcroft	while ($cnt) {
13374d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
13384d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
13394d001e4dSAndy Whitcroft		$cnt--;
13404d001e4dSAndy Whitcroft	}
13414d001e4dSAndy Whitcroft
13424d001e4dSAndy Whitcroft	return $line;
13434d001e4dSAndy Whitcroft}
13444d001e4dSAndy Whitcroft
13450a920b5bSAndy Whitcroftsub cat_vet {
13460a920b5bSAndy Whitcroft	my ($vet) = @_;
13479c0ca6f9SAndy Whitcroft	my ($res, $coded);
13480a920b5bSAndy Whitcroft
13499c0ca6f9SAndy Whitcroft	$res = '';
13506c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
13516c72ffaaSAndy Whitcroft		$res .= $1;
13526c72ffaaSAndy Whitcroft		if ($2 ne '') {
13539c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
13546c72ffaaSAndy Whitcroft			$res .= $coded;
13556c72ffaaSAndy Whitcroft		}
13569c0ca6f9SAndy Whitcroft	}
13579c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
13580a920b5bSAndy Whitcroft
13599c0ca6f9SAndy Whitcroft	return $res;
13600a920b5bSAndy Whitcroft}
13610a920b5bSAndy Whitcroft
1362c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1363cf655043SAndy Whitcroftmy $av_pending;
1364c2fdda0dSAndy Whitcroftmy @av_paren_type;
13651f65f947SAndy Whitcroftmy $av_pend_colon;
1366c2fdda0dSAndy Whitcroft
1367c2fdda0dSAndy Whitcroftsub annotate_reset {
1368c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1369cf655043SAndy Whitcroft	$av_pending = '_';
1370cf655043SAndy Whitcroft	@av_paren_type = ('E');
13711f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1372c2fdda0dSAndy Whitcroft}
1373c2fdda0dSAndy Whitcroft
13746c72ffaaSAndy Whitcroftsub annotate_values {
13756c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
13766c72ffaaSAndy Whitcroft
13776c72ffaaSAndy Whitcroft	my $res;
13781f65f947SAndy Whitcroft	my $var = '_' x length($stream);
13796c72ffaaSAndy Whitcroft	my $cur = $stream;
13806c72ffaaSAndy Whitcroft
1381c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
13826c72ffaaSAndy Whitcroft
13836c72ffaaSAndy Whitcroft	while (length($cur)) {
1384773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1385cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1386171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13876c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1388c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1389c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1390cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1391c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13926c72ffaaSAndy Whitcroft			}
13936c72ffaaSAndy Whitcroft
1394c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
13959446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
13969446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1397addcdceaSAndy Whitcroft			$type = 'c';
13989446ef56SAndy Whitcroft
1399e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1400c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
14016c72ffaaSAndy Whitcroft			$type = 'T';
14026c72ffaaSAndy Whitcroft
1403389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1404389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1405389a2fe5SAndy Whitcroft			$type = 'T';
1406389a2fe5SAndy Whitcroft
1407c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1408171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1409c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1410171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1411171ae1a4SAndy Whitcroft			if ($2 ne '') {
1412cf655043SAndy Whitcroft				$av_pending = 'N';
1413171ae1a4SAndy Whitcroft			}
1414171ae1a4SAndy Whitcroft			$type = 'E';
1415171ae1a4SAndy Whitcroft
1416c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1417171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1418171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1419171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
14206c72ffaaSAndy Whitcroft
1421c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1422cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1423c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1424cf655043SAndy Whitcroft
1425cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1426cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1427171ae1a4SAndy Whitcroft			$type = 'E';
1428cf655043SAndy Whitcroft
1429c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1430cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1431cf655043SAndy Whitcroft			$av_preprocessor = 1;
1432cf655043SAndy Whitcroft
1433cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1434cf655043SAndy Whitcroft
1435171ae1a4SAndy Whitcroft			$type = 'E';
1436cf655043SAndy Whitcroft
1437c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1438cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1439cf655043SAndy Whitcroft
1440cf655043SAndy Whitcroft			$av_preprocessor = 1;
1441cf655043SAndy Whitcroft
1442cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1443cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1444cf655043SAndy Whitcroft			pop(@av_paren_type);
1445cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1446171ae1a4SAndy Whitcroft			$type = 'E';
14476c72ffaaSAndy Whitcroft
14486c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1449c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
14506c72ffaaSAndy Whitcroft
1451171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1452171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1453171ae1a4SAndy Whitcroft			$av_pending = $type;
1454171ae1a4SAndy Whitcroft			$type = 'N';
1455171ae1a4SAndy Whitcroft
14566c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1457c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
14586c72ffaaSAndy Whitcroft			if (defined $2) {
1459cf655043SAndy Whitcroft				$av_pending = 'V';
14606c72ffaaSAndy Whitcroft			}
14616c72ffaaSAndy Whitcroft			$type = 'N';
14626c72ffaaSAndy Whitcroft
146314b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1464c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
146514b111c1SAndy Whitcroft			$av_pending = 'E';
14666c72ffaaSAndy Whitcroft			$type = 'N';
14676c72ffaaSAndy Whitcroft
14681f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
14691f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
14701f65f947SAndy Whitcroft			$av_pend_colon = 'C';
14711f65f947SAndy Whitcroft			$type = 'N';
14721f65f947SAndy Whitcroft
147314b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1474c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
14756c72ffaaSAndy Whitcroft			$type = 'N';
14766c72ffaaSAndy Whitcroft
14776c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1478c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1479cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1480cf655043SAndy Whitcroft			$av_pending = '_';
14816c72ffaaSAndy Whitcroft			$type = 'N';
14826c72ffaaSAndy Whitcroft
14836c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1484cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1485cf655043SAndy Whitcroft			if ($new_type ne '_') {
1486cf655043SAndy Whitcroft				$type = $new_type;
1487c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1488c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14896c72ffaaSAndy Whitcroft			} else {
1490c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14916c72ffaaSAndy Whitcroft			}
14926c72ffaaSAndy Whitcroft
1493c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1494c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1495c8cb2ca3SAndy Whitcroft			$type = 'V';
1496cf655043SAndy Whitcroft			$av_pending = 'V';
14976c72ffaaSAndy Whitcroft
14988e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
14998e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
15001f65f947SAndy Whitcroft				$av_pend_colon = 'B';
15018e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
15028e761b04SAndy Whitcroft				$av_pend_colon = 'L';
15031f65f947SAndy Whitcroft			}
15041f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
15051f65f947SAndy Whitcroft			$type = 'V';
15061f65f947SAndy Whitcroft
15076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1508c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
15096c72ffaaSAndy Whitcroft			$type = 'V';
15106c72ffaaSAndy Whitcroft
15116c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1512c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
15136c72ffaaSAndy Whitcroft			$type = 'N';
15146c72ffaaSAndy Whitcroft
1515cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1516c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
151713214adfSAndy Whitcroft			$type = 'E';
15181f65f947SAndy Whitcroft			$av_pend_colon = 'O';
151913214adfSAndy Whitcroft
15208e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
15218e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
15228e761b04SAndy Whitcroft			$type = 'C';
15238e761b04SAndy Whitcroft
15241f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
15251f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
15261f65f947SAndy Whitcroft			$type = 'N';
15271f65f947SAndy Whitcroft
15281f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
15291f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
15301f65f947SAndy Whitcroft
15311f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
15321f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
15331f65f947SAndy Whitcroft				$type = 'E';
15341f65f947SAndy Whitcroft			} else {
15351f65f947SAndy Whitcroft				$type = 'N';
15361f65f947SAndy Whitcroft			}
15371f65f947SAndy Whitcroft			$av_pend_colon = 'O';
15381f65f947SAndy Whitcroft
15398e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
154013214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
15416c72ffaaSAndy Whitcroft			$type = 'N';
15426c72ffaaSAndy Whitcroft
15430d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
154474048ed8SAndy Whitcroft			my $variant;
154574048ed8SAndy Whitcroft
154674048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
154774048ed8SAndy Whitcroft			if ($type eq 'V') {
154874048ed8SAndy Whitcroft				$variant = 'B';
154974048ed8SAndy Whitcroft			} else {
155074048ed8SAndy Whitcroft				$variant = 'U';
155174048ed8SAndy Whitcroft			}
155274048ed8SAndy Whitcroft
155374048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
155474048ed8SAndy Whitcroft			$type = 'N';
155574048ed8SAndy Whitcroft
15566c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1557c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
15586c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
15596c72ffaaSAndy Whitcroft				$type = 'N';
15606c72ffaaSAndy Whitcroft			}
15616c72ffaaSAndy Whitcroft
15626c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1563c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
15646c72ffaaSAndy Whitcroft		}
15656c72ffaaSAndy Whitcroft		if (defined $1) {
15666c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
15676c72ffaaSAndy Whitcroft			$res .= $type x length($1);
15686c72ffaaSAndy Whitcroft		}
15696c72ffaaSAndy Whitcroft	}
15706c72ffaaSAndy Whitcroft
15711f65f947SAndy Whitcroft	return ($res, $var);
15726c72ffaaSAndy Whitcroft}
15736c72ffaaSAndy Whitcroft
15748905a67cSAndy Whitcroftsub possible {
157513214adfSAndy Whitcroft	my ($possible, $line) = @_;
15769a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
15770776e594SAndy Whitcroft		^(?:
15780776e594SAndy Whitcroft			$Modifier|
15790776e594SAndy Whitcroft			$Storage|
15800776e594SAndy Whitcroft			$Type|
15819a974fdbSAndy Whitcroft			DEFINE_\S+
15829a974fdbSAndy Whitcroft		)$|
15839a974fdbSAndy Whitcroft		^(?:
15840776e594SAndy Whitcroft			goto|
15850776e594SAndy Whitcroft			return|
15860776e594SAndy Whitcroft			case|
15870776e594SAndy Whitcroft			else|
15880776e594SAndy Whitcroft			asm|__asm__|
158989a88353SAndy Whitcroft			do|
159089a88353SAndy Whitcroft			\#|
159189a88353SAndy Whitcroft			\#\#|
15929a974fdbSAndy Whitcroft		)(?:\s|$)|
15930776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
15949a974fdbSAndy Whitcroft	    )}x;
15959a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
15969a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1597c45dcabdSAndy Whitcroft		# Check for modifiers.
1598c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1599c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1600c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1601c45dcabdSAndy Whitcroft
1602c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1603c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1604d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
16059a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1606d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1607d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1608d2506586SAndy Whitcroft				}
16099a974fdbSAndy Whitcroft			}
1610c45dcabdSAndy Whitcroft
1611c45dcabdSAndy Whitcroft		} else {
161213214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
16138905a67cSAndy Whitcroft			push(@typeList, $possible);
1614c45dcabdSAndy Whitcroft		}
16158905a67cSAndy Whitcroft		build_types();
16160776e594SAndy Whitcroft	} else {
16170776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
16188905a67cSAndy Whitcroft	}
16198905a67cSAndy Whitcroft}
16208905a67cSAndy Whitcroft
16216c72ffaaSAndy Whitcroftmy $prefix = '';
16226c72ffaaSAndy Whitcroft
1623000d1cc1SJoe Perchessub show_type {
1624cbec18afSJoe Perches	my ($type) = @_;
162591bfe484SJoe Perches
1626cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1627cbec18afSJoe Perches
1628cbec18afSJoe Perches	return !defined $ignore_type{$type};
1629000d1cc1SJoe Perches}
1630000d1cc1SJoe Perches
1631f0a594c1SAndy Whitcroftsub report {
1632cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1633cbec18afSJoe Perches
1634cbec18afSJoe Perches	if (!show_type($type) ||
1635cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1636773647a0SAndy Whitcroft		return 0;
1637773647a0SAndy Whitcroft	}
1638000d1cc1SJoe Perches	my $line;
1639000d1cc1SJoe Perches	if ($show_types) {
1640cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1641000d1cc1SJoe Perches	} else {
1642cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1643000d1cc1SJoe Perches	}
16448905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
16458905a67cSAndy Whitcroft
164613214adfSAndy Whitcroft	push(our @report, $line);
1647773647a0SAndy Whitcroft
1648773647a0SAndy Whitcroft	return 1;
1649f0a594c1SAndy Whitcroft}
1650cbec18afSJoe Perches
1651f0a594c1SAndy Whitcroftsub report_dump {
165213214adfSAndy Whitcroft	our @report;
1653f0a594c1SAndy Whitcroft}
1654000d1cc1SJoe Perches
1655d752fcc8SJoe Perchessub fixup_current_range {
1656d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1657d752fcc8SJoe Perches
1658d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1659d752fcc8SJoe Perches		my $o = $1;
1660d752fcc8SJoe Perches		my $l = $2;
1661d752fcc8SJoe Perches		my $no = $o + $offset;
1662d752fcc8SJoe Perches		my $nl = $l + $length;
1663d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1664d752fcc8SJoe Perches	}
1665d752fcc8SJoe Perches}
1666d752fcc8SJoe Perches
1667d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1668d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1669d752fcc8SJoe Perches
1670d752fcc8SJoe Perches	my $range_last_linenr = 0;
1671d752fcc8SJoe Perches	my $delta_offset = 0;
1672d752fcc8SJoe Perches
1673d752fcc8SJoe Perches	my $old_linenr = 0;
1674d752fcc8SJoe Perches	my $new_linenr = 0;
1675d752fcc8SJoe Perches
1676d752fcc8SJoe Perches	my $next_insert = 0;
1677d752fcc8SJoe Perches	my $next_delete = 0;
1678d752fcc8SJoe Perches
1679d752fcc8SJoe Perches	my @lines = ();
1680d752fcc8SJoe Perches
1681d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1682d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1683d752fcc8SJoe Perches
1684d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1685d752fcc8SJoe Perches		my $save_line = 1;
1686d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1687d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1688d752fcc8SJoe Perches			$delta_offset = 0;
1689d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1690d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1691d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1692d752fcc8SJoe Perches		}
1693d752fcc8SJoe Perches
1694d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1695d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1696d752fcc8SJoe Perches			$save_line = 0;
1697d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1698d752fcc8SJoe Perches		}
1699d752fcc8SJoe Perches
1700d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1701d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1702d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1703d752fcc8SJoe Perches			$new_linenr++;
1704d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1705d752fcc8SJoe Perches		}
1706d752fcc8SJoe Perches
1707d752fcc8SJoe Perches		if ($save_line) {
1708d752fcc8SJoe Perches			push(@lines, $line);
1709d752fcc8SJoe Perches			$new_linenr++;
1710d752fcc8SJoe Perches		}
1711d752fcc8SJoe Perches
1712d752fcc8SJoe Perches		$old_linenr++;
1713d752fcc8SJoe Perches	}
1714d752fcc8SJoe Perches
1715d752fcc8SJoe Perches	return @lines;
1716d752fcc8SJoe Perches}
1717d752fcc8SJoe Perches
1718f2d7e4d4SJoe Perchessub fix_insert_line {
1719f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1720f2d7e4d4SJoe Perches
1721f2d7e4d4SJoe Perches	my $inserted = {
1722f2d7e4d4SJoe Perches		LINENR => $linenr,
1723f2d7e4d4SJoe Perches		LINE => $line,
1724f2d7e4d4SJoe Perches	};
1725f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1726f2d7e4d4SJoe Perches}
1727f2d7e4d4SJoe Perches
1728f2d7e4d4SJoe Perchessub fix_delete_line {
1729f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1730f2d7e4d4SJoe Perches
1731f2d7e4d4SJoe Perches	my $deleted = {
1732f2d7e4d4SJoe Perches		LINENR => $linenr,
1733f2d7e4d4SJoe Perches		LINE => $line,
1734f2d7e4d4SJoe Perches	};
1735f2d7e4d4SJoe Perches
1736f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1737f2d7e4d4SJoe Perches}
1738f2d7e4d4SJoe Perches
1739de7d4f0eSAndy Whitcroftsub ERROR {
1740cbec18afSJoe Perches	my ($type, $msg) = @_;
1741cbec18afSJoe Perches
1742cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1743de7d4f0eSAndy Whitcroft		our $clean = 0;
17446c72ffaaSAndy Whitcroft		our $cnt_error++;
17453705ce5bSJoe Perches		return 1;
1746de7d4f0eSAndy Whitcroft	}
17473705ce5bSJoe Perches	return 0;
1748773647a0SAndy Whitcroft}
1749de7d4f0eSAndy Whitcroftsub WARN {
1750cbec18afSJoe Perches	my ($type, $msg) = @_;
1751cbec18afSJoe Perches
1752cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1753de7d4f0eSAndy Whitcroft		our $clean = 0;
17546c72ffaaSAndy Whitcroft		our $cnt_warn++;
17553705ce5bSJoe Perches		return 1;
1756de7d4f0eSAndy Whitcroft	}
17573705ce5bSJoe Perches	return 0;
1758773647a0SAndy Whitcroft}
1759de7d4f0eSAndy Whitcroftsub CHK {
1760cbec18afSJoe Perches	my ($type, $msg) = @_;
1761cbec18afSJoe Perches
1762cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1763de7d4f0eSAndy Whitcroft		our $clean = 0;
17646c72ffaaSAndy Whitcroft		our $cnt_chk++;
17653705ce5bSJoe Perches		return 1;
17666c72ffaaSAndy Whitcroft	}
17673705ce5bSJoe Perches	return 0;
1768de7d4f0eSAndy Whitcroft}
1769de7d4f0eSAndy Whitcroft
17706ecd9674SAndy Whitcroftsub check_absolute_file {
17716ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
17726ecd9674SAndy Whitcroft	my $file = $absolute;
17736ecd9674SAndy Whitcroft
17746ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
17756ecd9674SAndy Whitcroft
17766ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
17776ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
17786ecd9674SAndy Whitcroft		if (-f "$root/$file") {
17796ecd9674SAndy Whitcroft			##print "file<$file>\n";
17806ecd9674SAndy Whitcroft			last;
17816ecd9674SAndy Whitcroft		}
17826ecd9674SAndy Whitcroft	}
17836ecd9674SAndy Whitcroft	if (! -f _)  {
17846ecd9674SAndy Whitcroft		return 0;
17856ecd9674SAndy Whitcroft	}
17866ecd9674SAndy Whitcroft
17876ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17886ecd9674SAndy Whitcroft	my $prefix = $absolute;
17896ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17906ecd9674SAndy Whitcroft
17916ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17926ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1793000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1794000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
17956ecd9674SAndy Whitcroft	}
17966ecd9674SAndy Whitcroft}
17976ecd9674SAndy Whitcroft
17983705ce5bSJoe Perchessub trim {
17993705ce5bSJoe Perches	my ($string) = @_;
18003705ce5bSJoe Perches
1801b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1802b34c648bSJoe Perches
1803b34c648bSJoe Perches	return $string;
1804b34c648bSJoe Perches}
1805b34c648bSJoe Perches
1806b34c648bSJoe Perchessub ltrim {
1807b34c648bSJoe Perches	my ($string) = @_;
1808b34c648bSJoe Perches
1809b34c648bSJoe Perches	$string =~ s/^\s+//;
1810b34c648bSJoe Perches
1811b34c648bSJoe Perches	return $string;
1812b34c648bSJoe Perches}
1813b34c648bSJoe Perches
1814b34c648bSJoe Perchessub rtrim {
1815b34c648bSJoe Perches	my ($string) = @_;
1816b34c648bSJoe Perches
1817b34c648bSJoe Perches	$string =~ s/\s+$//;
18183705ce5bSJoe Perches
18193705ce5bSJoe Perches	return $string;
18203705ce5bSJoe Perches}
18213705ce5bSJoe Perches
182252ea8506SJoe Perchessub string_find_replace {
182352ea8506SJoe Perches	my ($string, $find, $replace) = @_;
182452ea8506SJoe Perches
182552ea8506SJoe Perches	$string =~ s/$find/$replace/g;
182652ea8506SJoe Perches
182752ea8506SJoe Perches	return $string;
182852ea8506SJoe Perches}
182952ea8506SJoe Perches
18303705ce5bSJoe Perchessub tabify {
18313705ce5bSJoe Perches	my ($leading) = @_;
18323705ce5bSJoe Perches
18333705ce5bSJoe Perches	my $source_indent = 8;
18343705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
18353705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
18363705ce5bSJoe Perches
18373705ce5bSJoe Perches	#convert leading spaces to tabs
18383705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
18393705ce5bSJoe Perches	#Remove spaces before a tab
18403705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
18413705ce5bSJoe Perches
18423705ce5bSJoe Perches	return "$leading";
18433705ce5bSJoe Perches}
18443705ce5bSJoe Perches
1845d1fe9c09SJoe Perchessub pos_last_openparen {
1846d1fe9c09SJoe Perches	my ($line) = @_;
1847d1fe9c09SJoe Perches
1848d1fe9c09SJoe Perches	my $pos = 0;
1849d1fe9c09SJoe Perches
1850d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1851d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1852d1fe9c09SJoe Perches
1853d1fe9c09SJoe Perches	my $last_openparen = 0;
1854d1fe9c09SJoe Perches
1855d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1856d1fe9c09SJoe Perches		return -1;
1857d1fe9c09SJoe Perches	}
1858d1fe9c09SJoe Perches
1859d1fe9c09SJoe Perches	my $len = length($line);
1860d1fe9c09SJoe Perches
1861d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1862d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1863d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1864d1fe9c09SJoe Perches			$pos += length($1) - 1;
1865d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1866d1fe9c09SJoe Perches			$last_openparen = $pos;
1867d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1868d1fe9c09SJoe Perches			last;
1869d1fe9c09SJoe Perches		}
1870d1fe9c09SJoe Perches	}
1871d1fe9c09SJoe Perches
187291cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1873d1fe9c09SJoe Perches}
1874d1fe9c09SJoe Perches
18750a920b5bSAndy Whitcroftsub process {
18760a920b5bSAndy Whitcroft	my $filename = shift;
18770a920b5bSAndy Whitcroft
18780a920b5bSAndy Whitcroft	my $linenr=0;
18790a920b5bSAndy Whitcroft	my $prevline="";
1880c2fdda0dSAndy Whitcroft	my $prevrawline="";
18810a920b5bSAndy Whitcroft	my $stashline="";
1882c2fdda0dSAndy Whitcroft	my $stashrawline="";
18830a920b5bSAndy Whitcroft
18844a0df2efSAndy Whitcroft	my $length;
18850a920b5bSAndy Whitcroft	my $indent;
18860a920b5bSAndy Whitcroft	my $previndent=0;
18870a920b5bSAndy Whitcroft	my $stashindent=0;
18880a920b5bSAndy Whitcroft
1889de7d4f0eSAndy Whitcroft	our $clean = 1;
18900a920b5bSAndy Whitcroft	my $signoff = 0;
18910a920b5bSAndy Whitcroft	my $is_patch = 0;
18920a920b5bSAndy Whitcroft
189329ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
189415662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
189513f1937eSJoe Perches	my $reported_maintainer_file = 0;
1896fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1897fa64205dSPasi Savanainen
1898365dd4eaSJoe Perches	my $last_blank_line = 0;
18995e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
1900365dd4eaSJoe Perches
190113214adfSAndy Whitcroft	our @report = ();
19026c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
19036c72ffaaSAndy Whitcroft	our $cnt_error = 0;
19046c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
19056c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
19066c72ffaaSAndy Whitcroft
19070a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
19080a920b5bSAndy Whitcroft	my $realfile = '';
19090a920b5bSAndy Whitcroft	my $realline = 0;
19100a920b5bSAndy Whitcroft	my $realcnt = 0;
19110a920b5bSAndy Whitcroft	my $here = '';
19120a920b5bSAndy Whitcroft	my $in_comment = 0;
1913c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
19140a920b5bSAndy Whitcroft	my $first_line = 0;
19151e855726SWolfram Sang	my $p1_prefix = '';
19160a920b5bSAndy Whitcroft
191713214adfSAndy Whitcroft	my $prev_values = 'E';
191813214adfSAndy Whitcroft
191913214adfSAndy Whitcroft	# suppression flags
1920773647a0SAndy Whitcroft	my %suppress_ifbraces;
1921170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
19222b474a1aSAndy Whitcroft	my %suppress_export;
19233e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1924653d4876SAndy Whitcroft
19257e51f197SJoe Perches	my %signatures = ();
1926323c1260SJoe Perches
1927c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1928de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1929c2fdda0dSAndy Whitcroft	#
1930de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1931de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1932773647a0SAndy Whitcroft
1933d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1934d8b07710SJoe Perches
1935773647a0SAndy Whitcroft	sanitise_line_reset();
1936c2fdda0dSAndy Whitcroft	my $line;
1937c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1938773647a0SAndy Whitcroft		$linenr++;
1939773647a0SAndy Whitcroft		$line = $rawline;
1940c2fdda0dSAndy Whitcroft
19413705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
19423705ce5bSJoe Perches
1943773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1944de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1945de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1946de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1947de7d4f0eSAndy Whitcroft			}
1948773647a0SAndy Whitcroft			#next;
1949de7d4f0eSAndy Whitcroft		}
1950773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1951773647a0SAndy Whitcroft			$realline=$1-1;
1952773647a0SAndy Whitcroft			if (defined $2) {
1953773647a0SAndy Whitcroft				$realcnt=$3+1;
1954773647a0SAndy Whitcroft			} else {
1955773647a0SAndy Whitcroft				$realcnt=1+1;
1956773647a0SAndy Whitcroft			}
1957c45dcabdSAndy Whitcroft			$in_comment = 0;
1958773647a0SAndy Whitcroft
1959773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1960773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1961773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1962773647a0SAndy Whitcroft			# at context start.
1963773647a0SAndy Whitcroft			my $edge;
196401fa9147SAndy Whitcroft			my $cnt = $realcnt;
196501fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
196601fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
196701fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
196801fa9147SAndy Whitcroft				$cnt--;
196901fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1970721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1971fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1972fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1973fae17daeSAndy Whitcroft					($edge) = $1;
1974fae17daeSAndy Whitcroft					last;
1975fae17daeSAndy Whitcroft				}
1976773647a0SAndy Whitcroft			}
1977773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1978773647a0SAndy Whitcroft				$in_comment = 1;
1979773647a0SAndy Whitcroft			}
1980773647a0SAndy Whitcroft
1981773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1982773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1983773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1984773647a0SAndy Whitcroft			if (!defined $edge &&
198583242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1986773647a0SAndy Whitcroft			{
1987773647a0SAndy Whitcroft				$in_comment = 1;
1988773647a0SAndy Whitcroft			}
1989773647a0SAndy Whitcroft
1990773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1991773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1992773647a0SAndy Whitcroft
1993171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1994773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1995171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1996773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1997773647a0SAndy Whitcroft		}
1998773647a0SAndy Whitcroft		push(@lines, $line);
1999773647a0SAndy Whitcroft
2000773647a0SAndy Whitcroft		if ($realcnt > 1) {
2001773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2002773647a0SAndy Whitcroft		} else {
2003773647a0SAndy Whitcroft			$realcnt = 0;
2004773647a0SAndy Whitcroft		}
2005773647a0SAndy Whitcroft
2006773647a0SAndy Whitcroft		#print "==>$rawline\n";
2007773647a0SAndy Whitcroft		#print "-->$line\n";
2008de7d4f0eSAndy Whitcroft
2009de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2010de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2011de7d4f0eSAndy Whitcroft		}
2012de7d4f0eSAndy Whitcroft	}
2013de7d4f0eSAndy Whitcroft
20146c72ffaaSAndy Whitcroft	$prefix = '';
20156c72ffaaSAndy Whitcroft
2016773647a0SAndy Whitcroft	$realcnt = 0;
2017773647a0SAndy Whitcroft	$linenr = 0;
2018194f66fcSJoe Perches	$fixlinenr = -1;
20190a920b5bSAndy Whitcroft	foreach my $line (@lines) {
20200a920b5bSAndy Whitcroft		$linenr++;
2021194f66fcSJoe Perches		$fixlinenr++;
20221b5539b1SJoe Perches		my $sline = $line;	#copy of $line
20231b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
20240a920b5bSAndy Whitcroft
2025c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
20266c72ffaaSAndy Whitcroft
20270a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
20286c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
20290a920b5bSAndy Whitcroft			$is_patch = 1;
20304a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
20310a920b5bSAndy Whitcroft			$realline=$1-1;
20320a920b5bSAndy Whitcroft			if (defined $2) {
20330a920b5bSAndy Whitcroft				$realcnt=$3+1;
20340a920b5bSAndy Whitcroft			} else {
20350a920b5bSAndy Whitcroft				$realcnt=1+1;
20360a920b5bSAndy Whitcroft			}
2037c2fdda0dSAndy Whitcroft			annotate_reset();
203813214adfSAndy Whitcroft			$prev_values = 'E';
203913214adfSAndy Whitcroft
2040773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2041170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
20422b474a1aSAndy Whitcroft			%suppress_export = ();
20433e469cdcSAndy Whitcroft			$suppress_statement = 0;
20440a920b5bSAndy Whitcroft			next;
20450a920b5bSAndy Whitcroft
20464a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
20474a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
20484a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2049773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
20500a920b5bSAndy Whitcroft			$realline++;
2051d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
20520a920b5bSAndy Whitcroft
20534a0df2efSAndy Whitcroft			# Measure the line length and indent.
2054c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
20550a920b5bSAndy Whitcroft
20560a920b5bSAndy Whitcroft			# Track the previous line.
20570a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
20580a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2059c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2060c2fdda0dSAndy Whitcroft
2061773647a0SAndy Whitcroft			#warn "line<$line>\n";
20626c72ffaaSAndy Whitcroft
2063d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2064d8aaf121SAndy Whitcroft			$realcnt--;
20650a920b5bSAndy Whitcroft		}
20660a920b5bSAndy Whitcroft
2067cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2068cc77cdcaSAndy Whitcroft
20690a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
2070773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
2071773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
2072773647a0SAndy Whitcroft
20736c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
20746c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2075773647a0SAndy Whitcroft
20762ac73b4fSJoe Perches		my $found_file = 0;
2077773647a0SAndy Whitcroft		# extract the filename as it passes
20783bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
20793bf9a009SRabin Vincent			$realfile = $1;
20802b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2081270c49a0SJoe Perches			$in_commit_log = 0;
20822ac73b4fSJoe Perches			$found_file = 1;
20833bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2084773647a0SAndy Whitcroft			$realfile = $1;
20852b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2086270c49a0SJoe Perches			$in_commit_log = 0;
20871e855726SWolfram Sang
20881e855726SWolfram Sang			$p1_prefix = $1;
2089e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2090e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2091000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2092000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20931e855726SWolfram Sang			}
2094773647a0SAndy Whitcroft
2095c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2096000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2097000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2098773647a0SAndy Whitcroft			}
20992ac73b4fSJoe Perches			$found_file = 1;
21002ac73b4fSJoe Perches		}
21012ac73b4fSJoe Perches
21022ac73b4fSJoe Perches		if ($found_file) {
21032ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
21042ac73b4fSJoe Perches				$check = 1;
21052ac73b4fSJoe Perches			} else {
21062ac73b4fSJoe Perches				$check = $check_orig;
21072ac73b4fSJoe Perches			}
2108773647a0SAndy Whitcroft			next;
2109773647a0SAndy Whitcroft		}
2110773647a0SAndy Whitcroft
2111389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
21120a920b5bSAndy Whitcroft
2113c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2114c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2115c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
21160a920b5bSAndy Whitcroft
21176c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
21186c72ffaaSAndy Whitcroft
21193bf9a009SRabin Vincent# Check for incorrect file permissions
21203bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
21213bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
212204db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
212304db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2124000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2125000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
21263bf9a009SRabin Vincent			}
21273bf9a009SRabin Vincent		}
21283bf9a009SRabin Vincent
212920112475SJoe Perches# Check the patch for a signoff:
2130d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
21314a0df2efSAndy Whitcroft			$signoff++;
213215662b3eSJoe Perches			$in_commit_log = 0;
21330a920b5bSAndy Whitcroft		}
213420112475SJoe Perches
2135e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2136e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2137e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2138e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2139e0d975b1SJoe Perches		}
2140e0d975b1SJoe Perches
214120112475SJoe Perches# Check signature styles
2142270c49a0SJoe Perches		if (!$in_header_lines &&
2143ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
214420112475SJoe Perches			my $space_before = $1;
214520112475SJoe Perches			my $sign_off = $2;
214620112475SJoe Perches			my $space_after = $3;
214720112475SJoe Perches			my $email = $4;
214820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
214920112475SJoe Perches
2150ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2151ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2152ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2153ce0338dfSJoe Perches			}
215420112475SJoe Perches			if (defined $space_before && $space_before ne "") {
21553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21563705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
21573705ce5bSJoe Perches				    $fix) {
2158194f66fcSJoe Perches					$fixed[$fixlinenr] =
21593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21603705ce5bSJoe Perches				}
216120112475SJoe Perches			}
216220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
21633705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21643705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
21653705ce5bSJoe Perches				    $fix) {
2166194f66fcSJoe Perches					$fixed[$fixlinenr] =
21673705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21683705ce5bSJoe Perches				}
21693705ce5bSJoe Perches
217020112475SJoe Perches			}
217120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
21723705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21733705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
21743705ce5bSJoe Perches				    $fix) {
2175194f66fcSJoe Perches					$fixed[$fixlinenr] =
21763705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21773705ce5bSJoe Perches				}
217820112475SJoe Perches			}
217920112475SJoe Perches
218020112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
218120112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
218220112475SJoe Perches			if ($suggested_email eq "") {
2183000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2184000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
218520112475SJoe Perches			} else {
218620112475SJoe Perches				my $dequoted = $suggested_email;
218720112475SJoe Perches				$dequoted =~ s/^"//;
218820112475SJoe Perches				$dequoted =~ s/" </ </;
218920112475SJoe Perches				# Don't force email to have quotes
219020112475SJoe Perches				# Allow just an angle bracketed address
219120112475SJoe Perches				if ("$dequoted$comment" ne $email &&
219220112475SJoe Perches				    "<$email_address>$comment" ne $email &&
219320112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2194000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2195000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
219620112475SJoe Perches				}
21970a920b5bSAndy Whitcroft			}
21987e51f197SJoe Perches
21997e51f197SJoe Perches# Check for duplicate signatures
22007e51f197SJoe Perches			my $sig_nospace = $line;
22017e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
22027e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
22037e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
22047e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
22057e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
22067e51f197SJoe Perches			} else {
22077e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
22087e51f197SJoe Perches			}
22090a920b5bSAndy Whitcroft		}
22100a920b5bSAndy Whitcroft
2211a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2212a2fe16b9SJoe Perches		if ($in_header_lines &&
2213a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2214a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2215a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2216a2fe16b9SJoe Perches		}
2217a2fe16b9SJoe Perches
22189b3189ebSJoe Perches# Check for old stable address
22199b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
22209b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
22219b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
22229b3189ebSJoe Perches		}
22239b3189ebSJoe Perches
22247ebd05efSChristopher Covington# Check for unwanted Gerrit info
22257ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
22267ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
22277ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
22287ebd05efSChristopher Covington		}
22297ebd05efSChristopher Covington
22300d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
22310d7835fcSJoe Perches		if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
2232d311cd44SJoe Perches			my $init_char = $1;
2233d311cd44SJoe Perches			my $orig_commit = lc($2);
22340d7835fcSJoe Perches			my $short = 1;
22350d7835fcSJoe Perches			my $long = 0;
22360d7835fcSJoe Perches			my $case = 1;
22370d7835fcSJoe Perches			my $space = 1;
22380d7835fcSJoe Perches			my $hasdesc = 0;
223919c146a6SJoe Perches			my $hasparens = 0;
22400d7835fcSJoe Perches			my $id = '0123456789ab';
22410d7835fcSJoe Perches			my $orig_desc = "commit description";
22420d7835fcSJoe Perches			my $description = "";
22430d7835fcSJoe Perches
22440d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
22450d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
22460d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
22470d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
22480d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
22490d7835fcSJoe Perches				$orig_desc = $1;
225019c146a6SJoe Perches				$hasparens = 1;
22510d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
22520d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
22530d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
22540d7835fcSJoe Perches				$orig_desc = $1;
225519c146a6SJoe Perches				$hasparens = 1;
2256b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2257b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2258b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2259b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2260b671fde0SJoe Perches				$orig_desc = $1;
2261b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2262b671fde0SJoe Perches				$orig_desc .= " " . $1;
226319c146a6SJoe Perches				$hasparens = 1;
22640d7835fcSJoe Perches			}
22650d7835fcSJoe Perches
22660d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
22670d7835fcSJoe Perches							      $id, $orig_desc);
22680d7835fcSJoe Perches
226919c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2270d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
22710d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
22720d7835fcSJoe Perches			}
2273d311cd44SJoe Perches		}
2274d311cd44SJoe Perches
227513f1937eSJoe Perches# Check for added, moved or deleted files
227613f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
227713f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
227813f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
227913f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
228013f1937eSJoe Perches		      (defined($1) || defined($2))))) {
228113f1937eSJoe Perches			$reported_maintainer_file = 1;
228213f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
228313f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
228413f1937eSJoe Perches		}
228513f1937eSJoe Perches
228600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
22878905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2288000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2289000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
22906c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2291de7d4f0eSAndy Whitcroft		}
2292de7d4f0eSAndy Whitcroft
22936ecd9674SAndy Whitcroft# Check for absolute kernel paths.
22946ecd9674SAndy Whitcroft		if ($tree) {
22956ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
22966ecd9674SAndy Whitcroft				my $file = $1;
22976ecd9674SAndy Whitcroft
22986ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
22996ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
23006ecd9674SAndy Whitcroft					#
23016ecd9674SAndy Whitcroft				} else {
23026ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
23036ecd9674SAndy Whitcroft				}
23046ecd9674SAndy Whitcroft			}
23056ecd9674SAndy Whitcroft		}
23066ecd9674SAndy Whitcroft
2307de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2308de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2309171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2310171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2311171ae1a4SAndy Whitcroft
2312171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2313171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2314171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2315171ae1a4SAndy Whitcroft
231634d99219SJoe Perches			CHK("INVALID_UTF8",
2317000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
231800df344fSAndy Whitcroft		}
23190a920b5bSAndy Whitcroft
232015662b3eSJoe Perches# Check if it's the start of a commit log
232115662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
232215662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
232329ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
232429ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
232515662b3eSJoe Perches			$in_header_lines = 0;
232615662b3eSJoe Perches			$in_commit_log = 1;
232715662b3eSJoe Perches		}
232815662b3eSJoe Perches
2329fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2330fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2331fa64205dSPasi Savanainen		if ($in_header_lines &&
2332fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2333fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2334fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2335fa64205dSPasi Savanainen		}
2336fa64205dSPasi Savanainen
2337fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
233815662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2339fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
234015662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
234115662b3eSJoe Perches		}
234215662b3eSJoe Perches
234366b47b4aSKees Cook# Check for various typo / spelling mistakes
234466d7a382SJoe Perches		if (defined($misspellings) &&
234566d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2346ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
234766b47b4aSKees Cook				my $typo = $1;
234866b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
234966b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
235066b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
235166b47b4aSKees Cook				my $msg_type = \&WARN;
235266b47b4aSKees Cook				$msg_type = \&CHK if ($file);
235366b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
235466b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
235566b47b4aSKees Cook				    $fix) {
235666b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
235766b47b4aSKees Cook				}
235866b47b4aSKees Cook			}
235966b47b4aSKees Cook		}
236066b47b4aSKees Cook
236130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
236230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
236300df344fSAndy Whitcroft
23640a920b5bSAndy Whitcroft#trailing whitespace
23659c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2366c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2367d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2368d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2369d5e616fcSJoe Perches			    $fix) {
2370194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2371d5e616fcSJoe Perches			}
2372c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2373c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23743705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
23753705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
23763705ce5bSJoe Perches			    $fix) {
2377194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
23783705ce5bSJoe Perches			}
23793705ce5bSJoe Perches
2380d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
23810a920b5bSAndy Whitcroft		}
23825368df20SAndy Whitcroft
23834783f894SJosh Triplett# Check for FSF mailing addresses.
2384109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
23853e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
23863e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
23874783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23884783f894SJosh Triplett			my $msg_type = \&ERROR;
23894783f894SJosh Triplett			$msg_type = \&CHK if ($file);
23904783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
23914783f894SJosh 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)
23924783f894SJosh Triplett		}
23934783f894SJosh Triplett
23943354957aSAndi Kleen# check for Kconfig help text having a real description
23959fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
23969fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
23973354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
23988d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
23993354957aSAndi Kleen			my $length = 0;
24009fe287d7SAndy Whitcroft			my $cnt = $realcnt;
24019fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
24029fe287d7SAndy Whitcroft			my $f;
2403a1385803SAndy Whitcroft			my $is_start = 0;
24049fe287d7SAndy Whitcroft			my $is_end = 0;
2405a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
24069fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
24079fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
24089fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
24099fe287d7SAndy Whitcroft
24109fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
24118d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2412a1385803SAndy Whitcroft
24138d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2414a1385803SAndy Whitcroft					$is_start = 1;
24158d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2416a1385803SAndy Whitcroft					$length = -1;
2417a1385803SAndy Whitcroft				}
2418a1385803SAndy Whitcroft
24199fe287d7SAndy Whitcroft				$f =~ s/^.//;
24203354957aSAndi Kleen				$f =~ s/#.*//;
24213354957aSAndi Kleen				$f =~ s/^\s+//;
24223354957aSAndi Kleen				next if ($f =~ /^$/);
24239fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
24249fe287d7SAndy Whitcroft					$is_end = 1;
24259fe287d7SAndy Whitcroft					last;
24269fe287d7SAndy Whitcroft				}
24273354957aSAndi Kleen				$length++;
24283354957aSAndi Kleen			}
242956193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2430000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
243156193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
243256193274SVadim Bendebury			}
2433a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
24343354957aSAndi Kleen		}
24353354957aSAndi Kleen
24361ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
24371ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
24381ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
24391ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
24401ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
24411ba8dfd1SKees Cook		}
24421ba8dfd1SKees Cook
2443327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2444327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2445327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2446327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2447327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2448327953e9SChristoph Jaeger		}
2449327953e9SChristoph Jaeger
2450c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2451c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2452c68e5878SArnaud Lacombe			my $flag = $1;
2453c68e5878SArnaud Lacombe			my $replacement = {
2454c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2455c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2456c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2457c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2458c68e5878SArnaud Lacombe			};
2459c68e5878SArnaud Lacombe
2460c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2461c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2462c68e5878SArnaud Lacombe		}
2463c68e5878SArnaud Lacombe
2464bff5da43SRob Herring# check for DT compatible documentation
24657dd05b38SFlorian Vaussard		if (defined $root &&
24667dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
24677dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
24687dd05b38SFlorian Vaussard
2469bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2470bff5da43SRob Herring
2471cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2472cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2473cc93319bSFlorian Vaussard
2474bff5da43SRob Herring			foreach my $compat (@compats) {
2475bff5da43SRob Herring				my $compat2 = $compat;
2476185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2477185d566bSRob Herring				my $compat3 = $compat;
2478185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2479185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2480bff5da43SRob Herring				if ( $? >> 8 ) {
2481bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2482bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2483bff5da43SRob Herring				}
2484bff5da43SRob Herring
24854fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
24864fbf32a6SFlorian Vaussard				my $vendor = $1;
2487cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2488bff5da43SRob Herring				if ( $? >> 8 ) {
2489bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2490cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2491bff5da43SRob Herring				}
2492bff5da43SRob Herring			}
2493bff5da43SRob Herring		}
2494bff5da43SRob Herring
24955368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2496de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
24975368df20SAndy Whitcroft
24986cd7f386SJoe Perches#line length limit
2499c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2500f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
25010fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
25028bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
25036cd7f386SJoe Perches		    $length > $max_line_length)
2504c45dcabdSAndy Whitcroft		{
2505000d1cc1SJoe Perches			WARN("LONG_LINE",
25066cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
25070a920b5bSAndy Whitcroft		}
25080a920b5bSAndy Whitcroft
25098905a67cSAndy Whitcroft# check for adding lines without a newline.
25108905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2511000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2512000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
25138905a67cSAndy Whitcroft		}
25148905a67cSAndy Whitcroft
251542e41c54SMike Frysinger# Blackfin: use hi/lo macros
251642e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
251742e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
251842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2519000d1cc1SJoe Perches				ERROR("LO_MACRO",
2520000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
252142e41c54SMike Frysinger			}
252242e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
252342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2524000d1cc1SJoe Perches				ERROR("HI_MACRO",
2525000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
252642e41c54SMike Frysinger			}
252742e41c54SMike Frysinger		}
252842e41c54SMike Frysinger
2529b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2530de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
25310a920b5bSAndy Whitcroft
25320a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
25330a920b5bSAndy Whitcroft# more than 8 must use tabs.
2534c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2535c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2536c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2537d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
25383705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
25393705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
25403705ce5bSJoe Perches			    $fix) {
2541194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
25423705ce5bSJoe Perches			}
25430a920b5bSAndy Whitcroft		}
25440a920b5bSAndy Whitcroft
254508e44365SAlberto Panizzo# check for space before tabs.
254608e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
254708e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25483705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
25493705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
25503705ce5bSJoe Perches			    $fix) {
2551194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2552d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2553194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2554c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
25553705ce5bSJoe Perches			}
255608e44365SAlberto Panizzo		}
255708e44365SAlberto Panizzo
2558d1fe9c09SJoe Perches# check for && or || at the start of a line
2559d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2560d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2561d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2562d1fe9c09SJoe Perches		}
2563d1fe9c09SJoe Perches
2564d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2565d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
256691cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2567d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2568d1fe9c09SJoe Perches			my $oldindent = $1;
2569d1fe9c09SJoe Perches			my $rest = $2;
2570d1fe9c09SJoe Perches
2571d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2572d1fe9c09SJoe Perches			if ($pos >= 0) {
2573b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2574b34a26f3SJoe Perches				my $newindent = $2;
2575d1fe9c09SJoe Perches
2576d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2577d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2578d1fe9c09SJoe Perches					" "  x ($pos % 8);
2579d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2580d1fe9c09SJoe Perches
2581d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2582d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
25833705ce5bSJoe Perches
25843705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
25853705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
25863705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2587194f66fcSJoe Perches						$fixed[$fixlinenr] =~
25883705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
25893705ce5bSJoe Perches					}
2590d1fe9c09SJoe Perches				}
2591d1fe9c09SJoe Perches			}
2592d1fe9c09SJoe Perches		}
2593d1fe9c09SJoe Perches
25946ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
25956ab3a970SJoe Perches# avoid checking a few false positives:
25966ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
25976ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
25986ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
25996ab3a970SJoe Perches#   multiline macros that define functions
26006ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
26016ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
26026ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
26033705ce5bSJoe Perches			if (CHK("SPACING",
2604f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
26053705ce5bSJoe Perches			    $fix) {
2606194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2607f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
26083705ce5bSJoe Perches			}
2609aad4f614SJoe Perches		}
2610aad4f614SJoe Perches
261105880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2612fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
261385ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
261485ad978cSJoe Perches		    $realline > 2) {
261505880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
261605880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
261705880600SJoe Perches		}
261805880600SJoe Perches
261905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2620a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2621a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
262261135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2623a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2624a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2625a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2626a605e32eSJoe Perches		}
2627a605e32eSJoe Perches
2628a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2629c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2630c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2631c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2632c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
263305880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
263405880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
263505880600SJoe Perches		}
263605880600SJoe Perches
26377f619191SJoe Perches# check for missing blank lines after struct/union declarations
26387f619191SJoe Perches# with exceptions for various attributes and macros
26397f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
26407f619191SJoe Perches		    $line =~ /^\+/ &&
26417f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
26427f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
26437f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
26447f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
26457f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
26467f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
26477f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
26487f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2649d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2650d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2651d752fcc8SJoe Perches			    $fix) {
2652f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2653d752fcc8SJoe Perches			}
26547f619191SJoe Perches		}
26557f619191SJoe Perches
2656365dd4eaSJoe Perches# check for multiple consecutive blank lines
2657365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2658365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2659365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2660d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2661d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2662d752fcc8SJoe Perches			    $fix) {
2663f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2664d752fcc8SJoe Perches			}
2665d752fcc8SJoe Perches
2666365dd4eaSJoe Perches			$last_blank_line = $linenr;
2667365dd4eaSJoe Perches		}
2668365dd4eaSJoe Perches
26693b617e3bSJoe Perches# check for missing blank lines after declarations
26703f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
26713f7bac03SJoe Perches			# actual declarations
26723f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26735a4e1fd3SJoe Perches			# function pointer declarations
26745a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26753f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26763f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26773f7bac03SJoe Perches			# known declaration macros
26783f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
26793f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
26803f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
26813f7bac03SJoe Perches			# other possible extensions of declaration lines
26823f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
26833f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
26843f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
26853f7bac03SJoe Perches			# looks like a declaration
26863f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26875a4e1fd3SJoe Perches			# function pointer declarations
26885a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26893f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26903f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26913f7bac03SJoe Perches			# known declaration macros
26923f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
26933f7bac03SJoe Perches			# start of struct or union or enum
26943b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
26953f7bac03SJoe Perches			# start or end of block or continuation of declaration
26963f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
26973f7bac03SJoe Perches			# bitfield continuation
26983f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
26993f7bac03SJoe Perches			# other possible extensions of declaration lines
27003f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
27013f7bac03SJoe Perches			# indentation of previous and current line are the same
27023f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2703d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2704d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2705d752fcc8SJoe Perches			    $fix) {
2706f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2707d752fcc8SJoe Perches			}
27083b617e3bSJoe Perches		}
27093b617e3bSJoe Perches
27105f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
27116b4c5bebSAndy Whitcroft# Exceptions:
27126b4c5bebSAndy Whitcroft#  1) within comments
27136b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
27146b4c5bebSAndy Whitcroft#  3) hanging labels
27153705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
27165f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27173705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
27183705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
27193705ce5bSJoe Perches			    $fix) {
2720194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
27213705ce5bSJoe Perches			}
27225f7ddae6SRaffaele Recalcati		}
27235f7ddae6SRaffaele Recalcati
2724b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2725b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2726b9ea10d6SAndy Whitcroft
2727032a4c0fSJoe Perches# check indentation of any line with a bare else
2728840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2729032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2730032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2731032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2732840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2733840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2734840080a0SJoe Perches			     defined $lines[$linenr] &&
2735840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2736032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2737032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2738032a4c0fSJoe Perches			}
2739032a4c0fSJoe Perches		}
2740032a4c0fSJoe Perches
2741c00df19aSJoe Perches# check indentation of a line with a break;
2742c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2743c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2744c00df19aSJoe Perches			my $tabs = $1;
2745c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2746c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2747c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2748c00df19aSJoe Perches			}
2749c00df19aSJoe Perches		}
2750c00df19aSJoe Perches
27511ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
27521ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
27531ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
27541ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
27551ba8dfd1SKees Cook		}
27561ba8dfd1SKees Cook
2757c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2758cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2759000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2760000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2761c2fdda0dSAndy Whitcroft		}
276222f2a2efSAndy Whitcroft
276342e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
276442e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
276542e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2766000d1cc1SJoe Perches			ERROR("CSYNC",
2767000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
276842e41c54SMike Frysinger		}
276942e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
277042e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2771000d1cc1SJoe Perches			ERROR("SSYNC",
2772000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
277342e41c54SMike Frysinger		}
277442e41c54SMike Frysinger
277556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
277656e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
277756e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
277856e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
277956e77d70SJoe Perches		}
278056e77d70SJoe Perches
27819c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
27822b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
27832b474a1aSAndy Whitcroft		    $realline_next);
27843e469cdcSAndy Whitcroft#print "LINE<$line>\n";
27853e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
27861b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2787170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2788f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2789171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2790171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2791171ae1a4SAndy Whitcroft
27923e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
27933e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
27943e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
27953e469cdcSAndy Whitcroft			# until we hit end of it.
27963e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
27973e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
27983e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
27993e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
28003e469cdcSAndy Whitcroft			}
2801f74bd194SAndy Whitcroft
28022b474a1aSAndy Whitcroft			# Find the real next line.
28032b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
28042b474a1aSAndy Whitcroft			if (defined $realline_next &&
28052b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
28062b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
28072b474a1aSAndy Whitcroft				$realline_next++;
28082b474a1aSAndy Whitcroft			}
28092b474a1aSAndy Whitcroft
2810171ae1a4SAndy Whitcroft			my $s = $stat;
2811171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2812cf655043SAndy Whitcroft
2813c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2814171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2815c2fdda0dSAndy Whitcroft
2816c2fdda0dSAndy Whitcroft			# Ignore functions being called
2817171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2818c2fdda0dSAndy Whitcroft
2819463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2820463f2864SAndy Whitcroft
2821c45dcabdSAndy Whitcroft			# declarations always start with types
2822d2506586SAndy 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) {
2823c45dcabdSAndy Whitcroft				my $type = $1;
2824c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2825c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2826c45dcabdSAndy Whitcroft
28276c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2828a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2829c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2830c2fdda0dSAndy Whitcroft			}
28318905a67cSAndy Whitcroft
28326c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
283365863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2834c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
28359c0ca6f9SAndy Whitcroft			}
28368905a67cSAndy Whitcroft
28378905a67cSAndy Whitcroft			# Check for any sort of function declaration.
28388905a67cSAndy Whitcroft			# int foo(something bar, other baz);
28398905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2840171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
28418905a67cSAndy Whitcroft				my ($name_len) = length($1);
28428905a67cSAndy Whitcroft
2843cf655043SAndy Whitcroft				my $ctx = $s;
2844773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
28458905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2846cf655043SAndy Whitcroft
28478905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2848c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
28498905a67cSAndy Whitcroft
2850c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
28518905a67cSAndy Whitcroft					}
28528905a67cSAndy Whitcroft				}
28538905a67cSAndy Whitcroft			}
28548905a67cSAndy Whitcroft
28559c0ca6f9SAndy Whitcroft		}
28569c0ca6f9SAndy Whitcroft
285700df344fSAndy Whitcroft#
285800df344fSAndy Whitcroft# Checks which may be anchored in the context.
285900df344fSAndy Whitcroft#
286000df344fSAndy Whitcroft
286100df344fSAndy Whitcroft# Check for switch () and associated case and default
286200df344fSAndy Whitcroft# statements should be at the same indent.
286300df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
286400df344fSAndy Whitcroft			my $err = '';
286500df344fSAndy Whitcroft			my $sep = '';
286600df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
286700df344fSAndy Whitcroft			shift(@ctx);
286800df344fSAndy Whitcroft			for my $ctx (@ctx) {
286900df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
287000df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
287100df344fSAndy Whitcroft							$indent != $cindent) {
287200df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
287300df344fSAndy Whitcroft					$sep = '';
287400df344fSAndy Whitcroft				} else {
287500df344fSAndy Whitcroft					$sep = "[...]\n";
287600df344fSAndy Whitcroft				}
287700df344fSAndy Whitcroft			}
287800df344fSAndy Whitcroft			if ($err ne '') {
2879000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2880000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2881de7d4f0eSAndy Whitcroft			}
2882de7d4f0eSAndy Whitcroft		}
2883de7d4f0eSAndy Whitcroft
2884de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2885de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
28860fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2887773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2888773647a0SAndy Whitcroft
28899c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
28908eef05ddSJoe Perches
28918eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
28928eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
28938eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
28948eef05ddSJoe Perches			}
28958eef05ddSJoe Perches
2896de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2897de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2898de7d4f0eSAndy Whitcroft
2899548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2900548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2901de7d4f0eSAndy Whitcroft
2902548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2903548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2904548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2905548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2906548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2907773647a0SAndy Whitcroft				$ctx_ln++;
2908773647a0SAndy Whitcroft			}
2909548596d5SAndy Whitcroft
291053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
291153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2912773647a0SAndy Whitcroft
2913773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2914000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2915000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
291601464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
291700df344fSAndy Whitcroft			}
2918773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2919773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2920773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2921773647a0SAndy Whitcroft			{
29229c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
29239c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2924000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2925000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
292601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
29279c0ca6f9SAndy Whitcroft				}
29289c0ca6f9SAndy Whitcroft			}
292900df344fSAndy Whitcroft		}
293000df344fSAndy Whitcroft
29314d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
29320fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
29333e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
29343e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
29353e469cdcSAndy Whitcroft					if (!defined $stat);
29364d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
29374d001e4dSAndy Whitcroft
29384d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
29394d001e4dSAndy Whitcroft
29404d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
29414d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
29424d001e4dSAndy Whitcroft			# where necessary.
29434d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
29444d001e4dSAndy Whitcroft
29454d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
29466f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
29476f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
29484d001e4dSAndy Whitcroft
29494d001e4dSAndy Whitcroft			# We want to check the first line inside the block
29504d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
29514d001e4dSAndy Whitcroft			#  1) any blank line termination
29524d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
29534d001e4dSAndy Whitcroft			#  3) any do (...) {
29544d001e4dSAndy Whitcroft			my $continuation = 0;
29554d001e4dSAndy Whitcroft			my $check = 0;
29564d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
29574d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
29584d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
29594d001e4dSAndy Whitcroft				$continuation = 1;
29604d001e4dSAndy Whitcroft			}
29619bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
29624d001e4dSAndy Whitcroft				$check = 1;
29634d001e4dSAndy Whitcroft				$cond_lines++;
29644d001e4dSAndy Whitcroft			}
29654d001e4dSAndy Whitcroft
29664d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
29674d001e4dSAndy Whitcroft			# preprocessor statement.
29684d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
29694d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
29704d001e4dSAndy Whitcroft				$check = 0;
29714d001e4dSAndy Whitcroft			}
29724d001e4dSAndy Whitcroft
29739bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2974740504c6SAndy Whitcroft			$continuation = 0;
29759bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
29769bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
29774d001e4dSAndy Whitcroft
2978f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2979f16fa28fSAndy Whitcroft				# is not linear.
2980f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2981f16fa28fSAndy Whitcroft					$check = 0;
2982f16fa28fSAndy Whitcroft				}
2983f16fa28fSAndy Whitcroft
29849bd49efeSAndy Whitcroft				# Ignore:
29859bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
29869bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
29879bd49efeSAndy Whitcroft				#  3) labels.
2988740504c6SAndy Whitcroft				if ($continuation ||
2989740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
29909bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
29919bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2992740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
299330dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
29949bd49efeSAndy Whitcroft						$cond_lines++;
29959bd49efeSAndy Whitcroft					}
29964d001e4dSAndy Whitcroft				}
299730dad6ebSAndy Whitcroft			}
29984d001e4dSAndy Whitcroft
29994d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
30004d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
30014d001e4dSAndy Whitcroft
30024d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
30034d001e4dSAndy Whitcroft			# this is not this patch's fault.
30044d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
30054d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
30064d001e4dSAndy Whitcroft				$check = 0;
30074d001e4dSAndy Whitcroft			}
30084d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
30094d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
30104d001e4dSAndy Whitcroft			}
30114d001e4dSAndy Whitcroft
30129bd49efeSAndy 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";
30134d001e4dSAndy Whitcroft
30144d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
30154d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
3016000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3017000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
30184d001e4dSAndy Whitcroft			}
30194d001e4dSAndy Whitcroft		}
30204d001e4dSAndy Whitcroft
30216c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
30226c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
30231f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
30241f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
30256c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3026c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3027c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3028cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3029cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
30301f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3031c2fdda0dSAndy Whitcroft		}
30326c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
30336c72ffaaSAndy Whitcroft
303400df344fSAndy Whitcroft#ignore lines not being added
30353705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
303600df344fSAndy Whitcroft
3037653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
30387429c690SAndy Whitcroft		if ($dbg_type) {
30397429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3040000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3041000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
30427429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3043000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3044000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
30457429c690SAndy Whitcroft			}
3046653d4876SAndy Whitcroft			next;
3047653d4876SAndy Whitcroft		}
3048a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3049a1ef277eSAndy Whitcroft		if ($dbg_attr) {
30509360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3051000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3052000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
30539360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3054000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3055000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3056a1ef277eSAndy Whitcroft			}
3057a1ef277eSAndy Whitcroft			next;
3058a1ef277eSAndy Whitcroft		}
3059653d4876SAndy Whitcroft
3060f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
306199423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
306299423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3063d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3064d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3065f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3066f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3067f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3068d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3069d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3070f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3071d752fcc8SJoe Perches				$fixedline = $line;
3072d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3073f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3074d752fcc8SJoe Perches			}
3075f0a594c1SAndy Whitcroft		}
3076f0a594c1SAndy Whitcroft
307700df344fSAndy Whitcroft#
307800df344fSAndy Whitcroft# Checks which are anchored on the added line.
307900df344fSAndy Whitcroft#
308000df344fSAndy Whitcroft
3081653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3082c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3083653d4876SAndy Whitcroft			my $path = $1;
3084653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3085000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3086495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3087495e9d84SJoe Perches			}
3088495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3089495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3090495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3091653d4876SAndy Whitcroft			}
3092653d4876SAndy Whitcroft		}
3093653d4876SAndy Whitcroft
309400df344fSAndy Whitcroft# no C99 // comments
309500df344fSAndy Whitcroft		if ($line =~ m{//}) {
30963705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
30973705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
30983705ce5bSJoe Perches			    $fix) {
3099194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
31003705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
31013705ce5bSJoe Perches					my $comment = trim($1);
3102194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
31033705ce5bSJoe Perches				}
31043705ce5bSJoe Perches			}
310500df344fSAndy Whitcroft		}
310600df344fSAndy Whitcroft		# Remove C99 comments.
31070a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
31086c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
31090a920b5bSAndy Whitcroft
31102b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
31112b474a1aSAndy Whitcroft# the whole statement.
31122b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
31132b474a1aSAndy Whitcroft		if (defined $realline_next &&
31142b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
31152b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
31162b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
31172b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
31183cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
31193cbf62dfSAndy Whitcroft			# a prefix:
31203cbf62dfSAndy Whitcroft			#   XXX(foo);
31213cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3122653d4876SAndy Whitcroft			my $name = $1;
312387a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
31243cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
31253cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
31263cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
31273cbf62dfSAndy Whitcroft
31283cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
31292b474a1aSAndy Whitcroft				\n.}\s*$|
313048012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
313148012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
313248012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
31332b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
31342b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
313548012058SAndy Whitcroft			    )/x) {
31362b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
31372b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
31382b474a1aSAndy Whitcroft			} else {
31392b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
31400a920b5bSAndy Whitcroft			}
31410a920b5bSAndy Whitcroft		}
31422b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
31432b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
31442b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
31452b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
31462b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
31472b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
31482b474a1aSAndy Whitcroft		}
31492b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
31502b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3151000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3152000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
31532b474a1aSAndy Whitcroft		}
31540a920b5bSAndy Whitcroft
31555150bda4SJoe Eloff# check for global initialisers.
3156d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3157d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3158000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3159d5e616fcSJoe Perches				      $herecurr) &&
3160d5e616fcSJoe Perches			    $fix) {
3161194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3162d5e616fcSJoe Perches			}
3163f0a594c1SAndy Whitcroft		}
31640a920b5bSAndy Whitcroft# check for static initialisers.
3165d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3166d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3167000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3168d5e616fcSJoe Perches				      $herecurr) &&
3169d5e616fcSJoe Perches			    $fix) {
3170194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3171d5e616fcSJoe Perches			}
31720a920b5bSAndy Whitcroft		}
31730a920b5bSAndy Whitcroft
31741813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
31751813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
31761813087dSJoe Perches			my $tmp = trim($1);
31771813087dSJoe Perches			WARN("MISORDERED_TYPE",
31781813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
31791813087dSJoe Perches		}
31801813087dSJoe Perches
3181cb710ecaSJoe Perches# check for static const char * arrays.
3182cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3183000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3184000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3185cb710ecaSJoe Perches				$herecurr);
3186cb710ecaSJoe Perches               }
3187cb710ecaSJoe Perches
3188cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3189cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3190000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3191000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3192cb710ecaSJoe Perches				$herecurr);
3193cb710ecaSJoe Perches               }
3194cb710ecaSJoe Perches
31959b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
31969b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
31979b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
31989b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
31999b0fa60dSJoe Perches				$herecurr);
32009b0fa60dSJoe Perches               }
32019b0fa60dSJoe Perches
3202b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3203b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3204b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3205b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3206b36190c5SJoe Perches			    $fix) {
3207194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3208b36190c5SJoe Perches			}
3209b36190c5SJoe Perches		}
3210b36190c5SJoe Perches
321192e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
321292e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
321392e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
321492e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
321592e112fdSJoe Perches			    $fix) {
3216194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
321792e112fdSJoe Perches			}
321893ed0e2dSJoe Perches		}
321993ed0e2dSJoe Perches
3220653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3221653d4876SAndy Whitcroft# make sense.
3222653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
32238054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3224c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
32258ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3226021158b4SJoe Perches		    $line !~ /\b$typeOtherOSTypedefs\b/ &&
3227653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3228000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3229000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
32300a920b5bSAndy Whitcroft		}
32310a920b5bSAndy Whitcroft
32320a920b5bSAndy Whitcroft# * goes on variable not on type
323365863862SAndy Whitcroft		# (char*[ const])
3234bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3235bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
32363705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3237d8aaf121SAndy Whitcroft
323865863862SAndy Whitcroft			# Should start with a space.
323965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
324065863862SAndy Whitcroft			# Should not end with a space.
324165863862SAndy Whitcroft			$to =~ s/\s+$//;
324265863862SAndy Whitcroft			# '*'s should not have spaces between.
3243f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
324465863862SAndy Whitcroft			}
3245d8aaf121SAndy Whitcroft
32463705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
324765863862SAndy Whitcroft			if ($from ne $to) {
32483705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
32493705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
32503705ce5bSJoe Perches				    $fix) {
32513705ce5bSJoe Perches					my $sub_from = $ident;
32523705ce5bSJoe Perches					my $sub_to = $ident;
32533705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3254194f66fcSJoe Perches					$fixed[$fixlinenr] =~
32553705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
32563705ce5bSJoe Perches				}
325765863862SAndy Whitcroft			}
3258bfcb2cc7SAndy Whitcroft		}
3259bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3260bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
32613705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3262d8aaf121SAndy Whitcroft
326365863862SAndy Whitcroft			# Should start with a space.
326465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
326565863862SAndy Whitcroft			# Should not end with a space.
326665863862SAndy Whitcroft			$to =~ s/\s+$//;
326765863862SAndy Whitcroft			# '*'s should not have spaces between.
3268f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
326965863862SAndy Whitcroft			}
327065863862SAndy Whitcroft			# Modifiers should have spaces.
327165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
327265863862SAndy Whitcroft
32733705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3274667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
32753705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
32763705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
32773705ce5bSJoe Perches				    $fix) {
32783705ce5bSJoe Perches
32793705ce5bSJoe Perches					my $sub_from = $match;
32803705ce5bSJoe Perches					my $sub_to = $match;
32813705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3282194f66fcSJoe Perches					$fixed[$fixlinenr] =~
32833705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
32843705ce5bSJoe Perches				}
328565863862SAndy Whitcroft			}
32860a920b5bSAndy Whitcroft		}
32870a920b5bSAndy Whitcroft
32880a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
32890a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
32900a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
32910a920b5bSAndy Whitcroft# 			print "$herecurr";
32920a920b5bSAndy Whitcroft# 			$clean = 0;
32930a920b5bSAndy Whitcroft# 		}
32940a920b5bSAndy Whitcroft
32958905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3296000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3297000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
32988905a67cSAndy Whitcroft		}
32998905a67cSAndy Whitcroft
330017441227SJoe Perches# check for uses of printk_ratelimit
330117441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3302000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3303000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
330417441227SJoe Perches		}
330517441227SJoe Perches
330600df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
330700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
330800df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
330925985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
331000df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3311f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
331200df344fSAndy Whitcroft			my $ok = 0;
331300df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
331400df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
331525985edcSLucas De Marchi				# we have a preceding printk if it ends
331600df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
331700df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
331800df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
331900df344fSAndy Whitcroft						$ok = 1;
332000df344fSAndy Whitcroft					}
332100df344fSAndy Whitcroft					last;
332200df344fSAndy Whitcroft				}
332300df344fSAndy Whitcroft			}
332400df344fSAndy Whitcroft			if ($ok == 0) {
3325000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3326000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
33270a920b5bSAndy Whitcroft			}
332800df344fSAndy Whitcroft		}
33290a920b5bSAndy Whitcroft
3330243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3331243f3803SJoe Perches			my $orig = $1;
3332243f3803SJoe Perches			my $level = lc($orig);
3333243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
33348f26b837SJoe Perches			my $level2 = $level;
33358f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3336243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3337daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3338243f3803SJoe Perches		}
3339243f3803SJoe Perches
3340243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3341d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3342d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3343d5e616fcSJoe Perches			    $fix) {
3344194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3345d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3346d5e616fcSJoe Perches			}
3347243f3803SJoe Perches		}
3348243f3803SJoe Perches
3349dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3350dc139313SJoe Perches			my $orig = $1;
3351dc139313SJoe Perches			my $level = lc($orig);
3352dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3353dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3354dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3355dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3356dc139313SJoe Perches		}
3357dc139313SJoe Perches
3358653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3359653d4876SAndy Whitcroft# or if closed on same line
33608d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3361c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
33628d182478SJoe Perches			if (ERROR("OPEN_BRACE",
33638d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
33648d182478SJoe Perches			    $fix) {
33658d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
33668d182478SJoe Perches				my $fixed_line = $rawline;
33678d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
33688d182478SJoe Perches				my $line1 = $1;
33698d182478SJoe Perches				my $line2 = $2;
33708d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
33718d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
33728d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
33738d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
33748d182478SJoe Perches				}
33758d182478SJoe Perches			}
33760a920b5bSAndy Whitcroft		}
3377653d4876SAndy Whitcroft
33788905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
33798905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
33808905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
33818d182478SJoe Perches			if (ERROR("OPEN_BRACE",
33828d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
33838d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
33848d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
33858d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
33868d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
33878d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
33888d182478SJoe Perches				$fixedline = $rawline;
33898d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
33908d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
33918d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
33928d182478SJoe Perches				}
33938d182478SJoe Perches			}
33948905a67cSAndy Whitcroft		}
33958905a67cSAndy Whitcroft
33960c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
33973705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
33983705ce5bSJoe Perches			if (WARN("SPACING",
33993705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
34003705ce5bSJoe Perches			    $fix) {
3401194f66fcSJoe Perches				$fixed[$fixlinenr] =~
34023705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
34033705ce5bSJoe Perches			}
34040c73b4ebSAndy Whitcroft		}
34050c73b4ebSAndy Whitcroft
340631070b5dSJoe Perches# Function pointer declarations
340731070b5dSJoe Perches# check spacing between type, funcptr, and args
340831070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
340991f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
341031070b5dSJoe Perches			my $declare = $1;
341131070b5dSJoe Perches			my $pre_pointer_space = $2;
341231070b5dSJoe Perches			my $post_pointer_space = $3;
341331070b5dSJoe Perches			my $funcname = $4;
341431070b5dSJoe Perches			my $post_funcname_space = $5;
341531070b5dSJoe Perches			my $pre_args_space = $6;
341631070b5dSJoe Perches
341791f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
341891f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
341991f72e9cSJoe Perches# don't need a space so don't warn for those.
342091f72e9cSJoe Perches			my $post_declare_space = "";
342191f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
342291f72e9cSJoe Perches				$post_declare_space = $1;
342391f72e9cSJoe Perches				$declare = rtrim($declare);
342491f72e9cSJoe Perches			}
342591f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
342631070b5dSJoe Perches				WARN("SPACING",
342731070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
342891f72e9cSJoe Perches				$post_declare_space = " ";
342931070b5dSJoe Perches			}
343031070b5dSJoe Perches
343131070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
343291f72e9cSJoe Perches# This test is not currently implemented because these declarations are
343391f72e9cSJoe Perches# equivalent to
343491f72e9cSJoe Perches#	int  foo(int bar, ...)
343591f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
343691f72e9cSJoe Perches#
343791f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
343891f72e9cSJoe Perches#				WARN("SPACING",
343991f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
344091f72e9cSJoe Perches#			}
344131070b5dSJoe Perches
344231070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
344331070b5dSJoe Perches			if (defined $pre_pointer_space &&
344431070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
344531070b5dSJoe Perches				WARN("SPACING",
344631070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
344731070b5dSJoe Perches			}
344831070b5dSJoe Perches
344931070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
345031070b5dSJoe Perches			if (defined $post_pointer_space &&
345131070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
345231070b5dSJoe Perches				WARN("SPACING",
345331070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
345431070b5dSJoe Perches			}
345531070b5dSJoe Perches
345631070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
345731070b5dSJoe Perches			if (defined $post_funcname_space &&
345831070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
345931070b5dSJoe Perches				WARN("SPACING",
346031070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
346131070b5dSJoe Perches			}
346231070b5dSJoe Perches
346331070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
346431070b5dSJoe Perches			if (defined $pre_args_space &&
346531070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
346631070b5dSJoe Perches				WARN("SPACING",
346731070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
346831070b5dSJoe Perches			}
346931070b5dSJoe Perches
347031070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3471194f66fcSJoe Perches				$fixed[$fixlinenr] =~
347291f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
347331070b5dSJoe Perches			}
347431070b5dSJoe Perches		}
347531070b5dSJoe Perches
34768d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
34778d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3478fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3479fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
34808d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
34818d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
34828d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3483fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3484daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
34853705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
34863705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
34873705ce5bSJoe Perches				    $fix) {
3488194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
34893705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
34903705ce5bSJoe Perches				}
34918d31cfceSAndy Whitcroft			}
34928d31cfceSAndy Whitcroft		}
34938d31cfceSAndy Whitcroft
3494f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
34956c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3496c2fdda0dSAndy Whitcroft			my $name = $1;
3497773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3498773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3499c2fdda0dSAndy Whitcroft
3500c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3501773647a0SAndy Whitcroft			if ($name =~ /^(?:
3502773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3503773647a0SAndy Whitcroft				volatile|__volatile__|
3504773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3505773647a0SAndy Whitcroft				asm|__asm__)$/x)
3506773647a0SAndy Whitcroft			{
3507c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3508c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3509c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3510c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3511773647a0SAndy Whitcroft
3512773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3513c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3514c2fdda0dSAndy Whitcroft
3515c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3516c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3517773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3518c2fdda0dSAndy Whitcroft
3519c2fdda0dSAndy Whitcroft			} else {
35203705ce5bSJoe Perches				if (WARN("SPACING",
35213705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
35223705ce5bSJoe Perches					     $fix) {
3523194f66fcSJoe Perches					$fixed[$fixlinenr] =~
35243705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
35253705ce5bSJoe Perches				}
3526f0a594c1SAndy Whitcroft			}
35276c72ffaaSAndy Whitcroft		}
35289a4cad4eSEric Nelson
3529653d4876SAndy Whitcroft# Check operator spacing.
35300a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
35313705ce5bSJoe Perches			my $fixed_line = "";
35323705ce5bSJoe Perches			my $line_fixed = 0;
35333705ce5bSJoe Perches
35349c0ca6f9SAndy Whitcroft			my $ops = qr{
35359c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
35369c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
35379c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
35381f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
353984731623SJoe Perches				\?:|\?|:
35409c0ca6f9SAndy Whitcroft			}x;
3541cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
35423705ce5bSJoe Perches
35433705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
35443705ce5bSJoe Perches##			foreach my $el (@elements) {
35453705ce5bSJoe Perches##				print("el: <$el>\n");
35463705ce5bSJoe Perches##			}
35473705ce5bSJoe Perches
35483705ce5bSJoe Perches			my @fix_elements = ();
354900df344fSAndy Whitcroft			my $off = 0;
35506c72ffaaSAndy Whitcroft
35513705ce5bSJoe Perches			foreach my $el (@elements) {
35523705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
35533705ce5bSJoe Perches				$off += length($el);
35543705ce5bSJoe Perches			}
35553705ce5bSJoe Perches
35563705ce5bSJoe Perches			$off = 0;
35573705ce5bSJoe Perches
35586c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3559b34c648bSJoe Perches			my $last_after = -1;
35606c72ffaaSAndy Whitcroft
35610a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
35623705ce5bSJoe Perches
35633705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
35643705ce5bSJoe Perches
35653705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
35663705ce5bSJoe Perches
35674a0df2efSAndy Whitcroft				$off += length($elements[$n]);
35684a0df2efSAndy Whitcroft
356925985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3570773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3571773647a0SAndy Whitcroft				my $cc = '';
3572773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3573773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3574773647a0SAndy Whitcroft				}
3575773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3576773647a0SAndy Whitcroft
35774a0df2efSAndy Whitcroft				my $a = '';
35784a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
35794a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3580cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
35814a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
35824a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3583773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
35844a0df2efSAndy Whitcroft
35850a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
35864a0df2efSAndy Whitcroft
35874a0df2efSAndy Whitcroft				my $c = '';
35880a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
35894a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
35904a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3591cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
35924a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
35934a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
35948b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
35954a0df2efSAndy Whitcroft				} else {
35964a0df2efSAndy Whitcroft					$c = 'E';
35970a920b5bSAndy Whitcroft				}
35980a920b5bSAndy Whitcroft
35994a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
36004a0df2efSAndy Whitcroft
36014a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
36024a0df2efSAndy Whitcroft
36036c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3604de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
36050a920b5bSAndy Whitcroft
360674048ed8SAndy Whitcroft				# Pull out the value of this operator.
36076c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
36080a920b5bSAndy Whitcroft
36091f65f947SAndy Whitcroft				# Get the full operator variant.
36101f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
36111f65f947SAndy Whitcroft
361213214adfSAndy Whitcroft				# Ignore operators passed as parameters.
361313214adfSAndy Whitcroft				if ($op_type ne 'V' &&
361413214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
361513214adfSAndy Whitcroft
3616cf655043SAndy Whitcroft#				# Ignore comments
3617cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
361813214adfSAndy Whitcroft
3619d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
362013214adfSAndy Whitcroft				} elsif ($op eq ';') {
3621cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3622cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
36233705ce5bSJoe Perches						if (ERROR("SPACING",
36243705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3625b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
36263705ce5bSJoe Perches							$line_fixed = 1;
36273705ce5bSJoe Perches						}
3628d8aaf121SAndy Whitcroft					}
3629d8aaf121SAndy Whitcroft
3630d8aaf121SAndy Whitcroft				# // is a comment
3631d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
36320a920b5bSAndy Whitcroft
3633b00e4814SJoe Perches				#   :   when part of a bitfield
3634b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3635b00e4814SJoe Perches					# skip the bitfield test for now
3636b00e4814SJoe Perches
36371f65f947SAndy Whitcroft				# No spaces for:
36381f65f947SAndy Whitcroft				#   ->
3639b00e4814SJoe Perches				} elsif ($op eq '->') {
36404a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
36413705ce5bSJoe Perches						if (ERROR("SPACING",
36423705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3643b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36443705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36453705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
36463705ce5bSJoe Perches							}
3647b34c648bSJoe Perches							$line_fixed = 1;
36483705ce5bSJoe Perches						}
36490a920b5bSAndy Whitcroft					}
36500a920b5bSAndy Whitcroft
36512381097bSJoe Perches				# , must not have a space before and must have a space on the right.
36520a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
36532381097bSJoe Perches					my $rtrim_before = 0;
36542381097bSJoe Perches					my $space_after = 0;
36552381097bSJoe Perches					if ($ctx =~ /Wx./) {
36562381097bSJoe Perches						if (ERROR("SPACING",
36572381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
36582381097bSJoe Perches							$line_fixed = 1;
36592381097bSJoe Perches							$rtrim_before = 1;
36602381097bSJoe Perches						}
36612381097bSJoe Perches					}
3662cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
36633705ce5bSJoe Perches						if (ERROR("SPACING",
36643705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
36653705ce5bSJoe Perches							$line_fixed = 1;
3666b34c648bSJoe Perches							$last_after = $n;
36672381097bSJoe Perches							$space_after = 1;
36682381097bSJoe Perches						}
36692381097bSJoe Perches					}
36702381097bSJoe Perches					if ($rtrim_before || $space_after) {
36712381097bSJoe Perches						if ($rtrim_before) {
36722381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36732381097bSJoe Perches						} else {
36742381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
36752381097bSJoe Perches						}
36762381097bSJoe Perches						if ($space_after) {
36772381097bSJoe Perches							$good .= " ";
36783705ce5bSJoe Perches						}
36790a920b5bSAndy Whitcroft					}
36800a920b5bSAndy Whitcroft
36819c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
368274048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
36839c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
36849c0ca6f9SAndy Whitcroft
36859c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
36869c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
36879c0ca6f9SAndy Whitcroft				# unary operator, or a cast
36889c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
368974048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
36900d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3691cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
36923705ce5bSJoe Perches						if (ERROR("SPACING",
36933705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3694b34c648bSJoe Perches							if ($n != $last_after + 2) {
3695b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
36963705ce5bSJoe Perches								$line_fixed = 1;
36973705ce5bSJoe Perches							}
36980a920b5bSAndy Whitcroft						}
3699b34c648bSJoe Perches					}
3700a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3701171ae1a4SAndy Whitcroft						# A unary '*' may be const
3702171ae1a4SAndy Whitcroft
3703171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
37043705ce5bSJoe Perches						if (ERROR("SPACING",
37053705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3706b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
37073705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
37083705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
37093705ce5bSJoe Perches							}
3710b34c648bSJoe Perches							$line_fixed = 1;
37113705ce5bSJoe Perches						}
37120a920b5bSAndy Whitcroft					}
37130a920b5bSAndy Whitcroft
37140a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
37150a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3716773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
37173705ce5bSJoe Perches						if (ERROR("SPACING",
37183705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3719b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
37203705ce5bSJoe Perches							$line_fixed = 1;
37213705ce5bSJoe Perches						}
37220a920b5bSAndy Whitcroft					}
3723773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3724773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
37253705ce5bSJoe Perches						if (ERROR("SPACING",
37263705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3727b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
37283705ce5bSJoe Perches							$line_fixed = 1;
37293705ce5bSJoe Perches						}
3730653d4876SAndy Whitcroft					}
3731773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
37323705ce5bSJoe Perches						if (ERROR("SPACING",
37333705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3734b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
37353705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
37363705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3737773647a0SAndy Whitcroft							}
3738b34c648bSJoe Perches							$line_fixed = 1;
37393705ce5bSJoe Perches						}
37403705ce5bSJoe Perches					}
37410a920b5bSAndy Whitcroft
37420a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
37439c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
37449c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
37459c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3746c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3747c2fdda0dSAndy Whitcroft					 $op eq '%')
37480a920b5bSAndy Whitcroft				{
3749d2e025f3SJoe Perches					if ($check) {
3750d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3751d2e025f3SJoe Perches							if (CHK("SPACING",
3752d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
3753d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3754d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3755d2e025f3SJoe Perches								$line_fixed = 1;
3756d2e025f3SJoe Perches							}
3757d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3758d2e025f3SJoe Perches							if (CHK("SPACING",
3759d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
3760d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3761d2e025f3SJoe Perches								$line_fixed = 1;
3762d2e025f3SJoe Perches							}
3763d2e025f3SJoe Perches						}
3764d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
37653705ce5bSJoe Perches						if (ERROR("SPACING",
37663705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3767b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3768b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3769b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3770b34c648bSJoe Perches							}
37713705ce5bSJoe Perches							$line_fixed = 1;
37723705ce5bSJoe Perches						}
37730a920b5bSAndy Whitcroft					}
37740a920b5bSAndy Whitcroft
37751f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
37761f65f947SAndy Whitcroft				# terminating a case value or a label.
37771f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
37781f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
37793705ce5bSJoe Perches						if (ERROR("SPACING",
37803705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3781b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
37823705ce5bSJoe Perches							$line_fixed = 1;
37833705ce5bSJoe Perches						}
37841f65f947SAndy Whitcroft					}
37851f65f947SAndy Whitcroft
37860a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3787cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
37881f65f947SAndy Whitcroft					my $ok = 0;
37891f65f947SAndy Whitcroft
379022f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
37911f65f947SAndy Whitcroft					if (($op eq '<' &&
37921f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
37931f65f947SAndy Whitcroft					    ($op eq '>' &&
37941f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
37951f65f947SAndy Whitcroft					{
37961f65f947SAndy Whitcroft					    	$ok = 1;
37971f65f947SAndy Whitcroft					}
37981f65f947SAndy Whitcroft
379984731623SJoe Perches					# messages are ERROR, but ?: are CHK
38001f65f947SAndy Whitcroft					if ($ok == 0) {
380184731623SJoe Perches						my $msg_type = \&ERROR;
380284731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
380384731623SJoe Perches
380484731623SJoe Perches						if (&{$msg_type}("SPACING",
38053705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3806b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3807b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3808b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3809b34c648bSJoe Perches							}
38103705ce5bSJoe Perches							$line_fixed = 1;
38113705ce5bSJoe Perches						}
38120a920b5bSAndy Whitcroft					}
381322f2a2efSAndy Whitcroft				}
38144a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
38153705ce5bSJoe Perches
38163705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
38173705ce5bSJoe Perches
38183705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
38190a920b5bSAndy Whitcroft			}
38203705ce5bSJoe Perches
38213705ce5bSJoe Perches			if (($#elements % 2) == 0) {
38223705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
38233705ce5bSJoe Perches			}
38243705ce5bSJoe Perches
3825194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3826194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
38273705ce5bSJoe Perches			}
38283705ce5bSJoe Perches
38293705ce5bSJoe Perches
38300a920b5bSAndy Whitcroft		}
38310a920b5bSAndy Whitcroft
3832786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3833d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3834786b6326SJoe Perches			if (WARN("SPACING",
3835786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3836786b6326SJoe Perches			    $fix) {
3837194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3838786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3839786b6326SJoe Perches			}
3840786b6326SJoe Perches		}
3841786b6326SJoe Perches
3842f0a594c1SAndy Whitcroft# check for multiple assignments
3843f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3844000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3845000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3846f0a594c1SAndy Whitcroft		}
3847f0a594c1SAndy Whitcroft
384822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
384922f2a2efSAndy Whitcroft## # continuation.
385022f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
385122f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
385222f2a2efSAndy Whitcroft##
385322f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
385422f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
385522f2a2efSAndy Whitcroft## 			my $ln = $line;
385622f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
385722f2a2efSAndy Whitcroft## 			}
385822f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3859000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3860000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
386122f2a2efSAndy Whitcroft## 			}
386222f2a2efSAndy Whitcroft## 		}
3863f0a594c1SAndy Whitcroft
38640a920b5bSAndy Whitcroft#need space before brace following if, while, etc
386522f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
386622f2a2efSAndy Whitcroft		    $line =~ /do{/) {
38673705ce5bSJoe Perches			if (ERROR("SPACING",
38683705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
38693705ce5bSJoe Perches			    $fix) {
3870194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
38713705ce5bSJoe Perches			}
3872de7d4f0eSAndy Whitcroft		}
3873de7d4f0eSAndy Whitcroft
3874c4a62ef9SJoe Perches## # check for blank lines before declarations
3875c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3876c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3877c4a62ef9SJoe Perches##			WARN("SPACING",
3878c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3879c4a62ef9SJoe Perches##		}
3880c4a62ef9SJoe Perches##
3881c4a62ef9SJoe Perches
3882de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3883de7d4f0eSAndy Whitcroft# on the line
3884de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3885d5e616fcSJoe Perches			if (ERROR("SPACING",
3886d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3887d5e616fcSJoe Perches			    $fix) {
3888194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3889d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3890d5e616fcSJoe Perches			}
38910a920b5bSAndy Whitcroft		}
38920a920b5bSAndy Whitcroft
389322f2a2efSAndy Whitcroft# check spacing on square brackets
389422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
38953705ce5bSJoe Perches			if (ERROR("SPACING",
38963705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
38973705ce5bSJoe Perches			    $fix) {
3898194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38993705ce5bSJoe Perches				    s/\[\s+/\[/;
39003705ce5bSJoe Perches			}
390122f2a2efSAndy Whitcroft		}
390222f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
39033705ce5bSJoe Perches			if (ERROR("SPACING",
39043705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
39053705ce5bSJoe Perches			    $fix) {
3906194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39073705ce5bSJoe Perches				    s/\s+\]/\]/;
39083705ce5bSJoe Perches			}
390922f2a2efSAndy Whitcroft		}
391022f2a2efSAndy Whitcroft
3911c45dcabdSAndy Whitcroft# check spacing on parentheses
39129c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
39139c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
39143705ce5bSJoe Perches			if (ERROR("SPACING",
39153705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
39163705ce5bSJoe Perches			    $fix) {
3917194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39183705ce5bSJoe Perches				    s/\(\s+/\(/;
39193705ce5bSJoe Perches			}
392022f2a2efSAndy Whitcroft		}
392113214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3922c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3923c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
39243705ce5bSJoe Perches			if (ERROR("SPACING",
39253705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
39263705ce5bSJoe Perches			    $fix) {
3927194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39283705ce5bSJoe Perches				    s/\s+\)/\)/;
39293705ce5bSJoe Perches			}
393022f2a2efSAndy Whitcroft		}
393122f2a2efSAndy Whitcroft
3932e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3933e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3934e2826fd0SJoe Perches
3935e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3936ea4acbb1SJoe Perches			my $var = $1;
3937ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3938ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
3939ea4acbb1SJoe Perches			    $fix) {
3940ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3941ea4acbb1SJoe Perches			}
3942ea4acbb1SJoe Perches		}
3943ea4acbb1SJoe Perches
3944ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
3945ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
3946ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
3947ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3948ea4acbb1SJoe Perches			my $var = $2;
3949ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3950ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3951ea4acbb1SJoe Perches			    $fix) {
3952ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
3953ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
3954ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3955ea4acbb1SJoe Perches			}
3956e2826fd0SJoe Perches		}
3957e2826fd0SJoe Perches
39580a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
39594a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
39600a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
39613705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
39623705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
39633705ce5bSJoe Perches			    $fix) {
3964194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39653705ce5bSJoe Perches				    s/^(.)\s+/$1/;
39663705ce5bSJoe Perches			}
39670a920b5bSAndy Whitcroft		}
39680a920b5bSAndy Whitcroft
39695b9553abSJoe Perches# return is not a function
3970507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3971c45dcabdSAndy Whitcroft			my $spacing = $1;
3972507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
39735b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
39745b9553abSJoe Perches				my $value = $1;
39755b9553abSJoe Perches				$value = deparenthesize($value);
39765b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3977000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3978000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
39795b9553abSJoe Perches				}
3980c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3981000d1cc1SJoe Perches				ERROR("SPACING",
3982000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3983c45dcabdSAndy Whitcroft			}
3984c45dcabdSAndy Whitcroft		}
3985507e5141SJoe Perches
3986b43ae21bSJoe Perches# unnecessary return in a void function
3987b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3988b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3989b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3990b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3991b43ae21bSJoe Perches		    $linenr >= 3 &&
3992b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3993b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
39949819cf25SJoe Perches			WARN("RETURN_VOID",
3995b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
39969819cf25SJoe Perches               }
39979819cf25SJoe Perches
3998189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3999189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4000189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4001189248d8SJoe Perches			my $openparens = $1;
4002189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4003189248d8SJoe Perches			my $msg = "";
4004189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4005189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4006189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4007189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4008189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4009189248d8SJoe Perches			}
4010189248d8SJoe Perches		}
4011189248d8SJoe Perches
4012*f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4013*f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
401453a3c448SAndy Whitcroft			my $name = $1;
401553a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4016000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4017*f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
401853a3c448SAndy Whitcroft			}
401953a3c448SAndy Whitcroft		}
4020c45dcabdSAndy Whitcroft
40210a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
40224a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
40233705ce5bSJoe Perches			if (ERROR("SPACING",
40243705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
40253705ce5bSJoe Perches			    $fix) {
4026194f66fcSJoe Perches				$fixed[$fixlinenr] =~
40273705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
40283705ce5bSJoe Perches			}
40290a920b5bSAndy Whitcroft		}
40300a920b5bSAndy Whitcroft
4031f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4032f5fe35ddSAndy Whitcroft# statements after the conditional.
4033170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
40343e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
40353e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
40363e469cdcSAndy Whitcroft					if (!defined $stat);
4037170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4038170d3a22SAndy Whitcroft						$remain_next, $off_next);
4039170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4040170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4041170d3a22SAndy Whitcroft
4042170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4043170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4044170d3a22SAndy Whitcroft				# then count those as offsets.
4045170d3a22SAndy Whitcroft				my ($whitespace) =
4046170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4047170d3a22SAndy Whitcroft				my $offset =
4048170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4049170d3a22SAndy Whitcroft
4050170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4051170d3a22SAndy Whitcroft								$offset} = 1;
4052170d3a22SAndy Whitcroft			}
4053170d3a22SAndy Whitcroft		}
4054170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4055c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4056170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4057171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
40588905a67cSAndy Whitcroft
4059b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4060000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4061000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
40628905a67cSAndy Whitcroft			}
40638905a67cSAndy Whitcroft
40648905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
40658905a67cSAndy Whitcroft			# conditional.
4066773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
40678905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
406813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
406953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
407053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4071773647a0SAndy Whitcroft			{
4072bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4073bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4074bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
407542bdf74cSHidetoshi Seto				my $stat_real = '';
4076bb44ad39SAndy Whitcroft
407742bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
407842bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4079bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4080bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4081bb44ad39SAndy Whitcroft				}
4082bb44ad39SAndy Whitcroft
4083000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4084000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
40858905a67cSAndy Whitcroft			}
40868905a67cSAndy Whitcroft		}
40878905a67cSAndy Whitcroft
408813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
408913214adfSAndy Whitcroft		if ($line =~ /
409013214adfSAndy Whitcroft			(?:
409113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
409213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
409313214adfSAndy Whitcroft				(?:\&\&|\|\|)
409413214adfSAndy Whitcroft			|
409513214adfSAndy Whitcroft				(?:\&\&|\|\|)
409613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
409713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
409813214adfSAndy Whitcroft			)/x)
409913214adfSAndy Whitcroft		{
4100000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4101000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
410213214adfSAndy Whitcroft		}
410313214adfSAndy Whitcroft
41048905a67cSAndy Whitcroft# if and else should not have general statements after it
410513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
410613214adfSAndy Whitcroft			my $s = $1;
410713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
410813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4109000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4110000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
41110a920b5bSAndy Whitcroft			}
411213214adfSAndy Whitcroft		}
411339667782SAndy Whitcroft# if should not continue a brace
411439667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4115000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4116048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
411739667782SAndy Whitcroft				$herecurr);
411839667782SAndy Whitcroft		}
4119a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4120a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4121a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
41223fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4123a1080bf8SAndy Whitcroft			\s*return\s+
4124a1080bf8SAndy Whitcroft		    )/xg)
4125a1080bf8SAndy Whitcroft		{
4126000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4127000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4128a1080bf8SAndy Whitcroft		}
41290a920b5bSAndy Whitcroft
41300a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
41310a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
41328b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
41330a920b5bSAndy Whitcroft		    $previndent == $indent) {
41348b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
41358b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
41368b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41378b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
41388b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41398b8856f4SJoe Perches				my $fixedline = $prevrawline;
41408b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
41418b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
41428b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41438b8856f4SJoe Perches				}
41448b8856f4SJoe Perches				$fixedline = $rawline;
41458b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
41468b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
41478b8856f4SJoe Perches			}
41480a920b5bSAndy Whitcroft		}
41490a920b5bSAndy Whitcroft
41508b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4151c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4152c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4153c2fdda0dSAndy Whitcroft
4154c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4155c2fdda0dSAndy Whitcroft			# conditional.
4156773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4157c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4158c2fdda0dSAndy Whitcroft
4159c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
41608b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
41618b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
41628b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41638b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
41648b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
41658b8856f4SJoe Perches					my $fixedline = $prevrawline;
41668b8856f4SJoe Perches					my $trailing = $rawline;
41678b8856f4SJoe Perches					$trailing =~ s/^\+//;
41688b8856f4SJoe Perches					$trailing = trim($trailing);
41698b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
41708b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41718b8856f4SJoe Perches				}
4172c2fdda0dSAndy Whitcroft			}
4173c2fdda0dSAndy Whitcroft		}
4174c2fdda0dSAndy Whitcroft
417595e2c602SJoe Perches#Specific variable tests
4176323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4177323c1260SJoe Perches			my $var = $1;
417895e2c602SJoe Perches
417995e2c602SJoe Perches#gcc binary extension
418095e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4181d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4182d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4183d5e616fcSJoe Perches				    $fix) {
4184d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4185194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4186d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4187d5e616fcSJoe Perches				}
418895e2c602SJoe Perches			}
418995e2c602SJoe Perches
419095e2c602SJoe Perches#CamelCase
4191807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4192be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
419322735ce8SJoe Perches#Ignore Page<foo> variants
4194807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
419522735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4196f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4197f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4198f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
41997e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
42007e781f67SJoe Perches					my $word = $1;
42017e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4202d8b07710SJoe Perches					if ($check) {
4203d8b07710SJoe Perches						seed_camelcase_includes();
4204d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4205d8b07710SJoe Perches							seed_camelcase_file($realfile);
4206d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4207d8b07710SJoe Perches						}
4208d8b07710SJoe Perches					}
42097e781f67SJoe Perches					if (!defined $camelcase{$word}) {
42107e781f67SJoe Perches						$camelcase{$word} = 1;
4211be79794bSJoe Perches						CHK("CAMELCASE",
42127e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
42137e781f67SJoe Perches					}
4214323c1260SJoe Perches				}
4215323c1260SJoe Perches			}
42163445686aSJoe Perches		}
42170a920b5bSAndy Whitcroft
42180a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4219d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4220d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4221d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4222d5e616fcSJoe Perches			    $fix) {
4223194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4224d5e616fcSJoe Perches			}
42250a920b5bSAndy Whitcroft		}
42260a920b5bSAndy Whitcroft
4227653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4228c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4229e09dec48SAndy Whitcroft			my $file = "$1.h";
4230e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4231e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4232e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
42337840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4234c45dcabdSAndy Whitcroft			{
4235e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4236000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4237000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4238e09dec48SAndy Whitcroft				} else {
4239000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4240000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4241e09dec48SAndy Whitcroft				}
42420a920b5bSAndy Whitcroft			}
42430a920b5bSAndy Whitcroft		}
42440a920b5bSAndy Whitcroft
4245653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4246653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4247cf655043SAndy Whitcroft# in a known good container
4248b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4249b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4250d8aaf121SAndy Whitcroft			my $ln = $linenr;
4251d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4252c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4253c45dcabdSAndy Whitcroft			my $ctx = '';
425408a2843eSJoe Perches			my $has_flow_statement = 0;
425508a2843eSJoe Perches			my $has_arg_concat = 0;
4256c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4257f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4258f74bd194SAndy Whitcroft			$ctx = $dstat;
4259c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4260a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4261c45dcabdSAndy Whitcroft
426208a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
426308a2843eSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/);
426408a2843eSJoe Perches
4265f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4266292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4267c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4268c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4269c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4270c45dcabdSAndy Whitcroft
4271c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4272bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4273bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4274c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4275bf30d6edSAndy Whitcroft			{
4276c45dcabdSAndy Whitcroft			}
4277c45dcabdSAndy Whitcroft
4278e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4279e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4280e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4281e45bab8eSAndy Whitcroft			{
4282e45bab8eSAndy Whitcroft			}
4283e45bab8eSAndy Whitcroft
4284c45dcabdSAndy Whitcroft			my $exceptions = qr{
4285c45dcabdSAndy Whitcroft				$Declare|
4286c45dcabdSAndy Whitcroft				module_param_named|
4287a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4288c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4289c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4290383099fdSAndy Whitcroft				__typeof__\(|
429122fd2d3eSStefani Seibold				union|
429222fd2d3eSStefani Seibold				struct|
4293ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4294ea71a0a0SAndy Whitcroft				^\"|\"$
4295c45dcabdSAndy Whitcroft			}x;
42965eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4297f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4298f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4299f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
43003cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4301356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4302f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4303f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4304e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
430572f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4306f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4307f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4308f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4309f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4310f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4311c45dcabdSAndy Whitcroft			{
4312f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4313f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4314f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4315f74bd194SAndy Whitcroft
4316f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4317f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4318c45dcabdSAndy Whitcroft				}
4319c45dcabdSAndy Whitcroft
4320f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4321f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4322f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4323f74bd194SAndy Whitcroft				} else {
4324000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4325388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4326d8aaf121SAndy Whitcroft				}
43270a920b5bSAndy Whitcroft			}
43285023d347SJoe Perches
432908a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
433008a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
433108a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
433208a2843eSJoe Perches				my $herectx = $here . "\n";
433308a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
433408a2843eSJoe Perches
433508a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
433608a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
433708a2843eSJoe Perches				}
433808a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
433908a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
434008a2843eSJoe Perches			}
434108a2843eSJoe Perches
4342481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
43435023d347SJoe Perches
43445023d347SJoe Perches		} else {
43455023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4346481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4347481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
43485023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
43495023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
43505023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
43515023d347SJoe Perches			}
4352653d4876SAndy Whitcroft		}
43530a920b5bSAndy Whitcroft
4354b13edf7fSJoe Perches# do {} while (0) macro tests:
4355b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4356b13edf7fSJoe Perches# macro should not end with a semicolon
4357b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4358b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4359b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4360b13edf7fSJoe Perches			my $ln = $linenr;
4361b13edf7fSJoe Perches			my $cnt = $realcnt;
4362b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4363b13edf7fSJoe Perches			my $ctx = '';
4364b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4365b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4366b13edf7fSJoe Perches			$ctx = $dstat;
4367b13edf7fSJoe Perches
4368b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
43691b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4370b13edf7fSJoe Perches
4371b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4372b13edf7fSJoe Perches				my $stmts = $2;
4373b13edf7fSJoe Perches				my $semis = $3;
4374b13edf7fSJoe Perches
4375b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4376b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4377b13edf7fSJoe Perches				my $herectx = $here . "\n";
4378b13edf7fSJoe Perches
4379b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4380b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4381b13edf7fSJoe Perches				}
4382b13edf7fSJoe Perches
4383ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4384ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4385b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4386b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4387b13edf7fSJoe Perches				}
4388b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4389b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4390b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4391b13edf7fSJoe Perches				}
4392f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4393f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4394f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4395f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4396f5ef95b1SJoe Perches
4397f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4398f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4399f5ef95b1SJoe Perches				}
4400f5ef95b1SJoe Perches
4401f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4402f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4403b13edf7fSJoe Perches			}
4404b13edf7fSJoe Perches		}
4405b13edf7fSJoe Perches
4406080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4407080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4408080ba929SMike Frysinger#	.
4409080ba929SMike Frysinger#	ALIGN(...)
4410080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4411080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4412000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4413000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4414080ba929SMike Frysinger		}
4415080ba929SMike Frysinger
4416f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
441713214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
441813214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4419cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
442013214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4421cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4422cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4423aad4f614SJoe Perches				my @allowed = ();
4424aad4f614SJoe Perches				my $allow = 0;
442513214adfSAndy Whitcroft				my $seen = 0;
4426773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4427cf655043SAndy Whitcroft				my $ln = $linenr - 1;
442813214adfSAndy Whitcroft				for my $chunk (@chunks) {
442913214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
443013214adfSAndy Whitcroft
4431773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4432773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4433773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4434773647a0SAndy Whitcroft
4435aad4f614SJoe Perches					$allowed[$allow] = 0;
4436773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4437773647a0SAndy Whitcroft
4438773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4439773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4440773647a0SAndy Whitcroft
4441773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4442cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4443cf655043SAndy Whitcroft
4444773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
444513214adfSAndy Whitcroft
444613214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
444713214adfSAndy Whitcroft
4448aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4449cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4450cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4451aad4f614SJoe Perches						$allowed[$allow] = 1;
445213214adfSAndy Whitcroft					}
445313214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4454cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4455aad4f614SJoe Perches						$allowed[$allow] = 1;
445613214adfSAndy Whitcroft					}
4457cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4458cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4459aad4f614SJoe Perches						$allowed[$allow] = 1;
446013214adfSAndy Whitcroft					}
4461aad4f614SJoe Perches					$allow++;
446213214adfSAndy Whitcroft				}
4463aad4f614SJoe Perches				if ($seen) {
4464aad4f614SJoe Perches					my $sum_allowed = 0;
4465aad4f614SJoe Perches					foreach (@allowed) {
4466aad4f614SJoe Perches						$sum_allowed += $_;
4467aad4f614SJoe Perches					}
4468aad4f614SJoe Perches					if ($sum_allowed == 0) {
4469000d1cc1SJoe Perches						WARN("BRACES",
4470000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4471aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4472aad4f614SJoe Perches						 $seen != $allow) {
4473aad4f614SJoe Perches						CHK("BRACES",
4474aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4475aad4f614SJoe Perches					}
447613214adfSAndy Whitcroft				}
447713214adfSAndy Whitcroft			}
447813214adfSAndy Whitcroft		}
4479773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
448013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4481cf655043SAndy Whitcroft			my $allowed = 0;
4482f0a594c1SAndy Whitcroft
4483cf655043SAndy Whitcroft			# Check the pre-context.
4484cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4485cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4486cf655043SAndy Whitcroft				$allowed = 1;
4487f0a594c1SAndy Whitcroft			}
4488773647a0SAndy Whitcroft
4489773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4490773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4491773647a0SAndy Whitcroft
4492cf655043SAndy Whitcroft			# Check the condition.
4493cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4494773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4495cf655043SAndy Whitcroft			if (defined $cond) {
4496773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4497cf655043SAndy Whitcroft			}
4498cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4499cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4500cf655043SAndy Whitcroft				$allowed = 1;
4501cf655043SAndy Whitcroft			}
4502cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4503cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4504cf655043SAndy Whitcroft				$allowed = 1;
4505cf655043SAndy Whitcroft			}
4506cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4507cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4508cf655043SAndy Whitcroft				$allowed = 1;
4509cf655043SAndy Whitcroft			}
4510cf655043SAndy Whitcroft			# Check the post-context.
4511cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4512cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4513cf655043SAndy Whitcroft				if (defined $cond) {
4514773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4515cf655043SAndy Whitcroft				}
4516cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4517cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4518cf655043SAndy Whitcroft					$allowed = 1;
4519cf655043SAndy Whitcroft				}
4520cf655043SAndy Whitcroft			}
4521cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
452269932487SJustin P. Mattock				my $herectx = $here . "\n";
4523f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4524cf655043SAndy Whitcroft
4525f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
452669932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4527cf655043SAndy Whitcroft				}
4528cf655043SAndy Whitcroft
4529000d1cc1SJoe Perches				WARN("BRACES",
4530000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4531f0a594c1SAndy Whitcroft			}
4532f0a594c1SAndy Whitcroft		}
4533f0a594c1SAndy Whitcroft
45340979ae66SJoe Perches# check for unnecessary blank lines around braces
453577b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4536f8e58219SJoe Perches			if (CHK("BRACES",
4537f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4538f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4539f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4540f8e58219SJoe Perches			}
45410979ae66SJoe Perches		}
454277b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4543f8e58219SJoe Perches			if (CHK("BRACES",
4544f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4545f8e58219SJoe Perches			    $fix) {
4546f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4547f8e58219SJoe Perches			}
45480979ae66SJoe Perches		}
45490979ae66SJoe Perches
45504a0df2efSAndy Whitcroft# no volatiles please
45516c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
45526c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4553000d1cc1SJoe Perches			WARN("VOLATILE",
4554000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
45554a0df2efSAndy Whitcroft		}
45564a0df2efSAndy Whitcroft
45575e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
45585e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
45595e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
45605e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
45615e4f6ba5SJoe Perches		if ($line =~ /^\+\s*"[X\t]*"/ &&
45625e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
45635e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
45645e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
45655e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
45665e4f6ba5SJoe Perches				     $fix &&
45675e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
45685e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
45695e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
45705e4f6ba5SJoe Perches				my $comma_close = "";
45715e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
45725e4f6ba5SJoe Perches					$comma_close = $1;
45735e4f6ba5SJoe Perches				}
45745e4f6ba5SJoe Perches
45755e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
45765e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45775e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
45785e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
45795e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
45805e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
45815e4f6ba5SJoe Perches				$fixedline = $rawline;
45825e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
45835e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
45845e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
45855e4f6ba5SJoe Perches				}
45865e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
45875e4f6ba5SJoe Perches			}
45885e4f6ba5SJoe Perches		}
45895e4f6ba5SJoe Perches
45905e4f6ba5SJoe Perches# check for missing a space in a string concatenation
45915e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
45925e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
45935e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
45945e4f6ba5SJoe Perches		}
45955e4f6ba5SJoe Perches
45965e4f6ba5SJoe Perches# check for spaces before a quoted newline
45975e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
45985e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
45995e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
46005e4f6ba5SJoe Perches			    $fix) {
46015e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
46025e4f6ba5SJoe Perches			}
46035e4f6ba5SJoe Perches
46045e4f6ba5SJoe Perches		}
46055e4f6ba5SJoe Perches
4606f17dba4fSJoe Perches# concatenated string without spaces between elements
4607f17dba4fSJoe Perches		if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4608f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4609f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4610f17dba4fSJoe Perches		}
4611f17dba4fSJoe Perches
461290ad30e5SJoe Perches# uncoalesced string fragments
461390ad30e5SJoe Perches		if ($line =~ /"X*"\s*"/) {
461490ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
461590ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
461690ad30e5SJoe Perches		}
461790ad30e5SJoe Perches
46185e4f6ba5SJoe Perches# check for %L{u,d,i} in strings
46195e4f6ba5SJoe Perches		my $string;
46205e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
46215e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
46225e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
46235e4f6ba5SJoe Perches			if ($string =~ /(?<!%)%L[udi]/) {
46245e4f6ba5SJoe Perches				WARN("PRINTF_L",
46255e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
46265e4f6ba5SJoe Perches				last;
46275e4f6ba5SJoe Perches			}
46285e4f6ba5SJoe Perches		}
46295e4f6ba5SJoe Perches
46305e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
46315e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
46325e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
46335e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
46345e4f6ba5SJoe Perches		}
46355e4f6ba5SJoe Perches
463600df344fSAndy Whitcroft# warn about #if 0
4637c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4638000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4639000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4640de7d4f0eSAndy Whitcroft				$herecurr);
46414a0df2efSAndy Whitcroft		}
46424a0df2efSAndy Whitcroft
464303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
464403df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
464503df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
464603df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
464703df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
464815160f90SFabio Estevam				     "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
46494c432a8fSGreg Kroah-Hartman			}
46504c432a8fSGreg Kroah-Hartman		}
4651f0a594c1SAndy Whitcroft
4652ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4653ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4654ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4655ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4656ebfdc409SJoe Perches		    $linenr > 3) {
4657ebfdc409SJoe Perches			my $testval = $2;
4658ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4659ebfdc409SJoe Perches
4660ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4661ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4662ebfdc409SJoe Perches
4663ebfdc409SJoe Perches			if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4664ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4665ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4666ebfdc409SJoe Perches			}
4667ebfdc409SJoe Perches		}
4668ebfdc409SJoe Perches
4669f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
4670dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
4671f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4672f78d98f6SJoe Perches			my $level = $1;
4673f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
4674f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
4675f78d98f6SJoe Perches			    $fix) {
4676f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
4677f78d98f6SJoe Perches			}
4678f78d98f6SJoe Perches		}
4679f78d98f6SJoe Perches
4680abb08a53SJoe Perches# check for mask then right shift without a parentheses
4681abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4682abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4683abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4684abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
4685abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4686abb08a53SJoe Perches		}
4687abb08a53SJoe Perches
4688b75ac618SJoe Perches# check for pointer comparisons to NULL
4689b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
4690b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4691b75ac618SJoe Perches				my $val = $1;
4692b75ac618SJoe Perches				my $equal = "!";
4693b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
4694b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
4695b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4696b75ac618SJoe Perches					    $fix) {
4697b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4698b75ac618SJoe Perches				}
4699b75ac618SJoe Perches			}
4700b75ac618SJoe Perches		}
4701b75ac618SJoe Perches
47028716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
47038716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
47048716de38SJoe Perches			my $attr = $1;
47058716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
47068716de38SJoe Perches				my $ptr = $1;
47078716de38SJoe Perches				my $var = $2;
47088716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
47098716de38SJoe Perches				      ERROR("MISPLACED_INIT",
47108716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
47118716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
47128716de38SJoe Perches				      WARN("MISPLACED_INIT",
47138716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
47148716de38SJoe Perches				    $fix) {
4715194f66fcSJoe 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;
47168716de38SJoe Perches				}
47178716de38SJoe Perches			}
47188716de38SJoe Perches		}
47198716de38SJoe Perches
4720e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4721e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4722e970b884SJoe Perches			my $attr = $1;
4723e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4724e970b884SJoe Perches			my $attr_prefix = $1;
4725e970b884SJoe Perches			my $attr_type = $2;
4726e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4727e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4728e970b884SJoe Perches			    $fix) {
4729194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4730e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4731e970b884SJoe Perches			}
4732e970b884SJoe Perches		}
4733e970b884SJoe Perches
4734e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4735e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4736e970b884SJoe Perches			my $attr = $1;
4737e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4738e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4739e970b884SJoe Perches			    $fix) {
4740194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4741e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4742e970b884SJoe Perches				$lead = rtrim($1);
4743e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4744e970b884SJoe Perches				$lead = "${lead}const ";
4745194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4746e970b884SJoe Perches			}
4747e970b884SJoe Perches		}
4748e970b884SJoe Perches
4749fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4750fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4751fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4752fbdb8138SJoe Perches			my $constant_func = $1;
4753fbdb8138SJoe Perches			my $func = $constant_func;
4754fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4755fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4756fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4757fbdb8138SJoe Perches			    $fix) {
4758194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4759fbdb8138SJoe Perches			}
4760fbdb8138SJoe Perches		}
4761fbdb8138SJoe Perches
47621a15a250SPatrick Pannuto# prefer usleep_range over udelay
476337581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
476443c1d77cSJoe Perches			my $delay = $1;
47651a15a250SPatrick Pannuto			# ignore udelay's < 10, however
476643c1d77cSJoe Perches			if (! ($delay < 10) ) {
4767000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
476843c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
476943c1d77cSJoe Perches			}
477043c1d77cSJoe Perches			if ($delay > 2000) {
477143c1d77cSJoe Perches				WARN("LONG_UDELAY",
477243c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
47731a15a250SPatrick Pannuto			}
47741a15a250SPatrick Pannuto		}
47751a15a250SPatrick Pannuto
477609ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
477709ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
477809ef8725SPatrick Pannuto			if ($1 < 20) {
4779000d1cc1SJoe Perches				WARN("MSLEEP",
478043c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
478109ef8725SPatrick Pannuto			}
478209ef8725SPatrick Pannuto		}
478309ef8725SPatrick Pannuto
478436ec1939SJoe Perches# check for comparisons of jiffies
478536ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
478636ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
478736ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
478836ec1939SJoe Perches		}
478936ec1939SJoe Perches
47909d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
47919d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
47929d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
47939d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
47949d7a34a5SJoe Perches		}
47959d7a34a5SJoe Perches
479600df344fSAndy Whitcroft# warn about #ifdefs in C files
4797c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
479800df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
479900df344fSAndy Whitcroft#			print "$herecurr";
480000df344fSAndy Whitcroft#			$clean = 0;
480100df344fSAndy Whitcroft#		}
480200df344fSAndy Whitcroft
480322f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4804c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
48053705ce5bSJoe Perches			if (ERROR("SPACING",
48063705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
48073705ce5bSJoe Perches			    $fix) {
4808194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48093705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
48103705ce5bSJoe Perches			}
48113705ce5bSJoe Perches
481222f2a2efSAndy Whitcroft		}
481322f2a2efSAndy Whitcroft
48144a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4815171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4816171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
48174a0df2efSAndy Whitcroft			my $which = $1;
48184a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4819000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4820000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
48214a0df2efSAndy Whitcroft			}
48224a0df2efSAndy Whitcroft		}
48234a0df2efSAndy Whitcroft# check for memory barriers without a comment.
48244a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
48254a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4826c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4827000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
48284a0df2efSAndy Whitcroft			}
48294a0df2efSAndy Whitcroft		}
48304a0df2efSAndy Whitcroft# check of hardware specific defines
4831c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4832000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4833000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
48340a920b5bSAndy Whitcroft		}
4835653d4876SAndy Whitcroft
4836d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4837d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4838000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4839000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4840d4977c78STobias Klauser		}
4841d4977c78STobias Klauser
4842de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4843de7d4f0eSAndy Whitcroft# storage class and type.
48449c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
48459c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4846000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4847000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4848de7d4f0eSAndy Whitcroft		}
4849de7d4f0eSAndy Whitcroft
48508905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
48512b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48522b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4853d5e616fcSJoe Perches			if (WARN("INLINE",
4854d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4855d5e616fcSJoe Perches			    $fix) {
4856194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4857d5e616fcSJoe Perches
4858d5e616fcSJoe Perches			}
48598905a67cSAndy Whitcroft		}
48608905a67cSAndy Whitcroft
48613d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
48622b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48632b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4864000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4865000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
48663d130fd0SJoe Perches		}
48673d130fd0SJoe Perches
486839b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
48692b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48702b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4871000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4872000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
487339b7e287SJoe Perches		}
487439b7e287SJoe Perches
48755f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
48762b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48772b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4878d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4879d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4880d5e616fcSJoe Perches			    $fix) {
4881194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4882d5e616fcSJoe Perches
4883d5e616fcSJoe Perches			}
48845f14d3bdSJoe Perches		}
48855f14d3bdSJoe Perches
48866061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
48872b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48882b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4889d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4890d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4891d5e616fcSJoe Perches			    $fix) {
4892194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4893d5e616fcSJoe Perches			}
48946061d949SJoe Perches		}
48956061d949SJoe Perches
4896619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
4897619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4898619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4899619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4900619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
4901619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
4902619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
4903619a908aSJoe Perches		}
4904619a908aSJoe Perches
49058f53a9b8SJoe Perches# check for sizeof(&)
49068f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4907000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4908000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
49098f53a9b8SJoe Perches		}
49108f53a9b8SJoe Perches
491166c80b60SJoe Perches# check for sizeof without parenthesis
491266c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4913d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4914d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4915d5e616fcSJoe Perches			    $fix) {
4916194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4917d5e616fcSJoe Perches			}
491866c80b60SJoe Perches		}
491966c80b60SJoe Perches
492088982feaSJoe Perches# check for struct spinlock declarations
492188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
492288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
492388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
492488982feaSJoe Perches		}
492588982feaSJoe Perches
4926a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
492706668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4928a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
4929caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
4930caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
4931d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4932d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4933d5e616fcSJoe Perches				    $fix) {
4934194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4935d5e616fcSJoe Perches				}
4936a6962d72SJoe Perches			}
4937a6962d72SJoe Perches		}
4938a6962d72SJoe Perches
4939554e165cSAndy Whitcroft# Check for misused memsets
4940d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4941d1fe9c09SJoe Perches		    defined $stat &&
4942d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4943554e165cSAndy Whitcroft
4944d7c76ba7SJoe Perches			my $ms_addr = $2;
4945d1fe9c09SJoe Perches			my $ms_val = $7;
4946d1fe9c09SJoe Perches			my $ms_size = $12;
4947d7c76ba7SJoe Perches
4948554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4949554e165cSAndy Whitcroft				ERROR("MEMSET",
4950d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4951554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4952554e165cSAndy Whitcroft				WARN("MEMSET",
4953d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4954d7c76ba7SJoe Perches			}
4955d7c76ba7SJoe Perches		}
4956d7c76ba7SJoe Perches
495798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
495898a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
495998a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
496098a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
496198a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
496298a9bba5SJoe Perches			    $fix) {
4963194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
496498a9bba5SJoe Perches			}
496598a9bba5SJoe Perches		}
496698a9bba5SJoe Perches
4967d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4968d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4969d1fe9c09SJoe Perches		    defined $stat &&
4970d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4971d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4972d7c76ba7SJoe Perches				my $call = $1;
4973d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4974d7c76ba7SJoe Perches				my $arg1 = $3;
4975d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4976d1fe9c09SJoe Perches				my $arg2 = $8;
4977d7c76ba7SJoe Perches				my $cast;
4978d7c76ba7SJoe Perches
4979d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4980d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4981d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4982d7c76ba7SJoe Perches					$cast = $cast1;
4983d7c76ba7SJoe Perches				} else {
4984d7c76ba7SJoe Perches					$cast = $cast2;
4985d7c76ba7SJoe Perches				}
4986d7c76ba7SJoe Perches				WARN("MINMAX",
4987d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4988554e165cSAndy Whitcroft			}
4989554e165cSAndy Whitcroft		}
4990554e165cSAndy Whitcroft
49914a273195SJoe Perches# check usleep_range arguments
49924a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
49934a273195SJoe Perches		    defined $stat &&
49944a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
49954a273195SJoe Perches			my $min = $1;
49964a273195SJoe Perches			my $max = $7;
49974a273195SJoe Perches			if ($min eq $max) {
49984a273195SJoe Perches				WARN("USLEEP_RANGE",
49994a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
50004a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
50014a273195SJoe Perches				 $min > $max) {
50024a273195SJoe Perches				WARN("USLEEP_RANGE",
50034a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
50044a273195SJoe Perches			}
50054a273195SJoe Perches		}
50064a273195SJoe Perches
5007823b794cSJoe Perches# check for naked sscanf
5008823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5009823b794cSJoe Perches		    defined $stat &&
50106c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5011823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5012823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5013823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5014823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5015823b794cSJoe Perches			$lc = $lc + $linenr;
5016823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5017823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5018823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5019823b794cSJoe Perches			}
5020823b794cSJoe Perches			WARN("NAKED_SSCANF",
5021823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5022823b794cSJoe Perches		}
5023823b794cSJoe Perches
5024afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5025afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5026afc819abSJoe Perches		    defined $stat &&
5027afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5028afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5029afc819abSJoe Perches			$lc = $lc + $linenr;
5030afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5031afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5032afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5033afc819abSJoe Perches			}
5034afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5035afc819abSJoe Perches				my $format = $6;
5036afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5037afc819abSJoe Perches				if ($count == 1 &&
5038afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5039afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5040afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5041afc819abSJoe Perches				}
5042afc819abSJoe Perches			}
5043afc819abSJoe Perches		}
5044afc819abSJoe Perches
504570dc8a48SJoe Perches# check for new externs in .h files.
504670dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
504770dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5048d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
504970dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
505070dc8a48SJoe Perches			    $fix) {
5051194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
505270dc8a48SJoe Perches			}
505370dc8a48SJoe Perches		}
505470dc8a48SJoe Perches
5055de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5056171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5057c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5058171ae1a4SAndy Whitcroft		{
5059c45dcabdSAndy Whitcroft			my $function_name = $1;
5060c45dcabdSAndy Whitcroft			my $paren_space = $2;
5061171ae1a4SAndy Whitcroft
5062171ae1a4SAndy Whitcroft			my $s = $stat;
5063171ae1a4SAndy Whitcroft			if (defined $cond) {
5064171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5065171ae1a4SAndy Whitcroft			}
5066c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5067c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5068c45dcabdSAndy Whitcroft			{
5069000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5070000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5071de7d4f0eSAndy Whitcroft			}
5072de7d4f0eSAndy Whitcroft
5073171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5074000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5075000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5076171ae1a4SAndy Whitcroft			}
50779c9ba34eSAndy Whitcroft
50789c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
50799c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
50809c9ba34eSAndy Whitcroft		{
5081000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5082000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5083171ae1a4SAndy Whitcroft		}
5084171ae1a4SAndy Whitcroft
5085de7d4f0eSAndy Whitcroft# checks for new __setup's
5086de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5087de7d4f0eSAndy Whitcroft			my $name = $1;
5088de7d4f0eSAndy Whitcroft
5089de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5090000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5091000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5092de7d4f0eSAndy Whitcroft			}
5093653d4876SAndy Whitcroft		}
50949c0ca6f9SAndy Whitcroft
50959c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5096caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5097000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5098000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
50999c0ca6f9SAndy Whitcroft		}
510013214adfSAndy Whitcroft
5101a640d25cSJoe Perches# alloc style
5102a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5103a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5104a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5105a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5106a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5107a640d25cSJoe Perches		}
5108a640d25cSJoe Perches
510960a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
511060a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5111e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
511260a55369SJoe Perches			my $oldfunc = $3;
511360a55369SJoe Perches			my $a1 = $4;
511460a55369SJoe Perches			my $a2 = $10;
511560a55369SJoe Perches			my $newfunc = "kmalloc_array";
511660a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
511760a55369SJoe Perches			my $r1 = $a1;
511860a55369SJoe Perches			my $r2 = $a2;
511960a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
512060a55369SJoe Perches				$r1 = $a2;
512160a55369SJoe Perches				$r2 = $a1;
512260a55369SJoe Perches			}
5123e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5124e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5125e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5126e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5127e367455aSJoe Perches				    $fix) {
5128194f66fcSJoe 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;
512960a55369SJoe Perches
513060a55369SJoe Perches				}
513160a55369SJoe Perches			}
513260a55369SJoe Perches		}
513360a55369SJoe Perches
5134972fdea2SJoe Perches# check for krealloc arg reuse
5135972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5136972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5137972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5138972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5139972fdea2SJoe Perches		}
5140972fdea2SJoe Perches
51415ce59ae0SJoe Perches# check for alloc argument mismatch
51425ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
51435ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
51445ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
51455ce59ae0SJoe Perches		}
51465ce59ae0SJoe Perches
5147caf2a54fSJoe Perches# check for multiple semicolons
5148caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5149d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5150d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5151d5e616fcSJoe Perches			    $fix) {
5152194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5153d5e616fcSJoe Perches			}
5154d1e2ad07SJoe Perches		}
5155d1e2ad07SJoe Perches
51560ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
51570ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
51580ab90191SJoe Perches			my $ull = "";
51590ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
51600ab90191SJoe Perches			if (CHK("BIT_MACRO",
51610ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
51620ab90191SJoe Perches			    $fix) {
51630ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
51640ab90191SJoe Perches			}
51650ab90191SJoe Perches		}
51660ab90191SJoe Perches
5167e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5168c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5169c34c09a8SJoe Perches			my $has_break = 0;
5170c34c09a8SJoe Perches			my $has_statement = 0;
5171c34c09a8SJoe Perches			my $count = 0;
5172c34c09a8SJoe Perches			my $prevline = $linenr;
5173e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5174c34c09a8SJoe Perches				$prevline--;
5175c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5176c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5177c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5178c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5179c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5180c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5181c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5182c34c09a8SJoe Perches				$has_statement = 1;
5183c34c09a8SJoe Perches				$count++;
5184c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5185c34c09a8SJoe Perches			}
5186c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5187c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5188c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5189c34c09a8SJoe Perches			}
5190c34c09a8SJoe Perches		}
5191c34c09a8SJoe Perches
5192d1e2ad07SJoe Perches# check for switch/default statements without a break;
5193d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5194d1e2ad07SJoe Perches		    defined $stat &&
5195d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5196d1e2ad07SJoe Perches			my $ctx = '';
5197d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5198d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5199d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5200d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5201d1e2ad07SJoe Perches			}
5202d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5203d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5204caf2a54fSJoe Perches		}
5205caf2a54fSJoe Perches
520613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5207d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5208d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5209d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5210d5e616fcSJoe Perches			    $fix) {
5211194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5212d5e616fcSJoe Perches			}
521313214adfSAndy Whitcroft		}
5214773647a0SAndy Whitcroft
521562ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
521662ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
521762ec818fSJoe Perches			ERROR("DATE_TIME",
521862ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
521962ec818fSJoe Perches		}
522062ec818fSJoe Perches
52212c92488aSJoe Perches# check for use of yield()
52222c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
52232c92488aSJoe Perches			WARN("YIELD",
52242c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
52252c92488aSJoe Perches		}
52262c92488aSJoe Perches
5227179f8f40SJoe Perches# check for comparisons against true and false
5228179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5229179f8f40SJoe Perches			my $lead = $1;
5230179f8f40SJoe Perches			my $arg = $2;
5231179f8f40SJoe Perches			my $test = $3;
5232179f8f40SJoe Perches			my $otype = $4;
5233179f8f40SJoe Perches			my $trail = $5;
5234179f8f40SJoe Perches			my $op = "!";
5235179f8f40SJoe Perches
5236179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5237179f8f40SJoe Perches
5238179f8f40SJoe Perches			my $type = lc($otype);
5239179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5240179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5241179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5242179f8f40SJoe Perches					$op = "";
5243179f8f40SJoe Perches				}
5244179f8f40SJoe Perches
5245179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5246179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5247179f8f40SJoe Perches
5248179f8f40SJoe Perches## maybe suggesting a correct construct would better
5249179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5250179f8f40SJoe Perches
5251179f8f40SJoe Perches			}
5252179f8f40SJoe Perches		}
5253179f8f40SJoe Perches
52544882720bSThomas Gleixner# check for semaphores initialized locked
52554882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5256000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5257000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5258773647a0SAndy Whitcroft		}
52596712d858SJoe Perches
526067d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
526167d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5262000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
526367d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5264773647a0SAndy Whitcroft		}
52656712d858SJoe Perches
5266ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5267f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5268000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5269ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5270f3db6639SMichael Ellerman		}
52716712d858SJoe Perches
52720f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
52730f3c5aabSJoe Perches		my $const_structs = qr{
52740f3c5aabSJoe Perches				acpi_dock_ops|
527579404849SEmese Revfy				address_space_operations|
527679404849SEmese Revfy				backlight_ops|
527779404849SEmese Revfy				block_device_operations|
527879404849SEmese Revfy				dentry_operations|
527979404849SEmese Revfy				dev_pm_ops|
528079404849SEmese Revfy				dma_map_ops|
528179404849SEmese Revfy				extent_io_ops|
528279404849SEmese Revfy				file_lock_operations|
528379404849SEmese Revfy				file_operations|
528479404849SEmese Revfy				hv_ops|
528579404849SEmese Revfy				ide_dma_ops|
528679404849SEmese Revfy				intel_dvo_dev_ops|
528779404849SEmese Revfy				item_operations|
528879404849SEmese Revfy				iwl_ops|
528979404849SEmese Revfy				kgdb_arch|
529079404849SEmese Revfy				kgdb_io|
529179404849SEmese Revfy				kset_uevent_ops|
529279404849SEmese Revfy				lock_manager_operations|
529379404849SEmese Revfy				microcode_ops|
529479404849SEmese Revfy				mtrr_ops|
529579404849SEmese Revfy				neigh_ops|
529679404849SEmese Revfy				nlmsvc_binding|
52970f3c5aabSJoe Perches				of_device_id|
529879404849SEmese Revfy				pci_raw_ops|
529979404849SEmese Revfy				pipe_buf_operations|
530079404849SEmese Revfy				platform_hibernation_ops|
530179404849SEmese Revfy				platform_suspend_ops|
530279404849SEmese Revfy				proto_ops|
530379404849SEmese Revfy				rpc_pipe_ops|
530479404849SEmese Revfy				seq_operations|
530579404849SEmese Revfy				snd_ac97_build_ops|
530679404849SEmese Revfy				soc_pcmcia_socket_ops|
530779404849SEmese Revfy				stacktrace_ops|
530879404849SEmese Revfy				sysfs_ops|
530979404849SEmese Revfy				tty_operations|
531079404849SEmese Revfy				usb_mon_operations|
531179404849SEmese Revfy				wd_ops}x;
53126903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
53130f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5314000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5315000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
53166903ffb2SAndy Whitcroft				$herecurr);
53172b6db5cbSAndy Whitcroft		}
5318773647a0SAndy Whitcroft
5319773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5320773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5321773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5322c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5323c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5324171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5325171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5326171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5327773647a0SAndy Whitcroft		{
5328000d1cc1SJoe Perches			WARN("NR_CPUS",
5329000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5330773647a0SAndy Whitcroft		}
53319c9ba34eSAndy Whitcroft
533252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
533352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
533452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
533552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
533652ea8506SJoe Perches		}
533752ea8506SJoe Perches
5338acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5339acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5340acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5341acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5342acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5343acd9362cSJoe Perches		}
5344acd9362cSJoe Perches
5345691d77b6SAndy Whitcroft# whine mightly about in_atomic
5346691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5347691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5348000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5349000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5350f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5351000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5352000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5353691d77b6SAndy Whitcroft			}
5354691d77b6SAndy Whitcroft		}
53551704f47bSPeter Zijlstra
53561704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
53571704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
53581704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
53591704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
53601704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
53611704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5362000d1cc1SJoe Perches				ERROR("LOCKDEP",
5363000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
53641704f47bSPeter Zijlstra			}
53651704f47bSPeter Zijlstra		}
536688f8831cSDave Jones
5367b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5368b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5369000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5370000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
537188f8831cSDave Jones		}
53722435880fSJoe Perches
5373515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5374515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5375515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5376515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
53772435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
53782435880fSJoe Perches				my $func = $entry->[0];
53792435880fSJoe Perches				my $arg_pos = $entry->[1];
53802435880fSJoe Perches
53812435880fSJoe Perches				my $skip_args = "";
53822435880fSJoe Perches				if ($arg_pos > 1) {
53832435880fSJoe Perches					$arg_pos--;
53842435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
53852435880fSJoe Perches				}
53862435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5387515a235eSJoe Perches				if ($line =~ /$test/) {
53882435880fSJoe Perches					my $val = $1;
53892435880fSJoe Perches					$val = $6 if ($skip_args ne "");
53902435880fSJoe Perches
53911727cc70SJoe Perches					if ($val !~ /^0$/ &&
53921727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
53931727cc70SJoe Perches					     length($val) ne 4)) {
53942435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
53951727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5396c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5397c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5398c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
53992435880fSJoe Perches					}
54002435880fSJoe Perches				}
54012435880fSJoe Perches			}
540213214adfSAndy Whitcroft		}
5403515a235eSJoe Perches	}
540413214adfSAndy Whitcroft
540513214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
540613214adfSAndy Whitcroft	# so just keep quiet.
540713214adfSAndy Whitcroft	if ($#rawlines == -1) {
540813214adfSAndy Whitcroft		exit(0);
54090a920b5bSAndy Whitcroft	}
54100a920b5bSAndy Whitcroft
54118905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
54128905a67cSAndy Whitcroft	# things that appear to be patches.
54138905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
54148905a67cSAndy Whitcroft		exit(0);
54158905a67cSAndy Whitcroft	}
54168905a67cSAndy Whitcroft
54178905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
54188905a67cSAndy Whitcroft	# just keep quiet.
54198905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
54208905a67cSAndy Whitcroft		exit(0);
54218905a67cSAndy Whitcroft	}
54228905a67cSAndy Whitcroft
54238905a67cSAndy Whitcroft	if (!$is_patch) {
5424000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5425000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
54260a920b5bSAndy Whitcroft	}
54270a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5428000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5429000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
54300a920b5bSAndy Whitcroft	}
54310a920b5bSAndy Whitcroft
5432f0a594c1SAndy Whitcroft	print report_dump();
543313214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
543413214adfSAndy Whitcroft		print "$filename " if ($summary_file);
54356c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
54366c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
54376c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
54388905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
54396c72ffaaSAndy Whitcroft	}
54408905a67cSAndy Whitcroft
5441d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5442d1fe9c09SJoe Perches
5443d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5444d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5445d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5446d1fe9c09SJoe Perches		}
5447d1fe9c09SJoe Perches
5448d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5449d2c0a235SAndy Whitcroft		# then suggest that.
5450d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5451d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5452d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5453b0781216SMike Frysinger			$rpt_cleaners = 0;
5454d2c0a235SAndy Whitcroft		}
5455d2c0a235SAndy Whitcroft	}
5456d2c0a235SAndy Whitcroft
545791bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
545891bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5459000d1cc1SJoe Perches
5460d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5461d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5462d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
54639624b8d6SJoe Perches		my $newfile = $filename;
54649624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
54653705ce5bSJoe Perches		my $linecount = 0;
54663705ce5bSJoe Perches		my $f;
54673705ce5bSJoe Perches
5468d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5469d752fcc8SJoe Perches
54703705ce5bSJoe Perches		open($f, '>', $newfile)
54713705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
54723705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
54733705ce5bSJoe Perches			$linecount++;
54743705ce5bSJoe Perches			if ($file) {
54753705ce5bSJoe Perches				if ($linecount > 3) {
54763705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
54773705ce5bSJoe Perches					print $f $fixed_line . "\n";
54783705ce5bSJoe Perches				}
54793705ce5bSJoe Perches			} else {
54803705ce5bSJoe Perches				print $f $fixed_line . "\n";
54813705ce5bSJoe Perches			}
54823705ce5bSJoe Perches		}
54833705ce5bSJoe Perches		close($f);
54843705ce5bSJoe Perches
54853705ce5bSJoe Perches		if (!$quiet) {
54863705ce5bSJoe Perches			print << "EOM";
54873705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
54883705ce5bSJoe Perches
54893705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
54903705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
54913705ce5bSJoe Perches
54923705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
54933705ce5bSJoe PerchesNo warranties, expressed or implied...
54943705ce5bSJoe Perches
54953705ce5bSJoe PerchesEOM
54963705ce5bSJoe Perches		}
54973705ce5bSJoe Perches	}
54983705ce5bSJoe Perches
54990a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5500c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
55010a920b5bSAndy Whitcroft	}
55020a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5503000d1cc1SJoe Perches		print << "EOM";
5504000d1cc1SJoe Perches$vname has style problems, please review.
5505000d1cc1SJoe Perches
5506000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5507000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5508000d1cc1SJoe PerchesEOM
55090a920b5bSAndy Whitcroft	}
551013214adfSAndy Whitcroft
55110a920b5bSAndy Whitcroft	return $clean;
55120a920b5bSAndy Whitcroft}
5513