xref: /linux-6.15/scripts/checkpatch.pl (revision b671fde0)
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
3428ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
343fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
3448ed22cadSAndy Whitcroft	atomic_t
3458ed22cadSAndy Whitcroft)};
3468ed22cadSAndy Whitcroft
347691e669bSJoe Perchesour $logFunctions = qr{(?x:
3486e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
3497d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
3506e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
351b0531722SJoe Perches	panic|
35206668727SJoe Perches	MODULE_[A-Z_]+|
35306668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
354691e669bSJoe Perches)};
355691e669bSJoe Perches
35620112475SJoe Perchesour $signature_tags = qr{(?xi:
35720112475SJoe Perches	Signed-off-by:|
35820112475SJoe Perches	Acked-by:|
35920112475SJoe Perches	Tested-by:|
36020112475SJoe Perches	Reviewed-by:|
36120112475SJoe Perches	Reported-by:|
3628543ae12SMugunthan V N	Suggested-by:|
36320112475SJoe Perches	To:|
36420112475SJoe Perches	Cc:
36520112475SJoe Perches)};
36620112475SJoe Perches
3671813087dSJoe Perchesour @typeListMisordered = (
3681813087dSJoe Perches	qr{char\s+(?:un)?signed},
3691813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
3701813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
3711813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
3721813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
3731813087dSJoe Perches	qr{short\s+(?:un)?signed},
3741813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
3751813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
3761813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
3771813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
3781813087dSJoe Perches	qr{int\s+(?:un)?signed},
3791813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
3801813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
3811813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
3821813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
3831813087dSJoe Perches	qr{long\s+(?:un)?signed},
3841813087dSJoe Perches);
3851813087dSJoe Perches
3868905a67cSAndy Whitcroftour @typeList = (
3878905a67cSAndy Whitcroft	qr{void},
3880c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
3890c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
3900c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
3910c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
3920c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
3930c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
3940c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
3950c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
3960c773d9dSJoe Perches	qr{(?:un)?signed},
3978905a67cSAndy Whitcroft	qr{float},
3988905a67cSAndy Whitcroft	qr{double},
3998905a67cSAndy Whitcroft	qr{bool},
4008905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4018905a67cSAndy Whitcroft	qr{union\s+$Ident},
4028905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4038905a67cSAndy Whitcroft	qr{${Ident}_t},
4048905a67cSAndy Whitcroft	qr{${Ident}_handler},
4058905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4061813087dSJoe Perches	@typeListMisordered,
4078905a67cSAndy Whitcroft);
4088716de38SJoe Perchesour @typeListWithAttr = (
4098716de38SJoe Perches	@typeList,
4108716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4118716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
4128716de38SJoe Perches);
4138716de38SJoe Perches
414c45dcabdSAndy Whitcroftour @modifierList = (
415c45dcabdSAndy Whitcroft	qr{fastcall},
416c45dcabdSAndy Whitcroft);
4178905a67cSAndy Whitcroft
4182435880fSJoe Perchesour @mode_permission_funcs = (
4192435880fSJoe Perches	["module_param", 3],
4202435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
4212435880fSJoe Perches	["module_param_array_named", 5],
4222435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
4232435880fSJoe Perches	["proc_create(?:_data|)", 2],
4242435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
4252435880fSJoe Perches);
4262435880fSJoe Perches
427515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
428515a235eSJoe Perchesour $mode_perms_search = "";
429515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
430515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
431515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
432515a235eSJoe Perches}
433515a235eSJoe Perches
4347840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
4357840a94cSWolfram Sang	irq|
436cdcee686SSergey Ryazanov	memory|
437cdcee686SSergey Ryazanov	time|
438cdcee686SSergey Ryazanov	reboot
4397840a94cSWolfram Sang)};
4407840a94cSWolfram Sang# memory.h: ARM has a custom one
4417840a94cSWolfram Sang
44266b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
44366b47b4aSKees Cookmy $misspellings;
44466b47b4aSKees Cookmy %spelling_fix;
44536061e38SJoe Perches
44636061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
44736061e38SJoe Perches	my @spelling_list;
44866b47b4aSKees Cook	while (<$spelling>) {
44966b47b4aSKees Cook		my $line = $_;
45066b47b4aSKees Cook
45166b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
45266b47b4aSKees Cook		$line =~ s/^\s*//g;
45366b47b4aSKees Cook
45466b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
45566b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
45666b47b4aSKees Cook
45766b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
45866b47b4aSKees Cook
45966b47b4aSKees Cook		push(@spelling_list, $suspect);
46066b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
46166b47b4aSKees Cook	}
46266b47b4aSKees Cook	close($spelling);
46366b47b4aSKees Cook	$misspellings = join("|", @spelling_list);
46436061e38SJoe Perches} else {
46536061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
46636061e38SJoe Perches}
46766b47b4aSKees Cook
4688905a67cSAndy Whitcroftsub build_types {
469d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
470d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
4711813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
4728716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
473c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
4748905a67cSAndy Whitcroft	$NonptrType	= qr{
475d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
476cf655043SAndy Whitcroft			(?:
4776b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
4788ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
479c45dcabdSAndy Whitcroft				(?:${all}\b)
480cf655043SAndy Whitcroft			)
481c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
4828905a67cSAndy Whitcroft		  }x;
4831813087dSJoe Perches	$NonptrTypeMisordered	= qr{
4841813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
4851813087dSJoe Perches			(?:
4861813087dSJoe Perches				(?:${Misordered}\b)
4871813087dSJoe Perches			)
4881813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
4891813087dSJoe Perches		  }x;
4908716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
4918716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
4928716de38SJoe Perches			(?:
4938716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
4948716de38SJoe Perches				(?:$typeTypedefs\b)|
4958716de38SJoe Perches				(?:${allWithAttr}\b)
4968716de38SJoe Perches			)
4978716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
4988716de38SJoe Perches		  }x;
4998905a67cSAndy Whitcroft	$Type	= qr{
500c45dcabdSAndy Whitcroft			$NonptrType
5011574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
502c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
5038905a67cSAndy Whitcroft		  }x;
5041813087dSJoe Perches	$TypeMisordered	= qr{
5051813087dSJoe Perches			$NonptrTypeMisordered
5061813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
5071813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
5081813087dSJoe Perches		  }x;
50991cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
5101813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
5118905a67cSAndy Whitcroft}
5128905a67cSAndy Whitcroftbuild_types();
5136c72ffaaSAndy Whitcroft
5147d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
515d1fe9c09SJoe Perches
516d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
517d1fe9c09SJoe Perches# requires at least perl version v5.10.0
518d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
519d1fe9c09SJoe Perches
520d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
5212435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
522c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
5237d2367afSJoe Perches
524f8422308SJoe Perchesour $declaration_macros = qr{(?x:
525f8422308SJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
526f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
527f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
528f8422308SJoe Perches)};
529f8422308SJoe Perches
5307d2367afSJoe Perchessub deparenthesize {
5317d2367afSJoe Perches	my ($string) = @_;
5327d2367afSJoe Perches	return "" if (!defined($string));
5335b9553abSJoe Perches
5345b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
5355b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
5365b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
5375b9553abSJoe Perches	}
5385b9553abSJoe Perches
5397d2367afSJoe Perches	$string =~ s@\s+@ @g;
5405b9553abSJoe Perches
5417d2367afSJoe Perches	return $string;
5427d2367afSJoe Perches}
5437d2367afSJoe Perches
5443445686aSJoe Perchessub seed_camelcase_file {
5453445686aSJoe Perches	my ($file) = @_;
5463445686aSJoe Perches
5473445686aSJoe Perches	return if (!(-f $file));
5483445686aSJoe Perches
5493445686aSJoe Perches	local $/;
5503445686aSJoe Perches
5513445686aSJoe Perches	open(my $include_file, '<', "$file")
5523445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
5533445686aSJoe Perches	my $text = <$include_file>;
5543445686aSJoe Perches	close($include_file);
5553445686aSJoe Perches
5563445686aSJoe Perches	my @lines = split('\n', $text);
5573445686aSJoe Perches
5583445686aSJoe Perches	foreach my $line (@lines) {
5593445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
5603445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
5613445686aSJoe Perches			$camelcase{$1} = 1;
56211ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
56311ea516aSJoe Perches			$camelcase{$1} = 1;
56411ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
5653445686aSJoe Perches			$camelcase{$1} = 1;
5663445686aSJoe Perches		}
5673445686aSJoe Perches	}
5683445686aSJoe Perches}
5693445686aSJoe Perches
5703445686aSJoe Perchesmy $camelcase_seeded = 0;
5713445686aSJoe Perchessub seed_camelcase_includes {
5723445686aSJoe Perches	return if ($camelcase_seeded);
5733445686aSJoe Perches
5743445686aSJoe Perches	my $files;
575c707a81dSJoe Perches	my $camelcase_cache = "";
576c707a81dSJoe Perches	my @include_files = ();
577c707a81dSJoe Perches
578c707a81dSJoe Perches	$camelcase_seeded = 1;
579351b2a1fSJoe Perches
5803645e328SRichard Genoud	if (-e ".git") {
581351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
582351b2a1fSJoe Perches		chomp $git_last_include_commit;
583c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
584c707a81dSJoe Perches	} else {
585c707a81dSJoe Perches		my $last_mod_date = 0;
586c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
587c707a81dSJoe Perches		@include_files = split('\n', $files);
588c707a81dSJoe Perches		foreach my $file (@include_files) {
589c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
590c707a81dSJoe Perches						   localtime((stat $file)[9]));
591c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
592c707a81dSJoe Perches		}
593c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
594c707a81dSJoe Perches	}
595c707a81dSJoe Perches
596c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
597c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
598c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
599351b2a1fSJoe Perches		while (<$camelcase_file>) {
600351b2a1fSJoe Perches			chomp;
601351b2a1fSJoe Perches			$camelcase{$_} = 1;
602351b2a1fSJoe Perches		}
603351b2a1fSJoe Perches		close($camelcase_file);
604351b2a1fSJoe Perches
605351b2a1fSJoe Perches		return;
606351b2a1fSJoe Perches	}
607c707a81dSJoe Perches
6083645e328SRichard Genoud	if (-e ".git") {
609c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
610c707a81dSJoe Perches		@include_files = split('\n', $files);
6113445686aSJoe Perches	}
612c707a81dSJoe Perches
6133445686aSJoe Perches	foreach my $file (@include_files) {
6143445686aSJoe Perches		seed_camelcase_file($file);
6153445686aSJoe Perches	}
616351b2a1fSJoe Perches
617c707a81dSJoe Perches	if ($camelcase_cache ne "") {
618351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
619c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
620c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
621351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
622351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
623351b2a1fSJoe Perches		}
624351b2a1fSJoe Perches		close($camelcase_file);
625351b2a1fSJoe Perches	}
6263445686aSJoe Perches}
6273445686aSJoe Perches
628d311cd44SJoe Perchessub git_commit_info {
629d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
630d311cd44SJoe Perches
631d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
632d311cd44SJoe Perches
633d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
634d311cd44SJoe Perches	$output =~ s/^\s*//gm;
635d311cd44SJoe Perches	my @lines = split("\n", $output);
636d311cd44SJoe Perches
6370d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
6380d7835fcSJoe Perches
639d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
640d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
641d311cd44SJoe Perches# all matching commit ids, but it's very slow...
642d311cd44SJoe Perches#
643d311cd44SJoe Perches#		echo "checking commits $1..."
644d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
645d311cd44SJoe Perches#		while read line ; do
646d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
647d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
648d311cd44SJoe Perches#		done
649d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
650d311cd44SJoe Perches	} else {
651d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
652d311cd44SJoe Perches		$desc = substr($lines[0], 41);
653d311cd44SJoe Perches	}
654d311cd44SJoe Perches
655d311cd44SJoe Perches	return ($id, $desc);
656d311cd44SJoe Perches}
657d311cd44SJoe Perches
6586c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
6590a920b5bSAndy Whitcroft
66000df344fSAndy Whitcroftmy @rawlines = ();
661c2fdda0dSAndy Whitcroftmy @lines = ();
6623705ce5bSJoe Perchesmy @fixed = ();
663d752fcc8SJoe Perchesmy @fixed_inserted = ();
664d752fcc8SJoe Perchesmy @fixed_deleted = ();
665194f66fcSJoe Perchesmy $fixlinenr = -1;
666194f66fcSJoe Perches
667c2fdda0dSAndy Whitcroftmy $vname;
6686c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
66921caa13cSAndy Whitcroft	my $FILE;
6706c72ffaaSAndy Whitcroft	if ($file) {
67121caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
6726c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
67321caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
67421caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
6756c72ffaaSAndy Whitcroft	} else {
67621caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
6776c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
6786c72ffaaSAndy Whitcroft	}
679c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
680c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
681c2fdda0dSAndy Whitcroft	} else {
682c2fdda0dSAndy Whitcroft		$vname = $filename;
683c2fdda0dSAndy Whitcroft	}
68421caa13cSAndy Whitcroft	while (<$FILE>) {
6850a920b5bSAndy Whitcroft		chomp;
68600df344fSAndy Whitcroft		push(@rawlines, $_);
6876c72ffaaSAndy Whitcroft	}
68821caa13cSAndy Whitcroft	close($FILE);
689c2fdda0dSAndy Whitcroft	if (!process($filename)) {
6900a920b5bSAndy Whitcroft		$exit = 1;
6910a920b5bSAndy Whitcroft	}
69200df344fSAndy Whitcroft	@rawlines = ();
69313214adfSAndy Whitcroft	@lines = ();
6943705ce5bSJoe Perches	@fixed = ();
695d752fcc8SJoe Perches	@fixed_inserted = ();
696d752fcc8SJoe Perches	@fixed_deleted = ();
697194f66fcSJoe Perches	$fixlinenr = -1;
6980a920b5bSAndy Whitcroft}
6990a920b5bSAndy Whitcroft
7000a920b5bSAndy Whitcroftexit($exit);
7010a920b5bSAndy Whitcroft
7020a920b5bSAndy Whitcroftsub top_of_kernel_tree {
7036c72ffaaSAndy Whitcroft	my ($root) = @_;
7046c72ffaaSAndy Whitcroft
7056c72ffaaSAndy Whitcroft	my @tree_check = (
7066c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
7076c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
7086c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
7096c72ffaaSAndy Whitcroft	);
7106c72ffaaSAndy Whitcroft
7116c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
7126c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
7130a920b5bSAndy Whitcroft			return 0;
7140a920b5bSAndy Whitcroft		}
7156c72ffaaSAndy Whitcroft	}
7166c72ffaaSAndy Whitcroft	return 1;
7176c72ffaaSAndy Whitcroft}
7180a920b5bSAndy Whitcroft
71920112475SJoe Perchessub parse_email {
72020112475SJoe Perches	my ($formatted_email) = @_;
72120112475SJoe Perches
72220112475SJoe Perches	my $name = "";
72320112475SJoe Perches	my $address = "";
72420112475SJoe Perches	my $comment = "";
72520112475SJoe Perches
72620112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
72720112475SJoe Perches		$name = $1;
72820112475SJoe Perches		$address = $2;
72920112475SJoe Perches		$comment = $3 if defined $3;
73020112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
73120112475SJoe Perches		$address = $1;
73220112475SJoe Perches		$comment = $2 if defined $2;
73320112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
73420112475SJoe Perches		$address = $1;
73520112475SJoe Perches		$comment = $2 if defined $2;
73620112475SJoe Perches		$formatted_email =~ s/$address.*$//;
73720112475SJoe Perches		$name = $formatted_email;
7383705ce5bSJoe Perches		$name = trim($name);
73920112475SJoe Perches		$name =~ s/^\"|\"$//g;
74020112475SJoe Perches		# If there's a name left after stripping spaces and
74120112475SJoe Perches		# leading quotes, and the address doesn't have both
74220112475SJoe Perches		# leading and trailing angle brackets, the address
74320112475SJoe Perches		# is invalid. ie:
74420112475SJoe Perches		#   "joe smith [email protected]" bad
74520112475SJoe Perches		#   "joe smith <[email protected]" bad
74620112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
74720112475SJoe Perches			$name = "";
74820112475SJoe Perches			$address = "";
74920112475SJoe Perches			$comment = "";
75020112475SJoe Perches		}
75120112475SJoe Perches	}
75220112475SJoe Perches
7533705ce5bSJoe Perches	$name = trim($name);
75420112475SJoe Perches	$name =~ s/^\"|\"$//g;
7553705ce5bSJoe Perches	$address = trim($address);
75620112475SJoe Perches	$address =~ s/^\<|\>$//g;
75720112475SJoe Perches
75820112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
75920112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
76020112475SJoe Perches		$name = "\"$name\"";
76120112475SJoe Perches	}
76220112475SJoe Perches
76320112475SJoe Perches	return ($name, $address, $comment);
76420112475SJoe Perches}
76520112475SJoe Perches
76620112475SJoe Perchessub format_email {
76720112475SJoe Perches	my ($name, $address) = @_;
76820112475SJoe Perches
76920112475SJoe Perches	my $formatted_email;
77020112475SJoe Perches
7713705ce5bSJoe Perches	$name = trim($name);
77220112475SJoe Perches	$name =~ s/^\"|\"$//g;
7733705ce5bSJoe Perches	$address = trim($address);
77420112475SJoe Perches
77520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
77620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
77720112475SJoe Perches		$name = "\"$name\"";
77820112475SJoe Perches	}
77920112475SJoe Perches
78020112475SJoe Perches	if ("$name" eq "") {
78120112475SJoe Perches		$formatted_email = "$address";
78220112475SJoe Perches	} else {
78320112475SJoe Perches		$formatted_email = "$name <$address>";
78420112475SJoe Perches	}
78520112475SJoe Perches
78620112475SJoe Perches	return $formatted_email;
78720112475SJoe Perches}
78820112475SJoe Perches
789d311cd44SJoe Perchessub which {
790d311cd44SJoe Perches	my ($bin) = @_;
791d311cd44SJoe Perches
792d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
793d311cd44SJoe Perches		if (-e "$path/$bin") {
794d311cd44SJoe Perches			return "$path/$bin";
795d311cd44SJoe Perches		}
796d311cd44SJoe Perches	}
797d311cd44SJoe Perches
798d311cd44SJoe Perches	return "";
799d311cd44SJoe Perches}
800d311cd44SJoe Perches
801000d1cc1SJoe Perchessub which_conf {
802000d1cc1SJoe Perches	my ($conf) = @_;
803000d1cc1SJoe Perches
804000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
805000d1cc1SJoe Perches		if (-e "$path/$conf") {
806000d1cc1SJoe Perches			return "$path/$conf";
807000d1cc1SJoe Perches		}
808000d1cc1SJoe Perches	}
809000d1cc1SJoe Perches
810000d1cc1SJoe Perches	return "";
811000d1cc1SJoe Perches}
812000d1cc1SJoe Perches
8130a920b5bSAndy Whitcroftsub expand_tabs {
8140a920b5bSAndy Whitcroft	my ($str) = @_;
8150a920b5bSAndy Whitcroft
8160a920b5bSAndy Whitcroft	my $res = '';
8170a920b5bSAndy Whitcroft	my $n = 0;
8180a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
8190a920b5bSAndy Whitcroft		if ($c eq "\t") {
8200a920b5bSAndy Whitcroft			$res .= ' ';
8210a920b5bSAndy Whitcroft			$n++;
8220a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
8230a920b5bSAndy Whitcroft				$res .= ' ';
8240a920b5bSAndy Whitcroft			}
8250a920b5bSAndy Whitcroft			next;
8260a920b5bSAndy Whitcroft		}
8270a920b5bSAndy Whitcroft		$res .= $c;
8280a920b5bSAndy Whitcroft		$n++;
8290a920b5bSAndy Whitcroft	}
8300a920b5bSAndy Whitcroft
8310a920b5bSAndy Whitcroft	return $res;
8320a920b5bSAndy Whitcroft}
8336c72ffaaSAndy Whitcroftsub copy_spacing {
834773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
8356c72ffaaSAndy Whitcroft	return $res;
8366c72ffaaSAndy Whitcroft}
8370a920b5bSAndy Whitcroft
8384a0df2efSAndy Whitcroftsub line_stats {
8394a0df2efSAndy Whitcroft	my ($line) = @_;
8404a0df2efSAndy Whitcroft
8414a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
8424a0df2efSAndy Whitcroft	$line =~ s/^.//;
8434a0df2efSAndy Whitcroft	$line = expand_tabs($line);
8444a0df2efSAndy Whitcroft
8454a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
8464a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
8474a0df2efSAndy Whitcroft
8484a0df2efSAndy Whitcroft	return (length($line), length($white));
8494a0df2efSAndy Whitcroft}
8504a0df2efSAndy Whitcroft
851773647a0SAndy Whitcroftmy $sanitise_quote = '';
852773647a0SAndy Whitcroft
853773647a0SAndy Whitcroftsub sanitise_line_reset {
854773647a0SAndy Whitcroft	my ($in_comment) = @_;
855773647a0SAndy Whitcroft
856773647a0SAndy Whitcroft	if ($in_comment) {
857773647a0SAndy Whitcroft		$sanitise_quote = '*/';
858773647a0SAndy Whitcroft	} else {
859773647a0SAndy Whitcroft		$sanitise_quote = '';
860773647a0SAndy Whitcroft	}
861773647a0SAndy Whitcroft}
86200df344fSAndy Whitcroftsub sanitise_line {
86300df344fSAndy Whitcroft	my ($line) = @_;
86400df344fSAndy Whitcroft
86500df344fSAndy Whitcroft	my $res = '';
86600df344fSAndy Whitcroft	my $l = '';
86700df344fSAndy Whitcroft
868c2fdda0dSAndy Whitcroft	my $qlen = 0;
869773647a0SAndy Whitcroft	my $off = 0;
870773647a0SAndy Whitcroft	my $c;
87100df344fSAndy Whitcroft
872773647a0SAndy Whitcroft	# Always copy over the diff marker.
873773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
874773647a0SAndy Whitcroft
875773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
876773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
877773647a0SAndy Whitcroft
878773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
879773647a0SAndy Whitcroft		# and end, all to $;.
880773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
881773647a0SAndy Whitcroft			$sanitise_quote = '*/';
882773647a0SAndy Whitcroft
883773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
884773647a0SAndy Whitcroft			$off++;
88500df344fSAndy Whitcroft			next;
886773647a0SAndy Whitcroft		}
88781bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
888773647a0SAndy Whitcroft			$sanitise_quote = '';
889773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
890773647a0SAndy Whitcroft			$off++;
891773647a0SAndy Whitcroft			next;
892773647a0SAndy Whitcroft		}
893113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
894113f04a8SDaniel Walker			$sanitise_quote = '//';
895113f04a8SDaniel Walker
896113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
897113f04a8SDaniel Walker			$off++;
898113f04a8SDaniel Walker			next;
899113f04a8SDaniel Walker		}
900773647a0SAndy Whitcroft
901773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
902773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
903773647a0SAndy Whitcroft		    $c eq "\\") {
904773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
905773647a0SAndy Whitcroft			$off++;
906773647a0SAndy Whitcroft			next;
907773647a0SAndy Whitcroft		}
908773647a0SAndy Whitcroft		# Regular quotes.
909773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
910773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
911773647a0SAndy Whitcroft				$sanitise_quote = $c;
912773647a0SAndy Whitcroft
913773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
914773647a0SAndy Whitcroft				next;
915773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
916773647a0SAndy Whitcroft				$sanitise_quote = '';
91700df344fSAndy Whitcroft			}
91800df344fSAndy Whitcroft		}
919773647a0SAndy Whitcroft
920fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
921773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
922773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
923113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
924113f04a8SDaniel Walker			substr($res, $off, 1, $;);
925773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
926773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
92700df344fSAndy Whitcroft		} else {
928773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
92900df344fSAndy Whitcroft		}
930c2fdda0dSAndy Whitcroft	}
931c2fdda0dSAndy Whitcroft
932113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
933113f04a8SDaniel Walker		$sanitise_quote = '';
934113f04a8SDaniel Walker	}
935113f04a8SDaniel Walker
936c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
937c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
938c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
939c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
940c2fdda0dSAndy Whitcroft
941c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
942c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
943c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
944c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
945c2fdda0dSAndy Whitcroft	}
946c2fdda0dSAndy Whitcroft
94700df344fSAndy Whitcroft	return $res;
94800df344fSAndy Whitcroft}
94900df344fSAndy Whitcroft
950a6962d72SJoe Perchessub get_quoted_string {
951a6962d72SJoe Perches	my ($line, $rawline) = @_;
952a6962d72SJoe Perches
9535e4f6ba5SJoe Perches	return "" if ($line !~ m/(\"[X\t]+\")/g);
954a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
955a6962d72SJoe Perches}
956a6962d72SJoe Perches
9578905a67cSAndy Whitcroftsub ctx_statement_block {
9588905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
9598905a67cSAndy Whitcroft	my $line = $linenr - 1;
9608905a67cSAndy Whitcroft	my $blk = '';
9618905a67cSAndy Whitcroft	my $soff = $off;
9628905a67cSAndy Whitcroft	my $coff = $off - 1;
963773647a0SAndy Whitcroft	my $coff_set = 0;
9648905a67cSAndy Whitcroft
96513214adfSAndy Whitcroft	my $loff = 0;
96613214adfSAndy Whitcroft
9678905a67cSAndy Whitcroft	my $type = '';
9688905a67cSAndy Whitcroft	my $level = 0;
969a2750645SAndy Whitcroft	my @stack = ();
970cf655043SAndy Whitcroft	my $p;
9718905a67cSAndy Whitcroft	my $c;
9728905a67cSAndy Whitcroft	my $len = 0;
97313214adfSAndy Whitcroft
97413214adfSAndy Whitcroft	my $remainder;
9758905a67cSAndy Whitcroft	while (1) {
976a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
977a2750645SAndy Whitcroft
978773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
9798905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
9808905a67cSAndy Whitcroft		# context.
9818905a67cSAndy Whitcroft		if ($off >= $len) {
9828905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
983dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
984c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
9858905a67cSAndy Whitcroft				$remain--;
98613214adfSAndy Whitcroft				$loff = $len;
987c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
9888905a67cSAndy Whitcroft				$len = length($blk);
9898905a67cSAndy Whitcroft				$line++;
9908905a67cSAndy Whitcroft				last;
9918905a67cSAndy Whitcroft			}
9928905a67cSAndy Whitcroft			# Bail if there is no further context.
9938905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
99413214adfSAndy Whitcroft			if ($off >= $len) {
9958905a67cSAndy Whitcroft				last;
9968905a67cSAndy Whitcroft			}
997f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
998f74bd194SAndy Whitcroft				$level++;
999f74bd194SAndy Whitcroft				$type = '#';
1000f74bd194SAndy Whitcroft			}
10018905a67cSAndy Whitcroft		}
1002cf655043SAndy Whitcroft		$p = $c;
10038905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
100413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
10058905a67cSAndy Whitcroft
1006773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
10074635f4fbSAndy Whitcroft
10084635f4fbSAndy Whitcroft		# Handle nested #if/#else.
10094635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
10104635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
10114635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
10124635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
10134635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
10144635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
10154635f4fbSAndy Whitcroft		}
10164635f4fbSAndy Whitcroft
10178905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
10188905a67cSAndy Whitcroft		# outermost level.
10198905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
10208905a67cSAndy Whitcroft			last;
10218905a67cSAndy Whitcroft		}
10228905a67cSAndy Whitcroft
102313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1024773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1025773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1026773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1027773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1028773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1029773647a0SAndy Whitcroft			$coff_set = 1;
1030773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1031773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
103213214adfSAndy Whitcroft		}
103313214adfSAndy Whitcroft
10348905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
10358905a67cSAndy Whitcroft			$level++;
10368905a67cSAndy Whitcroft			$type = '(';
10378905a67cSAndy Whitcroft		}
10388905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
10398905a67cSAndy Whitcroft			$level--;
10408905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
10418905a67cSAndy Whitcroft
10428905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
10438905a67cSAndy Whitcroft				$coff = $off;
1044773647a0SAndy Whitcroft				$coff_set = 1;
1045773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
10468905a67cSAndy Whitcroft			}
10478905a67cSAndy Whitcroft		}
10488905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
10498905a67cSAndy Whitcroft			$level++;
10508905a67cSAndy Whitcroft			$type = '{';
10518905a67cSAndy Whitcroft		}
10528905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
10538905a67cSAndy Whitcroft			$level--;
10548905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
10558905a67cSAndy Whitcroft
10568905a67cSAndy Whitcroft			if ($level == 0) {
1057b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1058b998e001SPatrick Pannuto					$off++;
1059b998e001SPatrick Pannuto				}
10608905a67cSAndy Whitcroft				last;
10618905a67cSAndy Whitcroft			}
10628905a67cSAndy Whitcroft		}
1063f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1064f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1065f74bd194SAndy Whitcroft			$level--;
1066f74bd194SAndy Whitcroft			$type = '';
1067f74bd194SAndy Whitcroft			$off++;
1068f74bd194SAndy Whitcroft			last;
1069f74bd194SAndy Whitcroft		}
10708905a67cSAndy Whitcroft		$off++;
10718905a67cSAndy Whitcroft	}
1072a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
107313214adfSAndy Whitcroft	if ($off == $len) {
1074a3bb97a7SAndy Whitcroft		$loff = $len + 1;
107513214adfSAndy Whitcroft		$line++;
107613214adfSAndy Whitcroft		$remain--;
107713214adfSAndy Whitcroft	}
10788905a67cSAndy Whitcroft
10798905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
10808905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
10818905a67cSAndy Whitcroft
10828905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
10838905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
10848905a67cSAndy Whitcroft
1085773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
108613214adfSAndy Whitcroft
108713214adfSAndy Whitcroft	return ($statement, $condition,
108813214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
108913214adfSAndy Whitcroft}
109013214adfSAndy Whitcroft
1091cf655043SAndy Whitcroftsub statement_lines {
1092cf655043SAndy Whitcroft	my ($stmt) = @_;
1093cf655043SAndy Whitcroft
1094cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1095cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1096cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1097cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1098cf655043SAndy Whitcroft
1099cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1100cf655043SAndy Whitcroft
1101cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1102cf655043SAndy Whitcroft}
1103cf655043SAndy Whitcroft
1104cf655043SAndy Whitcroftsub statement_rawlines {
1105cf655043SAndy Whitcroft	my ($stmt) = @_;
1106cf655043SAndy Whitcroft
1107cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1108cf655043SAndy Whitcroft
1109cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1110cf655043SAndy Whitcroft}
1111cf655043SAndy Whitcroft
1112cf655043SAndy Whitcroftsub statement_block_size {
1113cf655043SAndy Whitcroft	my ($stmt) = @_;
1114cf655043SAndy Whitcroft
1115cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1116cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1117cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1118cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1119cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1120cf655043SAndy Whitcroft
1121cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1122cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1123cf655043SAndy Whitcroft
1124cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1125cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1126cf655043SAndy Whitcroft
1127cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1128cf655043SAndy Whitcroft		return $stmt_lines;
1129cf655043SAndy Whitcroft	} else {
1130cf655043SAndy Whitcroft		return $stmt_statements;
1131cf655043SAndy Whitcroft	}
1132cf655043SAndy Whitcroft}
1133cf655043SAndy Whitcroft
113413214adfSAndy Whitcroftsub ctx_statement_full {
113513214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
113613214adfSAndy Whitcroft	my ($statement, $condition, $level);
113713214adfSAndy Whitcroft
113813214adfSAndy Whitcroft	my (@chunks);
113913214adfSAndy Whitcroft
1140cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
114113214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
114213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1143773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
114413214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1145cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1146cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1147cf655043SAndy Whitcroft	}
1148cf655043SAndy Whitcroft
1149cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1150cf655043SAndy Whitcroft	# could continue the statement.
1151cf655043SAndy Whitcroft	for (;;) {
115213214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
115313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1154cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1155773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1156cf655043SAndy Whitcroft		#print "C: push\n";
1157cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
115813214adfSAndy Whitcroft	}
115913214adfSAndy Whitcroft
116013214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
11618905a67cSAndy Whitcroft}
11628905a67cSAndy Whitcroft
11634a0df2efSAndy Whitcroftsub ctx_block_get {
1164f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
11654a0df2efSAndy Whitcroft	my $line;
11664a0df2efSAndy Whitcroft	my $start = $linenr - 1;
11674a0df2efSAndy Whitcroft	my $blk = '';
11684a0df2efSAndy Whitcroft	my @o;
11694a0df2efSAndy Whitcroft	my @c;
11704a0df2efSAndy Whitcroft	my @res = ();
11714a0df2efSAndy Whitcroft
1172f0a594c1SAndy Whitcroft	my $level = 0;
11734635f4fbSAndy Whitcroft	my @stack = ($level);
117400df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
117500df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
117600df344fSAndy Whitcroft		$remain--;
117700df344fSAndy Whitcroft
117800df344fSAndy Whitcroft		$blk .= $rawlines[$line];
11794635f4fbSAndy Whitcroft
11804635f4fbSAndy Whitcroft		# Handle nested #if/#else.
118101464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
11824635f4fbSAndy Whitcroft			push(@stack, $level);
118301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
11844635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
118501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
11864635f4fbSAndy Whitcroft			$level = pop(@stack);
11874635f4fbSAndy Whitcroft		}
11884635f4fbSAndy Whitcroft
118901464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1190f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1191f0a594c1SAndy Whitcroft			if ($off > 0) {
1192f0a594c1SAndy Whitcroft				$off--;
1193f0a594c1SAndy Whitcroft				next;
1194f0a594c1SAndy Whitcroft			}
11954a0df2efSAndy Whitcroft
1196f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1197f0a594c1SAndy Whitcroft				$level--;
1198f0a594c1SAndy Whitcroft				last if ($level == 0);
1199f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1200f0a594c1SAndy Whitcroft				$level++;
1201f0a594c1SAndy Whitcroft			}
1202f0a594c1SAndy Whitcroft		}
12034a0df2efSAndy Whitcroft
1204f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
120500df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
12064a0df2efSAndy Whitcroft		}
12074a0df2efSAndy Whitcroft
1208f0a594c1SAndy Whitcroft		last if ($level == 0);
12094a0df2efSAndy Whitcroft	}
12104a0df2efSAndy Whitcroft
1211f0a594c1SAndy Whitcroft	return ($level, @res);
12124a0df2efSAndy Whitcroft}
12134a0df2efSAndy Whitcroftsub ctx_block_outer {
12144a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12154a0df2efSAndy Whitcroft
1216f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1217f0a594c1SAndy Whitcroft	return @r;
12184a0df2efSAndy Whitcroft}
12194a0df2efSAndy Whitcroftsub ctx_block {
12204a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
12214a0df2efSAndy Whitcroft
1222f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1223f0a594c1SAndy Whitcroft	return @r;
1224653d4876SAndy Whitcroft}
1225653d4876SAndy Whitcroftsub ctx_statement {
1226f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1227f0a594c1SAndy Whitcroft
1228f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1229f0a594c1SAndy Whitcroft	return @r;
1230f0a594c1SAndy Whitcroft}
1231f0a594c1SAndy Whitcroftsub ctx_block_level {
1232653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1233653d4876SAndy Whitcroft
1234f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
12354a0df2efSAndy Whitcroft}
12369c0ca6f9SAndy Whitcroftsub ctx_statement_level {
12379c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12389c0ca6f9SAndy Whitcroft
12399c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
12409c0ca6f9SAndy Whitcroft}
12414a0df2efSAndy Whitcroft
12424a0df2efSAndy Whitcroftsub ctx_locate_comment {
12434a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12444a0df2efSAndy Whitcroft
12454a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1246beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
12474a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
12484a0df2efSAndy Whitcroft
12494a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
12504a0df2efSAndy Whitcroft	# comment.
12514a0df2efSAndy Whitcroft	my $in_comment = 0;
12524a0df2efSAndy Whitcroft	$current_comment = '';
12534a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
125400df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
125500df344fSAndy Whitcroft		#warn "           $line\n";
12564a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
12574a0df2efSAndy Whitcroft			$in_comment = 1;
12584a0df2efSAndy Whitcroft		}
12594a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
12604a0df2efSAndy Whitcroft			$in_comment = 1;
12614a0df2efSAndy Whitcroft		}
12624a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
12634a0df2efSAndy Whitcroft			$current_comment = '';
12644a0df2efSAndy Whitcroft		}
12654a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
12664a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
12674a0df2efSAndy Whitcroft			$in_comment = 0;
12684a0df2efSAndy Whitcroft		}
12694a0df2efSAndy Whitcroft	}
12704a0df2efSAndy Whitcroft
12714a0df2efSAndy Whitcroft	chomp($current_comment);
12724a0df2efSAndy Whitcroft	return($current_comment);
12734a0df2efSAndy Whitcroft}
12744a0df2efSAndy Whitcroftsub ctx_has_comment {
12754a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
12764a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
12774a0df2efSAndy Whitcroft
127800df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
12794a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
12804a0df2efSAndy Whitcroft
12814a0df2efSAndy Whitcroft	return ($cmt ne '');
12824a0df2efSAndy Whitcroft}
12834a0df2efSAndy Whitcroft
12844d001e4dSAndy Whitcroftsub raw_line {
12854d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
12864d001e4dSAndy Whitcroft
12874d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
12884d001e4dSAndy Whitcroft	$cnt++;
12894d001e4dSAndy Whitcroft
12904d001e4dSAndy Whitcroft	my $line;
12914d001e4dSAndy Whitcroft	while ($cnt) {
12924d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
12934d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
12944d001e4dSAndy Whitcroft		$cnt--;
12954d001e4dSAndy Whitcroft	}
12964d001e4dSAndy Whitcroft
12974d001e4dSAndy Whitcroft	return $line;
12984d001e4dSAndy Whitcroft}
12994d001e4dSAndy Whitcroft
13000a920b5bSAndy Whitcroftsub cat_vet {
13010a920b5bSAndy Whitcroft	my ($vet) = @_;
13029c0ca6f9SAndy Whitcroft	my ($res, $coded);
13030a920b5bSAndy Whitcroft
13049c0ca6f9SAndy Whitcroft	$res = '';
13056c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
13066c72ffaaSAndy Whitcroft		$res .= $1;
13076c72ffaaSAndy Whitcroft		if ($2 ne '') {
13089c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
13096c72ffaaSAndy Whitcroft			$res .= $coded;
13106c72ffaaSAndy Whitcroft		}
13119c0ca6f9SAndy Whitcroft	}
13129c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
13130a920b5bSAndy Whitcroft
13149c0ca6f9SAndy Whitcroft	return $res;
13150a920b5bSAndy Whitcroft}
13160a920b5bSAndy Whitcroft
1317c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1318cf655043SAndy Whitcroftmy $av_pending;
1319c2fdda0dSAndy Whitcroftmy @av_paren_type;
13201f65f947SAndy Whitcroftmy $av_pend_colon;
1321c2fdda0dSAndy Whitcroft
1322c2fdda0dSAndy Whitcroftsub annotate_reset {
1323c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1324cf655043SAndy Whitcroft	$av_pending = '_';
1325cf655043SAndy Whitcroft	@av_paren_type = ('E');
13261f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1327c2fdda0dSAndy Whitcroft}
1328c2fdda0dSAndy Whitcroft
13296c72ffaaSAndy Whitcroftsub annotate_values {
13306c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
13316c72ffaaSAndy Whitcroft
13326c72ffaaSAndy Whitcroft	my $res;
13331f65f947SAndy Whitcroft	my $var = '_' x length($stream);
13346c72ffaaSAndy Whitcroft	my $cur = $stream;
13356c72ffaaSAndy Whitcroft
1336c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
13376c72ffaaSAndy Whitcroft
13386c72ffaaSAndy Whitcroft	while (length($cur)) {
1339773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1340cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1341171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
13426c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1343c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1344c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1345cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1346c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
13476c72ffaaSAndy Whitcroft			}
13486c72ffaaSAndy Whitcroft
1349c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
13509446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
13519446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1352addcdceaSAndy Whitcroft			$type = 'c';
13539446ef56SAndy Whitcroft
1354e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1355c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
13566c72ffaaSAndy Whitcroft			$type = 'T';
13576c72ffaaSAndy Whitcroft
1358389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1359389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1360389a2fe5SAndy Whitcroft			$type = 'T';
1361389a2fe5SAndy Whitcroft
1362c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1363171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1364c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1365171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1366171ae1a4SAndy Whitcroft			if ($2 ne '') {
1367cf655043SAndy Whitcroft				$av_pending = 'N';
1368171ae1a4SAndy Whitcroft			}
1369171ae1a4SAndy Whitcroft			$type = 'E';
1370171ae1a4SAndy Whitcroft
1371c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1372171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1373171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1374171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
13756c72ffaaSAndy Whitcroft
1376c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1377cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1378c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1379cf655043SAndy Whitcroft
1380cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1381cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1382171ae1a4SAndy Whitcroft			$type = 'E';
1383cf655043SAndy Whitcroft
1384c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1385cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1386cf655043SAndy Whitcroft			$av_preprocessor = 1;
1387cf655043SAndy Whitcroft
1388cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1389cf655043SAndy Whitcroft
1390171ae1a4SAndy Whitcroft			$type = 'E';
1391cf655043SAndy Whitcroft
1392c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1393cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1394cf655043SAndy Whitcroft
1395cf655043SAndy Whitcroft			$av_preprocessor = 1;
1396cf655043SAndy Whitcroft
1397cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1398cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1399cf655043SAndy Whitcroft			pop(@av_paren_type);
1400cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1401171ae1a4SAndy Whitcroft			$type = 'E';
14026c72ffaaSAndy Whitcroft
14036c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1404c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
14056c72ffaaSAndy Whitcroft
1406171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1407171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1408171ae1a4SAndy Whitcroft			$av_pending = $type;
1409171ae1a4SAndy Whitcroft			$type = 'N';
1410171ae1a4SAndy Whitcroft
14116c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1412c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
14136c72ffaaSAndy Whitcroft			if (defined $2) {
1414cf655043SAndy Whitcroft				$av_pending = 'V';
14156c72ffaaSAndy Whitcroft			}
14166c72ffaaSAndy Whitcroft			$type = 'N';
14176c72ffaaSAndy Whitcroft
141814b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1419c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
142014b111c1SAndy Whitcroft			$av_pending = 'E';
14216c72ffaaSAndy Whitcroft			$type = 'N';
14226c72ffaaSAndy Whitcroft
14231f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
14241f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
14251f65f947SAndy Whitcroft			$av_pend_colon = 'C';
14261f65f947SAndy Whitcroft			$type = 'N';
14271f65f947SAndy Whitcroft
142814b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1429c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
14306c72ffaaSAndy Whitcroft			$type = 'N';
14316c72ffaaSAndy Whitcroft
14326c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1433c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1434cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1435cf655043SAndy Whitcroft			$av_pending = '_';
14366c72ffaaSAndy Whitcroft			$type = 'N';
14376c72ffaaSAndy Whitcroft
14386c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1439cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1440cf655043SAndy Whitcroft			if ($new_type ne '_') {
1441cf655043SAndy Whitcroft				$type = $new_type;
1442c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1443c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
14446c72ffaaSAndy Whitcroft			} else {
1445c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
14466c72ffaaSAndy Whitcroft			}
14476c72ffaaSAndy Whitcroft
1448c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1449c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1450c8cb2ca3SAndy Whitcroft			$type = 'V';
1451cf655043SAndy Whitcroft			$av_pending = 'V';
14526c72ffaaSAndy Whitcroft
14538e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
14548e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
14551f65f947SAndy Whitcroft				$av_pend_colon = 'B';
14568e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
14578e761b04SAndy Whitcroft				$av_pend_colon = 'L';
14581f65f947SAndy Whitcroft			}
14591f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
14601f65f947SAndy Whitcroft			$type = 'V';
14611f65f947SAndy Whitcroft
14626c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1463c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
14646c72ffaaSAndy Whitcroft			$type = 'V';
14656c72ffaaSAndy Whitcroft
14666c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1467c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
14686c72ffaaSAndy Whitcroft			$type = 'N';
14696c72ffaaSAndy Whitcroft
1470cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1471c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
147213214adfSAndy Whitcroft			$type = 'E';
14731f65f947SAndy Whitcroft			$av_pend_colon = 'O';
147413214adfSAndy Whitcroft
14758e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
14768e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
14778e761b04SAndy Whitcroft			$type = 'C';
14788e761b04SAndy Whitcroft
14791f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
14801f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
14811f65f947SAndy Whitcroft			$type = 'N';
14821f65f947SAndy Whitcroft
14831f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
14841f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
14851f65f947SAndy Whitcroft
14861f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
14871f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
14881f65f947SAndy Whitcroft				$type = 'E';
14891f65f947SAndy Whitcroft			} else {
14901f65f947SAndy Whitcroft				$type = 'N';
14911f65f947SAndy Whitcroft			}
14921f65f947SAndy Whitcroft			$av_pend_colon = 'O';
14931f65f947SAndy Whitcroft
14948e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
149513214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
14966c72ffaaSAndy Whitcroft			$type = 'N';
14976c72ffaaSAndy Whitcroft
14980d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
149974048ed8SAndy Whitcroft			my $variant;
150074048ed8SAndy Whitcroft
150174048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
150274048ed8SAndy Whitcroft			if ($type eq 'V') {
150374048ed8SAndy Whitcroft				$variant = 'B';
150474048ed8SAndy Whitcroft			} else {
150574048ed8SAndy Whitcroft				$variant = 'U';
150674048ed8SAndy Whitcroft			}
150774048ed8SAndy Whitcroft
150874048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
150974048ed8SAndy Whitcroft			$type = 'N';
151074048ed8SAndy Whitcroft
15116c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1512c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
15136c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
15146c72ffaaSAndy Whitcroft				$type = 'N';
15156c72ffaaSAndy Whitcroft			}
15166c72ffaaSAndy Whitcroft
15176c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1518c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
15196c72ffaaSAndy Whitcroft		}
15206c72ffaaSAndy Whitcroft		if (defined $1) {
15216c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
15226c72ffaaSAndy Whitcroft			$res .= $type x length($1);
15236c72ffaaSAndy Whitcroft		}
15246c72ffaaSAndy Whitcroft	}
15256c72ffaaSAndy Whitcroft
15261f65f947SAndy Whitcroft	return ($res, $var);
15276c72ffaaSAndy Whitcroft}
15286c72ffaaSAndy Whitcroft
15298905a67cSAndy Whitcroftsub possible {
153013214adfSAndy Whitcroft	my ($possible, $line) = @_;
15319a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
15320776e594SAndy Whitcroft		^(?:
15330776e594SAndy Whitcroft			$Modifier|
15340776e594SAndy Whitcroft			$Storage|
15350776e594SAndy Whitcroft			$Type|
15369a974fdbSAndy Whitcroft			DEFINE_\S+
15379a974fdbSAndy Whitcroft		)$|
15389a974fdbSAndy Whitcroft		^(?:
15390776e594SAndy Whitcroft			goto|
15400776e594SAndy Whitcroft			return|
15410776e594SAndy Whitcroft			case|
15420776e594SAndy Whitcroft			else|
15430776e594SAndy Whitcroft			asm|__asm__|
154489a88353SAndy Whitcroft			do|
154589a88353SAndy Whitcroft			\#|
154689a88353SAndy Whitcroft			\#\#|
15479a974fdbSAndy Whitcroft		)(?:\s|$)|
15480776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
15499a974fdbSAndy Whitcroft	    )}x;
15509a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
15519a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1552c45dcabdSAndy Whitcroft		# Check for modifiers.
1553c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1554c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1555c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1556c45dcabdSAndy Whitcroft
1557c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1558c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1559d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
15609a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1561d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1562d2506586SAndy Whitcroft					push(@modifierList, $modifier);
1563d2506586SAndy Whitcroft				}
15649a974fdbSAndy Whitcroft			}
1565c45dcabdSAndy Whitcroft
1566c45dcabdSAndy Whitcroft		} else {
156713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
15688905a67cSAndy Whitcroft			push(@typeList, $possible);
1569c45dcabdSAndy Whitcroft		}
15708905a67cSAndy Whitcroft		build_types();
15710776e594SAndy Whitcroft	} else {
15720776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
15738905a67cSAndy Whitcroft	}
15748905a67cSAndy Whitcroft}
15758905a67cSAndy Whitcroft
15766c72ffaaSAndy Whitcroftmy $prefix = '';
15776c72ffaaSAndy Whitcroft
1578000d1cc1SJoe Perchessub show_type {
1579cbec18afSJoe Perches	my ($type) = @_;
158091bfe484SJoe Perches
1581cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1582cbec18afSJoe Perches
1583cbec18afSJoe Perches	return !defined $ignore_type{$type};
1584000d1cc1SJoe Perches}
1585000d1cc1SJoe Perches
1586f0a594c1SAndy Whitcroftsub report {
1587cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1588cbec18afSJoe Perches
1589cbec18afSJoe Perches	if (!show_type($type) ||
1590cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1591773647a0SAndy Whitcroft		return 0;
1592773647a0SAndy Whitcroft	}
1593000d1cc1SJoe Perches	my $line;
1594000d1cc1SJoe Perches	if ($show_types) {
1595cbec18afSJoe Perches		$line = "$prefix$level:$type: $msg\n";
1596000d1cc1SJoe Perches	} else {
1597cbec18afSJoe Perches		$line = "$prefix$level: $msg\n";
1598000d1cc1SJoe Perches	}
15998905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
16008905a67cSAndy Whitcroft
160113214adfSAndy Whitcroft	push(our @report, $line);
1602773647a0SAndy Whitcroft
1603773647a0SAndy Whitcroft	return 1;
1604f0a594c1SAndy Whitcroft}
1605cbec18afSJoe Perches
1606f0a594c1SAndy Whitcroftsub report_dump {
160713214adfSAndy Whitcroft	our @report;
1608f0a594c1SAndy Whitcroft}
1609000d1cc1SJoe Perches
1610d752fcc8SJoe Perchessub fixup_current_range {
1611d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1612d752fcc8SJoe Perches
1613d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1614d752fcc8SJoe Perches		my $o = $1;
1615d752fcc8SJoe Perches		my $l = $2;
1616d752fcc8SJoe Perches		my $no = $o + $offset;
1617d752fcc8SJoe Perches		my $nl = $l + $length;
1618d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1619d752fcc8SJoe Perches	}
1620d752fcc8SJoe Perches}
1621d752fcc8SJoe Perches
1622d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1623d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1624d752fcc8SJoe Perches
1625d752fcc8SJoe Perches	my $range_last_linenr = 0;
1626d752fcc8SJoe Perches	my $delta_offset = 0;
1627d752fcc8SJoe Perches
1628d752fcc8SJoe Perches	my $old_linenr = 0;
1629d752fcc8SJoe Perches	my $new_linenr = 0;
1630d752fcc8SJoe Perches
1631d752fcc8SJoe Perches	my $next_insert = 0;
1632d752fcc8SJoe Perches	my $next_delete = 0;
1633d752fcc8SJoe Perches
1634d752fcc8SJoe Perches	my @lines = ();
1635d752fcc8SJoe Perches
1636d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1637d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1638d752fcc8SJoe Perches
1639d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1640d752fcc8SJoe Perches		my $save_line = 1;
1641d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1642d752fcc8SJoe Perches		if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {	#new filename
1643d752fcc8SJoe Perches			$delta_offset = 0;
1644d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1645d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1646d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1647d752fcc8SJoe Perches		}
1648d752fcc8SJoe Perches
1649d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1650d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1651d752fcc8SJoe Perches			$save_line = 0;
1652d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1653d752fcc8SJoe Perches		}
1654d752fcc8SJoe Perches
1655d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1656d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1657d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1658d752fcc8SJoe Perches			$new_linenr++;
1659d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1660d752fcc8SJoe Perches		}
1661d752fcc8SJoe Perches
1662d752fcc8SJoe Perches		if ($save_line) {
1663d752fcc8SJoe Perches			push(@lines, $line);
1664d752fcc8SJoe Perches			$new_linenr++;
1665d752fcc8SJoe Perches		}
1666d752fcc8SJoe Perches
1667d752fcc8SJoe Perches		$old_linenr++;
1668d752fcc8SJoe Perches	}
1669d752fcc8SJoe Perches
1670d752fcc8SJoe Perches	return @lines;
1671d752fcc8SJoe Perches}
1672d752fcc8SJoe Perches
1673f2d7e4d4SJoe Perchessub fix_insert_line {
1674f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1675f2d7e4d4SJoe Perches
1676f2d7e4d4SJoe Perches	my $inserted = {
1677f2d7e4d4SJoe Perches		LINENR => $linenr,
1678f2d7e4d4SJoe Perches		LINE => $line,
1679f2d7e4d4SJoe Perches	};
1680f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1681f2d7e4d4SJoe Perches}
1682f2d7e4d4SJoe Perches
1683f2d7e4d4SJoe Perchessub fix_delete_line {
1684f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1685f2d7e4d4SJoe Perches
1686f2d7e4d4SJoe Perches	my $deleted = {
1687f2d7e4d4SJoe Perches		LINENR => $linenr,
1688f2d7e4d4SJoe Perches		LINE => $line,
1689f2d7e4d4SJoe Perches	};
1690f2d7e4d4SJoe Perches
1691f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1692f2d7e4d4SJoe Perches}
1693f2d7e4d4SJoe Perches
1694de7d4f0eSAndy Whitcroftsub ERROR {
1695cbec18afSJoe Perches	my ($type, $msg) = @_;
1696cbec18afSJoe Perches
1697cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1698de7d4f0eSAndy Whitcroft		our $clean = 0;
16996c72ffaaSAndy Whitcroft		our $cnt_error++;
17003705ce5bSJoe Perches		return 1;
1701de7d4f0eSAndy Whitcroft	}
17023705ce5bSJoe Perches	return 0;
1703773647a0SAndy Whitcroft}
1704de7d4f0eSAndy Whitcroftsub WARN {
1705cbec18afSJoe Perches	my ($type, $msg) = @_;
1706cbec18afSJoe Perches
1707cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1708de7d4f0eSAndy Whitcroft		our $clean = 0;
17096c72ffaaSAndy Whitcroft		our $cnt_warn++;
17103705ce5bSJoe Perches		return 1;
1711de7d4f0eSAndy Whitcroft	}
17123705ce5bSJoe Perches	return 0;
1713773647a0SAndy Whitcroft}
1714de7d4f0eSAndy Whitcroftsub CHK {
1715cbec18afSJoe Perches	my ($type, $msg) = @_;
1716cbec18afSJoe Perches
1717cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1718de7d4f0eSAndy Whitcroft		our $clean = 0;
17196c72ffaaSAndy Whitcroft		our $cnt_chk++;
17203705ce5bSJoe Perches		return 1;
17216c72ffaaSAndy Whitcroft	}
17223705ce5bSJoe Perches	return 0;
1723de7d4f0eSAndy Whitcroft}
1724de7d4f0eSAndy Whitcroft
17256ecd9674SAndy Whitcroftsub check_absolute_file {
17266ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
17276ecd9674SAndy Whitcroft	my $file = $absolute;
17286ecd9674SAndy Whitcroft
17296ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
17306ecd9674SAndy Whitcroft
17316ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
17326ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
17336ecd9674SAndy Whitcroft		if (-f "$root/$file") {
17346ecd9674SAndy Whitcroft			##print "file<$file>\n";
17356ecd9674SAndy Whitcroft			last;
17366ecd9674SAndy Whitcroft		}
17376ecd9674SAndy Whitcroft	}
17386ecd9674SAndy Whitcroft	if (! -f _)  {
17396ecd9674SAndy Whitcroft		return 0;
17406ecd9674SAndy Whitcroft	}
17416ecd9674SAndy Whitcroft
17426ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
17436ecd9674SAndy Whitcroft	my $prefix = $absolute;
17446ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
17456ecd9674SAndy Whitcroft
17466ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
17476ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1748000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1749000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
17506ecd9674SAndy Whitcroft	}
17516ecd9674SAndy Whitcroft}
17526ecd9674SAndy Whitcroft
17533705ce5bSJoe Perchessub trim {
17543705ce5bSJoe Perches	my ($string) = @_;
17553705ce5bSJoe Perches
1756b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1757b34c648bSJoe Perches
1758b34c648bSJoe Perches	return $string;
1759b34c648bSJoe Perches}
1760b34c648bSJoe Perches
1761b34c648bSJoe Perchessub ltrim {
1762b34c648bSJoe Perches	my ($string) = @_;
1763b34c648bSJoe Perches
1764b34c648bSJoe Perches	$string =~ s/^\s+//;
1765b34c648bSJoe Perches
1766b34c648bSJoe Perches	return $string;
1767b34c648bSJoe Perches}
1768b34c648bSJoe Perches
1769b34c648bSJoe Perchessub rtrim {
1770b34c648bSJoe Perches	my ($string) = @_;
1771b34c648bSJoe Perches
1772b34c648bSJoe Perches	$string =~ s/\s+$//;
17733705ce5bSJoe Perches
17743705ce5bSJoe Perches	return $string;
17753705ce5bSJoe Perches}
17763705ce5bSJoe Perches
177752ea8506SJoe Perchessub string_find_replace {
177852ea8506SJoe Perches	my ($string, $find, $replace) = @_;
177952ea8506SJoe Perches
178052ea8506SJoe Perches	$string =~ s/$find/$replace/g;
178152ea8506SJoe Perches
178252ea8506SJoe Perches	return $string;
178352ea8506SJoe Perches}
178452ea8506SJoe Perches
17853705ce5bSJoe Perchessub tabify {
17863705ce5bSJoe Perches	my ($leading) = @_;
17873705ce5bSJoe Perches
17883705ce5bSJoe Perches	my $source_indent = 8;
17893705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
17903705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
17913705ce5bSJoe Perches
17923705ce5bSJoe Perches	#convert leading spaces to tabs
17933705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
17943705ce5bSJoe Perches	#Remove spaces before a tab
17953705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
17963705ce5bSJoe Perches
17973705ce5bSJoe Perches	return "$leading";
17983705ce5bSJoe Perches}
17993705ce5bSJoe Perches
1800d1fe9c09SJoe Perchessub pos_last_openparen {
1801d1fe9c09SJoe Perches	my ($line) = @_;
1802d1fe9c09SJoe Perches
1803d1fe9c09SJoe Perches	my $pos = 0;
1804d1fe9c09SJoe Perches
1805d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1806d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1807d1fe9c09SJoe Perches
1808d1fe9c09SJoe Perches	my $last_openparen = 0;
1809d1fe9c09SJoe Perches
1810d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1811d1fe9c09SJoe Perches		return -1;
1812d1fe9c09SJoe Perches	}
1813d1fe9c09SJoe Perches
1814d1fe9c09SJoe Perches	my $len = length($line);
1815d1fe9c09SJoe Perches
1816d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1817d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1818d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1819d1fe9c09SJoe Perches			$pos += length($1) - 1;
1820d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1821d1fe9c09SJoe Perches			$last_openparen = $pos;
1822d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1823d1fe9c09SJoe Perches			last;
1824d1fe9c09SJoe Perches		}
1825d1fe9c09SJoe Perches	}
1826d1fe9c09SJoe Perches
182791cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1828d1fe9c09SJoe Perches}
1829d1fe9c09SJoe Perches
18300a920b5bSAndy Whitcroftsub process {
18310a920b5bSAndy Whitcroft	my $filename = shift;
18320a920b5bSAndy Whitcroft
18330a920b5bSAndy Whitcroft	my $linenr=0;
18340a920b5bSAndy Whitcroft	my $prevline="";
1835c2fdda0dSAndy Whitcroft	my $prevrawline="";
18360a920b5bSAndy Whitcroft	my $stashline="";
1837c2fdda0dSAndy Whitcroft	my $stashrawline="";
18380a920b5bSAndy Whitcroft
18394a0df2efSAndy Whitcroft	my $length;
18400a920b5bSAndy Whitcroft	my $indent;
18410a920b5bSAndy Whitcroft	my $previndent=0;
18420a920b5bSAndy Whitcroft	my $stashindent=0;
18430a920b5bSAndy Whitcroft
1844de7d4f0eSAndy Whitcroft	our $clean = 1;
18450a920b5bSAndy Whitcroft	my $signoff = 0;
18460a920b5bSAndy Whitcroft	my $is_patch = 0;
18470a920b5bSAndy Whitcroft
184829ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
184915662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
185013f1937eSJoe Perches	my $reported_maintainer_file = 0;
1851fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
1852fa64205dSPasi Savanainen
1853365dd4eaSJoe Perches	my $last_blank_line = 0;
18545e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
1855365dd4eaSJoe Perches
185613214adfSAndy Whitcroft	our @report = ();
18576c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
18586c72ffaaSAndy Whitcroft	our $cnt_error = 0;
18596c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
18606c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
18616c72ffaaSAndy Whitcroft
18620a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
18630a920b5bSAndy Whitcroft	my $realfile = '';
18640a920b5bSAndy Whitcroft	my $realline = 0;
18650a920b5bSAndy Whitcroft	my $realcnt = 0;
18660a920b5bSAndy Whitcroft	my $here = '';
18670a920b5bSAndy Whitcroft	my $in_comment = 0;
1868c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
18690a920b5bSAndy Whitcroft	my $first_line = 0;
18701e855726SWolfram Sang	my $p1_prefix = '';
18710a920b5bSAndy Whitcroft
187213214adfSAndy Whitcroft	my $prev_values = 'E';
187313214adfSAndy Whitcroft
187413214adfSAndy Whitcroft	# suppression flags
1875773647a0SAndy Whitcroft	my %suppress_ifbraces;
1876170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
18772b474a1aSAndy Whitcroft	my %suppress_export;
18783e469cdcSAndy Whitcroft	my $suppress_statement = 0;
1879653d4876SAndy Whitcroft
18807e51f197SJoe Perches	my %signatures = ();
1881323c1260SJoe Perches
1882c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1883de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1884c2fdda0dSAndy Whitcroft	#
1885de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1886de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1887773647a0SAndy Whitcroft
1888d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
1889d8b07710SJoe Perches
1890773647a0SAndy Whitcroft	sanitise_line_reset();
1891c2fdda0dSAndy Whitcroft	my $line;
1892c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1893773647a0SAndy Whitcroft		$linenr++;
1894773647a0SAndy Whitcroft		$line = $rawline;
1895c2fdda0dSAndy Whitcroft
18963705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
18973705ce5bSJoe Perches
1898773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1899de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1900de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1901de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1902de7d4f0eSAndy Whitcroft			}
1903773647a0SAndy Whitcroft			#next;
1904de7d4f0eSAndy Whitcroft		}
1905773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1906773647a0SAndy Whitcroft			$realline=$1-1;
1907773647a0SAndy Whitcroft			if (defined $2) {
1908773647a0SAndy Whitcroft				$realcnt=$3+1;
1909773647a0SAndy Whitcroft			} else {
1910773647a0SAndy Whitcroft				$realcnt=1+1;
1911773647a0SAndy Whitcroft			}
1912c45dcabdSAndy Whitcroft			$in_comment = 0;
1913773647a0SAndy Whitcroft
1914773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1915773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1916773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1917773647a0SAndy Whitcroft			# at context start.
1918773647a0SAndy Whitcroft			my $edge;
191901fa9147SAndy Whitcroft			my $cnt = $realcnt;
192001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
192101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
192201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
192301fa9147SAndy Whitcroft				$cnt--;
192401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1925721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1926fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1927fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1928fae17daeSAndy Whitcroft					($edge) = $1;
1929fae17daeSAndy Whitcroft					last;
1930fae17daeSAndy Whitcroft				}
1931773647a0SAndy Whitcroft			}
1932773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1933773647a0SAndy Whitcroft				$in_comment = 1;
1934773647a0SAndy Whitcroft			}
1935773647a0SAndy Whitcroft
1936773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1937773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1938773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1939773647a0SAndy Whitcroft			if (!defined $edge &&
194083242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1941773647a0SAndy Whitcroft			{
1942773647a0SAndy Whitcroft				$in_comment = 1;
1943773647a0SAndy Whitcroft			}
1944773647a0SAndy Whitcroft
1945773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1946773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1947773647a0SAndy Whitcroft
1948171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1949773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1950171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1951773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1952773647a0SAndy Whitcroft		}
1953773647a0SAndy Whitcroft		push(@lines, $line);
1954773647a0SAndy Whitcroft
1955773647a0SAndy Whitcroft		if ($realcnt > 1) {
1956773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1957773647a0SAndy Whitcroft		} else {
1958773647a0SAndy Whitcroft			$realcnt = 0;
1959773647a0SAndy Whitcroft		}
1960773647a0SAndy Whitcroft
1961773647a0SAndy Whitcroft		#print "==>$rawline\n";
1962773647a0SAndy Whitcroft		#print "-->$line\n";
1963de7d4f0eSAndy Whitcroft
1964de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1965de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1966de7d4f0eSAndy Whitcroft		}
1967de7d4f0eSAndy Whitcroft	}
1968de7d4f0eSAndy Whitcroft
19696c72ffaaSAndy Whitcroft	$prefix = '';
19706c72ffaaSAndy Whitcroft
1971773647a0SAndy Whitcroft	$realcnt = 0;
1972773647a0SAndy Whitcroft	$linenr = 0;
1973194f66fcSJoe Perches	$fixlinenr = -1;
19740a920b5bSAndy Whitcroft	foreach my $line (@lines) {
19750a920b5bSAndy Whitcroft		$linenr++;
1976194f66fcSJoe Perches		$fixlinenr++;
19771b5539b1SJoe Perches		my $sline = $line;	#copy of $line
19781b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
19790a920b5bSAndy Whitcroft
1980c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
19816c72ffaaSAndy Whitcroft
19820a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
19836c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
19840a920b5bSAndy Whitcroft			$is_patch = 1;
19854a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
19860a920b5bSAndy Whitcroft			$realline=$1-1;
19870a920b5bSAndy Whitcroft			if (defined $2) {
19880a920b5bSAndy Whitcroft				$realcnt=$3+1;
19890a920b5bSAndy Whitcroft			} else {
19900a920b5bSAndy Whitcroft				$realcnt=1+1;
19910a920b5bSAndy Whitcroft			}
1992c2fdda0dSAndy Whitcroft			annotate_reset();
199313214adfSAndy Whitcroft			$prev_values = 'E';
199413214adfSAndy Whitcroft
1995773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1996170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
19972b474a1aSAndy Whitcroft			%suppress_export = ();
19983e469cdcSAndy Whitcroft			$suppress_statement = 0;
19990a920b5bSAndy Whitcroft			next;
20000a920b5bSAndy Whitcroft
20014a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
20024a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
20034a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2004773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
20050a920b5bSAndy Whitcroft			$realline++;
2006d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
20070a920b5bSAndy Whitcroft
20084a0df2efSAndy Whitcroft			# Measure the line length and indent.
2009c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
20100a920b5bSAndy Whitcroft
20110a920b5bSAndy Whitcroft			# Track the previous line.
20120a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
20130a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2014c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2015c2fdda0dSAndy Whitcroft
2016773647a0SAndy Whitcroft			#warn "line<$line>\n";
20176c72ffaaSAndy Whitcroft
2018d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2019d8aaf121SAndy Whitcroft			$realcnt--;
20200a920b5bSAndy Whitcroft		}
20210a920b5bSAndy Whitcroft
2022cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2023cc77cdcaSAndy Whitcroft
20240a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
2025773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
2026773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
2027773647a0SAndy Whitcroft
20286c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
20296c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2030773647a0SAndy Whitcroft
20312ac73b4fSJoe Perches		my $found_file = 0;
2032773647a0SAndy Whitcroft		# extract the filename as it passes
20333bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
20343bf9a009SRabin Vincent			$realfile = $1;
20352b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2036270c49a0SJoe Perches			$in_commit_log = 0;
20372ac73b4fSJoe Perches			$found_file = 1;
20383bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2039773647a0SAndy Whitcroft			$realfile = $1;
20402b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2041270c49a0SJoe Perches			$in_commit_log = 0;
20421e855726SWolfram Sang
20431e855726SWolfram Sang			$p1_prefix = $1;
2044e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2045e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2046000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2047000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
20481e855726SWolfram Sang			}
2049773647a0SAndy Whitcroft
2050c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2051000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2052000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2053773647a0SAndy Whitcroft			}
20542ac73b4fSJoe Perches			$found_file = 1;
20552ac73b4fSJoe Perches		}
20562ac73b4fSJoe Perches
20572ac73b4fSJoe Perches		if ($found_file) {
20582ac73b4fSJoe Perches			if ($realfile =~ m@^(drivers/net/|net/)@) {
20592ac73b4fSJoe Perches				$check = 1;
20602ac73b4fSJoe Perches			} else {
20612ac73b4fSJoe Perches				$check = $check_orig;
20622ac73b4fSJoe Perches			}
2063773647a0SAndy Whitcroft			next;
2064773647a0SAndy Whitcroft		}
2065773647a0SAndy Whitcroft
2066389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
20670a920b5bSAndy Whitcroft
2068c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2069c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2070c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
20710a920b5bSAndy Whitcroft
20726c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
20736c72ffaaSAndy Whitcroft
20743bf9a009SRabin Vincent# Check for incorrect file permissions
20753bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
20763bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
207704db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
207804db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2079000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2080000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
20813bf9a009SRabin Vincent			}
20823bf9a009SRabin Vincent		}
20833bf9a009SRabin Vincent
208420112475SJoe Perches# Check the patch for a signoff:
2085d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
20864a0df2efSAndy Whitcroft			$signoff++;
208715662b3eSJoe Perches			$in_commit_log = 0;
20880a920b5bSAndy Whitcroft		}
208920112475SJoe Perches
2090e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2091e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2092e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2093e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2094e0d975b1SJoe Perches		}
2095e0d975b1SJoe Perches
209620112475SJoe Perches# Check signature styles
2097270c49a0SJoe Perches		if (!$in_header_lines &&
2098ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
209920112475SJoe Perches			my $space_before = $1;
210020112475SJoe Perches			my $sign_off = $2;
210120112475SJoe Perches			my $space_after = $3;
210220112475SJoe Perches			my $email = $4;
210320112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
210420112475SJoe Perches
2105ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2106ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2107ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2108ce0338dfSJoe Perches			}
210920112475SJoe Perches			if (defined $space_before && $space_before ne "") {
21103705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21113705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
21123705ce5bSJoe Perches				    $fix) {
2113194f66fcSJoe Perches					$fixed[$fixlinenr] =
21143705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21153705ce5bSJoe Perches				}
211620112475SJoe Perches			}
211720112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
21183705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21193705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
21203705ce5bSJoe Perches				    $fix) {
2121194f66fcSJoe Perches					$fixed[$fixlinenr] =
21223705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21233705ce5bSJoe Perches				}
21243705ce5bSJoe Perches
212520112475SJoe Perches			}
212620112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
21273705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
21283705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
21293705ce5bSJoe Perches				    $fix) {
2130194f66fcSJoe Perches					$fixed[$fixlinenr] =
21313705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
21323705ce5bSJoe Perches				}
213320112475SJoe Perches			}
213420112475SJoe Perches
213520112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
213620112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
213720112475SJoe Perches			if ($suggested_email eq "") {
2138000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2139000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
214020112475SJoe Perches			} else {
214120112475SJoe Perches				my $dequoted = $suggested_email;
214220112475SJoe Perches				$dequoted =~ s/^"//;
214320112475SJoe Perches				$dequoted =~ s/" </ </;
214420112475SJoe Perches				# Don't force email to have quotes
214520112475SJoe Perches				# Allow just an angle bracketed address
214620112475SJoe Perches				if ("$dequoted$comment" ne $email &&
214720112475SJoe Perches				    "<$email_address>$comment" ne $email &&
214820112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2149000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2150000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
215120112475SJoe Perches				}
21520a920b5bSAndy Whitcroft			}
21537e51f197SJoe Perches
21547e51f197SJoe Perches# Check for duplicate signatures
21557e51f197SJoe Perches			my $sig_nospace = $line;
21567e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
21577e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
21587e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
21597e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
21607e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
21617e51f197SJoe Perches			} else {
21627e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
21637e51f197SJoe Perches			}
21640a920b5bSAndy Whitcroft		}
21650a920b5bSAndy Whitcroft
21669b3189ebSJoe Perches# Check for old stable address
21679b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
21689b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
21699b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
21709b3189ebSJoe Perches		}
21719b3189ebSJoe Perches
21727ebd05efSChristopher Covington# Check for unwanted Gerrit info
21737ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
21747ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
21757ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
21767ebd05efSChristopher Covington		}
21777ebd05efSChristopher Covington
21780d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
21790d7835fcSJoe Perches		if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
2180d311cd44SJoe Perches			my $init_char = $1;
2181d311cd44SJoe Perches			my $orig_commit = lc($2);
21820d7835fcSJoe Perches			my $short = 1;
21830d7835fcSJoe Perches			my $long = 0;
21840d7835fcSJoe Perches			my $case = 1;
21850d7835fcSJoe Perches			my $space = 1;
21860d7835fcSJoe Perches			my $hasdesc = 0;
21870d7835fcSJoe Perches			my $id = '0123456789ab';
21880d7835fcSJoe Perches			my $orig_desc = "commit description";
21890d7835fcSJoe Perches			my $description = "";
21900d7835fcSJoe Perches
21910d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
21920d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
21930d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
21940d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
21950d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
21960d7835fcSJoe Perches				$orig_desc = $1;
21970d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
21980d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
21990d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
22000d7835fcSJoe Perches				$orig_desc = $1;
2201*b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2202*b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2203*b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2204*b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2205*b671fde0SJoe Perches				$orig_desc = $1;
2206*b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2207*b671fde0SJoe Perches				$orig_desc .= " " . $1;
22080d7835fcSJoe Perches			}
22090d7835fcSJoe Perches
22100d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
22110d7835fcSJoe Perches							      $id, $orig_desc);
22120d7835fcSJoe Perches
22130d7835fcSJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description)) {
2214d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
22150d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
22160d7835fcSJoe Perches			}
2217d311cd44SJoe Perches		}
2218d311cd44SJoe Perches
221913f1937eSJoe Perches# Check for added, moved or deleted files
222013f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
222113f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
222213f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
222313f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
222413f1937eSJoe Perches		      (defined($1) || defined($2))))) {
222513f1937eSJoe Perches			$reported_maintainer_file = 1;
222613f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
222713f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
222813f1937eSJoe Perches		}
222913f1937eSJoe Perches
223000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
22318905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2232000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2233000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
22346c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2235de7d4f0eSAndy Whitcroft		}
2236de7d4f0eSAndy Whitcroft
22376ecd9674SAndy Whitcroft# Check for absolute kernel paths.
22386ecd9674SAndy Whitcroft		if ($tree) {
22396ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
22406ecd9674SAndy Whitcroft				my $file = $1;
22416ecd9674SAndy Whitcroft
22426ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
22436ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
22446ecd9674SAndy Whitcroft					#
22456ecd9674SAndy Whitcroft				} else {
22466ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
22476ecd9674SAndy Whitcroft				}
22486ecd9674SAndy Whitcroft			}
22496ecd9674SAndy Whitcroft		}
22506ecd9674SAndy Whitcroft
2251de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2252de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2253171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2254171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2255171ae1a4SAndy Whitcroft
2256171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2257171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2258171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2259171ae1a4SAndy Whitcroft
226034d99219SJoe Perches			CHK("INVALID_UTF8",
2261000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
226200df344fSAndy Whitcroft		}
22630a920b5bSAndy Whitcroft
226415662b3eSJoe Perches# Check if it's the start of a commit log
226515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
226615662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
226729ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
226829ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
226915662b3eSJoe Perches			$in_header_lines = 0;
227015662b3eSJoe Perches			$in_commit_log = 1;
227115662b3eSJoe Perches		}
227215662b3eSJoe Perches
2273fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2274fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2275fa64205dSPasi Savanainen		if ($in_header_lines &&
2276fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2277fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2278fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2279fa64205dSPasi Savanainen		}
2280fa64205dSPasi Savanainen
2281fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
228215662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2283fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
228415662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
228515662b3eSJoe Perches		}
228615662b3eSJoe Perches
228766b47b4aSKees Cook# Check for various typo / spelling mistakes
228836061e38SJoe Perches		if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
228966b47b4aSKees Cook			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
229066b47b4aSKees Cook				my $typo = $1;
229166b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
229266b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
229366b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
229466b47b4aSKees Cook				my $msg_type = \&WARN;
229566b47b4aSKees Cook				$msg_type = \&CHK if ($file);
229666b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
229766b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
229866b47b4aSKees Cook				    $fix) {
229966b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
230066b47b4aSKees Cook				}
230166b47b4aSKees Cook			}
230266b47b4aSKees Cook		}
230366b47b4aSKees Cook
230430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
230530670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
230600df344fSAndy Whitcroft
23070a920b5bSAndy Whitcroft#trailing whitespace
23089c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2309c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2310d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2311d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2312d5e616fcSJoe Perches			    $fix) {
2313194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2314d5e616fcSJoe Perches			}
2315c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2316c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23173705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
23183705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
23193705ce5bSJoe Perches			    $fix) {
2320194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
23213705ce5bSJoe Perches			}
23223705ce5bSJoe Perches
2323d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
23240a920b5bSAndy Whitcroft		}
23255368df20SAndy Whitcroft
23264783f894SJosh Triplett# Check for FSF mailing addresses.
2327109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
23283e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
23293e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
23304783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
23314783f894SJosh Triplett			my $msg_type = \&ERROR;
23324783f894SJosh Triplett			$msg_type = \&CHK if ($file);
23334783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
23344783f894SJosh 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)
23354783f894SJosh Triplett		}
23364783f894SJosh Triplett
23373354957aSAndi Kleen# check for Kconfig help text having a real description
23389fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
23399fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
23403354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
23418d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
23423354957aSAndi Kleen			my $length = 0;
23439fe287d7SAndy Whitcroft			my $cnt = $realcnt;
23449fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
23459fe287d7SAndy Whitcroft			my $f;
2346a1385803SAndy Whitcroft			my $is_start = 0;
23479fe287d7SAndy Whitcroft			my $is_end = 0;
2348a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
23499fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
23509fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
23519fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
23529fe287d7SAndy Whitcroft
23539fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
23548d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2355a1385803SAndy Whitcroft
23568d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2357a1385803SAndy Whitcroft					$is_start = 1;
23588d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2359a1385803SAndy Whitcroft					$length = -1;
2360a1385803SAndy Whitcroft				}
2361a1385803SAndy Whitcroft
23629fe287d7SAndy Whitcroft				$f =~ s/^.//;
23633354957aSAndi Kleen				$f =~ s/#.*//;
23643354957aSAndi Kleen				$f =~ s/^\s+//;
23653354957aSAndi Kleen				next if ($f =~ /^$/);
23669fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
23679fe287d7SAndy Whitcroft					$is_end = 1;
23689fe287d7SAndy Whitcroft					last;
23699fe287d7SAndy Whitcroft				}
23703354957aSAndi Kleen				$length++;
23713354957aSAndi Kleen			}
237256193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2373000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
237456193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
237556193274SVadim Bendebury			}
2376a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
23773354957aSAndi Kleen		}
23783354957aSAndi Kleen
23791ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
23801ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
23811ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
23821ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
23831ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
23841ba8dfd1SKees Cook		}
23851ba8dfd1SKees Cook
2386327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2387327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2388327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2389327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2390327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2391327953e9SChristoph Jaeger		}
2392327953e9SChristoph Jaeger
2393c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2394c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2395c68e5878SArnaud Lacombe			my $flag = $1;
2396c68e5878SArnaud Lacombe			my $replacement = {
2397c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2398c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2399c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2400c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2401c68e5878SArnaud Lacombe			};
2402c68e5878SArnaud Lacombe
2403c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2404c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2405c68e5878SArnaud Lacombe		}
2406c68e5878SArnaud Lacombe
2407bff5da43SRob Herring# check for DT compatible documentation
24087dd05b38SFlorian Vaussard		if (defined $root &&
24097dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
24107dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
24117dd05b38SFlorian Vaussard
2412bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2413bff5da43SRob Herring
2414cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2415cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2416cc93319bSFlorian Vaussard
2417bff5da43SRob Herring			foreach my $compat (@compats) {
2418bff5da43SRob Herring				my $compat2 = $compat;
2419185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2420185d566bSRob Herring				my $compat3 = $compat;
2421185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2422185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2423bff5da43SRob Herring				if ( $? >> 8 ) {
2424bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2425bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2426bff5da43SRob Herring				}
2427bff5da43SRob Herring
24284fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
24294fbf32a6SFlorian Vaussard				my $vendor = $1;
2430cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2431bff5da43SRob Herring				if ( $? >> 8 ) {
2432bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2433cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2434bff5da43SRob Herring				}
2435bff5da43SRob Herring			}
2436bff5da43SRob Herring		}
2437bff5da43SRob Herring
24385368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2439de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
24405368df20SAndy Whitcroft
24416cd7f386SJoe Perches#line length limit
2442c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2443f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
24440fccc622SJoe Perches		    !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
24458bbea968SJoe Perches		    $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
24466cd7f386SJoe Perches		    $length > $max_line_length)
2447c45dcabdSAndy Whitcroft		{
2448000d1cc1SJoe Perches			WARN("LONG_LINE",
24496cd7f386SJoe Perches			     "line over $max_line_length characters\n" . $herecurr);
24500a920b5bSAndy Whitcroft		}
24510a920b5bSAndy Whitcroft
24528905a67cSAndy Whitcroft# check for adding lines without a newline.
24538905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2454000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2455000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
24568905a67cSAndy Whitcroft		}
24578905a67cSAndy Whitcroft
245842e41c54SMike Frysinger# Blackfin: use hi/lo macros
245942e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
246042e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
246142e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2462000d1cc1SJoe Perches				ERROR("LO_MACRO",
2463000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
246442e41c54SMike Frysinger			}
246542e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
246642e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2467000d1cc1SJoe Perches				ERROR("HI_MACRO",
2468000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
246942e41c54SMike Frysinger			}
247042e41c54SMike Frysinger		}
247142e41c54SMike Frysinger
2472b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2473de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
24740a920b5bSAndy Whitcroft
24750a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
24760a920b5bSAndy Whitcroft# more than 8 must use tabs.
2477c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2478c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2479c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2480d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
24813705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
24823705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
24833705ce5bSJoe Perches			    $fix) {
2484194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
24853705ce5bSJoe Perches			}
24860a920b5bSAndy Whitcroft		}
24870a920b5bSAndy Whitcroft
248808e44365SAlberto Panizzo# check for space before tabs.
248908e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
249008e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
24913705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
24923705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
24933705ce5bSJoe Perches			    $fix) {
2494194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2495d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2496194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2497c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
24983705ce5bSJoe Perches			}
249908e44365SAlberto Panizzo		}
250008e44365SAlberto Panizzo
2501d1fe9c09SJoe Perches# check for && or || at the start of a line
2502d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2503d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2504d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2505d1fe9c09SJoe Perches		}
2506d1fe9c09SJoe Perches
2507d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2508d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
250991cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2510d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2511d1fe9c09SJoe Perches			my $oldindent = $1;
2512d1fe9c09SJoe Perches			my $rest = $2;
2513d1fe9c09SJoe Perches
2514d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2515d1fe9c09SJoe Perches			if ($pos >= 0) {
2516b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2517b34a26f3SJoe Perches				my $newindent = $2;
2518d1fe9c09SJoe Perches
2519d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2520d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2521d1fe9c09SJoe Perches					" "  x ($pos % 8);
2522d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2523d1fe9c09SJoe Perches
2524d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2525d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
25263705ce5bSJoe Perches
25273705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
25283705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
25293705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2530194f66fcSJoe Perches						$fixed[$fixlinenr] =~
25313705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
25323705ce5bSJoe Perches					}
2533d1fe9c09SJoe Perches				}
2534d1fe9c09SJoe Perches			}
2535d1fe9c09SJoe Perches		}
2536d1fe9c09SJoe Perches
253704941aa7SJoe Perches		if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
253804941aa7SJoe Perches		    (!defined($1) || $1 !~ /sizeof\s*/)) {
25393705ce5bSJoe Perches			if (CHK("SPACING",
2540f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
25413705ce5bSJoe Perches			    $fix) {
2542194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2543f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
25443705ce5bSJoe Perches			}
2545aad4f614SJoe Perches		}
2546aad4f614SJoe Perches
254705880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2548fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
254985ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
255085ad978cSJoe Perches		    $realline > 2) {
255105880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
255205880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
255305880600SJoe Perches		}
255405880600SJoe Perches
255505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2556a605e32eSJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*/ &&		#starting /*
2557a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
255861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2559a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
2560a605e32eSJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2561a605e32eSJoe Perches			     "networking block comments start with * on subsequent lines\n" . $hereprev);
2562a605e32eSJoe Perches		}
2563a605e32eSJoe Perches
2564a605e32eSJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2565c24f9f19SJoe Perches		    $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2566c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2567c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2568c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
256905880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
257005880600SJoe Perches			     "networking block comments put the trailing */ on a separate line\n" . $herecurr);
257105880600SJoe Perches		}
257205880600SJoe Perches
25737f619191SJoe Perches# check for missing blank lines after struct/union declarations
25747f619191SJoe Perches# with exceptions for various attributes and macros
25757f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
25767f619191SJoe Perches		    $line =~ /^\+/ &&
25777f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
25787f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
25797f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
25807f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
25817f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
25827f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
25837f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
25847f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2585d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2586d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2587d752fcc8SJoe Perches			    $fix) {
2588f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2589d752fcc8SJoe Perches			}
25907f619191SJoe Perches		}
25917f619191SJoe Perches
2592365dd4eaSJoe Perches# check for multiple consecutive blank lines
2593365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2594365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2595365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2596d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2597d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2598d752fcc8SJoe Perches			    $fix) {
2599f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2600d752fcc8SJoe Perches			}
2601d752fcc8SJoe Perches
2602365dd4eaSJoe Perches			$last_blank_line = $linenr;
2603365dd4eaSJoe Perches		}
2604365dd4eaSJoe Perches
26053b617e3bSJoe Perches# check for missing blank lines after declarations
26063f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
26073f7bac03SJoe Perches			# actual declarations
26083f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26095a4e1fd3SJoe Perches			# function pointer declarations
26105a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26113f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26123f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26133f7bac03SJoe Perches			# known declaration macros
26143f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
26153f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
26163f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
26173f7bac03SJoe Perches			# other possible extensions of declaration lines
26183f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
26193f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
26203f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
26213f7bac03SJoe Perches			# looks like a declaration
26223f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
26235a4e1fd3SJoe Perches			# function pointer declarations
26245a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
26253f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
26263f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
26273f7bac03SJoe Perches			# known declaration macros
26283f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
26293f7bac03SJoe Perches			# start of struct or union or enum
26303b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
26313f7bac03SJoe Perches			# start or end of block or continuation of declaration
26323f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
26333f7bac03SJoe Perches			# bitfield continuation
26343f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
26353f7bac03SJoe Perches			# other possible extensions of declaration lines
26363f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
26373f7bac03SJoe Perches			# indentation of previous and current line are the same
26383f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2639d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2640d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2641d752fcc8SJoe Perches			    $fix) {
2642f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2643d752fcc8SJoe Perches			}
26443b617e3bSJoe Perches		}
26453b617e3bSJoe Perches
26465f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
26476b4c5bebSAndy Whitcroft# Exceptions:
26486b4c5bebSAndy Whitcroft#  1) within comments
26496b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
26506b4c5bebSAndy Whitcroft#  3) hanging labels
26513705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
26525f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26533705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
26543705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
26553705ce5bSJoe Perches			    $fix) {
2656194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
26573705ce5bSJoe Perches			}
26585f7ddae6SRaffaele Recalcati		}
26595f7ddae6SRaffaele Recalcati
2660b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2661b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2662b9ea10d6SAndy Whitcroft
2663032a4c0fSJoe Perches# check indentation of any line with a bare else
2664840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2665032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2666032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2667032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2668840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2669840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2670840080a0SJoe Perches			     defined $lines[$linenr] &&
2671840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2672032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2673032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2674032a4c0fSJoe Perches			}
2675032a4c0fSJoe Perches		}
2676032a4c0fSJoe Perches
2677c00df19aSJoe Perches# check indentation of a line with a break;
2678c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2679c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2680c00df19aSJoe Perches			my $tabs = $1;
2681c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2682c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2683c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2684c00df19aSJoe Perches			}
2685c00df19aSJoe Perches		}
2686c00df19aSJoe Perches
26871ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
26881ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
26891ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26901ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26911ba8dfd1SKees Cook		}
26921ba8dfd1SKees Cook
2693c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
2694cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2695000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
2696000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2697c2fdda0dSAndy Whitcroft		}
269822f2a2efSAndy Whitcroft
269942e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
270042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
270142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2702000d1cc1SJoe Perches			ERROR("CSYNC",
2703000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
270442e41c54SMike Frysinger		}
270542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
270642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
2707000d1cc1SJoe Perches			ERROR("SSYNC",
2708000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
270942e41c54SMike Frysinger		}
271042e41c54SMike Frysinger
271156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
271256e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
271356e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
271456e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
271556e77d70SJoe Perches		}
271656e77d70SJoe Perches
27179c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
27182b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
27192b474a1aSAndy Whitcroft		    $realline_next);
27203e469cdcSAndy Whitcroft#print "LINE<$line>\n";
27213e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
27221b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
2723170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2724f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
2725171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
2726171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
2727171ae1a4SAndy Whitcroft
27283e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
27293e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
27303e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
27313e469cdcSAndy Whitcroft			# until we hit end of it.
27323e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
27333e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
27343e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
27353e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
27363e469cdcSAndy Whitcroft			}
2737f74bd194SAndy Whitcroft
27382b474a1aSAndy Whitcroft			# Find the real next line.
27392b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
27402b474a1aSAndy Whitcroft			if (defined $realline_next &&
27412b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
27422b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
27432b474a1aSAndy Whitcroft				$realline_next++;
27442b474a1aSAndy Whitcroft			}
27452b474a1aSAndy Whitcroft
2746171ae1a4SAndy Whitcroft			my $s = $stat;
2747171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
2748cf655043SAndy Whitcroft
2749c2fdda0dSAndy Whitcroft			# Ignore goto labels.
2750171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
2751c2fdda0dSAndy Whitcroft
2752c2fdda0dSAndy Whitcroft			# Ignore functions being called
2753171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2754c2fdda0dSAndy Whitcroft
2755463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
2756463f2864SAndy Whitcroft
2757c45dcabdSAndy Whitcroft			# declarations always start with types
2758d2506586SAndy 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) {
2759c45dcabdSAndy Whitcroft				my $type = $1;
2760c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
2761c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
2762c45dcabdSAndy Whitcroft
27636c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
2764a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2765c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
2766c2fdda0dSAndy Whitcroft			}
27678905a67cSAndy Whitcroft
27686c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
276965863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2770c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
27719c0ca6f9SAndy Whitcroft			}
27728905a67cSAndy Whitcroft
27738905a67cSAndy Whitcroft			# Check for any sort of function declaration.
27748905a67cSAndy Whitcroft			# int foo(something bar, other baz);
27758905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
2776171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
27778905a67cSAndy Whitcroft				my ($name_len) = length($1);
27788905a67cSAndy Whitcroft
2779cf655043SAndy Whitcroft				my $ctx = $s;
2780773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
27818905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
2782cf655043SAndy Whitcroft
27838905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
2784c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
27858905a67cSAndy Whitcroft
2786c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
27878905a67cSAndy Whitcroft					}
27888905a67cSAndy Whitcroft				}
27898905a67cSAndy Whitcroft			}
27908905a67cSAndy Whitcroft
27919c0ca6f9SAndy Whitcroft		}
27929c0ca6f9SAndy Whitcroft
279300df344fSAndy Whitcroft#
279400df344fSAndy Whitcroft# Checks which may be anchored in the context.
279500df344fSAndy Whitcroft#
279600df344fSAndy Whitcroft
279700df344fSAndy Whitcroft# Check for switch () and associated case and default
279800df344fSAndy Whitcroft# statements should be at the same indent.
279900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
280000df344fSAndy Whitcroft			my $err = '';
280100df344fSAndy Whitcroft			my $sep = '';
280200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
280300df344fSAndy Whitcroft			shift(@ctx);
280400df344fSAndy Whitcroft			for my $ctx (@ctx) {
280500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
280600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
280700df344fSAndy Whitcroft							$indent != $cindent) {
280800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
280900df344fSAndy Whitcroft					$sep = '';
281000df344fSAndy Whitcroft				} else {
281100df344fSAndy Whitcroft					$sep = "[...]\n";
281200df344fSAndy Whitcroft				}
281300df344fSAndy Whitcroft			}
281400df344fSAndy Whitcroft			if ($err ne '') {
2815000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
2816000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
2817de7d4f0eSAndy Whitcroft			}
2818de7d4f0eSAndy Whitcroft		}
2819de7d4f0eSAndy Whitcroft
2820de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
2821de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
28220fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2823773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
2824773647a0SAndy Whitcroft
28259c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
28268eef05ddSJoe Perches
28278eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
28288eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
28298eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
28308eef05ddSJoe Perches			}
28318eef05ddSJoe Perches
2832de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
2833de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
2834de7d4f0eSAndy Whitcroft
2835548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
2836548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
2837de7d4f0eSAndy Whitcroft
2838548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2839548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
2840548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
2841548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2842548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2843773647a0SAndy Whitcroft				$ctx_ln++;
2844773647a0SAndy Whitcroft			}
2845548596d5SAndy Whitcroft
284653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
284753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2848773647a0SAndy Whitcroft
2849773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2850000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
2851000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
285201464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
285300df344fSAndy Whitcroft			}
2854773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2855773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
2856773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
2857773647a0SAndy Whitcroft			{
28589c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
28599c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
2860000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
2861000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
286201464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
28639c0ca6f9SAndy Whitcroft				}
28649c0ca6f9SAndy Whitcroft			}
286500df344fSAndy Whitcroft		}
286600df344fSAndy Whitcroft
28674d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
28680fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
28693e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
28703e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
28713e469cdcSAndy Whitcroft					if (!defined $stat);
28724d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
28734d001e4dSAndy Whitcroft
28744d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
28754d001e4dSAndy Whitcroft
28764d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
28774d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
28784d001e4dSAndy Whitcroft			# where necessary.
28794d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
28804d001e4dSAndy Whitcroft
28814d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
28826f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
28836f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
28844d001e4dSAndy Whitcroft
28854d001e4dSAndy Whitcroft			# We want to check the first line inside the block
28864d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
28874d001e4dSAndy Whitcroft			#  1) any blank line termination
28884d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
28894d001e4dSAndy Whitcroft			#  3) any do (...) {
28904d001e4dSAndy Whitcroft			my $continuation = 0;
28914d001e4dSAndy Whitcroft			my $check = 0;
28924d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
28934d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
28944d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
28954d001e4dSAndy Whitcroft				$continuation = 1;
28964d001e4dSAndy Whitcroft			}
28979bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
28984d001e4dSAndy Whitcroft				$check = 1;
28994d001e4dSAndy Whitcroft				$cond_lines++;
29004d001e4dSAndy Whitcroft			}
29014d001e4dSAndy Whitcroft
29024d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
29034d001e4dSAndy Whitcroft			# preprocessor statement.
29044d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
29054d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
29064d001e4dSAndy Whitcroft				$check = 0;
29074d001e4dSAndy Whitcroft			}
29084d001e4dSAndy Whitcroft
29099bd49efeSAndy Whitcroft			my $cond_ptr = -1;
2910740504c6SAndy Whitcroft			$continuation = 0;
29119bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
29129bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
29134d001e4dSAndy Whitcroft
2914f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
2915f16fa28fSAndy Whitcroft				# is not linear.
2916f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2917f16fa28fSAndy Whitcroft					$check = 0;
2918f16fa28fSAndy Whitcroft				}
2919f16fa28fSAndy Whitcroft
29209bd49efeSAndy Whitcroft				# Ignore:
29219bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
29229bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
29239bd49efeSAndy Whitcroft				#  3) labels.
2924740504c6SAndy Whitcroft				if ($continuation ||
2925740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
29269bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
29279bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
2928740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
292930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
29309bd49efeSAndy Whitcroft						$cond_lines++;
29319bd49efeSAndy Whitcroft					}
29324d001e4dSAndy Whitcroft				}
293330dad6ebSAndy Whitcroft			}
29344d001e4dSAndy Whitcroft
29354d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
29364d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
29374d001e4dSAndy Whitcroft
29384d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
29394d001e4dSAndy Whitcroft			# this is not this patch's fault.
29404d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
29414d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
29424d001e4dSAndy Whitcroft				$check = 0;
29434d001e4dSAndy Whitcroft			}
29444d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
29454d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
29464d001e4dSAndy Whitcroft			}
29474d001e4dSAndy Whitcroft
29489bd49efeSAndy 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";
29494d001e4dSAndy Whitcroft
29504d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
29514d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
2952000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
2953000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
29544d001e4dSAndy Whitcroft			}
29554d001e4dSAndy Whitcroft		}
29564d001e4dSAndy Whitcroft
29576c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
29586c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
29591f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
29601f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
29616c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
2962c2fdda0dSAndy Whitcroft		if ($dbg_values) {
2963c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
2964cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
2965cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
29661f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
2967c2fdda0dSAndy Whitcroft		}
29686c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
29696c72ffaaSAndy Whitcroft
297000df344fSAndy Whitcroft#ignore lines not being added
29713705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
297200df344fSAndy Whitcroft
2973653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
29747429c690SAndy Whitcroft		if ($dbg_type) {
29757429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
2976000d1cc1SJoe Perches				ERROR("TEST_TYPE",
2977000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
29787429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2979000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
2980000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
29817429c690SAndy Whitcroft			}
2982653d4876SAndy Whitcroft			next;
2983653d4876SAndy Whitcroft		}
2984a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
2985a1ef277eSAndy Whitcroft		if ($dbg_attr) {
29869360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
2987000d1cc1SJoe Perches				ERROR("TEST_ATTR",
2988000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
29899360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2990000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
2991000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
2992a1ef277eSAndy Whitcroft			}
2993a1ef277eSAndy Whitcroft			next;
2994a1ef277eSAndy Whitcroft		}
2995653d4876SAndy Whitcroft
2996f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
299799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
299899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
2999d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3000d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3001f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3002f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3003f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3004d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3005d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3006f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3007d752fcc8SJoe Perches				$fixedline = $line;
3008d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3009f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3010d752fcc8SJoe Perches			}
3011f0a594c1SAndy Whitcroft		}
3012f0a594c1SAndy Whitcroft
301300df344fSAndy Whitcroft#
301400df344fSAndy Whitcroft# Checks which are anchored on the added line.
301500df344fSAndy Whitcroft#
301600df344fSAndy Whitcroft
3017653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3018c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3019653d4876SAndy Whitcroft			my $path = $1;
3020653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3021000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3022495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3023495e9d84SJoe Perches			}
3024495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3025495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3026495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3027653d4876SAndy Whitcroft			}
3028653d4876SAndy Whitcroft		}
3029653d4876SAndy Whitcroft
303000df344fSAndy Whitcroft# no C99 // comments
303100df344fSAndy Whitcroft		if ($line =~ m{//}) {
30323705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
30333705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
30343705ce5bSJoe Perches			    $fix) {
3035194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
30363705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
30373705ce5bSJoe Perches					my $comment = trim($1);
3038194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
30393705ce5bSJoe Perches				}
30403705ce5bSJoe Perches			}
304100df344fSAndy Whitcroft		}
304200df344fSAndy Whitcroft		# Remove C99 comments.
30430a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
30446c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
30450a920b5bSAndy Whitcroft
30462b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
30472b474a1aSAndy Whitcroft# the whole statement.
30482b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
30492b474a1aSAndy Whitcroft		if (defined $realline_next &&
30502b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
30512b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
30522b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30532b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30543cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
30553cbf62dfSAndy Whitcroft			# a prefix:
30563cbf62dfSAndy Whitcroft			#   XXX(foo);
30573cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3058653d4876SAndy Whitcroft			my $name = $1;
305987a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
30603cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
30613cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
30623cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30633cbf62dfSAndy Whitcroft
30643cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
30652b474a1aSAndy Whitcroft				\n.}\s*$|
306648012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
306748012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
306848012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
30692b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
30702b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
307148012058SAndy Whitcroft			    )/x) {
30722b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
30732b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
30742b474a1aSAndy Whitcroft			} else {
30752b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
30760a920b5bSAndy Whitcroft			}
30770a920b5bSAndy Whitcroft		}
30782b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
30792b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
30802b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
30812b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
30822b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
30832b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
30842b474a1aSAndy Whitcroft		}
30852b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
30862b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3087000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3088000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
30892b474a1aSAndy Whitcroft		}
30900a920b5bSAndy Whitcroft
30915150bda4SJoe Eloff# check for global initialisers.
3092d5e616fcSJoe Perches		if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3093d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
3094000d1cc1SJoe Perches				  "do not initialise globals to 0 or NULL\n" .
3095d5e616fcSJoe Perches				      $herecurr) &&
3096d5e616fcSJoe Perches			    $fix) {
3097194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3098d5e616fcSJoe Perches			}
3099f0a594c1SAndy Whitcroft		}
31000a920b5bSAndy Whitcroft# check for static initialisers.
3101d5e616fcSJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3102d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
3103000d1cc1SJoe Perches				  "do not initialise statics to 0 or NULL\n" .
3104d5e616fcSJoe Perches				      $herecurr) &&
3105d5e616fcSJoe Perches			    $fix) {
3106194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3107d5e616fcSJoe Perches			}
31080a920b5bSAndy Whitcroft		}
31090a920b5bSAndy Whitcroft
31101813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
31111813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
31121813087dSJoe Perches			my $tmp = trim($1);
31131813087dSJoe Perches			WARN("MISORDERED_TYPE",
31141813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
31151813087dSJoe Perches		}
31161813087dSJoe Perches
3117cb710ecaSJoe Perches# check for static const char * arrays.
3118cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3119000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3120000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3121cb710ecaSJoe Perches				$herecurr);
3122cb710ecaSJoe Perches               }
3123cb710ecaSJoe Perches
3124cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3125cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3126000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3127000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3128cb710ecaSJoe Perches				$herecurr);
3129cb710ecaSJoe Perches               }
3130cb710ecaSJoe Perches
31319b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
31329b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
31339b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
31349b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
31359b0fa60dSJoe Perches				$herecurr);
31369b0fa60dSJoe Perches               }
31379b0fa60dSJoe Perches
3138b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3139b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3140b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3141b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3142b36190c5SJoe Perches			    $fix) {
3143194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3144b36190c5SJoe Perches			}
3145b36190c5SJoe Perches		}
3146b36190c5SJoe Perches
314792e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
314892e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
314992e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
315092e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
315192e112fdSJoe Perches			    $fix) {
3152194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
315392e112fdSJoe Perches			}
315493ed0e2dSJoe Perches		}
315593ed0e2dSJoe Perches
3156653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3157653d4876SAndy Whitcroft# make sense.
3158653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
31598054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3160c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
31618ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3162653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3163000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3164000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
31650a920b5bSAndy Whitcroft		}
31660a920b5bSAndy Whitcroft
31670a920b5bSAndy Whitcroft# * goes on variable not on type
316865863862SAndy Whitcroft		# (char*[ const])
3169bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3170bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
31713705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3172d8aaf121SAndy Whitcroft
317365863862SAndy Whitcroft			# Should start with a space.
317465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
317565863862SAndy Whitcroft			# Should not end with a space.
317665863862SAndy Whitcroft			$to =~ s/\s+$//;
317765863862SAndy Whitcroft			# '*'s should not have spaces between.
3178f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
317965863862SAndy Whitcroft			}
3180d8aaf121SAndy Whitcroft
31813705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
318265863862SAndy Whitcroft			if ($from ne $to) {
31833705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
31843705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
31853705ce5bSJoe Perches				    $fix) {
31863705ce5bSJoe Perches					my $sub_from = $ident;
31873705ce5bSJoe Perches					my $sub_to = $ident;
31883705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3189194f66fcSJoe Perches					$fixed[$fixlinenr] =~
31903705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
31913705ce5bSJoe Perches				}
319265863862SAndy Whitcroft			}
3193bfcb2cc7SAndy Whitcroft		}
3194bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3195bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
31963705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3197d8aaf121SAndy Whitcroft
319865863862SAndy Whitcroft			# Should start with a space.
319965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
320065863862SAndy Whitcroft			# Should not end with a space.
320165863862SAndy Whitcroft			$to =~ s/\s+$//;
320265863862SAndy Whitcroft			# '*'s should not have spaces between.
3203f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
320465863862SAndy Whitcroft			}
320565863862SAndy Whitcroft			# Modifiers should have spaces.
320665863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
320765863862SAndy Whitcroft
32083705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3209667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
32103705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
32113705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
32123705ce5bSJoe Perches				    $fix) {
32133705ce5bSJoe Perches
32143705ce5bSJoe Perches					my $sub_from = $match;
32153705ce5bSJoe Perches					my $sub_to = $match;
32163705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3217194f66fcSJoe Perches					$fixed[$fixlinenr] =~
32183705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
32193705ce5bSJoe Perches				}
322065863862SAndy Whitcroft			}
32210a920b5bSAndy Whitcroft		}
32220a920b5bSAndy Whitcroft
32230a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
32240a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
32250a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
32260a920b5bSAndy Whitcroft# 			print "$herecurr";
32270a920b5bSAndy Whitcroft# 			$clean = 0;
32280a920b5bSAndy Whitcroft# 		}
32290a920b5bSAndy Whitcroft
32308905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3231000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3232000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
32338905a67cSAndy Whitcroft		}
32348905a67cSAndy Whitcroft
323517441227SJoe Perches# check for uses of printk_ratelimit
323617441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3237000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3238000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
323917441227SJoe Perches		}
324017441227SJoe Perches
324100df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
324200df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
324300df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
324425985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
324500df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3246f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
324700df344fSAndy Whitcroft			my $ok = 0;
324800df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
324900df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
325025985edcSLucas De Marchi				# we have a preceding printk if it ends
325100df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
325200df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
325300df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
325400df344fSAndy Whitcroft						$ok = 1;
325500df344fSAndy Whitcroft					}
325600df344fSAndy Whitcroft					last;
325700df344fSAndy Whitcroft				}
325800df344fSAndy Whitcroft			}
325900df344fSAndy Whitcroft			if ($ok == 0) {
3260000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3261000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
32620a920b5bSAndy Whitcroft			}
326300df344fSAndy Whitcroft		}
32640a920b5bSAndy Whitcroft
3265243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3266243f3803SJoe Perches			my $orig = $1;
3267243f3803SJoe Perches			my $level = lc($orig);
3268243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
32698f26b837SJoe Perches			my $level2 = $level;
32708f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3271243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3272daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3273243f3803SJoe Perches		}
3274243f3803SJoe Perches
3275243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3276d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3277d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3278d5e616fcSJoe Perches			    $fix) {
3279194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3280d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3281d5e616fcSJoe Perches			}
3282243f3803SJoe Perches		}
3283243f3803SJoe Perches
3284dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3285dc139313SJoe Perches			my $orig = $1;
3286dc139313SJoe Perches			my $level = lc($orig);
3287dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3288dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3289dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3290dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3291dc139313SJoe Perches		}
3292dc139313SJoe Perches
3293653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3294653d4876SAndy Whitcroft# or if closed on same line
32958d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3296c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
32978d182478SJoe Perches			if (ERROR("OPEN_BRACE",
32988d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
32998d182478SJoe Perches			    $fix) {
33008d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
33018d182478SJoe Perches				my $fixed_line = $rawline;
33028d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
33038d182478SJoe Perches				my $line1 = $1;
33048d182478SJoe Perches				my $line2 = $2;
33058d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
33068d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
33078d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
33088d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
33098d182478SJoe Perches				}
33108d182478SJoe Perches			}
33110a920b5bSAndy Whitcroft		}
3312653d4876SAndy Whitcroft
33138905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
33148905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
33158905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
33168d182478SJoe Perches			if (ERROR("OPEN_BRACE",
33178d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
33188d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
33198d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
33208d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
33218d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
33228d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
33238d182478SJoe Perches				$fixedline = $rawline;
33248d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
33258d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
33268d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
33278d182478SJoe Perches				}
33288d182478SJoe Perches			}
33298905a67cSAndy Whitcroft		}
33308905a67cSAndy Whitcroft
33310c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
33323705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
33333705ce5bSJoe Perches			if (WARN("SPACING",
33343705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
33353705ce5bSJoe Perches			    $fix) {
3336194f66fcSJoe Perches				$fixed[$fixlinenr] =~
33373705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
33383705ce5bSJoe Perches			}
33390c73b4ebSAndy Whitcroft		}
33400c73b4ebSAndy Whitcroft
334131070b5dSJoe Perches# Function pointer declarations
334231070b5dSJoe Perches# check spacing between type, funcptr, and args
334331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
334491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
334531070b5dSJoe Perches			my $declare = $1;
334631070b5dSJoe Perches			my $pre_pointer_space = $2;
334731070b5dSJoe Perches			my $post_pointer_space = $3;
334831070b5dSJoe Perches			my $funcname = $4;
334931070b5dSJoe Perches			my $post_funcname_space = $5;
335031070b5dSJoe Perches			my $pre_args_space = $6;
335131070b5dSJoe Perches
335291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
335391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
335491f72e9cSJoe Perches# don't need a space so don't warn for those.
335591f72e9cSJoe Perches			my $post_declare_space = "";
335691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
335791f72e9cSJoe Perches				$post_declare_space = $1;
335891f72e9cSJoe Perches				$declare = rtrim($declare);
335991f72e9cSJoe Perches			}
336091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
336131070b5dSJoe Perches				WARN("SPACING",
336231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
336391f72e9cSJoe Perches				$post_declare_space = " ";
336431070b5dSJoe Perches			}
336531070b5dSJoe Perches
336631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
336791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
336891f72e9cSJoe Perches# equivalent to
336991f72e9cSJoe Perches#	int  foo(int bar, ...)
337091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
337191f72e9cSJoe Perches#
337291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
337391f72e9cSJoe Perches#				WARN("SPACING",
337491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
337591f72e9cSJoe Perches#			}
337631070b5dSJoe Perches
337731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
337831070b5dSJoe Perches			if (defined $pre_pointer_space &&
337931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
338031070b5dSJoe Perches				WARN("SPACING",
338131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
338231070b5dSJoe Perches			}
338331070b5dSJoe Perches
338431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
338531070b5dSJoe Perches			if (defined $post_pointer_space &&
338631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
338731070b5dSJoe Perches				WARN("SPACING",
338831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
338931070b5dSJoe Perches			}
339031070b5dSJoe Perches
339131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
339231070b5dSJoe Perches			if (defined $post_funcname_space &&
339331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
339431070b5dSJoe Perches				WARN("SPACING",
339531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
339631070b5dSJoe Perches			}
339731070b5dSJoe Perches
339831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
339931070b5dSJoe Perches			if (defined $pre_args_space &&
340031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
340131070b5dSJoe Perches				WARN("SPACING",
340231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
340331070b5dSJoe Perches			}
340431070b5dSJoe Perches
340531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3406194f66fcSJoe Perches				$fixed[$fixlinenr] =~
340791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
340831070b5dSJoe Perches			}
340931070b5dSJoe Perches		}
341031070b5dSJoe Perches
34118d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
34128d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3413fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3414fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
34158d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
34168d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
34178d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3418fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3419daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
34203705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
34213705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
34223705ce5bSJoe Perches				    $fix) {
3423194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
34243705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
34253705ce5bSJoe Perches				}
34268d31cfceSAndy Whitcroft			}
34278d31cfceSAndy Whitcroft		}
34288d31cfceSAndy Whitcroft
3429f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
34306c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3431c2fdda0dSAndy Whitcroft			my $name = $1;
3432773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3433773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3434c2fdda0dSAndy Whitcroft
3435c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3436773647a0SAndy Whitcroft			if ($name =~ /^(?:
3437773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3438773647a0SAndy Whitcroft				volatile|__volatile__|
3439773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3440773647a0SAndy Whitcroft				asm|__asm__)$/x)
3441773647a0SAndy Whitcroft			{
3442c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3443c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3444c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3445c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3446773647a0SAndy Whitcroft
3447773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3448c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3449c2fdda0dSAndy Whitcroft
3450c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3451c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3452773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3453c2fdda0dSAndy Whitcroft
3454c2fdda0dSAndy Whitcroft			} else {
34553705ce5bSJoe Perches				if (WARN("SPACING",
34563705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
34573705ce5bSJoe Perches					     $fix) {
3458194f66fcSJoe Perches					$fixed[$fixlinenr] =~
34593705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
34603705ce5bSJoe Perches				}
3461f0a594c1SAndy Whitcroft			}
34626c72ffaaSAndy Whitcroft		}
34639a4cad4eSEric Nelson
3464653d4876SAndy Whitcroft# Check operator spacing.
34650a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
34663705ce5bSJoe Perches			my $fixed_line = "";
34673705ce5bSJoe Perches			my $line_fixed = 0;
34683705ce5bSJoe Perches
34699c0ca6f9SAndy Whitcroft			my $ops = qr{
34709c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
34719c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
34729c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
34731f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
347484731623SJoe Perches				\?:|\?|:
34759c0ca6f9SAndy Whitcroft			}x;
3476cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
34773705ce5bSJoe Perches
34783705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
34793705ce5bSJoe Perches##			foreach my $el (@elements) {
34803705ce5bSJoe Perches##				print("el: <$el>\n");
34813705ce5bSJoe Perches##			}
34823705ce5bSJoe Perches
34833705ce5bSJoe Perches			my @fix_elements = ();
348400df344fSAndy Whitcroft			my $off = 0;
34856c72ffaaSAndy Whitcroft
34863705ce5bSJoe Perches			foreach my $el (@elements) {
34873705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
34883705ce5bSJoe Perches				$off += length($el);
34893705ce5bSJoe Perches			}
34903705ce5bSJoe Perches
34913705ce5bSJoe Perches			$off = 0;
34923705ce5bSJoe Perches
34936c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3494b34c648bSJoe Perches			my $last_after = -1;
34956c72ffaaSAndy Whitcroft
34960a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
34973705ce5bSJoe Perches
34983705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
34993705ce5bSJoe Perches
35003705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
35013705ce5bSJoe Perches
35024a0df2efSAndy Whitcroft				$off += length($elements[$n]);
35034a0df2efSAndy Whitcroft
350425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3505773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3506773647a0SAndy Whitcroft				my $cc = '';
3507773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3508773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3509773647a0SAndy Whitcroft				}
3510773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3511773647a0SAndy Whitcroft
35124a0df2efSAndy Whitcroft				my $a = '';
35134a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
35144a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3515cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
35164a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
35174a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3518773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
35194a0df2efSAndy Whitcroft
35200a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
35214a0df2efSAndy Whitcroft
35224a0df2efSAndy Whitcroft				my $c = '';
35230a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
35244a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
35254a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3526cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
35274a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
35284a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
35298b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
35304a0df2efSAndy Whitcroft				} else {
35314a0df2efSAndy Whitcroft					$c = 'E';
35320a920b5bSAndy Whitcroft				}
35330a920b5bSAndy Whitcroft
35344a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
35354a0df2efSAndy Whitcroft
35364a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
35374a0df2efSAndy Whitcroft
35386c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3539de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
35400a920b5bSAndy Whitcroft
354174048ed8SAndy Whitcroft				# Pull out the value of this operator.
35426c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
35430a920b5bSAndy Whitcroft
35441f65f947SAndy Whitcroft				# Get the full operator variant.
35451f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
35461f65f947SAndy Whitcroft
354713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
354813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
354913214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
355013214adfSAndy Whitcroft
3551cf655043SAndy Whitcroft#				# Ignore comments
3552cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
355313214adfSAndy Whitcroft
3554d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
355513214adfSAndy Whitcroft				} elsif ($op eq ';') {
3556cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3557cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
35583705ce5bSJoe Perches						if (ERROR("SPACING",
35593705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3560b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
35613705ce5bSJoe Perches							$line_fixed = 1;
35623705ce5bSJoe Perches						}
3563d8aaf121SAndy Whitcroft					}
3564d8aaf121SAndy Whitcroft
3565d8aaf121SAndy Whitcroft				# // is a comment
3566d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
35670a920b5bSAndy Whitcroft
3568b00e4814SJoe Perches				#   :   when part of a bitfield
3569b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3570b00e4814SJoe Perches					# skip the bitfield test for now
3571b00e4814SJoe Perches
35721f65f947SAndy Whitcroft				# No spaces for:
35731f65f947SAndy Whitcroft				#   ->
3574b00e4814SJoe Perches				} elsif ($op eq '->') {
35754a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
35763705ce5bSJoe Perches						if (ERROR("SPACING",
35773705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3578b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
35793705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
35803705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
35813705ce5bSJoe Perches							}
3582b34c648bSJoe Perches							$line_fixed = 1;
35833705ce5bSJoe Perches						}
35840a920b5bSAndy Whitcroft					}
35850a920b5bSAndy Whitcroft
35862381097bSJoe Perches				# , must not have a space before and must have a space on the right.
35870a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
35882381097bSJoe Perches					my $rtrim_before = 0;
35892381097bSJoe Perches					my $space_after = 0;
35902381097bSJoe Perches					if ($ctx =~ /Wx./) {
35912381097bSJoe Perches						if (ERROR("SPACING",
35922381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
35932381097bSJoe Perches							$line_fixed = 1;
35942381097bSJoe Perches							$rtrim_before = 1;
35952381097bSJoe Perches						}
35962381097bSJoe Perches					}
3597cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
35983705ce5bSJoe Perches						if (ERROR("SPACING",
35993705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
36003705ce5bSJoe Perches							$line_fixed = 1;
3601b34c648bSJoe Perches							$last_after = $n;
36022381097bSJoe Perches							$space_after = 1;
36032381097bSJoe Perches						}
36042381097bSJoe Perches					}
36052381097bSJoe Perches					if ($rtrim_before || $space_after) {
36062381097bSJoe Perches						if ($rtrim_before) {
36072381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36082381097bSJoe Perches						} else {
36092381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
36102381097bSJoe Perches						}
36112381097bSJoe Perches						if ($space_after) {
36122381097bSJoe Perches							$good .= " ";
36133705ce5bSJoe Perches						}
36140a920b5bSAndy Whitcroft					}
36150a920b5bSAndy Whitcroft
36169c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
361774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
36189c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
36199c0ca6f9SAndy Whitcroft
36209c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
36219c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
36229c0ca6f9SAndy Whitcroft				# unary operator, or a cast
36239c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
362474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
36250d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
3626cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
36273705ce5bSJoe Perches						if (ERROR("SPACING",
36283705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
3629b34c648bSJoe Perches							if ($n != $last_after + 2) {
3630b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
36313705ce5bSJoe Perches								$line_fixed = 1;
36323705ce5bSJoe Perches							}
36330a920b5bSAndy Whitcroft						}
3634b34c648bSJoe Perches					}
3635a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3636171ae1a4SAndy Whitcroft						# A unary '*' may be const
3637171ae1a4SAndy Whitcroft
3638171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
36393705ce5bSJoe Perches						if (ERROR("SPACING",
36403705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3641b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
36423705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36433705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
36443705ce5bSJoe Perches							}
3645b34c648bSJoe Perches							$line_fixed = 1;
36463705ce5bSJoe Perches						}
36470a920b5bSAndy Whitcroft					}
36480a920b5bSAndy Whitcroft
36490a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
36500a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
3651773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
36523705ce5bSJoe Perches						if (ERROR("SPACING",
36533705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
3654b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
36553705ce5bSJoe Perches							$line_fixed = 1;
36563705ce5bSJoe Perches						}
36570a920b5bSAndy Whitcroft					}
3658773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
3659773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
36603705ce5bSJoe Perches						if (ERROR("SPACING",
36613705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3662b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
36633705ce5bSJoe Perches							$line_fixed = 1;
36643705ce5bSJoe Perches						}
3665653d4876SAndy Whitcroft					}
3666773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
36673705ce5bSJoe Perches						if (ERROR("SPACING",
36683705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
3669b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
36703705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
36713705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3672773647a0SAndy Whitcroft							}
3673b34c648bSJoe Perches							$line_fixed = 1;
36743705ce5bSJoe Perches						}
36753705ce5bSJoe Perches					}
36760a920b5bSAndy Whitcroft
36770a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
36789c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
36799c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
36809c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
3681c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
3682c2fdda0dSAndy Whitcroft					 $op eq '%')
36830a920b5bSAndy Whitcroft				{
3684773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
36853705ce5bSJoe Perches						if (ERROR("SPACING",
36863705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
3687b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3688b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3689b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3690b34c648bSJoe Perches							}
36913705ce5bSJoe Perches							$line_fixed = 1;
36923705ce5bSJoe Perches						}
36930a920b5bSAndy Whitcroft					}
36940a920b5bSAndy Whitcroft
36951f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
36961f65f947SAndy Whitcroft				# terminating a case value or a label.
36971f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
36981f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
36993705ce5bSJoe Perches						if (ERROR("SPACING",
37003705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
3701b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
37023705ce5bSJoe Perches							$line_fixed = 1;
37033705ce5bSJoe Perches						}
37041f65f947SAndy Whitcroft					}
37051f65f947SAndy Whitcroft
37060a920b5bSAndy Whitcroft				# All the others need spaces both sides.
3707cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
37081f65f947SAndy Whitcroft					my $ok = 0;
37091f65f947SAndy Whitcroft
371022f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
37111f65f947SAndy Whitcroft					if (($op eq '<' &&
37121f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
37131f65f947SAndy Whitcroft					    ($op eq '>' &&
37141f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
37151f65f947SAndy Whitcroft					{
37161f65f947SAndy Whitcroft					    	$ok = 1;
37171f65f947SAndy Whitcroft					}
37181f65f947SAndy Whitcroft
371984731623SJoe Perches					# messages are ERROR, but ?: are CHK
37201f65f947SAndy Whitcroft					if ($ok == 0) {
372184731623SJoe Perches						my $msg_type = \&ERROR;
372284731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
372384731623SJoe Perches
372484731623SJoe Perches						if (&{$msg_type}("SPACING",
37253705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
3726b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3727b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
3728b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
3729b34c648bSJoe Perches							}
37303705ce5bSJoe Perches							$line_fixed = 1;
37313705ce5bSJoe Perches						}
37320a920b5bSAndy Whitcroft					}
373322f2a2efSAndy Whitcroft				}
37344a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
37353705ce5bSJoe Perches
37363705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
37373705ce5bSJoe Perches
37383705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
37390a920b5bSAndy Whitcroft			}
37403705ce5bSJoe Perches
37413705ce5bSJoe Perches			if (($#elements % 2) == 0) {
37423705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
37433705ce5bSJoe Perches			}
37443705ce5bSJoe Perches
3745194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3746194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
37473705ce5bSJoe Perches			}
37483705ce5bSJoe Perches
37493705ce5bSJoe Perches
37500a920b5bSAndy Whitcroft		}
37510a920b5bSAndy Whitcroft
3752786b6326SJoe Perches# check for whitespace before a non-naked semicolon
3753d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
3754786b6326SJoe Perches			if (WARN("SPACING",
3755786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
3756786b6326SJoe Perches			    $fix) {
3757194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
3758786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
3759786b6326SJoe Perches			}
3760786b6326SJoe Perches		}
3761786b6326SJoe Perches
3762f0a594c1SAndy Whitcroft# check for multiple assignments
3763f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3764000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
3765000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
3766f0a594c1SAndy Whitcroft		}
3767f0a594c1SAndy Whitcroft
376822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
376922f2a2efSAndy Whitcroft## # continuation.
377022f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
377122f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
377222f2a2efSAndy Whitcroft##
377322f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
377422f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
377522f2a2efSAndy Whitcroft## 			my $ln = $line;
377622f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
377722f2a2efSAndy Whitcroft## 			}
377822f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
3779000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
3780000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
378122f2a2efSAndy Whitcroft## 			}
378222f2a2efSAndy Whitcroft## 		}
3783f0a594c1SAndy Whitcroft
37840a920b5bSAndy Whitcroft#need space before brace following if, while, etc
378522f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
378622f2a2efSAndy Whitcroft		    $line =~ /do{/) {
37873705ce5bSJoe Perches			if (ERROR("SPACING",
37883705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
37893705ce5bSJoe Perches			    $fix) {
3790194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
37913705ce5bSJoe Perches			}
3792de7d4f0eSAndy Whitcroft		}
3793de7d4f0eSAndy Whitcroft
3794c4a62ef9SJoe Perches## # check for blank lines before declarations
3795c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3796c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
3797c4a62ef9SJoe Perches##			WARN("SPACING",
3798c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
3799c4a62ef9SJoe Perches##		}
3800c4a62ef9SJoe Perches##
3801c4a62ef9SJoe Perches
3802de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
3803de7d4f0eSAndy Whitcroft# on the line
3804de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
3805d5e616fcSJoe Perches			if (ERROR("SPACING",
3806d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
3807d5e616fcSJoe Perches			    $fix) {
3808194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3809d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
3810d5e616fcSJoe Perches			}
38110a920b5bSAndy Whitcroft		}
38120a920b5bSAndy Whitcroft
381322f2a2efSAndy Whitcroft# check spacing on square brackets
381422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
38153705ce5bSJoe Perches			if (ERROR("SPACING",
38163705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
38173705ce5bSJoe Perches			    $fix) {
3818194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38193705ce5bSJoe Perches				    s/\[\s+/\[/;
38203705ce5bSJoe Perches			}
382122f2a2efSAndy Whitcroft		}
382222f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
38233705ce5bSJoe Perches			if (ERROR("SPACING",
38243705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
38253705ce5bSJoe Perches			    $fix) {
3826194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38273705ce5bSJoe Perches				    s/\s+\]/\]/;
38283705ce5bSJoe Perches			}
382922f2a2efSAndy Whitcroft		}
383022f2a2efSAndy Whitcroft
3831c45dcabdSAndy Whitcroft# check spacing on parentheses
38329c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
38339c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
38343705ce5bSJoe Perches			if (ERROR("SPACING",
38353705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
38363705ce5bSJoe Perches			    $fix) {
3837194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38383705ce5bSJoe Perches				    s/\(\s+/\(/;
38393705ce5bSJoe Perches			}
384022f2a2efSAndy Whitcroft		}
384113214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3842c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
3843c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
38443705ce5bSJoe Perches			if (ERROR("SPACING",
38453705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
38463705ce5bSJoe Perches			    $fix) {
3847194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38483705ce5bSJoe Perches				    s/\s+\)/\)/;
38493705ce5bSJoe Perches			}
385022f2a2efSAndy Whitcroft		}
385122f2a2efSAndy Whitcroft
3852e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
3853e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3854e2826fd0SJoe Perches
3855e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3856ea4acbb1SJoe Perches			my $var = $1;
3857ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3858ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
3859ea4acbb1SJoe Perches			    $fix) {
3860ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3861ea4acbb1SJoe Perches			}
3862ea4acbb1SJoe Perches		}
3863ea4acbb1SJoe Perches
3864ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
3865ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
3866ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
3867ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3868ea4acbb1SJoe Perches			my $var = $2;
3869ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
3870ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3871ea4acbb1SJoe Perches			    $fix) {
3872ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
3873ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
3874ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3875ea4acbb1SJoe Perches			}
3876e2826fd0SJoe Perches		}
3877e2826fd0SJoe Perches
38780a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
38794a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
38800a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
38813705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
38823705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
38833705ce5bSJoe Perches			    $fix) {
3884194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38853705ce5bSJoe Perches				    s/^(.)\s+/$1/;
38863705ce5bSJoe Perches			}
38870a920b5bSAndy Whitcroft		}
38880a920b5bSAndy Whitcroft
38895b9553abSJoe Perches# return is not a function
3890507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3891c45dcabdSAndy Whitcroft			my $spacing = $1;
3892507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
38935b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
38945b9553abSJoe Perches				my $value = $1;
38955b9553abSJoe Perches				$value = deparenthesize($value);
38965b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3897000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
3898000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
38995b9553abSJoe Perches				}
3900c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
3901000d1cc1SJoe Perches				ERROR("SPACING",
3902000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
3903c45dcabdSAndy Whitcroft			}
3904c45dcabdSAndy Whitcroft		}
3905507e5141SJoe Perches
3906b43ae21bSJoe Perches# unnecessary return in a void function
3907b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
3908b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
3909b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
3910b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
3911b43ae21bSJoe Perches		    $linenr >= 3 &&
3912b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
3913b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
39149819cf25SJoe Perches			WARN("RETURN_VOID",
3915b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
39169819cf25SJoe Perches               }
39179819cf25SJoe Perches
3918189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
3919189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3920189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
3921189248d8SJoe Perches			my $openparens = $1;
3922189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
3923189248d8SJoe Perches			my $msg = "";
3924189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3925189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
3926189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
3927189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
3928189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
3929189248d8SJoe Perches			}
3930189248d8SJoe Perches		}
3931189248d8SJoe Perches
393253a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve
393353a3c448SAndy Whitcroft		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
393453a3c448SAndy Whitcroft			my $name = $1;
393553a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
3936000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
3937000d1cc1SJoe Perches				     "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
393853a3c448SAndy Whitcroft			}
393953a3c448SAndy Whitcroft		}
3940c45dcabdSAndy Whitcroft
39410a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
39424a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
39433705ce5bSJoe Perches			if (ERROR("SPACING",
39443705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
39453705ce5bSJoe Perches			    $fix) {
3946194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39473705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
39483705ce5bSJoe Perches			}
39490a920b5bSAndy Whitcroft		}
39500a920b5bSAndy Whitcroft
3951f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
3952f5fe35ddSAndy Whitcroft# statements after the conditional.
3953170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
39543e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
39553e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
39563e469cdcSAndy Whitcroft					if (!defined $stat);
3957170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
3958170d3a22SAndy Whitcroft						$remain_next, $off_next);
3959170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
3960170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
3961170d3a22SAndy Whitcroft
3962170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
3963170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
3964170d3a22SAndy Whitcroft				# then count those as offsets.
3965170d3a22SAndy Whitcroft				my ($whitespace) =
3966170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3967170d3a22SAndy Whitcroft				my $offset =
3968170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
3969170d3a22SAndy Whitcroft
3970170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
3971170d3a22SAndy Whitcroft								$offset} = 1;
3972170d3a22SAndy Whitcroft			}
3973170d3a22SAndy Whitcroft		}
3974170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
3975c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
3976170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3977171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
39788905a67cSAndy Whitcroft
3979b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3980000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
3981000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
39828905a67cSAndy Whitcroft			}
39838905a67cSAndy Whitcroft
39848905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
39858905a67cSAndy Whitcroft			# conditional.
3986773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
39878905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
398813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
398953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
399053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
3991773647a0SAndy Whitcroft			{
3992bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
3993bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
3994bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
399542bdf74cSHidetoshi Seto				my $stat_real = '';
3996bb44ad39SAndy Whitcroft
399742bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
399842bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
3999bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4000bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4001bb44ad39SAndy Whitcroft				}
4002bb44ad39SAndy Whitcroft
4003000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4004000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
40058905a67cSAndy Whitcroft			}
40068905a67cSAndy Whitcroft		}
40078905a67cSAndy Whitcroft
400813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
400913214adfSAndy Whitcroft		if ($line =~ /
401013214adfSAndy Whitcroft			(?:
401113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
401213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
401313214adfSAndy Whitcroft				(?:\&\&|\|\|)
401413214adfSAndy Whitcroft			|
401513214adfSAndy Whitcroft				(?:\&\&|\|\|)
401613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
401713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
401813214adfSAndy Whitcroft			)/x)
401913214adfSAndy Whitcroft		{
4020000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4021000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
402213214adfSAndy Whitcroft		}
402313214adfSAndy Whitcroft
40248905a67cSAndy Whitcroft# if and else should not have general statements after it
402513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
402613214adfSAndy Whitcroft			my $s = $1;
402713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
402813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4029000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4030000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
40310a920b5bSAndy Whitcroft			}
403213214adfSAndy Whitcroft		}
403339667782SAndy Whitcroft# if should not continue a brace
403439667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4035000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4036048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
403739667782SAndy Whitcroft				$herecurr);
403839667782SAndy Whitcroft		}
4039a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4040a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4041a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
40423fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4043a1080bf8SAndy Whitcroft			\s*return\s+
4044a1080bf8SAndy Whitcroft		    )/xg)
4045a1080bf8SAndy Whitcroft		{
4046000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4047000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4048a1080bf8SAndy Whitcroft		}
40490a920b5bSAndy Whitcroft
40500a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
40510a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
40528b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
40530a920b5bSAndy Whitcroft		    $previndent == $indent) {
40548b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
40558b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
40568b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
40578b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
40588b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
40598b8856f4SJoe Perches				my $fixedline = $prevrawline;
40608b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
40618b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
40628b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
40638b8856f4SJoe Perches				}
40648b8856f4SJoe Perches				$fixedline = $rawline;
40658b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
40668b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
40678b8856f4SJoe Perches			}
40680a920b5bSAndy Whitcroft		}
40690a920b5bSAndy Whitcroft
40708b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4071c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4072c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4073c2fdda0dSAndy Whitcroft
4074c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4075c2fdda0dSAndy Whitcroft			# conditional.
4076773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4077c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4078c2fdda0dSAndy Whitcroft
4079c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
40808b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
40818b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
40828b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
40838b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
40848b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
40858b8856f4SJoe Perches					my $fixedline = $prevrawline;
40868b8856f4SJoe Perches					my $trailing = $rawline;
40878b8856f4SJoe Perches					$trailing =~ s/^\+//;
40888b8856f4SJoe Perches					$trailing = trim($trailing);
40898b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
40908b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
40918b8856f4SJoe Perches				}
4092c2fdda0dSAndy Whitcroft			}
4093c2fdda0dSAndy Whitcroft		}
4094c2fdda0dSAndy Whitcroft
409595e2c602SJoe Perches#Specific variable tests
4096323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4097323c1260SJoe Perches			my $var = $1;
409895e2c602SJoe Perches
409995e2c602SJoe Perches#gcc binary extension
410095e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4101d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4102d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4103d5e616fcSJoe Perches				    $fix) {
4104d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4105194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4106d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4107d5e616fcSJoe Perches				}
410895e2c602SJoe Perches			}
410995e2c602SJoe Perches
411095e2c602SJoe Perches#CamelCase
4111807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4112be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
411322735ce8SJoe Perches#Ignore Page<foo> variants
4114807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
411522735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4116f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4117f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4118f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
41197e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
41207e781f67SJoe Perches					my $word = $1;
41217e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4122d8b07710SJoe Perches					if ($check) {
4123d8b07710SJoe Perches						seed_camelcase_includes();
4124d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4125d8b07710SJoe Perches							seed_camelcase_file($realfile);
4126d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4127d8b07710SJoe Perches						}
4128d8b07710SJoe Perches					}
41297e781f67SJoe Perches					if (!defined $camelcase{$word}) {
41307e781f67SJoe Perches						$camelcase{$word} = 1;
4131be79794bSJoe Perches						CHK("CAMELCASE",
41327e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
41337e781f67SJoe Perches					}
4134323c1260SJoe Perches				}
4135323c1260SJoe Perches			}
41363445686aSJoe Perches		}
41370a920b5bSAndy Whitcroft
41380a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4139d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4140d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4141d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4142d5e616fcSJoe Perches			    $fix) {
4143194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4144d5e616fcSJoe Perches			}
41450a920b5bSAndy Whitcroft		}
41460a920b5bSAndy Whitcroft
4147653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4148c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4149e09dec48SAndy Whitcroft			my $file = "$1.h";
4150e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4151e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4152e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
41537840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4154c45dcabdSAndy Whitcroft			{
4155e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
4156000d1cc1SJoe Perches					CHK("ARCH_INCLUDE_LINUX",
4157000d1cc1SJoe Perches					    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4158e09dec48SAndy Whitcroft				} else {
4159000d1cc1SJoe Perches					WARN("INCLUDE_LINUX",
4160000d1cc1SJoe Perches					     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4161e09dec48SAndy Whitcroft				}
41620a920b5bSAndy Whitcroft			}
41630a920b5bSAndy Whitcroft		}
41640a920b5bSAndy Whitcroft
4165653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4166653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4167cf655043SAndy Whitcroft# in a known good container
4168b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4169b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4170d8aaf121SAndy Whitcroft			my $ln = $linenr;
4171d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4172c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4173c45dcabdSAndy Whitcroft			my $ctx = '';
417408a2843eSJoe Perches			my $has_flow_statement = 0;
417508a2843eSJoe Perches			my $has_arg_concat = 0;
4176c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4177f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4178f74bd194SAndy Whitcroft			$ctx = $dstat;
4179c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4180a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4181c45dcabdSAndy Whitcroft
418208a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
418308a2843eSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/);
418408a2843eSJoe Perches
4185f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4186292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4187c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4188c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4189c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4190c45dcabdSAndy Whitcroft
4191c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4192bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4193bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
4194c81769fdSAndy Whitcroft			       $dstat =~ s/\[[^\[\]]*\]/1/)
4195bf30d6edSAndy Whitcroft			{
4196c45dcabdSAndy Whitcroft			}
4197c45dcabdSAndy Whitcroft
4198e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
4199e45bab8eSAndy Whitcroft			while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4200e45bab8eSAndy Whitcroft			       $dstat =~ s/$Ident\s*("X*")/$1/)
4201e45bab8eSAndy Whitcroft			{
4202e45bab8eSAndy Whitcroft			}
4203e45bab8eSAndy Whitcroft
4204c45dcabdSAndy Whitcroft			my $exceptions = qr{
4205c45dcabdSAndy Whitcroft				$Declare|
4206c45dcabdSAndy Whitcroft				module_param_named|
4207a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4208c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4209c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4210383099fdSAndy Whitcroft				__typeof__\(|
421122fd2d3eSStefani Seibold				union|
421222fd2d3eSStefani Seibold				struct|
4213ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
4214ea71a0a0SAndy Whitcroft				^\"|\"$
4215c45dcabdSAndy Whitcroft			}x;
42165eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4217f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4218f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4219f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
42203cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4221356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4222f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4223f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4224e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
422572f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4226f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4227f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4228f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
4229f95a7e6aSJoe Perches			    $dstat !~ /^\({/ &&						# ({...
4230f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4231c45dcabdSAndy Whitcroft			{
4232f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4233f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4234f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4235f74bd194SAndy Whitcroft
4236f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4237f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4238c45dcabdSAndy Whitcroft				}
4239c45dcabdSAndy Whitcroft
4240f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4241f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4242f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4243f74bd194SAndy Whitcroft				} else {
4244000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4245388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4246d8aaf121SAndy Whitcroft				}
42470a920b5bSAndy Whitcroft			}
42485023d347SJoe Perches
424908a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
425008a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
425108a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
425208a2843eSJoe Perches				my $herectx = $here . "\n";
425308a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
425408a2843eSJoe Perches
425508a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
425608a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
425708a2843eSJoe Perches				}
425808a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
425908a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
426008a2843eSJoe Perches			}
426108a2843eSJoe Perches
4262481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
42635023d347SJoe Perches
42645023d347SJoe Perches		} else {
42655023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4266481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4267481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
42685023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
42695023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
42705023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
42715023d347SJoe Perches			}
4272653d4876SAndy Whitcroft		}
42730a920b5bSAndy Whitcroft
4274b13edf7fSJoe Perches# do {} while (0) macro tests:
4275b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4276b13edf7fSJoe Perches# macro should not end with a semicolon
4277b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4278b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4279b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4280b13edf7fSJoe Perches			my $ln = $linenr;
4281b13edf7fSJoe Perches			my $cnt = $realcnt;
4282b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4283b13edf7fSJoe Perches			my $ctx = '';
4284b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4285b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4286b13edf7fSJoe Perches			$ctx = $dstat;
4287b13edf7fSJoe Perches
4288b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
42891b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4290b13edf7fSJoe Perches
4291b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4292b13edf7fSJoe Perches				my $stmts = $2;
4293b13edf7fSJoe Perches				my $semis = $3;
4294b13edf7fSJoe Perches
4295b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4296b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4297b13edf7fSJoe Perches				my $herectx = $here . "\n";
4298b13edf7fSJoe Perches
4299b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4300b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4301b13edf7fSJoe Perches				}
4302b13edf7fSJoe Perches
4303ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4304ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4305b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4306b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4307b13edf7fSJoe Perches				}
4308b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4309b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4310b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4311b13edf7fSJoe Perches				}
4312f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4313f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4314f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4315f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4316f5ef95b1SJoe Perches
4317f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4318f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4319f5ef95b1SJoe Perches				}
4320f5ef95b1SJoe Perches
4321f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4322f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4323b13edf7fSJoe Perches			}
4324b13edf7fSJoe Perches		}
4325b13edf7fSJoe Perches
4326080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4327080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4328080ba929SMike Frysinger#	.
4329080ba929SMike Frysinger#	ALIGN(...)
4330080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4331080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4332000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4333000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4334080ba929SMike Frysinger		}
4335080ba929SMike Frysinger
4336f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
433713214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
433813214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4339cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
434013214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4341cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4342cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4343aad4f614SJoe Perches				my @allowed = ();
4344aad4f614SJoe Perches				my $allow = 0;
434513214adfSAndy Whitcroft				my $seen = 0;
4346773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4347cf655043SAndy Whitcroft				my $ln = $linenr - 1;
434813214adfSAndy Whitcroft				for my $chunk (@chunks) {
434913214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
435013214adfSAndy Whitcroft
4351773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4352773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4353773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4354773647a0SAndy Whitcroft
4355aad4f614SJoe Perches					$allowed[$allow] = 0;
4356773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4357773647a0SAndy Whitcroft
4358773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4359773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4360773647a0SAndy Whitcroft
4361773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4362cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4363cf655043SAndy Whitcroft
4364773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
436513214adfSAndy Whitcroft
436613214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
436713214adfSAndy Whitcroft
4368aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4369cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4370cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4371aad4f614SJoe Perches						$allowed[$allow] = 1;
437213214adfSAndy Whitcroft					}
437313214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4374cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4375aad4f614SJoe Perches						$allowed[$allow] = 1;
437613214adfSAndy Whitcroft					}
4377cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4378cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4379aad4f614SJoe Perches						$allowed[$allow] = 1;
438013214adfSAndy Whitcroft					}
4381aad4f614SJoe Perches					$allow++;
438213214adfSAndy Whitcroft				}
4383aad4f614SJoe Perches				if ($seen) {
4384aad4f614SJoe Perches					my $sum_allowed = 0;
4385aad4f614SJoe Perches					foreach (@allowed) {
4386aad4f614SJoe Perches						$sum_allowed += $_;
4387aad4f614SJoe Perches					}
4388aad4f614SJoe Perches					if ($sum_allowed == 0) {
4389000d1cc1SJoe Perches						WARN("BRACES",
4390000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4391aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4392aad4f614SJoe Perches						 $seen != $allow) {
4393aad4f614SJoe Perches						CHK("BRACES",
4394aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4395aad4f614SJoe Perches					}
439613214adfSAndy Whitcroft				}
439713214adfSAndy Whitcroft			}
439813214adfSAndy Whitcroft		}
4399773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
440013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4401cf655043SAndy Whitcroft			my $allowed = 0;
4402f0a594c1SAndy Whitcroft
4403cf655043SAndy Whitcroft			# Check the pre-context.
4404cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4405cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4406cf655043SAndy Whitcroft				$allowed = 1;
4407f0a594c1SAndy Whitcroft			}
4408773647a0SAndy Whitcroft
4409773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4410773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4411773647a0SAndy Whitcroft
4412cf655043SAndy Whitcroft			# Check the condition.
4413cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4414773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4415cf655043SAndy Whitcroft			if (defined $cond) {
4416773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4417cf655043SAndy Whitcroft			}
4418cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4419cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4420cf655043SAndy Whitcroft				$allowed = 1;
4421cf655043SAndy Whitcroft			}
4422cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4423cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4424cf655043SAndy Whitcroft				$allowed = 1;
4425cf655043SAndy Whitcroft			}
4426cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4427cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4428cf655043SAndy Whitcroft				$allowed = 1;
4429cf655043SAndy Whitcroft			}
4430cf655043SAndy Whitcroft			# Check the post-context.
4431cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4432cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4433cf655043SAndy Whitcroft				if (defined $cond) {
4434773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4435cf655043SAndy Whitcroft				}
4436cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4437cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4438cf655043SAndy Whitcroft					$allowed = 1;
4439cf655043SAndy Whitcroft				}
4440cf655043SAndy Whitcroft			}
4441cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
444269932487SJustin P. Mattock				my $herectx = $here . "\n";
4443f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4444cf655043SAndy Whitcroft
4445f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
444669932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4447cf655043SAndy Whitcroft				}
4448cf655043SAndy Whitcroft
4449000d1cc1SJoe Perches				WARN("BRACES",
4450000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4451f0a594c1SAndy Whitcroft			}
4452f0a594c1SAndy Whitcroft		}
4453f0a594c1SAndy Whitcroft
44540979ae66SJoe Perches# check for unnecessary blank lines around braces
445577b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
44560979ae66SJoe Perches			CHK("BRACES",
44570979ae66SJoe Perches			    "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
44580979ae66SJoe Perches		}
445977b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
44600979ae66SJoe Perches			CHK("BRACES",
44610979ae66SJoe Perches			    "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
44620979ae66SJoe Perches		}
44630979ae66SJoe Perches
44644a0df2efSAndy Whitcroft# no volatiles please
44656c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
44666c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4467000d1cc1SJoe Perches			WARN("VOLATILE",
4468000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
44694a0df2efSAndy Whitcroft		}
44704a0df2efSAndy Whitcroft
44715e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
44725e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
44735e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
44745e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
44755e4f6ba5SJoe Perches		if ($line =~ /^\+\s*"[X\t]*"/ &&
44765e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
44775e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
44785e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
44795e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
44805e4f6ba5SJoe Perches				     $fix &&
44815e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
44825e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
44835e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
44845e4f6ba5SJoe Perches				my $comma_close = "";
44855e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
44865e4f6ba5SJoe Perches					$comma_close = $1;
44875e4f6ba5SJoe Perches				}
44885e4f6ba5SJoe Perches
44895e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
44905e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
44915e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
44925e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
44935e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
44945e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
44955e4f6ba5SJoe Perches				$fixedline = $rawline;
44965e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
44975e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
44985e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
44995e4f6ba5SJoe Perches				}
45005e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
45015e4f6ba5SJoe Perches			}
45025e4f6ba5SJoe Perches		}
45035e4f6ba5SJoe Perches
45045e4f6ba5SJoe Perches# check for missing a space in a string concatenation
45055e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
45065e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
45075e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
45085e4f6ba5SJoe Perches		}
45095e4f6ba5SJoe Perches
45105e4f6ba5SJoe Perches# check for spaces before a quoted newline
45115e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
45125e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
45135e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
45145e4f6ba5SJoe Perches			    $fix) {
45155e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
45165e4f6ba5SJoe Perches			}
45175e4f6ba5SJoe Perches
45185e4f6ba5SJoe Perches		}
45195e4f6ba5SJoe Perches
4520f17dba4fSJoe Perches# concatenated string without spaces between elements
4521f17dba4fSJoe Perches		if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4522f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4523f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4524f17dba4fSJoe Perches		}
4525f17dba4fSJoe Perches
452690ad30e5SJoe Perches# uncoalesced string fragments
452790ad30e5SJoe Perches		if ($line =~ /"X*"\s*"/) {
452890ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
452990ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
453090ad30e5SJoe Perches		}
453190ad30e5SJoe Perches
45325e4f6ba5SJoe Perches# check for %L{u,d,i} in strings
45335e4f6ba5SJoe Perches		my $string;
45345e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
45355e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
45365e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
45375e4f6ba5SJoe Perches			if ($string =~ /(?<!%)%L[udi]/) {
45385e4f6ba5SJoe Perches				WARN("PRINTF_L",
45395e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
45405e4f6ba5SJoe Perches				last;
45415e4f6ba5SJoe Perches			}
45425e4f6ba5SJoe Perches		}
45435e4f6ba5SJoe Perches
45445e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
45455e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
45465e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
45475e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
45485e4f6ba5SJoe Perches		}
45495e4f6ba5SJoe Perches
455000df344fSAndy Whitcroft# warn about #if 0
4551c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4552000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
4553000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
4554de7d4f0eSAndy Whitcroft				$herecurr);
45554a0df2efSAndy Whitcroft		}
45564a0df2efSAndy Whitcroft
455703df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
455803df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
455903df4b51SAndy Whitcroft			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
456003df4b51SAndy Whitcroft			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
456103df4b51SAndy Whitcroft				WARN('NEEDLESS_IF',
456215160f90SFabio Estevam				     "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
45634c432a8fSGreg Kroah-Hartman			}
45644c432a8fSGreg Kroah-Hartman		}
4565f0a594c1SAndy Whitcroft
4566ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
4567ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4568ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4569ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
4570ebfdc409SJoe Perches		    $linenr > 3) {
4571ebfdc409SJoe Perches			my $testval = $2;
4572ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
4573ebfdc409SJoe Perches
4574ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4575ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4576ebfdc409SJoe Perches
4577ebfdc409SJoe 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)/) {
4578ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
4579ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
4580ebfdc409SJoe Perches			}
4581ebfdc409SJoe Perches		}
4582ebfdc409SJoe Perches
4583f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
4584dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
4585f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4586f78d98f6SJoe Perches			my $level = $1;
4587f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
4588f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
4589f78d98f6SJoe Perches			    $fix) {
4590f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
4591f78d98f6SJoe Perches			}
4592f78d98f6SJoe Perches		}
4593f78d98f6SJoe Perches
4594abb08a53SJoe Perches# check for mask then right shift without a parentheses
4595abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4596abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4597abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4598abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
4599abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4600abb08a53SJoe Perches		}
4601abb08a53SJoe Perches
4602b75ac618SJoe Perches# check for pointer comparisons to NULL
4603b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
4604b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4605b75ac618SJoe Perches				my $val = $1;
4606b75ac618SJoe Perches				my $equal = "!";
4607b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
4608b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
4609b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4610b75ac618SJoe Perches					    $fix) {
4611b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4612b75ac618SJoe Perches				}
4613b75ac618SJoe Perches			}
4614b75ac618SJoe Perches		}
4615b75ac618SJoe Perches
46168716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
46178716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
46188716de38SJoe Perches			my $attr = $1;
46198716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
46208716de38SJoe Perches				my $ptr = $1;
46218716de38SJoe Perches				my $var = $2;
46228716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
46238716de38SJoe Perches				      ERROR("MISPLACED_INIT",
46248716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
46258716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
46268716de38SJoe Perches				      WARN("MISPLACED_INIT",
46278716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
46288716de38SJoe Perches				    $fix) {
4629194f66fcSJoe 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;
46308716de38SJoe Perches				}
46318716de38SJoe Perches			}
46328716de38SJoe Perches		}
46338716de38SJoe Perches
4634e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
4635e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4636e970b884SJoe Perches			my $attr = $1;
4637e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
4638e970b884SJoe Perches			my $attr_prefix = $1;
4639e970b884SJoe Perches			my $attr_type = $2;
4640e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4641e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4642e970b884SJoe Perches			    $fix) {
4643194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4644e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
4645e970b884SJoe Perches			}
4646e970b884SJoe Perches		}
4647e970b884SJoe Perches
4648e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
4649e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4650e970b884SJoe Perches			my $attr = $1;
4651e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
4652e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
4653e970b884SJoe Perches			    $fix) {
4654194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
4655e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
4656e970b884SJoe Perches				$lead = rtrim($1);
4657e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
4658e970b884SJoe Perches				$lead = "${lead}const ";
4659194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4660e970b884SJoe Perches			}
4661e970b884SJoe Perches		}
4662e970b884SJoe Perches
4663fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
4664fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
4665fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4666fbdb8138SJoe Perches			my $constant_func = $1;
4667fbdb8138SJoe Perches			my $func = $constant_func;
4668fbdb8138SJoe Perches			$func =~ s/^__constant_//;
4669fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
4670fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
4671fbdb8138SJoe Perches			    $fix) {
4672194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4673fbdb8138SJoe Perches			}
4674fbdb8138SJoe Perches		}
4675fbdb8138SJoe Perches
46761a15a250SPatrick Pannuto# prefer usleep_range over udelay
467737581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
467843c1d77cSJoe Perches			my $delay = $1;
46791a15a250SPatrick Pannuto			# ignore udelay's < 10, however
468043c1d77cSJoe Perches			if (! ($delay < 10) ) {
4681000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
468243c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
468343c1d77cSJoe Perches			}
468443c1d77cSJoe Perches			if ($delay > 2000) {
468543c1d77cSJoe Perches				WARN("LONG_UDELAY",
468643c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
46871a15a250SPatrick Pannuto			}
46881a15a250SPatrick Pannuto		}
46891a15a250SPatrick Pannuto
469009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
469109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
469209ef8725SPatrick Pannuto			if ($1 < 20) {
4693000d1cc1SJoe Perches				WARN("MSLEEP",
469443c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
469509ef8725SPatrick Pannuto			}
469609ef8725SPatrick Pannuto		}
469709ef8725SPatrick Pannuto
469836ec1939SJoe Perches# check for comparisons of jiffies
469936ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
470036ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
470136ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
470236ec1939SJoe Perches		}
470336ec1939SJoe Perches
47049d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
47059d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
47069d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
47079d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
47089d7a34a5SJoe Perches		}
47099d7a34a5SJoe Perches
471000df344fSAndy Whitcroft# warn about #ifdefs in C files
4711c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
471200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
471300df344fSAndy Whitcroft#			print "$herecurr";
471400df344fSAndy Whitcroft#			$clean = 0;
471500df344fSAndy Whitcroft#		}
471600df344fSAndy Whitcroft
471722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
4718c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
47193705ce5bSJoe Perches			if (ERROR("SPACING",
47203705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
47213705ce5bSJoe Perches			    $fix) {
4722194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47233705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
47243705ce5bSJoe Perches			}
47253705ce5bSJoe Perches
472622f2a2efSAndy Whitcroft		}
472722f2a2efSAndy Whitcroft
47284a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
4729171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4730171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
47314a0df2efSAndy Whitcroft			my $which = $1;
47324a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4733000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
4734000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
47354a0df2efSAndy Whitcroft			}
47364a0df2efSAndy Whitcroft		}
47374a0df2efSAndy Whitcroft# check for memory barriers without a comment.
47384a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
47394a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
4740c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
4741000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
47424a0df2efSAndy Whitcroft			}
47434a0df2efSAndy Whitcroft		}
47444a0df2efSAndy Whitcroft# check of hardware specific defines
4745c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4746000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
4747000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
47480a920b5bSAndy Whitcroft		}
4749653d4876SAndy Whitcroft
4750d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
4751d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4752000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
4753000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
4754d4977c78STobias Klauser		}
4755d4977c78STobias Klauser
4756de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
4757de7d4f0eSAndy Whitcroft# storage class and type.
47589c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
47599c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
4760000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
4761000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
4762de7d4f0eSAndy Whitcroft		}
4763de7d4f0eSAndy Whitcroft
47648905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
47652b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
47662b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
4767d5e616fcSJoe Perches			if (WARN("INLINE",
4768d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
4769d5e616fcSJoe Perches			    $fix) {
4770194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4771d5e616fcSJoe Perches
4772d5e616fcSJoe Perches			}
47738905a67cSAndy Whitcroft		}
47748905a67cSAndy Whitcroft
47753d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
47762b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
47772b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4778000d1cc1SJoe Perches			WARN("PREFER_PACKED",
4779000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
47803d130fd0SJoe Perches		}
47813d130fd0SJoe Perches
478239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
47832b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
47842b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4785000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
4786000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
478739b7e287SJoe Perches		}
478839b7e287SJoe Perches
47895f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
47902b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
47912b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4792d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
4793d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4794d5e616fcSJoe Perches			    $fix) {
4795194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4796d5e616fcSJoe Perches
4797d5e616fcSJoe Perches			}
47985f14d3bdSJoe Perches		}
47995f14d3bdSJoe Perches
48006061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
48012b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
48022b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4803d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
4804d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4805d5e616fcSJoe Perches			    $fix) {
4806194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4807d5e616fcSJoe Perches			}
48086061d949SJoe Perches		}
48096061d949SJoe Perches
4810619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
4811619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4812619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4813619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4814619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
4815619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
4816619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
4817619a908aSJoe Perches		}
4818619a908aSJoe Perches
48198f53a9b8SJoe Perches# check for sizeof(&)
48208f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
4821000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
4822000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
48238f53a9b8SJoe Perches		}
48248f53a9b8SJoe Perches
482566c80b60SJoe Perches# check for sizeof without parenthesis
482666c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4827d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
4828d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4829d5e616fcSJoe Perches			    $fix) {
4830194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4831d5e616fcSJoe Perches			}
483266c80b60SJoe Perches		}
483366c80b60SJoe Perches
483488982feaSJoe Perches# check for struct spinlock declarations
483588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
483688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
483788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
483888982feaSJoe Perches		}
483988982feaSJoe Perches
4840a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
484106668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4842a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
484306668727SJoe Perches			if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4844d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
4845d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4846d5e616fcSJoe Perches				    $fix) {
4847194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4848d5e616fcSJoe Perches				}
4849a6962d72SJoe Perches			}
4850a6962d72SJoe Perches		}
4851a6962d72SJoe Perches
4852554e165cSAndy Whitcroft# Check for misused memsets
4853d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4854d1fe9c09SJoe Perches		    defined $stat &&
4855d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4856554e165cSAndy Whitcroft
4857d7c76ba7SJoe Perches			my $ms_addr = $2;
4858d1fe9c09SJoe Perches			my $ms_val = $7;
4859d1fe9c09SJoe Perches			my $ms_size = $12;
4860d7c76ba7SJoe Perches
4861554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
4862554e165cSAndy Whitcroft				ERROR("MEMSET",
4863d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4864554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
4865554e165cSAndy Whitcroft				WARN("MEMSET",
4866d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4867d7c76ba7SJoe Perches			}
4868d7c76ba7SJoe Perches		}
4869d7c76ba7SJoe Perches
487098a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
487198a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
487298a9bba5SJoe Perches		    $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
487398a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
487498a9bba5SJoe Perches				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
487598a9bba5SJoe Perches			    $fix) {
4876194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
487798a9bba5SJoe Perches			}
487898a9bba5SJoe Perches		}
487998a9bba5SJoe Perches
4880d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
4881d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4882d1fe9c09SJoe Perches		    defined $stat &&
4883d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4884d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
4885d7c76ba7SJoe Perches				my $call = $1;
4886d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
4887d7c76ba7SJoe Perches				my $arg1 = $3;
4888d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
4889d1fe9c09SJoe Perches				my $arg2 = $8;
4890d7c76ba7SJoe Perches				my $cast;
4891d7c76ba7SJoe Perches
4892d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4893d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
4894d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
4895d7c76ba7SJoe Perches					$cast = $cast1;
4896d7c76ba7SJoe Perches				} else {
4897d7c76ba7SJoe Perches					$cast = $cast2;
4898d7c76ba7SJoe Perches				}
4899d7c76ba7SJoe Perches				WARN("MINMAX",
4900d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4901554e165cSAndy Whitcroft			}
4902554e165cSAndy Whitcroft		}
4903554e165cSAndy Whitcroft
49044a273195SJoe Perches# check usleep_range arguments
49054a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
49064a273195SJoe Perches		    defined $stat &&
49074a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
49084a273195SJoe Perches			my $min = $1;
49094a273195SJoe Perches			my $max = $7;
49104a273195SJoe Perches			if ($min eq $max) {
49114a273195SJoe Perches				WARN("USLEEP_RANGE",
49124a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
49134a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
49144a273195SJoe Perches				 $min > $max) {
49154a273195SJoe Perches				WARN("USLEEP_RANGE",
49164a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
49174a273195SJoe Perches			}
49184a273195SJoe Perches		}
49194a273195SJoe Perches
4920823b794cSJoe Perches# check for naked sscanf
4921823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4922823b794cSJoe Perches		    defined $stat &&
49236c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
4924823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4925823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4926823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4927823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
4928823b794cSJoe Perches			$lc = $lc + $linenr;
4929823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
4930823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4931823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4932823b794cSJoe Perches			}
4933823b794cSJoe Perches			WARN("NAKED_SSCANF",
4934823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4935823b794cSJoe Perches		}
4936823b794cSJoe Perches
4937afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
4938afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4939afc819abSJoe Perches		    defined $stat &&
4940afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
4941afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
4942afc819abSJoe Perches			$lc = $lc + $linenr;
4943afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
4944afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
4945afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
4946afc819abSJoe Perches			}
4947afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4948afc819abSJoe Perches				my $format = $6;
4949afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
4950afc819abSJoe Perches				if ($count == 1 &&
4951afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4952afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
4953afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4954afc819abSJoe Perches				}
4955afc819abSJoe Perches			}
4956afc819abSJoe Perches		}
4957afc819abSJoe Perches
495870dc8a48SJoe Perches# check for new externs in .h files.
495970dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
496070dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4961d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
496270dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
496370dc8a48SJoe Perches			    $fix) {
4964194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
496570dc8a48SJoe Perches			}
496670dc8a48SJoe Perches		}
496770dc8a48SJoe Perches
4968de7d4f0eSAndy Whitcroft# check for new externs in .c files.
4969171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
4970c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4971171ae1a4SAndy Whitcroft		{
4972c45dcabdSAndy Whitcroft			my $function_name = $1;
4973c45dcabdSAndy Whitcroft			my $paren_space = $2;
4974171ae1a4SAndy Whitcroft
4975171ae1a4SAndy Whitcroft			my $s = $stat;
4976171ae1a4SAndy Whitcroft			if (defined $cond) {
4977171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
4978171ae1a4SAndy Whitcroft			}
4979c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
4980c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
4981c45dcabdSAndy Whitcroft			{
4982000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
4983000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
4984de7d4f0eSAndy Whitcroft			}
4985de7d4f0eSAndy Whitcroft
4986171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
4987000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
4988000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
4989171ae1a4SAndy Whitcroft			}
49909c9ba34eSAndy Whitcroft
49919c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
49929c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
49939c9ba34eSAndy Whitcroft		{
4994000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
4995000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
4996171ae1a4SAndy Whitcroft		}
4997171ae1a4SAndy Whitcroft
4998de7d4f0eSAndy Whitcroft# checks for new __setup's
4999de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5000de7d4f0eSAndy Whitcroft			my $name = $1;
5001de7d4f0eSAndy Whitcroft
5002de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5003000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5004000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5005de7d4f0eSAndy Whitcroft			}
5006653d4876SAndy Whitcroft		}
50079c0ca6f9SAndy Whitcroft
50089c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5009caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5010000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5011000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
50129c0ca6f9SAndy Whitcroft		}
501313214adfSAndy Whitcroft
5014a640d25cSJoe Perches# alloc style
5015a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5016a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5017a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5018a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5019a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5020a640d25cSJoe Perches		}
5021a640d25cSJoe Perches
502260a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
502360a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5024e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
502560a55369SJoe Perches			my $oldfunc = $3;
502660a55369SJoe Perches			my $a1 = $4;
502760a55369SJoe Perches			my $a2 = $10;
502860a55369SJoe Perches			my $newfunc = "kmalloc_array";
502960a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
503060a55369SJoe Perches			my $r1 = $a1;
503160a55369SJoe Perches			my $r2 = $a2;
503260a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
503360a55369SJoe Perches				$r1 = $a2;
503460a55369SJoe Perches				$r2 = $a1;
503560a55369SJoe Perches			}
5036e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5037e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5038e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5039e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5040e367455aSJoe Perches				    $fix) {
5041194f66fcSJoe 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;
504260a55369SJoe Perches
504360a55369SJoe Perches				}
504460a55369SJoe Perches			}
504560a55369SJoe Perches		}
504660a55369SJoe Perches
5047972fdea2SJoe Perches# check for krealloc arg reuse
5048972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5049972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5050972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5051972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5052972fdea2SJoe Perches		}
5053972fdea2SJoe Perches
50545ce59ae0SJoe Perches# check for alloc argument mismatch
50555ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
50565ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
50575ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
50585ce59ae0SJoe Perches		}
50595ce59ae0SJoe Perches
5060caf2a54fSJoe Perches# check for multiple semicolons
5061caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5062d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5063d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5064d5e616fcSJoe Perches			    $fix) {
5065194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5066d5e616fcSJoe Perches			}
5067d1e2ad07SJoe Perches		}
5068d1e2ad07SJoe Perches
50690ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
50700ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
50710ab90191SJoe Perches			my $ull = "";
50720ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
50730ab90191SJoe Perches			if (CHK("BIT_MACRO",
50740ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
50750ab90191SJoe Perches			    $fix) {
50760ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
50770ab90191SJoe Perches			}
50780ab90191SJoe Perches		}
50790ab90191SJoe Perches
5080e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5081c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5082c34c09a8SJoe Perches			my $has_break = 0;
5083c34c09a8SJoe Perches			my $has_statement = 0;
5084c34c09a8SJoe Perches			my $count = 0;
5085c34c09a8SJoe Perches			my $prevline = $linenr;
5086e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5087c34c09a8SJoe Perches				$prevline--;
5088c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5089c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5090c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5091c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5092c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5093c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5094c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5095c34c09a8SJoe Perches				$has_statement = 1;
5096c34c09a8SJoe Perches				$count++;
5097c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5098c34c09a8SJoe Perches			}
5099c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5100c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5101c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5102c34c09a8SJoe Perches			}
5103c34c09a8SJoe Perches		}
5104c34c09a8SJoe Perches
5105d1e2ad07SJoe Perches# check for switch/default statements without a break;
5106d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5107d1e2ad07SJoe Perches		    defined $stat &&
5108d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5109d1e2ad07SJoe Perches			my $ctx = '';
5110d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5111d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5112d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5113d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5114d1e2ad07SJoe Perches			}
5115d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5116d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5117caf2a54fSJoe Perches		}
5118caf2a54fSJoe Perches
511913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5120d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5121d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5122d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5123d5e616fcSJoe Perches			    $fix) {
5124194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5125d5e616fcSJoe Perches			}
512613214adfSAndy Whitcroft		}
5127773647a0SAndy Whitcroft
512862ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
512962ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
513062ec818fSJoe Perches			ERROR("DATE_TIME",
513162ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
513262ec818fSJoe Perches		}
513362ec818fSJoe Perches
51342c92488aSJoe Perches# check for use of yield()
51352c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
51362c92488aSJoe Perches			WARN("YIELD",
51372c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
51382c92488aSJoe Perches		}
51392c92488aSJoe Perches
5140179f8f40SJoe Perches# check for comparisons against true and false
5141179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5142179f8f40SJoe Perches			my $lead = $1;
5143179f8f40SJoe Perches			my $arg = $2;
5144179f8f40SJoe Perches			my $test = $3;
5145179f8f40SJoe Perches			my $otype = $4;
5146179f8f40SJoe Perches			my $trail = $5;
5147179f8f40SJoe Perches			my $op = "!";
5148179f8f40SJoe Perches
5149179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5150179f8f40SJoe Perches
5151179f8f40SJoe Perches			my $type = lc($otype);
5152179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5153179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5154179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5155179f8f40SJoe Perches					$op = "";
5156179f8f40SJoe Perches				}
5157179f8f40SJoe Perches
5158179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5159179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5160179f8f40SJoe Perches
5161179f8f40SJoe Perches## maybe suggesting a correct construct would better
5162179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5163179f8f40SJoe Perches
5164179f8f40SJoe Perches			}
5165179f8f40SJoe Perches		}
5166179f8f40SJoe Perches
51674882720bSThomas Gleixner# check for semaphores initialized locked
51684882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5169000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5170000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5171773647a0SAndy Whitcroft		}
51726712d858SJoe Perches
517367d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
517467d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5175000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
517667d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5177773647a0SAndy Whitcroft		}
51786712d858SJoe Perches
5179ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5180f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5181000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5182ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5183f3db6639SMichael Ellerman		}
51846712d858SJoe Perches
518579404849SEmese Revfy# check for various ops structs, ensure they are const.
518679404849SEmese Revfy		my $struct_ops = qr{acpi_dock_ops|
518779404849SEmese Revfy				address_space_operations|
518879404849SEmese Revfy				backlight_ops|
518979404849SEmese Revfy				block_device_operations|
519079404849SEmese Revfy				dentry_operations|
519179404849SEmese Revfy				dev_pm_ops|
519279404849SEmese Revfy				dma_map_ops|
519379404849SEmese Revfy				extent_io_ops|
519479404849SEmese Revfy				file_lock_operations|
519579404849SEmese Revfy				file_operations|
519679404849SEmese Revfy				hv_ops|
519779404849SEmese Revfy				ide_dma_ops|
519879404849SEmese Revfy				intel_dvo_dev_ops|
519979404849SEmese Revfy				item_operations|
520079404849SEmese Revfy				iwl_ops|
520179404849SEmese Revfy				kgdb_arch|
520279404849SEmese Revfy				kgdb_io|
520379404849SEmese Revfy				kset_uevent_ops|
520479404849SEmese Revfy				lock_manager_operations|
520579404849SEmese Revfy				microcode_ops|
520679404849SEmese Revfy				mtrr_ops|
520779404849SEmese Revfy				neigh_ops|
520879404849SEmese Revfy				nlmsvc_binding|
520979404849SEmese Revfy				pci_raw_ops|
521079404849SEmese Revfy				pipe_buf_operations|
521179404849SEmese Revfy				platform_hibernation_ops|
521279404849SEmese Revfy				platform_suspend_ops|
521379404849SEmese Revfy				proto_ops|
521479404849SEmese Revfy				rpc_pipe_ops|
521579404849SEmese Revfy				seq_operations|
521679404849SEmese Revfy				snd_ac97_build_ops|
521779404849SEmese Revfy				soc_pcmcia_socket_ops|
521879404849SEmese Revfy				stacktrace_ops|
521979404849SEmese Revfy				sysfs_ops|
522079404849SEmese Revfy				tty_operations|
522179404849SEmese Revfy				usb_mon_operations|
522279404849SEmese Revfy				wd_ops}x;
52236903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
522479404849SEmese Revfy		    $line =~ /\bstruct\s+($struct_ops)\b/) {
5225000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5226000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
52276903ffb2SAndy Whitcroft				$herecurr);
52282b6db5cbSAndy Whitcroft		}
5229773647a0SAndy Whitcroft
5230773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5231773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5232773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5233c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5234c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5235171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5236171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5237171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5238773647a0SAndy Whitcroft		{
5239000d1cc1SJoe Perches			WARN("NR_CPUS",
5240000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5241773647a0SAndy Whitcroft		}
52429c9ba34eSAndy Whitcroft
524352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
524452ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
524552ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
524652ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
524752ea8506SJoe Perches		}
524852ea8506SJoe Perches
5249acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5250acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5251acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5252acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5253acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5254acd9362cSJoe Perches		}
5255acd9362cSJoe Perches
5256691d77b6SAndy Whitcroft# whine mightly about in_atomic
5257691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5258691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5259000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5260000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5261f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5262000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5263000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5264691d77b6SAndy Whitcroft			}
5265691d77b6SAndy Whitcroft		}
52661704f47bSPeter Zijlstra
52671704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
52681704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
52691704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
52701704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
52711704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
52721704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5273000d1cc1SJoe Perches				ERROR("LOCKDEP",
5274000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
52751704f47bSPeter Zijlstra			}
52761704f47bSPeter Zijlstra		}
527788f8831cSDave Jones
527888f8831cSDave Jones		if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
527988f8831cSDave Jones		    $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5280000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5281000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
528288f8831cSDave Jones		}
52832435880fSJoe Perches
5284515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5285515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5286515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5287515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
52882435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
52892435880fSJoe Perches				my $func = $entry->[0];
52902435880fSJoe Perches				my $arg_pos = $entry->[1];
52912435880fSJoe Perches
52922435880fSJoe Perches				my $skip_args = "";
52932435880fSJoe Perches				if ($arg_pos > 1) {
52942435880fSJoe Perches					$arg_pos--;
52952435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
52962435880fSJoe Perches				}
52972435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5298515a235eSJoe Perches				if ($line =~ /$test/) {
52992435880fSJoe Perches					my $val = $1;
53002435880fSJoe Perches					$val = $6 if ($skip_args ne "");
53012435880fSJoe Perches
53021727cc70SJoe Perches					if ($val !~ /^0$/ &&
53031727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
53041727cc70SJoe Perches					     length($val) ne 4)) {
53052435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
53061727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5307c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5308c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5309c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
53102435880fSJoe Perches					}
53112435880fSJoe Perches				}
53122435880fSJoe Perches			}
531313214adfSAndy Whitcroft		}
5314515a235eSJoe Perches	}
531513214adfSAndy Whitcroft
531613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
531713214adfSAndy Whitcroft	# so just keep quiet.
531813214adfSAndy Whitcroft	if ($#rawlines == -1) {
531913214adfSAndy Whitcroft		exit(0);
53200a920b5bSAndy Whitcroft	}
53210a920b5bSAndy Whitcroft
53228905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
53238905a67cSAndy Whitcroft	# things that appear to be patches.
53248905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
53258905a67cSAndy Whitcroft		exit(0);
53268905a67cSAndy Whitcroft	}
53278905a67cSAndy Whitcroft
53288905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
53298905a67cSAndy Whitcroft	# just keep quiet.
53308905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
53318905a67cSAndy Whitcroft		exit(0);
53328905a67cSAndy Whitcroft	}
53338905a67cSAndy Whitcroft
53348905a67cSAndy Whitcroft	if (!$is_patch) {
5335000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5336000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
53370a920b5bSAndy Whitcroft	}
53380a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
5339000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
5340000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
53410a920b5bSAndy Whitcroft	}
53420a920b5bSAndy Whitcroft
5343f0a594c1SAndy Whitcroft	print report_dump();
534413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
534513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
53466c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
53476c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
53486c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
53498905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
53506c72ffaaSAndy Whitcroft	}
53518905a67cSAndy Whitcroft
5352d2c0a235SAndy Whitcroft	if ($quiet == 0) {
5353d1fe9c09SJoe Perches
5354d1fe9c09SJoe Perches		if ($^V lt 5.10.0) {
5355d1fe9c09SJoe Perches			print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5356d1fe9c09SJoe Perches			print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5357d1fe9c09SJoe Perches		}
5358d1fe9c09SJoe Perches
5359d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
5360d2c0a235SAndy Whitcroft		# then suggest that.
5361d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
5362d2c0a235SAndy Whitcroft			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5363d2c0a235SAndy Whitcroft			print "      scripts/cleanfile\n\n";
5364b0781216SMike Frysinger			$rpt_cleaners = 0;
5365d2c0a235SAndy Whitcroft		}
5366d2c0a235SAndy Whitcroft	}
5367d2c0a235SAndy Whitcroft
536891bfe484SJoe Perches	hash_show_words(\%use_type, "Used");
536991bfe484SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
5370000d1cc1SJoe Perches
5371d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
5372d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
5373d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
53749624b8d6SJoe Perches		my $newfile = $filename;
53759624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
53763705ce5bSJoe Perches		my $linecount = 0;
53773705ce5bSJoe Perches		my $f;
53783705ce5bSJoe Perches
5379d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5380d752fcc8SJoe Perches
53813705ce5bSJoe Perches		open($f, '>', $newfile)
53823705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
53833705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
53843705ce5bSJoe Perches			$linecount++;
53853705ce5bSJoe Perches			if ($file) {
53863705ce5bSJoe Perches				if ($linecount > 3) {
53873705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
53883705ce5bSJoe Perches					print $f $fixed_line . "\n";
53893705ce5bSJoe Perches				}
53903705ce5bSJoe Perches			} else {
53913705ce5bSJoe Perches				print $f $fixed_line . "\n";
53923705ce5bSJoe Perches			}
53933705ce5bSJoe Perches		}
53943705ce5bSJoe Perches		close($f);
53953705ce5bSJoe Perches
53963705ce5bSJoe Perches		if (!$quiet) {
53973705ce5bSJoe Perches			print << "EOM";
53983705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
53993705ce5bSJoe Perches
54003705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
54013705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
54023705ce5bSJoe Perches
54033705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
54043705ce5bSJoe PerchesNo warranties, expressed or implied...
54053705ce5bSJoe Perches
54063705ce5bSJoe PerchesEOM
54073705ce5bSJoe Perches		}
54083705ce5bSJoe Perches	}
54093705ce5bSJoe Perches
54100a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
5411c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
54120a920b5bSAndy Whitcroft	}
54130a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
5414000d1cc1SJoe Perches		print << "EOM";
5415000d1cc1SJoe Perches$vname has style problems, please review.
5416000d1cc1SJoe Perches
5417000d1cc1SJoe PerchesIf any of these errors are false positives, please report
5418000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS.
5419000d1cc1SJoe PerchesEOM
54200a920b5bSAndy Whitcroft	}
542113214adfSAndy Whitcroft
54220a920b5bSAndy Whitcroft	return $clean;
54230a920b5bSAndy Whitcroft}
5424