xref: /linux-6.15/scripts/checkpatch.pl (revision a1ef277e)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
20a920b5bSAndy Whitcroft# (c) 2001, Dave Jones. <[email protected]> (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
40a920b5bSAndy Whitcroft# (c) 2007, Andy Whitcroft <[email protected]> (new conditions, test suite, etc)
50a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
60a920b5bSAndy Whitcroft
70a920b5bSAndy Whitcroftuse strict;
80a920b5bSAndy Whitcroft
90a920b5bSAndy Whitcroftmy $P = $0;
1000df344fSAndy Whitcroft$P =~ s@.*/@@g;
110a920b5bSAndy Whitcroft
1233cba065SAndy Whitcroftmy $V = '0.21';
130a920b5bSAndy Whitcroft
140a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftmy $quiet = 0;
170a920b5bSAndy Whitcroftmy $tree = 1;
180a920b5bSAndy Whitcroftmy $chk_signoff = 1;
190a920b5bSAndy Whitcroftmy $chk_patch = 1;
20773647a0SAndy Whitcroftmy $tst_only;
216c72ffaaSAndy Whitcroftmy $emacs = 0;
228905a67cSAndy Whitcroftmy $terse = 0;
236c72ffaaSAndy Whitcroftmy $file = 0;
246c72ffaaSAndy Whitcroftmy $check = 0;
258905a67cSAndy Whitcroftmy $summary = 1;
268905a67cSAndy Whitcroftmy $mailback = 0;
2713214adfSAndy Whitcroftmy $summary_file = 0;
286c72ffaaSAndy Whitcroftmy $root;
29c2fdda0dSAndy Whitcroftmy %debug;
300a920b5bSAndy WhitcroftGetOptions(
316c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
320a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
330a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
340a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
356c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
368905a67cSAndy Whitcroft	'terse!'	=> \$terse,
376c72ffaaSAndy Whitcroft	'file!'		=> \$file,
386c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
396c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
406c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
418905a67cSAndy Whitcroft	'summary!'	=> \$summary,
428905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
4313214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
4413214adfSAndy Whitcroft
45c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
46773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
470a920b5bSAndy Whitcroft) or exit;
480a920b5bSAndy Whitcroft
490a920b5bSAndy Whitcroftmy $exit = 0;
500a920b5bSAndy Whitcroft
510a920b5bSAndy Whitcroftif ($#ARGV < 0) {
5200df344fSAndy Whitcroft	print "usage: $P [options] patchfile\n";
530a920b5bSAndy Whitcroft	print "version: $V\n";
540a920b5bSAndy Whitcroft	print "options: -q               => quiet\n";
550a920b5bSAndy Whitcroft	print "         --no-tree        => run without a kernel tree\n";
568905a67cSAndy Whitcroft	print "         --terse          => one line per report\n";
576c72ffaaSAndy Whitcroft	print "         --emacs          => emacs compile window format\n";
586c72ffaaSAndy Whitcroft	print "         --file           => check a source file\n";
596c72ffaaSAndy Whitcroft	print "         --strict         => enable more subjective tests\n";
606c72ffaaSAndy Whitcroft	print "         --root           => path to the kernel tree root\n";
6113214adfSAndy Whitcroft	print "         --no-summary     => suppress the per-file summary\n";
6213214adfSAndy Whitcroft	print "         --summary-file   => include the filename in summary\n";
630a920b5bSAndy Whitcroft	exit(1);
640a920b5bSAndy Whitcroft}
650a920b5bSAndy Whitcroft
66c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
67c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
687429c690SAndy Whitcroftmy $dbg_type = 0;
69*a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
70c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
71c2fdda0dSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';"
72c2fdda0dSAndy Whitcroft}
73c2fdda0dSAndy Whitcroft
748905a67cSAndy Whitcroftif ($terse) {
758905a67cSAndy Whitcroft	$emacs = 1;
768905a67cSAndy Whitcroft	$quiet++;
778905a67cSAndy Whitcroft}
788905a67cSAndy Whitcroft
796c72ffaaSAndy Whitcroftif ($tree) {
806c72ffaaSAndy Whitcroft	if (defined $root) {
816c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
826c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
836c72ffaaSAndy Whitcroft		}
846c72ffaaSAndy Whitcroft	} else {
856c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
866c72ffaaSAndy Whitcroft			$root = '.';
876c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
886c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
896c72ffaaSAndy Whitcroft			$root = $1;
906c72ffaaSAndy Whitcroft		}
916c72ffaaSAndy Whitcroft	}
926c72ffaaSAndy Whitcroft
936c72ffaaSAndy Whitcroft	if (!defined $root) {
940a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
950a920b5bSAndy Whitcroft		exit(2);
960a920b5bSAndy Whitcroft	}
976c72ffaaSAndy Whitcroft}
986c72ffaaSAndy Whitcroft
996c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1006c72ffaaSAndy Whitcroft
1016c72ffaaSAndy Whitcroftour $Ident       = qr{[A-Za-z_][A-Za-z\d_]*};
1026c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1036c72ffaaSAndy Whitcroftour $Sparse	= qr{
1046c72ffaaSAndy Whitcroft			__user|
1056c72ffaaSAndy Whitcroft			__kernel|
1066c72ffaaSAndy Whitcroft			__force|
1076c72ffaaSAndy Whitcroft			__iomem|
1086c72ffaaSAndy Whitcroft			__must_check|
1096c72ffaaSAndy Whitcroft			__init_refok|
110cf655043SAndy Whitcroft			__kprobes
1116c72ffaaSAndy Whitcroft		}x;
1126c72ffaaSAndy Whitcroftour $Attribute	= qr{
1136c72ffaaSAndy Whitcroft			const|
1146c72ffaaSAndy Whitcroft			__read_mostly|
1156c72ffaaSAndy Whitcroft			__kprobes|
1166c72ffaaSAndy Whitcroft			__(?:mem|cpu|dev|)(?:initdata|init)
1176c72ffaaSAndy Whitcroft		  }x;
118c45dcabdSAndy Whitcroftour $Modifier;
1196c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1206c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1216c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1226c72ffaaSAndy Whitcroft
1236c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1246c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
1256c72ffaaSAndy Whitcroftour $Operators	= qr{
1266c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1276c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
128c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1296c72ffaaSAndy Whitcroft		  }x;
1306c72ffaaSAndy Whitcroft
1318905a67cSAndy Whitcroftour $NonptrType;
1328905a67cSAndy Whitcroftour $Type;
1338905a67cSAndy Whitcroftour $Declare;
1348905a67cSAndy Whitcroft
135171ae1a4SAndy Whitcroftour $UTF8	= qr {
136171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
137171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
138171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
139171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
140171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
141171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
142171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
143171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
144171ae1a4SAndy Whitcroft}x;
145171ae1a4SAndy Whitcroft
1468905a67cSAndy Whitcroftour @typeList = (
1478905a67cSAndy Whitcroft	qr{void},
148c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
149c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
150c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
151c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
152c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
153c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
154c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
1558905a67cSAndy Whitcroft	qr{unsigned},
1568905a67cSAndy Whitcroft	qr{float},
1578905a67cSAndy Whitcroft	qr{double},
1588905a67cSAndy Whitcroft	qr{bool},
1598905a67cSAndy Whitcroft	qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
1608905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1618905a67cSAndy Whitcroft	qr{union\s+$Ident},
1628905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1638905a67cSAndy Whitcroft	qr{${Ident}_t},
1648905a67cSAndy Whitcroft	qr{${Ident}_handler},
1658905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1668905a67cSAndy Whitcroft);
167c45dcabdSAndy Whitcroftour @modifierList = (
168c45dcabdSAndy Whitcroft	qr{fastcall},
169c45dcabdSAndy Whitcroft);
1708905a67cSAndy Whitcroft
1718905a67cSAndy Whitcroftsub build_types {
172d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
173d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
174c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
1758905a67cSAndy Whitcroft	$NonptrType	= qr{
176d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
177cf655043SAndy Whitcroft			(?:
178c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
179c45dcabdSAndy Whitcroft				(?:${all}\b)
180cf655043SAndy Whitcroft			)
181c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
1828905a67cSAndy Whitcroft		  }x;
1838905a67cSAndy Whitcroft	$Type	= qr{
184c45dcabdSAndy Whitcroft			$NonptrType
1858905a67cSAndy Whitcroft			(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
186c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
1878905a67cSAndy Whitcroft		  }x;
1888905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
1898905a67cSAndy Whitcroft}
1908905a67cSAndy Whitcroftbuild_types();
1916c72ffaaSAndy Whitcroft
1926c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
1930a920b5bSAndy Whitcroft
1944a0df2efSAndy Whitcroftmy @dep_includes = ();
1954a0df2efSAndy Whitcroftmy @dep_functions = ();
1966c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
1976c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
1986c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
1996c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
2000a920b5bSAndy Whitcroft	while (<REMOVE>) {
201f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
202f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
203f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2044a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2054a0df2efSAndy Whitcroft
206f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
207f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
208f0a594c1SAndy Whitcroft				}
2094a0df2efSAndy Whitcroft			}
2100a920b5bSAndy Whitcroft		}
2110a920b5bSAndy Whitcroft	}
2120a920b5bSAndy Whitcroft}
2130a920b5bSAndy Whitcroft
21400df344fSAndy Whitcroftmy @rawlines = ();
215c2fdda0dSAndy Whitcroftmy @lines = ();
216c2fdda0dSAndy Whitcroftmy $vname;
2176c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2186c72ffaaSAndy Whitcroft	if ($file) {
2196c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2206c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2216c72ffaaSAndy Whitcroft	} else {
2226c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2236c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2246c72ffaaSAndy Whitcroft	}
225c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
226c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
227c2fdda0dSAndy Whitcroft	} else {
228c2fdda0dSAndy Whitcroft		$vname = $filename;
229c2fdda0dSAndy Whitcroft	}
2306c72ffaaSAndy Whitcroft	while (<FILE>) {
2310a920b5bSAndy Whitcroft		chomp;
23200df344fSAndy Whitcroft		push(@rawlines, $_);
2336c72ffaaSAndy Whitcroft	}
2346c72ffaaSAndy Whitcroft	close(FILE);
235c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2360a920b5bSAndy Whitcroft		$exit = 1;
2370a920b5bSAndy Whitcroft	}
23800df344fSAndy Whitcroft	@rawlines = ();
23913214adfSAndy Whitcroft	@lines = ();
2400a920b5bSAndy Whitcroft}
2410a920b5bSAndy Whitcroft
2420a920b5bSAndy Whitcroftexit($exit);
2430a920b5bSAndy Whitcroft
2440a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2456c72ffaaSAndy Whitcroft	my ($root) = @_;
2466c72ffaaSAndy Whitcroft
2476c72ffaaSAndy Whitcroft	my @tree_check = (
2486c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2496c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2506c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2516c72ffaaSAndy Whitcroft	);
2526c72ffaaSAndy Whitcroft
2536c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2546c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2550a920b5bSAndy Whitcroft			return 0;
2560a920b5bSAndy Whitcroft		}
2576c72ffaaSAndy Whitcroft	}
2586c72ffaaSAndy Whitcroft	return 1;
2596c72ffaaSAndy Whitcroft}
2600a920b5bSAndy Whitcroft
2610a920b5bSAndy Whitcroftsub expand_tabs {
2620a920b5bSAndy Whitcroft	my ($str) = @_;
2630a920b5bSAndy Whitcroft
2640a920b5bSAndy Whitcroft	my $res = '';
2650a920b5bSAndy Whitcroft	my $n = 0;
2660a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2670a920b5bSAndy Whitcroft		if ($c eq "\t") {
2680a920b5bSAndy Whitcroft			$res .= ' ';
2690a920b5bSAndy Whitcroft			$n++;
2700a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2710a920b5bSAndy Whitcroft				$res .= ' ';
2720a920b5bSAndy Whitcroft			}
2730a920b5bSAndy Whitcroft			next;
2740a920b5bSAndy Whitcroft		}
2750a920b5bSAndy Whitcroft		$res .= $c;
2760a920b5bSAndy Whitcroft		$n++;
2770a920b5bSAndy Whitcroft	}
2780a920b5bSAndy Whitcroft
2790a920b5bSAndy Whitcroft	return $res;
2800a920b5bSAndy Whitcroft}
2816c72ffaaSAndy Whitcroftsub copy_spacing {
282773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
2836c72ffaaSAndy Whitcroft	return $res;
2846c72ffaaSAndy Whitcroft}
2850a920b5bSAndy Whitcroft
2864a0df2efSAndy Whitcroftsub line_stats {
2874a0df2efSAndy Whitcroft	my ($line) = @_;
2884a0df2efSAndy Whitcroft
2894a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
2904a0df2efSAndy Whitcroft	$line =~ s/^.//;
2914a0df2efSAndy Whitcroft	$line = expand_tabs($line);
2924a0df2efSAndy Whitcroft
2934a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
2944a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
2954a0df2efSAndy Whitcroft
2964a0df2efSAndy Whitcroft	return (length($line), length($white));
2974a0df2efSAndy Whitcroft}
2984a0df2efSAndy Whitcroft
299773647a0SAndy Whitcroftmy $sanitise_quote = '';
300773647a0SAndy Whitcroft
301773647a0SAndy Whitcroftsub sanitise_line_reset {
302773647a0SAndy Whitcroft	my ($in_comment) = @_;
303773647a0SAndy Whitcroft
304773647a0SAndy Whitcroft	if ($in_comment) {
305773647a0SAndy Whitcroft		$sanitise_quote = '*/';
306773647a0SAndy Whitcroft	} else {
307773647a0SAndy Whitcroft		$sanitise_quote = '';
308773647a0SAndy Whitcroft	}
309773647a0SAndy Whitcroft}
31000df344fSAndy Whitcroftsub sanitise_line {
31100df344fSAndy Whitcroft	my ($line) = @_;
31200df344fSAndy Whitcroft
31300df344fSAndy Whitcroft	my $res = '';
31400df344fSAndy Whitcroft	my $l = '';
31500df344fSAndy Whitcroft
316c2fdda0dSAndy Whitcroft	my $qlen = 0;
317773647a0SAndy Whitcroft	my $off = 0;
318773647a0SAndy Whitcroft	my $c;
31900df344fSAndy Whitcroft
320773647a0SAndy Whitcroft	# Always copy over the diff marker.
321773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
322773647a0SAndy Whitcroft
323773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
324773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
325773647a0SAndy Whitcroft
326773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
327773647a0SAndy Whitcroft		# and end, all to $;.
328773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
329773647a0SAndy Whitcroft			$sanitise_quote = '*/';
330773647a0SAndy Whitcroft
331773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
332773647a0SAndy Whitcroft			$off++;
33300df344fSAndy Whitcroft			next;
334773647a0SAndy Whitcroft		}
335c45dcabdSAndy Whitcroft		if (substr($line, $off, 2) eq '*/') {
336773647a0SAndy Whitcroft			$sanitise_quote = '';
337773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
338773647a0SAndy Whitcroft			$off++;
339773647a0SAndy Whitcroft			next;
340773647a0SAndy Whitcroft		}
341773647a0SAndy Whitcroft
342773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
343773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
344773647a0SAndy Whitcroft		    $c eq "\\") {
345773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
346773647a0SAndy Whitcroft			$off++;
347773647a0SAndy Whitcroft			next;
348773647a0SAndy Whitcroft		}
349773647a0SAndy Whitcroft		# Regular quotes.
350773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
351773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
352773647a0SAndy Whitcroft				$sanitise_quote = $c;
353773647a0SAndy Whitcroft
354773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
355773647a0SAndy Whitcroft				next;
356773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
357773647a0SAndy Whitcroft				$sanitise_quote = '';
35800df344fSAndy Whitcroft			}
35900df344fSAndy Whitcroft		}
360773647a0SAndy Whitcroft
361773647a0SAndy Whitcroft		#print "SQ:$sanitise_quote\n";
362773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
363773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
364773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
365773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
36600df344fSAndy Whitcroft		} else {
367773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
36800df344fSAndy Whitcroft		}
369c2fdda0dSAndy Whitcroft	}
370c2fdda0dSAndy Whitcroft
371c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
372c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
373c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
374c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
375c2fdda0dSAndy Whitcroft
376c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
377c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
378c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
379c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
380c2fdda0dSAndy Whitcroft	}
381c2fdda0dSAndy Whitcroft
38200df344fSAndy Whitcroft	return $res;
38300df344fSAndy Whitcroft}
38400df344fSAndy Whitcroft
3858905a67cSAndy Whitcroftsub ctx_statement_block {
3868905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3878905a67cSAndy Whitcroft	my $line = $linenr - 1;
3888905a67cSAndy Whitcroft	my $blk = '';
3898905a67cSAndy Whitcroft	my $soff = $off;
3908905a67cSAndy Whitcroft	my $coff = $off - 1;
391773647a0SAndy Whitcroft	my $coff_set = 0;
3928905a67cSAndy Whitcroft
39313214adfSAndy Whitcroft	my $loff = 0;
39413214adfSAndy Whitcroft
3958905a67cSAndy Whitcroft	my $type = '';
3968905a67cSAndy Whitcroft	my $level = 0;
397cf655043SAndy Whitcroft	my $p;
3988905a67cSAndy Whitcroft	my $c;
3998905a67cSAndy Whitcroft	my $len = 0;
40013214adfSAndy Whitcroft
40113214adfSAndy Whitcroft	my $remainder;
4028905a67cSAndy Whitcroft	while (1) {
403773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4048905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4058905a67cSAndy Whitcroft		# context.
4068905a67cSAndy Whitcroft		if ($off >= $len) {
4078905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
408c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4098905a67cSAndy Whitcroft				$remain--;
41013214adfSAndy Whitcroft				$loff = $len;
411c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4128905a67cSAndy Whitcroft				$len = length($blk);
4138905a67cSAndy Whitcroft				$line++;
4148905a67cSAndy Whitcroft				last;
4158905a67cSAndy Whitcroft			}
4168905a67cSAndy Whitcroft			# Bail if there is no further context.
4178905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
41813214adfSAndy Whitcroft			if ($off >= $len) {
4198905a67cSAndy Whitcroft				last;
4208905a67cSAndy Whitcroft			}
4218905a67cSAndy Whitcroft		}
422cf655043SAndy Whitcroft		$p = $c;
4238905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
42413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4258905a67cSAndy Whitcroft
426773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4278905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4288905a67cSAndy Whitcroft		# outermost level.
4298905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4308905a67cSAndy Whitcroft			last;
4318905a67cSAndy Whitcroft		}
4328905a67cSAndy Whitcroft
43313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
434773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
435773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
436773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
437773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
438773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
439773647a0SAndy Whitcroft			$coff_set = 1;
440773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
441773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
44213214adfSAndy Whitcroft		}
44313214adfSAndy Whitcroft
4448905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4458905a67cSAndy Whitcroft			$level++;
4468905a67cSAndy Whitcroft			$type = '(';
4478905a67cSAndy Whitcroft		}
4488905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4498905a67cSAndy Whitcroft			$level--;
4508905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4518905a67cSAndy Whitcroft
4528905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4538905a67cSAndy Whitcroft				$coff = $off;
454773647a0SAndy Whitcroft				$coff_set = 1;
455773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4568905a67cSAndy Whitcroft			}
4578905a67cSAndy Whitcroft		}
4588905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4598905a67cSAndy Whitcroft			$level++;
4608905a67cSAndy Whitcroft			$type = '{';
4618905a67cSAndy Whitcroft		}
4628905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4638905a67cSAndy Whitcroft			$level--;
4648905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4658905a67cSAndy Whitcroft
4668905a67cSAndy Whitcroft			if ($level == 0) {
4678905a67cSAndy Whitcroft				last;
4688905a67cSAndy Whitcroft			}
4698905a67cSAndy Whitcroft		}
4708905a67cSAndy Whitcroft		$off++;
4718905a67cSAndy Whitcroft	}
472a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
47313214adfSAndy Whitcroft	if ($off == $len) {
474a3bb97a7SAndy Whitcroft		$loff = $len + 1;
47513214adfSAndy Whitcroft		$line++;
47613214adfSAndy Whitcroft		$remain--;
47713214adfSAndy Whitcroft	}
4788905a67cSAndy Whitcroft
4798905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4808905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4818905a67cSAndy Whitcroft
4828905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4838905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4848905a67cSAndy Whitcroft
485773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
48613214adfSAndy Whitcroft
48713214adfSAndy Whitcroft	return ($statement, $condition,
48813214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
48913214adfSAndy Whitcroft}
49013214adfSAndy Whitcroft
491cf655043SAndy Whitcroftsub statement_lines {
492cf655043SAndy Whitcroft	my ($stmt) = @_;
493cf655043SAndy Whitcroft
494cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
495cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
496cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
497cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
498cf655043SAndy Whitcroft
499cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
500cf655043SAndy Whitcroft
501cf655043SAndy Whitcroft	return $#stmt_lines + 2;
502cf655043SAndy Whitcroft}
503cf655043SAndy Whitcroft
504cf655043SAndy Whitcroftsub statement_rawlines {
505cf655043SAndy Whitcroft	my ($stmt) = @_;
506cf655043SAndy Whitcroft
507cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
508cf655043SAndy Whitcroft
509cf655043SAndy Whitcroft	return $#stmt_lines + 2;
510cf655043SAndy Whitcroft}
511cf655043SAndy Whitcroft
512cf655043SAndy Whitcroftsub statement_block_size {
513cf655043SAndy Whitcroft	my ($stmt) = @_;
514cf655043SAndy Whitcroft
515cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
516cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
517cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
518cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
519cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
520cf655043SAndy Whitcroft
521cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
522cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
523cf655043SAndy Whitcroft
524cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
525cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
526cf655043SAndy Whitcroft
527cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
528cf655043SAndy Whitcroft		return $stmt_lines;
529cf655043SAndy Whitcroft	} else {
530cf655043SAndy Whitcroft		return $stmt_statements;
531cf655043SAndy Whitcroft	}
532cf655043SAndy Whitcroft}
533cf655043SAndy Whitcroft
53413214adfSAndy Whitcroftsub ctx_statement_full {
53513214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
53613214adfSAndy Whitcroft	my ($statement, $condition, $level);
53713214adfSAndy Whitcroft
53813214adfSAndy Whitcroft	my (@chunks);
53913214adfSAndy Whitcroft
540cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
54113214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
54213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
543773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
54413214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
545cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
546cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
547cf655043SAndy Whitcroft	}
548cf655043SAndy Whitcroft
549cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
550cf655043SAndy Whitcroft	# could continue the statement.
551cf655043SAndy Whitcroft	for (;;) {
55213214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
55313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
554cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
555773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
556cf655043SAndy Whitcroft		#print "C: push\n";
557cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
55813214adfSAndy Whitcroft	}
55913214adfSAndy Whitcroft
56013214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5618905a67cSAndy Whitcroft}
5628905a67cSAndy Whitcroft
5634a0df2efSAndy Whitcroftsub ctx_block_get {
564f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5654a0df2efSAndy Whitcroft	my $line;
5664a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5674a0df2efSAndy Whitcroft	my $blk = '';
5684a0df2efSAndy Whitcroft	my @o;
5694a0df2efSAndy Whitcroft	my @c;
5704a0df2efSAndy Whitcroft	my @res = ();
5714a0df2efSAndy Whitcroft
572f0a594c1SAndy Whitcroft	my $level = 0;
57300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
57400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
57500df344fSAndy Whitcroft		$remain--;
57600df344fSAndy Whitcroft
57700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
578f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
579f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
580f0a594c1SAndy Whitcroft			if ($off > 0) {
581f0a594c1SAndy Whitcroft				$off--;
582f0a594c1SAndy Whitcroft				next;
583f0a594c1SAndy Whitcroft			}
5844a0df2efSAndy Whitcroft
585f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
586f0a594c1SAndy Whitcroft				$level--;
587f0a594c1SAndy Whitcroft				last if ($level == 0);
588f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
589f0a594c1SAndy Whitcroft				$level++;
590f0a594c1SAndy Whitcroft			}
591f0a594c1SAndy Whitcroft		}
5924a0df2efSAndy Whitcroft
593f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
59400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
5954a0df2efSAndy Whitcroft		}
5964a0df2efSAndy Whitcroft
597f0a594c1SAndy Whitcroft		last if ($level == 0);
5984a0df2efSAndy Whitcroft	}
5994a0df2efSAndy Whitcroft
600f0a594c1SAndy Whitcroft	return ($level, @res);
6014a0df2efSAndy Whitcroft}
6024a0df2efSAndy Whitcroftsub ctx_block_outer {
6034a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6044a0df2efSAndy Whitcroft
605f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
606f0a594c1SAndy Whitcroft	return @r;
6074a0df2efSAndy Whitcroft}
6084a0df2efSAndy Whitcroftsub ctx_block {
6094a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6104a0df2efSAndy Whitcroft
611f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
612f0a594c1SAndy Whitcroft	return @r;
613653d4876SAndy Whitcroft}
614653d4876SAndy Whitcroftsub ctx_statement {
615f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
616f0a594c1SAndy Whitcroft
617f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
618f0a594c1SAndy Whitcroft	return @r;
619f0a594c1SAndy Whitcroft}
620f0a594c1SAndy Whitcroftsub ctx_block_level {
621653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
622653d4876SAndy Whitcroft
623f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6244a0df2efSAndy Whitcroft}
6259c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6269c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6279c0ca6f9SAndy Whitcroft
6289c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6299c0ca6f9SAndy Whitcroft}
6304a0df2efSAndy Whitcroft
6314a0df2efSAndy Whitcroftsub ctx_locate_comment {
6324a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6334a0df2efSAndy Whitcroft
6344a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
635beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
6364a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6374a0df2efSAndy Whitcroft
6384a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6394a0df2efSAndy Whitcroft	# comment.
6404a0df2efSAndy Whitcroft	my $in_comment = 0;
6414a0df2efSAndy Whitcroft	$current_comment = '';
6424a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
64300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
64400df344fSAndy Whitcroft		#warn "           $line\n";
6454a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6464a0df2efSAndy Whitcroft			$in_comment = 1;
6474a0df2efSAndy Whitcroft		}
6484a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6494a0df2efSAndy Whitcroft			$in_comment = 1;
6504a0df2efSAndy Whitcroft		}
6514a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6524a0df2efSAndy Whitcroft			$current_comment = '';
6534a0df2efSAndy Whitcroft		}
6544a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6554a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6564a0df2efSAndy Whitcroft			$in_comment = 0;
6574a0df2efSAndy Whitcroft		}
6584a0df2efSAndy Whitcroft	}
6594a0df2efSAndy Whitcroft
6604a0df2efSAndy Whitcroft	chomp($current_comment);
6614a0df2efSAndy Whitcroft	return($current_comment);
6624a0df2efSAndy Whitcroft}
6634a0df2efSAndy Whitcroftsub ctx_has_comment {
6644a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6654a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6664a0df2efSAndy Whitcroft
66700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6684a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6694a0df2efSAndy Whitcroft
6704a0df2efSAndy Whitcroft	return ($cmt ne '');
6714a0df2efSAndy Whitcroft}
6724a0df2efSAndy Whitcroft
6730a920b5bSAndy Whitcroftsub cat_vet {
6740a920b5bSAndy Whitcroft	my ($vet) = @_;
6759c0ca6f9SAndy Whitcroft	my ($res, $coded);
6760a920b5bSAndy Whitcroft
6779c0ca6f9SAndy Whitcroft	$res = '';
6786c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
6796c72ffaaSAndy Whitcroft		$res .= $1;
6806c72ffaaSAndy Whitcroft		if ($2 ne '') {
6819c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
6826c72ffaaSAndy Whitcroft			$res .= $coded;
6836c72ffaaSAndy Whitcroft		}
6849c0ca6f9SAndy Whitcroft	}
6859c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
6860a920b5bSAndy Whitcroft
6879c0ca6f9SAndy Whitcroft	return $res;
6880a920b5bSAndy Whitcroft}
6890a920b5bSAndy Whitcroft
690c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
691cf655043SAndy Whitcroftmy $av_pending;
692c2fdda0dSAndy Whitcroftmy @av_paren_type;
6931f65f947SAndy Whitcroftmy $av_pend_colon;
694c2fdda0dSAndy Whitcroft
695c2fdda0dSAndy Whitcroftsub annotate_reset {
696c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
697cf655043SAndy Whitcroft	$av_pending = '_';
698cf655043SAndy Whitcroft	@av_paren_type = ('E');
6991f65f947SAndy Whitcroft	$av_pend_colon = 'O';
700c2fdda0dSAndy Whitcroft}
701c2fdda0dSAndy Whitcroft
7026c72ffaaSAndy Whitcroftsub annotate_values {
7036c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
7046c72ffaaSAndy Whitcroft
7056c72ffaaSAndy Whitcroft	my $res;
7061f65f947SAndy Whitcroft	my $var = '_' x length($stream);
7076c72ffaaSAndy Whitcroft	my $cur = $stream;
7086c72ffaaSAndy Whitcroft
709c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
7106c72ffaaSAndy Whitcroft
7116c72ffaaSAndy Whitcroft	while (length($cur)) {
712773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
713cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
714171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
7156c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
716c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
717c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
718cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
719c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7206c72ffaaSAndy Whitcroft			}
7216c72ffaaSAndy Whitcroft
7228ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
723c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7246c72ffaaSAndy Whitcroft			$type = 'T';
7256c72ffaaSAndy Whitcroft
726389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
727389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
728389a2fe5SAndy Whitcroft			$type = 'T';
729389a2fe5SAndy Whitcroft
730c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
731171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
732c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
733171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
734171ae1a4SAndy Whitcroft			if ($2 ne '') {
735cf655043SAndy Whitcroft				$av_pending = 'N';
736171ae1a4SAndy Whitcroft			}
737171ae1a4SAndy Whitcroft			$type = 'E';
738171ae1a4SAndy Whitcroft
739c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
740171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
741171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
742171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
7436c72ffaaSAndy Whitcroft
744c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
745cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
746c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
747cf655043SAndy Whitcroft
748cf655043SAndy Whitcroft			push(@av_paren_type, $type);
749cf655043SAndy Whitcroft			push(@av_paren_type, $type);
750171ae1a4SAndy Whitcroft			$type = 'E';
751cf655043SAndy Whitcroft
752c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
753cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
754cf655043SAndy Whitcroft			$av_preprocessor = 1;
755cf655043SAndy Whitcroft
756cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
757cf655043SAndy Whitcroft
758171ae1a4SAndy Whitcroft			$type = 'E';
759cf655043SAndy Whitcroft
760c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
761cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
762cf655043SAndy Whitcroft
763cf655043SAndy Whitcroft			$av_preprocessor = 1;
764cf655043SAndy Whitcroft
765cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
766cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
767cf655043SAndy Whitcroft			pop(@av_paren_type);
768cf655043SAndy Whitcroft			push(@av_paren_type, $type);
769171ae1a4SAndy Whitcroft			$type = 'E';
7706c72ffaaSAndy Whitcroft
7716c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
772c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7736c72ffaaSAndy Whitcroft
774171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
775171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
776171ae1a4SAndy Whitcroft			$av_pending = $type;
777171ae1a4SAndy Whitcroft			$type = 'N';
778171ae1a4SAndy Whitcroft
7796c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
780c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
7816c72ffaaSAndy Whitcroft			if (defined $2) {
782cf655043SAndy Whitcroft				$av_pending = 'V';
7836c72ffaaSAndy Whitcroft			}
7846c72ffaaSAndy Whitcroft			$type = 'N';
7856c72ffaaSAndy Whitcroft
78614b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
787c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
78814b111c1SAndy Whitcroft			$av_pending = 'E';
7896c72ffaaSAndy Whitcroft			$type = 'N';
7906c72ffaaSAndy Whitcroft
7911f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
7921f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
7931f65f947SAndy Whitcroft			$av_pend_colon = 'C';
7941f65f947SAndy Whitcroft			$type = 'N';
7951f65f947SAndy Whitcroft
79614b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
797c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
7986c72ffaaSAndy Whitcroft			$type = 'N';
7996c72ffaaSAndy Whitcroft
8006c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
801c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
802cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
803cf655043SAndy Whitcroft			$av_pending = '_';
8046c72ffaaSAndy Whitcroft			$type = 'N';
8056c72ffaaSAndy Whitcroft
8066c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
807cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
808cf655043SAndy Whitcroft			if ($new_type ne '_') {
809cf655043SAndy Whitcroft				$type = $new_type;
810c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
811c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
8126c72ffaaSAndy Whitcroft			} else {
813c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
8146c72ffaaSAndy Whitcroft			}
8156c72ffaaSAndy Whitcroft
816c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
817c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
818c8cb2ca3SAndy Whitcroft			$type = 'V';
819cf655043SAndy Whitcroft			$av_pending = 'V';
8206c72ffaaSAndy Whitcroft
8211f65f947SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):/) {
8221f65f947SAndy Whitcroft			if ($type eq 'E') {
8231f65f947SAndy Whitcroft				$av_pend_colon = 'L';
8241f65f947SAndy Whitcroft			} elsif ($type eq 'T') {
8251f65f947SAndy Whitcroft				$av_pend_colon = 'B';
8261f65f947SAndy Whitcroft			}
8271f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
8281f65f947SAndy Whitcroft			$type = 'V';
8291f65f947SAndy Whitcroft
8306c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
831c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
8326c72ffaaSAndy Whitcroft			$type = 'V';
8336c72ffaaSAndy Whitcroft
8346c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
835c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
8366c72ffaaSAndy Whitcroft			$type = 'N';
8376c72ffaaSAndy Whitcroft
838cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
839c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
84013214adfSAndy Whitcroft			$type = 'E';
8411f65f947SAndy Whitcroft			$av_pend_colon = 'O';
84213214adfSAndy Whitcroft
8431f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
8441f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
8451f65f947SAndy Whitcroft			$type = 'N';
8461f65f947SAndy Whitcroft
8471f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
8481f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
8491f65f947SAndy Whitcroft
8501f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
8511f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
8521f65f947SAndy Whitcroft				$type = 'E';
8531f65f947SAndy Whitcroft			} else {
8541f65f947SAndy Whitcroft				$type = 'N';
8551f65f947SAndy Whitcroft			}
8561f65f947SAndy Whitcroft			$av_pend_colon = 'O';
8571f65f947SAndy Whitcroft
8581f65f947SAndy Whitcroft		} elsif ($cur =~ /^(;|\[)/o) {
85913214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
8606c72ffaaSAndy Whitcroft			$type = 'N';
8616c72ffaaSAndy Whitcroft
8620d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
86374048ed8SAndy Whitcroft			my $variant;
86474048ed8SAndy Whitcroft
86574048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
86674048ed8SAndy Whitcroft			if ($type eq 'V') {
86774048ed8SAndy Whitcroft				$variant = 'B';
86874048ed8SAndy Whitcroft			} else {
86974048ed8SAndy Whitcroft				$variant = 'U';
87074048ed8SAndy Whitcroft			}
87174048ed8SAndy Whitcroft
87274048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
87374048ed8SAndy Whitcroft			$type = 'N';
87474048ed8SAndy Whitcroft
8756c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
876c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
8776c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
8786c72ffaaSAndy Whitcroft				$type = 'N';
8796c72ffaaSAndy Whitcroft			}
8806c72ffaaSAndy Whitcroft
8816c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
882c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
8836c72ffaaSAndy Whitcroft		}
8846c72ffaaSAndy Whitcroft		if (defined $1) {
8856c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
8866c72ffaaSAndy Whitcroft			$res .= $type x length($1);
8876c72ffaaSAndy Whitcroft		}
8886c72ffaaSAndy Whitcroft	}
8896c72ffaaSAndy Whitcroft
8901f65f947SAndy Whitcroft	return ($res, $var);
8916c72ffaaSAndy Whitcroft}
8926c72ffaaSAndy Whitcroft
8938905a67cSAndy Whitcroftsub possible {
89413214adfSAndy Whitcroft	my ($possible, $line) = @_;
8958905a67cSAndy Whitcroft
896c45dcabdSAndy Whitcroft	print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
8970221f55cSAndy Whitcroft	if ($possible !~ /^(?:$Modifier|$Storage|$Type|DEFINE_\S+)$/ &&
8988905a67cSAndy Whitcroft	    $possible ne 'goto' && $possible ne 'return' &&
8998905a67cSAndy Whitcroft	    $possible ne 'case' && $possible ne 'else' &&
900d3ddcf47SAndy Whitcroft	    $possible ne 'asm' && $possible ne '__asm__' &&
901c45dcabdSAndy Whitcroft	    $possible !~ /^(typedef|struct|enum)\b/) {
902c45dcabdSAndy Whitcroft		# Check for modifiers.
903c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
904c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
905c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
906c45dcabdSAndy Whitcroft
907c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
908c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
909d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
910d2506586SAndy Whitcroft				warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
911d2506586SAndy Whitcroft				push(@modifierList, $modifier);
912d2506586SAndy Whitcroft			}
913c45dcabdSAndy Whitcroft
914c45dcabdSAndy Whitcroft		} else {
91513214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
9168905a67cSAndy Whitcroft			push(@typeList, $possible);
917c45dcabdSAndy Whitcroft		}
9188905a67cSAndy Whitcroft		build_types();
9198905a67cSAndy Whitcroft	}
9208905a67cSAndy Whitcroft}
9218905a67cSAndy Whitcroft
9226c72ffaaSAndy Whitcroftmy $prefix = '';
9236c72ffaaSAndy Whitcroft
924f0a594c1SAndy Whitcroftsub report {
925773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
926773647a0SAndy Whitcroft		return 0;
927773647a0SAndy Whitcroft	}
9288905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
9298905a67cSAndy Whitcroft
9308905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
9318905a67cSAndy Whitcroft
93213214adfSAndy Whitcroft	push(our @report, $line);
933773647a0SAndy Whitcroft
934773647a0SAndy Whitcroft	return 1;
935f0a594c1SAndy Whitcroft}
936f0a594c1SAndy Whitcroftsub report_dump {
93713214adfSAndy Whitcroft	our @report;
938f0a594c1SAndy Whitcroft}
939de7d4f0eSAndy Whitcroftsub ERROR {
940773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
941de7d4f0eSAndy Whitcroft		our $clean = 0;
9426c72ffaaSAndy Whitcroft		our $cnt_error++;
943de7d4f0eSAndy Whitcroft	}
944773647a0SAndy Whitcroft}
945de7d4f0eSAndy Whitcroftsub WARN {
946773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
947de7d4f0eSAndy Whitcroft		our $clean = 0;
9486c72ffaaSAndy Whitcroft		our $cnt_warn++;
949de7d4f0eSAndy Whitcroft	}
950773647a0SAndy Whitcroft}
951de7d4f0eSAndy Whitcroftsub CHK {
952773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
953de7d4f0eSAndy Whitcroft		our $clean = 0;
9546c72ffaaSAndy Whitcroft		our $cnt_chk++;
9556c72ffaaSAndy Whitcroft	}
956de7d4f0eSAndy Whitcroft}
957de7d4f0eSAndy Whitcroft
9580a920b5bSAndy Whitcroftsub process {
9590a920b5bSAndy Whitcroft	my $filename = shift;
9600a920b5bSAndy Whitcroft
9610a920b5bSAndy Whitcroft	my $linenr=0;
9620a920b5bSAndy Whitcroft	my $prevline="";
963c2fdda0dSAndy Whitcroft	my $prevrawline="";
9640a920b5bSAndy Whitcroft	my $stashline="";
965c2fdda0dSAndy Whitcroft	my $stashrawline="";
9660a920b5bSAndy Whitcroft
9674a0df2efSAndy Whitcroft	my $length;
9680a920b5bSAndy Whitcroft	my $indent;
9690a920b5bSAndy Whitcroft	my $previndent=0;
9700a920b5bSAndy Whitcroft	my $stashindent=0;
9710a920b5bSAndy Whitcroft
972de7d4f0eSAndy Whitcroft	our $clean = 1;
9730a920b5bSAndy Whitcroft	my $signoff = 0;
9740a920b5bSAndy Whitcroft	my $is_patch = 0;
9750a920b5bSAndy Whitcroft
97613214adfSAndy Whitcroft	our @report = ();
9776c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
9786c72ffaaSAndy Whitcroft	our $cnt_error = 0;
9796c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
9806c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
9816c72ffaaSAndy Whitcroft
9820a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
9830a920b5bSAndy Whitcroft	my $realfile = '';
9840a920b5bSAndy Whitcroft	my $realline = 0;
9850a920b5bSAndy Whitcroft	my $realcnt = 0;
9860a920b5bSAndy Whitcroft	my $here = '';
9870a920b5bSAndy Whitcroft	my $in_comment = 0;
988c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
9890a920b5bSAndy Whitcroft	my $first_line = 0;
9900a920b5bSAndy Whitcroft
99113214adfSAndy Whitcroft	my $prev_values = 'E';
99213214adfSAndy Whitcroft
99313214adfSAndy Whitcroft	# suppression flags
994773647a0SAndy Whitcroft	my %suppress_ifbraces;
995653d4876SAndy Whitcroft
996c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
997de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
998c2fdda0dSAndy Whitcroft	#
999de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1000de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1001773647a0SAndy Whitcroft
1002773647a0SAndy Whitcroft	sanitise_line_reset();
1003c2fdda0dSAndy Whitcroft	my $line;
1004c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1005773647a0SAndy Whitcroft		$linenr++;
1006773647a0SAndy Whitcroft		$line = $rawline;
1007c2fdda0dSAndy Whitcroft
1008773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1009de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1010de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1011de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1012de7d4f0eSAndy Whitcroft			}
1013773647a0SAndy Whitcroft			#next;
1014de7d4f0eSAndy Whitcroft		}
1015773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1016773647a0SAndy Whitcroft			$realline=$1-1;
1017773647a0SAndy Whitcroft			if (defined $2) {
1018773647a0SAndy Whitcroft				$realcnt=$3+1;
1019773647a0SAndy Whitcroft			} else {
1020773647a0SAndy Whitcroft				$realcnt=1+1;
1021773647a0SAndy Whitcroft			}
1022c45dcabdSAndy Whitcroft			$in_comment = 0;
1023773647a0SAndy Whitcroft
1024773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1025773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1026773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1027773647a0SAndy Whitcroft			# at context start.
1028773647a0SAndy Whitcroft			my $edge;
1029171ae1a4SAndy Whitcroft			for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
1030773647a0SAndy Whitcroft				next if ($line =~ /^-/);
1031773647a0SAndy Whitcroft				($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
1032773647a0SAndy Whitcroft				last if (defined $edge);
1033773647a0SAndy Whitcroft			}
1034773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1035773647a0SAndy Whitcroft				$in_comment = 1;
1036773647a0SAndy Whitcroft			}
1037773647a0SAndy Whitcroft
1038773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1039773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1040773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1041773647a0SAndy Whitcroft			if (!defined $edge &&
1042773647a0SAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
1043773647a0SAndy Whitcroft			{
1044773647a0SAndy Whitcroft				$in_comment = 1;
1045773647a0SAndy Whitcroft			}
1046773647a0SAndy Whitcroft
1047773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1048773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1049773647a0SAndy Whitcroft
1050171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1051773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1052171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1053773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1054773647a0SAndy Whitcroft		}
1055773647a0SAndy Whitcroft		push(@lines, $line);
1056773647a0SAndy Whitcroft
1057773647a0SAndy Whitcroft		if ($realcnt > 1) {
1058773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1059773647a0SAndy Whitcroft		} else {
1060773647a0SAndy Whitcroft			$realcnt = 0;
1061773647a0SAndy Whitcroft		}
1062773647a0SAndy Whitcroft
1063773647a0SAndy Whitcroft		#print "==>$rawline\n";
1064773647a0SAndy Whitcroft		#print "-->$line\n";
1065de7d4f0eSAndy Whitcroft
1066de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1067de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1068de7d4f0eSAndy Whitcroft		}
1069de7d4f0eSAndy Whitcroft	}
1070de7d4f0eSAndy Whitcroft
10716c72ffaaSAndy Whitcroft	$prefix = '';
10726c72ffaaSAndy Whitcroft
1073773647a0SAndy Whitcroft	$realcnt = 0;
1074773647a0SAndy Whitcroft	$linenr = 0;
10750a920b5bSAndy Whitcroft	foreach my $line (@lines) {
10760a920b5bSAndy Whitcroft		$linenr++;
10770a920b5bSAndy Whitcroft
1078c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
10796c72ffaaSAndy Whitcroft
10800a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
10816c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
10820a920b5bSAndy Whitcroft			$is_patch = 1;
10834a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
10840a920b5bSAndy Whitcroft			$realline=$1-1;
10850a920b5bSAndy Whitcroft			if (defined $2) {
10860a920b5bSAndy Whitcroft				$realcnt=$3+1;
10870a920b5bSAndy Whitcroft			} else {
10880a920b5bSAndy Whitcroft				$realcnt=1+1;
10890a920b5bSAndy Whitcroft			}
1090c2fdda0dSAndy Whitcroft			annotate_reset();
109113214adfSAndy Whitcroft			$prev_values = 'E';
109213214adfSAndy Whitcroft
1093773647a0SAndy Whitcroft			%suppress_ifbraces = ();
10940a920b5bSAndy Whitcroft			next;
10950a920b5bSAndy Whitcroft
10964a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
10974a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
10984a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1099773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
11000a920b5bSAndy Whitcroft			$realline++;
1101d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
11020a920b5bSAndy Whitcroft
11034a0df2efSAndy Whitcroft			# Measure the line length and indent.
1104c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
11050a920b5bSAndy Whitcroft
11060a920b5bSAndy Whitcroft			# Track the previous line.
11070a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
11080a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1109c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1110c2fdda0dSAndy Whitcroft
1111773647a0SAndy Whitcroft			#warn "line<$line>\n";
11126c72ffaaSAndy Whitcroft
1113d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1114d8aaf121SAndy Whitcroft			$realcnt--;
11150a920b5bSAndy Whitcroft		}
11160a920b5bSAndy Whitcroft
11170a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1118773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1119773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1120773647a0SAndy Whitcroft
11216c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
11226c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1123773647a0SAndy Whitcroft
1124773647a0SAndy Whitcroft		# extract the filename as it passes
1125773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1126773647a0SAndy Whitcroft			$realfile = $1;
1127773647a0SAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
1128773647a0SAndy Whitcroft
1129773647a0SAndy Whitcroft			if ($realfile =~ m@include/asm/@) {
1130773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1131773647a0SAndy Whitcroft			}
1132773647a0SAndy Whitcroft			next;
1133773647a0SAndy Whitcroft		}
1134773647a0SAndy Whitcroft
1135389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
11360a920b5bSAndy Whitcroft
1137c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1138c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1139c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
11400a920b5bSAndy Whitcroft
11416c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
11426c72ffaaSAndy Whitcroft
11430a920b5bSAndy Whitcroft#check the patch for a signoff:
1144d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
11454a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
11464a0df2efSAndy Whitcroft			$signoff++;
11470a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1148de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1149de7d4f0eSAndy Whitcroft					$herecurr);
11500a920b5bSAndy Whitcroft			}
11510a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1152773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1153de7d4f0eSAndy Whitcroft					$herecurr);
11540a920b5bSAndy Whitcroft			}
11550a920b5bSAndy Whitcroft		}
11560a920b5bSAndy Whitcroft
115700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
11588905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1159de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
11606c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1161de7d4f0eSAndy Whitcroft		}
1162de7d4f0eSAndy Whitcroft
1163de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1164de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1165171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1166171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1167171ae1a4SAndy Whitcroft
1168171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1169171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1170171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1171171ae1a4SAndy Whitcroft
1172171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
117300df344fSAndy Whitcroft		}
11740a920b5bSAndy Whitcroft
117500df344fSAndy Whitcroft#ignore lines being removed
117600df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
117700df344fSAndy Whitcroft
117800df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
117900df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
11800a920b5bSAndy Whitcroft
11810a920b5bSAndy Whitcroft#trailing whitespace
11829c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1183c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
11849c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
11859c0ca6f9SAndy Whitcroft
1186c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1187c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1188de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
11890a920b5bSAndy Whitcroft		}
11900a920b5bSAndy Whitcroft#80 column limit
1191c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1192f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1193f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1194f4c014c0SAndy Whitcroft		    $length > 80)
1195c45dcabdSAndy Whitcroft		{
1196de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
11970a920b5bSAndy Whitcroft		}
11980a920b5bSAndy Whitcroft
11998905a67cSAndy Whitcroft# check for adding lines without a newline.
12008905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
12018905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
12028905a67cSAndy Whitcroft		}
12038905a67cSAndy Whitcroft
12040a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
12050a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
12060a920b5bSAndy Whitcroft
12070a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
12080a920b5bSAndy Whitcroft# more than 8 must use tabs.
1209c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1210c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1211c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1212171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
12130a920b5bSAndy Whitcroft		}
12140a920b5bSAndy Whitcroft
1215c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1216cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1217c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1218c2fdda0dSAndy Whitcroft		}
121922f2a2efSAndy Whitcroft
12209c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1221f5fe35ddSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next);
12229c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1223f5fe35ddSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next) =
1224f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1225171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1226171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1227171ae1a4SAndy Whitcroft
1228171ae1a4SAndy Whitcroft			my $s = $stat;
1229171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1230cf655043SAndy Whitcroft
1231c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1232171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1233c2fdda0dSAndy Whitcroft
1234c2fdda0dSAndy Whitcroft			# Ignore functions being called
1235171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1236c2fdda0dSAndy Whitcroft
1237c45dcabdSAndy Whitcroft			# declarations always start with types
1238d2506586SAndy 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) {
1239c45dcabdSAndy Whitcroft				my $type = $1;
1240c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1241c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1242c45dcabdSAndy Whitcroft
12436c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1244171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1245c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1246c2fdda0dSAndy Whitcroft			}
12478905a67cSAndy Whitcroft
12486c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1249171ae1a4SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1250c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
12519c0ca6f9SAndy Whitcroft			}
12528905a67cSAndy Whitcroft
12538905a67cSAndy Whitcroft			# Check for any sort of function declaration.
12548905a67cSAndy Whitcroft			# int foo(something bar, other baz);
12558905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1256171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
12578905a67cSAndy Whitcroft				my ($name_len) = length($1);
12588905a67cSAndy Whitcroft
1259cf655043SAndy Whitcroft				my $ctx = $s;
1260773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
12618905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1262cf655043SAndy Whitcroft
12638905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1264c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
12658905a67cSAndy Whitcroft
1266c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
12678905a67cSAndy Whitcroft					}
12688905a67cSAndy Whitcroft				}
12698905a67cSAndy Whitcroft			}
12708905a67cSAndy Whitcroft
12719c0ca6f9SAndy Whitcroft		}
12729c0ca6f9SAndy Whitcroft
127300df344fSAndy Whitcroft#
127400df344fSAndy Whitcroft# Checks which may be anchored in the context.
127500df344fSAndy Whitcroft#
127600df344fSAndy Whitcroft
127700df344fSAndy Whitcroft# Check for switch () and associated case and default
127800df344fSAndy Whitcroft# statements should be at the same indent.
127900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
128000df344fSAndy Whitcroft			my $err = '';
128100df344fSAndy Whitcroft			my $sep = '';
128200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
128300df344fSAndy Whitcroft			shift(@ctx);
128400df344fSAndy Whitcroft			for my $ctx (@ctx) {
128500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
128600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
128700df344fSAndy Whitcroft							$indent != $cindent) {
128800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
128900df344fSAndy Whitcroft					$sep = '';
129000df344fSAndy Whitcroft				} else {
129100df344fSAndy Whitcroft					$sep = "[...]\n";
129200df344fSAndy Whitcroft				}
129300df344fSAndy Whitcroft			}
129400df344fSAndy Whitcroft			if ($err ne '') {
12959c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1296de7d4f0eSAndy Whitcroft			}
1297de7d4f0eSAndy Whitcroft		}
1298e2a763c2SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
12991bdab9e5SAndy Whitcroft		    $line !~ /\G(?:
13001bdab9e5SAndy Whitcroft			(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
13011bdab9e5SAndy Whitcroft			\s*return\s+
13021bdab9e5SAndy Whitcroft		    )/xg)
13031bdab9e5SAndy Whitcroft		{
1304e2a763c2SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
1305e2a763c2SAndy Whitcroft		}
1306de7d4f0eSAndy Whitcroft
1307de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1308de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1309c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1310773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1311773647a0SAndy Whitcroft
13129c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1313de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1314de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1315de7d4f0eSAndy Whitcroft
1316548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1317548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1318de7d4f0eSAndy Whitcroft
1319548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1320548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1321548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1322548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1323548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1324773647a0SAndy Whitcroft				$ctx_ln++;
1325773647a0SAndy Whitcroft			}
1326548596d5SAndy Whitcroft
132753210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
132853210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1329773647a0SAndy Whitcroft
1330773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1331773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1332c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
133300df344fSAndy Whitcroft			}
1334773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1335773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1336773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1337773647a0SAndy Whitcroft			{
13389c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
13399c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1340773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1341c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
13429c0ca6f9SAndy Whitcroft				}
13439c0ca6f9SAndy Whitcroft			}
134400df344fSAndy Whitcroft		}
134500df344fSAndy Whitcroft
13466c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
13476c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
13481f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
13491f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
13506c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1351c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1352c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1353cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1354cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
13551f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1356c2fdda0dSAndy Whitcroft		}
13576c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
13586c72ffaaSAndy Whitcroft
135900df344fSAndy Whitcroft#ignore lines not being added
136000df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
136100df344fSAndy Whitcroft
1362653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
13637429c690SAndy Whitcroft		if ($dbg_type) {
13647429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
13657429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
13667429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
13677429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
13687429c690SAndy Whitcroft			}
1369653d4876SAndy Whitcroft			next;
1370653d4876SAndy Whitcroft		}
1371*a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1372*a1ef277eSAndy Whitcroft		if ($dbg_attr) {
1373*a1ef277eSAndy Whitcroft			if ($line =~ /^.\s*$Attribute\s*$/) {
1374*a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
1375*a1ef277eSAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1376*a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1377*a1ef277eSAndy Whitcroft			}
1378*a1ef277eSAndy Whitcroft			next;
1379*a1ef277eSAndy Whitcroft		}
1380653d4876SAndy Whitcroft
1381f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1382f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1383f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1384773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1385f0a594c1SAndy Whitcroft		}
1386f0a594c1SAndy Whitcroft
138700df344fSAndy Whitcroft#
138800df344fSAndy Whitcroft# Checks which are anchored on the added line.
138900df344fSAndy Whitcroft#
139000df344fSAndy Whitcroft
1391653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1392c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1393653d4876SAndy Whitcroft			my $path = $1;
1394653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1395de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1396de7d4f0eSAndy Whitcroft					$herecurr);
1397653d4876SAndy Whitcroft			}
1398653d4876SAndy Whitcroft		}
1399653d4876SAndy Whitcroft
140000df344fSAndy Whitcroft# no C99 // comments
140100df344fSAndy Whitcroft		if ($line =~ m{//}) {
1402de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
140300df344fSAndy Whitcroft		}
140400df344fSAndy Whitcroft		# Remove C99 comments.
14050a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
14066c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
14070a920b5bSAndy Whitcroft
14080a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1409653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1410653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1411653d4876SAndy Whitcroft			my $name = $1;
14120a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
14130a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
1414653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
1415cf655043SAndy Whitcroft			   ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1416cf655043SAndy Whitcroft			   ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1417171ae1a4SAndy Whitcroft			   ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
1418cf655043SAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1419de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
14200a920b5bSAndy Whitcroft			}
14210a920b5bSAndy Whitcroft		}
14220a920b5bSAndy Whitcroft
1423f0a594c1SAndy Whitcroft# check for external initialisers.
1424c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1425f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1426f0a594c1SAndy Whitcroft				$herecurr);
1427f0a594c1SAndy Whitcroft		}
14280a920b5bSAndy Whitcroft# check for static initialisers.
14299c9ba34eSAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1430de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1431de7d4f0eSAndy Whitcroft				$herecurr);
14320a920b5bSAndy Whitcroft		}
14330a920b5bSAndy Whitcroft
1434653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1435653d4876SAndy Whitcroft# make sense.
1436653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
14379c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1438c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1439653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1440de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
14410a920b5bSAndy Whitcroft		}
14420a920b5bSAndy Whitcroft
14430a920b5bSAndy Whitcroft# * goes on variable not on type
1444d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1445de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1446de7d4f0eSAndy Whitcroft				$herecurr);
1447d8aaf121SAndy Whitcroft
1448d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1449de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1450de7d4f0eSAndy Whitcroft				$herecurr);
1451d8aaf121SAndy Whitcroft
1452234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1453de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1454de7d4f0eSAndy Whitcroft				$herecurr);
1455d8aaf121SAndy Whitcroft
1456234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1457de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1458de7d4f0eSAndy Whitcroft				$herecurr);
14590a920b5bSAndy Whitcroft		}
14600a920b5bSAndy Whitcroft
14610a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
14620a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
14630a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
14640a920b5bSAndy Whitcroft# 			print "$herecurr";
14650a920b5bSAndy Whitcroft# 			$clean = 0;
14660a920b5bSAndy Whitcroft# 		}
14670a920b5bSAndy Whitcroft
14688905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1469c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
14708905a67cSAndy Whitcroft		}
14718905a67cSAndy Whitcroft
147200df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
147300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
147400df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
147500df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
147600df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1477f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
147800df344fSAndy Whitcroft			my $ok = 0;
147900df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
148000df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
148100df344fSAndy Whitcroft				# we have a preceeding printk if it ends
148200df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
148300df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
148400df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
148500df344fSAndy Whitcroft						$ok = 1;
148600df344fSAndy Whitcroft					}
148700df344fSAndy Whitcroft					last;
148800df344fSAndy Whitcroft				}
148900df344fSAndy Whitcroft			}
149000df344fSAndy Whitcroft			if ($ok == 0) {
1491de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
14920a920b5bSAndy Whitcroft			}
149300df344fSAndy Whitcroft		}
14940a920b5bSAndy Whitcroft
1495653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1496653d4876SAndy Whitcroft# or if closed on same line
1497c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1498c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1499de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
15000a920b5bSAndy Whitcroft		}
1501653d4876SAndy Whitcroft
15028905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
15038905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
15048905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
15058905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
15068905a67cSAndy Whitcroft		}
15078905a67cSAndy Whitcroft
15088d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
15098d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1510fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1511fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
15128d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
15138d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
15148d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1515fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1516fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
15178d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
15188d31cfceSAndy Whitcroft			}
15198d31cfceSAndy Whitcroft		}
15208d31cfceSAndy Whitcroft
1521f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
15226c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1523c2fdda0dSAndy Whitcroft			my $name = $1;
1524773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1525773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1526c2fdda0dSAndy Whitcroft
1527c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1528773647a0SAndy Whitcroft			if ($name =~ /^(?:
1529773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1530773647a0SAndy Whitcroft				volatile|__volatile__|
1531773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1532773647a0SAndy Whitcroft				asm|__asm__)$/x)
1533773647a0SAndy Whitcroft			{
1534c2fdda0dSAndy Whitcroft
1535c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1536c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1537c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1538c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1539773647a0SAndy Whitcroft
1540773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1541c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1542c2fdda0dSAndy Whitcroft
1543c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1544c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1545773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1546c2fdda0dSAndy Whitcroft
1547c2fdda0dSAndy Whitcroft			} else {
1548773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1549f0a594c1SAndy Whitcroft			}
15506c72ffaaSAndy Whitcroft		}
1551653d4876SAndy Whitcroft# Check operator spacing.
15520a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
15539c0ca6f9SAndy Whitcroft			my $ops = qr{
15549c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
15559c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
15569c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
15571f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
15581f65f947SAndy Whitcroft				\?|:
15599c0ca6f9SAndy Whitcroft			}x;
1560cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
156100df344fSAndy Whitcroft			my $off = 0;
15626c72ffaaSAndy Whitcroft
15636c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
15646c72ffaaSAndy Whitcroft
15650a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
15664a0df2efSAndy Whitcroft				$off += length($elements[$n]);
15674a0df2efSAndy Whitcroft
1568773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1569773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1570773647a0SAndy Whitcroft				my $cc = '';
1571773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1572773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1573773647a0SAndy Whitcroft				}
1574773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1575773647a0SAndy Whitcroft
15764a0df2efSAndy Whitcroft				my $a = '';
15774a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
15784a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1579cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
15804a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
15814a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1582773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
15834a0df2efSAndy Whitcroft
15840a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
15854a0df2efSAndy Whitcroft
15864a0df2efSAndy Whitcroft				my $c = '';
15870a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
15884a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
15894a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1590cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
15914a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
15924a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
159322f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
15944a0df2efSAndy Whitcroft				} else {
15954a0df2efSAndy Whitcroft					$c = 'E';
15960a920b5bSAndy Whitcroft				}
15970a920b5bSAndy Whitcroft
15984a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
15994a0df2efSAndy Whitcroft
16004a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
16014a0df2efSAndy Whitcroft
16026c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1603de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
16040a920b5bSAndy Whitcroft
160574048ed8SAndy Whitcroft				# Pull out the value of this operator.
16066c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
16070a920b5bSAndy Whitcroft
16081f65f947SAndy Whitcroft				# Get the full operator variant.
16091f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
16101f65f947SAndy Whitcroft
161113214adfSAndy Whitcroft				# Ignore operators passed as parameters.
161213214adfSAndy Whitcroft				if ($op_type ne 'V' &&
161313214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
161413214adfSAndy Whitcroft
1615cf655043SAndy Whitcroft#				# Ignore comments
1616cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
161713214adfSAndy Whitcroft
1618d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
161913214adfSAndy Whitcroft				} elsif ($op eq ';') {
1620cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1621cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1622773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1623d8aaf121SAndy Whitcroft					}
1624d8aaf121SAndy Whitcroft
1625d8aaf121SAndy Whitcroft				# // is a comment
1626d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
16270a920b5bSAndy Whitcroft
16281f65f947SAndy Whitcroft				# No spaces for:
16291f65f947SAndy Whitcroft				#   ->
16301f65f947SAndy Whitcroft				#   :   when part of a bitfield
16311f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
16324a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1633773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
16340a920b5bSAndy Whitcroft					}
16350a920b5bSAndy Whitcroft
16360a920b5bSAndy Whitcroft				# , must have a space on the right.
16370a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1638cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1639773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
16400a920b5bSAndy Whitcroft					}
16410a920b5bSAndy Whitcroft
16429c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
164374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
16449c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
16459c0ca6f9SAndy Whitcroft
16469c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
16479c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
16489c0ca6f9SAndy Whitcroft				# unary operator, or a cast
16499c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
165074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
16510d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1652cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1653773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
16540a920b5bSAndy Whitcroft					}
1655171ae1a4SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*const\b/) {
1656171ae1a4SAndy Whitcroft						# A unary '*' may be const
1657171ae1a4SAndy Whitcroft
1658171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1659773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
16600a920b5bSAndy Whitcroft					}
16610a920b5bSAndy Whitcroft
16620a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
16630a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1664773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1665773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
16660a920b5bSAndy Whitcroft					}
1667773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1668773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1669773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1670653d4876SAndy Whitcroft					}
1671773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1672773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1673773647a0SAndy Whitcroft					}
1674773647a0SAndy Whitcroft
16750a920b5bSAndy Whitcroft
16760a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
16779c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
16789c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
16799c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1680c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1681c2fdda0dSAndy Whitcroft					 $op eq '%')
16820a920b5bSAndy Whitcroft				{
1683773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1684de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1685de7d4f0eSAndy Whitcroft							$hereptr);
16860a920b5bSAndy Whitcroft					}
16870a920b5bSAndy Whitcroft
16881f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
16891f65f947SAndy Whitcroft				# terminating a case value or a label.
16901f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
16911f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
16921f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
16931f65f947SAndy Whitcroft					}
16941f65f947SAndy Whitcroft
16950a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1696cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
16971f65f947SAndy Whitcroft					my $ok = 0;
16981f65f947SAndy Whitcroft
169922f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
17001f65f947SAndy Whitcroft					if (($op eq '<' &&
17011f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
17021f65f947SAndy Whitcroft					    ($op eq '>' &&
17031f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
17041f65f947SAndy Whitcroft					{
17051f65f947SAndy Whitcroft					    	$ok = 1;
17061f65f947SAndy Whitcroft					}
17071f65f947SAndy Whitcroft
17081f65f947SAndy Whitcroft					# Ignore ?:
17091f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
17101f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
17111f65f947SAndy Whitcroft					    	$ok = 1;
17121f65f947SAndy Whitcroft					}
17131f65f947SAndy Whitcroft
17141f65f947SAndy Whitcroft					if ($ok == 0) {
1715773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
17160a920b5bSAndy Whitcroft					}
171722f2a2efSAndy Whitcroft				}
17184a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
17190a920b5bSAndy Whitcroft			}
17200a920b5bSAndy Whitcroft		}
17210a920b5bSAndy Whitcroft
1722f0a594c1SAndy Whitcroft# check for multiple assignments
1723f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
17246c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1725f0a594c1SAndy Whitcroft		}
1726f0a594c1SAndy Whitcroft
172722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
172822f2a2efSAndy Whitcroft## # continuation.
172922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
173022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
173122f2a2efSAndy Whitcroft##
173222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
173322f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
173422f2a2efSAndy Whitcroft## 			my $ln = $line;
173522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
173622f2a2efSAndy Whitcroft## 			}
173722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
173822f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
173922f2a2efSAndy Whitcroft## 			}
174022f2a2efSAndy Whitcroft## 		}
1741f0a594c1SAndy Whitcroft
17420a920b5bSAndy Whitcroft#need space before brace following if, while, etc
174322f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
174422f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1745773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1746de7d4f0eSAndy Whitcroft		}
1747de7d4f0eSAndy Whitcroft
1748de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1749de7d4f0eSAndy Whitcroft# on the line
1750de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1751773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
17520a920b5bSAndy Whitcroft		}
17530a920b5bSAndy Whitcroft
175422f2a2efSAndy Whitcroft# check spacing on square brackets
175522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1756773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
175722f2a2efSAndy Whitcroft		}
175822f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1759773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
176022f2a2efSAndy Whitcroft		}
176122f2a2efSAndy Whitcroft
1762c45dcabdSAndy Whitcroft# check spacing on parentheses
17639c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
17649c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1765773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
176622f2a2efSAndy Whitcroft		}
176713214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1768c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
1769c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
1770773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
177122f2a2efSAndy Whitcroft		}
177222f2a2efSAndy Whitcroft
17730a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
17744a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
17750a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1776de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
17770a920b5bSAndy Whitcroft		}
17780a920b5bSAndy Whitcroft
1779c45dcabdSAndy Whitcroft# Return is not a function.
1780c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1781c45dcabdSAndy Whitcroft			my $spacing = $1;
1782c45dcabdSAndy Whitcroft			my $value = $2;
1783c45dcabdSAndy Whitcroft
1784c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
1785fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
1786c45dcabdSAndy Whitcroft			while ($value =~ s/\([^\(\)]*\)/1/) {
1787c45dcabdSAndy Whitcroft			}
1788c45dcabdSAndy Whitcroft
1789c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1790c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1791c45dcabdSAndy Whitcroft
1792c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
1793c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
1794c45dcabdSAndy Whitcroft			}
1795c45dcabdSAndy Whitcroft		}
1796c45dcabdSAndy Whitcroft
17970a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
17984a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1799773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
18000a920b5bSAndy Whitcroft		}
18010a920b5bSAndy Whitcroft
1802f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
1803f5fe35ddSAndy Whitcroft# statements after the conditional.
180453210168SAndy Whitcroft		if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
1805171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
18068905a67cSAndy Whitcroft
18078905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1808c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
18098905a67cSAndy Whitcroft			}
18108905a67cSAndy Whitcroft
18118905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
18128905a67cSAndy Whitcroft			# conditional.
1813773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
18148905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
181513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
181653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
181753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
1818773647a0SAndy Whitcroft			{
18198905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
18208905a67cSAndy Whitcroft			}
18218905a67cSAndy Whitcroft		}
18228905a67cSAndy Whitcroft
1823f5fe35ddSAndy Whitcroft# Check relative indent for conditionals and blocks.
1824f5fe35ddSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1825f5fe35ddSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
1826f5fe35ddSAndy Whitcroft
1827f5fe35ddSAndy Whitcroft			substr($s, 0, length($c), '');
1828f5fe35ddSAndy Whitcroft
1829f5fe35ddSAndy Whitcroft			# Make sure we remove the line prefixes as we have
1830f5fe35ddSAndy Whitcroft			# none on the first line, and are going to readd them
1831f5fe35ddSAndy Whitcroft			# where necessary.
1832f5fe35ddSAndy Whitcroft			$s =~ s/\n./\n/gs;
1833f5fe35ddSAndy Whitcroft
1834f5fe35ddSAndy Whitcroft			# We want to check the first line inside the block
1835f5fe35ddSAndy Whitcroft			# starting at the end of the conditional, so remove:
1836f5fe35ddSAndy Whitcroft			#  1) any blank line termination
1837f5fe35ddSAndy Whitcroft			#  2) any opening brace { on end of the line
1838f5fe35ddSAndy Whitcroft			#  3) any do (...) {
1839f5fe35ddSAndy Whitcroft			my $continuation = 0;
1840f5fe35ddSAndy Whitcroft			my $check = 0;
1841f5fe35ddSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
1842f5fe35ddSAndy Whitcroft			$s =~ s/^\s*{//;
1843f5fe35ddSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
1844f5fe35ddSAndy Whitcroft				$continuation = 1;
1845f5fe35ddSAndy Whitcroft			}
1846f5fe35ddSAndy Whitcroft			if ($s =~ s/^\s*\n//) {
1847f5fe35ddSAndy Whitcroft				$check = 1;
1848f5fe35ddSAndy Whitcroft			}
1849f5fe35ddSAndy Whitcroft
1850f5fe35ddSAndy Whitcroft			# Also ignore a loop construct at the end of a
1851f5fe35ddSAndy Whitcroft			# preprocessor statement.
1852f5fe35ddSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
1853f5fe35ddSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
1854f5fe35ddSAndy Whitcroft				$check = 0;
1855f5fe35ddSAndy Whitcroft			}
1856f5fe35ddSAndy Whitcroft
1857f5fe35ddSAndy Whitcroft			# Ignore the current line if its is a preprocessor
1858f5fe35ddSAndy Whitcroft			# line.
1859f5fe35ddSAndy Whitcroft			if ($s =~ /^\s*#\s*/) {
1860f5fe35ddSAndy Whitcroft				$check = 0;
1861f5fe35ddSAndy Whitcroft			}
1862f5fe35ddSAndy Whitcroft
186314b111c1SAndy Whitcroft			# Ignore the current line if it is label.
186414b111c1SAndy Whitcroft			if ($s =~ /^\s*$Ident\s*:/) {
186514b111c1SAndy Whitcroft				$check = 0;
186614b111c1SAndy Whitcroft			}
186714b111c1SAndy Whitcroft
1868f5fe35ddSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
1869f5fe35ddSAndy Whitcroft
1870f5fe35ddSAndy Whitcroft			##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n";
1871f5fe35ddSAndy Whitcroft
1872f5fe35ddSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
1873f5fe35ddSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
1874f5fe35ddSAndy Whitcroft				WARN("suspect code indent for conditional statements\n" . $herecurr);
1875f5fe35ddSAndy Whitcroft			}
1876f5fe35ddSAndy Whitcroft		}
1877f5fe35ddSAndy Whitcroft
187813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
187913214adfSAndy Whitcroft		if ($line =~ /
188013214adfSAndy Whitcroft			(?:
188113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
188213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
188313214adfSAndy Whitcroft				(?:\&\&|\|\|)
188413214adfSAndy Whitcroft			|
188513214adfSAndy Whitcroft				(?:\&\&|\|\|)
188613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
188713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
188813214adfSAndy Whitcroft			)/x)
188913214adfSAndy Whitcroft		{
189013214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
189113214adfSAndy Whitcroft		}
189213214adfSAndy Whitcroft
18938905a67cSAndy Whitcroft# if and else should not have general statements after it
189413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
189513214adfSAndy Whitcroft			my $s = $1;
189613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
189713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
18988905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
18990a920b5bSAndy Whitcroft			}
190013214adfSAndy Whitcroft		}
19010a920b5bSAndy Whitcroft
19020a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
19030a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
19040a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
19050a920b5bSAndy Whitcroft						$previndent == $indent) {
1906de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
19070a920b5bSAndy Whitcroft		}
19080a920b5bSAndy Whitcroft
1909c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1910c2fdda0dSAndy Whitcroft						$previndent == $indent) {
1911c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1912c2fdda0dSAndy Whitcroft
1913c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
1914c2fdda0dSAndy Whitcroft			# conditional.
1915773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
1916c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
1917c2fdda0dSAndy Whitcroft
1918c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
1919c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
1920c2fdda0dSAndy Whitcroft			}
1921c2fdda0dSAndy Whitcroft		}
1922c2fdda0dSAndy Whitcroft
19230a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
19240a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
19250a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
19260a920b5bSAndy Whitcroft#		    print "$herecurr";
19270a920b5bSAndy Whitcroft#		    $clean = 0;
19280a920b5bSAndy Whitcroft#		}
19290a920b5bSAndy Whitcroft
19300a920b5bSAndy Whitcroft#no spaces allowed after \ in define
1931c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
1932de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
19330a920b5bSAndy Whitcroft		}
19340a920b5bSAndy Whitcroft
1935653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1936c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
1937c45dcabdSAndy Whitcroft			my $checkfile = "include/linux/$1.h";
1938c45dcabdSAndy Whitcroft			if (-f "$root/$checkfile" && $realfile ne $checkfile &&
1939c45dcabdSAndy Whitcroft			    $1 ne 'irq')
1940c45dcabdSAndy Whitcroft			{
1941773647a0SAndy Whitcroft				WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1942de7d4f0eSAndy Whitcroft					$herecurr);
19430a920b5bSAndy Whitcroft			}
19440a920b5bSAndy Whitcroft		}
19450a920b5bSAndy Whitcroft
1946653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
1947653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
1948cf655043SAndy Whitcroft# in a known good container
1949b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
1950b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
1951d8aaf121SAndy Whitcroft			my $ln = $linenr;
1952d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
1953c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
1954c45dcabdSAndy Whitcroft			my $ctx = '';
1955653d4876SAndy Whitcroft
1956c45dcabdSAndy Whitcroft			my $args = defined($1);
1957de7d4f0eSAndy Whitcroft
1958c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
1959c45dcabdSAndy Whitcroft			# search to that.
1960c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
1961c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
1962c45dcabdSAndy Whitcroft			{
1963c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
1964a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
1965c45dcabdSAndy Whitcroft				$ln++;
1966de7d4f0eSAndy Whitcroft			}
1967c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
1968d8aaf121SAndy Whitcroft
1969c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
1970c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
1971c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
1972a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
1973c45dcabdSAndy Whitcroft
1974c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
1975c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
1976c45dcabdSAndy Whitcroft			$rest = '';
1977a3bb97a7SAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /(?:^|\\)\s*$/)) {
1978a3bb97a7SAndy Whitcroft				#print "ADDING $off <" . substr($lines[$ln - 1], $off) . ">\n";
1979a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
1980c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
1981c45dcabdSAndy Whitcroft					$cnt--;
1982a3bb97a7SAndy Whitcroft				}
1983a3bb97a7SAndy Whitcroft				$ln++;
1984c45dcabdSAndy Whitcroft				$off = 0;
1985c45dcabdSAndy Whitcroft			}
1986c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
1987c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
1988c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
1989c45dcabdSAndy Whitcroft
1990c45dcabdSAndy Whitcroft			# Clean up the original statement.
1991c45dcabdSAndy Whitcroft			if ($args) {
1992c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
1993d8aaf121SAndy Whitcroft			} else {
1994c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
1995c45dcabdSAndy Whitcroft			}
1996292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
1997c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
1998c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
1999c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2000c45dcabdSAndy Whitcroft
2001c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2002c45dcabdSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/) {
2003c45dcabdSAndy Whitcroft			}
2004c45dcabdSAndy Whitcroft			while ($dstat =~ s/\{[^\{\}]*\}/1/) {
2005c45dcabdSAndy Whitcroft			}
2006c45dcabdSAndy Whitcroft
2007c45dcabdSAndy Whitcroft			my $exceptions = qr{
2008c45dcabdSAndy Whitcroft				$Declare|
2009c45dcabdSAndy Whitcroft				module_param_named|
2010c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2011c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2012c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2013c45dcabdSAndy Whitcroft				__typeof__\(
2014c45dcabdSAndy Whitcroft			}x;
2015a3bb97a7SAndy Whitcroft			#print "REST<$rest>\n";
2016c45dcabdSAndy Whitcroft			if ($rest ne '') {
2017c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2018c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2019c45dcabdSAndy Whitcroft				{
2020c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2021c45dcabdSAndy Whitcroft				}
2022c45dcabdSAndy Whitcroft
2023c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2024c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2025c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2026c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2027c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2028c45dcabdSAndy Whitcroft				{
2029de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2030d8aaf121SAndy Whitcroft				}
20310a920b5bSAndy Whitcroft			}
2032653d4876SAndy Whitcroft		}
20330a920b5bSAndy Whitcroft
2034f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
203513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
203613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2037cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
203813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2039cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2040cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
204113214adfSAndy Whitcroft				my $allowed = 0;
204213214adfSAndy Whitcroft				my $seen = 0;
2043773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2044cf655043SAndy Whitcroft				my $ln = $linenr - 1;
204513214adfSAndy Whitcroft				for my $chunk (@chunks) {
204613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
204713214adfSAndy Whitcroft
2048773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2049773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2050773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2051773647a0SAndy Whitcroft
2052773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2053773647a0SAndy Whitcroft
2054773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2055773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2056773647a0SAndy Whitcroft
2057773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2058cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2059cf655043SAndy Whitcroft
2060773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
206113214adfSAndy Whitcroft
206213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
206313214adfSAndy Whitcroft
2064cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2065cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2066cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
206713214adfSAndy Whitcroft						$allowed = 1;
206813214adfSAndy Whitcroft					}
206913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2070cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
207113214adfSAndy Whitcroft						$allowed = 1;
207213214adfSAndy Whitcroft					}
2073cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2074cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
207513214adfSAndy Whitcroft						$allowed = 1;
207613214adfSAndy Whitcroft					}
207713214adfSAndy Whitcroft				}
207813214adfSAndy Whitcroft				if ($seen && !$allowed) {
2079cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
208013214adfSAndy Whitcroft				}
208113214adfSAndy Whitcroft			}
208213214adfSAndy Whitcroft		}
2083773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
208413214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2085cf655043SAndy Whitcroft			my $allowed = 0;
2086f0a594c1SAndy Whitcroft
2087cf655043SAndy Whitcroft			# Check the pre-context.
2088cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2089cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2090cf655043SAndy Whitcroft				$allowed = 1;
2091f0a594c1SAndy Whitcroft			}
2092773647a0SAndy Whitcroft
2093773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2094773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2095773647a0SAndy Whitcroft
2096cf655043SAndy Whitcroft			# Check the condition.
2097cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2098773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2099cf655043SAndy Whitcroft			if (defined $cond) {
2100773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2101cf655043SAndy Whitcroft			}
2102cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2103cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2104cf655043SAndy Whitcroft				$allowed = 1;
2105cf655043SAndy Whitcroft			}
2106cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2107cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2108cf655043SAndy Whitcroft				$allowed = 1;
2109cf655043SAndy Whitcroft			}
2110cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2111cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2112cf655043SAndy Whitcroft				$allowed = 1;
2113cf655043SAndy Whitcroft			}
2114cf655043SAndy Whitcroft			# Check the post-context.
2115cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2116cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2117cf655043SAndy Whitcroft				if (defined $cond) {
2118773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2119cf655043SAndy Whitcroft				}
2120cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2121cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2122cf655043SAndy Whitcroft					$allowed = 1;
2123cf655043SAndy Whitcroft				}
2124cf655043SAndy Whitcroft			}
2125cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2126cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2127cf655043SAndy Whitcroft				my $end = $linenr + statement_rawlines($block) - 1;
2128cf655043SAndy Whitcroft
2129cf655043SAndy Whitcroft				for (my $ln = $linenr - 1; $ln < $end; $ln++) {
2130cf655043SAndy Whitcroft					$herectx .= $rawlines[$ln] . "\n";;
2131cf655043SAndy Whitcroft				}
2132cf655043SAndy Whitcroft
2133cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2134f0a594c1SAndy Whitcroft			}
2135f0a594c1SAndy Whitcroft		}
2136f0a594c1SAndy Whitcroft
2137653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
21384a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2139c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2140de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
21410a920b5bSAndy Whitcroft			}
21420a920b5bSAndy Whitcroft		}
21430a920b5bSAndy Whitcroft
21444a0df2efSAndy Whitcroft# don't use deprecated functions
21454a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
214600df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2147de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
21484a0df2efSAndy Whitcroft			}
21494a0df2efSAndy Whitcroft		}
21504a0df2efSAndy Whitcroft
21514a0df2efSAndy Whitcroft# no volatiles please
21526c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
21536c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2154de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
21554a0df2efSAndy Whitcroft		}
21564a0df2efSAndy Whitcroft
21579c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
21589c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
21599c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
21609c0ca6f9SAndy Whitcroft		}
21619c0ca6f9SAndy Whitcroft
216200df344fSAndy Whitcroft# warn about #if 0
2163c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2164de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2165de7d4f0eSAndy Whitcroft				$herecurr);
21664a0df2efSAndy Whitcroft		}
21674a0df2efSAndy Whitcroft
2168f0a594c1SAndy Whitcroft# check for needless kfree() checks
2169f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2170f0a594c1SAndy Whitcroft			my $expr = $1;
2171f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
21723c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2173f0a594c1SAndy Whitcroft			}
2174f0a594c1SAndy Whitcroft		}
21754c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
21764c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
21774c432a8fSGreg Kroah-Hartman			my $expr = $1;
21784c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
21794c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
21804c432a8fSGreg Kroah-Hartman			}
21814c432a8fSGreg Kroah-Hartman		}
2182f0a594c1SAndy Whitcroft
218300df344fSAndy Whitcroft# warn about #ifdefs in C files
2184c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
218500df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
218600df344fSAndy Whitcroft#			print "$herecurr";
218700df344fSAndy Whitcroft#			$clean = 0;
218800df344fSAndy Whitcroft#		}
218900df344fSAndy Whitcroft
219022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2191c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
219222f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
219322f2a2efSAndy Whitcroft		}
219422f2a2efSAndy Whitcroft
21954a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2196171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2197171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
21984a0df2efSAndy Whitcroft			my $which = $1;
21994a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2200de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
22014a0df2efSAndy Whitcroft			}
22024a0df2efSAndy Whitcroft		}
22034a0df2efSAndy Whitcroft# check for memory barriers without a comment.
22044a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
22054a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2206de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
22074a0df2efSAndy Whitcroft			}
22084a0df2efSAndy Whitcroft		}
22094a0df2efSAndy Whitcroft# check of hardware specific defines
2210c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2211de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
22120a920b5bSAndy Whitcroft		}
2213653d4876SAndy Whitcroft
2214de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2215de7d4f0eSAndy Whitcroft# storage class and type.
22169c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
22179c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2218de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2219de7d4f0eSAndy Whitcroft		}
2220de7d4f0eSAndy Whitcroft
22218905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
22228905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
22238905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
22248905a67cSAndy Whitcroft		}
22258905a67cSAndy Whitcroft
2226de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2227171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2228c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2229171ae1a4SAndy Whitcroft		{
2230c45dcabdSAndy Whitcroft			my $function_name = $1;
2231c45dcabdSAndy Whitcroft			my $paren_space = $2;
2232171ae1a4SAndy Whitcroft
2233171ae1a4SAndy Whitcroft			my $s = $stat;
2234171ae1a4SAndy Whitcroft			if (defined $cond) {
2235171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2236171ae1a4SAndy Whitcroft			}
2237c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2238c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2239c45dcabdSAndy Whitcroft			{
2240de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2241de7d4f0eSAndy Whitcroft			}
2242de7d4f0eSAndy Whitcroft
2243171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2244171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2245171ae1a4SAndy Whitcroft			}
22469c9ba34eSAndy Whitcroft
22479c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
22489c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
22499c9ba34eSAndy Whitcroft		{
22509c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2251171ae1a4SAndy Whitcroft		}
2252171ae1a4SAndy Whitcroft
2253de7d4f0eSAndy Whitcroft# checks for new __setup's
2254de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2255de7d4f0eSAndy Whitcroft			my $name = $1;
2256de7d4f0eSAndy Whitcroft
2257de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2258de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2259de7d4f0eSAndy Whitcroft			}
2260653d4876SAndy Whitcroft		}
22619c0ca6f9SAndy Whitcroft
22629c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
22639c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
22649c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
22659c0ca6f9SAndy Whitcroft		}
226613214adfSAndy Whitcroft
226713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
226813214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
226913214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
227013214adfSAndy Whitcroft		}
2271773647a0SAndy Whitcroft
2272773647a0SAndy Whitcroft# check for semaphores used as mutexes
2273171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2274773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2275773647a0SAndy Whitcroft		}
2276773647a0SAndy Whitcroft# check for semaphores used as mutexes
2277171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2278773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2279773647a0SAndy Whitcroft		}
2280773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2281773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2282773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2283773647a0SAndy Whitcroft		}
2284f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2285f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2286f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2287f3db6639SMichael Ellerman		}
2288773647a0SAndy Whitcroft
2289773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2290773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2291773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2292c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2293c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2294171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2295171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2296171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2297773647a0SAndy Whitcroft		{
2298773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2299773647a0SAndy Whitcroft		}
23009c9ba34eSAndy Whitcroft
23019c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
23029c9ba34eSAndy Whitcroft		my $string;
23039c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
23049c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
23059c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
23069c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
23079c9ba34eSAndy Whitcroft				last;
23089c9ba34eSAndy Whitcroft			}
23099c9ba34eSAndy Whitcroft		}
231013214adfSAndy Whitcroft	}
231113214adfSAndy Whitcroft
231213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
231313214adfSAndy Whitcroft	# so just keep quiet.
231413214adfSAndy Whitcroft	if ($#rawlines == -1) {
231513214adfSAndy Whitcroft		exit(0);
23160a920b5bSAndy Whitcroft	}
23170a920b5bSAndy Whitcroft
23188905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
23198905a67cSAndy Whitcroft	# things that appear to be patches.
23208905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
23218905a67cSAndy Whitcroft		exit(0);
23228905a67cSAndy Whitcroft	}
23238905a67cSAndy Whitcroft
23248905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
23258905a67cSAndy Whitcroft	# just keep quiet.
23268905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
23278905a67cSAndy Whitcroft		exit(0);
23288905a67cSAndy Whitcroft	}
23298905a67cSAndy Whitcroft
23308905a67cSAndy Whitcroft	if (!$is_patch) {
2331de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
23320a920b5bSAndy Whitcroft	}
23330a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2334de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
23350a920b5bSAndy Whitcroft	}
23360a920b5bSAndy Whitcroft
2337f0a594c1SAndy Whitcroft	print report_dump();
233813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
233913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
23406c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
23416c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
23426c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
23438905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
23446c72ffaaSAndy Whitcroft	}
23458905a67cSAndy Whitcroft
23460a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2347c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
23480a920b5bSAndy Whitcroft	}
23490a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2350c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
23510a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
23520a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
23530a920b5bSAndy Whitcroft	}
235413214adfSAndy Whitcroft
23550a920b5bSAndy Whitcroft	return $clean;
23560a920b5bSAndy Whitcroft}
2357