xref: /linux-6.15/scripts/checkpatch.pl (revision d2e025f3)
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";
5077f5b10aSHannes Eder
5177f5b10aSHannes Edersub help {
5277f5b10aSHannes Eder	my ($exitcode) = @_;
5377f5b10aSHannes Eder
5477f5b10aSHannes Eder	print << "EOM";
5577f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
5677f5b10aSHannes EderVersion: $V
5777f5b10aSHannes Eder
5877f5b10aSHannes EderOptions:
5977f5b10aSHannes Eder  -q, --quiet                quiet
6077f5b10aSHannes Eder  --no-tree                  run without a kernel tree
6177f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
6277f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
6377f5b10aSHannes Eder  --emacs                    emacs compile window format
6477f5b10aSHannes Eder  --terse                    one line per report
6577f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
6677f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
6791bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
68000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
696cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
7056193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
71000d1cc1SJoe Perches  --show-types               show the message "types" in the output
7277f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
7377f5b10aSHannes Eder  --no-summary               suppress the per-file summary
7477f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
7577f5b10aSHannes Eder  --summary-file             include the filename in summary
7677f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
7777f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
7877f5b10aSHannes Eder                             is all off)
7977f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
8077f5b10aSHannes Eder                             literally
813705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
823705ce5bSJoe Perches                             If correctable single-line errors exist, create
833705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
843705ce5bSJoe Perches                             with potential errors corrected to the preferred
853705ce5bSJoe Perches                             checkpatch style
869624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
879624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
889624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
89d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
90d62a201fSDave Hansen                             runtime errors.
9177f5b10aSHannes Eder  -h, --help, --version      display this help and exit
9277f5b10aSHannes Eder
9377f5b10aSHannes EderWhen FILE is - read standard input.
9477f5b10aSHannes EderEOM
9577f5b10aSHannes Eder
9677f5b10aSHannes Eder	exit($exitcode);
9777f5b10aSHannes Eder}
9877f5b10aSHannes Eder
99000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
100000d1cc1SJoe Perchesif (-f $conf) {
101000d1cc1SJoe Perches	my @conf_args;
102000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
103000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
104000d1cc1SJoe Perches
105000d1cc1SJoe Perches	while (<$conffile>) {
106000d1cc1SJoe Perches		my $line = $_;
107000d1cc1SJoe Perches
108000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
109000d1cc1SJoe Perches		$line =~ s/^\s*//g;
110000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
111000d1cc1SJoe Perches
112000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
113000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
114000d1cc1SJoe Perches
115000d1cc1SJoe Perches		my @words = split(" ", $line);
116000d1cc1SJoe Perches		foreach my $word (@words) {
117000d1cc1SJoe Perches			last if ($word =~ m/^#/);
118000d1cc1SJoe Perches			push (@conf_args, $word);
119000d1cc1SJoe Perches		}
120000d1cc1SJoe Perches	}
121000d1cc1SJoe Perches	close($conffile);
122000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
123000d1cc1SJoe Perches}
124000d1cc1SJoe Perches
1250a920b5bSAndy WhitcroftGetOptions(
1266c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1270a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1280a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1290a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1306c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1318905a67cSAndy Whitcroft	'terse!'	=> \$terse,
13277f5b10aSHannes Eder	'f|file!'	=> \$file,
1336c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1346c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
135000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
13691bfe484SJoe Perches	'types=s'	=> \@use,
137000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1386cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
13956193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1406c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1418905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1428905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
14313214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1443705ce5bSJoe Perches	'fix!'		=> \$fix,
1459624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
146d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
147c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
148773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
14977f5b10aSHannes Eder	'h|help'	=> \$help,
15077f5b10aSHannes Eder	'version'	=> \$help
15177f5b10aSHannes Eder) or help(1);
15277f5b10aSHannes Eder
15377f5b10aSHannes Ederhelp(0) if ($help);
1540a920b5bSAndy Whitcroft
1559624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
1562ac73b4fSJoe Perches$check_orig = $check;
1579624b8d6SJoe Perches
1580a920b5bSAndy Whitcroftmy $exit = 0;
1590a920b5bSAndy Whitcroft
160d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
161d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
162d62a201fSDave Hansen	if (!$ignore_perl_version) {
163d62a201fSDave Hansen		exit(1);
164d62a201fSDave Hansen	}
165d62a201fSDave Hansen}
166d62a201fSDave Hansen
1670a920b5bSAndy Whitcroftif ($#ARGV < 0) {
16877f5b10aSHannes Eder	print "$P: no input files\n";
1690a920b5bSAndy Whitcroft	exit(1);
1700a920b5bSAndy Whitcroft}
1710a920b5bSAndy Whitcroft
17291bfe484SJoe Perchessub hash_save_array_words {
17391bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
17491bfe484SJoe Perches
17591bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
17691bfe484SJoe Perches	foreach my $word (@array) {
177000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
178000d1cc1SJoe Perches		$word =~ s/^\s*//g;
179000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
180000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
181000d1cc1SJoe Perches
182000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
183000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
184000d1cc1SJoe Perches
18591bfe484SJoe Perches		$hashRef->{$word}++;
186000d1cc1SJoe Perches	}
18791bfe484SJoe Perches}
18891bfe484SJoe Perches
18991bfe484SJoe Perchessub hash_show_words {
19091bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
19191bfe484SJoe Perches
19258cb3cf6SJoe Perches	if ($quiet == 0 && keys %$hashRef) {
19391bfe484SJoe Perches		print "NOTE: $prefix message types:";
19458cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
19591bfe484SJoe Perches			print " $word";
19691bfe484SJoe Perches		}
19791bfe484SJoe Perches		print "\n\n";
19891bfe484SJoe Perches	}
19991bfe484SJoe Perches}
20091bfe484SJoe Perches
20191bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
20291bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
203000d1cc1SJoe Perches
204c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
205c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2067429c690SAndy Whitcroftmy $dbg_type = 0;
207a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
208c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
20921caa13cSAndy Whitcroft	## no critic
21021caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
21121caa13cSAndy Whitcroft	die "$@" if ($@);
212c2fdda0dSAndy Whitcroft}
213c2fdda0dSAndy Whitcroft
214d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
215d2c0a235SAndy Whitcroft
2168905a67cSAndy Whitcroftif ($terse) {
2178905a67cSAndy Whitcroft	$emacs = 1;
2188905a67cSAndy Whitcroft	$quiet++;
2198905a67cSAndy Whitcroft}
2208905a67cSAndy Whitcroft
2216c72ffaaSAndy Whitcroftif ($tree) {
2226c72ffaaSAndy Whitcroft	if (defined $root) {
2236c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2246c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2256c72ffaaSAndy Whitcroft		}
2266c72ffaaSAndy Whitcroft	} else {
2276c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2286c72ffaaSAndy Whitcroft			$root = '.';
2296c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2306c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2316c72ffaaSAndy Whitcroft			$root = $1;
2326c72ffaaSAndy Whitcroft		}
2336c72ffaaSAndy Whitcroft	}
2346c72ffaaSAndy Whitcroft
2356c72ffaaSAndy Whitcroft	if (!defined $root) {
2360a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2370a920b5bSAndy Whitcroft		exit(2);
2380a920b5bSAndy Whitcroft	}
2396c72ffaaSAndy Whitcroft}
2406c72ffaaSAndy Whitcroft
2416c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2426c72ffaaSAndy Whitcroft
2432ceb532bSAndy Whitcroftour $Ident	= qr{
2442ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2452ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2462ceb532bSAndy Whitcroft		}x;
2476c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2486c72ffaaSAndy Whitcroftour $Sparse	= qr{
2496c72ffaaSAndy Whitcroft			__user|
2506c72ffaaSAndy Whitcroft			__kernel|
2516c72ffaaSAndy Whitcroft			__force|
2526c72ffaaSAndy Whitcroft			__iomem|
2536c72ffaaSAndy Whitcroft			__must_check|
2546c72ffaaSAndy Whitcroft			__init_refok|
255417495edSAndy Whitcroft			__kprobes|
256165e72a6SSven Eckelmann			__ref|
257165e72a6SSven Eckelmann			__rcu
2586c72ffaaSAndy Whitcroft		}x;
259e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
260e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
261e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
262e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
263e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
2648716de38SJoe Perches
26552131292SWolfram Sang# Notes to $Attribute:
26652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
2676c72ffaaSAndy Whitcroftour $Attribute	= qr{
2686c72ffaaSAndy Whitcroft			const|
26903f1df7dSJoe Perches			__percpu|
27003f1df7dSJoe Perches			__nocast|
27103f1df7dSJoe Perches			__safe|
27203f1df7dSJoe Perches			__bitwise__|
27303f1df7dSJoe Perches			__packed__|
27403f1df7dSJoe Perches			__packed2__|
27503f1df7dSJoe Perches			__naked|
27603f1df7dSJoe Perches			__maybe_unused|
27703f1df7dSJoe Perches			__always_unused|
27803f1df7dSJoe Perches			__noreturn|
27903f1df7dSJoe Perches			__used|
28003f1df7dSJoe Perches			__cold|
281e23ef1f3SJoe Perches			__pure|
28203f1df7dSJoe Perches			__noclone|
28303f1df7dSJoe Perches			__deprecated|
2846c72ffaaSAndy Whitcroft			__read_mostly|
2856c72ffaaSAndy Whitcroft			__kprobes|
2868716de38SJoe Perches			$InitAttribute|
28724e1d81aSAndy Whitcroft			____cacheline_aligned|
28824e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
2895fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
2905fe3af11SAndy Whitcroft			__weak
2916c72ffaaSAndy Whitcroft		  }x;
292c45dcabdSAndy Whitcroftour $Modifier;
29391cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
2946c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
2956c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
2966c72ffaaSAndy Whitcroft
29795e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
29895e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
29995e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
30095e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3012435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
302c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
303326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
304326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
305326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
30674349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3072435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
308326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
309447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
31023f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3116c72ffaaSAndy Whitcroftour $Operators	= qr{
3126c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3136c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
31423f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3156c72ffaaSAndy Whitcroft		  }x;
3166c72ffaaSAndy Whitcroft
31791cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
31891cb5195SJoe Perches
3198905a67cSAndy Whitcroftour $NonptrType;
3201813087dSJoe Perchesour $NonptrTypeMisordered;
3218716de38SJoe Perchesour $NonptrTypeWithAttr;
3228905a67cSAndy Whitcroftour $Type;
3231813087dSJoe Perchesour $TypeMisordered;
3248905a67cSAndy Whitcroftour $Declare;
3251813087dSJoe Perchesour $DeclareMisordered;
3268905a67cSAndy Whitcroft
32715662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
32815662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
329171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
330171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
331171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
332171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
333171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
334171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
335171ae1a4SAndy Whitcroft}x;
336171ae1a4SAndy Whitcroft
33715662b3eSJoe Perchesour $UTF8	= qr{
33815662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
33915662b3eSJoe Perches	| $NON_ASCII_UTF8
34015662b3eSJoe Perches}x;
34115662b3eSJoe Perches
342021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
343021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
344021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
345021158b4SJoe Perches)};
346021158b4SJoe Perches
3478ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
348fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3498ed22cadSAndy Whitcroft	atomic_t
3508ed22cadSAndy Whitcroft)};
3518ed22cadSAndy Whitcroft
352691e669bSJoe Perchesour $logFunctions = qr{(?x:
3536e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3547d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3556e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
356b0531722SJoe Perches	panic|
35706668727SJoe Perches	MODULE_[A-Z_]+|
35806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
359691e669bSJoe Perches)};
360691e669bSJoe Perches
36120112475SJoe Perchesour $signature_tags = qr{(?xi:
36220112475SJoe Perches	Signed-off-by:|
36320112475SJoe Perches	Acked-by:|
36420112475SJoe Perches	Tested-by:|
36520112475SJoe Perches	Reviewed-by:|
36620112475SJoe Perches	Reported-by:|
3678543ae12SMugunthan V N	Suggested-by:|
36820112475SJoe Perches	To:|
36920112475SJoe Perches	Cc:
37020112475SJoe Perches)};
37120112475SJoe Perches
3721813087dSJoe Perchesour @typeListMisordered = (
3731813087dSJoe Perches	qr{char\s+(?:un)?signed},
3741813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3751813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3761813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3771813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3781813087dSJoe Perches	qr{short\s+(?:un)?signed},
3791813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
3801813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
3811813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
3821813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
3831813087dSJoe Perches	qr{int\s+(?:un)?signed},
3841813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
3851813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
3861813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
3871813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
3881813087dSJoe Perches	qr{long\s+(?:un)?signed},
3891813087dSJoe Perches);
3901813087dSJoe Perches
3918905a67cSAndy Whitcroftour @typeList = (
3928905a67cSAndy Whitcroft	qr{void},
3930c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
3940c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
3950c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
3960c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
3970c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
3980c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
3990c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4000c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4010c773d9dSJoe Perches	qr{(?:un)?signed},
4028905a67cSAndy Whitcroft	qr{float},
4038905a67cSAndy Whitcroft	qr{double},
4048905a67cSAndy Whitcroft	qr{bool},
4058905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4068905a67cSAndy Whitcroft	qr{union\s+$Ident},
4078905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4088905a67cSAndy Whitcroft	qr{${Ident}_t},
4098905a67cSAndy Whitcroft	qr{${Ident}_handler},
4108905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4111813087dSJoe Perches	@typeListMisordered,
4128905a67cSAndy Whitcroft);
4138716de38SJoe Perchesour @typeListWithAttr = (
4148716de38SJoe Perches	@typeList,
4158716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4168716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4178716de38SJoe Perches);
4188716de38SJoe Perches
419c45dcabdSAndy Whitcroftour @modifierList = (
420c45dcabdSAndy Whitcroft	qr{fastcall},
421c45dcabdSAndy Whitcroft);
4228905a67cSAndy Whitcroft
4232435880fSJoe Perchesour @mode_permission_funcs = (
4242435880fSJoe Perches	["module_param", 3],
4252435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4262435880fSJoe Perches	["module_param_array_named", 5],
4272435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4282435880fSJoe Perches	["proc_create(?:_data|)", 2],
4292435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4302435880fSJoe Perches);
4312435880fSJoe Perches
432515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
433515a235eSJoe Perchesour $mode_perms_search = "";
434515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
435515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
436515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
437515a235eSJoe Perches}
438515a235eSJoe Perches
4397840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4407840a94cSWolfram Sang	irq|
441cdcee686SSergey Ryazanov	memory|
442cdcee686SSergey Ryazanov	time|
443cdcee686SSergey Ryazanov	reboot
4447840a94cSWolfram Sang)};
4457840a94cSWolfram Sang# memory.h: ARM has a custom one
4467840a94cSWolfram Sang
44766b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
44866b47b4aSKees Cookmy $misspellings;
44966b47b4aSKees Cookmy %spelling_fix;
45036061e38SJoe Perches
45136061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
45236061e38SJoe Perches	my @spelling_list;
45366b47b4aSKees Cook	while (<$spelling>) {
45466b47b4aSKees Cook		my $line = $_;
45566b47b4aSKees Cook
45666b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
45766b47b4aSKees Cook		$line =~ s/^\s*//g;
45866b47b4aSKees Cook
45966b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
46066b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
46166b47b4aSKees Cook
46266b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
46366b47b4aSKees Cook
46466b47b4aSKees Cook		push(@spelling_list, $suspect);
46566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
46666b47b4aSKees Cook	}
46766b47b4aSKees Cook	close($spelling);
46866b47b4aSKees Cook	$misspellings = join("|", @spelling_list);
46936061e38SJoe Perches} else {
47036061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
47136061e38SJoe Perches}
47266b47b4aSKees Cook
4738905a67cSAndy Whitcroftsub build_types {
474d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
475d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4761813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
4778716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
478c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4798905a67cSAndy Whitcroft	$NonptrType	= qr{
480d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
481cf655043SAndy Whitcroft			(?:
4826b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
483021158b4SJoe Perches				(?:$typeOtherOSTypedefs\b)|
4848ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
485c45dcabdSAndy Whitcroft				(?:${all}\b)
486cf655043SAndy Whitcroft			)
487c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4888905a67cSAndy Whitcroft		  }x;
4891813087dSJoe Perches	$NonptrTypeMisordered	= qr{
4901813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
4911813087dSJoe Perches			(?:
4921813087dSJoe Perches				(?:${Misordered}\b)
4931813087dSJoe Perches			)
4941813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
4951813087dSJoe Perches		  }x;
4968716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4978716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4988716de38SJoe Perches			(?:
4998716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
5008716de38SJoe Perches				(?:$typeTypedefs\b)|
501021158b4SJoe Perches				(?:$typeOtherOSTypedefs\b)|
5028716de38SJoe Perches				(?:${allWithAttr}\b)
5038716de38SJoe Perches			)
5048716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
5058716de38SJoe Perches		  }x;
5068905a67cSAndy Whitcroft	$Type	= qr{
507c45dcabdSAndy Whitcroft			$NonptrType
5081574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
509c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
5108905a67cSAndy Whitcroft		  }x;
5111813087dSJoe Perches	$TypeMisordered	= qr{
5121813087dSJoe Perches			$NonptrTypeMisordered
5131813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5141813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5151813087dSJoe Perches		  }x;
51691cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5171813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5188905a67cSAndy Whitcroft}
5198905a67cSAndy Whitcroftbuild_types();
5206c72ffaaSAndy Whitcroft
5217d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
522d1fe9c09SJoe Perches
523d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
524d1fe9c09SJoe Perches# requires at least perl version v5.10.0
525d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
526d1fe9c09SJoe Perches
527d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
5282435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
529c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
5307d2367afSJoe Perches
531f8422308SJoe Perchesour $declaration_macros = qr{(?x:
532f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
533f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
534f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
535f8422308SJoe Perches)};
536f8422308SJoe Perches
5377d2367afSJoe Perchessub deparenthesize {
5387d2367afSJoe Perches	my ($string) = @_;
5397d2367afSJoe Perches	return "" if (!defined($string));
5405b9553abSJoe Perches
5415b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5425b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5435b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5445b9553abSJoe Perches	}
5455b9553abSJoe Perches
5467d2367afSJoe Perches	$string =~ s@\s+@ @g;
5475b9553abSJoe Perches
5487d2367afSJoe Perches	return $string;
5497d2367afSJoe Perches}
5507d2367afSJoe Perches
5513445686aSJoe Perchessub seed_camelcase_file {
5523445686aSJoe Perches	my ($file) = @_;
5533445686aSJoe Perches
5543445686aSJoe Perches	return if (!(-f $file));
5553445686aSJoe Perches
5563445686aSJoe Perches	local $/;
5573445686aSJoe Perches
5583445686aSJoe Perches	open(my $include_file, '<', "$file")
5593445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
5603445686aSJoe Perches	my $text = <$include_file>;
5613445686aSJoe Perches	close($include_file);
5623445686aSJoe Perches
5633445686aSJoe Perches	my @lines = split('\n', $text);
5643445686aSJoe Perches
5653445686aSJoe Perches	foreach my $line (@lines) {
5663445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
5673445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
5683445686aSJoe Perches			$camelcase{$1} = 1;
56911ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
57011ea516aSJoe Perches			$camelcase{$1} = 1;
57111ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
5723445686aSJoe Perches			$camelcase{$1} = 1;
5733445686aSJoe Perches		}
5743445686aSJoe Perches	}
5753445686aSJoe Perches}
5763445686aSJoe Perches
5773445686aSJoe Perchesmy $camelcase_seeded = 0;
5783445686aSJoe Perchessub seed_camelcase_includes {
5793445686aSJoe Perches	return if ($camelcase_seeded);
5803445686aSJoe Perches
5813445686aSJoe Perches	my $files;
582c707a81dSJoe Perches	my $camelcase_cache = "";
583c707a81dSJoe Perches	my @include_files = ();
584c707a81dSJoe Perches
585c707a81dSJoe Perches	$camelcase_seeded = 1;
586351b2a1fSJoe Perches
5873645e328SRichard Genoud	if (-e ".git") {
588351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
589351b2a1fSJoe Perches		chomp $git_last_include_commit;
590c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
591c707a81dSJoe Perches	} else {
592c707a81dSJoe Perches		my $last_mod_date = 0;
593c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
594c707a81dSJoe Perches		@include_files = split('\n', $files);
595c707a81dSJoe Perches		foreach my $file (@include_files) {
596c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
597c707a81dSJoe Perches						   localtime((stat $file)[9]));
598c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
599c707a81dSJoe Perches		}
600c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
601c707a81dSJoe Perches	}
602c707a81dSJoe Perches
603c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
604c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
605c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
606351b2a1fSJoe Perches		while (<$camelcase_file>) {
607351b2a1fSJoe Perches			chomp;
608351b2a1fSJoe Perches			$camelcase{$_} = 1;
609351b2a1fSJoe Perches		}
610351b2a1fSJoe Perches		close($camelcase_file);
611351b2a1fSJoe Perches
612351b2a1fSJoe Perches		return;
613351b2a1fSJoe Perches	}
614c707a81dSJoe Perches
6153645e328SRichard Genoud	if (-e ".git") {
616c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
617c707a81dSJoe Perches		@include_files = split('\n', $files);
6183445686aSJoe Perches	}
619c707a81dSJoe Perches
6203445686aSJoe Perches	foreach my $file (@include_files) {
6213445686aSJoe Perches		seed_camelcase_file($file);
6223445686aSJoe Perches	}
623351b2a1fSJoe Perches
624c707a81dSJoe Perches	if ($camelcase_cache ne "") {
625351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
626c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
627c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
628351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
629351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
630351b2a1fSJoe Perches		}
631351b2a1fSJoe Perches		close($camelcase_file);
632351b2a1fSJoe Perches	}
6333445686aSJoe Perches}
6343445686aSJoe Perches
635d311cd44SJoe Perchessub git_commit_info {
636d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
637d311cd44SJoe Perches
638d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
639d311cd44SJoe Perches
640d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
641d311cd44SJoe Perches	$output =~ s/^\s*//gm;
642d311cd44SJoe Perches	my @lines = split("\n", $output);
643d311cd44SJoe Perches
6440d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
6450d7835fcSJoe Perches
646d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
647d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
648d311cd44SJoe Perches# all matching commit ids, but it's very slow...
649d311cd44SJoe Perches#
650d311cd44SJoe Perches#		echo "checking commits $1..."
651d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
652d311cd44SJoe Perches#		while read line ; do
653d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
654d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
655d311cd44SJoe Perches#		done
656d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
657d311cd44SJoe Perches	} else {
658d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
659d311cd44SJoe Perches		$desc = substr($lines[0], 41);
660d311cd44SJoe Perches	}
661d311cd44SJoe Perches
662d311cd44SJoe Perches	return ($id, $desc);
663d311cd44SJoe Perches}
664d311cd44SJoe Perches
6656c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
6660a920b5bSAndy Whitcroft
66700df344fSAndy Whitcroftmy @rawlines = ();
668c2fdda0dSAndy Whitcroftmy @lines = ();
6693705ce5bSJoe Perchesmy @fixed = ();
670d752fcc8SJoe Perchesmy @fixed_inserted = ();
671d752fcc8SJoe Perchesmy @fixed_deleted = ();
672194f66fcSJoe Perchesmy $fixlinenr = -1;
673194f66fcSJoe Perches
674c2fdda0dSAndy Whitcroftmy $vname;
6756c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
67621caa13cSAndy Whitcroft	my $FILE;
6776c72ffaaSAndy Whitcroft	if ($file) {
67821caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
6796c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
68021caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
68121caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
6826c72ffaaSAndy Whitcroft	} else {
68321caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6846c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6856c72ffaaSAndy Whitcroft	}
686c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
687c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
688c2fdda0dSAndy Whitcroft	} else {
689c2fdda0dSAndy Whitcroft		$vname = $filename;
690c2fdda0dSAndy Whitcroft	}
69121caa13cSAndy Whitcroft	while (<$FILE>) {
6920a920b5bSAndy Whitcroft		chomp;
69300df344fSAndy Whitcroft		push(@rawlines, $_);
6946c72ffaaSAndy Whitcroft	}
69521caa13cSAndy Whitcroft	close($FILE);
696c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6970a920b5bSAndy Whitcroft		$exit = 1;
6980a920b5bSAndy Whitcroft	}
69900df344fSAndy Whitcroft	@rawlines = ();
70013214adfSAndy Whitcroft	@lines = ();
7013705ce5bSJoe Perches	@fixed = ();
702d752fcc8SJoe Perches	@fixed_inserted = ();
703d752fcc8SJoe Perches	@fixed_deleted = ();
704194f66fcSJoe Perches	$fixlinenr = -1;
7050a920b5bSAndy Whitcroft}
7060a920b5bSAndy Whitcroft
7070a920b5bSAndy Whitcroftexit($exit);
7080a920b5bSAndy Whitcroft
7090a920b5bSAndy Whitcroftsub top_of_kernel_tree {
7106c72ffaaSAndy Whitcroft	my ($root) = @_;
7116c72ffaaSAndy Whitcroft
7126c72ffaaSAndy Whitcroft	my @tree_check = (
7136c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
7146c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
7156c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
7166c72ffaaSAndy Whitcroft	);
7176c72ffaaSAndy Whitcroft
7186c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
7196c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
7200a920b5bSAndy Whitcroft			return 0;
7210a920b5bSAndy Whitcroft		}
7226c72ffaaSAndy Whitcroft	}
7236c72ffaaSAndy Whitcroft	return 1;
7246c72ffaaSAndy Whitcroft}
7250a920b5bSAndy Whitcroft
72620112475SJoe Perchessub parse_email {
72720112475SJoe Perches	my ($formatted_email) = @_;
72820112475SJoe Perches
72920112475SJoe Perches	my $name = "";
73020112475SJoe Perches	my $address = "";
73120112475SJoe Perches	my $comment = "";
73220112475SJoe Perches
73320112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
73420112475SJoe Perches		$name = $1;
73520112475SJoe Perches		$address = $2;
73620112475SJoe Perches		$comment = $3 if defined $3;
73720112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
73820112475SJoe Perches		$address = $1;
73920112475SJoe Perches		$comment = $2 if defined $2;
74020112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
74120112475SJoe Perches		$address = $1;
74220112475SJoe Perches		$comment = $2 if defined $2;
74320112475SJoe Perches		$formatted_email =~ s/$address.*$//;
74420112475SJoe Perches		$name = $formatted_email;
7453705ce5bSJoe Perches		$name = trim($name);
74620112475SJoe Perches		$name =~ s/^\"|\"$//g;
74720112475SJoe Perches		# If there's a name left after stripping spaces and
74820112475SJoe Perches		# leading quotes, and the address doesn't have both
74920112475SJoe Perches		# leading and trailing angle brackets, the address
75020112475SJoe Perches		# is invalid. ie:
75120112475SJoe Perches		#   "joe smith [email protected]" bad
75220112475SJoe Perches		#   "joe smith <[email protected]" bad
75320112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
75420112475SJoe Perches			$name = "";
75520112475SJoe Perches			$address = "";
75620112475SJoe Perches			$comment = "";
75720112475SJoe Perches		}
75820112475SJoe Perches	}
75920112475SJoe Perches
7603705ce5bSJoe Perches	$name = trim($name);
76120112475SJoe Perches	$name =~ s/^\"|\"$//g;
7623705ce5bSJoe Perches	$address = trim($address);
76320112475SJoe Perches	$address =~ s/^\<|\>$//g;
76420112475SJoe Perches
76520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
76620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
76720112475SJoe Perches		$name = "\"$name\"";
76820112475SJoe Perches	}
76920112475SJoe Perches
77020112475SJoe Perches	return ($name, $address, $comment);
77120112475SJoe Perches}
77220112475SJoe Perches
77320112475SJoe Perchessub format_email {
77420112475SJoe Perches	my ($name, $address) = @_;
77520112475SJoe Perches
77620112475SJoe Perches	my $formatted_email;
77720112475SJoe Perches
7783705ce5bSJoe Perches	$name = trim($name);
77920112475SJoe Perches	$name =~ s/^\"|\"$//g;
7803705ce5bSJoe Perches	$address = trim($address);
78120112475SJoe Perches
78220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
78320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
78420112475SJoe Perches		$name = "\"$name\"";
78520112475SJoe Perches	}
78620112475SJoe Perches
78720112475SJoe Perches	if ("$name" eq "") {
78820112475SJoe Perches		$formatted_email = "$address";
78920112475SJoe Perches	} else {
79020112475SJoe Perches		$formatted_email = "$name <$address>";
79120112475SJoe Perches	}
79220112475SJoe Perches
79320112475SJoe Perches	return $formatted_email;
79420112475SJoe Perches}
79520112475SJoe Perches
796d311cd44SJoe Perchessub which {
797d311cd44SJoe Perches	my ($bin) = @_;
798d311cd44SJoe Perches
799d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
800d311cd44SJoe Perches		if (-e "$path/$bin") {
801d311cd44SJoe Perches			return "$path/$bin";
802d311cd44SJoe Perches		}
803d311cd44SJoe Perches	}
804d311cd44SJoe Perches
805d311cd44SJoe Perches	return "";
806d311cd44SJoe Perches}
807d311cd44SJoe Perches
808000d1cc1SJoe Perchessub which_conf {
809000d1cc1SJoe Perches	my ($conf) = @_;
810000d1cc1SJoe Perches
811000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
812000d1cc1SJoe Perches		if (-e "$path/$conf") {
813000d1cc1SJoe Perches			return "$path/$conf";
814000d1cc1SJoe Perches		}
815000d1cc1SJoe Perches	}
816000d1cc1SJoe Perches
817000d1cc1SJoe Perches	return "";
818000d1cc1SJoe Perches}
819000d1cc1SJoe Perches
8200a920b5bSAndy Whitcroftsub expand_tabs {
8210a920b5bSAndy Whitcroft	my ($str) = @_;
8220a920b5bSAndy Whitcroft
8230a920b5bSAndy Whitcroft	my $res = '';
8240a920b5bSAndy Whitcroft	my $n = 0;
8250a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
8260a920b5bSAndy Whitcroft		if ($c eq "\t") {
8270a920b5bSAndy Whitcroft			$res .= ' ';
8280a920b5bSAndy Whitcroft			$n++;
8290a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
8300a920b5bSAndy Whitcroft				$res .= ' ';
8310a920b5bSAndy Whitcroft			}
8320a920b5bSAndy Whitcroft			next;
8330a920b5bSAndy Whitcroft		}
8340a920b5bSAndy Whitcroft		$res .= $c;
8350a920b5bSAndy Whitcroft		$n++;
8360a920b5bSAndy Whitcroft	}
8370a920b5bSAndy Whitcroft
8380a920b5bSAndy Whitcroft	return $res;
8390a920b5bSAndy Whitcroft}
8406c72ffaaSAndy Whitcroftsub copy_spacing {
841773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
8426c72ffaaSAndy Whitcroft	return $res;
8436c72ffaaSAndy Whitcroft}
8440a920b5bSAndy Whitcroft
8454a0df2efSAndy Whitcroftsub line_stats {
8464a0df2efSAndy Whitcroft	my ($line) = @_;
8474a0df2efSAndy Whitcroft
8484a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8494a0df2efSAndy Whitcroft	$line =~ s/^.//;
8504a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8514a0df2efSAndy Whitcroft
8524a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8534a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8544a0df2efSAndy Whitcroft
8554a0df2efSAndy Whitcroft	return (length($line), length($white));
8564a0df2efSAndy Whitcroft}
8574a0df2efSAndy Whitcroft
858773647a0SAndy Whitcroftmy $sanitise_quote = '';
859773647a0SAndy Whitcroft
860773647a0SAndy Whitcroftsub sanitise_line_reset {
861773647a0SAndy Whitcroft	my ($in_comment) = @_;
862773647a0SAndy Whitcroft
863773647a0SAndy Whitcroft	if ($in_comment) {
864773647a0SAndy Whitcroft		$sanitise_quote = '*/';
865773647a0SAndy Whitcroft	} else {
866773647a0SAndy Whitcroft		$sanitise_quote = '';
867773647a0SAndy Whitcroft	}
868773647a0SAndy Whitcroft}
86900df344fSAndy Whitcroftsub sanitise_line {
87000df344fSAndy Whitcroft	my ($line) = @_;
87100df344fSAndy Whitcroft
87200df344fSAndy Whitcroft	my $res = '';
87300df344fSAndy Whitcroft	my $l = '';
87400df344fSAndy Whitcroft
875c2fdda0dSAndy Whitcroft	my $qlen = 0;
876773647a0SAndy Whitcroft	my $off = 0;
877773647a0SAndy Whitcroft	my $c;
87800df344fSAndy Whitcroft
879773647a0SAndy Whitcroft	# Always copy over the diff marker.
880773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
881773647a0SAndy Whitcroft
882773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
883773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
884773647a0SAndy Whitcroft
885773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
886773647a0SAndy Whitcroft		# and end, all to $;.
887773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
888773647a0SAndy Whitcroft			$sanitise_quote = '*/';
889773647a0SAndy Whitcroft
890773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
891773647a0SAndy Whitcroft			$off++;
89200df344fSAndy Whitcroft			next;
893773647a0SAndy Whitcroft		}
89481bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
895773647a0SAndy Whitcroft			$sanitise_quote = '';
896773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
897773647a0SAndy Whitcroft			$off++;
898773647a0SAndy Whitcroft			next;
899773647a0SAndy Whitcroft		}
900113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
901113f04a8SDaniel Walker			$sanitise_quote = '//';
902113f04a8SDaniel Walker
903113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
904113f04a8SDaniel Walker			$off++;
905113f04a8SDaniel Walker			next;
906113f04a8SDaniel Walker		}
907773647a0SAndy Whitcroft
908773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
909773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
910773647a0SAndy Whitcroft		    $c eq "\\") {
911773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
912773647a0SAndy Whitcroft			$off++;
913773647a0SAndy Whitcroft			next;
914773647a0SAndy Whitcroft		}
915773647a0SAndy Whitcroft		# Regular quotes.
916773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
917773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
918773647a0SAndy Whitcroft				$sanitise_quote = $c;
919773647a0SAndy Whitcroft
920773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
921773647a0SAndy Whitcroft				next;
922773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
923773647a0SAndy Whitcroft				$sanitise_quote = '';
92400df344fSAndy Whitcroft			}
92500df344fSAndy Whitcroft		}
926773647a0SAndy Whitcroft
927fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
928773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
929773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
930113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
931113f04a8SDaniel Walker			substr($res, $off, 1, $;);
932773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
933773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
93400df344fSAndy Whitcroft		} else {
935773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
93600df344fSAndy Whitcroft		}
937c2fdda0dSAndy Whitcroft	}
938c2fdda0dSAndy Whitcroft
939113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
940113f04a8SDaniel Walker		$sanitise_quote = '';
941113f04a8SDaniel Walker	}
942113f04a8SDaniel Walker
943c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
944c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
945c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
946c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
947c2fdda0dSAndy Whitcroft
948c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
949c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
950c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
951c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
952c2fdda0dSAndy Whitcroft	}
953c2fdda0dSAndy Whitcroft
95400df344fSAndy Whitcroft	return $res;
95500df344fSAndy Whitcroft}
95600df344fSAndy Whitcroft
957a6962d72SJoe Perchessub get_quoted_string {
958a6962d72SJoe Perches	my ($line, $rawline) = @_;
959a6962d72SJoe Perches
9605e4f6ba5SJoe Perches	return "" if ($line !~ m/(\"[X\t]+\")/g);
961a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
962a6962d72SJoe Perches}
963a6962d72SJoe Perches
9648905a67cSAndy Whitcroftsub ctx_statement_block {
9658905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9668905a67cSAndy Whitcroft	my $line = $linenr - 1;
9678905a67cSAndy Whitcroft	my $blk = '';
9688905a67cSAndy Whitcroft	my $soff = $off;
9698905a67cSAndy Whitcroft	my $coff = $off - 1;
970773647a0SAndy Whitcroft	my $coff_set = 0;
9718905a67cSAndy Whitcroft
97213214adfSAndy Whitcroft	my $loff = 0;
97313214adfSAndy Whitcroft
9748905a67cSAndy Whitcroft	my $type = '';
9758905a67cSAndy Whitcroft	my $level = 0;
976a2750645SAndy Whitcroft	my @stack = ();
977cf655043SAndy Whitcroft	my $p;
9788905a67cSAndy Whitcroft	my $c;
9798905a67cSAndy Whitcroft	my $len = 0;
98013214adfSAndy Whitcroft
98113214adfSAndy Whitcroft	my $remainder;
9828905a67cSAndy Whitcroft	while (1) {
983a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
984a2750645SAndy Whitcroft
985773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9868905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9878905a67cSAndy Whitcroft		# context.
9888905a67cSAndy Whitcroft		if ($off >= $len) {
9898905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
990dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
991c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9928905a67cSAndy Whitcroft				$remain--;
99313214adfSAndy Whitcroft				$loff = $len;
994c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9958905a67cSAndy Whitcroft				$len = length($blk);
9968905a67cSAndy Whitcroft				$line++;
9978905a67cSAndy Whitcroft				last;
9988905a67cSAndy Whitcroft			}
9998905a67cSAndy Whitcroft			# Bail if there is no further context.
10008905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
100113214adfSAndy Whitcroft			if ($off >= $len) {
10028905a67cSAndy Whitcroft				last;
10038905a67cSAndy Whitcroft			}
1004f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1005f74bd194SAndy Whitcroft				$level++;
1006f74bd194SAndy Whitcroft				$type = '#';
1007f74bd194SAndy Whitcroft			}
10088905a67cSAndy Whitcroft		}
1009cf655043SAndy Whitcroft		$p = $c;
10108905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
101113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
10128905a67cSAndy Whitcroft
1013773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
10144635f4fbSAndy Whitcroft
10154635f4fbSAndy Whitcroft		# Handle nested #if/#else.
10164635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
10174635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
10184635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
10194635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
10204635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
10214635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
10224635f4fbSAndy Whitcroft		}
10234635f4fbSAndy Whitcroft
10248905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
10258905a67cSAndy Whitcroft		# outermost level.
10268905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
10278905a67cSAndy Whitcroft			last;
10288905a67cSAndy Whitcroft		}
10298905a67cSAndy Whitcroft
103013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1031773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1032773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1033773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1034773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1035773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1036773647a0SAndy Whitcroft			$coff_set = 1;
1037773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1038773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
103913214adfSAndy Whitcroft		}
104013214adfSAndy Whitcroft
10418905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
10428905a67cSAndy Whitcroft			$level++;
10438905a67cSAndy Whitcroft			$type = '(';
10448905a67cSAndy Whitcroft		}
10458905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10468905a67cSAndy Whitcroft			$level--;
10478905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10488905a67cSAndy Whitcroft
10498905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10508905a67cSAndy Whitcroft				$coff = $off;
1051773647a0SAndy Whitcroft				$coff_set = 1;
1052773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10538905a67cSAndy Whitcroft			}
10548905a67cSAndy Whitcroft		}
10558905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
10568905a67cSAndy Whitcroft			$level++;
10578905a67cSAndy Whitcroft			$type = '{';
10588905a67cSAndy Whitcroft		}
10598905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
10608905a67cSAndy Whitcroft			$level--;
10618905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
10628905a67cSAndy Whitcroft
10638905a67cSAndy Whitcroft			if ($level == 0) {
1064b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1065b998e001SPatrick Pannuto					$off++;
1066b998e001SPatrick Pannuto				}
10678905a67cSAndy Whitcroft				last;
10688905a67cSAndy Whitcroft			}
10698905a67cSAndy Whitcroft		}
1070f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1071f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1072f74bd194SAndy Whitcroft			$level--;
1073f74bd194SAndy Whitcroft			$type = '';
1074f74bd194SAndy Whitcroft			$off++;
1075f74bd194SAndy Whitcroft			last;
1076f74bd194SAndy Whitcroft		}
10778905a67cSAndy Whitcroft		$off++;
10788905a67cSAndy Whitcroft	}
1079a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
108013214adfSAndy Whitcroft	if ($off == $len) {
1081a3bb97a7SAndy Whitcroft		$loff = $len + 1;
108213214adfSAndy Whitcroft		$line++;
108313214adfSAndy Whitcroft		$remain--;
108413214adfSAndy Whitcroft	}
10858905a67cSAndy Whitcroft
10868905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10878905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10888905a67cSAndy Whitcroft
10898905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10908905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10918905a67cSAndy Whitcroft
1092773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
109313214adfSAndy Whitcroft
109413214adfSAndy Whitcroft	return ($statement, $condition,
109513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
109613214adfSAndy Whitcroft}
109713214adfSAndy Whitcroft
1098cf655043SAndy Whitcroftsub statement_lines {
1099cf655043SAndy Whitcroft	my ($stmt) = @_;
1100cf655043SAndy Whitcroft
1101cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1102cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1103cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1104cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1105cf655043SAndy Whitcroft
1106cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1107cf655043SAndy Whitcroft
1108cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1109cf655043SAndy Whitcroft}
1110cf655043SAndy Whitcroft
1111cf655043SAndy Whitcroftsub statement_rawlines {
1112cf655043SAndy Whitcroft	my ($stmt) = @_;
1113cf655043SAndy Whitcroft
1114cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1115cf655043SAndy Whitcroft
1116cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1117cf655043SAndy Whitcroft}
1118cf655043SAndy Whitcroft
1119cf655043SAndy Whitcroftsub statement_block_size {
1120cf655043SAndy Whitcroft	my ($stmt) = @_;
1121cf655043SAndy Whitcroft
1122cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1123cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1124cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1125cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1126cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1127cf655043SAndy Whitcroft
1128cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1129cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1130cf655043SAndy Whitcroft
1131cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1132cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1133cf655043SAndy Whitcroft
1134cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1135cf655043SAndy Whitcroft		return $stmt_lines;
1136cf655043SAndy Whitcroft	} else {
1137cf655043SAndy Whitcroft		return $stmt_statements;
1138cf655043SAndy Whitcroft	}
1139cf655043SAndy Whitcroft}
1140cf655043SAndy Whitcroft
114113214adfSAndy Whitcroftsub ctx_statement_full {
114213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
114313214adfSAndy Whitcroft	my ($statement, $condition, $level);
114413214adfSAndy Whitcroft
114513214adfSAndy Whitcroft	my (@chunks);
114613214adfSAndy Whitcroft
1147cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
114813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
114913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1150773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
115113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1152cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1153cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1154cf655043SAndy Whitcroft	}
1155cf655043SAndy Whitcroft
1156cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1157cf655043SAndy Whitcroft	# could continue the statement.
1158cf655043SAndy Whitcroft	for (;;) {
115913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
116013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1161cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1162773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1163cf655043SAndy Whitcroft		#print "C: push\n";
1164cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
116513214adfSAndy Whitcroft	}
116613214adfSAndy Whitcroft
116713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
11688905a67cSAndy Whitcroft}
11698905a67cSAndy Whitcroft
11704a0df2efSAndy Whitcroftsub ctx_block_get {
1171f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
11724a0df2efSAndy Whitcroft	my $line;
11734a0df2efSAndy Whitcroft	my $start = $linenr - 1;
11744a0df2efSAndy Whitcroft	my $blk = '';
11754a0df2efSAndy Whitcroft	my @o;
11764a0df2efSAndy Whitcroft	my @c;
11774a0df2efSAndy Whitcroft	my @res = ();
11784a0df2efSAndy Whitcroft
1179f0a594c1SAndy Whitcroft	my $level = 0;
11804635f4fbSAndy Whitcroft	my @stack = ($level);
118100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
118200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
118300df344fSAndy Whitcroft		$remain--;
118400df344fSAndy Whitcroft
118500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11864635f4fbSAndy Whitcroft
11874635f4fbSAndy Whitcroft		# Handle nested #if/#else.
118801464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11894635f4fbSAndy Whitcroft			push(@stack, $level);
119001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11914635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
119201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11934635f4fbSAndy Whitcroft			$level = pop(@stack);
11944635f4fbSAndy Whitcroft		}
11954635f4fbSAndy Whitcroft
119601464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1197f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1198f0a594c1SAndy Whitcroft			if ($off > 0) {
1199f0a594c1SAndy Whitcroft				$off--;
1200f0a594c1SAndy Whitcroft				next;
1201f0a594c1SAndy Whitcroft			}
12024a0df2efSAndy Whitcroft
1203f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1204f0a594c1SAndy Whitcroft				$level--;
1205f0a594c1SAndy Whitcroft				last if ($level == 0);
1206f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1207f0a594c1SAndy Whitcroft				$level++;
1208f0a594c1SAndy Whitcroft			}
1209f0a594c1SAndy Whitcroft		}
12104a0df2efSAndy Whitcroft
1211f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
121200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
12134a0df2efSAndy Whitcroft		}
12144a0df2efSAndy Whitcroft
1215f0a594c1SAndy Whitcroft		last if ($level == 0);
12164a0df2efSAndy Whitcroft	}
12174a0df2efSAndy Whitcroft
1218f0a594c1SAndy Whitcroft	return ($level, @res);
12194a0df2efSAndy Whitcroft}
12204a0df2efSAndy Whitcroftsub ctx_block_outer {
12214a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12224a0df2efSAndy Whitcroft
1223f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1224f0a594c1SAndy Whitcroft	return @r;
12254a0df2efSAndy Whitcroft}
12264a0df2efSAndy Whitcroftsub ctx_block {
12274a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12284a0df2efSAndy Whitcroft
1229f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1230f0a594c1SAndy Whitcroft	return @r;
1231653d4876SAndy Whitcroft}
1232653d4876SAndy Whitcroftsub ctx_statement {
1233f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1234f0a594c1SAndy Whitcroft
1235f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1236f0a594c1SAndy Whitcroft	return @r;
1237f0a594c1SAndy Whitcroft}
1238f0a594c1SAndy Whitcroftsub ctx_block_level {
1239653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1240653d4876SAndy Whitcroft
1241f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
12424a0df2efSAndy Whitcroft}
12439c0ca6f9SAndy Whitcroftsub ctx_statement_level {
12449c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12459c0ca6f9SAndy Whitcroft
12469c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12479c0ca6f9SAndy Whitcroft}
12484a0df2efSAndy Whitcroft
12494a0df2efSAndy Whitcroftsub ctx_locate_comment {
12504a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12514a0df2efSAndy Whitcroft
12524a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1253beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12544a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12554a0df2efSAndy Whitcroft
12564a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
12574a0df2efSAndy Whitcroft	# comment.
12584a0df2efSAndy Whitcroft	my $in_comment = 0;
12594a0df2efSAndy Whitcroft	$current_comment = '';
12604a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
126100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
126200df344fSAndy Whitcroft		#warn "           $line\n";
12634a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
12644a0df2efSAndy Whitcroft			$in_comment = 1;
12654a0df2efSAndy Whitcroft		}
12664a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
12674a0df2efSAndy Whitcroft			$in_comment = 1;
12684a0df2efSAndy Whitcroft		}
12694a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
12704a0df2efSAndy Whitcroft			$current_comment = '';
12714a0df2efSAndy Whitcroft		}
12724a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
12734a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
12744a0df2efSAndy Whitcroft			$in_comment = 0;
12754a0df2efSAndy Whitcroft		}
12764a0df2efSAndy Whitcroft	}
12774a0df2efSAndy Whitcroft
12784a0df2efSAndy Whitcroft	chomp($current_comment);
12794a0df2efSAndy Whitcroft	return($current_comment);
12804a0df2efSAndy Whitcroft}
12814a0df2efSAndy Whitcroftsub ctx_has_comment {
12824a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12834a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12844a0df2efSAndy Whitcroft
128500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12864a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12874a0df2efSAndy Whitcroft
12884a0df2efSAndy Whitcroft	return ($cmt ne '');
12894a0df2efSAndy Whitcroft}
12904a0df2efSAndy Whitcroft
12914d001e4dSAndy Whitcroftsub raw_line {
12924d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12934d001e4dSAndy Whitcroft
12944d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12954d001e4dSAndy Whitcroft	$cnt++;
12964d001e4dSAndy Whitcroft
12974d001e4dSAndy Whitcroft	my $line;
12984d001e4dSAndy Whitcroft	while ($cnt) {
12994d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
13004d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
13014d001e4dSAndy Whitcroft		$cnt--;
13024d001e4dSAndy Whitcroft	}
13034d001e4dSAndy Whitcroft
13044d001e4dSAndy Whitcroft	return $line;
13054d001e4dSAndy Whitcroft}
13064d001e4dSAndy Whitcroft
13070a920b5bSAndy Whitcroftsub cat_vet {
13080a920b5bSAndy Whitcroft	my ($vet) = @_;
13099c0ca6f9SAndy Whitcroft	my ($res, $coded);
13100a920b5bSAndy Whitcroft
13119c0ca6f9SAndy Whitcroft	$res = '';
13126c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
13136c72ffaaSAndy Whitcroft		$res .= $1;
13146c72ffaaSAndy Whitcroft		if ($2 ne '') {
13159c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
13166c72ffaaSAndy Whitcroft			$res .= $coded;
13176c72ffaaSAndy Whitcroft		}
13189c0ca6f9SAndy Whitcroft	}
13199c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
13200a920b5bSAndy Whitcroft
13219c0ca6f9SAndy Whitcroft	return $res;
13220a920b5bSAndy Whitcroft}
13230a920b5bSAndy Whitcroft
1324c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1325cf655043SAndy Whitcroftmy $av_pending;
1326c2fdda0dSAndy Whitcroftmy @av_paren_type;
13271f65f947SAndy Whitcroftmy $av_pend_colon;
1328c2fdda0dSAndy Whitcroft
1329c2fdda0dSAndy Whitcroftsub annotate_reset {
1330c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1331cf655043SAndy Whitcroft	$av_pending = '_';
1332cf655043SAndy Whitcroft	@av_paren_type = ('E');
13331f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1334c2fdda0dSAndy Whitcroft}
1335c2fdda0dSAndy Whitcroft
13366c72ffaaSAndy Whitcroftsub annotate_values {
13376c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
13386c72ffaaSAndy Whitcroft
13396c72ffaaSAndy Whitcroft	my $res;
13401f65f947SAndy Whitcroft	my $var = '_' x length($stream);
13416c72ffaaSAndy Whitcroft	my $cur = $stream;
13426c72ffaaSAndy Whitcroft
1343c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
13446c72ffaaSAndy Whitcroft
13456c72ffaaSAndy Whitcroft	while (length($cur)) {
1346773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1347cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1348171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13496c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1350c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1351c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1352cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1353c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13546c72ffaaSAndy Whitcroft			}
13556c72ffaaSAndy Whitcroft
1356c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
13579446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
13589446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1359addcdceaSAndy Whitcroft			$type = 'c';
13609446ef56SAndy Whitcroft
1361e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1362c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
13636c72ffaaSAndy Whitcroft			$type = 'T';
13646c72ffaaSAndy Whitcroft
1365389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1366389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1367389a2fe5SAndy Whitcroft			$type = 'T';
1368389a2fe5SAndy Whitcroft
1369c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1370171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1371c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1372171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1373171ae1a4SAndy Whitcroft			if ($2 ne '') {
1374cf655043SAndy Whitcroft				$av_pending = 'N';
1375171ae1a4SAndy Whitcroft			}
1376171ae1a4SAndy Whitcroft			$type = 'E';
1377171ae1a4SAndy Whitcroft
1378c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1379171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1380171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1381171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
13826c72ffaaSAndy Whitcroft
1383c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1384cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1385c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1386cf655043SAndy Whitcroft
1387cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1388cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1389171ae1a4SAndy Whitcroft			$type = 'E';
1390cf655043SAndy Whitcroft
1391c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1392cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1393cf655043SAndy Whitcroft			$av_preprocessor = 1;
1394cf655043SAndy Whitcroft
1395cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1396cf655043SAndy Whitcroft
1397171ae1a4SAndy Whitcroft			$type = 'E';
1398cf655043SAndy Whitcroft
1399c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1400cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1401cf655043SAndy Whitcroft
1402cf655043SAndy Whitcroft			$av_preprocessor = 1;
1403cf655043SAndy Whitcroft
1404cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1405cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1406cf655043SAndy Whitcroft			pop(@av_paren_type);
1407cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1408171ae1a4SAndy Whitcroft			$type = 'E';
14096c72ffaaSAndy Whitcroft
14106c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1411c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
14126c72ffaaSAndy Whitcroft
1413171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1414171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1415171ae1a4SAndy Whitcroft			$av_pending = $type;
1416171ae1a4SAndy Whitcroft			$type = 'N';
1417171ae1a4SAndy Whitcroft
14186c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1419c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
14206c72ffaaSAndy Whitcroft			if (defined $2) {
1421cf655043SAndy Whitcroft				$av_pending = 'V';
14226c72ffaaSAndy Whitcroft			}
14236c72ffaaSAndy Whitcroft			$type = 'N';
14246c72ffaaSAndy Whitcroft
142514b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1426c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
142714b111c1SAndy Whitcroft			$av_pending = 'E';
14286c72ffaaSAndy Whitcroft			$type = 'N';
14296c72ffaaSAndy Whitcroft
14301f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
14311f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
14321f65f947SAndy Whitcroft			$av_pend_colon = 'C';
14331f65f947SAndy Whitcroft			$type = 'N';
14341f65f947SAndy Whitcroft
143514b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1436c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
14376c72ffaaSAndy Whitcroft			$type = 'N';
14386c72ffaaSAndy Whitcroft
14396c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1440c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1441cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1442cf655043SAndy Whitcroft			$av_pending = '_';
14436c72ffaaSAndy Whitcroft			$type = 'N';
14446c72ffaaSAndy Whitcroft
14456c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1446cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1447cf655043SAndy Whitcroft			if ($new_type ne '_') {
1448cf655043SAndy Whitcroft				$type = $new_type;
1449c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1450c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14516c72ffaaSAndy Whitcroft			} else {
1452c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14536c72ffaaSAndy Whitcroft			}
14546c72ffaaSAndy Whitcroft
1455c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1456c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1457c8cb2ca3SAndy Whitcroft			$type = 'V';
1458cf655043SAndy Whitcroft			$av_pending = 'V';
14596c72ffaaSAndy Whitcroft
14608e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
14618e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
14621f65f947SAndy Whitcroft				$av_pend_colon = 'B';
14638e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
14648e761b04SAndy Whitcroft				$av_pend_colon = 'L';
14651f65f947SAndy Whitcroft			}
14661f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
14671f65f947SAndy Whitcroft			$type = 'V';
14681f65f947SAndy Whitcroft
14696c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1470c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
14716c72ffaaSAndy Whitcroft			$type = 'V';
14726c72ffaaSAndy Whitcroft
14736c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1474c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
14756c72ffaaSAndy Whitcroft			$type = 'N';
14766c72ffaaSAndy Whitcroft
1477cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1478c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
147913214adfSAndy Whitcroft			$type = 'E';
14801f65f947SAndy Whitcroft			$av_pend_colon = 'O';
148113214adfSAndy Whitcroft
14828e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
14838e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14848e761b04SAndy Whitcroft			$type = 'C';
14858e761b04SAndy Whitcroft
14861f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14871f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14881f65f947SAndy Whitcroft			$type = 'N';
14891f65f947SAndy Whitcroft
14901f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14911f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14921f65f947SAndy Whitcroft
14931f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14941f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14951f65f947SAndy Whitcroft				$type = 'E';
14961f65f947SAndy Whitcroft			} else {
14971f65f947SAndy Whitcroft				$type = 'N';
14981f65f947SAndy Whitcroft			}
14991f65f947SAndy Whitcroft			$av_pend_colon = 'O';
15001f65f947SAndy Whitcroft
15018e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
150213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
15036c72ffaaSAndy Whitcroft			$type = 'N';
15046c72ffaaSAndy Whitcroft
15050d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
150674048ed8SAndy Whitcroft			my $variant;
150774048ed8SAndy Whitcroft
150874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
150974048ed8SAndy Whitcroft			if ($type eq 'V') {
151074048ed8SAndy Whitcroft				$variant = 'B';
151174048ed8SAndy Whitcroft			} else {
151274048ed8SAndy Whitcroft				$variant = 'U';
151374048ed8SAndy Whitcroft			}
151474048ed8SAndy Whitcroft
151574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
151674048ed8SAndy Whitcroft			$type = 'N';
151774048ed8SAndy Whitcroft
15186c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1519c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
15206c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
15216c72ffaaSAndy Whitcroft				$type = 'N';
15226c72ffaaSAndy Whitcroft			}
15236c72ffaaSAndy Whitcroft
15246c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1525c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
15266c72ffaaSAndy Whitcroft		}
15276c72ffaaSAndy Whitcroft		if (defined $1) {
15286c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
15296c72ffaaSAndy Whitcroft			$res .= $type x length($1);
15306c72ffaaSAndy Whitcroft		}
15316c72ffaaSAndy Whitcroft	}
15326c72ffaaSAndy Whitcroft
15331f65f947SAndy Whitcroft	return ($res, $var);
15346c72ffaaSAndy Whitcroft}
15356c72ffaaSAndy Whitcroft
15368905a67cSAndy Whitcroftsub possible {
153713214adfSAndy Whitcroft	my ($possible, $line) = @_;
15389a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
15390776e594SAndy Whitcroft		^(?:
15400776e594SAndy Whitcroft			$Modifier|
15410776e594SAndy Whitcroft			$Storage|
15420776e594SAndy Whitcroft			$Type|
15439a974fdbSAndy Whitcroft			DEFINE_\S+
15449a974fdbSAndy Whitcroft		)$|
15459a974fdbSAndy Whitcroft		^(?:
15460776e594SAndy Whitcroft			goto|
15470776e594SAndy Whitcroft			return|
15480776e594SAndy Whitcroft			case|
15490776e594SAndy Whitcroft			else|
15500776e594SAndy Whitcroft			asm|__asm__|
155189a88353SAndy Whitcroft			do|
155289a88353SAndy Whitcroft			\#|
155389a88353SAndy Whitcroft			\#\#|
15549a974fdbSAndy Whitcroft		)(?:\s|$)|
15550776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
15569a974fdbSAndy Whitcroft	    )}x;
15579a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
15589a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1559c45dcabdSAndy Whitcroft		# Check for modifiers.
1560c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1561c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1562c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1563c45dcabdSAndy Whitcroft
1564c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1565c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1566d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
15679a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1568d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1569d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1570d2506586SAndy Whitcroft				}
15719a974fdbSAndy Whitcroft			}
1572c45dcabdSAndy Whitcroft
1573c45dcabdSAndy Whitcroft		} else {
157413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
15758905a67cSAndy Whitcroft			push(@typeList, $possible);
1576c45dcabdSAndy Whitcroft		}
15778905a67cSAndy Whitcroft		build_types();
15780776e594SAndy Whitcroft	} else {
15790776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
15808905a67cSAndy Whitcroft	}
15818905a67cSAndy Whitcroft}
15828905a67cSAndy Whitcroft
15836c72ffaaSAndy Whitcroftmy $prefix = '';
15846c72ffaaSAndy Whitcroft
1585000d1cc1SJoe Perchessub show_type {
1586cbec18afSJoe Perches	my ($type) = @_;
158791bfe484SJoe Perches
1588cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1589cbec18afSJoe Perches
1590cbec18afSJoe Perches	return !defined $ignore_type{$type};
1591000d1cc1SJoe Perches}
1592000d1cc1SJoe Perches
1593f0a594c1SAndy Whitcroftsub report {
1594cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1595cbec18afSJoe Perches
1596cbec18afSJoe Perches	if (!show_type($type) ||
1597cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1598773647a0SAndy Whitcroft		return 0;
1599773647a0SAndy Whitcroft	}
1600000d1cc1SJoe Perches	my $line;
1601000d1cc1SJoe Perches	if ($show_types) {
1602cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1603000d1cc1SJoe Perches	} else {
1604cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1605000d1cc1SJoe Perches	}
16068905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
16078905a67cSAndy Whitcroft
160813214adfSAndy Whitcroft	push(our @report, $line);
1609773647a0SAndy Whitcroft
1610773647a0SAndy Whitcroft	return 1;
1611f0a594c1SAndy Whitcroft}
1612cbec18afSJoe Perches
1613f0a594c1SAndy Whitcroftsub report_dump {
161413214adfSAndy Whitcroft	our @report;
1615f0a594c1SAndy Whitcroft}
1616000d1cc1SJoe Perches
1617d752fcc8SJoe Perchessub fixup_current_range {
1618d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1619d752fcc8SJoe Perches
1620d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1621d752fcc8SJoe Perches		my $o = $1;
1622d752fcc8SJoe Perches		my $l = $2;
1623d752fcc8SJoe Perches		my $no = $o + $offset;
1624d752fcc8SJoe Perches		my $nl = $l + $length;
1625d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1626d752fcc8SJoe Perches	}
1627d752fcc8SJoe Perches}
1628d752fcc8SJoe Perches
1629d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1630d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1631d752fcc8SJoe Perches
1632d752fcc8SJoe Perches	my $range_last_linenr = 0;
1633d752fcc8SJoe Perches	my $delta_offset = 0;
1634d752fcc8SJoe Perches
1635d752fcc8SJoe Perches	my $old_linenr = 0;
1636d752fcc8SJoe Perches	my $new_linenr = 0;
1637d752fcc8SJoe Perches
1638d752fcc8SJoe Perches	my $next_insert = 0;
1639d752fcc8SJoe Perches	my $next_delete = 0;
1640d752fcc8SJoe Perches
1641d752fcc8SJoe Perches	my @lines = ();
1642d752fcc8SJoe Perches
1643d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1644d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1645d752fcc8SJoe Perches
1646d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1647d752fcc8SJoe Perches		my $save_line = 1;
1648d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1649d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1650d752fcc8SJoe Perches			$delta_offset = 0;
1651d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1652d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1653d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1654d752fcc8SJoe Perches		}
1655d752fcc8SJoe Perches
1656d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1657d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1658d752fcc8SJoe Perches			$save_line = 0;
1659d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1660d752fcc8SJoe Perches		}
1661d752fcc8SJoe Perches
1662d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1663d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1664d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1665d752fcc8SJoe Perches			$new_linenr++;
1666d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1667d752fcc8SJoe Perches		}
1668d752fcc8SJoe Perches
1669d752fcc8SJoe Perches		if ($save_line) {
1670d752fcc8SJoe Perches			push(@lines, $line);
1671d752fcc8SJoe Perches			$new_linenr++;
1672d752fcc8SJoe Perches		}
1673d752fcc8SJoe Perches
1674d752fcc8SJoe Perches		$old_linenr++;
1675d752fcc8SJoe Perches	}
1676d752fcc8SJoe Perches
1677d752fcc8SJoe Perches	return @lines;
1678d752fcc8SJoe Perches}
1679d752fcc8SJoe Perches
1680f2d7e4d4SJoe Perchessub fix_insert_line {
1681f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1682f2d7e4d4SJoe Perches
1683f2d7e4d4SJoe Perches	my $inserted = {
1684f2d7e4d4SJoe Perches		LINENR => $linenr,
1685f2d7e4d4SJoe Perches		LINE => $line,
1686f2d7e4d4SJoe Perches	};
1687f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1688f2d7e4d4SJoe Perches}
1689f2d7e4d4SJoe Perches
1690f2d7e4d4SJoe Perchessub fix_delete_line {
1691f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1692f2d7e4d4SJoe Perches
1693f2d7e4d4SJoe Perches	my $deleted = {
1694f2d7e4d4SJoe Perches		LINENR => $linenr,
1695f2d7e4d4SJoe Perches		LINE => $line,
1696f2d7e4d4SJoe Perches	};
1697f2d7e4d4SJoe Perches
1698f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1699f2d7e4d4SJoe Perches}
1700f2d7e4d4SJoe Perches
1701de7d4f0eSAndy Whitcroftsub ERROR {
1702cbec18afSJoe Perches	my ($type, $msg) = @_;
1703cbec18afSJoe Perches
1704cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1705de7d4f0eSAndy Whitcroft		our $clean = 0;
17066c72ffaaSAndy Whitcroft		our $cnt_error++;
17073705ce5bSJoe Perches		return 1;
1708de7d4f0eSAndy Whitcroft	}
17093705ce5bSJoe Perches	return 0;
1710773647a0SAndy Whitcroft}
1711de7d4f0eSAndy Whitcroftsub WARN {
1712cbec18afSJoe Perches	my ($type, $msg) = @_;
1713cbec18afSJoe Perches
1714cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1715de7d4f0eSAndy Whitcroft		our $clean = 0;
17166c72ffaaSAndy Whitcroft		our $cnt_warn++;
17173705ce5bSJoe Perches		return 1;
1718de7d4f0eSAndy Whitcroft	}
17193705ce5bSJoe Perches	return 0;
1720773647a0SAndy Whitcroft}
1721de7d4f0eSAndy Whitcroftsub CHK {
1722cbec18afSJoe Perches	my ($type, $msg) = @_;
1723cbec18afSJoe Perches
1724cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1725de7d4f0eSAndy Whitcroft		our $clean = 0;
17266c72ffaaSAndy Whitcroft		our $cnt_chk++;
17273705ce5bSJoe Perches		return 1;
17286c72ffaaSAndy Whitcroft	}
17293705ce5bSJoe Perches	return 0;
1730de7d4f0eSAndy Whitcroft}
1731de7d4f0eSAndy Whitcroft
17326ecd9674SAndy Whitcroftsub check_absolute_file {
17336ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
17346ecd9674SAndy Whitcroft	my $file = $absolute;
17356ecd9674SAndy Whitcroft
17366ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
17376ecd9674SAndy Whitcroft
17386ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
17396ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
17406ecd9674SAndy Whitcroft		if (-f "$root/$file") {
17416ecd9674SAndy Whitcroft			##print "file<$file>\n";
17426ecd9674SAndy Whitcroft			last;
17436ecd9674SAndy Whitcroft		}
17446ecd9674SAndy Whitcroft	}
17456ecd9674SAndy Whitcroft	if (! -f _)  {
17466ecd9674SAndy Whitcroft		return 0;
17476ecd9674SAndy Whitcroft	}
17486ecd9674SAndy Whitcroft
17496ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17506ecd9674SAndy Whitcroft	my $prefix = $absolute;
17516ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17526ecd9674SAndy Whitcroft
17536ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17546ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1755000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1756000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
17576ecd9674SAndy Whitcroft	}
17586ecd9674SAndy Whitcroft}
17596ecd9674SAndy Whitcroft
17603705ce5bSJoe Perchessub trim {
17613705ce5bSJoe Perches	my ($string) = @_;
17623705ce5bSJoe Perches
1763b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1764b34c648bSJoe Perches
1765b34c648bSJoe Perches	return $string;
1766b34c648bSJoe Perches}
1767b34c648bSJoe Perches
1768b34c648bSJoe Perchessub ltrim {
1769b34c648bSJoe Perches	my ($string) = @_;
1770b34c648bSJoe Perches
1771b34c648bSJoe Perches	$string =~ s/^\s+//;
1772b34c648bSJoe Perches
1773b34c648bSJoe Perches	return $string;
1774b34c648bSJoe Perches}
1775b34c648bSJoe Perches
1776b34c648bSJoe Perchessub rtrim {
1777b34c648bSJoe Perches	my ($string) = @_;
1778b34c648bSJoe Perches
1779b34c648bSJoe Perches	$string =~ s/\s+$//;
17803705ce5bSJoe Perches
17813705ce5bSJoe Perches	return $string;
17823705ce5bSJoe Perches}
17833705ce5bSJoe Perches
178452ea8506SJoe Perchessub string_find_replace {
178552ea8506SJoe Perches	my ($string, $find, $replace) = @_;
178652ea8506SJoe Perches
178752ea8506SJoe Perches	$string =~ s/$find/$replace/g;
178852ea8506SJoe Perches
178952ea8506SJoe Perches	return $string;
179052ea8506SJoe Perches}
179152ea8506SJoe Perches
17923705ce5bSJoe Perchessub tabify {
17933705ce5bSJoe Perches	my ($leading) = @_;
17943705ce5bSJoe Perches
17953705ce5bSJoe Perches	my $source_indent = 8;
17963705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
17973705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
17983705ce5bSJoe Perches
17993705ce5bSJoe Perches	#convert leading spaces to tabs
18003705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
18013705ce5bSJoe Perches	#Remove spaces before a tab
18023705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
18033705ce5bSJoe Perches
18043705ce5bSJoe Perches	return "$leading";
18053705ce5bSJoe Perches}
18063705ce5bSJoe Perches
1807d1fe9c09SJoe Perchessub pos_last_openparen {
1808d1fe9c09SJoe Perches	my ($line) = @_;
1809d1fe9c09SJoe Perches
1810d1fe9c09SJoe Perches	my $pos = 0;
1811d1fe9c09SJoe Perches
1812d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1813d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1814d1fe9c09SJoe Perches
1815d1fe9c09SJoe Perches	my $last_openparen = 0;
1816d1fe9c09SJoe Perches
1817d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1818d1fe9c09SJoe Perches		return -1;
1819d1fe9c09SJoe Perches	}
1820d1fe9c09SJoe Perches
1821d1fe9c09SJoe Perches	my $len = length($line);
1822d1fe9c09SJoe Perches
1823d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1824d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1825d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1826d1fe9c09SJoe Perches			$pos += length($1) - 1;
1827d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1828d1fe9c09SJoe Perches			$last_openparen = $pos;
1829d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1830d1fe9c09SJoe Perches			last;
1831d1fe9c09SJoe Perches		}
1832d1fe9c09SJoe Perches	}
1833d1fe9c09SJoe Perches
183491cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1835d1fe9c09SJoe Perches}
1836d1fe9c09SJoe Perches
18370a920b5bSAndy Whitcroftsub process {
18380a920b5bSAndy Whitcroft	my $filename = shift;
18390a920b5bSAndy Whitcroft
18400a920b5bSAndy Whitcroft	my $linenr=0;
18410a920b5bSAndy Whitcroft	my $prevline="";
1842c2fdda0dSAndy Whitcroft	my $prevrawline="";
18430a920b5bSAndy Whitcroft	my $stashline="";
1844c2fdda0dSAndy Whitcroft	my $stashrawline="";
18450a920b5bSAndy Whitcroft
18464a0df2efSAndy Whitcroft	my $length;
18470a920b5bSAndy Whitcroft	my $indent;
18480a920b5bSAndy Whitcroft	my $previndent=0;
18490a920b5bSAndy Whitcroft	my $stashindent=0;
18500a920b5bSAndy Whitcroft
1851de7d4f0eSAndy Whitcroft	our $clean = 1;
18520a920b5bSAndy Whitcroft	my $signoff = 0;
18530a920b5bSAndy Whitcroft	my $is_patch = 0;
18540a920b5bSAndy Whitcroft
185529ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
185615662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
185713f1937eSJoe Perches	my $reported_maintainer_file = 0;
1858fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1859fa64205dSPasi Savanainen
1860365dd4eaSJoe Perches	my $last_blank_line = 0;
18615e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
1862365dd4eaSJoe Perches
186313214adfSAndy Whitcroft	our @report = ();
18646c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
18656c72ffaaSAndy Whitcroft	our $cnt_error = 0;
18666c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
18676c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
18686c72ffaaSAndy Whitcroft
18690a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
18700a920b5bSAndy Whitcroft	my $realfile = '';
18710a920b5bSAndy Whitcroft	my $realline = 0;
18720a920b5bSAndy Whitcroft	my $realcnt = 0;
18730a920b5bSAndy Whitcroft	my $here = '';
18740a920b5bSAndy Whitcroft	my $in_comment = 0;
1875c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
18760a920b5bSAndy Whitcroft	my $first_line = 0;
18771e855726SWolfram Sang	my $p1_prefix = '';
18780a920b5bSAndy Whitcroft
187913214adfSAndy Whitcroft	my $prev_values = 'E';
188013214adfSAndy Whitcroft
188113214adfSAndy Whitcroft	# suppression flags
1882773647a0SAndy Whitcroft	my %suppress_ifbraces;
1883170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
18842b474a1aSAndy Whitcroft	my %suppress_export;
18853e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1886653d4876SAndy Whitcroft
18877e51f197SJoe Perches	my %signatures = ();
1888323c1260SJoe Perches
1889c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1890de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1891c2fdda0dSAndy Whitcroft	#
1892de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1893de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1894773647a0SAndy Whitcroft
1895d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1896d8b07710SJoe Perches
1897773647a0SAndy Whitcroft	sanitise_line_reset();
1898c2fdda0dSAndy Whitcroft	my $line;
1899c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1900773647a0SAndy Whitcroft		$linenr++;
1901773647a0SAndy Whitcroft		$line = $rawline;
1902c2fdda0dSAndy Whitcroft
19033705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
19043705ce5bSJoe Perches
1905773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1906de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1907de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1908de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1909de7d4f0eSAndy Whitcroft			}
1910773647a0SAndy Whitcroft			#next;
1911de7d4f0eSAndy Whitcroft		}
1912773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1913773647a0SAndy Whitcroft			$realline=$1-1;
1914773647a0SAndy Whitcroft			if (defined $2) {
1915773647a0SAndy Whitcroft				$realcnt=$3+1;
1916773647a0SAndy Whitcroft			} else {
1917773647a0SAndy Whitcroft				$realcnt=1+1;
1918773647a0SAndy Whitcroft			}
1919c45dcabdSAndy Whitcroft			$in_comment = 0;
1920773647a0SAndy Whitcroft
1921773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1922773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1923773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1924773647a0SAndy Whitcroft			# at context start.
1925773647a0SAndy Whitcroft			my $edge;
192601fa9147SAndy Whitcroft			my $cnt = $realcnt;
192701fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
192801fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
192901fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
193001fa9147SAndy Whitcroft				$cnt--;
193101fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1932721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1933fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1934fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1935fae17daeSAndy Whitcroft					($edge) = $1;
1936fae17daeSAndy Whitcroft					last;
1937fae17daeSAndy Whitcroft				}
1938773647a0SAndy Whitcroft			}
1939773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1940773647a0SAndy Whitcroft				$in_comment = 1;
1941773647a0SAndy Whitcroft			}
1942773647a0SAndy Whitcroft
1943773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1944773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1945773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1946773647a0SAndy Whitcroft			if (!defined $edge &&
194783242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1948773647a0SAndy Whitcroft			{
1949773647a0SAndy Whitcroft				$in_comment = 1;
1950773647a0SAndy Whitcroft			}
1951773647a0SAndy Whitcroft
1952773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1953773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1954773647a0SAndy Whitcroft
1955171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1956773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1957171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1958773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1959773647a0SAndy Whitcroft		}
1960773647a0SAndy Whitcroft		push(@lines, $line);
1961773647a0SAndy Whitcroft
1962773647a0SAndy Whitcroft		if ($realcnt > 1) {
1963773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1964773647a0SAndy Whitcroft		} else {
1965773647a0SAndy Whitcroft			$realcnt = 0;
1966773647a0SAndy Whitcroft		}
1967773647a0SAndy Whitcroft
1968773647a0SAndy Whitcroft		#print "==>$rawline\n";
1969773647a0SAndy Whitcroft		#print "-->$line\n";
1970de7d4f0eSAndy Whitcroft
1971de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1972de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1973de7d4f0eSAndy Whitcroft		}
1974de7d4f0eSAndy Whitcroft	}
1975de7d4f0eSAndy Whitcroft
19766c72ffaaSAndy Whitcroft	$prefix = '';
19776c72ffaaSAndy Whitcroft
1978773647a0SAndy Whitcroft	$realcnt = 0;
1979773647a0SAndy Whitcroft	$linenr = 0;
1980194f66fcSJoe Perches	$fixlinenr = -1;
19810a920b5bSAndy Whitcroft	foreach my $line (@lines) {
19820a920b5bSAndy Whitcroft		$linenr++;
1983194f66fcSJoe Perches		$fixlinenr++;
19841b5539b1SJoe Perches		my $sline = $line;	#copy of $line
19851b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
19860a920b5bSAndy Whitcroft
1987c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
19886c72ffaaSAndy Whitcroft
19890a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
19906c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
19910a920b5bSAndy Whitcroft			$is_patch = 1;
19924a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
19930a920b5bSAndy Whitcroft			$realline=$1-1;
19940a920b5bSAndy Whitcroft			if (defined $2) {
19950a920b5bSAndy Whitcroft				$realcnt=$3+1;
19960a920b5bSAndy Whitcroft			} else {
19970a920b5bSAndy Whitcroft				$realcnt=1+1;
19980a920b5bSAndy Whitcroft			}
1999c2fdda0dSAndy Whitcroft			annotate_reset();
200013214adfSAndy Whitcroft			$prev_values = 'E';
200113214adfSAndy Whitcroft
2002773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2003170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
20042b474a1aSAndy Whitcroft			%suppress_export = ();
20053e469cdcSAndy Whitcroft			$suppress_statement = 0;
20060a920b5bSAndy Whitcroft			next;
20070a920b5bSAndy Whitcroft
20084a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
20094a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
20104a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2011773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
20120a920b5bSAndy Whitcroft			$realline++;
2013d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
20140a920b5bSAndy Whitcroft
20154a0df2efSAndy Whitcroft			# Measure the line length and indent.
2016c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
20170a920b5bSAndy Whitcroft
20180a920b5bSAndy Whitcroft			# Track the previous line.
20190a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
20200a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2021c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2022c2fdda0dSAndy Whitcroft
2023773647a0SAndy Whitcroft			#warn "line<$line>\n";
20246c72ffaaSAndy Whitcroft
2025d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2026d8aaf121SAndy Whitcroft			$realcnt--;
20270a920b5bSAndy Whitcroft		}
20280a920b5bSAndy Whitcroft
2029cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2030cc77cdcaSAndy Whitcroft
20310a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
2032773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
2033773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
2034773647a0SAndy Whitcroft
20356c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
20366c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2037773647a0SAndy Whitcroft
20382ac73b4fSJoe Perches		my $found_file = 0;
2039773647a0SAndy Whitcroft		# extract the filename as it passes
20403bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
20413bf9a009SRabin Vincent			$realfile = $1;
20422b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2043270c49a0SJoe Perches			$in_commit_log = 0;
20442ac73b4fSJoe Perches			$found_file = 1;
20453bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2046773647a0SAndy Whitcroft			$realfile = $1;
20472b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2048270c49a0SJoe Perches			$in_commit_log = 0;
20491e855726SWolfram Sang
20501e855726SWolfram Sang			$p1_prefix = $1;
2051e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2052e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2053000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2054000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20551e855726SWolfram Sang			}
2056773647a0SAndy Whitcroft
2057c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2058000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2059000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2060773647a0SAndy Whitcroft			}
20612ac73b4fSJoe Perches			$found_file = 1;
20622ac73b4fSJoe Perches		}
20632ac73b4fSJoe Perches
20642ac73b4fSJoe Perches		if ($found_file) {
20652ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
20662ac73b4fSJoe Perches				$check = 1;
20672ac73b4fSJoe Perches			} else {
20682ac73b4fSJoe Perches				$check = $check_orig;
20692ac73b4fSJoe Perches			}
2070773647a0SAndy Whitcroft			next;
2071773647a0SAndy Whitcroft		}
2072773647a0SAndy Whitcroft
2073389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
20740a920b5bSAndy Whitcroft
2075c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2076c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2077c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
20780a920b5bSAndy Whitcroft
20796c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
20806c72ffaaSAndy Whitcroft
20813bf9a009SRabin Vincent# Check for incorrect file permissions
20823bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
20833bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
208404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
208504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2086000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2087000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
20883bf9a009SRabin Vincent			}
20893bf9a009SRabin Vincent		}
20903bf9a009SRabin Vincent
209120112475SJoe Perches# Check the patch for a signoff:
2092d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
20934a0df2efSAndy Whitcroft			$signoff++;
209415662b3eSJoe Perches			$in_commit_log = 0;
20950a920b5bSAndy Whitcroft		}
209620112475SJoe Perches
2097e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2098e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2099e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2100e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2101e0d975b1SJoe Perches		}
2102e0d975b1SJoe Perches
210320112475SJoe Perches# Check signature styles
2104270c49a0SJoe Perches		if (!$in_header_lines &&
2105ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
210620112475SJoe Perches			my $space_before = $1;
210720112475SJoe Perches			my $sign_off = $2;
210820112475SJoe Perches			my $space_after = $3;
210920112475SJoe Perches			my $email = $4;
211020112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
211120112475SJoe Perches
2112ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2113ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2114ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2115ce0338dfSJoe Perches			}
211620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
21173705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21183705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
21193705ce5bSJoe Perches				    $fix) {
2120194f66fcSJoe Perches					$fixed[$fixlinenr] =
21213705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21223705ce5bSJoe Perches				}
212320112475SJoe Perches			}
212420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
21253705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21263705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
21273705ce5bSJoe Perches				    $fix) {
2128194f66fcSJoe Perches					$fixed[$fixlinenr] =
21293705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21303705ce5bSJoe Perches				}
21313705ce5bSJoe Perches
213220112475SJoe Perches			}
213320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
21343705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21353705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
21363705ce5bSJoe Perches				    $fix) {
2137194f66fcSJoe Perches					$fixed[$fixlinenr] =
21383705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21393705ce5bSJoe Perches				}
214020112475SJoe Perches			}
214120112475SJoe Perches
214220112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
214320112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
214420112475SJoe Perches			if ($suggested_email eq "") {
2145000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2146000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
214720112475SJoe Perches			} else {
214820112475SJoe Perches				my $dequoted = $suggested_email;
214920112475SJoe Perches				$dequoted =~ s/^"//;
215020112475SJoe Perches				$dequoted =~ s/" </ </;
215120112475SJoe Perches				# Don't force email to have quotes
215220112475SJoe Perches				# Allow just an angle bracketed address
215320112475SJoe Perches				if ("$dequoted$comment" ne $email &&
215420112475SJoe Perches				    "<$email_address>$comment" ne $email &&
215520112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2156000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2157000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
215820112475SJoe Perches				}
21590a920b5bSAndy Whitcroft			}
21607e51f197SJoe Perches
21617e51f197SJoe Perches# Check for duplicate signatures
21627e51f197SJoe Perches			my $sig_nospace = $line;
21637e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
21647e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
21657e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
21667e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
21677e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
21687e51f197SJoe Perches			} else {
21697e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
21707e51f197SJoe Perches			}
21710a920b5bSAndy Whitcroft		}
21720a920b5bSAndy Whitcroft
21739b3189ebSJoe Perches# Check for old stable address
21749b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
21759b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
21769b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
21779b3189ebSJoe Perches		}
21789b3189ebSJoe Perches
21797ebd05efSChristopher Covington# Check for unwanted Gerrit info
21807ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
21817ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
21827ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
21837ebd05efSChristopher Covington		}
21847ebd05efSChristopher Covington
21850d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
21860d7835fcSJoe Perches		if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
2187d311cd44SJoe Perches			my $init_char = $1;
2188d311cd44SJoe Perches			my $orig_commit = lc($2);
21890d7835fcSJoe Perches			my $short = 1;
21900d7835fcSJoe Perches			my $long = 0;
21910d7835fcSJoe Perches			my $case = 1;
21920d7835fcSJoe Perches			my $space = 1;
21930d7835fcSJoe Perches			my $hasdesc = 0;
21940d7835fcSJoe Perches			my $id = '0123456789ab';
21950d7835fcSJoe Perches			my $orig_desc = "commit description";
21960d7835fcSJoe Perches			my $description = "";
21970d7835fcSJoe Perches
21980d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
21990d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
22000d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
22010d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
22020d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
22030d7835fcSJoe Perches				$orig_desc = $1;
22040d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
22050d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
22060d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
22070d7835fcSJoe Perches				$orig_desc = $1;
2208b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2209b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2210b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2211b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2212b671fde0SJoe Perches				$orig_desc = $1;
2213b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2214b671fde0SJoe Perches				$orig_desc .= " " . $1;
22150d7835fcSJoe Perches			}
22160d7835fcSJoe Perches
22170d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
22180d7835fcSJoe Perches							      $id, $orig_desc);
22190d7835fcSJoe Perches
22200d7835fcSJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description)) {
2221d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
22220d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
22230d7835fcSJoe Perches			}
2224d311cd44SJoe Perches		}
2225d311cd44SJoe Perches
222613f1937eSJoe Perches# Check for added, moved or deleted files
222713f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
222813f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
222913f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
223013f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
223113f1937eSJoe Perches		      (defined($1) || defined($2))))) {
223213f1937eSJoe Perches			$reported_maintainer_file = 1;
223313f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
223413f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
223513f1937eSJoe Perches		}
223613f1937eSJoe Perches
223700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
22388905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2239000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2240000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
22416c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2242de7d4f0eSAndy Whitcroft		}
2243de7d4f0eSAndy Whitcroft
22446ecd9674SAndy Whitcroft# Check for absolute kernel paths.
22456ecd9674SAndy Whitcroft		if ($tree) {
22466ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
22476ecd9674SAndy Whitcroft				my $file = $1;
22486ecd9674SAndy Whitcroft
22496ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
22506ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
22516ecd9674SAndy Whitcroft					#
22526ecd9674SAndy Whitcroft				} else {
22536ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
22546ecd9674SAndy Whitcroft				}
22556ecd9674SAndy Whitcroft			}
22566ecd9674SAndy Whitcroft		}
22576ecd9674SAndy Whitcroft
2258de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2259de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2260171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2261171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2262171ae1a4SAndy Whitcroft
2263171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2264171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2265171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2266171ae1a4SAndy Whitcroft
226734d99219SJoe Perches			CHK("INVALID_UTF8",
2268000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
226900df344fSAndy Whitcroft		}
22700a920b5bSAndy Whitcroft
227115662b3eSJoe Perches# Check if it's the start of a commit log
227215662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
227315662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
227429ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
227529ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
227615662b3eSJoe Perches			$in_header_lines = 0;
227715662b3eSJoe Perches			$in_commit_log = 1;
227815662b3eSJoe Perches		}
227915662b3eSJoe Perches
2280fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2281fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2282fa64205dSPasi Savanainen		if ($in_header_lines &&
2283fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2284fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2285fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2286fa64205dSPasi Savanainen		}
2287fa64205dSPasi Savanainen
2288fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
228915662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2290fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
229115662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
229215662b3eSJoe Perches		}
229315662b3eSJoe Perches
229466b47b4aSKees Cook# Check for various typo / spelling mistakes
229536061e38SJoe Perches		if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
229666b47b4aSKees Cook			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
229766b47b4aSKees Cook				my $typo = $1;
229866b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
229966b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
230066b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
230166b47b4aSKees Cook				my $msg_type = \&WARN;
230266b47b4aSKees Cook				$msg_type = \&CHK if ($file);
230366b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
230466b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
230566b47b4aSKees Cook				    $fix) {
230666b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
230766b47b4aSKees Cook				}
230866b47b4aSKees Cook			}
230966b47b4aSKees Cook		}
231066b47b4aSKees Cook
231130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
231230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
231300df344fSAndy Whitcroft
23140a920b5bSAndy Whitcroft#trailing whitespace
23159c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2316c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2317d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2318d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2319d5e616fcSJoe Perches			    $fix) {
2320194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2321d5e616fcSJoe Perches			}
2322c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2323c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23243705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
23253705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
23263705ce5bSJoe Perches			    $fix) {
2327194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
23283705ce5bSJoe Perches			}
23293705ce5bSJoe Perches
2330d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
23310a920b5bSAndy Whitcroft		}
23325368df20SAndy Whitcroft
23334783f894SJosh Triplett# Check for FSF mailing addresses.
2334109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
23353e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
23363e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
23374783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23384783f894SJosh Triplett			my $msg_type = \&ERROR;
23394783f894SJosh Triplett			$msg_type = \&CHK if ($file);
23404783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
23414783f894SJosh 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)
23424783f894SJosh Triplett		}
23434783f894SJosh Triplett
23443354957aSAndi Kleen# check for Kconfig help text having a real description
23459fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
23469fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
23473354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
23488d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
23493354957aSAndi Kleen			my $length = 0;
23509fe287d7SAndy Whitcroft			my $cnt = $realcnt;
23519fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
23529fe287d7SAndy Whitcroft			my $f;
2353a1385803SAndy Whitcroft			my $is_start = 0;
23549fe287d7SAndy Whitcroft			my $is_end = 0;
2355a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
23569fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
23579fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
23589fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
23599fe287d7SAndy Whitcroft
23609fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
23618d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2362a1385803SAndy Whitcroft
23638d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2364a1385803SAndy Whitcroft					$is_start = 1;
23658d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2366a1385803SAndy Whitcroft					$length = -1;
2367a1385803SAndy Whitcroft				}
2368a1385803SAndy Whitcroft
23699fe287d7SAndy Whitcroft				$f =~ s/^.//;
23703354957aSAndi Kleen				$f =~ s/#.*//;
23713354957aSAndi Kleen				$f =~ s/^\s+//;
23723354957aSAndi Kleen				next if ($f =~ /^$/);
23739fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
23749fe287d7SAndy Whitcroft					$is_end = 1;
23759fe287d7SAndy Whitcroft					last;
23769fe287d7SAndy Whitcroft				}
23773354957aSAndi Kleen				$length++;
23783354957aSAndi Kleen			}
237956193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2380000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
238156193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
238256193274SVadim Bendebury			}
2383a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
23843354957aSAndi Kleen		}
23853354957aSAndi Kleen
23861ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
23871ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
23881ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
23891ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
23901ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
23911ba8dfd1SKees Cook		}
23921ba8dfd1SKees Cook
2393327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2394327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2395327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2396327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2397327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2398327953e9SChristoph Jaeger		}
2399327953e9SChristoph Jaeger
2400c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2401c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2402c68e5878SArnaud Lacombe			my $flag = $1;
2403c68e5878SArnaud Lacombe			my $replacement = {
2404c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2405c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2406c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2407c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2408c68e5878SArnaud Lacombe			};
2409c68e5878SArnaud Lacombe
2410c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2411c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2412c68e5878SArnaud Lacombe		}
2413c68e5878SArnaud Lacombe
2414bff5da43SRob Herring# check for DT compatible documentation
24157dd05b38SFlorian Vaussard		if (defined $root &&
24167dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
24177dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
24187dd05b38SFlorian Vaussard
2419bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2420bff5da43SRob Herring
2421cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2422cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2423cc93319bSFlorian Vaussard
2424bff5da43SRob Herring			foreach my $compat (@compats) {
2425bff5da43SRob Herring				my $compat2 = $compat;
2426185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2427185d566bSRob Herring				my $compat3 = $compat;
2428185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2429185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2430bff5da43SRob Herring				if ( $? >> 8 ) {
2431bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2432bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2433bff5da43SRob Herring				}
2434bff5da43SRob Herring
24354fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
24364fbf32a6SFlorian Vaussard				my $vendor = $1;
2437cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2438bff5da43SRob Herring				if ( $? >> 8 ) {
2439bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2440cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2441bff5da43SRob Herring				}
2442bff5da43SRob Herring			}
2443bff5da43SRob Herring		}
2444bff5da43SRob Herring
24455368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2446de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
24475368df20SAndy Whitcroft
24486cd7f386SJoe Perches#line length limit
2449c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2450f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
24510fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
24528bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
24536cd7f386SJoe Perches		    $length > $max_line_length)
2454c45dcabdSAndy Whitcroft		{
2455000d1cc1SJoe Perches			WARN("LONG_LINE",
24566cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
24570a920b5bSAndy Whitcroft		}
24580a920b5bSAndy Whitcroft
24598905a67cSAndy Whitcroft# check for adding lines without a newline.
24608905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2461000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2462000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
24638905a67cSAndy Whitcroft		}
24648905a67cSAndy Whitcroft
246542e41c54SMike Frysinger# Blackfin: use hi/lo macros
246642e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
246742e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
246842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2469000d1cc1SJoe Perches				ERROR("LO_MACRO",
2470000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
247142e41c54SMike Frysinger			}
247242e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
247342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2474000d1cc1SJoe Perches				ERROR("HI_MACRO",
2475000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
247642e41c54SMike Frysinger			}
247742e41c54SMike Frysinger		}
247842e41c54SMike Frysinger
2479b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2480de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
24810a920b5bSAndy Whitcroft
24820a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
24830a920b5bSAndy Whitcroft# more than 8 must use tabs.
2484c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2485c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2486c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2487d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
24883705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
24893705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
24903705ce5bSJoe Perches			    $fix) {
2491194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
24923705ce5bSJoe Perches			}
24930a920b5bSAndy Whitcroft		}
24940a920b5bSAndy Whitcroft
249508e44365SAlberto Panizzo# check for space before tabs.
249608e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
249708e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24983705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
24993705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
25003705ce5bSJoe Perches			    $fix) {
2501194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2502d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2503194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2504c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
25053705ce5bSJoe Perches			}
250608e44365SAlberto Panizzo		}
250708e44365SAlberto Panizzo
2508d1fe9c09SJoe Perches# check for && or || at the start of a line
2509d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2510d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2511d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2512d1fe9c09SJoe Perches		}
2513d1fe9c09SJoe Perches
2514d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2515d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
251691cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2517d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2518d1fe9c09SJoe Perches			my $oldindent = $1;
2519d1fe9c09SJoe Perches			my $rest = $2;
2520d1fe9c09SJoe Perches
2521d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2522d1fe9c09SJoe Perches			if ($pos >= 0) {
2523b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2524b34a26f3SJoe Perches				my $newindent = $2;
2525d1fe9c09SJoe Perches
2526d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2527d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2528d1fe9c09SJoe Perches					" "  x ($pos % 8);
2529d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2530d1fe9c09SJoe Perches
2531d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2532d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
25333705ce5bSJoe Perches
25343705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
25353705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
25363705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2537194f66fcSJoe Perches						$fixed[$fixlinenr] =~
25383705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
25393705ce5bSJoe Perches					}
2540d1fe9c09SJoe Perches				}
2541d1fe9c09SJoe Perches			}
2542d1fe9c09SJoe Perches		}
2543d1fe9c09SJoe Perches
254443f7fe52SJoe Perches		if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;:\?\(\{\}\[\<\>]|&&|\|\||\\$)/ &&
254504941aa7SJoe Perches		    (!defined($1) || $1 !~ /sizeof\s*/)) {
25463705ce5bSJoe Perches			if (CHK("SPACING",
2547f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
25483705ce5bSJoe Perches			    $fix) {
2549194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2550f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
25513705ce5bSJoe Perches			}
2552aad4f614SJoe Perches		}
2553aad4f614SJoe Perches
255405880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2555fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
255685ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
255785ad978cSJoe Perches		    $realline > 2) {
255805880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
255905880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
256005880600SJoe Perches		}
256105880600SJoe Perches
256205880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2563a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2564a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
256561135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2566a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2567a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2568a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2569a605e32eSJoe Perches		}
2570a605e32eSJoe Perches
2571a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2572c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2573c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2574c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2575c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
257605880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
257705880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
257805880600SJoe Perches		}
257905880600SJoe Perches
25807f619191SJoe Perches# check for missing blank lines after struct/union declarations
25817f619191SJoe Perches# with exceptions for various attributes and macros
25827f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
25837f619191SJoe Perches		    $line =~ /^\+/ &&
25847f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
25857f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
25867f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
25877f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
25887f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
25897f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
25907f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
25917f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2592d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2593d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2594d752fcc8SJoe Perches			    $fix) {
2595f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2596d752fcc8SJoe Perches			}
25977f619191SJoe Perches		}
25987f619191SJoe Perches
2599365dd4eaSJoe Perches# check for multiple consecutive blank lines
2600365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2601365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2602365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2603d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2604d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2605d752fcc8SJoe Perches			    $fix) {
2606f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2607d752fcc8SJoe Perches			}
2608d752fcc8SJoe Perches
2609365dd4eaSJoe Perches			$last_blank_line = $linenr;
2610365dd4eaSJoe Perches		}
2611365dd4eaSJoe Perches
26123b617e3bSJoe Perches# check for missing blank lines after declarations
26133f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
26143f7bac03SJoe Perches			# actual declarations
26153f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26165a4e1fd3SJoe Perches			# function pointer declarations
26175a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26183f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26193f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26203f7bac03SJoe Perches			# known declaration macros
26213f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
26223f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
26233f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
26243f7bac03SJoe Perches			# other possible extensions of declaration lines
26253f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
26263f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
26273f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
26283f7bac03SJoe Perches			# looks like a declaration
26293f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26305a4e1fd3SJoe Perches			# function pointer declarations
26315a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26323f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26333f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26343f7bac03SJoe Perches			# known declaration macros
26353f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
26363f7bac03SJoe Perches			# start of struct or union or enum
26373b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
26383f7bac03SJoe Perches			# start or end of block or continuation of declaration
26393f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
26403f7bac03SJoe Perches			# bitfield continuation
26413f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
26423f7bac03SJoe Perches			# other possible extensions of declaration lines
26433f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
26443f7bac03SJoe Perches			# indentation of previous and current line are the same
26453f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2646d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2647d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2648d752fcc8SJoe Perches			    $fix) {
2649f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2650d752fcc8SJoe Perches			}
26513b617e3bSJoe Perches		}
26523b617e3bSJoe Perches
26535f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
26546b4c5bebSAndy Whitcroft# Exceptions:
26556b4c5bebSAndy Whitcroft#  1) within comments
26566b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
26576b4c5bebSAndy Whitcroft#  3) hanging labels
26583705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
26595f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26603705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
26613705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
26623705ce5bSJoe Perches			    $fix) {
2663194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
26643705ce5bSJoe Perches			}
26655f7ddae6SRaffaele Recalcati		}
26665f7ddae6SRaffaele Recalcati
2667b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2668b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2669b9ea10d6SAndy Whitcroft
2670032a4c0fSJoe Perches# check indentation of any line with a bare else
2671840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2672032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2673032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2674032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2675840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2676840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2677840080a0SJoe Perches			     defined $lines[$linenr] &&
2678840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2679032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2680032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2681032a4c0fSJoe Perches			}
2682032a4c0fSJoe Perches		}
2683032a4c0fSJoe Perches
2684c00df19aSJoe Perches# check indentation of a line with a break;
2685c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2686c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2687c00df19aSJoe Perches			my $tabs = $1;
2688c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2689c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2690c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2691c00df19aSJoe Perches			}
2692c00df19aSJoe Perches		}
2693c00df19aSJoe Perches
26941ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
26951ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
26961ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26971ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26981ba8dfd1SKees Cook		}
26991ba8dfd1SKees Cook
2700c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2701cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2702000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2703000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2704c2fdda0dSAndy Whitcroft		}
270522f2a2efSAndy Whitcroft
270642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
270742e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
270842e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2709000d1cc1SJoe Perches			ERROR("CSYNC",
2710000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
271142e41c54SMike Frysinger		}
271242e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
271342e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2714000d1cc1SJoe Perches			ERROR("SSYNC",
2715000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
271642e41c54SMike Frysinger		}
271742e41c54SMike Frysinger
271856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
271956e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
272056e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
272156e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
272256e77d70SJoe Perches		}
272356e77d70SJoe Perches
27249c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
27252b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
27262b474a1aSAndy Whitcroft		    $realline_next);
27273e469cdcSAndy Whitcroft#print "LINE<$line>\n";
27283e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
27291b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2730170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2731f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2732171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2733171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2734171ae1a4SAndy Whitcroft
27353e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
27363e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
27373e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
27383e469cdcSAndy Whitcroft			# until we hit end of it.
27393e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
27403e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
27413e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
27423e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
27433e469cdcSAndy Whitcroft			}
2744f74bd194SAndy Whitcroft
27452b474a1aSAndy Whitcroft			# Find the real next line.
27462b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
27472b474a1aSAndy Whitcroft			if (defined $realline_next &&
27482b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
27492b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
27502b474a1aSAndy Whitcroft				$realline_next++;
27512b474a1aSAndy Whitcroft			}
27522b474a1aSAndy Whitcroft
2753171ae1a4SAndy Whitcroft			my $s = $stat;
2754171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2755cf655043SAndy Whitcroft
2756c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2757171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2758c2fdda0dSAndy Whitcroft
2759c2fdda0dSAndy Whitcroft			# Ignore functions being called
2760171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2761c2fdda0dSAndy Whitcroft
2762463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2763463f2864SAndy Whitcroft
2764c45dcabdSAndy Whitcroft			# declarations always start with types
2765d2506586SAndy 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) {
2766c45dcabdSAndy Whitcroft				my $type = $1;
2767c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2768c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2769c45dcabdSAndy Whitcroft
27706c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2771a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2772c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2773c2fdda0dSAndy Whitcroft			}
27748905a67cSAndy Whitcroft
27756c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
277665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2777c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
27789c0ca6f9SAndy Whitcroft			}
27798905a67cSAndy Whitcroft
27808905a67cSAndy Whitcroft			# Check for any sort of function declaration.
27818905a67cSAndy Whitcroft			# int foo(something bar, other baz);
27828905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2783171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
27848905a67cSAndy Whitcroft				my ($name_len) = length($1);
27858905a67cSAndy Whitcroft
2786cf655043SAndy Whitcroft				my $ctx = $s;
2787773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
27888905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2789cf655043SAndy Whitcroft
27908905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2791c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
27928905a67cSAndy Whitcroft
2793c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
27948905a67cSAndy Whitcroft					}
27958905a67cSAndy Whitcroft				}
27968905a67cSAndy Whitcroft			}
27978905a67cSAndy Whitcroft
27989c0ca6f9SAndy Whitcroft		}
27999c0ca6f9SAndy Whitcroft
280000df344fSAndy Whitcroft#
280100df344fSAndy Whitcroft# Checks which may be anchored in the context.
280200df344fSAndy Whitcroft#
280300df344fSAndy Whitcroft
280400df344fSAndy Whitcroft# Check for switch () and associated case and default
280500df344fSAndy Whitcroft# statements should be at the same indent.
280600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
280700df344fSAndy Whitcroft			my $err = '';
280800df344fSAndy Whitcroft			my $sep = '';
280900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
281000df344fSAndy Whitcroft			shift(@ctx);
281100df344fSAndy Whitcroft			for my $ctx (@ctx) {
281200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
281300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
281400df344fSAndy Whitcroft							$indent != $cindent) {
281500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
281600df344fSAndy Whitcroft					$sep = '';
281700df344fSAndy Whitcroft				} else {
281800df344fSAndy Whitcroft					$sep = "[...]\n";
281900df344fSAndy Whitcroft				}
282000df344fSAndy Whitcroft			}
282100df344fSAndy Whitcroft			if ($err ne '') {
2822000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2823000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2824de7d4f0eSAndy Whitcroft			}
2825de7d4f0eSAndy Whitcroft		}
2826de7d4f0eSAndy Whitcroft
2827de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2828de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
28290fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2830773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2831773647a0SAndy Whitcroft
28329c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
28338eef05ddSJoe Perches
28348eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
28358eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
28368eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
28378eef05ddSJoe Perches			}
28388eef05ddSJoe Perches
2839de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2840de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2841de7d4f0eSAndy Whitcroft
2842548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2843548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2844de7d4f0eSAndy Whitcroft
2845548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2846548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2847548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2848548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2849548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2850773647a0SAndy Whitcroft				$ctx_ln++;
2851773647a0SAndy Whitcroft			}
2852548596d5SAndy Whitcroft
285353210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
285453210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2855773647a0SAndy Whitcroft
2856773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2857000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2858000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
285901464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
286000df344fSAndy Whitcroft			}
2861773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2862773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2863773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2864773647a0SAndy Whitcroft			{
28659c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
28669c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2867000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2868000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
286901464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
28709c0ca6f9SAndy Whitcroft				}
28719c0ca6f9SAndy Whitcroft			}
287200df344fSAndy Whitcroft		}
287300df344fSAndy Whitcroft
28744d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
28750fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
28763e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
28773e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
28783e469cdcSAndy Whitcroft					if (!defined $stat);
28794d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
28804d001e4dSAndy Whitcroft
28814d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
28824d001e4dSAndy Whitcroft
28834d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
28844d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
28854d001e4dSAndy Whitcroft			# where necessary.
28864d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
28874d001e4dSAndy Whitcroft
28884d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
28896f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
28906f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
28914d001e4dSAndy Whitcroft
28924d001e4dSAndy Whitcroft			# We want to check the first line inside the block
28934d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
28944d001e4dSAndy Whitcroft			#  1) any blank line termination
28954d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
28964d001e4dSAndy Whitcroft			#  3) any do (...) {
28974d001e4dSAndy Whitcroft			my $continuation = 0;
28984d001e4dSAndy Whitcroft			my $check = 0;
28994d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
29004d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
29014d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
29024d001e4dSAndy Whitcroft				$continuation = 1;
29034d001e4dSAndy Whitcroft			}
29049bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
29054d001e4dSAndy Whitcroft				$check = 1;
29064d001e4dSAndy Whitcroft				$cond_lines++;
29074d001e4dSAndy Whitcroft			}
29084d001e4dSAndy Whitcroft
29094d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
29104d001e4dSAndy Whitcroft			# preprocessor statement.
29114d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
29124d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
29134d001e4dSAndy Whitcroft				$check = 0;
29144d001e4dSAndy Whitcroft			}
29154d001e4dSAndy Whitcroft
29169bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2917740504c6SAndy Whitcroft			$continuation = 0;
29189bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
29199bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
29204d001e4dSAndy Whitcroft
2921f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2922f16fa28fSAndy Whitcroft				# is not linear.
2923f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2924f16fa28fSAndy Whitcroft					$check = 0;
2925f16fa28fSAndy Whitcroft				}
2926f16fa28fSAndy Whitcroft
29279bd49efeSAndy Whitcroft				# Ignore:
29289bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
29299bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
29309bd49efeSAndy Whitcroft				#  3) labels.
2931740504c6SAndy Whitcroft				if ($continuation ||
2932740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
29339bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
29349bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2935740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
293630dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
29379bd49efeSAndy Whitcroft						$cond_lines++;
29389bd49efeSAndy Whitcroft					}
29394d001e4dSAndy Whitcroft				}
294030dad6ebSAndy Whitcroft			}
29414d001e4dSAndy Whitcroft
29424d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
29434d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
29444d001e4dSAndy Whitcroft
29454d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
29464d001e4dSAndy Whitcroft			# this is not this patch's fault.
29474d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
29484d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
29494d001e4dSAndy Whitcroft				$check = 0;
29504d001e4dSAndy Whitcroft			}
29514d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
29524d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
29534d001e4dSAndy Whitcroft			}
29544d001e4dSAndy Whitcroft
29559bd49efeSAndy 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";
29564d001e4dSAndy Whitcroft
29574d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
29584d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2959000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2960000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
29614d001e4dSAndy Whitcroft			}
29624d001e4dSAndy Whitcroft		}
29634d001e4dSAndy Whitcroft
29646c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
29656c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
29661f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
29671f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
29686c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2969c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2970c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2971cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2972cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
29731f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2974c2fdda0dSAndy Whitcroft		}
29756c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
29766c72ffaaSAndy Whitcroft
297700df344fSAndy Whitcroft#ignore lines not being added
29783705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
297900df344fSAndy Whitcroft
2980653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
29817429c690SAndy Whitcroft		if ($dbg_type) {
29827429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2983000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2984000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
29857429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2986000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2987000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
29887429c690SAndy Whitcroft			}
2989653d4876SAndy Whitcroft			next;
2990653d4876SAndy Whitcroft		}
2991a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2992a1ef277eSAndy Whitcroft		if ($dbg_attr) {
29939360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2994000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2995000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
29969360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2997000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2998000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2999a1ef277eSAndy Whitcroft			}
3000a1ef277eSAndy Whitcroft			next;
3001a1ef277eSAndy Whitcroft		}
3002653d4876SAndy Whitcroft
3003f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
300499423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
300599423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3006d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3007d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3008f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3009f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3010f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3011d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3012d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3013f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3014d752fcc8SJoe Perches				$fixedline = $line;
3015d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3016f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3017d752fcc8SJoe Perches			}
3018f0a594c1SAndy Whitcroft		}
3019f0a594c1SAndy Whitcroft
302000df344fSAndy Whitcroft#
302100df344fSAndy Whitcroft# Checks which are anchored on the added line.
302200df344fSAndy Whitcroft#
302300df344fSAndy Whitcroft
3024653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3025c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3026653d4876SAndy Whitcroft			my $path = $1;
3027653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3028000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3029495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3030495e9d84SJoe Perches			}
3031495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3032495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3033495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3034653d4876SAndy Whitcroft			}
3035653d4876SAndy Whitcroft		}
3036653d4876SAndy Whitcroft
303700df344fSAndy Whitcroft# no C99 // comments
303800df344fSAndy Whitcroft		if ($line =~ m{//}) {
30393705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
30403705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
30413705ce5bSJoe Perches			    $fix) {
3042194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
30433705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
30443705ce5bSJoe Perches					my $comment = trim($1);
3045194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
30463705ce5bSJoe Perches				}
30473705ce5bSJoe Perches			}
304800df344fSAndy Whitcroft		}
304900df344fSAndy Whitcroft		# Remove C99 comments.
30500a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
30516c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
30520a920b5bSAndy Whitcroft
30532b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
30542b474a1aSAndy Whitcroft# the whole statement.
30552b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
30562b474a1aSAndy Whitcroft		if (defined $realline_next &&
30572b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
30582b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
30592b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30602b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30613cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
30623cbf62dfSAndy Whitcroft			# a prefix:
30633cbf62dfSAndy Whitcroft			#   XXX(foo);
30643cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3065653d4876SAndy Whitcroft			my $name = $1;
306687a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
30673cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
30683cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
30693cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30703cbf62dfSAndy Whitcroft
30713cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
30722b474a1aSAndy Whitcroft				\n.}\s*$|
307348012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
307448012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
307548012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
30762b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
30772b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
307848012058SAndy Whitcroft			    )/x) {
30792b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
30802b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
30812b474a1aSAndy Whitcroft			} else {
30822b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30830a920b5bSAndy Whitcroft			}
30840a920b5bSAndy Whitcroft		}
30852b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
30862b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
30872b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30882b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30892b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
30902b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
30912b474a1aSAndy Whitcroft		}
30922b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
30932b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3094000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3095000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
30962b474a1aSAndy Whitcroft		}
30970a920b5bSAndy Whitcroft
30985150bda4SJoe Eloff# check for global initialisers.
3099d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3100d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3101000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3102d5e616fcSJoe Perches				      $herecurr) &&
3103d5e616fcSJoe Perches			    $fix) {
3104194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3105d5e616fcSJoe Perches			}
3106f0a594c1SAndy Whitcroft		}
31070a920b5bSAndy Whitcroft# check for static initialisers.
3108d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3109d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3110000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3111d5e616fcSJoe Perches				      $herecurr) &&
3112d5e616fcSJoe Perches			    $fix) {
3113194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3114d5e616fcSJoe Perches			}
31150a920b5bSAndy Whitcroft		}
31160a920b5bSAndy Whitcroft
31171813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
31181813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
31191813087dSJoe Perches			my $tmp = trim($1);
31201813087dSJoe Perches			WARN("MISORDERED_TYPE",
31211813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
31221813087dSJoe Perches		}
31231813087dSJoe Perches
3124cb710ecaSJoe Perches# check for static const char * arrays.
3125cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3126000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3127000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3128cb710ecaSJoe Perches				$herecurr);
3129cb710ecaSJoe Perches               }
3130cb710ecaSJoe Perches
3131cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3132cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3133000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3134000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3135cb710ecaSJoe Perches				$herecurr);
3136cb710ecaSJoe Perches               }
3137cb710ecaSJoe Perches
31389b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
31399b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
31409b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
31419b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
31429b0fa60dSJoe Perches				$herecurr);
31439b0fa60dSJoe Perches               }
31449b0fa60dSJoe Perches
3145b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3146b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3147b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3148b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3149b36190c5SJoe Perches			    $fix) {
3150194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3151b36190c5SJoe Perches			}
3152b36190c5SJoe Perches		}
3153b36190c5SJoe Perches
315492e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
315592e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
315692e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
315792e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
315892e112fdSJoe Perches			    $fix) {
3159194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
316092e112fdSJoe Perches			}
316193ed0e2dSJoe Perches		}
316293ed0e2dSJoe Perches
3163653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3164653d4876SAndy Whitcroft# make sense.
3165653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
31668054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3167c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
31688ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3169021158b4SJoe Perches		    $line !~ /\b$typeOtherOSTypedefs\b/ &&
3170653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3171000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3172000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
31730a920b5bSAndy Whitcroft		}
31740a920b5bSAndy Whitcroft
31750a920b5bSAndy Whitcroft# * goes on variable not on type
317665863862SAndy Whitcroft		# (char*[ const])
3177bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3178bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
31793705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3180d8aaf121SAndy Whitcroft
318165863862SAndy Whitcroft			# Should start with a space.
318265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
318365863862SAndy Whitcroft			# Should not end with a space.
318465863862SAndy Whitcroft			$to =~ s/\s+$//;
318565863862SAndy Whitcroft			# '*'s should not have spaces between.
3186f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
318765863862SAndy Whitcroft			}
3188d8aaf121SAndy Whitcroft
31893705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
319065863862SAndy Whitcroft			if ($from ne $to) {
31913705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31923705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
31933705ce5bSJoe Perches				    $fix) {
31943705ce5bSJoe Perches					my $sub_from = $ident;
31953705ce5bSJoe Perches					my $sub_to = $ident;
31963705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3197194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31983705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31993705ce5bSJoe Perches				}
320065863862SAndy Whitcroft			}
3201bfcb2cc7SAndy Whitcroft		}
3202bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3203bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
32043705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3205d8aaf121SAndy Whitcroft
320665863862SAndy Whitcroft			# Should start with a space.
320765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
320865863862SAndy Whitcroft			# Should not end with a space.
320965863862SAndy Whitcroft			$to =~ s/\s+$//;
321065863862SAndy Whitcroft			# '*'s should not have spaces between.
3211f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
321265863862SAndy Whitcroft			}
321365863862SAndy Whitcroft			# Modifiers should have spaces.
321465863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
321565863862SAndy Whitcroft
32163705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3217667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
32183705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
32193705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
32203705ce5bSJoe Perches				    $fix) {
32213705ce5bSJoe Perches
32223705ce5bSJoe Perches					my $sub_from = $match;
32233705ce5bSJoe Perches					my $sub_to = $match;
32243705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3225194f66fcSJoe Perches					$fixed[$fixlinenr] =~
32263705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
32273705ce5bSJoe Perches				}
322865863862SAndy Whitcroft			}
32290a920b5bSAndy Whitcroft		}
32300a920b5bSAndy Whitcroft
32310a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
32320a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
32330a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
32340a920b5bSAndy Whitcroft# 			print "$herecurr";
32350a920b5bSAndy Whitcroft# 			$clean = 0;
32360a920b5bSAndy Whitcroft# 		}
32370a920b5bSAndy Whitcroft
32388905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3239000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3240000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
32418905a67cSAndy Whitcroft		}
32428905a67cSAndy Whitcroft
324317441227SJoe Perches# check for uses of printk_ratelimit
324417441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3245000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3246000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
324717441227SJoe Perches		}
324817441227SJoe Perches
324900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
325000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
325100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
325225985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
325300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3254f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
325500df344fSAndy Whitcroft			my $ok = 0;
325600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
325700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
325825985edcSLucas De Marchi				# we have a preceding printk if it ends
325900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
326000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
326100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
326200df344fSAndy Whitcroft						$ok = 1;
326300df344fSAndy Whitcroft					}
326400df344fSAndy Whitcroft					last;
326500df344fSAndy Whitcroft				}
326600df344fSAndy Whitcroft			}
326700df344fSAndy Whitcroft			if ($ok == 0) {
3268000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3269000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
32700a920b5bSAndy Whitcroft			}
327100df344fSAndy Whitcroft		}
32720a920b5bSAndy Whitcroft
3273243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3274243f3803SJoe Perches			my $orig = $1;
3275243f3803SJoe Perches			my $level = lc($orig);
3276243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
32778f26b837SJoe Perches			my $level2 = $level;
32788f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3279243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3280daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3281243f3803SJoe Perches		}
3282243f3803SJoe Perches
3283243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3284d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3285d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3286d5e616fcSJoe Perches			    $fix) {
3287194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3288d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3289d5e616fcSJoe Perches			}
3290243f3803SJoe Perches		}
3291243f3803SJoe Perches
3292dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3293dc139313SJoe Perches			my $orig = $1;
3294dc139313SJoe Perches			my $level = lc($orig);
3295dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3296dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3297dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3298dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3299dc139313SJoe Perches		}
3300dc139313SJoe Perches
3301653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3302653d4876SAndy Whitcroft# or if closed on same line
33038d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3304c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
33058d182478SJoe Perches			if (ERROR("OPEN_BRACE",
33068d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
33078d182478SJoe Perches			    $fix) {
33088d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
33098d182478SJoe Perches				my $fixed_line = $rawline;
33108d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
33118d182478SJoe Perches				my $line1 = $1;
33128d182478SJoe Perches				my $line2 = $2;
33138d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
33148d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
33158d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
33168d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
33178d182478SJoe Perches				}
33188d182478SJoe Perches			}
33190a920b5bSAndy Whitcroft		}
3320653d4876SAndy Whitcroft
33218905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
33228905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
33238905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
33248d182478SJoe Perches			if (ERROR("OPEN_BRACE",
33258d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
33268d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
33278d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
33288d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
33298d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
33308d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
33318d182478SJoe Perches				$fixedline = $rawline;
33328d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
33338d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
33348d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
33358d182478SJoe Perches				}
33368d182478SJoe Perches			}
33378905a67cSAndy Whitcroft		}
33388905a67cSAndy Whitcroft
33390c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
33403705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
33413705ce5bSJoe Perches			if (WARN("SPACING",
33423705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
33433705ce5bSJoe Perches			    $fix) {
3344194f66fcSJoe Perches				$fixed[$fixlinenr] =~
33453705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
33463705ce5bSJoe Perches			}
33470c73b4ebSAndy Whitcroft		}
33480c73b4ebSAndy Whitcroft
334931070b5dSJoe Perches# Function pointer declarations
335031070b5dSJoe Perches# check spacing between type, funcptr, and args
335131070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
335291f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
335331070b5dSJoe Perches			my $declare = $1;
335431070b5dSJoe Perches			my $pre_pointer_space = $2;
335531070b5dSJoe Perches			my $post_pointer_space = $3;
335631070b5dSJoe Perches			my $funcname = $4;
335731070b5dSJoe Perches			my $post_funcname_space = $5;
335831070b5dSJoe Perches			my $pre_args_space = $6;
335931070b5dSJoe Perches
336091f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
336191f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
336291f72e9cSJoe Perches# don't need a space so don't warn for those.
336391f72e9cSJoe Perches			my $post_declare_space = "";
336491f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
336591f72e9cSJoe Perches				$post_declare_space = $1;
336691f72e9cSJoe Perches				$declare = rtrim($declare);
336791f72e9cSJoe Perches			}
336891f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
336931070b5dSJoe Perches				WARN("SPACING",
337031070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
337191f72e9cSJoe Perches				$post_declare_space = " ";
337231070b5dSJoe Perches			}
337331070b5dSJoe Perches
337431070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
337591f72e9cSJoe Perches# This test is not currently implemented because these declarations are
337691f72e9cSJoe Perches# equivalent to
337791f72e9cSJoe Perches#	int  foo(int bar, ...)
337891f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
337991f72e9cSJoe Perches#
338091f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
338191f72e9cSJoe Perches#				WARN("SPACING",
338291f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
338391f72e9cSJoe Perches#			}
338431070b5dSJoe Perches
338531070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
338631070b5dSJoe Perches			if (defined $pre_pointer_space &&
338731070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
338831070b5dSJoe Perches				WARN("SPACING",
338931070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
339031070b5dSJoe Perches			}
339131070b5dSJoe Perches
339231070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
339331070b5dSJoe Perches			if (defined $post_pointer_space &&
339431070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
339531070b5dSJoe Perches				WARN("SPACING",
339631070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
339731070b5dSJoe Perches			}
339831070b5dSJoe Perches
339931070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
340031070b5dSJoe Perches			if (defined $post_funcname_space &&
340131070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
340231070b5dSJoe Perches				WARN("SPACING",
340331070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
340431070b5dSJoe Perches			}
340531070b5dSJoe Perches
340631070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
340731070b5dSJoe Perches			if (defined $pre_args_space &&
340831070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
340931070b5dSJoe Perches				WARN("SPACING",
341031070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
341131070b5dSJoe Perches			}
341231070b5dSJoe Perches
341331070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3414194f66fcSJoe Perches				$fixed[$fixlinenr] =~
341591f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
341631070b5dSJoe Perches			}
341731070b5dSJoe Perches		}
341831070b5dSJoe Perches
34198d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
34208d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3421fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3422fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
34238d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
34248d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
34258d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3426fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3427daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
34283705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
34293705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
34303705ce5bSJoe Perches				    $fix) {
3431194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
34323705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
34333705ce5bSJoe Perches				}
34348d31cfceSAndy Whitcroft			}
34358d31cfceSAndy Whitcroft		}
34368d31cfceSAndy Whitcroft
3437f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
34386c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3439c2fdda0dSAndy Whitcroft			my $name = $1;
3440773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3441773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3442c2fdda0dSAndy Whitcroft
3443c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3444773647a0SAndy Whitcroft			if ($name =~ /^(?:
3445773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3446773647a0SAndy Whitcroft				volatile|__volatile__|
3447773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3448773647a0SAndy Whitcroft				asm|__asm__)$/x)
3449773647a0SAndy Whitcroft			{
3450c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3451c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3452c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3453c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3454773647a0SAndy Whitcroft
3455773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3456c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3457c2fdda0dSAndy Whitcroft
3458c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3459c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3460773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3461c2fdda0dSAndy Whitcroft
3462c2fdda0dSAndy Whitcroft			} else {
34633705ce5bSJoe Perches				if (WARN("SPACING",
34643705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
34653705ce5bSJoe Perches					     $fix) {
3466194f66fcSJoe Perches					$fixed[$fixlinenr] =~
34673705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
34683705ce5bSJoe Perches				}
3469f0a594c1SAndy Whitcroft			}
34706c72ffaaSAndy Whitcroft		}
34719a4cad4eSEric Nelson
3472653d4876SAndy Whitcroft# Check operator spacing.
34730a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
34743705ce5bSJoe Perches			my $fixed_line = "";
34753705ce5bSJoe Perches			my $line_fixed = 0;
34763705ce5bSJoe Perches
34779c0ca6f9SAndy Whitcroft			my $ops = qr{
34789c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
34799c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
34809c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
34811f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
348284731623SJoe Perches				\?:|\?|:
34839c0ca6f9SAndy Whitcroft			}x;
3484cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
34853705ce5bSJoe Perches
34863705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
34873705ce5bSJoe Perches##			foreach my $el (@elements) {
34883705ce5bSJoe Perches##				print("el: <$el>\n");
34893705ce5bSJoe Perches##			}
34903705ce5bSJoe Perches
34913705ce5bSJoe Perches			my @fix_elements = ();
349200df344fSAndy Whitcroft			my $off = 0;
34936c72ffaaSAndy Whitcroft
34943705ce5bSJoe Perches			foreach my $el (@elements) {
34953705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
34963705ce5bSJoe Perches				$off += length($el);
34973705ce5bSJoe Perches			}
34983705ce5bSJoe Perches
34993705ce5bSJoe Perches			$off = 0;
35003705ce5bSJoe Perches
35016c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3502b34c648bSJoe Perches			my $last_after = -1;
35036c72ffaaSAndy Whitcroft
35040a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
35053705ce5bSJoe Perches
35063705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
35073705ce5bSJoe Perches
35083705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
35093705ce5bSJoe Perches
35104a0df2efSAndy Whitcroft				$off += length($elements[$n]);
35114a0df2efSAndy Whitcroft
351225985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3513773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3514773647a0SAndy Whitcroft				my $cc = '';
3515773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3516773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3517773647a0SAndy Whitcroft				}
3518773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3519773647a0SAndy Whitcroft
35204a0df2efSAndy Whitcroft				my $a = '';
35214a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
35224a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3523cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
35244a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
35254a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3526773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
35274a0df2efSAndy Whitcroft
35280a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
35294a0df2efSAndy Whitcroft
35304a0df2efSAndy Whitcroft				my $c = '';
35310a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
35324a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
35334a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3534cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
35354a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
35364a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
35378b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
35384a0df2efSAndy Whitcroft				} else {
35394a0df2efSAndy Whitcroft					$c = 'E';
35400a920b5bSAndy Whitcroft				}
35410a920b5bSAndy Whitcroft
35424a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
35434a0df2efSAndy Whitcroft
35444a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
35454a0df2efSAndy Whitcroft
35466c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3547de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
35480a920b5bSAndy Whitcroft
354974048ed8SAndy Whitcroft				# Pull out the value of this operator.
35506c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
35510a920b5bSAndy Whitcroft
35521f65f947SAndy Whitcroft				# Get the full operator variant.
35531f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
35541f65f947SAndy Whitcroft
355513214adfSAndy Whitcroft				# Ignore operators passed as parameters.
355613214adfSAndy Whitcroft				if ($op_type ne 'V' &&
355713214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
355813214adfSAndy Whitcroft
3559cf655043SAndy Whitcroft#				# Ignore comments
3560cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
356113214adfSAndy Whitcroft
3562d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
356313214adfSAndy Whitcroft				} elsif ($op eq ';') {
3564cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3565cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
35663705ce5bSJoe Perches						if (ERROR("SPACING",
35673705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3568b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35693705ce5bSJoe Perches							$line_fixed = 1;
35703705ce5bSJoe Perches						}
3571d8aaf121SAndy Whitcroft					}
3572d8aaf121SAndy Whitcroft
3573d8aaf121SAndy Whitcroft				# // is a comment
3574d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
35750a920b5bSAndy Whitcroft
3576b00e4814SJoe Perches				#   :   when part of a bitfield
3577b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3578b00e4814SJoe Perches					# skip the bitfield test for now
3579b00e4814SJoe Perches
35801f65f947SAndy Whitcroft				# No spaces for:
35811f65f947SAndy Whitcroft				#   ->
3582b00e4814SJoe Perches				} elsif ($op eq '->') {
35834a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
35843705ce5bSJoe Perches						if (ERROR("SPACING",
35853705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3586b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35873705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35883705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35893705ce5bSJoe Perches							}
3590b34c648bSJoe Perches							$line_fixed = 1;
35913705ce5bSJoe Perches						}
35920a920b5bSAndy Whitcroft					}
35930a920b5bSAndy Whitcroft
35942381097bSJoe Perches				# , must not have a space before and must have a space on the right.
35950a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
35962381097bSJoe Perches					my $rtrim_before = 0;
35972381097bSJoe Perches					my $space_after = 0;
35982381097bSJoe Perches					if ($ctx =~ /Wx./) {
35992381097bSJoe Perches						if (ERROR("SPACING",
36002381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
36012381097bSJoe Perches							$line_fixed = 1;
36022381097bSJoe Perches							$rtrim_before = 1;
36032381097bSJoe Perches						}
36042381097bSJoe Perches					}
3605cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
36063705ce5bSJoe Perches						if (ERROR("SPACING",
36073705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
36083705ce5bSJoe Perches							$line_fixed = 1;
3609b34c648bSJoe Perches							$last_after = $n;
36102381097bSJoe Perches							$space_after = 1;
36112381097bSJoe Perches						}
36122381097bSJoe Perches					}
36132381097bSJoe Perches					if ($rtrim_before || $space_after) {
36142381097bSJoe Perches						if ($rtrim_before) {
36152381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36162381097bSJoe Perches						} else {
36172381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
36182381097bSJoe Perches						}
36192381097bSJoe Perches						if ($space_after) {
36202381097bSJoe Perches							$good .= " ";
36213705ce5bSJoe Perches						}
36220a920b5bSAndy Whitcroft					}
36230a920b5bSAndy Whitcroft
36249c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
362574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
36269c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
36279c0ca6f9SAndy Whitcroft
36289c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
36299c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
36309c0ca6f9SAndy Whitcroft				# unary operator, or a cast
36319c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
363274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
36330d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3634cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
36353705ce5bSJoe Perches						if (ERROR("SPACING",
36363705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3637b34c648bSJoe Perches							if ($n != $last_after + 2) {
3638b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
36393705ce5bSJoe Perches								$line_fixed = 1;
36403705ce5bSJoe Perches							}
36410a920b5bSAndy Whitcroft						}
3642b34c648bSJoe Perches					}
3643a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3644171ae1a4SAndy Whitcroft						# A unary '*' may be const
3645171ae1a4SAndy Whitcroft
3646171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
36473705ce5bSJoe Perches						if (ERROR("SPACING",
36483705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3649b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
36503705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36513705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
36523705ce5bSJoe Perches							}
3653b34c648bSJoe Perches							$line_fixed = 1;
36543705ce5bSJoe Perches						}
36550a920b5bSAndy Whitcroft					}
36560a920b5bSAndy Whitcroft
36570a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
36580a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3659773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
36603705ce5bSJoe Perches						if (ERROR("SPACING",
36613705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3662b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
36633705ce5bSJoe Perches							$line_fixed = 1;
36643705ce5bSJoe Perches						}
36650a920b5bSAndy Whitcroft					}
3666773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3667773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
36683705ce5bSJoe Perches						if (ERROR("SPACING",
36693705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3670b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36713705ce5bSJoe Perches							$line_fixed = 1;
36723705ce5bSJoe Perches						}
3673653d4876SAndy Whitcroft					}
3674773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
36753705ce5bSJoe Perches						if (ERROR("SPACING",
36763705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3677b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
36783705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36793705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3680773647a0SAndy Whitcroft							}
3681b34c648bSJoe Perches							$line_fixed = 1;
36823705ce5bSJoe Perches						}
36833705ce5bSJoe Perches					}
36840a920b5bSAndy Whitcroft
36850a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
36869c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
36879c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
36889c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3689c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3690c2fdda0dSAndy Whitcroft					 $op eq '%')
36910a920b5bSAndy Whitcroft				{
3692*d2e025f3SJoe Perches					if ($check) {
3693*d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3694*d2e025f3SJoe Perches							if (CHK("SPACING",
3695*d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
3696*d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3697*d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3698*d2e025f3SJoe Perches								$line_fixed = 1;
3699*d2e025f3SJoe Perches							}
3700*d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3701*d2e025f3SJoe Perches							if (CHK("SPACING",
3702*d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
3703*d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3704*d2e025f3SJoe Perches								$line_fixed = 1;
3705*d2e025f3SJoe Perches							}
3706*d2e025f3SJoe Perches						}
3707*d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
37083705ce5bSJoe Perches						if (ERROR("SPACING",
37093705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3710b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3711b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3712b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3713b34c648bSJoe Perches							}
37143705ce5bSJoe Perches							$line_fixed = 1;
37153705ce5bSJoe Perches						}
37160a920b5bSAndy Whitcroft					}
37170a920b5bSAndy Whitcroft
37181f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
37191f65f947SAndy Whitcroft				# terminating a case value or a label.
37201f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
37211f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
37223705ce5bSJoe Perches						if (ERROR("SPACING",
37233705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3724b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
37253705ce5bSJoe Perches							$line_fixed = 1;
37263705ce5bSJoe Perches						}
37271f65f947SAndy Whitcroft					}
37281f65f947SAndy Whitcroft
37290a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3730cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
37311f65f947SAndy Whitcroft					my $ok = 0;
37321f65f947SAndy Whitcroft
373322f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
37341f65f947SAndy Whitcroft					if (($op eq '<' &&
37351f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
37361f65f947SAndy Whitcroft					    ($op eq '>' &&
37371f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
37381f65f947SAndy Whitcroft					{
37391f65f947SAndy Whitcroft					    	$ok = 1;
37401f65f947SAndy Whitcroft					}
37411f65f947SAndy Whitcroft
374284731623SJoe Perches					# messages are ERROR, but ?: are CHK
37431f65f947SAndy Whitcroft					if ($ok == 0) {
374484731623SJoe Perches						my $msg_type = \&ERROR;
374584731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
374684731623SJoe Perches
374784731623SJoe Perches						if (&{$msg_type}("SPACING",
37483705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3749b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3750b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3751b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3752b34c648bSJoe Perches							}
37533705ce5bSJoe Perches							$line_fixed = 1;
37543705ce5bSJoe Perches						}
37550a920b5bSAndy Whitcroft					}
375622f2a2efSAndy Whitcroft				}
37574a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
37583705ce5bSJoe Perches
37593705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
37603705ce5bSJoe Perches
37613705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
37620a920b5bSAndy Whitcroft			}
37633705ce5bSJoe Perches
37643705ce5bSJoe Perches			if (($#elements % 2) == 0) {
37653705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
37663705ce5bSJoe Perches			}
37673705ce5bSJoe Perches
3768194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3769194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
37703705ce5bSJoe Perches			}
37713705ce5bSJoe Perches
37723705ce5bSJoe Perches
37730a920b5bSAndy Whitcroft		}
37740a920b5bSAndy Whitcroft
3775786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3776d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3777786b6326SJoe Perches			if (WARN("SPACING",
3778786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3779786b6326SJoe Perches			    $fix) {
3780194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3781786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3782786b6326SJoe Perches			}
3783786b6326SJoe Perches		}
3784786b6326SJoe Perches
3785f0a594c1SAndy Whitcroft# check for multiple assignments
3786f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3787000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3788000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3789f0a594c1SAndy Whitcroft		}
3790f0a594c1SAndy Whitcroft
379122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
379222f2a2efSAndy Whitcroft## # continuation.
379322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
379422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
379522f2a2efSAndy Whitcroft##
379622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
379722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
379822f2a2efSAndy Whitcroft## 			my $ln = $line;
379922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
380022f2a2efSAndy Whitcroft## 			}
380122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3802000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3803000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
380422f2a2efSAndy Whitcroft## 			}
380522f2a2efSAndy Whitcroft## 		}
3806f0a594c1SAndy Whitcroft
38070a920b5bSAndy Whitcroft#need space before brace following if, while, etc
380822f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
380922f2a2efSAndy Whitcroft		    $line =~ /do{/) {
38103705ce5bSJoe Perches			if (ERROR("SPACING",
38113705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
38123705ce5bSJoe Perches			    $fix) {
3813194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
38143705ce5bSJoe Perches			}
3815de7d4f0eSAndy Whitcroft		}
3816de7d4f0eSAndy Whitcroft
3817c4a62ef9SJoe Perches## # check for blank lines before declarations
3818c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3819c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3820c4a62ef9SJoe Perches##			WARN("SPACING",
3821c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3822c4a62ef9SJoe Perches##		}
3823c4a62ef9SJoe Perches##
3824c4a62ef9SJoe Perches
3825de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3826de7d4f0eSAndy Whitcroft# on the line
3827de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3828d5e616fcSJoe Perches			if (ERROR("SPACING",
3829d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3830d5e616fcSJoe Perches			    $fix) {
3831194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3832d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3833d5e616fcSJoe Perches			}
38340a920b5bSAndy Whitcroft		}
38350a920b5bSAndy Whitcroft
383622f2a2efSAndy Whitcroft# check spacing on square brackets
383722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
38383705ce5bSJoe Perches			if (ERROR("SPACING",
38393705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
38403705ce5bSJoe Perches			    $fix) {
3841194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38423705ce5bSJoe Perches				    s/\[\s+/\[/;
38433705ce5bSJoe Perches			}
384422f2a2efSAndy Whitcroft		}
384522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
38463705ce5bSJoe Perches			if (ERROR("SPACING",
38473705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
38483705ce5bSJoe Perches			    $fix) {
3849194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38503705ce5bSJoe Perches				    s/\s+\]/\]/;
38513705ce5bSJoe Perches			}
385222f2a2efSAndy Whitcroft		}
385322f2a2efSAndy Whitcroft
3854c45dcabdSAndy Whitcroft# check spacing on parentheses
38559c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
38569c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
38573705ce5bSJoe Perches			if (ERROR("SPACING",
38583705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
38593705ce5bSJoe Perches			    $fix) {
3860194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38613705ce5bSJoe Perches				    s/\(\s+/\(/;
38623705ce5bSJoe Perches			}
386322f2a2efSAndy Whitcroft		}
386413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3865c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3866c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
38673705ce5bSJoe Perches			if (ERROR("SPACING",
38683705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
38693705ce5bSJoe Perches			    $fix) {
3870194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38713705ce5bSJoe Perches				    s/\s+\)/\)/;
38723705ce5bSJoe Perches			}
387322f2a2efSAndy Whitcroft		}
387422f2a2efSAndy Whitcroft
3875e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3876e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3877e2826fd0SJoe Perches
3878e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3879ea4acbb1SJoe Perches			my $var = $1;
3880ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3881ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
3882ea4acbb1SJoe Perches			    $fix) {
3883ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3884ea4acbb1SJoe Perches			}
3885ea4acbb1SJoe Perches		}
3886ea4acbb1SJoe Perches
3887ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
3888ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
3889ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
3890ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3891ea4acbb1SJoe Perches			my $var = $2;
3892ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3893ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3894ea4acbb1SJoe Perches			    $fix) {
3895ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
3896ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
3897ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3898ea4acbb1SJoe Perches			}
3899e2826fd0SJoe Perches		}
3900e2826fd0SJoe Perches
39010a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
39024a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
39030a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
39043705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
39053705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
39063705ce5bSJoe Perches			    $fix) {
3907194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39083705ce5bSJoe Perches				    s/^(.)\s+/$1/;
39093705ce5bSJoe Perches			}
39100a920b5bSAndy Whitcroft		}
39110a920b5bSAndy Whitcroft
39125b9553abSJoe Perches# return is not a function
3913507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3914c45dcabdSAndy Whitcroft			my $spacing = $1;
3915507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
39165b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
39175b9553abSJoe Perches				my $value = $1;
39185b9553abSJoe Perches				$value = deparenthesize($value);
39195b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3920000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3921000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
39225b9553abSJoe Perches				}
3923c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3924000d1cc1SJoe Perches				ERROR("SPACING",
3925000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3926c45dcabdSAndy Whitcroft			}
3927c45dcabdSAndy Whitcroft		}
3928507e5141SJoe Perches
3929b43ae21bSJoe Perches# unnecessary return in a void function
3930b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3931b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3932b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3933b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3934b43ae21bSJoe Perches		    $linenr >= 3 &&
3935b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3936b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
39379819cf25SJoe Perches			WARN("RETURN_VOID",
3938b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
39399819cf25SJoe Perches               }
39409819cf25SJoe Perches
3941189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3942189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3943189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3944189248d8SJoe Perches			my $openparens = $1;
3945189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3946189248d8SJoe Perches			my $msg = "";
3947189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3948189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3949189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3950189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3951189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3952189248d8SJoe Perches			}
3953189248d8SJoe Perches		}
3954189248d8SJoe Perches
395553a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
395653a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
395753a3c448SAndy Whitcroft			my $name = $1;
395853a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3959000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3960000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
396153a3c448SAndy Whitcroft			}
396253a3c448SAndy Whitcroft		}
3963c45dcabdSAndy Whitcroft
39640a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
39654a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
39663705ce5bSJoe Perches			if (ERROR("SPACING",
39673705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
39683705ce5bSJoe Perches			    $fix) {
3969194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39703705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
39713705ce5bSJoe Perches			}
39720a920b5bSAndy Whitcroft		}
39730a920b5bSAndy Whitcroft
3974f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3975f5fe35ddSAndy Whitcroft# statements after the conditional.
3976170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
39773e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
39783e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
39793e469cdcSAndy Whitcroft					if (!defined $stat);
3980170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3981170d3a22SAndy Whitcroft						$remain_next, $off_next);
3982170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3983170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3984170d3a22SAndy Whitcroft
3985170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3986170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3987170d3a22SAndy Whitcroft				# then count those as offsets.
3988170d3a22SAndy Whitcroft				my ($whitespace) =
3989170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3990170d3a22SAndy Whitcroft				my $offset =
3991170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3992170d3a22SAndy Whitcroft
3993170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3994170d3a22SAndy Whitcroft								$offset} = 1;
3995170d3a22SAndy Whitcroft			}
3996170d3a22SAndy Whitcroft		}
3997170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3998c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3999170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4000171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
40018905a67cSAndy Whitcroft
4002b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4003000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4004000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
40058905a67cSAndy Whitcroft			}
40068905a67cSAndy Whitcroft
40078905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
40088905a67cSAndy Whitcroft			# conditional.
4009773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
40108905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
401113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
401253210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
401353210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4014773647a0SAndy Whitcroft			{
4015bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4016bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4017bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
401842bdf74cSHidetoshi Seto				my $stat_real = '';
4019bb44ad39SAndy Whitcroft
402042bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
402142bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4022bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4023bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4024bb44ad39SAndy Whitcroft				}
4025bb44ad39SAndy Whitcroft
4026000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4027000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
40288905a67cSAndy Whitcroft			}
40298905a67cSAndy Whitcroft		}
40308905a67cSAndy Whitcroft
403113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
403213214adfSAndy Whitcroft		if ($line =~ /
403313214adfSAndy Whitcroft			(?:
403413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
403513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
403613214adfSAndy Whitcroft				(?:\&\&|\|\|)
403713214adfSAndy Whitcroft			|
403813214adfSAndy Whitcroft				(?:\&\&|\|\|)
403913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
404013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
404113214adfSAndy Whitcroft			)/x)
404213214adfSAndy Whitcroft		{
4043000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4044000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
404513214adfSAndy Whitcroft		}
404613214adfSAndy Whitcroft
40478905a67cSAndy Whitcroft# if and else should not have general statements after it
404813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
404913214adfSAndy Whitcroft			my $s = $1;
405013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
405113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4052000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4053000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
40540a920b5bSAndy Whitcroft			}
405513214adfSAndy Whitcroft		}
405639667782SAndy Whitcroft# if should not continue a brace
405739667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4058000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4059048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
406039667782SAndy Whitcroft				$herecurr);
406139667782SAndy Whitcroft		}
4062a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4063a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4064a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
40653fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4066a1080bf8SAndy Whitcroft			\s*return\s+
4067a1080bf8SAndy Whitcroft		    )/xg)
4068a1080bf8SAndy Whitcroft		{
4069000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4070000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4071a1080bf8SAndy Whitcroft		}
40720a920b5bSAndy Whitcroft
40730a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
40740a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
40758b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
40760a920b5bSAndy Whitcroft		    $previndent == $indent) {
40778b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
40788b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
40798b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
40808b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
40818b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
40828b8856f4SJoe Perches				my $fixedline = $prevrawline;
40838b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
40848b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
40858b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
40868b8856f4SJoe Perches				}
40878b8856f4SJoe Perches				$fixedline = $rawline;
40888b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
40898b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
40908b8856f4SJoe Perches			}
40910a920b5bSAndy Whitcroft		}
40920a920b5bSAndy Whitcroft
40938b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4094c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4095c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4096c2fdda0dSAndy Whitcroft
4097c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4098c2fdda0dSAndy Whitcroft			# conditional.
4099773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4100c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4101c2fdda0dSAndy Whitcroft
4102c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
41038b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
41048b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
41058b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41068b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
41078b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
41088b8856f4SJoe Perches					my $fixedline = $prevrawline;
41098b8856f4SJoe Perches					my $trailing = $rawline;
41108b8856f4SJoe Perches					$trailing =~ s/^\+//;
41118b8856f4SJoe Perches					$trailing = trim($trailing);
41128b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
41138b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41148b8856f4SJoe Perches				}
4115c2fdda0dSAndy Whitcroft			}
4116c2fdda0dSAndy Whitcroft		}
4117c2fdda0dSAndy Whitcroft
411895e2c602SJoe Perches#Specific variable tests
4119323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4120323c1260SJoe Perches			my $var = $1;
412195e2c602SJoe Perches
412295e2c602SJoe Perches#gcc binary extension
412395e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4124d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4125d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4126d5e616fcSJoe Perches				    $fix) {
4127d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4128194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4129d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4130d5e616fcSJoe Perches				}
413195e2c602SJoe Perches			}
413295e2c602SJoe Perches
413395e2c602SJoe Perches#CamelCase
4134807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4135be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
413622735ce8SJoe Perches#Ignore Page<foo> variants
4137807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
413822735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4139f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4140f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4141f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
41427e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
41437e781f67SJoe Perches					my $word = $1;
41447e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4145d8b07710SJoe Perches					if ($check) {
4146d8b07710SJoe Perches						seed_camelcase_includes();
4147d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4148d8b07710SJoe Perches							seed_camelcase_file($realfile);
4149d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4150d8b07710SJoe Perches						}
4151d8b07710SJoe Perches					}
41527e781f67SJoe Perches					if (!defined $camelcase{$word}) {
41537e781f67SJoe Perches						$camelcase{$word} = 1;
4154be79794bSJoe Perches						CHK("CAMELCASE",
41557e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
41567e781f67SJoe Perches					}
4157323c1260SJoe Perches				}
4158323c1260SJoe Perches			}
41593445686aSJoe Perches		}
41600a920b5bSAndy Whitcroft
41610a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4162d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4163d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4164d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4165d5e616fcSJoe Perches			    $fix) {
4166194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4167d5e616fcSJoe Perches			}
41680a920b5bSAndy Whitcroft		}
41690a920b5bSAndy Whitcroft
4170653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4171c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4172e09dec48SAndy Whitcroft			my $file = "$1.h";
4173e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4174e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4175e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
41767840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4177c45dcabdSAndy Whitcroft			{
4178e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4179000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4180000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4181e09dec48SAndy Whitcroft				} else {
4182000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4183000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4184e09dec48SAndy Whitcroft				}
41850a920b5bSAndy Whitcroft			}
41860a920b5bSAndy Whitcroft		}
41870a920b5bSAndy Whitcroft
4188653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4189653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4190cf655043SAndy Whitcroft# in a known good container
4191b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4192b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4193d8aaf121SAndy Whitcroft			my $ln = $linenr;
4194d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4195c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4196c45dcabdSAndy Whitcroft			my $ctx = '';
419708a2843eSJoe Perches			my $has_flow_statement = 0;
419808a2843eSJoe Perches			my $has_arg_concat = 0;
4199c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4200f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4201f74bd194SAndy Whitcroft			$ctx = $dstat;
4202c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4203a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4204c45dcabdSAndy Whitcroft
420508a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
420608a2843eSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/);
420708a2843eSJoe Perches
4208f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4209292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4210c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4211c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4212c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4213c45dcabdSAndy Whitcroft
4214c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4215bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4216bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4217c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4218bf30d6edSAndy Whitcroft			{
4219c45dcabdSAndy Whitcroft			}
4220c45dcabdSAndy Whitcroft
4221e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4222e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4223e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4224e45bab8eSAndy Whitcroft			{
4225e45bab8eSAndy Whitcroft			}
4226e45bab8eSAndy Whitcroft
4227c45dcabdSAndy Whitcroft			my $exceptions = qr{
4228c45dcabdSAndy Whitcroft				$Declare|
4229c45dcabdSAndy Whitcroft				module_param_named|
4230a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4231c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4232c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4233383099fdSAndy Whitcroft				__typeof__\(|
423422fd2d3eSStefani Seibold				union|
423522fd2d3eSStefani Seibold				struct|
4236ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4237ea71a0a0SAndy Whitcroft				^\"|\"$
4238c45dcabdSAndy Whitcroft			}x;
42395eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4240f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4241f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4242f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
42433cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4244356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4245f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4246f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4247e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
424872f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4249f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4250f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4251f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4252f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4253f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4254c45dcabdSAndy Whitcroft			{
4255f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4256f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4257f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4258f74bd194SAndy Whitcroft
4259f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4260f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4261c45dcabdSAndy Whitcroft				}
4262c45dcabdSAndy Whitcroft
4263f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4264f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4265f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4266f74bd194SAndy Whitcroft				} else {
4267000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4268388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4269d8aaf121SAndy Whitcroft				}
42700a920b5bSAndy Whitcroft			}
42715023d347SJoe Perches
427208a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
427308a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
427408a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
427508a2843eSJoe Perches				my $herectx = $here . "\n";
427608a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
427708a2843eSJoe Perches
427808a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
427908a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
428008a2843eSJoe Perches				}
428108a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
428208a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
428308a2843eSJoe Perches			}
428408a2843eSJoe Perches
4285481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
42865023d347SJoe Perches
42875023d347SJoe Perches		} else {
42885023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4289481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4290481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
42915023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
42925023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
42935023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
42945023d347SJoe Perches			}
4295653d4876SAndy Whitcroft		}
42960a920b5bSAndy Whitcroft
4297b13edf7fSJoe Perches# do {} while (0) macro tests:
4298b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4299b13edf7fSJoe Perches# macro should not end with a semicolon
4300b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4301b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4302b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4303b13edf7fSJoe Perches			my $ln = $linenr;
4304b13edf7fSJoe Perches			my $cnt = $realcnt;
4305b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4306b13edf7fSJoe Perches			my $ctx = '';
4307b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4308b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4309b13edf7fSJoe Perches			$ctx = $dstat;
4310b13edf7fSJoe Perches
4311b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
43121b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4313b13edf7fSJoe Perches
4314b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4315b13edf7fSJoe Perches				my $stmts = $2;
4316b13edf7fSJoe Perches				my $semis = $3;
4317b13edf7fSJoe Perches
4318b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4319b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4320b13edf7fSJoe Perches				my $herectx = $here . "\n";
4321b13edf7fSJoe Perches
4322b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4323b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4324b13edf7fSJoe Perches				}
4325b13edf7fSJoe Perches
4326ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4327ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4328b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4329b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4330b13edf7fSJoe Perches				}
4331b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4332b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4333b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4334b13edf7fSJoe Perches				}
4335f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4336f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4337f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4338f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4339f5ef95b1SJoe Perches
4340f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4341f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4342f5ef95b1SJoe Perches				}
4343f5ef95b1SJoe Perches
4344f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4345f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4346b13edf7fSJoe Perches			}
4347b13edf7fSJoe Perches		}
4348b13edf7fSJoe Perches
4349080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4350080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4351080ba929SMike Frysinger#	.
4352080ba929SMike Frysinger#	ALIGN(...)
4353080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4354080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4355000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4356000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4357080ba929SMike Frysinger		}
4358080ba929SMike Frysinger
4359f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
436013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
436113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4362cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
436313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4364cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4365cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4366aad4f614SJoe Perches				my @allowed = ();
4367aad4f614SJoe Perches				my $allow = 0;
436813214adfSAndy Whitcroft				my $seen = 0;
4369773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4370cf655043SAndy Whitcroft				my $ln = $linenr - 1;
437113214adfSAndy Whitcroft				for my $chunk (@chunks) {
437213214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
437313214adfSAndy Whitcroft
4374773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4375773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4376773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4377773647a0SAndy Whitcroft
4378aad4f614SJoe Perches					$allowed[$allow] = 0;
4379773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4380773647a0SAndy Whitcroft
4381773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4382773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4383773647a0SAndy Whitcroft
4384773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4385cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4386cf655043SAndy Whitcroft
4387773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
438813214adfSAndy Whitcroft
438913214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
439013214adfSAndy Whitcroft
4391aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4392cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4393cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4394aad4f614SJoe Perches						$allowed[$allow] = 1;
439513214adfSAndy Whitcroft					}
439613214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4397cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4398aad4f614SJoe Perches						$allowed[$allow] = 1;
439913214adfSAndy Whitcroft					}
4400cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4401cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4402aad4f614SJoe Perches						$allowed[$allow] = 1;
440313214adfSAndy Whitcroft					}
4404aad4f614SJoe Perches					$allow++;
440513214adfSAndy Whitcroft				}
4406aad4f614SJoe Perches				if ($seen) {
4407aad4f614SJoe Perches					my $sum_allowed = 0;
4408aad4f614SJoe Perches					foreach (@allowed) {
4409aad4f614SJoe Perches						$sum_allowed += $_;
4410aad4f614SJoe Perches					}
4411aad4f614SJoe Perches					if ($sum_allowed == 0) {
4412000d1cc1SJoe Perches						WARN("BRACES",
4413000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4414aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4415aad4f614SJoe Perches						 $seen != $allow) {
4416aad4f614SJoe Perches						CHK("BRACES",
4417aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4418aad4f614SJoe Perches					}
441913214adfSAndy Whitcroft				}
442013214adfSAndy Whitcroft			}
442113214adfSAndy Whitcroft		}
4422773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
442313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4424cf655043SAndy Whitcroft			my $allowed = 0;
4425f0a594c1SAndy Whitcroft
4426cf655043SAndy Whitcroft			# Check the pre-context.
4427cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4428cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4429cf655043SAndy Whitcroft				$allowed = 1;
4430f0a594c1SAndy Whitcroft			}
4431773647a0SAndy Whitcroft
4432773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4433773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4434773647a0SAndy Whitcroft
4435cf655043SAndy Whitcroft			# Check the condition.
4436cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4437773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4438cf655043SAndy Whitcroft			if (defined $cond) {
4439773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4440cf655043SAndy Whitcroft			}
4441cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4442cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4443cf655043SAndy Whitcroft				$allowed = 1;
4444cf655043SAndy Whitcroft			}
4445cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4446cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4447cf655043SAndy Whitcroft				$allowed = 1;
4448cf655043SAndy Whitcroft			}
4449cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4450cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4451cf655043SAndy Whitcroft				$allowed = 1;
4452cf655043SAndy Whitcroft			}
4453cf655043SAndy Whitcroft			# Check the post-context.
4454cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4455cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4456cf655043SAndy Whitcroft				if (defined $cond) {
4457773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4458cf655043SAndy Whitcroft				}
4459cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4460cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4461cf655043SAndy Whitcroft					$allowed = 1;
4462cf655043SAndy Whitcroft				}
4463cf655043SAndy Whitcroft			}
4464cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
446569932487SJustin P. Mattock				my $herectx = $here . "\n";
4466f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4467cf655043SAndy Whitcroft
4468f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
446969932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4470cf655043SAndy Whitcroft				}
4471cf655043SAndy Whitcroft
4472000d1cc1SJoe Perches				WARN("BRACES",
4473000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4474f0a594c1SAndy Whitcroft			}
4475f0a594c1SAndy Whitcroft		}
4476f0a594c1SAndy Whitcroft
44770979ae66SJoe Perches# check for unnecessary blank lines around braces
447877b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4479f8e58219SJoe Perches			if (CHK("BRACES",
4480f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4481f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4482f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4483f8e58219SJoe Perches			}
44840979ae66SJoe Perches		}
448577b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4486f8e58219SJoe Perches			if (CHK("BRACES",
4487f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4488f8e58219SJoe Perches			    $fix) {
4489f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4490f8e58219SJoe Perches			}
44910979ae66SJoe Perches		}
44920979ae66SJoe Perches
44934a0df2efSAndy Whitcroft# no volatiles please
44946c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
44956c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4496000d1cc1SJoe Perches			WARN("VOLATILE",
4497000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
44984a0df2efSAndy Whitcroft		}
44994a0df2efSAndy Whitcroft
45005e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
45015e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
45025e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
45035e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
45045e4f6ba5SJoe Perches		if ($line =~ /^\+\s*"[X\t]*"/ &&
45055e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
45065e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
45075e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
45085e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
45095e4f6ba5SJoe Perches				     $fix &&
45105e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
45115e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
45125e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
45135e4f6ba5SJoe Perches				my $comma_close = "";
45145e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
45155e4f6ba5SJoe Perches					$comma_close = $1;
45165e4f6ba5SJoe Perches				}
45175e4f6ba5SJoe Perches
45185e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
45195e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45205e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
45215e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
45225e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
45235e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
45245e4f6ba5SJoe Perches				$fixedline = $rawline;
45255e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
45265e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
45275e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
45285e4f6ba5SJoe Perches				}
45295e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
45305e4f6ba5SJoe Perches			}
45315e4f6ba5SJoe Perches		}
45325e4f6ba5SJoe Perches
45335e4f6ba5SJoe Perches# check for missing a space in a string concatenation
45345e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
45355e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
45365e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
45375e4f6ba5SJoe Perches		}
45385e4f6ba5SJoe Perches
45395e4f6ba5SJoe Perches# check for spaces before a quoted newline
45405e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
45415e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
45425e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
45435e4f6ba5SJoe Perches			    $fix) {
45445e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
45455e4f6ba5SJoe Perches			}
45465e4f6ba5SJoe Perches
45475e4f6ba5SJoe Perches		}
45485e4f6ba5SJoe Perches
4549f17dba4fSJoe Perches# concatenated string without spaces between elements
4550f17dba4fSJoe Perches		if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4551f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4552f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4553f17dba4fSJoe Perches		}
4554f17dba4fSJoe Perches
455590ad30e5SJoe Perches# uncoalesced string fragments
455690ad30e5SJoe Perches		if ($line =~ /"X*"\s*"/) {
455790ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
455890ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
455990ad30e5SJoe Perches		}
456090ad30e5SJoe Perches
45615e4f6ba5SJoe Perches# check for %L{u,d,i} in strings
45625e4f6ba5SJoe Perches		my $string;
45635e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
45645e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
45655e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
45665e4f6ba5SJoe Perches			if ($string =~ /(?<!%)%L[udi]/) {
45675e4f6ba5SJoe Perches				WARN("PRINTF_L",
45685e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
45695e4f6ba5SJoe Perches				last;
45705e4f6ba5SJoe Perches			}
45715e4f6ba5SJoe Perches		}
45725e4f6ba5SJoe Perches
45735e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
45745e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
45755e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
45765e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
45775e4f6ba5SJoe Perches		}
45785e4f6ba5SJoe Perches
457900df344fSAndy Whitcroft# warn about #if 0
4580c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4581000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4582000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4583de7d4f0eSAndy Whitcroft				$herecurr);
45844a0df2efSAndy Whitcroft		}
45854a0df2efSAndy Whitcroft
458603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
458703df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
458803df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
458903df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
459003df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
459115160f90SFabio Estevam				     "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
45924c432a8fSGreg Kroah-Hartman			}
45934c432a8fSGreg Kroah-Hartman		}
4594f0a594c1SAndy Whitcroft
4595ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4596ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4597ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4598ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4599ebfdc409SJoe Perches		    $linenr > 3) {
4600ebfdc409SJoe Perches			my $testval = $2;
4601ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4602ebfdc409SJoe Perches
4603ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4604ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4605ebfdc409SJoe Perches
4606ebfdc409SJoe 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)/) {
4607ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4608ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4609ebfdc409SJoe Perches			}
4610ebfdc409SJoe Perches		}
4611ebfdc409SJoe Perches
4612f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
4613dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
4614f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4615f78d98f6SJoe Perches			my $level = $1;
4616f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
4617f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
4618f78d98f6SJoe Perches			    $fix) {
4619f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
4620f78d98f6SJoe Perches			}
4621f78d98f6SJoe Perches		}
4622f78d98f6SJoe Perches
4623abb08a53SJoe Perches# check for mask then right shift without a parentheses
4624abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4625abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4626abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4627abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
4628abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4629abb08a53SJoe Perches		}
4630abb08a53SJoe Perches
4631b75ac618SJoe Perches# check for pointer comparisons to NULL
4632b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
4633b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4634b75ac618SJoe Perches				my $val = $1;
4635b75ac618SJoe Perches				my $equal = "!";
4636b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
4637b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
4638b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4639b75ac618SJoe Perches					    $fix) {
4640b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4641b75ac618SJoe Perches				}
4642b75ac618SJoe Perches			}
4643b75ac618SJoe Perches		}
4644b75ac618SJoe Perches
46458716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
46468716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
46478716de38SJoe Perches			my $attr = $1;
46488716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
46498716de38SJoe Perches				my $ptr = $1;
46508716de38SJoe Perches				my $var = $2;
46518716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
46528716de38SJoe Perches				      ERROR("MISPLACED_INIT",
46538716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
46548716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
46558716de38SJoe Perches				      WARN("MISPLACED_INIT",
46568716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
46578716de38SJoe Perches				    $fix) {
4658194f66fcSJoe 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;
46598716de38SJoe Perches				}
46608716de38SJoe Perches			}
46618716de38SJoe Perches		}
46628716de38SJoe Perches
4663e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4664e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4665e970b884SJoe Perches			my $attr = $1;
4666e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4667e970b884SJoe Perches			my $attr_prefix = $1;
4668e970b884SJoe Perches			my $attr_type = $2;
4669e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4670e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4671e970b884SJoe Perches			    $fix) {
4672194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4673e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4674e970b884SJoe Perches			}
4675e970b884SJoe Perches		}
4676e970b884SJoe Perches
4677e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4678e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4679e970b884SJoe Perches			my $attr = $1;
4680e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4681e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4682e970b884SJoe Perches			    $fix) {
4683194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4684e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4685e970b884SJoe Perches				$lead = rtrim($1);
4686e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4687e970b884SJoe Perches				$lead = "${lead}const ";
4688194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4689e970b884SJoe Perches			}
4690e970b884SJoe Perches		}
4691e970b884SJoe Perches
4692fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4693fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4694fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4695fbdb8138SJoe Perches			my $constant_func = $1;
4696fbdb8138SJoe Perches			my $func = $constant_func;
4697fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4698fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4699fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4700fbdb8138SJoe Perches			    $fix) {
4701194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4702fbdb8138SJoe Perches			}
4703fbdb8138SJoe Perches		}
4704fbdb8138SJoe Perches
47051a15a250SPatrick Pannuto# prefer usleep_range over udelay
470637581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
470743c1d77cSJoe Perches			my $delay = $1;
47081a15a250SPatrick Pannuto			# ignore udelay's < 10, however
470943c1d77cSJoe Perches			if (! ($delay < 10) ) {
4710000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
471143c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
471243c1d77cSJoe Perches			}
471343c1d77cSJoe Perches			if ($delay > 2000) {
471443c1d77cSJoe Perches				WARN("LONG_UDELAY",
471543c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
47161a15a250SPatrick Pannuto			}
47171a15a250SPatrick Pannuto		}
47181a15a250SPatrick Pannuto
471909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
472009ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
472109ef8725SPatrick Pannuto			if ($1 < 20) {
4722000d1cc1SJoe Perches				WARN("MSLEEP",
472343c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
472409ef8725SPatrick Pannuto			}
472509ef8725SPatrick Pannuto		}
472609ef8725SPatrick Pannuto
472736ec1939SJoe Perches# check for comparisons of jiffies
472836ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
472936ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
473036ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
473136ec1939SJoe Perches		}
473236ec1939SJoe Perches
47339d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
47349d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
47359d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
47369d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
47379d7a34a5SJoe Perches		}
47389d7a34a5SJoe Perches
473900df344fSAndy Whitcroft# warn about #ifdefs in C files
4740c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
474100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
474200df344fSAndy Whitcroft#			print "$herecurr";
474300df344fSAndy Whitcroft#			$clean = 0;
474400df344fSAndy Whitcroft#		}
474500df344fSAndy Whitcroft
474622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4747c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
47483705ce5bSJoe Perches			if (ERROR("SPACING",
47493705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
47503705ce5bSJoe Perches			    $fix) {
4751194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47523705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
47533705ce5bSJoe Perches			}
47543705ce5bSJoe Perches
475522f2a2efSAndy Whitcroft		}
475622f2a2efSAndy Whitcroft
47574a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4758171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4759171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
47604a0df2efSAndy Whitcroft			my $which = $1;
47614a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4762000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4763000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
47644a0df2efSAndy Whitcroft			}
47654a0df2efSAndy Whitcroft		}
47664a0df2efSAndy Whitcroft# check for memory barriers without a comment.
47674a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
47684a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4769c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4770000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
47714a0df2efSAndy Whitcroft			}
47724a0df2efSAndy Whitcroft		}
47734a0df2efSAndy Whitcroft# check of hardware specific defines
4774c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4775000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4776000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
47770a920b5bSAndy Whitcroft		}
4778653d4876SAndy Whitcroft
4779d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4780d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4781000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4782000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4783d4977c78STobias Klauser		}
4784d4977c78STobias Klauser
4785de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4786de7d4f0eSAndy Whitcroft# storage class and type.
47879c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
47889c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4789000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4790000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4791de7d4f0eSAndy Whitcroft		}
4792de7d4f0eSAndy Whitcroft
47938905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
47942b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
47952b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4796d5e616fcSJoe Perches			if (WARN("INLINE",
4797d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4798d5e616fcSJoe Perches			    $fix) {
4799194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4800d5e616fcSJoe Perches
4801d5e616fcSJoe Perches			}
48028905a67cSAndy Whitcroft		}
48038905a67cSAndy Whitcroft
48043d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
48052b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48062b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4807000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4808000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
48093d130fd0SJoe Perches		}
48103d130fd0SJoe Perches
481139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
48122b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48132b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4814000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4815000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
481639b7e287SJoe Perches		}
481739b7e287SJoe Perches
48185f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
48192b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48202b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4821d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4822d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4823d5e616fcSJoe Perches			    $fix) {
4824194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4825d5e616fcSJoe Perches
4826d5e616fcSJoe Perches			}
48275f14d3bdSJoe Perches		}
48285f14d3bdSJoe Perches
48296061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
48302b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48312b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4832d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4833d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4834d5e616fcSJoe Perches			    $fix) {
4835194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4836d5e616fcSJoe Perches			}
48376061d949SJoe Perches		}
48386061d949SJoe Perches
4839619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
4840619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4841619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4842619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4843619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
4844619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
4845619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
4846619a908aSJoe Perches		}
4847619a908aSJoe Perches
48488f53a9b8SJoe Perches# check for sizeof(&)
48498f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4850000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4851000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
48528f53a9b8SJoe Perches		}
48538f53a9b8SJoe Perches
485466c80b60SJoe Perches# check for sizeof without parenthesis
485566c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4856d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4857d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4858d5e616fcSJoe Perches			    $fix) {
4859194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4860d5e616fcSJoe Perches			}
486166c80b60SJoe Perches		}
486266c80b60SJoe Perches
486388982feaSJoe Perches# check for struct spinlock declarations
486488982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
486588982feaSJoe Perches			WARN("USE_SPINLOCK_T",
486688982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
486788982feaSJoe Perches		}
486888982feaSJoe Perches
4869a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
487006668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4871a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
4872caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
4873caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
4874d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4875d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4876d5e616fcSJoe Perches				    $fix) {
4877194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4878d5e616fcSJoe Perches				}
4879a6962d72SJoe Perches			}
4880a6962d72SJoe Perches		}
4881a6962d72SJoe Perches
4882554e165cSAndy Whitcroft# Check for misused memsets
4883d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4884d1fe9c09SJoe Perches		    defined $stat &&
4885d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4886554e165cSAndy Whitcroft
4887d7c76ba7SJoe Perches			my $ms_addr = $2;
4888d1fe9c09SJoe Perches			my $ms_val = $7;
4889d1fe9c09SJoe Perches			my $ms_size = $12;
4890d7c76ba7SJoe Perches
4891554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4892554e165cSAndy Whitcroft				ERROR("MEMSET",
4893d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4894554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4895554e165cSAndy Whitcroft				WARN("MEMSET",
4896d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4897d7c76ba7SJoe Perches			}
4898d7c76ba7SJoe Perches		}
4899d7c76ba7SJoe Perches
490098a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
490198a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
490298a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
490398a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
490498a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
490598a9bba5SJoe Perches			    $fix) {
4906194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
490798a9bba5SJoe Perches			}
490898a9bba5SJoe Perches		}
490998a9bba5SJoe Perches
4910d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4911d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4912d1fe9c09SJoe Perches		    defined $stat &&
4913d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4914d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4915d7c76ba7SJoe Perches				my $call = $1;
4916d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4917d7c76ba7SJoe Perches				my $arg1 = $3;
4918d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4919d1fe9c09SJoe Perches				my $arg2 = $8;
4920d7c76ba7SJoe Perches				my $cast;
4921d7c76ba7SJoe Perches
4922d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4923d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4924d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4925d7c76ba7SJoe Perches					$cast = $cast1;
4926d7c76ba7SJoe Perches				} else {
4927d7c76ba7SJoe Perches					$cast = $cast2;
4928d7c76ba7SJoe Perches				}
4929d7c76ba7SJoe Perches				WARN("MINMAX",
4930d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4931554e165cSAndy Whitcroft			}
4932554e165cSAndy Whitcroft		}
4933554e165cSAndy Whitcroft
49344a273195SJoe Perches# check usleep_range arguments
49354a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
49364a273195SJoe Perches		    defined $stat &&
49374a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
49384a273195SJoe Perches			my $min = $1;
49394a273195SJoe Perches			my $max = $7;
49404a273195SJoe Perches			if ($min eq $max) {
49414a273195SJoe Perches				WARN("USLEEP_RANGE",
49424a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
49434a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
49444a273195SJoe Perches				 $min > $max) {
49454a273195SJoe Perches				WARN("USLEEP_RANGE",
49464a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
49474a273195SJoe Perches			}
49484a273195SJoe Perches		}
49494a273195SJoe Perches
4950823b794cSJoe Perches# check for naked sscanf
4951823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4952823b794cSJoe Perches		    defined $stat &&
49536c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4954823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4955823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4956823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4957823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4958823b794cSJoe Perches			$lc = $lc + $linenr;
4959823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4960823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4961823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4962823b794cSJoe Perches			}
4963823b794cSJoe Perches			WARN("NAKED_SSCANF",
4964823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4965823b794cSJoe Perches		}
4966823b794cSJoe Perches
4967afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4968afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4969afc819abSJoe Perches		    defined $stat &&
4970afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4971afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4972afc819abSJoe Perches			$lc = $lc + $linenr;
4973afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4974afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4975afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4976afc819abSJoe Perches			}
4977afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4978afc819abSJoe Perches				my $format = $6;
4979afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4980afc819abSJoe Perches				if ($count == 1 &&
4981afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4982afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4983afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4984afc819abSJoe Perches				}
4985afc819abSJoe Perches			}
4986afc819abSJoe Perches		}
4987afc819abSJoe Perches
498870dc8a48SJoe Perches# check for new externs in .h files.
498970dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
499070dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4991d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
499270dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
499370dc8a48SJoe Perches			    $fix) {
4994194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
499570dc8a48SJoe Perches			}
499670dc8a48SJoe Perches		}
499770dc8a48SJoe Perches
4998de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4999171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5000c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5001171ae1a4SAndy Whitcroft		{
5002c45dcabdSAndy Whitcroft			my $function_name = $1;
5003c45dcabdSAndy Whitcroft			my $paren_space = $2;
5004171ae1a4SAndy Whitcroft
5005171ae1a4SAndy Whitcroft			my $s = $stat;
5006171ae1a4SAndy Whitcroft			if (defined $cond) {
5007171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5008171ae1a4SAndy Whitcroft			}
5009c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5010c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5011c45dcabdSAndy Whitcroft			{
5012000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5013000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5014de7d4f0eSAndy Whitcroft			}
5015de7d4f0eSAndy Whitcroft
5016171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5017000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5018000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5019171ae1a4SAndy Whitcroft			}
50209c9ba34eSAndy Whitcroft
50219c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
50229c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
50239c9ba34eSAndy Whitcroft		{
5024000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5025000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5026171ae1a4SAndy Whitcroft		}
5027171ae1a4SAndy Whitcroft
5028de7d4f0eSAndy Whitcroft# checks for new __setup's
5029de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5030de7d4f0eSAndy Whitcroft			my $name = $1;
5031de7d4f0eSAndy Whitcroft
5032de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5033000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5034000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5035de7d4f0eSAndy Whitcroft			}
5036653d4876SAndy Whitcroft		}
50379c0ca6f9SAndy Whitcroft
50389c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5039caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5040000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5041000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
50429c0ca6f9SAndy Whitcroft		}
504313214adfSAndy Whitcroft
5044a640d25cSJoe Perches# alloc style
5045a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5046a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5047a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5048a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5049a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5050a640d25cSJoe Perches		}
5051a640d25cSJoe Perches
505260a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
505360a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5054e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
505560a55369SJoe Perches			my $oldfunc = $3;
505660a55369SJoe Perches			my $a1 = $4;
505760a55369SJoe Perches			my $a2 = $10;
505860a55369SJoe Perches			my $newfunc = "kmalloc_array";
505960a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
506060a55369SJoe Perches			my $r1 = $a1;
506160a55369SJoe Perches			my $r2 = $a2;
506260a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
506360a55369SJoe Perches				$r1 = $a2;
506460a55369SJoe Perches				$r2 = $a1;
506560a55369SJoe Perches			}
5066e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5067e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5068e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5069e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5070e367455aSJoe Perches				    $fix) {
5071194f66fcSJoe 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;
507260a55369SJoe Perches
507360a55369SJoe Perches				}
507460a55369SJoe Perches			}
507560a55369SJoe Perches		}
507660a55369SJoe Perches
5077972fdea2SJoe Perches# check for krealloc arg reuse
5078972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5079972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5080972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5081972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5082972fdea2SJoe Perches		}
5083972fdea2SJoe Perches
50845ce59ae0SJoe Perches# check for alloc argument mismatch
50855ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
50865ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
50875ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
50885ce59ae0SJoe Perches		}
50895ce59ae0SJoe Perches
5090caf2a54fSJoe Perches# check for multiple semicolons
5091caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5092d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5093d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5094d5e616fcSJoe Perches			    $fix) {
5095194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5096d5e616fcSJoe Perches			}
5097d1e2ad07SJoe Perches		}
5098d1e2ad07SJoe Perches
50990ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
51000ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
51010ab90191SJoe Perches			my $ull = "";
51020ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
51030ab90191SJoe Perches			if (CHK("BIT_MACRO",
51040ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
51050ab90191SJoe Perches			    $fix) {
51060ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
51070ab90191SJoe Perches			}
51080ab90191SJoe Perches		}
51090ab90191SJoe Perches
5110e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5111c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5112c34c09a8SJoe Perches			my $has_break = 0;
5113c34c09a8SJoe Perches			my $has_statement = 0;
5114c34c09a8SJoe Perches			my $count = 0;
5115c34c09a8SJoe Perches			my $prevline = $linenr;
5116e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5117c34c09a8SJoe Perches				$prevline--;
5118c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5119c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5120c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5121c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5122c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5123c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5124c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5125c34c09a8SJoe Perches				$has_statement = 1;
5126c34c09a8SJoe Perches				$count++;
5127c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5128c34c09a8SJoe Perches			}
5129c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5130c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5131c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5132c34c09a8SJoe Perches			}
5133c34c09a8SJoe Perches		}
5134c34c09a8SJoe Perches
5135d1e2ad07SJoe Perches# check for switch/default statements without a break;
5136d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5137d1e2ad07SJoe Perches		    defined $stat &&
5138d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5139d1e2ad07SJoe Perches			my $ctx = '';
5140d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5141d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5142d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5143d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5144d1e2ad07SJoe Perches			}
5145d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5146d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5147caf2a54fSJoe Perches		}
5148caf2a54fSJoe Perches
514913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5150d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5151d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5152d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5153d5e616fcSJoe Perches			    $fix) {
5154194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5155d5e616fcSJoe Perches			}
515613214adfSAndy Whitcroft		}
5157773647a0SAndy Whitcroft
515862ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
515962ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
516062ec818fSJoe Perches			ERROR("DATE_TIME",
516162ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
516262ec818fSJoe Perches		}
516362ec818fSJoe Perches
51642c92488aSJoe Perches# check for use of yield()
51652c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
51662c92488aSJoe Perches			WARN("YIELD",
51672c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
51682c92488aSJoe Perches		}
51692c92488aSJoe Perches
5170179f8f40SJoe Perches# check for comparisons against true and false
5171179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5172179f8f40SJoe Perches			my $lead = $1;
5173179f8f40SJoe Perches			my $arg = $2;
5174179f8f40SJoe Perches			my $test = $3;
5175179f8f40SJoe Perches			my $otype = $4;
5176179f8f40SJoe Perches			my $trail = $5;
5177179f8f40SJoe Perches			my $op = "!";
5178179f8f40SJoe Perches
5179179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5180179f8f40SJoe Perches
5181179f8f40SJoe Perches			my $type = lc($otype);
5182179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5183179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5184179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5185179f8f40SJoe Perches					$op = "";
5186179f8f40SJoe Perches				}
5187179f8f40SJoe Perches
5188179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5189179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5190179f8f40SJoe Perches
5191179f8f40SJoe Perches## maybe suggesting a correct construct would better
5192179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5193179f8f40SJoe Perches
5194179f8f40SJoe Perches			}
5195179f8f40SJoe Perches		}
5196179f8f40SJoe Perches
51974882720bSThomas Gleixner# check for semaphores initialized locked
51984882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5199000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5200000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5201773647a0SAndy Whitcroft		}
52026712d858SJoe Perches
520367d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
520467d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5205000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
520667d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5207773647a0SAndy Whitcroft		}
52086712d858SJoe Perches
5209ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5210f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5211000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5212ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5213f3db6639SMichael Ellerman		}
52146712d858SJoe Perches
521579404849SEmese Revfy# check for various ops structs, ensure they are const.
521679404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
521779404849SEmese Revfy				address_space_operations|
521879404849SEmese Revfy				backlight_ops|
521979404849SEmese Revfy				block_device_operations|
522079404849SEmese Revfy				dentry_operations|
522179404849SEmese Revfy				dev_pm_ops|
522279404849SEmese Revfy				dma_map_ops|
522379404849SEmese Revfy				extent_io_ops|
522479404849SEmese Revfy				file_lock_operations|
522579404849SEmese Revfy				file_operations|
522679404849SEmese Revfy				hv_ops|
522779404849SEmese Revfy				ide_dma_ops|
522879404849SEmese Revfy				intel_dvo_dev_ops|
522979404849SEmese Revfy				item_operations|
523079404849SEmese Revfy				iwl_ops|
523179404849SEmese Revfy				kgdb_arch|
523279404849SEmese Revfy				kgdb_io|
523379404849SEmese Revfy				kset_uevent_ops|
523479404849SEmese Revfy				lock_manager_operations|
523579404849SEmese Revfy				microcode_ops|
523679404849SEmese Revfy				mtrr_ops|
523779404849SEmese Revfy				neigh_ops|
523879404849SEmese Revfy				nlmsvc_binding|
523979404849SEmese Revfy				pci_raw_ops|
524079404849SEmese Revfy				pipe_buf_operations|
524179404849SEmese Revfy				platform_hibernation_ops|
524279404849SEmese Revfy				platform_suspend_ops|
524379404849SEmese Revfy				proto_ops|
524479404849SEmese Revfy				rpc_pipe_ops|
524579404849SEmese Revfy				seq_operations|
524679404849SEmese Revfy				snd_ac97_build_ops|
524779404849SEmese Revfy				soc_pcmcia_socket_ops|
524879404849SEmese Revfy				stacktrace_ops|
524979404849SEmese Revfy				sysfs_ops|
525079404849SEmese Revfy				tty_operations|
525179404849SEmese Revfy				usb_mon_operations|
525279404849SEmese Revfy				wd_ops}x;
52536903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
525479404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
5255000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5256000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
52576903ffb2SAndy Whitcroft				$herecurr);
52582b6db5cbSAndy Whitcroft		}
5259773647a0SAndy Whitcroft
5260773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5261773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5262773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5263c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5264c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5265171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5266171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5267171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5268773647a0SAndy Whitcroft		{
5269000d1cc1SJoe Perches			WARN("NR_CPUS",
5270000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5271773647a0SAndy Whitcroft		}
52729c9ba34eSAndy Whitcroft
527352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
527452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
527552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
527652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
527752ea8506SJoe Perches		}
527852ea8506SJoe Perches
5279acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5280acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5281acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5282acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5283acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5284acd9362cSJoe Perches		}
5285acd9362cSJoe Perches
5286691d77b6SAndy Whitcroft# whine mightly about in_atomic
5287691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5288691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5289000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5290000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5291f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5292000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5293000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5294691d77b6SAndy Whitcroft			}
5295691d77b6SAndy Whitcroft		}
52961704f47bSPeter Zijlstra
52971704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
52981704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
52991704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
53001704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
53011704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
53021704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5303000d1cc1SJoe Perches				ERROR("LOCKDEP",
5304000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
53051704f47bSPeter Zijlstra			}
53061704f47bSPeter Zijlstra		}
530788f8831cSDave Jones
530888f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
530988f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5310000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5311000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
531288f8831cSDave Jones		}
53132435880fSJoe Perches
5314515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5315515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5316515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5317515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
53182435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
53192435880fSJoe Perches				my $func = $entry->[0];
53202435880fSJoe Perches				my $arg_pos = $entry->[1];
53212435880fSJoe Perches
53222435880fSJoe Perches				my $skip_args = "";
53232435880fSJoe Perches				if ($arg_pos > 1) {
53242435880fSJoe Perches					$arg_pos--;
53252435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
53262435880fSJoe Perches				}
53272435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5328515a235eSJoe Perches				if ($line =~ /$test/) {
53292435880fSJoe Perches					my $val = $1;
53302435880fSJoe Perches					$val = $6 if ($skip_args ne "");
53312435880fSJoe Perches
53321727cc70SJoe Perches					if ($val !~ /^0$/ &&
53331727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
53341727cc70SJoe Perches					     length($val) ne 4)) {
53352435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
53361727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5337c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5338c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5339c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
53402435880fSJoe Perches					}
53412435880fSJoe Perches				}
53422435880fSJoe Perches			}
534313214adfSAndy Whitcroft		}
5344515a235eSJoe Perches	}
534513214adfSAndy Whitcroft
534613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
534713214adfSAndy Whitcroft	# so just keep quiet.
534813214adfSAndy Whitcroft	if ($#rawlines == -1) {
534913214adfSAndy Whitcroft		exit(0);
53500a920b5bSAndy Whitcroft	}
53510a920b5bSAndy Whitcroft
53528905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
53538905a67cSAndy Whitcroft	# things that appear to be patches.
53548905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
53558905a67cSAndy Whitcroft		exit(0);
53568905a67cSAndy Whitcroft	}
53578905a67cSAndy Whitcroft
53588905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
53598905a67cSAndy Whitcroft	# just keep quiet.
53608905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
53618905a67cSAndy Whitcroft		exit(0);
53628905a67cSAndy Whitcroft	}
53638905a67cSAndy Whitcroft
53648905a67cSAndy Whitcroft	if (!$is_patch) {
5365000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5366000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
53670a920b5bSAndy Whitcroft	}
53680a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5369000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5370000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
53710a920b5bSAndy Whitcroft	}
53720a920b5bSAndy Whitcroft
5373f0a594c1SAndy Whitcroft	print report_dump();
537413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
537513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
53766c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
53776c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
53786c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
53798905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
53806c72ffaaSAndy Whitcroft	}
53818905a67cSAndy Whitcroft
5382d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5383d1fe9c09SJoe Perches
5384d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5385d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5386d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5387d1fe9c09SJoe Perches		}
5388d1fe9c09SJoe Perches
5389d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5390d2c0a235SAndy Whitcroft		# then suggest that.
5391d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5392d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5393d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5394b0781216SMike Frysinger			$rpt_cleaners = 0;
5395d2c0a235SAndy Whitcroft		}
5396d2c0a235SAndy Whitcroft	}
5397d2c0a235SAndy Whitcroft
539891bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
539991bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5400000d1cc1SJoe Perches
5401d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5402d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5403d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
54049624b8d6SJoe Perches		my $newfile = $filename;
54059624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
54063705ce5bSJoe Perches		my $linecount = 0;
54073705ce5bSJoe Perches		my $f;
54083705ce5bSJoe Perches
5409d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5410d752fcc8SJoe Perches
54113705ce5bSJoe Perches		open($f, '>', $newfile)
54123705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
54133705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
54143705ce5bSJoe Perches			$linecount++;
54153705ce5bSJoe Perches			if ($file) {
54163705ce5bSJoe Perches				if ($linecount > 3) {
54173705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
54183705ce5bSJoe Perches					print $f $fixed_line . "\n";
54193705ce5bSJoe Perches				}
54203705ce5bSJoe Perches			} else {
54213705ce5bSJoe Perches				print $f $fixed_line . "\n";
54223705ce5bSJoe Perches			}
54233705ce5bSJoe Perches		}
54243705ce5bSJoe Perches		close($f);
54253705ce5bSJoe Perches
54263705ce5bSJoe Perches		if (!$quiet) {
54273705ce5bSJoe Perches			print << "EOM";
54283705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
54293705ce5bSJoe Perches
54303705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
54313705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
54323705ce5bSJoe Perches
54333705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
54343705ce5bSJoe PerchesNo warranties, expressed or implied...
54353705ce5bSJoe Perches
54363705ce5bSJoe PerchesEOM
54373705ce5bSJoe Perches		}
54383705ce5bSJoe Perches	}
54393705ce5bSJoe Perches
54400a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5441c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
54420a920b5bSAndy Whitcroft	}
54430a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5444000d1cc1SJoe Perches		print << "EOM";
5445000d1cc1SJoe Perches$vname has style problems, please review.
5446000d1cc1SJoe Perches
5447000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5448000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5449000d1cc1SJoe PerchesEOM
54500a920b5bSAndy Whitcroft	}
545113214adfSAndy Whitcroft
54520a920b5bSAndy Whitcroft	return $clean;
54530a920b5bSAndy Whitcroft}
5454