xref: /linux-6.15/scripts/checkpatch.pl (revision 9c9ba34e)
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
12*9c9ba34eSAndy Whitcroftmy $V = '0.18';
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;
20653d4876SAndy Whitcroftmy $tst_type = 0;
21773647a0SAndy Whitcroftmy $tst_only;
226c72ffaaSAndy Whitcroftmy $emacs = 0;
238905a67cSAndy Whitcroftmy $terse = 0;
246c72ffaaSAndy Whitcroftmy $file = 0;
256c72ffaaSAndy Whitcroftmy $check = 0;
268905a67cSAndy Whitcroftmy $summary = 1;
278905a67cSAndy Whitcroftmy $mailback = 0;
2813214adfSAndy Whitcroftmy $summary_file = 0;
296c72ffaaSAndy Whitcroftmy $root;
30c2fdda0dSAndy Whitcroftmy %debug;
310a920b5bSAndy WhitcroftGetOptions(
326c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
330a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
340a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
350a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
366c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
378905a67cSAndy Whitcroft	'terse!'	=> \$terse,
386c72ffaaSAndy Whitcroft	'file!'		=> \$file,
396c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
406c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
416c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
428905a67cSAndy Whitcroft	'summary!'	=> \$summary,
438905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
4413214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
4513214adfSAndy Whitcroft
46c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
4713214adfSAndy Whitcroft	'test-type!'	=> \$tst_type,
48773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
490a920b5bSAndy Whitcroft) or exit;
500a920b5bSAndy Whitcroft
510a920b5bSAndy Whitcroftmy $exit = 0;
520a920b5bSAndy Whitcroft
530a920b5bSAndy Whitcroftif ($#ARGV < 0) {
5400df344fSAndy Whitcroft	print "usage: $P [options] patchfile\n";
550a920b5bSAndy Whitcroft	print "version: $V\n";
560a920b5bSAndy Whitcroft	print "options: -q               => quiet\n";
570a920b5bSAndy Whitcroft	print "         --no-tree        => run without a kernel tree\n";
588905a67cSAndy Whitcroft	print "         --terse          => one line per report\n";
596c72ffaaSAndy Whitcroft	print "         --emacs          => emacs compile window format\n";
606c72ffaaSAndy Whitcroft	print "         --file           => check a source file\n";
616c72ffaaSAndy Whitcroft	print "         --strict         => enable more subjective tests\n";
626c72ffaaSAndy Whitcroft	print "         --root           => path to the kernel tree root\n";
6313214adfSAndy Whitcroft	print "         --no-summary     => suppress the per-file summary\n";
6413214adfSAndy Whitcroft	print "         --summary-file   => include the filename in summary\n";
650a920b5bSAndy Whitcroft	exit(1);
660a920b5bSAndy Whitcroft}
670a920b5bSAndy Whitcroft
68c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
69c2fdda0dSAndy Whitcroftmy $dbg_possible = 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;
1186c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1196c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1206c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1216c72ffaaSAndy Whitcroft
1226c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1236c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
1246c72ffaaSAndy Whitcroftour $Operators	= qr{
1256c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1266c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
127c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1286c72ffaaSAndy Whitcroft		  }x;
1296c72ffaaSAndy Whitcroft
1308905a67cSAndy Whitcroftour $NonptrType;
1318905a67cSAndy Whitcroftour $Type;
1328905a67cSAndy Whitcroftour $Declare;
1338905a67cSAndy Whitcroft
134171ae1a4SAndy Whitcroftour $UTF8	= qr {
135171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
136171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
137171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
138171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
139171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
140171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
141171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
142171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
143171ae1a4SAndy Whitcroft}x;
144171ae1a4SAndy Whitcroft
1458905a67cSAndy Whitcroftour @typeList = (
1468905a67cSAndy Whitcroft	qr{void},
1478905a67cSAndy Whitcroft	qr{char},
1488905a67cSAndy Whitcroft	qr{short},
1498905a67cSAndy Whitcroft	qr{int},
1508905a67cSAndy Whitcroft	qr{long},
1518905a67cSAndy Whitcroft	qr{unsigned},
1528905a67cSAndy Whitcroft	qr{float},
1538905a67cSAndy Whitcroft	qr{double},
1548905a67cSAndy Whitcroft	qr{bool},
1558905a67cSAndy Whitcroft	qr{long\s+int},
1568905a67cSAndy Whitcroft	qr{long\s+long},
1578905a67cSAndy Whitcroft	qr{long\s+long\s+int},
1588905a67cSAndy Whitcroft	qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
1598905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1608905a67cSAndy Whitcroft	qr{union\s+$Ident},
1618905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1628905a67cSAndy Whitcroft	qr{${Ident}_t},
1638905a67cSAndy Whitcroft	qr{${Ident}_handler},
1648905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1658905a67cSAndy Whitcroft);
1668905a67cSAndy Whitcroft
1678905a67cSAndy Whitcroftsub build_types {
1688905a67cSAndy Whitcroft	my $all = "(?:  \n" . join("|\n  ", @typeList) . "\n)";
1698905a67cSAndy Whitcroft	$NonptrType	= qr{
1708905a67cSAndy Whitcroft			\b
1718905a67cSAndy Whitcroft			(?:const\s+)?
1728905a67cSAndy Whitcroft			(?:unsigned\s+)?
173cf655043SAndy Whitcroft			(?:
174cf655043SAndy Whitcroft				$all|
175cf655043SAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
176cf655043SAndy Whitcroft			)
1778905a67cSAndy Whitcroft			(?:\s+$Sparse|\s+const)*
1788905a67cSAndy Whitcroft			\b
1798905a67cSAndy Whitcroft		  }x;
1808905a67cSAndy Whitcroft	$Type	= qr{
1818905a67cSAndy Whitcroft			\b$NonptrType\b
1828905a67cSAndy Whitcroft			(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
183c2fdda0dSAndy Whitcroft			(?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
1848905a67cSAndy Whitcroft		  }x;
1858905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
1868905a67cSAndy Whitcroft}
1878905a67cSAndy Whitcroftbuild_types();
1886c72ffaaSAndy Whitcroft
1896c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
1900a920b5bSAndy Whitcroft
1914a0df2efSAndy Whitcroftmy @dep_includes = ();
1924a0df2efSAndy Whitcroftmy @dep_functions = ();
1936c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
1946c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
1956c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
1966c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
1970a920b5bSAndy Whitcroft	while (<REMOVE>) {
198f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
199f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
200f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2014a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2024a0df2efSAndy Whitcroft
203f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
204f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
205f0a594c1SAndy Whitcroft				}
2064a0df2efSAndy Whitcroft			}
2070a920b5bSAndy Whitcroft		}
2080a920b5bSAndy Whitcroft	}
2090a920b5bSAndy Whitcroft}
2100a920b5bSAndy Whitcroft
21100df344fSAndy Whitcroftmy @rawlines = ();
212c2fdda0dSAndy Whitcroftmy @lines = ();
213c2fdda0dSAndy Whitcroftmy $vname;
2146c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2156c72ffaaSAndy Whitcroft	if ($file) {
2166c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2176c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2186c72ffaaSAndy Whitcroft	} else {
2196c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2206c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2216c72ffaaSAndy Whitcroft	}
222c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
223c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
224c2fdda0dSAndy Whitcroft	} else {
225c2fdda0dSAndy Whitcroft		$vname = $filename;
226c2fdda0dSAndy Whitcroft	}
2276c72ffaaSAndy Whitcroft	while (<FILE>) {
2280a920b5bSAndy Whitcroft		chomp;
22900df344fSAndy Whitcroft		push(@rawlines, $_);
2306c72ffaaSAndy Whitcroft	}
2316c72ffaaSAndy Whitcroft	close(FILE);
232c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2330a920b5bSAndy Whitcroft		$exit = 1;
2340a920b5bSAndy Whitcroft	}
23500df344fSAndy Whitcroft	@rawlines = ();
23613214adfSAndy Whitcroft	@lines = ();
2370a920b5bSAndy Whitcroft}
2380a920b5bSAndy Whitcroft
2390a920b5bSAndy Whitcroftexit($exit);
2400a920b5bSAndy Whitcroft
2410a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2426c72ffaaSAndy Whitcroft	my ($root) = @_;
2436c72ffaaSAndy Whitcroft
2446c72ffaaSAndy Whitcroft	my @tree_check = (
2456c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2466c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2476c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2486c72ffaaSAndy Whitcroft	);
2496c72ffaaSAndy Whitcroft
2506c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2516c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2520a920b5bSAndy Whitcroft			return 0;
2530a920b5bSAndy Whitcroft		}
2546c72ffaaSAndy Whitcroft	}
2556c72ffaaSAndy Whitcroft	return 1;
2566c72ffaaSAndy Whitcroft}
2570a920b5bSAndy Whitcroft
2580a920b5bSAndy Whitcroftsub expand_tabs {
2590a920b5bSAndy Whitcroft	my ($str) = @_;
2600a920b5bSAndy Whitcroft
2610a920b5bSAndy Whitcroft	my $res = '';
2620a920b5bSAndy Whitcroft	my $n = 0;
2630a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2640a920b5bSAndy Whitcroft		if ($c eq "\t") {
2650a920b5bSAndy Whitcroft			$res .= ' ';
2660a920b5bSAndy Whitcroft			$n++;
2670a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2680a920b5bSAndy Whitcroft				$res .= ' ';
2690a920b5bSAndy Whitcroft			}
2700a920b5bSAndy Whitcroft			next;
2710a920b5bSAndy Whitcroft		}
2720a920b5bSAndy Whitcroft		$res .= $c;
2730a920b5bSAndy Whitcroft		$n++;
2740a920b5bSAndy Whitcroft	}
2750a920b5bSAndy Whitcroft
2760a920b5bSAndy Whitcroft	return $res;
2770a920b5bSAndy Whitcroft}
2786c72ffaaSAndy Whitcroftsub copy_spacing {
279773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
2806c72ffaaSAndy Whitcroft	return $res;
2816c72ffaaSAndy Whitcroft}
2820a920b5bSAndy Whitcroft
2834a0df2efSAndy Whitcroftsub line_stats {
2844a0df2efSAndy Whitcroft	my ($line) = @_;
2854a0df2efSAndy Whitcroft
2864a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
2874a0df2efSAndy Whitcroft	$line =~ s/^.//;
2884a0df2efSAndy Whitcroft	$line = expand_tabs($line);
2894a0df2efSAndy Whitcroft
2904a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
2914a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
2924a0df2efSAndy Whitcroft
2934a0df2efSAndy Whitcroft	return (length($line), length($white));
2944a0df2efSAndy Whitcroft}
2954a0df2efSAndy Whitcroft
296773647a0SAndy Whitcroftmy $sanitise_quote = '';
297773647a0SAndy Whitcroft
298773647a0SAndy Whitcroftsub sanitise_line_reset {
299773647a0SAndy Whitcroft	my ($in_comment) = @_;
300773647a0SAndy Whitcroft
301773647a0SAndy Whitcroft	if ($in_comment) {
302773647a0SAndy Whitcroft		$sanitise_quote = '*/';
303773647a0SAndy Whitcroft	} else {
304773647a0SAndy Whitcroft		$sanitise_quote = '';
305773647a0SAndy Whitcroft	}
306773647a0SAndy Whitcroft}
30700df344fSAndy Whitcroftsub sanitise_line {
30800df344fSAndy Whitcroft	my ($line) = @_;
30900df344fSAndy Whitcroft
31000df344fSAndy Whitcroft	my $res = '';
31100df344fSAndy Whitcroft	my $l = '';
31200df344fSAndy Whitcroft
313c2fdda0dSAndy Whitcroft	my $qlen = 0;
314773647a0SAndy Whitcroft	my $off = 0;
315773647a0SAndy Whitcroft	my $c;
31600df344fSAndy Whitcroft
317773647a0SAndy Whitcroft	# Always copy over the diff marker.
318773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
319773647a0SAndy Whitcroft
320773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
321773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
322773647a0SAndy Whitcroft
323773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
324773647a0SAndy Whitcroft		# and end, all to $;.
325773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
326773647a0SAndy Whitcroft			$sanitise_quote = '*/';
327773647a0SAndy Whitcroft
328773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
329773647a0SAndy Whitcroft			$off++;
33000df344fSAndy Whitcroft			next;
331773647a0SAndy Whitcroft		}
332773647a0SAndy Whitcroft		if (substr($line, $off, 2) eq $sanitise_quote) {
333773647a0SAndy Whitcroft			$sanitise_quote = '';
334773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
335773647a0SAndy Whitcroft			$off++;
336773647a0SAndy Whitcroft			next;
337773647a0SAndy Whitcroft		}
338773647a0SAndy Whitcroft
339773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
340773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
341773647a0SAndy Whitcroft		    $c eq "\\") {
342773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
343773647a0SAndy Whitcroft			$off++;
344773647a0SAndy Whitcroft			next;
345773647a0SAndy Whitcroft		}
346773647a0SAndy Whitcroft		# Regular quotes.
347773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
348773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
349773647a0SAndy Whitcroft				$sanitise_quote = $c;
350773647a0SAndy Whitcroft
351773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
352773647a0SAndy Whitcroft				next;
353773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
354773647a0SAndy Whitcroft				$sanitise_quote = '';
35500df344fSAndy Whitcroft			}
35600df344fSAndy Whitcroft		}
357773647a0SAndy Whitcroft
358773647a0SAndy Whitcroft		#print "SQ:$sanitise_quote\n";
359773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
360773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
361773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
362773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
36300df344fSAndy Whitcroft		} else {
364773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
36500df344fSAndy Whitcroft		}
366c2fdda0dSAndy Whitcroft	}
367c2fdda0dSAndy Whitcroft
368c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
369c2fdda0dSAndy Whitcroft	if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
370c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
371c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
372c2fdda0dSAndy Whitcroft
373c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
374c2fdda0dSAndy Whitcroft	} elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
375c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
376c2fdda0dSAndy Whitcroft		$res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
377c2fdda0dSAndy Whitcroft	}
378c2fdda0dSAndy Whitcroft
37900df344fSAndy Whitcroft	return $res;
38000df344fSAndy Whitcroft}
38100df344fSAndy Whitcroft
3828905a67cSAndy Whitcroftsub ctx_statement_block {
3838905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3848905a67cSAndy Whitcroft	my $line = $linenr - 1;
3858905a67cSAndy Whitcroft	my $blk = '';
3868905a67cSAndy Whitcroft	my $soff = $off;
3878905a67cSAndy Whitcroft	my $coff = $off - 1;
388773647a0SAndy Whitcroft	my $coff_set = 0;
3898905a67cSAndy Whitcroft
39013214adfSAndy Whitcroft	my $loff = 0;
39113214adfSAndy Whitcroft
3928905a67cSAndy Whitcroft	my $type = '';
3938905a67cSAndy Whitcroft	my $level = 0;
394cf655043SAndy Whitcroft	my $p;
3958905a67cSAndy Whitcroft	my $c;
3968905a67cSAndy Whitcroft	my $len = 0;
39713214adfSAndy Whitcroft
39813214adfSAndy Whitcroft	my $remainder;
3998905a67cSAndy Whitcroft	while (1) {
400773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4018905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4028905a67cSAndy Whitcroft		# context.
4038905a67cSAndy Whitcroft		if ($off >= $len) {
4048905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
405c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4068905a67cSAndy Whitcroft				$remain--;
40713214adfSAndy Whitcroft				$loff = $len;
408c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4098905a67cSAndy Whitcroft				$len = length($blk);
4108905a67cSAndy Whitcroft				$line++;
4118905a67cSAndy Whitcroft				last;
4128905a67cSAndy Whitcroft			}
4138905a67cSAndy Whitcroft			# Bail if there is no further context.
4148905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
41513214adfSAndy Whitcroft			if ($off >= $len) {
4168905a67cSAndy Whitcroft				last;
4178905a67cSAndy Whitcroft			}
4188905a67cSAndy Whitcroft		}
419cf655043SAndy Whitcroft		$p = $c;
4208905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
42113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4228905a67cSAndy Whitcroft
423773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4248905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4258905a67cSAndy Whitcroft		# outermost level.
4268905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4278905a67cSAndy Whitcroft			last;
4288905a67cSAndy Whitcroft		}
4298905a67cSAndy Whitcroft
43013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
431773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
432773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
433773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
434773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
435773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
436773647a0SAndy Whitcroft			$coff_set = 1;
437773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
438773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
43913214adfSAndy Whitcroft		}
44013214adfSAndy Whitcroft
4418905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4428905a67cSAndy Whitcroft			$level++;
4438905a67cSAndy Whitcroft			$type = '(';
4448905a67cSAndy Whitcroft		}
4458905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4468905a67cSAndy Whitcroft			$level--;
4478905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4488905a67cSAndy Whitcroft
4498905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4508905a67cSAndy Whitcroft				$coff = $off;
451773647a0SAndy Whitcroft				$coff_set = 1;
452773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4538905a67cSAndy Whitcroft			}
4548905a67cSAndy Whitcroft		}
4558905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4568905a67cSAndy Whitcroft			$level++;
4578905a67cSAndy Whitcroft			$type = '{';
4588905a67cSAndy Whitcroft		}
4598905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4608905a67cSAndy Whitcroft			$level--;
4618905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4628905a67cSAndy Whitcroft
4638905a67cSAndy Whitcroft			if ($level == 0) {
4648905a67cSAndy Whitcroft				last;
4658905a67cSAndy Whitcroft			}
4668905a67cSAndy Whitcroft		}
4678905a67cSAndy Whitcroft		$off++;
4688905a67cSAndy Whitcroft	}
46913214adfSAndy Whitcroft	if ($off == $len) {
47013214adfSAndy Whitcroft		$line++;
47113214adfSAndy Whitcroft		$remain--;
47213214adfSAndy Whitcroft	}
4738905a67cSAndy Whitcroft
4748905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4758905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4768905a67cSAndy Whitcroft
4778905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4788905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4798905a67cSAndy Whitcroft
480773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
48113214adfSAndy Whitcroft
48213214adfSAndy Whitcroft	return ($statement, $condition,
48313214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
48413214adfSAndy Whitcroft}
48513214adfSAndy Whitcroft
486cf655043SAndy Whitcroftsub statement_lines {
487cf655043SAndy Whitcroft	my ($stmt) = @_;
488cf655043SAndy Whitcroft
489cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
490cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
491cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
492cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
493cf655043SAndy Whitcroft
494cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
495cf655043SAndy Whitcroft
496cf655043SAndy Whitcroft	return $#stmt_lines + 2;
497cf655043SAndy Whitcroft}
498cf655043SAndy Whitcroft
499cf655043SAndy Whitcroftsub statement_rawlines {
500cf655043SAndy Whitcroft	my ($stmt) = @_;
501cf655043SAndy Whitcroft
502cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
503cf655043SAndy Whitcroft
504cf655043SAndy Whitcroft	return $#stmt_lines + 2;
505cf655043SAndy Whitcroft}
506cf655043SAndy Whitcroft
507cf655043SAndy Whitcroftsub statement_block_size {
508cf655043SAndy Whitcroft	my ($stmt) = @_;
509cf655043SAndy Whitcroft
510cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
511cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
512cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
513cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
514cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
515cf655043SAndy Whitcroft
516cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
517cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
518cf655043SAndy Whitcroft
519cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
520cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
521cf655043SAndy Whitcroft
522cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
523cf655043SAndy Whitcroft		return $stmt_lines;
524cf655043SAndy Whitcroft	} else {
525cf655043SAndy Whitcroft		return $stmt_statements;
526cf655043SAndy Whitcroft	}
527cf655043SAndy Whitcroft}
528cf655043SAndy Whitcroft
52913214adfSAndy Whitcroftsub ctx_statement_full {
53013214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
53113214adfSAndy Whitcroft	my ($statement, $condition, $level);
53213214adfSAndy Whitcroft
53313214adfSAndy Whitcroft	my (@chunks);
53413214adfSAndy Whitcroft
535cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
53613214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
53713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
538773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
53913214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
540cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
541cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
542cf655043SAndy Whitcroft	}
543cf655043SAndy Whitcroft
544cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
545cf655043SAndy Whitcroft	# could continue the statement.
546cf655043SAndy Whitcroft	for (;;) {
54713214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
54813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
549cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
550773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
551cf655043SAndy Whitcroft		#print "C: push\n";
552cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
55313214adfSAndy Whitcroft	}
55413214adfSAndy Whitcroft
55513214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5568905a67cSAndy Whitcroft}
5578905a67cSAndy Whitcroft
5584a0df2efSAndy Whitcroftsub ctx_block_get {
559f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5604a0df2efSAndy Whitcroft	my $line;
5614a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5624a0df2efSAndy Whitcroft	my $blk = '';
5634a0df2efSAndy Whitcroft	my @o;
5644a0df2efSAndy Whitcroft	my @c;
5654a0df2efSAndy Whitcroft	my @res = ();
5664a0df2efSAndy Whitcroft
567f0a594c1SAndy Whitcroft	my $level = 0;
56800df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
56900df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
57000df344fSAndy Whitcroft		$remain--;
57100df344fSAndy Whitcroft
57200df344fSAndy Whitcroft		$blk .= $rawlines[$line];
573f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
574f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
575f0a594c1SAndy Whitcroft			if ($off > 0) {
576f0a594c1SAndy Whitcroft				$off--;
577f0a594c1SAndy Whitcroft				next;
578f0a594c1SAndy Whitcroft			}
5794a0df2efSAndy Whitcroft
580f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
581f0a594c1SAndy Whitcroft				$level--;
582f0a594c1SAndy Whitcroft				last if ($level == 0);
583f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
584f0a594c1SAndy Whitcroft				$level++;
585f0a594c1SAndy Whitcroft			}
586f0a594c1SAndy Whitcroft		}
5874a0df2efSAndy Whitcroft
588f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
58900df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
5904a0df2efSAndy Whitcroft		}
5914a0df2efSAndy Whitcroft
592f0a594c1SAndy Whitcroft		last if ($level == 0);
5934a0df2efSAndy Whitcroft	}
5944a0df2efSAndy Whitcroft
595f0a594c1SAndy Whitcroft	return ($level, @res);
5964a0df2efSAndy Whitcroft}
5974a0df2efSAndy Whitcroftsub ctx_block_outer {
5984a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
5994a0df2efSAndy Whitcroft
600f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
601f0a594c1SAndy Whitcroft	return @r;
6024a0df2efSAndy Whitcroft}
6034a0df2efSAndy Whitcroftsub ctx_block {
6044a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6054a0df2efSAndy Whitcroft
606f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
607f0a594c1SAndy Whitcroft	return @r;
608653d4876SAndy Whitcroft}
609653d4876SAndy Whitcroftsub ctx_statement {
610f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
611f0a594c1SAndy Whitcroft
612f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
613f0a594c1SAndy Whitcroft	return @r;
614f0a594c1SAndy Whitcroft}
615f0a594c1SAndy Whitcroftsub ctx_block_level {
616653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
617653d4876SAndy Whitcroft
618f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6194a0df2efSAndy Whitcroft}
6209c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6219c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6229c0ca6f9SAndy Whitcroft
6239c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6249c0ca6f9SAndy Whitcroft}
6254a0df2efSAndy Whitcroft
6264a0df2efSAndy Whitcroftsub ctx_locate_comment {
6274a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6284a0df2efSAndy Whitcroft
6294a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
63000df344fSAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
6314a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6324a0df2efSAndy Whitcroft
6334a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6344a0df2efSAndy Whitcroft	# comment.
6354a0df2efSAndy Whitcroft	my $in_comment = 0;
6364a0df2efSAndy Whitcroft	$current_comment = '';
6374a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
63800df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
63900df344fSAndy Whitcroft		#warn "           $line\n";
6404a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6414a0df2efSAndy Whitcroft			$in_comment = 1;
6424a0df2efSAndy Whitcroft		}
6434a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6444a0df2efSAndy Whitcroft			$in_comment = 1;
6454a0df2efSAndy Whitcroft		}
6464a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6474a0df2efSAndy Whitcroft			$current_comment = '';
6484a0df2efSAndy Whitcroft		}
6494a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6504a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6514a0df2efSAndy Whitcroft			$in_comment = 0;
6524a0df2efSAndy Whitcroft		}
6534a0df2efSAndy Whitcroft	}
6544a0df2efSAndy Whitcroft
6554a0df2efSAndy Whitcroft	chomp($current_comment);
6564a0df2efSAndy Whitcroft	return($current_comment);
6574a0df2efSAndy Whitcroft}
6584a0df2efSAndy Whitcroftsub ctx_has_comment {
6594a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6604a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6614a0df2efSAndy Whitcroft
66200df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6634a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6644a0df2efSAndy Whitcroft
6654a0df2efSAndy Whitcroft	return ($cmt ne '');
6664a0df2efSAndy Whitcroft}
6674a0df2efSAndy Whitcroft
6680a920b5bSAndy Whitcroftsub cat_vet {
6690a920b5bSAndy Whitcroft	my ($vet) = @_;
6709c0ca6f9SAndy Whitcroft	my ($res, $coded);
6710a920b5bSAndy Whitcroft
6729c0ca6f9SAndy Whitcroft	$res = '';
6736c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
6746c72ffaaSAndy Whitcroft		$res .= $1;
6756c72ffaaSAndy Whitcroft		if ($2 ne '') {
6769c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
6776c72ffaaSAndy Whitcroft			$res .= $coded;
6786c72ffaaSAndy Whitcroft		}
6799c0ca6f9SAndy Whitcroft	}
6809c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
6810a920b5bSAndy Whitcroft
6829c0ca6f9SAndy Whitcroft	return $res;
6830a920b5bSAndy Whitcroft}
6840a920b5bSAndy Whitcroft
685c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
686cf655043SAndy Whitcroftmy $av_pending;
687c2fdda0dSAndy Whitcroftmy @av_paren_type;
688c2fdda0dSAndy Whitcroft
689c2fdda0dSAndy Whitcroftsub annotate_reset {
690c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
691cf655043SAndy Whitcroft	$av_pending = '_';
692cf655043SAndy Whitcroft	@av_paren_type = ('E');
693c2fdda0dSAndy Whitcroft}
694c2fdda0dSAndy Whitcroft
6956c72ffaaSAndy Whitcroftsub annotate_values {
6966c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
6976c72ffaaSAndy Whitcroft
6986c72ffaaSAndy Whitcroft	my $res;
6996c72ffaaSAndy Whitcroft	my $cur = $stream;
7006c72ffaaSAndy Whitcroft
701c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
7026c72ffaaSAndy Whitcroft
7036c72ffaaSAndy Whitcroft	while (length($cur)) {
704773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
705cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
706171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
7076c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
708c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
709c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
710cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
711c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7126c72ffaaSAndy Whitcroft			}
7136c72ffaaSAndy Whitcroft
7148905a67cSAndy Whitcroft		} elsif ($cur =~ /^($Type)/) {
715c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7166c72ffaaSAndy Whitcroft			$type = 'T';
7176c72ffaaSAndy Whitcroft
7186c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
719171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
720c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
721171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
722171ae1a4SAndy Whitcroft			if ($2 ne '') {
723cf655043SAndy Whitcroft				$av_pending = 'N';
724171ae1a4SAndy Whitcroft			}
725171ae1a4SAndy Whitcroft			$type = 'E';
726171ae1a4SAndy Whitcroft
727171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(#\s*undef\s*$Ident)/o) {
728171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
729171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
730171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
7316c72ffaaSAndy Whitcroft
732cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
733cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
734c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
735cf655043SAndy Whitcroft
736cf655043SAndy Whitcroft			push(@av_paren_type, $type);
737cf655043SAndy Whitcroft			push(@av_paren_type, $type);
738171ae1a4SAndy Whitcroft			$type = 'E';
739cf655043SAndy Whitcroft
740cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
741cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
742cf655043SAndy Whitcroft			$av_preprocessor = 1;
743cf655043SAndy Whitcroft
744cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
745cf655043SAndy Whitcroft
746171ae1a4SAndy Whitcroft			$type = 'E';
747cf655043SAndy Whitcroft
748cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:endif))/o) {
749cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
750cf655043SAndy Whitcroft
751cf655043SAndy Whitcroft			$av_preprocessor = 1;
752cf655043SAndy Whitcroft
753cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
754cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
755cf655043SAndy Whitcroft			pop(@av_paren_type);
756cf655043SAndy Whitcroft			push(@av_paren_type, $type);
757171ae1a4SAndy Whitcroft			$type = 'E';
7586c72ffaaSAndy Whitcroft
7596c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
760c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7616c72ffaaSAndy Whitcroft
762171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
763171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
764171ae1a4SAndy Whitcroft			$av_pending = $type;
765171ae1a4SAndy Whitcroft			$type = 'N';
766171ae1a4SAndy Whitcroft
7676c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
768c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
7696c72ffaaSAndy Whitcroft			if (defined $2) {
770cf655043SAndy Whitcroft				$av_pending = 'V';
7716c72ffaaSAndy Whitcroft			}
7726c72ffaaSAndy Whitcroft			$type = 'N';
7736c72ffaaSAndy Whitcroft
77413214adfSAndy Whitcroft		} elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
775c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
776cf655043SAndy Whitcroft			$av_pending = 'N';
7776c72ffaaSAndy Whitcroft			$type = 'N';
7786c72ffaaSAndy Whitcroft
7796c72ffaaSAndy Whitcroft		} elsif ($cur =~/^(return|case|else)/o) {
780c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
7816c72ffaaSAndy Whitcroft			$type = 'N';
7826c72ffaaSAndy Whitcroft
7836c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
784c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
785cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
786cf655043SAndy Whitcroft			$av_pending = '_';
7876c72ffaaSAndy Whitcroft			$type = 'N';
7886c72ffaaSAndy Whitcroft
7896c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
790cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
791cf655043SAndy Whitcroft			if ($new_type ne '_') {
792cf655043SAndy Whitcroft				$type = $new_type;
793c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
794c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
7956c72ffaaSAndy Whitcroft			} else {
796c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
7976c72ffaaSAndy Whitcroft			}
7986c72ffaaSAndy Whitcroft
7996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident)\(/o) {
800c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
801cf655043SAndy Whitcroft			$av_pending = 'V';
8026c72ffaaSAndy Whitcroft
8036c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
804c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
8056c72ffaaSAndy Whitcroft			$type = 'V';
8066c72ffaaSAndy Whitcroft
8076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
808c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
8096c72ffaaSAndy Whitcroft			$type = 'N';
8106c72ffaaSAndy Whitcroft
811cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
812c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
81313214adfSAndy Whitcroft			$type = 'E';
81413214adfSAndy Whitcroft
815cf655043SAndy Whitcroft		} elsif ($cur =~ /^(;|\?|:|\[)/o) {
81613214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
8176c72ffaaSAndy Whitcroft			$type = 'N';
8186c72ffaaSAndy Whitcroft
8196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
820c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
8216c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
8226c72ffaaSAndy Whitcroft				$type = 'N';
8236c72ffaaSAndy Whitcroft			}
8246c72ffaaSAndy Whitcroft
8256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
826c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
8276c72ffaaSAndy Whitcroft		}
8286c72ffaaSAndy Whitcroft		if (defined $1) {
8296c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
8306c72ffaaSAndy Whitcroft			$res .= $type x length($1);
8316c72ffaaSAndy Whitcroft		}
8326c72ffaaSAndy Whitcroft	}
8336c72ffaaSAndy Whitcroft
8346c72ffaaSAndy Whitcroft	return $res;
8356c72ffaaSAndy Whitcroft}
8366c72ffaaSAndy Whitcroft
8378905a67cSAndy Whitcroftsub possible {
83813214adfSAndy Whitcroft	my ($possible, $line) = @_;
8398905a67cSAndy Whitcroft
8408905a67cSAndy Whitcroft	#print "CHECK<$possible>\n";
8418905a67cSAndy Whitcroft	if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
8428905a67cSAndy Whitcroft	    $possible ne 'goto' && $possible ne 'return' &&
8438905a67cSAndy Whitcroft	    $possible ne 'struct' && $possible ne 'enum' &&
8448905a67cSAndy Whitcroft	    $possible ne 'case' && $possible ne 'else' &&
8458905a67cSAndy Whitcroft	    $possible ne 'typedef') {
84613214adfSAndy Whitcroft		warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
8478905a67cSAndy Whitcroft		push(@typeList, $possible);
8488905a67cSAndy Whitcroft		build_types();
8498905a67cSAndy Whitcroft	}
8508905a67cSAndy Whitcroft}
8518905a67cSAndy Whitcroft
8526c72ffaaSAndy Whitcroftmy $prefix = '';
8536c72ffaaSAndy Whitcroft
854f0a594c1SAndy Whitcroftsub report {
855773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
856773647a0SAndy Whitcroft		return 0;
857773647a0SAndy Whitcroft	}
8588905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
8598905a67cSAndy Whitcroft
8608905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
8618905a67cSAndy Whitcroft
86213214adfSAndy Whitcroft	push(our @report, $line);
863773647a0SAndy Whitcroft
864773647a0SAndy Whitcroft	return 1;
865f0a594c1SAndy Whitcroft}
866f0a594c1SAndy Whitcroftsub report_dump {
86713214adfSAndy Whitcroft	our @report;
868f0a594c1SAndy Whitcroft}
869de7d4f0eSAndy Whitcroftsub ERROR {
870773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
871de7d4f0eSAndy Whitcroft		our $clean = 0;
8726c72ffaaSAndy Whitcroft		our $cnt_error++;
873de7d4f0eSAndy Whitcroft	}
874773647a0SAndy Whitcroft}
875de7d4f0eSAndy Whitcroftsub WARN {
876773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
877de7d4f0eSAndy Whitcroft		our $clean = 0;
8786c72ffaaSAndy Whitcroft		our $cnt_warn++;
879de7d4f0eSAndy Whitcroft	}
880773647a0SAndy Whitcroft}
881de7d4f0eSAndy Whitcroftsub CHK {
882773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
883de7d4f0eSAndy Whitcroft		our $clean = 0;
8846c72ffaaSAndy Whitcroft		our $cnt_chk++;
8856c72ffaaSAndy Whitcroft	}
886de7d4f0eSAndy Whitcroft}
887de7d4f0eSAndy Whitcroft
8880a920b5bSAndy Whitcroftsub process {
8890a920b5bSAndy Whitcroft	my $filename = shift;
8900a920b5bSAndy Whitcroft
8910a920b5bSAndy Whitcroft	my $linenr=0;
8920a920b5bSAndy Whitcroft	my $prevline="";
893c2fdda0dSAndy Whitcroft	my $prevrawline="";
8940a920b5bSAndy Whitcroft	my $stashline="";
895c2fdda0dSAndy Whitcroft	my $stashrawline="";
8960a920b5bSAndy Whitcroft
8974a0df2efSAndy Whitcroft	my $length;
8980a920b5bSAndy Whitcroft	my $indent;
8990a920b5bSAndy Whitcroft	my $previndent=0;
9000a920b5bSAndy Whitcroft	my $stashindent=0;
9010a920b5bSAndy Whitcroft
902de7d4f0eSAndy Whitcroft	our $clean = 1;
9030a920b5bSAndy Whitcroft	my $signoff = 0;
9040a920b5bSAndy Whitcroft	my $is_patch = 0;
9050a920b5bSAndy Whitcroft
90613214adfSAndy Whitcroft	our @report = ();
9076c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
9086c72ffaaSAndy Whitcroft	our $cnt_error = 0;
9096c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
9106c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
9116c72ffaaSAndy Whitcroft
9120a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
9130a920b5bSAndy Whitcroft	my $realfile = '';
9140a920b5bSAndy Whitcroft	my $realline = 0;
9150a920b5bSAndy Whitcroft	my $realcnt = 0;
9160a920b5bSAndy Whitcroft	my $here = '';
9170a920b5bSAndy Whitcroft	my $in_comment = 0;
918c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
9190a920b5bSAndy Whitcroft	my $first_line = 0;
9200a920b5bSAndy Whitcroft
92113214adfSAndy Whitcroft	my $prev_values = 'E';
92213214adfSAndy Whitcroft
92313214adfSAndy Whitcroft	# suppression flags
924773647a0SAndy Whitcroft	my %suppress_ifbraces;
925653d4876SAndy Whitcroft
926c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
927de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
928c2fdda0dSAndy Whitcroft	#
929de7d4f0eSAndy Whitcroft	my @setup_docs = ();
930de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
931773647a0SAndy Whitcroft
932773647a0SAndy Whitcroft	sanitise_line_reset();
933c2fdda0dSAndy Whitcroft	my $line;
934c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
935773647a0SAndy Whitcroft		$linenr++;
936773647a0SAndy Whitcroft		$line = $rawline;
937c2fdda0dSAndy Whitcroft
938773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
939de7d4f0eSAndy Whitcroft			$setup_docs = 0;
940de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
941de7d4f0eSAndy Whitcroft				$setup_docs = 1;
942de7d4f0eSAndy Whitcroft			}
943773647a0SAndy Whitcroft			#next;
944de7d4f0eSAndy Whitcroft		}
945773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
946773647a0SAndy Whitcroft			$realline=$1-1;
947773647a0SAndy Whitcroft			if (defined $2) {
948773647a0SAndy Whitcroft				$realcnt=$3+1;
949773647a0SAndy Whitcroft			} else {
950773647a0SAndy Whitcroft				$realcnt=1+1;
951773647a0SAndy Whitcroft			}
952773647a0SAndy Whitcroft
953773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
954773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
955773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
956773647a0SAndy Whitcroft			# at context start.
957773647a0SAndy Whitcroft			my $edge;
958171ae1a4SAndy Whitcroft			for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
959773647a0SAndy Whitcroft				next if ($line =~ /^-/);
960773647a0SAndy Whitcroft				($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
961773647a0SAndy Whitcroft				last if (defined $edge);
962773647a0SAndy Whitcroft			}
963773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
964773647a0SAndy Whitcroft				$in_comment = 1;
965773647a0SAndy Whitcroft			}
966773647a0SAndy Whitcroft
967773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
968773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
969773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
970773647a0SAndy Whitcroft			if (!defined $edge &&
971773647a0SAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
972773647a0SAndy Whitcroft			{
973773647a0SAndy Whitcroft				$in_comment = 1;
974773647a0SAndy Whitcroft			}
975773647a0SAndy Whitcroft
976773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
977773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
978773647a0SAndy Whitcroft
979171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
980773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
981171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
982773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
983773647a0SAndy Whitcroft		}
984773647a0SAndy Whitcroft		push(@lines, $line);
985773647a0SAndy Whitcroft
986773647a0SAndy Whitcroft		if ($realcnt > 1) {
987773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
988773647a0SAndy Whitcroft		} else {
989773647a0SAndy Whitcroft			$realcnt = 0;
990773647a0SAndy Whitcroft		}
991773647a0SAndy Whitcroft
992773647a0SAndy Whitcroft		#print "==>$rawline\n";
993773647a0SAndy Whitcroft		#print "-->$line\n";
994de7d4f0eSAndy Whitcroft
995de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
996de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
997de7d4f0eSAndy Whitcroft		}
998de7d4f0eSAndy Whitcroft	}
999de7d4f0eSAndy Whitcroft
10006c72ffaaSAndy Whitcroft	$prefix = '';
10016c72ffaaSAndy Whitcroft
1002773647a0SAndy Whitcroft	$realcnt = 0;
1003773647a0SAndy Whitcroft	$linenr = 0;
10040a920b5bSAndy Whitcroft	foreach my $line (@lines) {
10050a920b5bSAndy Whitcroft		$linenr++;
10060a920b5bSAndy Whitcroft
1007c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
10086c72ffaaSAndy Whitcroft
10090a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
10106c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
10110a920b5bSAndy Whitcroft			$is_patch = 1;
10124a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
10130a920b5bSAndy Whitcroft			$realline=$1-1;
10140a920b5bSAndy Whitcroft			if (defined $2) {
10150a920b5bSAndy Whitcroft				$realcnt=$3+1;
10160a920b5bSAndy Whitcroft			} else {
10170a920b5bSAndy Whitcroft				$realcnt=1+1;
10180a920b5bSAndy Whitcroft			}
1019c2fdda0dSAndy Whitcroft			annotate_reset();
102013214adfSAndy Whitcroft			$prev_values = 'E';
102113214adfSAndy Whitcroft
1022773647a0SAndy Whitcroft			%suppress_ifbraces = ();
10230a920b5bSAndy Whitcroft			next;
10240a920b5bSAndy Whitcroft
10254a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
10264a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
10274a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1028773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
10290a920b5bSAndy Whitcroft			$realline++;
1030d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
10310a920b5bSAndy Whitcroft
10324a0df2efSAndy Whitcroft			# Measure the line length and indent.
1033c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
10340a920b5bSAndy Whitcroft
10350a920b5bSAndy Whitcroft			# Track the previous line.
10360a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
10370a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1038c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1039c2fdda0dSAndy Whitcroft
1040773647a0SAndy Whitcroft			#warn "line<$line>\n";
10416c72ffaaSAndy Whitcroft
1042d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1043d8aaf121SAndy Whitcroft			$realcnt--;
10440a920b5bSAndy Whitcroft		}
10450a920b5bSAndy Whitcroft
10460a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1047773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1048773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1049773647a0SAndy Whitcroft
10506c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
10516c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1052773647a0SAndy Whitcroft
1053773647a0SAndy Whitcroft		# extract the filename as it passes
1054773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1055773647a0SAndy Whitcroft			$realfile = $1;
1056773647a0SAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
1057773647a0SAndy Whitcroft
1058773647a0SAndy Whitcroft			if ($realfile =~ m@include/asm/@) {
1059773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1060773647a0SAndy Whitcroft			}
1061773647a0SAndy Whitcroft			next;
1062773647a0SAndy Whitcroft		}
1063773647a0SAndy Whitcroft
1064389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
10650a920b5bSAndy Whitcroft
1066c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1067c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1068c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
10690a920b5bSAndy Whitcroft
10706c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
10716c72ffaaSAndy Whitcroft
10720a920b5bSAndy Whitcroft#check the patch for a signoff:
1073d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
10744a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
10754a0df2efSAndy Whitcroft			$signoff++;
10760a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1077de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1078de7d4f0eSAndy Whitcroft					$herecurr);
10790a920b5bSAndy Whitcroft			}
10800a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1081773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1082de7d4f0eSAndy Whitcroft					$herecurr);
10830a920b5bSAndy Whitcroft			}
10840a920b5bSAndy Whitcroft		}
10850a920b5bSAndy Whitcroft
108600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
10878905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1088de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
10896c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1090de7d4f0eSAndy Whitcroft		}
1091de7d4f0eSAndy Whitcroft
1092de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1093de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1094171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1095171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1096171ae1a4SAndy Whitcroft
1097171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1098171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1099171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1100171ae1a4SAndy Whitcroft
1101171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
110200df344fSAndy Whitcroft		}
11030a920b5bSAndy Whitcroft
110400df344fSAndy Whitcroft#ignore lines being removed
110500df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
110600df344fSAndy Whitcroft
110700df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
110800df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
11090a920b5bSAndy Whitcroft
11100a920b5bSAndy Whitcroft#trailing whitespace
11119c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1112c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
11139c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
11149c0ca6f9SAndy Whitcroft
1115c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1116c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1117de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
11180a920b5bSAndy Whitcroft		}
11190a920b5bSAndy Whitcroft#80 column limit
1120c2fdda0dSAndy Whitcroft		if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
1121de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
11220a920b5bSAndy Whitcroft		}
11230a920b5bSAndy Whitcroft
11248905a67cSAndy Whitcroft# check for adding lines without a newline.
11258905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
11268905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
11278905a67cSAndy Whitcroft		}
11288905a67cSAndy Whitcroft
11290a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
11300a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
11310a920b5bSAndy Whitcroft
11320a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
11330a920b5bSAndy Whitcroft# more than 8 must use tabs.
1134c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1135c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1136c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1137171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
11380a920b5bSAndy Whitcroft		}
11390a920b5bSAndy Whitcroft
1140c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1141cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1142c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1143c2fdda0dSAndy Whitcroft		}
114422f2a2efSAndy Whitcroft
11459c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1146171ae1a4SAndy Whitcroft		my ($stat, $cond);
1147*9c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1148171ae1a4SAndy Whitcroft			($stat, $cond) = ctx_statement_block($linenr,
1149171ae1a4SAndy Whitcroft								$realcnt, 0);
1150171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1151171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1152171ae1a4SAndy Whitcroft
1153171ae1a4SAndy Whitcroft			my $s = $stat;
1154171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1155cf655043SAndy Whitcroft
1156c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1157171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1158c2fdda0dSAndy Whitcroft
1159c2fdda0dSAndy Whitcroft			# Ignore functions being called
1160171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1161c2fdda0dSAndy Whitcroft
11626c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1163171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1164cf655043SAndy Whitcroft				possible($1, $s);
11658905a67cSAndy Whitcroft
11666c72ffaaSAndy Whitcroft			# declarations always start with types
1167171ae1a4SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/s) {
1168cf655043SAndy Whitcroft				possible($1, $s);
1169c2fdda0dSAndy Whitcroft			}
11708905a67cSAndy Whitcroft
11716c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1172171ae1a4SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1173cf655043SAndy Whitcroft				possible($1, $s);
11749c0ca6f9SAndy Whitcroft			}
11758905a67cSAndy Whitcroft
11768905a67cSAndy Whitcroft			# Check for any sort of function declaration.
11778905a67cSAndy Whitcroft			# int foo(something bar, other baz);
11788905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1179171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
11808905a67cSAndy Whitcroft				my ($name_len) = length($1);
11818905a67cSAndy Whitcroft
1182cf655043SAndy Whitcroft				my $ctx = $s;
1183773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
11848905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1185cf655043SAndy Whitcroft
11868905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
11878905a67cSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
11888905a67cSAndy Whitcroft
1189cf655043SAndy Whitcroft						possible($1, $s);
11908905a67cSAndy Whitcroft					}
11918905a67cSAndy Whitcroft				}
11928905a67cSAndy Whitcroft			}
11938905a67cSAndy Whitcroft
11949c0ca6f9SAndy Whitcroft		}
11959c0ca6f9SAndy Whitcroft
119600df344fSAndy Whitcroft#
119700df344fSAndy Whitcroft# Checks which may be anchored in the context.
119800df344fSAndy Whitcroft#
119900df344fSAndy Whitcroft
120000df344fSAndy Whitcroft# Check for switch () and associated case and default
120100df344fSAndy Whitcroft# statements should be at the same indent.
120200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
120300df344fSAndy Whitcroft			my $err = '';
120400df344fSAndy Whitcroft			my $sep = '';
120500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
120600df344fSAndy Whitcroft			shift(@ctx);
120700df344fSAndy Whitcroft			for my $ctx (@ctx) {
120800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
120900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
121000df344fSAndy Whitcroft							$indent != $cindent) {
121100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
121200df344fSAndy Whitcroft					$sep = '';
121300df344fSAndy Whitcroft				} else {
121400df344fSAndy Whitcroft					$sep = "[...]\n";
121500df344fSAndy Whitcroft				}
121600df344fSAndy Whitcroft			}
121700df344fSAndy Whitcroft			if ($err ne '') {
12189c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1219de7d4f0eSAndy Whitcroft			}
1220de7d4f0eSAndy Whitcroft		}
1221de7d4f0eSAndy Whitcroft
1222de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1223de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1224773647a0SAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1225773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1226773647a0SAndy Whitcroft
12279c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1228de7d4f0eSAndy Whitcroft			my $ctx_ln = $linenr + $#ctx + 1;
1229de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1230de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1231de7d4f0eSAndy Whitcroft
1232773647a0SAndy Whitcroft			##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1233de7d4f0eSAndy Whitcroft
1234773647a0SAndy Whitcroft			# Skip over any removed lines in the context following statement.
1235773647a0SAndy Whitcroft			while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1236773647a0SAndy Whitcroft				$ctx_ln++;
1237773647a0SAndy Whitcroft			}
1238773647a0SAndy Whitcroft			##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1239773647a0SAndy Whitcroft
1240773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1241773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1242de7d4f0eSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]");
124300df344fSAndy Whitcroft			}
1244773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1245773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1246773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1247773647a0SAndy Whitcroft			{
12489c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
12499c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1250773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
12519c0ca6f9SAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]");
12529c0ca6f9SAndy Whitcroft				}
12539c0ca6f9SAndy Whitcroft			}
125400df344fSAndy Whitcroft		}
125500df344fSAndy Whitcroft
12566c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
12576c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
12586c72ffaaSAndy Whitcroft		my $curr_values = annotate_values($opline . "\n", $prev_values);
12596c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1260c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1261c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1262cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1263cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
1264c2fdda0dSAndy Whitcroft		}
12656c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
12666c72ffaaSAndy Whitcroft
126700df344fSAndy Whitcroft#ignore lines not being added
126800df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
126900df344fSAndy Whitcroft
1270653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
1271653d4876SAndy Whitcroft		if ($tst_type && $line =~ /^.$Declare$/) {
1272de7d4f0eSAndy Whitcroft			ERROR("TEST: is type $Declare\n" . $herecurr);
1273653d4876SAndy Whitcroft			next;
1274653d4876SAndy Whitcroft		}
1275653d4876SAndy Whitcroft
1276f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1277f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1278f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1279773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1280f0a594c1SAndy Whitcroft		}
1281f0a594c1SAndy Whitcroft
128200df344fSAndy Whitcroft#
128300df344fSAndy Whitcroft# Checks which are anchored on the added line.
128400df344fSAndy Whitcroft#
128500df344fSAndy Whitcroft
1286653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1287653d4876SAndy Whitcroft		if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1288653d4876SAndy Whitcroft			my $path = $1;
1289653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1290de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1291de7d4f0eSAndy Whitcroft					$herecurr);
1292653d4876SAndy Whitcroft			}
1293653d4876SAndy Whitcroft		}
1294653d4876SAndy Whitcroft
129500df344fSAndy Whitcroft# no C99 // comments
129600df344fSAndy Whitcroft		if ($line =~ m{//}) {
1297de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
129800df344fSAndy Whitcroft		}
129900df344fSAndy Whitcroft		# Remove C99 comments.
13000a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
13016c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
13020a920b5bSAndy Whitcroft
13030a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1304653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1305653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1306653d4876SAndy Whitcroft			my $name = $1;
13070a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
13080a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
1309653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
1310cf655043SAndy Whitcroft			   ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1311cf655043SAndy Whitcroft			   ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1312171ae1a4SAndy Whitcroft			   ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
1313cf655043SAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1314de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
13150a920b5bSAndy Whitcroft			}
13160a920b5bSAndy Whitcroft		}
13170a920b5bSAndy Whitcroft
1318f0a594c1SAndy Whitcroft# check for external initialisers.
1319*9c9ba34eSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL|false)\s*;/) {
1320f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1321f0a594c1SAndy Whitcroft				$herecurr);
1322f0a594c1SAndy Whitcroft		}
13230a920b5bSAndy Whitcroft# check for static initialisers.
1324*9c9ba34eSAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1325de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1326de7d4f0eSAndy Whitcroft				$herecurr);
13270a920b5bSAndy Whitcroft		}
13280a920b5bSAndy Whitcroft
1329653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1330653d4876SAndy Whitcroft# make sense.
1331653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
13329c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1333653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1334de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
13350a920b5bSAndy Whitcroft		}
13360a920b5bSAndy Whitcroft
13370a920b5bSAndy Whitcroft# * goes on variable not on type
1338d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1339de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1340de7d4f0eSAndy Whitcroft				$herecurr);
1341d8aaf121SAndy Whitcroft
1342d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1343de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1344de7d4f0eSAndy Whitcroft				$herecurr);
1345d8aaf121SAndy Whitcroft
13469c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1347de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1348de7d4f0eSAndy Whitcroft				$herecurr);
1349d8aaf121SAndy Whitcroft
13509c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1351de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1352de7d4f0eSAndy Whitcroft				$herecurr);
13530a920b5bSAndy Whitcroft		}
13540a920b5bSAndy Whitcroft
13550a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
13560a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
13570a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
13580a920b5bSAndy Whitcroft# 			print "$herecurr";
13590a920b5bSAndy Whitcroft# 			$clean = 0;
13600a920b5bSAndy Whitcroft# 		}
13610a920b5bSAndy Whitcroft
13628905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1363c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
13648905a67cSAndy Whitcroft		}
13658905a67cSAndy Whitcroft
136600df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
136700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
136800df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
136900df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
137000df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1371f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
137200df344fSAndy Whitcroft			my $ok = 0;
137300df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
137400df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
137500df344fSAndy Whitcroft				# we have a preceeding printk if it ends
137600df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
137700df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
137800df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
137900df344fSAndy Whitcroft						$ok = 1;
138000df344fSAndy Whitcroft					}
138100df344fSAndy Whitcroft					last;
138200df344fSAndy Whitcroft				}
138300df344fSAndy Whitcroft			}
138400df344fSAndy Whitcroft			if ($ok == 0) {
1385de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
13860a920b5bSAndy Whitcroft			}
138700df344fSAndy Whitcroft		}
13880a920b5bSAndy Whitcroft
1389653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1390653d4876SAndy Whitcroft# or if closed on same line
1391c2fdda0dSAndy Whitcroft		if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
13920a920b5bSAndy Whitcroft		    !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1393de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
13940a920b5bSAndy Whitcroft		}
1395653d4876SAndy Whitcroft
13968905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
13978905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
13988905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
13998905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
14008905a67cSAndy Whitcroft		}
14018905a67cSAndy Whitcroft
1402f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
14036c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1404c2fdda0dSAndy Whitcroft			my $name = $1;
1405773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1406773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1407c2fdda0dSAndy Whitcroft
1408c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1409773647a0SAndy Whitcroft			if ($name =~ /^(?:
1410773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1411773647a0SAndy Whitcroft				volatile|__volatile__|
1412773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1413773647a0SAndy Whitcroft				asm|__asm__)$/x)
1414773647a0SAndy Whitcroft			{
1415c2fdda0dSAndy Whitcroft
1416c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1417c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1418c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1419773647a0SAndy Whitcroft			} elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1420773647a0SAndy Whitcroft
1421773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1422773647a0SAndy Whitcroft			} elsif ($ctx =~ /^.\#\s*elif\s*$/) {
1423c2fdda0dSAndy Whitcroft
1424c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1425c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1426773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1427c2fdda0dSAndy Whitcroft
1428c2fdda0dSAndy Whitcroft			} else {
1429773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1430f0a594c1SAndy Whitcroft			}
14316c72ffaaSAndy Whitcroft		}
1432653d4876SAndy Whitcroft# Check operator spacing.
14330a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
14349c0ca6f9SAndy Whitcroft			my $ops = qr{
14359c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
14369c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
14379c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
1438c2fdda0dSAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
14399c0ca6f9SAndy Whitcroft			}x;
1440cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
144100df344fSAndy Whitcroft			my $off = 0;
14426c72ffaaSAndy Whitcroft
14436c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
14446c72ffaaSAndy Whitcroft
14450a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
14464a0df2efSAndy Whitcroft				$off += length($elements[$n]);
14474a0df2efSAndy Whitcroft
1448773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1449773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1450773647a0SAndy Whitcroft				my $cc = '';
1451773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1452773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1453773647a0SAndy Whitcroft				}
1454773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1455773647a0SAndy Whitcroft
14564a0df2efSAndy Whitcroft				my $a = '';
14574a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
14584a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1459cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
14604a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
14614a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1462773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
14634a0df2efSAndy Whitcroft
14640a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
14654a0df2efSAndy Whitcroft
14664a0df2efSAndy Whitcroft				my $c = '';
14670a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
14684a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
14694a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1470cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
14714a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
14724a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
147322f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
14744a0df2efSAndy Whitcroft				} else {
14754a0df2efSAndy Whitcroft					$c = 'E';
14760a920b5bSAndy Whitcroft				}
14770a920b5bSAndy Whitcroft
14784a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
14794a0df2efSAndy Whitcroft
14804a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
14814a0df2efSAndy Whitcroft
14826c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1483de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
14840a920b5bSAndy Whitcroft
14859c0ca6f9SAndy Whitcroft				# Classify operators into binary, unary, or
14869c0ca6f9SAndy Whitcroft				# definitions (* only) where they have more
14879c0ca6f9SAndy Whitcroft				# than one mode.
14886c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
14896c72ffaaSAndy Whitcroft				my $op_left = substr($curr_values, $off, 1);
14906c72ffaaSAndy Whitcroft				my $is_unary;
14916c72ffaaSAndy Whitcroft				if ($op_type eq 'T') {
14929c0ca6f9SAndy Whitcroft					$is_unary = 2;
14936c72ffaaSAndy Whitcroft				} elsif ($op_left eq 'V') {
14946c72ffaaSAndy Whitcroft					$is_unary = 0;
14956c72ffaaSAndy Whitcroft				} else {
14966c72ffaaSAndy Whitcroft					$is_unary = 1;
14979c0ca6f9SAndy Whitcroft				}
14989c0ca6f9SAndy Whitcroft				#if ($op eq '-' || $op eq '&' || $op eq '*') {
14996c72ffaaSAndy Whitcroft				#	print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
15009c0ca6f9SAndy Whitcroft				#}
15010a920b5bSAndy Whitcroft
150213214adfSAndy Whitcroft				# Ignore operators passed as parameters.
150313214adfSAndy Whitcroft				if ($op_type ne 'V' &&
150413214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
150513214adfSAndy Whitcroft
1506cf655043SAndy Whitcroft#				# Ignore comments
1507cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
150813214adfSAndy Whitcroft
1509d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
151013214adfSAndy Whitcroft				} elsif ($op eq ';') {
1511cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1512cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1513773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1514d8aaf121SAndy Whitcroft					}
1515d8aaf121SAndy Whitcroft
1516d8aaf121SAndy Whitcroft				# // is a comment
1517d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
15180a920b5bSAndy Whitcroft
15190a920b5bSAndy Whitcroft				# -> should have no spaces
15200a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
15214a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1522773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
15230a920b5bSAndy Whitcroft					}
15240a920b5bSAndy Whitcroft
15250a920b5bSAndy Whitcroft				# , must have a space on the right.
15260a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1527cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1528773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
15290a920b5bSAndy Whitcroft					}
15300a920b5bSAndy Whitcroft
15319c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
15329c0ca6f9SAndy Whitcroft				} elsif ($op eq '*' && $is_unary == 2) {
15339c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
15349c0ca6f9SAndy Whitcroft
15359c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
15369c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
15379c0ca6f9SAndy Whitcroft				# unary operator, or a cast
15389c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
15399c0ca6f9SAndy Whitcroft				         ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1540cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1541773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
15420a920b5bSAndy Whitcroft					}
1543171ae1a4SAndy Whitcroft					if ($op  eq '*' && $cc =~/\s*const\b/) {
1544171ae1a4SAndy Whitcroft						# A unary '*' may be const
1545171ae1a4SAndy Whitcroft
1546171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1547773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
15480a920b5bSAndy Whitcroft					}
15490a920b5bSAndy Whitcroft
15500a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
15510a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1552773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1553773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
15540a920b5bSAndy Whitcroft					}
1555773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1556773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1557773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1558653d4876SAndy Whitcroft					}
1559773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1560773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1561773647a0SAndy Whitcroft					}
1562773647a0SAndy Whitcroft
15630a920b5bSAndy Whitcroft
15640a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
15659c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
15669c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
15679c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1568c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1569c2fdda0dSAndy Whitcroft					 $op eq '%')
15700a920b5bSAndy Whitcroft				{
1571773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1572de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1573de7d4f0eSAndy Whitcroft							$hereptr);
15740a920b5bSAndy Whitcroft					}
15750a920b5bSAndy Whitcroft
15760a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1577cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
157822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
157922f2a2efSAndy Whitcroft					if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
158022f2a2efSAndy Whitcroft					    !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1581773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
15820a920b5bSAndy Whitcroft					}
158322f2a2efSAndy Whitcroft				}
15844a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
15850a920b5bSAndy Whitcroft			}
15860a920b5bSAndy Whitcroft		}
15870a920b5bSAndy Whitcroft
1588f0a594c1SAndy Whitcroft# check for multiple assignments
1589f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
15906c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1591f0a594c1SAndy Whitcroft		}
1592f0a594c1SAndy Whitcroft
159322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
159422f2a2efSAndy Whitcroft## # continuation.
159522f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
159622f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
159722f2a2efSAndy Whitcroft##
159822f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
159922f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
160022f2a2efSAndy Whitcroft## 			my $ln = $line;
160122f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
160222f2a2efSAndy Whitcroft## 			}
160322f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
160422f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
160522f2a2efSAndy Whitcroft## 			}
160622f2a2efSAndy Whitcroft## 		}
1607f0a594c1SAndy Whitcroft
16080a920b5bSAndy Whitcroft#need space before brace following if, while, etc
160922f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
161022f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1611773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1612de7d4f0eSAndy Whitcroft		}
1613de7d4f0eSAndy Whitcroft
1614de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1615de7d4f0eSAndy Whitcroft# on the line
1616de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1617773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
16180a920b5bSAndy Whitcroft		}
16190a920b5bSAndy Whitcroft
162022f2a2efSAndy Whitcroft# check spacing on square brackets
162122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1622773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
162322f2a2efSAndy Whitcroft		}
162422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1625773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
162622f2a2efSAndy Whitcroft		}
162722f2a2efSAndy Whitcroft
162822f2a2efSAndy Whitcroft# check spacing on paretheses
16299c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
16309c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1631773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
163222f2a2efSAndy Whitcroft		}
163313214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
16349c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/) {
1635773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
163622f2a2efSAndy Whitcroft		}
163722f2a2efSAndy Whitcroft
16380a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
16394a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
16400a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1641de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
16420a920b5bSAndy Whitcroft		}
16430a920b5bSAndy Whitcroft
16440a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
16454a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1646773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
16470a920b5bSAndy Whitcroft		}
16480a920b5bSAndy Whitcroft
16490a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional.
16508905a67cSAndy Whitcroft		if ($line =~ /\bif\s*\(/) {
1651171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
16528905a67cSAndy Whitcroft
16538905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1654c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
16558905a67cSAndy Whitcroft			}
16568905a67cSAndy Whitcroft
16578905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
16588905a67cSAndy Whitcroft			# conditional.
1659773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
16608905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
166113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
1662773647a0SAndy Whitcroft			if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1663773647a0SAndy Whitcroft			    $c !~ /^.\#\s*if/)
1664773647a0SAndy Whitcroft			{
16658905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
16668905a67cSAndy Whitcroft			}
16678905a67cSAndy Whitcroft		}
16688905a67cSAndy Whitcroft
166913214adfSAndy Whitcroft# Check for bitwise tests written as boolean
167013214adfSAndy Whitcroft		if ($line =~ /
167113214adfSAndy Whitcroft			(?:
167213214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
167313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
167413214adfSAndy Whitcroft				(?:\&\&|\|\|)
167513214adfSAndy Whitcroft			|
167613214adfSAndy Whitcroft				(?:\&\&|\|\|)
167713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
167813214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
167913214adfSAndy Whitcroft			)/x)
168013214adfSAndy Whitcroft		{
168113214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
168213214adfSAndy Whitcroft		}
168313214adfSAndy Whitcroft
16848905a67cSAndy Whitcroft# if and else should not have general statements after it
168513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
168613214adfSAndy Whitcroft			my $s = $1;
168713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
168813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
16898905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
16900a920b5bSAndy Whitcroft			}
169113214adfSAndy Whitcroft		}
16920a920b5bSAndy Whitcroft
16930a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
16940a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
16950a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
16960a920b5bSAndy Whitcroft						$previndent == $indent) {
1697de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
16980a920b5bSAndy Whitcroft		}
16990a920b5bSAndy Whitcroft
1700c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1701c2fdda0dSAndy Whitcroft						$previndent == $indent) {
1702c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1703c2fdda0dSAndy Whitcroft
1704c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
1705c2fdda0dSAndy Whitcroft			# conditional.
1706773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
1707c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
1708c2fdda0dSAndy Whitcroft
1709c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
1710c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
1711c2fdda0dSAndy Whitcroft			}
1712c2fdda0dSAndy Whitcroft		}
1713c2fdda0dSAndy Whitcroft
17140a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
17150a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
17160a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
17170a920b5bSAndy Whitcroft#		    print "$herecurr";
17180a920b5bSAndy Whitcroft#		    $clean = 0;
17190a920b5bSAndy Whitcroft#		}
17200a920b5bSAndy Whitcroft
17210a920b5bSAndy Whitcroft#no spaces allowed after \ in define
17220a920b5bSAndy Whitcroft		if ($line=~/\#define.*\\\s$/) {
1723de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
17240a920b5bSAndy Whitcroft		}
17250a920b5bSAndy Whitcroft
1726653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1727653d4876SAndy Whitcroft		if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
17286c72ffaaSAndy Whitcroft			my $checkfile = "$root/include/linux/$1.h";
1729171ae1a4SAndy Whitcroft			if (-f $checkfile && $1 ne 'irq') {
1730773647a0SAndy Whitcroft				WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1731de7d4f0eSAndy Whitcroft					$herecurr);
17320a920b5bSAndy Whitcroft			}
17330a920b5bSAndy Whitcroft		}
17340a920b5bSAndy Whitcroft
1735653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
1736653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
1737cf655043SAndy Whitcroft# in a known good container
17389c0ca6f9SAndy Whitcroft		if ($prevline =~ /\#define.*\\/ &&
17399c0ca6f9SAndy Whitcroft		   $prevline !~/(?:do\s+{|\(\{|\{)/ &&
17409c0ca6f9SAndy Whitcroft		   $line !~ /(?:do\s+{|\(\{|\{)/ &&
17419c0ca6f9SAndy Whitcroft		   $line !~ /^.\s*$Declare\s/) {
1742653d4876SAndy Whitcroft			# Grab the first statement, if that is the entire macro
1743653d4876SAndy Whitcroft			# its ok.  This may start either on the #define line
1744653d4876SAndy Whitcroft			# or the one below.
1745d8aaf121SAndy Whitcroft			my $ln = $linenr;
1746d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
1747f0a594c1SAndy Whitcroft			my $off = 0;
1748653d4876SAndy Whitcroft
1749f0a594c1SAndy Whitcroft			# If the macro starts on the define line start
1750f0a594c1SAndy Whitcroft			# grabbing the statement after the identifier
1751f0a594c1SAndy Whitcroft			$prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1752f0a594c1SAndy Whitcroft			##print "1<$1> 2<$2>\n";
175322f2a2efSAndy Whitcroft			if (defined $2 && $2 ne '') {
1754f0a594c1SAndy Whitcroft				$off = length($1);
1755d8aaf121SAndy Whitcroft				$ln--;
1756d8aaf121SAndy Whitcroft				$cnt++;
17578905a67cSAndy Whitcroft				while ($lines[$ln - 1] =~ /^-/) {
17588905a67cSAndy Whitcroft					$ln--;
17598905a67cSAndy Whitcroft					$cnt++;
17608905a67cSAndy Whitcroft				}
1761d8aaf121SAndy Whitcroft			}
1762f0a594c1SAndy Whitcroft			my @ctx = ctx_statement($ln, $cnt, $off);
1763de7d4f0eSAndy Whitcroft			my $ctx_ln = $ln + $#ctx + 1;
1764de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1765de7d4f0eSAndy Whitcroft
1766de7d4f0eSAndy Whitcroft			# Pull in any empty extension lines.
1767de7d4f0eSAndy Whitcroft			while ($ctx =~ /\\$/ &&
1768de7d4f0eSAndy Whitcroft			       $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1769de7d4f0eSAndy Whitcroft				$ctx .= $lines[$ctx_ln - 1];
1770de7d4f0eSAndy Whitcroft				$ctx_ln++;
1771de7d4f0eSAndy Whitcroft			}
1772d8aaf121SAndy Whitcroft
1773d8aaf121SAndy Whitcroft			if ($ctx =~ /\\$/) {
1774d8aaf121SAndy Whitcroft				if ($ctx =~ /;/) {
1775de7d4f0eSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1776d8aaf121SAndy Whitcroft				} else {
1777de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1778d8aaf121SAndy Whitcroft				}
17790a920b5bSAndy Whitcroft			}
1780653d4876SAndy Whitcroft		}
17810a920b5bSAndy Whitcroft
1782f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
178313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
178413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
1785cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
178613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1787cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1788cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
178913214adfSAndy Whitcroft				my $allowed = 0;
179013214adfSAndy Whitcroft				my $seen = 0;
1791773647a0SAndy Whitcroft				my $herectx = $here . "\n";
1792cf655043SAndy Whitcroft				my $ln = $linenr - 1;
179313214adfSAndy Whitcroft				for my $chunk (@chunks) {
179413214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
179513214adfSAndy Whitcroft
1796773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
1797773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1798773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
1799773647a0SAndy Whitcroft
1800773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1801773647a0SAndy Whitcroft
1802773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
1803773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
1804773647a0SAndy Whitcroft
1805773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1806cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
1807cf655043SAndy Whitcroft
1808773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
180913214adfSAndy Whitcroft
181013214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
181113214adfSAndy Whitcroft
1812cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
1813cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
1814cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
181513214adfSAndy Whitcroft						$allowed = 1;
181613214adfSAndy Whitcroft					}
181713214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
1818cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
181913214adfSAndy Whitcroft						$allowed = 1;
182013214adfSAndy Whitcroft					}
1821cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
1822cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
182313214adfSAndy Whitcroft						$allowed = 1;
182413214adfSAndy Whitcroft					}
182513214adfSAndy Whitcroft				}
182613214adfSAndy Whitcroft				if ($seen && !$allowed) {
1827cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
182813214adfSAndy Whitcroft				}
182913214adfSAndy Whitcroft			}
183013214adfSAndy Whitcroft		}
1831773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
183213214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
1833cf655043SAndy Whitcroft			my $allowed = 0;
1834f0a594c1SAndy Whitcroft
1835cf655043SAndy Whitcroft			# Check the pre-context.
1836cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1837cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
1838cf655043SAndy Whitcroft				$allowed = 1;
1839f0a594c1SAndy Whitcroft			}
1840773647a0SAndy Whitcroft
1841773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
1842773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
1843773647a0SAndy Whitcroft
1844cf655043SAndy Whitcroft			# Check the condition.
1845cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
1846773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
1847cf655043SAndy Whitcroft			if (defined $cond) {
1848773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
1849cf655043SAndy Whitcroft			}
1850cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
1851cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
1852cf655043SAndy Whitcroft				$allowed = 1;
1853cf655043SAndy Whitcroft			}
1854cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
1855cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
1856cf655043SAndy Whitcroft				$allowed = 1;
1857cf655043SAndy Whitcroft			}
1858cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
1859cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
1860cf655043SAndy Whitcroft				$allowed = 1;
1861cf655043SAndy Whitcroft			}
1862cf655043SAndy Whitcroft			# Check the post-context.
1863cf655043SAndy Whitcroft			if (defined $chunks[1]) {
1864cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
1865cf655043SAndy Whitcroft				if (defined $cond) {
1866773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
1867cf655043SAndy Whitcroft				}
1868cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
1869cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
1870cf655043SAndy Whitcroft					$allowed = 1;
1871cf655043SAndy Whitcroft				}
1872cf655043SAndy Whitcroft			}
1873cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1874cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
1875cf655043SAndy Whitcroft				my $end = $linenr + statement_rawlines($block) - 1;
1876cf655043SAndy Whitcroft
1877cf655043SAndy Whitcroft				for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1878cf655043SAndy Whitcroft					$herectx .= $rawlines[$ln] . "\n";;
1879cf655043SAndy Whitcroft				}
1880cf655043SAndy Whitcroft
1881cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1882f0a594c1SAndy Whitcroft			}
1883f0a594c1SAndy Whitcroft		}
1884f0a594c1SAndy Whitcroft
1885653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
18864a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
1887653d4876SAndy Whitcroft			if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1888de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
18890a920b5bSAndy Whitcroft			}
18900a920b5bSAndy Whitcroft		}
18910a920b5bSAndy Whitcroft
18924a0df2efSAndy Whitcroft# don't use deprecated functions
18934a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
189400df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
1895de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
18964a0df2efSAndy Whitcroft			}
18974a0df2efSAndy Whitcroft		}
18984a0df2efSAndy Whitcroft
18994a0df2efSAndy Whitcroft# no volatiles please
19006c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
19016c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1902de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
19034a0df2efSAndy Whitcroft		}
19044a0df2efSAndy Whitcroft
19059c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
19069c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
19079c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
19089c0ca6f9SAndy Whitcroft		}
19099c0ca6f9SAndy Whitcroft
191000df344fSAndy Whitcroft# warn about #if 0
191100df344fSAndy Whitcroft		if ($line =~ /^.#\s*if\s+0\b/) {
1912de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
1913de7d4f0eSAndy Whitcroft				$herecurr);
19144a0df2efSAndy Whitcroft		}
19154a0df2efSAndy Whitcroft
1916f0a594c1SAndy Whitcroft# check for needless kfree() checks
1917f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1918f0a594c1SAndy Whitcroft			my $expr = $1;
1919f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1920f0a594c1SAndy Whitcroft				WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1921f0a594c1SAndy Whitcroft			}
1922f0a594c1SAndy Whitcroft		}
1923c2010a3bSGreg Kroah-Hartman# check for needless usb_free_urb() checks
1924c2010a3bSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1925c2010a3bSGreg Kroah-Hartman			my $expr = $1;
1926c2010a3bSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1927c2010a3bSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1928c2010a3bSGreg Kroah-Hartman			}
1929c2010a3bSGreg Kroah-Hartman		}
1930f0a594c1SAndy Whitcroft
193100df344fSAndy Whitcroft# warn about #ifdefs in C files
193200df344fSAndy Whitcroft#		if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
193300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
193400df344fSAndy Whitcroft#			print "$herecurr";
193500df344fSAndy Whitcroft#			$clean = 0;
193600df344fSAndy Whitcroft#		}
193700df344fSAndy Whitcroft
193822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
193922f2a2efSAndy Whitcroft		if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
194022f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
194122f2a2efSAndy Whitcroft		}
194222f2a2efSAndy Whitcroft
19434a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
1944171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
1945171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
19464a0df2efSAndy Whitcroft			my $which = $1;
19474a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1948de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
19494a0df2efSAndy Whitcroft			}
19504a0df2efSAndy Whitcroft		}
19514a0df2efSAndy Whitcroft# check for memory barriers without a comment.
19524a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
19534a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1954de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
19554a0df2efSAndy Whitcroft			}
19564a0df2efSAndy Whitcroft		}
19574a0df2efSAndy Whitcroft# check of hardware specific defines
195822f2a2efSAndy Whitcroft		if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1959de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
19600a920b5bSAndy Whitcroft		}
1961653d4876SAndy Whitcroft
1962de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
1963de7d4f0eSAndy Whitcroft# storage class and type.
19649c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
19659c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
1966de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1967de7d4f0eSAndy Whitcroft		}
1968de7d4f0eSAndy Whitcroft
19698905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
19708905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
19718905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
19728905a67cSAndy Whitcroft		}
19738905a67cSAndy Whitcroft
1974de7d4f0eSAndy Whitcroft# check for new externs in .c files.
1975171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
1976*9c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+$Ident(\s*)\(/s)
1977171ae1a4SAndy Whitcroft		{
1978171ae1a4SAndy Whitcroft			my $paren_space = $1;
1979171ae1a4SAndy Whitcroft
1980171ae1a4SAndy Whitcroft			my $s = $stat;
1981171ae1a4SAndy Whitcroft			if (defined $cond) {
1982171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
1983171ae1a4SAndy Whitcroft			}
1984171ae1a4SAndy Whitcroft			if ($s =~ /^\s*;/) {
1985de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
1986de7d4f0eSAndy Whitcroft			}
1987de7d4f0eSAndy Whitcroft
1988171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
1989171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
1990171ae1a4SAndy Whitcroft			}
1991*9c9ba34eSAndy Whitcroft
1992*9c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
1993*9c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
1994*9c9ba34eSAndy Whitcroft		{
1995*9c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
1996171ae1a4SAndy Whitcroft		}
1997171ae1a4SAndy Whitcroft
1998de7d4f0eSAndy Whitcroft# checks for new __setup's
1999de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2000de7d4f0eSAndy Whitcroft			my $name = $1;
2001de7d4f0eSAndy Whitcroft
2002de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2003de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2004de7d4f0eSAndy Whitcroft			}
2005653d4876SAndy Whitcroft		}
20069c0ca6f9SAndy Whitcroft
20079c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
20089c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
20099c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
20109c0ca6f9SAndy Whitcroft		}
201113214adfSAndy Whitcroft
201213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
201313214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
201413214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
201513214adfSAndy Whitcroft		}
2016773647a0SAndy Whitcroft
2017773647a0SAndy Whitcroft# check for semaphores used as mutexes
2018171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2019773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2020773647a0SAndy Whitcroft		}
2021773647a0SAndy Whitcroft# check for semaphores used as mutexes
2022171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2023773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2024773647a0SAndy Whitcroft		}
2025773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2026773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2027773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2028773647a0SAndy Whitcroft		}
2029773647a0SAndy Whitcroft
2030773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2031773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2032773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2033171ae1a4SAndy Whitcroft		    $line !~ /^.#\s*if\b.*\bNR_CPUS\b/ &&
2034171ae1a4SAndy Whitcroft		    $line !~ /^.#\s*define\b.*\bNR_CPUS\b/ &&
2035171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2036171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2037171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2038773647a0SAndy Whitcroft		{
2039773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2040773647a0SAndy Whitcroft		}
2041*9c9ba34eSAndy Whitcroft
2042*9c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
2043*9c9ba34eSAndy Whitcroft		my $string;
2044*9c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2045*9c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
2046*9c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
2047*9c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2048*9c9ba34eSAndy Whitcroft				last;
2049*9c9ba34eSAndy Whitcroft			}
2050*9c9ba34eSAndy Whitcroft		}
205113214adfSAndy Whitcroft	}
205213214adfSAndy Whitcroft
205313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
205413214adfSAndy Whitcroft	# so just keep quiet.
205513214adfSAndy Whitcroft	if ($#rawlines == -1) {
205613214adfSAndy Whitcroft		exit(0);
20570a920b5bSAndy Whitcroft	}
20580a920b5bSAndy Whitcroft
20598905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
20608905a67cSAndy Whitcroft	# things that appear to be patches.
20618905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
20628905a67cSAndy Whitcroft		exit(0);
20638905a67cSAndy Whitcroft	}
20648905a67cSAndy Whitcroft
20658905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
20668905a67cSAndy Whitcroft	# just keep quiet.
20678905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
20688905a67cSAndy Whitcroft		exit(0);
20698905a67cSAndy Whitcroft	}
20708905a67cSAndy Whitcroft
20718905a67cSAndy Whitcroft	if (!$is_patch) {
2072de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
20730a920b5bSAndy Whitcroft	}
20740a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2075de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
20760a920b5bSAndy Whitcroft	}
20770a920b5bSAndy Whitcroft
2078f0a594c1SAndy Whitcroft	print report_dump();
207913214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
208013214adfSAndy Whitcroft		print "$filename " if ($summary_file);
20816c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
20826c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
20836c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
20848905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
20856c72ffaaSAndy Whitcroft	}
20868905a67cSAndy Whitcroft
20870a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2088c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
20890a920b5bSAndy Whitcroft	}
20900a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2091c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
20920a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
20930a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
20940a920b5bSAndy Whitcroft	}
209513214adfSAndy Whitcroft
20960a920b5bSAndy Whitcroft	return $clean;
20970a920b5bSAndy Whitcroft}
2098