xref: /linux-6.15/scripts/checkpatch.pl (revision c2010a3b)
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
12773647a0SAndy Whitcroftmy $V = '0.16';
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
1348905a67cSAndy Whitcroftour @typeList = (
1358905a67cSAndy Whitcroft	qr{void},
1368905a67cSAndy Whitcroft	qr{char},
1378905a67cSAndy Whitcroft	qr{short},
1388905a67cSAndy Whitcroft	qr{int},
1398905a67cSAndy Whitcroft	qr{long},
1408905a67cSAndy Whitcroft	qr{unsigned},
1418905a67cSAndy Whitcroft	qr{float},
1428905a67cSAndy Whitcroft	qr{double},
1438905a67cSAndy Whitcroft	qr{bool},
1448905a67cSAndy Whitcroft	qr{long\s+int},
1458905a67cSAndy Whitcroft	qr{long\s+long},
1468905a67cSAndy Whitcroft	qr{long\s+long\s+int},
1478905a67cSAndy Whitcroft	qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
1488905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1498905a67cSAndy Whitcroft	qr{union\s+$Ident},
1508905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1518905a67cSAndy Whitcroft	qr{${Ident}_t},
1528905a67cSAndy Whitcroft	qr{${Ident}_handler},
1538905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1548905a67cSAndy Whitcroft);
1558905a67cSAndy Whitcroft
1568905a67cSAndy Whitcroftsub build_types {
1578905a67cSAndy Whitcroft	my $all = "(?:  \n" . join("|\n  ", @typeList) . "\n)";
1588905a67cSAndy Whitcroft	$NonptrType	= qr{
1598905a67cSAndy Whitcroft			\b
1608905a67cSAndy Whitcroft			(?:const\s+)?
1618905a67cSAndy Whitcroft			(?:unsigned\s+)?
162cf655043SAndy Whitcroft			(?:
163cf655043SAndy Whitcroft				$all|
164cf655043SAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
165cf655043SAndy Whitcroft			)
1668905a67cSAndy Whitcroft			(?:\s+$Sparse|\s+const)*
1678905a67cSAndy Whitcroft			\b
1688905a67cSAndy Whitcroft		  }x;
1698905a67cSAndy Whitcroft	$Type	= qr{
1708905a67cSAndy Whitcroft			\b$NonptrType\b
1718905a67cSAndy Whitcroft			(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
172c2fdda0dSAndy Whitcroft			(?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
1738905a67cSAndy Whitcroft		  }x;
1748905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
1758905a67cSAndy Whitcroft}
1768905a67cSAndy Whitcroftbuild_types();
1776c72ffaaSAndy Whitcroft
1786c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
1790a920b5bSAndy Whitcroft
1804a0df2efSAndy Whitcroftmy @dep_includes = ();
1814a0df2efSAndy Whitcroftmy @dep_functions = ();
1826c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
1836c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
1846c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
1856c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
1860a920b5bSAndy Whitcroft	while (<REMOVE>) {
187f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
188f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
189f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
1904a0df2efSAndy Whitcroft					push(@dep_includes, $1);
1914a0df2efSAndy Whitcroft
192f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
193f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
194f0a594c1SAndy Whitcroft				}
1954a0df2efSAndy Whitcroft			}
1960a920b5bSAndy Whitcroft		}
1970a920b5bSAndy Whitcroft	}
1980a920b5bSAndy Whitcroft}
1990a920b5bSAndy Whitcroft
20000df344fSAndy Whitcroftmy @rawlines = ();
201c2fdda0dSAndy Whitcroftmy @lines = ();
202c2fdda0dSAndy Whitcroftmy $vname;
2036c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2046c72ffaaSAndy Whitcroft	if ($file) {
2056c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2066c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2076c72ffaaSAndy Whitcroft	} else {
2086c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2096c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2106c72ffaaSAndy Whitcroft	}
211c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
212c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
213c2fdda0dSAndy Whitcroft	} else {
214c2fdda0dSAndy Whitcroft		$vname = $filename;
215c2fdda0dSAndy Whitcroft	}
2166c72ffaaSAndy Whitcroft	while (<FILE>) {
2170a920b5bSAndy Whitcroft		chomp;
21800df344fSAndy Whitcroft		push(@rawlines, $_);
2196c72ffaaSAndy Whitcroft	}
2206c72ffaaSAndy Whitcroft	close(FILE);
221c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2220a920b5bSAndy Whitcroft		$exit = 1;
2230a920b5bSAndy Whitcroft	}
22400df344fSAndy Whitcroft	@rawlines = ();
22513214adfSAndy Whitcroft	@lines = ();
2260a920b5bSAndy Whitcroft}
2270a920b5bSAndy Whitcroft
2280a920b5bSAndy Whitcroftexit($exit);
2290a920b5bSAndy Whitcroft
2300a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2316c72ffaaSAndy Whitcroft	my ($root) = @_;
2326c72ffaaSAndy Whitcroft
2336c72ffaaSAndy Whitcroft	my @tree_check = (
2346c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2356c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2366c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2376c72ffaaSAndy Whitcroft	);
2386c72ffaaSAndy Whitcroft
2396c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2406c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2410a920b5bSAndy Whitcroft			return 0;
2420a920b5bSAndy Whitcroft		}
2436c72ffaaSAndy Whitcroft	}
2446c72ffaaSAndy Whitcroft	return 1;
2456c72ffaaSAndy Whitcroft}
2460a920b5bSAndy Whitcroft
2470a920b5bSAndy Whitcroftsub expand_tabs {
2480a920b5bSAndy Whitcroft	my ($str) = @_;
2490a920b5bSAndy Whitcroft
2500a920b5bSAndy Whitcroft	my $res = '';
2510a920b5bSAndy Whitcroft	my $n = 0;
2520a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2530a920b5bSAndy Whitcroft		if ($c eq "\t") {
2540a920b5bSAndy Whitcroft			$res .= ' ';
2550a920b5bSAndy Whitcroft			$n++;
2560a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2570a920b5bSAndy Whitcroft				$res .= ' ';
2580a920b5bSAndy Whitcroft			}
2590a920b5bSAndy Whitcroft			next;
2600a920b5bSAndy Whitcroft		}
2610a920b5bSAndy Whitcroft		$res .= $c;
2620a920b5bSAndy Whitcroft		$n++;
2630a920b5bSAndy Whitcroft	}
2640a920b5bSAndy Whitcroft
2650a920b5bSAndy Whitcroft	return $res;
2660a920b5bSAndy Whitcroft}
2676c72ffaaSAndy Whitcroftsub copy_spacing {
268773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
2696c72ffaaSAndy Whitcroft	return $res;
2706c72ffaaSAndy Whitcroft}
2710a920b5bSAndy Whitcroft
2724a0df2efSAndy Whitcroftsub line_stats {
2734a0df2efSAndy Whitcroft	my ($line) = @_;
2744a0df2efSAndy Whitcroft
2754a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
2764a0df2efSAndy Whitcroft	$line =~ s/^.//;
2774a0df2efSAndy Whitcroft	$line = expand_tabs($line);
2784a0df2efSAndy Whitcroft
2794a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
2804a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
2814a0df2efSAndy Whitcroft
2824a0df2efSAndy Whitcroft	return (length($line), length($white));
2834a0df2efSAndy Whitcroft}
2844a0df2efSAndy Whitcroft
285773647a0SAndy Whitcroftmy $sanitise_quote = '';
286773647a0SAndy Whitcroft
287773647a0SAndy Whitcroftsub sanitise_line_reset {
288773647a0SAndy Whitcroft	my ($in_comment) = @_;
289773647a0SAndy Whitcroft
290773647a0SAndy Whitcroft	if ($in_comment) {
291773647a0SAndy Whitcroft		$sanitise_quote = '*/';
292773647a0SAndy Whitcroft	} else {
293773647a0SAndy Whitcroft		$sanitise_quote = '';
294773647a0SAndy Whitcroft	}
295773647a0SAndy Whitcroft}
29600df344fSAndy Whitcroftsub sanitise_line {
29700df344fSAndy Whitcroft	my ($line) = @_;
29800df344fSAndy Whitcroft
29900df344fSAndy Whitcroft	my $res = '';
30000df344fSAndy Whitcroft	my $l = '';
30100df344fSAndy Whitcroft
302c2fdda0dSAndy Whitcroft	my $qlen = 0;
303773647a0SAndy Whitcroft	my $off = 0;
304773647a0SAndy Whitcroft	my $c;
30500df344fSAndy Whitcroft
306773647a0SAndy Whitcroft	# Always copy over the diff marker.
307773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
308773647a0SAndy Whitcroft
309773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
310773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
311773647a0SAndy Whitcroft
312773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
313773647a0SAndy Whitcroft		# and end, all to $;.
314773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
315773647a0SAndy Whitcroft			$sanitise_quote = '*/';
316773647a0SAndy Whitcroft
317773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
318773647a0SAndy Whitcroft			$off++;
31900df344fSAndy Whitcroft			next;
320773647a0SAndy Whitcroft		}
321773647a0SAndy Whitcroft		if (substr($line, $off, 2) eq $sanitise_quote) {
322773647a0SAndy Whitcroft			$sanitise_quote = '';
323773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
324773647a0SAndy Whitcroft			$off++;
325773647a0SAndy Whitcroft			next;
326773647a0SAndy Whitcroft		}
327773647a0SAndy Whitcroft
328773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
329773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
330773647a0SAndy Whitcroft		    $c eq "\\") {
331773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
332773647a0SAndy Whitcroft			$off++;
333773647a0SAndy Whitcroft			next;
334773647a0SAndy Whitcroft		}
335773647a0SAndy Whitcroft		# Regular quotes.
336773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
337773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
338773647a0SAndy Whitcroft				$sanitise_quote = $c;
339773647a0SAndy Whitcroft
340773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
341773647a0SAndy Whitcroft				next;
342773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
343773647a0SAndy Whitcroft				$sanitise_quote = '';
34400df344fSAndy Whitcroft			}
34500df344fSAndy Whitcroft		}
346773647a0SAndy Whitcroft
347773647a0SAndy Whitcroft		#print "SQ:$sanitise_quote\n";
348773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
349773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
350773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
351773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
35200df344fSAndy Whitcroft		} else {
353773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
35400df344fSAndy Whitcroft		}
355c2fdda0dSAndy Whitcroft	}
356c2fdda0dSAndy Whitcroft
357c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
358c2fdda0dSAndy Whitcroft	if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
359c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
360c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
361c2fdda0dSAndy Whitcroft
362c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
363c2fdda0dSAndy Whitcroft	} elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
364c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
365c2fdda0dSAndy Whitcroft		$res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
366c2fdda0dSAndy Whitcroft	}
367c2fdda0dSAndy Whitcroft
36800df344fSAndy Whitcroft	return $res;
36900df344fSAndy Whitcroft}
37000df344fSAndy Whitcroft
3718905a67cSAndy Whitcroftsub ctx_statement_block {
3728905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3738905a67cSAndy Whitcroft	my $line = $linenr - 1;
3748905a67cSAndy Whitcroft	my $blk = '';
3758905a67cSAndy Whitcroft	my $soff = $off;
3768905a67cSAndy Whitcroft	my $coff = $off - 1;
377773647a0SAndy Whitcroft	my $coff_set = 0;
3788905a67cSAndy Whitcroft
37913214adfSAndy Whitcroft	my $loff = 0;
38013214adfSAndy Whitcroft
3818905a67cSAndy Whitcroft	my $type = '';
3828905a67cSAndy Whitcroft	my $level = 0;
383cf655043SAndy Whitcroft	my $p;
3848905a67cSAndy Whitcroft	my $c;
3858905a67cSAndy Whitcroft	my $len = 0;
38613214adfSAndy Whitcroft
38713214adfSAndy Whitcroft	my $remainder;
3888905a67cSAndy Whitcroft	while (1) {
389773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
3908905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
3918905a67cSAndy Whitcroft		# context.
3928905a67cSAndy Whitcroft		if ($off >= $len) {
3938905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
394c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
3958905a67cSAndy Whitcroft				$remain--;
39613214adfSAndy Whitcroft				$loff = $len;
397c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
3988905a67cSAndy Whitcroft				$len = length($blk);
3998905a67cSAndy Whitcroft				$line++;
4008905a67cSAndy Whitcroft				last;
4018905a67cSAndy Whitcroft			}
4028905a67cSAndy Whitcroft			# Bail if there is no further context.
4038905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
40413214adfSAndy Whitcroft			if ($off >= $len) {
4058905a67cSAndy Whitcroft				last;
4068905a67cSAndy Whitcroft			}
4078905a67cSAndy Whitcroft		}
408cf655043SAndy Whitcroft		$p = $c;
4098905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
41013214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4118905a67cSAndy Whitcroft
412773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4138905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4148905a67cSAndy Whitcroft		# outermost level.
4158905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4168905a67cSAndy Whitcroft			last;
4178905a67cSAndy Whitcroft		}
4188905a67cSAndy Whitcroft
41913214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
420773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
421773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
422773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
423773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
424773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
425773647a0SAndy Whitcroft			$coff_set = 1;
426773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
427773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
42813214adfSAndy Whitcroft		}
42913214adfSAndy Whitcroft
4308905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4318905a67cSAndy Whitcroft			$level++;
4328905a67cSAndy Whitcroft			$type = '(';
4338905a67cSAndy Whitcroft		}
4348905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4358905a67cSAndy Whitcroft			$level--;
4368905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4378905a67cSAndy Whitcroft
4388905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4398905a67cSAndy Whitcroft				$coff = $off;
440773647a0SAndy Whitcroft				$coff_set = 1;
441773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4428905a67cSAndy Whitcroft			}
4438905a67cSAndy Whitcroft		}
4448905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4458905a67cSAndy Whitcroft			$level++;
4468905a67cSAndy Whitcroft			$type = '{';
4478905a67cSAndy Whitcroft		}
4488905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4498905a67cSAndy Whitcroft			$level--;
4508905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4518905a67cSAndy Whitcroft
4528905a67cSAndy Whitcroft			if ($level == 0) {
4538905a67cSAndy Whitcroft				last;
4548905a67cSAndy Whitcroft			}
4558905a67cSAndy Whitcroft		}
4568905a67cSAndy Whitcroft		$off++;
4578905a67cSAndy Whitcroft	}
45813214adfSAndy Whitcroft	if ($off == $len) {
45913214adfSAndy Whitcroft		$line++;
46013214adfSAndy Whitcroft		$remain--;
46113214adfSAndy Whitcroft	}
4628905a67cSAndy Whitcroft
4638905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4648905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4658905a67cSAndy Whitcroft
4668905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4678905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4688905a67cSAndy Whitcroft
469773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
47013214adfSAndy Whitcroft
47113214adfSAndy Whitcroft	return ($statement, $condition,
47213214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
47313214adfSAndy Whitcroft}
47413214adfSAndy Whitcroft
475cf655043SAndy Whitcroftsub statement_lines {
476cf655043SAndy Whitcroft	my ($stmt) = @_;
477cf655043SAndy Whitcroft
478cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
479cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
480cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
481cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
482cf655043SAndy Whitcroft
483cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
484cf655043SAndy Whitcroft
485cf655043SAndy Whitcroft	return $#stmt_lines + 2;
486cf655043SAndy Whitcroft}
487cf655043SAndy Whitcroft
488cf655043SAndy Whitcroftsub statement_rawlines {
489cf655043SAndy Whitcroft	my ($stmt) = @_;
490cf655043SAndy Whitcroft
491cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
492cf655043SAndy Whitcroft
493cf655043SAndy Whitcroft	return $#stmt_lines + 2;
494cf655043SAndy Whitcroft}
495cf655043SAndy Whitcroft
496cf655043SAndy Whitcroftsub statement_block_size {
497cf655043SAndy Whitcroft	my ($stmt) = @_;
498cf655043SAndy Whitcroft
499cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
500cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
501cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
502cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
503cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
504cf655043SAndy Whitcroft
505cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
506cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
507cf655043SAndy Whitcroft
508cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
509cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
510cf655043SAndy Whitcroft
511cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
512cf655043SAndy Whitcroft		return $stmt_lines;
513cf655043SAndy Whitcroft	} else {
514cf655043SAndy Whitcroft		return $stmt_statements;
515cf655043SAndy Whitcroft	}
516cf655043SAndy Whitcroft}
517cf655043SAndy Whitcroft
51813214adfSAndy Whitcroftsub ctx_statement_full {
51913214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
52013214adfSAndy Whitcroft	my ($statement, $condition, $level);
52113214adfSAndy Whitcroft
52213214adfSAndy Whitcroft	my (@chunks);
52313214adfSAndy Whitcroft
524cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
52513214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
52613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
527773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
52813214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
529cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
530cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
531cf655043SAndy Whitcroft	}
532cf655043SAndy Whitcroft
533cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
534cf655043SAndy Whitcroft	# could continue the statement.
535cf655043SAndy Whitcroft	for (;;) {
53613214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
53713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
538cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
539773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
540cf655043SAndy Whitcroft		#print "C: push\n";
541cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
54213214adfSAndy Whitcroft	}
54313214adfSAndy Whitcroft
54413214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5458905a67cSAndy Whitcroft}
5468905a67cSAndy Whitcroft
5474a0df2efSAndy Whitcroftsub ctx_block_get {
548f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5494a0df2efSAndy Whitcroft	my $line;
5504a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5514a0df2efSAndy Whitcroft	my $blk = '';
5524a0df2efSAndy Whitcroft	my @o;
5534a0df2efSAndy Whitcroft	my @c;
5544a0df2efSAndy Whitcroft	my @res = ();
5554a0df2efSAndy Whitcroft
556f0a594c1SAndy Whitcroft	my $level = 0;
55700df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
55800df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
55900df344fSAndy Whitcroft		$remain--;
56000df344fSAndy Whitcroft
56100df344fSAndy Whitcroft		$blk .= $rawlines[$line];
562f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
563f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
564f0a594c1SAndy Whitcroft			if ($off > 0) {
565f0a594c1SAndy Whitcroft				$off--;
566f0a594c1SAndy Whitcroft				next;
567f0a594c1SAndy Whitcroft			}
5684a0df2efSAndy Whitcroft
569f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
570f0a594c1SAndy Whitcroft				$level--;
571f0a594c1SAndy Whitcroft				last if ($level == 0);
572f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
573f0a594c1SAndy Whitcroft				$level++;
574f0a594c1SAndy Whitcroft			}
575f0a594c1SAndy Whitcroft		}
5764a0df2efSAndy Whitcroft
577f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
57800df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
5794a0df2efSAndy Whitcroft		}
5804a0df2efSAndy Whitcroft
581f0a594c1SAndy Whitcroft		last if ($level == 0);
5824a0df2efSAndy Whitcroft	}
5834a0df2efSAndy Whitcroft
584f0a594c1SAndy Whitcroft	return ($level, @res);
5854a0df2efSAndy Whitcroft}
5864a0df2efSAndy Whitcroftsub ctx_block_outer {
5874a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
5884a0df2efSAndy Whitcroft
589f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
590f0a594c1SAndy Whitcroft	return @r;
5914a0df2efSAndy Whitcroft}
5924a0df2efSAndy Whitcroftsub ctx_block {
5934a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
5944a0df2efSAndy Whitcroft
595f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
596f0a594c1SAndy Whitcroft	return @r;
597653d4876SAndy Whitcroft}
598653d4876SAndy Whitcroftsub ctx_statement {
599f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
600f0a594c1SAndy Whitcroft
601f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
602f0a594c1SAndy Whitcroft	return @r;
603f0a594c1SAndy Whitcroft}
604f0a594c1SAndy Whitcroftsub ctx_block_level {
605653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
606653d4876SAndy Whitcroft
607f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6084a0df2efSAndy Whitcroft}
6099c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6109c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6119c0ca6f9SAndy Whitcroft
6129c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6139c0ca6f9SAndy Whitcroft}
6144a0df2efSAndy Whitcroft
6154a0df2efSAndy Whitcroftsub ctx_locate_comment {
6164a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6174a0df2efSAndy Whitcroft
6184a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
61900df344fSAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
6204a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6214a0df2efSAndy Whitcroft
6224a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6234a0df2efSAndy Whitcroft	# comment.
6244a0df2efSAndy Whitcroft	my $in_comment = 0;
6254a0df2efSAndy Whitcroft	$current_comment = '';
6264a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
62700df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
62800df344fSAndy Whitcroft		#warn "           $line\n";
6294a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6304a0df2efSAndy Whitcroft			$in_comment = 1;
6314a0df2efSAndy Whitcroft		}
6324a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6334a0df2efSAndy Whitcroft			$in_comment = 1;
6344a0df2efSAndy Whitcroft		}
6354a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6364a0df2efSAndy Whitcroft			$current_comment = '';
6374a0df2efSAndy Whitcroft		}
6384a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6394a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6404a0df2efSAndy Whitcroft			$in_comment = 0;
6414a0df2efSAndy Whitcroft		}
6424a0df2efSAndy Whitcroft	}
6434a0df2efSAndy Whitcroft
6444a0df2efSAndy Whitcroft	chomp($current_comment);
6454a0df2efSAndy Whitcroft	return($current_comment);
6464a0df2efSAndy Whitcroft}
6474a0df2efSAndy Whitcroftsub ctx_has_comment {
6484a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6494a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6504a0df2efSAndy Whitcroft
65100df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6524a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6534a0df2efSAndy Whitcroft
6544a0df2efSAndy Whitcroft	return ($cmt ne '');
6554a0df2efSAndy Whitcroft}
6564a0df2efSAndy Whitcroft
6570a920b5bSAndy Whitcroftsub cat_vet {
6580a920b5bSAndy Whitcroft	my ($vet) = @_;
6599c0ca6f9SAndy Whitcroft	my ($res, $coded);
6600a920b5bSAndy Whitcroft
6619c0ca6f9SAndy Whitcroft	$res = '';
6626c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
6636c72ffaaSAndy Whitcroft		$res .= $1;
6646c72ffaaSAndy Whitcroft		if ($2 ne '') {
6659c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
6666c72ffaaSAndy Whitcroft			$res .= $coded;
6676c72ffaaSAndy Whitcroft		}
6689c0ca6f9SAndy Whitcroft	}
6699c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
6700a920b5bSAndy Whitcroft
6719c0ca6f9SAndy Whitcroft	return $res;
6720a920b5bSAndy Whitcroft}
6730a920b5bSAndy Whitcroft
674c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
675cf655043SAndy Whitcroftmy $av_pending;
676c2fdda0dSAndy Whitcroftmy @av_paren_type;
677c2fdda0dSAndy Whitcroft
678c2fdda0dSAndy Whitcroftsub annotate_reset {
679c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
680cf655043SAndy Whitcroft	$av_pending = '_';
681cf655043SAndy Whitcroft	@av_paren_type = ('E');
682c2fdda0dSAndy Whitcroft}
683c2fdda0dSAndy Whitcroft
6846c72ffaaSAndy Whitcroftsub annotate_values {
6856c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
6866c72ffaaSAndy Whitcroft
6876c72ffaaSAndy Whitcroft	my $res;
6886c72ffaaSAndy Whitcroft	my $cur = $stream;
6896c72ffaaSAndy Whitcroft
690c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
6916c72ffaaSAndy Whitcroft
6926c72ffaaSAndy Whitcroft	while (length($cur)) {
693773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
694cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
695cf655043SAndy Whitcroft					"> <$type> " if ($dbg_values > 1);
6966c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
697c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
698c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
699cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
700c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7016c72ffaaSAndy Whitcroft			}
7026c72ffaaSAndy Whitcroft
7038905a67cSAndy Whitcroft		} elsif ($cur =~ /^($Type)/) {
704c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7056c72ffaaSAndy Whitcroft			$type = 'T';
7066c72ffaaSAndy Whitcroft
7076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
708c2fdda0dSAndy Whitcroft			print "DEFINE($1)\n" if ($dbg_values > 1);
709c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
710cf655043SAndy Whitcroft			$av_pending = 'N';
7116c72ffaaSAndy Whitcroft
712cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
713cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
714c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
715cf655043SAndy Whitcroft
716cf655043SAndy Whitcroft			push(@av_paren_type, $type);
717cf655043SAndy Whitcroft			push(@av_paren_type, $type);
718cf655043SAndy Whitcroft			$type = 'N';
719cf655043SAndy Whitcroft
720cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
721cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
722cf655043SAndy Whitcroft			$av_preprocessor = 1;
723cf655043SAndy Whitcroft
724cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
725cf655043SAndy Whitcroft
726cf655043SAndy Whitcroft			$type = 'N';
727cf655043SAndy Whitcroft
728cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:endif))/o) {
729cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
730cf655043SAndy Whitcroft
731cf655043SAndy Whitcroft			$av_preprocessor = 1;
732cf655043SAndy Whitcroft
733cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
734cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
735cf655043SAndy Whitcroft			pop(@av_paren_type);
736cf655043SAndy Whitcroft			push(@av_paren_type, $type);
7376c72ffaaSAndy Whitcroft			$type = 'N';
7386c72ffaaSAndy Whitcroft
7396c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
740c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7416c72ffaaSAndy Whitcroft
7426c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
743c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
7446c72ffaaSAndy Whitcroft			if (defined $2) {
745cf655043SAndy Whitcroft				$av_pending = 'V';
7466c72ffaaSAndy Whitcroft			}
7476c72ffaaSAndy Whitcroft			$type = 'N';
7486c72ffaaSAndy Whitcroft
74913214adfSAndy Whitcroft		} elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
750c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
751cf655043SAndy Whitcroft			$av_pending = 'N';
7526c72ffaaSAndy Whitcroft			$type = 'N';
7536c72ffaaSAndy Whitcroft
7546c72ffaaSAndy Whitcroft		} elsif ($cur =~/^(return|case|else)/o) {
755c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
7566c72ffaaSAndy Whitcroft			$type = 'N';
7576c72ffaaSAndy Whitcroft
7586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
759c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
760cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
761cf655043SAndy Whitcroft			$av_pending = '_';
7626c72ffaaSAndy Whitcroft			$type = 'N';
7636c72ffaaSAndy Whitcroft
7646c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
765cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
766cf655043SAndy Whitcroft			if ($new_type ne '_') {
767cf655043SAndy Whitcroft				$type = $new_type;
768c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
769c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
7706c72ffaaSAndy Whitcroft			} else {
771c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
7726c72ffaaSAndy Whitcroft			}
7736c72ffaaSAndy Whitcroft
7746c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident)\(/o) {
775c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
776cf655043SAndy Whitcroft			$av_pending = 'V';
7776c72ffaaSAndy Whitcroft
7786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
779c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
7806c72ffaaSAndy Whitcroft			$type = 'V';
7816c72ffaaSAndy Whitcroft
7826c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
783c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
7846c72ffaaSAndy Whitcroft			$type = 'N';
7856c72ffaaSAndy Whitcroft
786cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
787c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
78813214adfSAndy Whitcroft			$type = 'E';
78913214adfSAndy Whitcroft
790cf655043SAndy Whitcroft		} elsif ($cur =~ /^(;|\?|:|\[)/o) {
79113214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
7926c72ffaaSAndy Whitcroft			$type = 'N';
7936c72ffaaSAndy Whitcroft
7946c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
795c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
7966c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
7976c72ffaaSAndy Whitcroft				$type = 'N';
7986c72ffaaSAndy Whitcroft			}
7996c72ffaaSAndy Whitcroft
8006c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
801c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
8026c72ffaaSAndy Whitcroft		}
8036c72ffaaSAndy Whitcroft		if (defined $1) {
8046c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
8056c72ffaaSAndy Whitcroft			$res .= $type x length($1);
8066c72ffaaSAndy Whitcroft		}
8076c72ffaaSAndy Whitcroft	}
8086c72ffaaSAndy Whitcroft
8096c72ffaaSAndy Whitcroft	return $res;
8106c72ffaaSAndy Whitcroft}
8116c72ffaaSAndy Whitcroft
8128905a67cSAndy Whitcroftsub possible {
81313214adfSAndy Whitcroft	my ($possible, $line) = @_;
8148905a67cSAndy Whitcroft
8158905a67cSAndy Whitcroft	#print "CHECK<$possible>\n";
8168905a67cSAndy Whitcroft	if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
8178905a67cSAndy Whitcroft	    $possible ne 'goto' && $possible ne 'return' &&
8188905a67cSAndy Whitcroft	    $possible ne 'struct' && $possible ne 'enum' &&
8198905a67cSAndy Whitcroft	    $possible ne 'case' && $possible ne 'else' &&
8208905a67cSAndy Whitcroft	    $possible ne 'typedef') {
82113214adfSAndy Whitcroft		warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
8228905a67cSAndy Whitcroft		push(@typeList, $possible);
8238905a67cSAndy Whitcroft		build_types();
8248905a67cSAndy Whitcroft	}
8258905a67cSAndy Whitcroft}
8268905a67cSAndy Whitcroft
8276c72ffaaSAndy Whitcroftmy $prefix = '';
8286c72ffaaSAndy Whitcroft
829f0a594c1SAndy Whitcroftsub report {
830773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
831773647a0SAndy Whitcroft		return 0;
832773647a0SAndy Whitcroft	}
8338905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
8348905a67cSAndy Whitcroft
8358905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
8368905a67cSAndy Whitcroft
83713214adfSAndy Whitcroft	push(our @report, $line);
838773647a0SAndy Whitcroft
839773647a0SAndy Whitcroft	return 1;
840f0a594c1SAndy Whitcroft}
841f0a594c1SAndy Whitcroftsub report_dump {
84213214adfSAndy Whitcroft	our @report;
843f0a594c1SAndy Whitcroft}
844de7d4f0eSAndy Whitcroftsub ERROR {
845773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
846de7d4f0eSAndy Whitcroft		our $clean = 0;
8476c72ffaaSAndy Whitcroft		our $cnt_error++;
848de7d4f0eSAndy Whitcroft	}
849773647a0SAndy Whitcroft}
850de7d4f0eSAndy Whitcroftsub WARN {
851773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
852de7d4f0eSAndy Whitcroft		our $clean = 0;
8536c72ffaaSAndy Whitcroft		our $cnt_warn++;
854de7d4f0eSAndy Whitcroft	}
855773647a0SAndy Whitcroft}
856de7d4f0eSAndy Whitcroftsub CHK {
857773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
858de7d4f0eSAndy Whitcroft		our $clean = 0;
8596c72ffaaSAndy Whitcroft		our $cnt_chk++;
8606c72ffaaSAndy Whitcroft	}
861de7d4f0eSAndy Whitcroft}
862de7d4f0eSAndy Whitcroft
8630a920b5bSAndy Whitcroftsub process {
8640a920b5bSAndy Whitcroft	my $filename = shift;
8650a920b5bSAndy Whitcroft
8660a920b5bSAndy Whitcroft	my $linenr=0;
8670a920b5bSAndy Whitcroft	my $prevline="";
868c2fdda0dSAndy Whitcroft	my $prevrawline="";
8690a920b5bSAndy Whitcroft	my $stashline="";
870c2fdda0dSAndy Whitcroft	my $stashrawline="";
8710a920b5bSAndy Whitcroft
8724a0df2efSAndy Whitcroft	my $length;
8730a920b5bSAndy Whitcroft	my $indent;
8740a920b5bSAndy Whitcroft	my $previndent=0;
8750a920b5bSAndy Whitcroft	my $stashindent=0;
8760a920b5bSAndy Whitcroft
877de7d4f0eSAndy Whitcroft	our $clean = 1;
8780a920b5bSAndy Whitcroft	my $signoff = 0;
8790a920b5bSAndy Whitcroft	my $is_patch = 0;
8800a920b5bSAndy Whitcroft
88113214adfSAndy Whitcroft	our @report = ();
8826c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
8836c72ffaaSAndy Whitcroft	our $cnt_error = 0;
8846c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
8856c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
8866c72ffaaSAndy Whitcroft
8870a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
8880a920b5bSAndy Whitcroft	my $realfile = '';
8890a920b5bSAndy Whitcroft	my $realline = 0;
8900a920b5bSAndy Whitcroft	my $realcnt = 0;
8910a920b5bSAndy Whitcroft	my $here = '';
8920a920b5bSAndy Whitcroft	my $in_comment = 0;
893c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
8940a920b5bSAndy Whitcroft	my $first_line = 0;
8950a920b5bSAndy Whitcroft
89613214adfSAndy Whitcroft	my $prev_values = 'E';
89713214adfSAndy Whitcroft
89813214adfSAndy Whitcroft	# suppression flags
899773647a0SAndy Whitcroft	my %suppress_ifbraces;
900653d4876SAndy Whitcroft
901c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
902de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
903c2fdda0dSAndy Whitcroft	#
904de7d4f0eSAndy Whitcroft	my @setup_docs = ();
905de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
906773647a0SAndy Whitcroft
907773647a0SAndy Whitcroft	sanitise_line_reset();
908c2fdda0dSAndy Whitcroft	my $line;
909c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
910773647a0SAndy Whitcroft		$linenr++;
911773647a0SAndy Whitcroft		$line = $rawline;
912c2fdda0dSAndy Whitcroft
913773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
914de7d4f0eSAndy Whitcroft			$setup_docs = 0;
915de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
916de7d4f0eSAndy Whitcroft				$setup_docs = 1;
917de7d4f0eSAndy Whitcroft			}
918773647a0SAndy Whitcroft			#next;
919de7d4f0eSAndy Whitcroft		}
920773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
921773647a0SAndy Whitcroft			$realline=$1-1;
922773647a0SAndy Whitcroft			if (defined $2) {
923773647a0SAndy Whitcroft				$realcnt=$3+1;
924773647a0SAndy Whitcroft			} else {
925773647a0SAndy Whitcroft				$realcnt=1+1;
926773647a0SAndy Whitcroft			}
927773647a0SAndy Whitcroft
928773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
929773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
930773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
931773647a0SAndy Whitcroft			# at context start.
932773647a0SAndy Whitcroft			my $edge;
933773647a0SAndy Whitcroft			for (my $ln = $linenr; $ln < ($linenr + $realcnt); $ln++) {
934773647a0SAndy Whitcroft				next if ($line =~ /^-/);
935773647a0SAndy Whitcroft				($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
936773647a0SAndy Whitcroft				last if (defined $edge);
937773647a0SAndy Whitcroft			}
938773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
939773647a0SAndy Whitcroft				$in_comment = 1;
940773647a0SAndy Whitcroft			}
941773647a0SAndy Whitcroft
942773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
943773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
944773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
945773647a0SAndy Whitcroft			if (!defined $edge &&
946773647a0SAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
947773647a0SAndy Whitcroft			{
948773647a0SAndy Whitcroft				$in_comment = 1;
949773647a0SAndy Whitcroft			}
950773647a0SAndy Whitcroft
951773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
952773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
953773647a0SAndy Whitcroft
954773647a0SAndy Whitcroft		} elsif ($realcnt) {
955773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
956773647a0SAndy Whitcroft			# simplify matching.
957773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
958773647a0SAndy Whitcroft		}
959773647a0SAndy Whitcroft		push(@lines, $line);
960773647a0SAndy Whitcroft
961773647a0SAndy Whitcroft		if ($realcnt > 1) {
962773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
963773647a0SAndy Whitcroft		} else {
964773647a0SAndy Whitcroft			$realcnt = 0;
965773647a0SAndy Whitcroft		}
966773647a0SAndy Whitcroft
967773647a0SAndy Whitcroft		#print "==>$rawline\n";
968773647a0SAndy Whitcroft		#print "-->$line\n";
969de7d4f0eSAndy Whitcroft
970de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
971de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
972de7d4f0eSAndy Whitcroft		}
973de7d4f0eSAndy Whitcroft	}
974de7d4f0eSAndy Whitcroft
9756c72ffaaSAndy Whitcroft	$prefix = '';
9766c72ffaaSAndy Whitcroft
977773647a0SAndy Whitcroft	$realcnt = 0;
978773647a0SAndy Whitcroft	$linenr = 0;
9790a920b5bSAndy Whitcroft	foreach my $line (@lines) {
9800a920b5bSAndy Whitcroft		$linenr++;
9810a920b5bSAndy Whitcroft
982c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
9836c72ffaaSAndy Whitcroft
9840a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
9856c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
9860a920b5bSAndy Whitcroft			$is_patch = 1;
9874a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
9880a920b5bSAndy Whitcroft			$realline=$1-1;
9890a920b5bSAndy Whitcroft			if (defined $2) {
9900a920b5bSAndy Whitcroft				$realcnt=$3+1;
9910a920b5bSAndy Whitcroft			} else {
9920a920b5bSAndy Whitcroft				$realcnt=1+1;
9930a920b5bSAndy Whitcroft			}
994c2fdda0dSAndy Whitcroft			annotate_reset();
99513214adfSAndy Whitcroft			$prev_values = 'E';
99613214adfSAndy Whitcroft
997773647a0SAndy Whitcroft			%suppress_ifbraces = ();
9980a920b5bSAndy Whitcroft			next;
9990a920b5bSAndy Whitcroft
10004a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
10014a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
10024a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1003773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
10040a920b5bSAndy Whitcroft			$realline++;
1005d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
10060a920b5bSAndy Whitcroft
10074a0df2efSAndy Whitcroft			# Measure the line length and indent.
1008c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
10090a920b5bSAndy Whitcroft
10100a920b5bSAndy Whitcroft			# Track the previous line.
10110a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
10120a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1013c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1014c2fdda0dSAndy Whitcroft
1015773647a0SAndy Whitcroft			#warn "line<$line>\n";
10166c72ffaaSAndy Whitcroft
1017d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1018d8aaf121SAndy Whitcroft			$realcnt--;
10190a920b5bSAndy Whitcroft		}
10200a920b5bSAndy Whitcroft
10210a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1022773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1023773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1024773647a0SAndy Whitcroft
10256c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
10266c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1027773647a0SAndy Whitcroft
1028773647a0SAndy Whitcroft		# extract the filename as it passes
1029773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1030773647a0SAndy Whitcroft			$realfile = $1;
1031773647a0SAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
1032773647a0SAndy Whitcroft
1033773647a0SAndy Whitcroft			if ($realfile =~ m@include/asm/@) {
1034773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1035773647a0SAndy Whitcroft			}
1036773647a0SAndy Whitcroft			next;
1037773647a0SAndy Whitcroft		}
1038773647a0SAndy Whitcroft
1039389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
10400a920b5bSAndy Whitcroft
1041c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1042c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1043c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
10440a920b5bSAndy Whitcroft
10456c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
10466c72ffaaSAndy Whitcroft
10470a920b5bSAndy Whitcroft#check the patch for a signoff:
1048d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
10494a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
10504a0df2efSAndy Whitcroft			$signoff++;
10510a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1052de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1053de7d4f0eSAndy Whitcroft					$herecurr);
10540a920b5bSAndy Whitcroft			}
10550a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1056773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1057de7d4f0eSAndy Whitcroft					$herecurr);
10580a920b5bSAndy Whitcroft			}
10590a920b5bSAndy Whitcroft		}
10600a920b5bSAndy Whitcroft
106100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
10628905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1063de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
10646c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1065de7d4f0eSAndy Whitcroft		}
1066de7d4f0eSAndy Whitcroft
1067de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1068de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1069c2fdda0dSAndy Whitcroft		     !($rawline =~ m/^(
1070de7d4f0eSAndy Whitcroft				[\x09\x0A\x0D\x20-\x7E]              # ASCII
1071de7d4f0eSAndy Whitcroft				| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
1072de7d4f0eSAndy Whitcroft				|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
1073de7d4f0eSAndy Whitcroft				| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
1074de7d4f0eSAndy Whitcroft				|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
1075de7d4f0eSAndy Whitcroft				|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
1076de7d4f0eSAndy Whitcroft				| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
1077de7d4f0eSAndy Whitcroft				|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
1078de7d4f0eSAndy Whitcroft				)*$/x )) {
1079c2fdda0dSAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
108000df344fSAndy Whitcroft		}
10810a920b5bSAndy Whitcroft
108200df344fSAndy Whitcroft#ignore lines being removed
108300df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
108400df344fSAndy Whitcroft
108500df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
108600df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
10870a920b5bSAndy Whitcroft
10880a920b5bSAndy Whitcroft#trailing whitespace
10899c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1090c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
10919c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
10929c0ca6f9SAndy Whitcroft
1093c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1094c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1095de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
10960a920b5bSAndy Whitcroft		}
10970a920b5bSAndy Whitcroft#80 column limit
1098c2fdda0dSAndy Whitcroft		if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
1099de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
11000a920b5bSAndy Whitcroft		}
11010a920b5bSAndy Whitcroft
11028905a67cSAndy Whitcroft# check for adding lines without a newline.
11038905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
11048905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
11058905a67cSAndy Whitcroft		}
11068905a67cSAndy Whitcroft
11070a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
11080a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
11090a920b5bSAndy Whitcroft
11100a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
11110a920b5bSAndy Whitcroft# more than 8 must use tabs.
1112c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1113c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1114c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1115de7d4f0eSAndy Whitcroft			ERROR("use tabs not spaces\n" . $herevet);
11160a920b5bSAndy Whitcroft		}
11170a920b5bSAndy Whitcroft
1118c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1119cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1120c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1121c2fdda0dSAndy Whitcroft		}
112222f2a2efSAndy Whitcroft
11239c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1124c2fdda0dSAndy Whitcroft		if ($realcnt) {
1125cf655043SAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1126cf655043SAndy Whitcroft			$s =~ s/\n./ /g;
1127cf655043SAndy Whitcroft			$s =~ s/{.*$//;
1128cf655043SAndy Whitcroft
1129c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1130cf655043SAndy Whitcroft			if ($s =~ /$Ident:\*$/) {
1131c2fdda0dSAndy Whitcroft
1132c2fdda0dSAndy Whitcroft			# Ignore functions being called
1133cf655043SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/) {
1134c2fdda0dSAndy Whitcroft
11356c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1136cf655043SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1137cf655043SAndy Whitcroft				possible($1, $s);
11388905a67cSAndy Whitcroft
11396c72ffaaSAndy Whitcroft			# declarations always start with types
1140cf655043SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1141cf655043SAndy Whitcroft				possible($1, $s);
1142c2fdda0dSAndy Whitcroft			}
11438905a67cSAndy Whitcroft
11446c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1145cf655043SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1146cf655043SAndy Whitcroft				possible($1, $s);
11479c0ca6f9SAndy Whitcroft			}
11488905a67cSAndy Whitcroft
11498905a67cSAndy Whitcroft			# Check for any sort of function declaration.
11508905a67cSAndy Whitcroft			# int foo(something bar, other baz);
11518905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1152cf655043SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
11538905a67cSAndy Whitcroft				my ($name_len) = length($1);
11548905a67cSAndy Whitcroft
1155cf655043SAndy Whitcroft				my $ctx = $s;
1156773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
11578905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1158cf655043SAndy Whitcroft
11598905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
11608905a67cSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
11618905a67cSAndy Whitcroft
1162cf655043SAndy Whitcroft						possible($1, $s);
11638905a67cSAndy Whitcroft					}
11648905a67cSAndy Whitcroft				}
11658905a67cSAndy Whitcroft			}
11668905a67cSAndy Whitcroft
11679c0ca6f9SAndy Whitcroft		}
11689c0ca6f9SAndy Whitcroft
116900df344fSAndy Whitcroft#
117000df344fSAndy Whitcroft# Checks which may be anchored in the context.
117100df344fSAndy Whitcroft#
117200df344fSAndy Whitcroft
117300df344fSAndy Whitcroft# Check for switch () and associated case and default
117400df344fSAndy Whitcroft# statements should be at the same indent.
117500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
117600df344fSAndy Whitcroft			my $err = '';
117700df344fSAndy Whitcroft			my $sep = '';
117800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
117900df344fSAndy Whitcroft			shift(@ctx);
118000df344fSAndy Whitcroft			for my $ctx (@ctx) {
118100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
118200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
118300df344fSAndy Whitcroft							$indent != $cindent) {
118400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
118500df344fSAndy Whitcroft					$sep = '';
118600df344fSAndy Whitcroft				} else {
118700df344fSAndy Whitcroft					$sep = "[...]\n";
118800df344fSAndy Whitcroft				}
118900df344fSAndy Whitcroft			}
119000df344fSAndy Whitcroft			if ($err ne '') {
11919c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1192de7d4f0eSAndy Whitcroft			}
1193de7d4f0eSAndy Whitcroft		}
1194de7d4f0eSAndy Whitcroft
1195de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1196de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1197773647a0SAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1198773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1199773647a0SAndy Whitcroft
12009c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1201de7d4f0eSAndy Whitcroft			my $ctx_ln = $linenr + $#ctx + 1;
1202de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1203de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1204de7d4f0eSAndy Whitcroft
1205773647a0SAndy Whitcroft			##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1206de7d4f0eSAndy Whitcroft
1207773647a0SAndy Whitcroft			# Skip over any removed lines in the context following statement.
1208773647a0SAndy Whitcroft			while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1209773647a0SAndy Whitcroft				$ctx_ln++;
1210773647a0SAndy Whitcroft			}
1211773647a0SAndy Whitcroft			##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1212773647a0SAndy Whitcroft
1213773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1214773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1215de7d4f0eSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]");
121600df344fSAndy Whitcroft			}
1217773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1218773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1219773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1220773647a0SAndy Whitcroft			{
12219c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
12229c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1223773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
12249c0ca6f9SAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]");
12259c0ca6f9SAndy Whitcroft				}
12269c0ca6f9SAndy Whitcroft			}
122700df344fSAndy Whitcroft		}
122800df344fSAndy Whitcroft
12296c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
12306c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
12316c72ffaaSAndy Whitcroft		my $curr_values = annotate_values($opline . "\n", $prev_values);
12326c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1233c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1234c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1235cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1236cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
1237c2fdda0dSAndy Whitcroft		}
12386c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
12396c72ffaaSAndy Whitcroft
124000df344fSAndy Whitcroft#ignore lines not being added
124100df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
124200df344fSAndy Whitcroft
1243653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
1244653d4876SAndy Whitcroft		if ($tst_type && $line =~ /^.$Declare$/) {
1245de7d4f0eSAndy Whitcroft			ERROR("TEST: is type $Declare\n" . $herecurr);
1246653d4876SAndy Whitcroft			next;
1247653d4876SAndy Whitcroft		}
1248653d4876SAndy Whitcroft
1249f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1250f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1251f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1252773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1253f0a594c1SAndy Whitcroft		}
1254f0a594c1SAndy Whitcroft
125500df344fSAndy Whitcroft#
125600df344fSAndy Whitcroft# Checks which are anchored on the added line.
125700df344fSAndy Whitcroft#
125800df344fSAndy Whitcroft
1259653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1260653d4876SAndy Whitcroft		if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1261653d4876SAndy Whitcroft			my $path = $1;
1262653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1263de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1264de7d4f0eSAndy Whitcroft					$herecurr);
1265653d4876SAndy Whitcroft			}
1266653d4876SAndy Whitcroft		}
1267653d4876SAndy Whitcroft
126800df344fSAndy Whitcroft# no C99 // comments
126900df344fSAndy Whitcroft		if ($line =~ m{//}) {
1270de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
127100df344fSAndy Whitcroft		}
127200df344fSAndy Whitcroft		# Remove C99 comments.
12730a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
12746c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
12750a920b5bSAndy Whitcroft
12760a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1277653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1278653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1279653d4876SAndy Whitcroft			my $name = $1;
12800a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
12810a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
1282653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
1283cf655043SAndy Whitcroft			   ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1284cf655043SAndy Whitcroft			   ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1285cf655043SAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1286de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
12870a920b5bSAndy Whitcroft			}
12880a920b5bSAndy Whitcroft		}
12890a920b5bSAndy Whitcroft
1290f0a594c1SAndy Whitcroft# check for external initialisers.
1291f0a594c1SAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1292f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1293f0a594c1SAndy Whitcroft				$herecurr);
1294f0a594c1SAndy Whitcroft		}
12950a920b5bSAndy Whitcroft# check for static initialisers.
1296f0a594c1SAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1297de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1298de7d4f0eSAndy Whitcroft				$herecurr);
12990a920b5bSAndy Whitcroft		}
13000a920b5bSAndy Whitcroft
1301653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1302653d4876SAndy Whitcroft# make sense.
1303653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
13049c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1305653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1306de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
13070a920b5bSAndy Whitcroft		}
13080a920b5bSAndy Whitcroft
13090a920b5bSAndy Whitcroft# * goes on variable not on type
1310d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1311de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1312de7d4f0eSAndy Whitcroft				$herecurr);
1313d8aaf121SAndy Whitcroft
1314d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1315de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1316de7d4f0eSAndy Whitcroft				$herecurr);
1317d8aaf121SAndy Whitcroft
13189c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1319de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1320de7d4f0eSAndy Whitcroft				$herecurr);
1321d8aaf121SAndy Whitcroft
13229c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1323de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1324de7d4f0eSAndy Whitcroft				$herecurr);
13250a920b5bSAndy Whitcroft		}
13260a920b5bSAndy Whitcroft
13270a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
13280a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
13290a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
13300a920b5bSAndy Whitcroft# 			print "$herecurr";
13310a920b5bSAndy Whitcroft# 			$clean = 0;
13320a920b5bSAndy Whitcroft# 		}
13330a920b5bSAndy Whitcroft
13348905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1335c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
13368905a67cSAndy Whitcroft		}
13378905a67cSAndy Whitcroft
133800df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
133900df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
134000df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
134100df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
134200df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1343f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
134400df344fSAndy Whitcroft			my $ok = 0;
134500df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
134600df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
134700df344fSAndy Whitcroft				# we have a preceeding printk if it ends
134800df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
134900df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
135000df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
135100df344fSAndy Whitcroft						$ok = 1;
135200df344fSAndy Whitcroft					}
135300df344fSAndy Whitcroft					last;
135400df344fSAndy Whitcroft				}
135500df344fSAndy Whitcroft			}
135600df344fSAndy Whitcroft			if ($ok == 0) {
1357de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
13580a920b5bSAndy Whitcroft			}
135900df344fSAndy Whitcroft		}
13600a920b5bSAndy Whitcroft
1361653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1362653d4876SAndy Whitcroft# or if closed on same line
1363c2fdda0dSAndy Whitcroft		if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
13640a920b5bSAndy Whitcroft		    !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1365de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
13660a920b5bSAndy Whitcroft		}
1367653d4876SAndy Whitcroft
13688905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
13698905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
13708905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
13718905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
13728905a67cSAndy Whitcroft		}
13738905a67cSAndy Whitcroft
1374f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
13756c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1376c2fdda0dSAndy Whitcroft			my $name = $1;
1377773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1378773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1379c2fdda0dSAndy Whitcroft
1380c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1381773647a0SAndy Whitcroft			if ($name =~ /^(?:
1382773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1383773647a0SAndy Whitcroft				volatile|__volatile__|
1384773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1385773647a0SAndy Whitcroft				asm|__asm__)$/x)
1386773647a0SAndy Whitcroft			{
1387c2fdda0dSAndy Whitcroft
1388c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1389c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1390c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1391773647a0SAndy Whitcroft			} elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1392773647a0SAndy Whitcroft
1393773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1394773647a0SAndy Whitcroft			} elsif ($ctx =~ /^.\#\s*elif\s*$/) {
1395c2fdda0dSAndy Whitcroft
1396c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1397c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1398773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1399c2fdda0dSAndy Whitcroft
1400c2fdda0dSAndy Whitcroft			} else {
1401773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1402f0a594c1SAndy Whitcroft			}
14036c72ffaaSAndy Whitcroft		}
1404653d4876SAndy Whitcroft# Check operator spacing.
14050a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
14069c0ca6f9SAndy Whitcroft			my $ops = qr{
14079c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
14089c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
14099c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
1410c2fdda0dSAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
14119c0ca6f9SAndy Whitcroft			}x;
1412cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
141300df344fSAndy Whitcroft			my $off = 0;
14146c72ffaaSAndy Whitcroft
14156c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
14166c72ffaaSAndy Whitcroft
14170a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
14184a0df2efSAndy Whitcroft				$off += length($elements[$n]);
14194a0df2efSAndy Whitcroft
1420773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1421773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1422773647a0SAndy Whitcroft				my $cc = '';
1423773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1424773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1425773647a0SAndy Whitcroft				}
1426773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1427773647a0SAndy Whitcroft
14284a0df2efSAndy Whitcroft				my $a = '';
14294a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
14304a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1431cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
14324a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
14334a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1434773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
14354a0df2efSAndy Whitcroft
14360a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
14374a0df2efSAndy Whitcroft
14384a0df2efSAndy Whitcroft				my $c = '';
14390a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
14404a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
14414a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1442cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
14434a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
14444a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
144522f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
14464a0df2efSAndy Whitcroft				} else {
14474a0df2efSAndy Whitcroft					$c = 'E';
14480a920b5bSAndy Whitcroft				}
14490a920b5bSAndy Whitcroft
14504a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
14514a0df2efSAndy Whitcroft
14524a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
14534a0df2efSAndy Whitcroft
14546c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1455de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
14560a920b5bSAndy Whitcroft
14579c0ca6f9SAndy Whitcroft				# Classify operators into binary, unary, or
14589c0ca6f9SAndy Whitcroft				# definitions (* only) where they have more
14599c0ca6f9SAndy Whitcroft				# than one mode.
14606c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
14616c72ffaaSAndy Whitcroft				my $op_left = substr($curr_values, $off, 1);
14626c72ffaaSAndy Whitcroft				my $is_unary;
14636c72ffaaSAndy Whitcroft				if ($op_type eq 'T') {
14649c0ca6f9SAndy Whitcroft					$is_unary = 2;
14656c72ffaaSAndy Whitcroft				} elsif ($op_left eq 'V') {
14666c72ffaaSAndy Whitcroft					$is_unary = 0;
14676c72ffaaSAndy Whitcroft				} else {
14686c72ffaaSAndy Whitcroft					$is_unary = 1;
14699c0ca6f9SAndy Whitcroft				}
14709c0ca6f9SAndy Whitcroft				#if ($op eq '-' || $op eq '&' || $op eq '*') {
14716c72ffaaSAndy Whitcroft				#	print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
14729c0ca6f9SAndy Whitcroft				#}
14730a920b5bSAndy Whitcroft
147413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
147513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
147613214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
147713214adfSAndy Whitcroft
1478cf655043SAndy Whitcroft#				# Ignore comments
1479cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
148013214adfSAndy Whitcroft
1481d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
148213214adfSAndy Whitcroft				} elsif ($op eq ';') {
1483cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1484cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1485773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1486d8aaf121SAndy Whitcroft					}
1487d8aaf121SAndy Whitcroft
1488d8aaf121SAndy Whitcroft				# // is a comment
1489d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
14900a920b5bSAndy Whitcroft
14910a920b5bSAndy Whitcroft				# -> should have no spaces
14920a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
14934a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1494773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
14950a920b5bSAndy Whitcroft					}
14960a920b5bSAndy Whitcroft
14970a920b5bSAndy Whitcroft				# , must have a space on the right.
14980a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1499cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1500773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
15010a920b5bSAndy Whitcroft					}
15020a920b5bSAndy Whitcroft
15039c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
15049c0ca6f9SAndy Whitcroft				} elsif ($op eq '*' && $is_unary == 2) {
15059c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
15069c0ca6f9SAndy Whitcroft
15079c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
15089c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
15099c0ca6f9SAndy Whitcroft				# unary operator, or a cast
15109c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
15119c0ca6f9SAndy Whitcroft				         ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1512cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1513773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
15140a920b5bSAndy Whitcroft					}
15154a0df2efSAndy Whitcroft					if ($ctx =~ /.xW/) {
1516773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
15170a920b5bSAndy Whitcroft					}
15180a920b5bSAndy Whitcroft
15190a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
15200a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1521773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1522773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
15230a920b5bSAndy Whitcroft					}
1524773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1525773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1526773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1527653d4876SAndy Whitcroft					}
1528773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1529773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1530773647a0SAndy Whitcroft					}
1531773647a0SAndy Whitcroft
15320a920b5bSAndy Whitcroft
15330a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
15349c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
15359c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
15369c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1537c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1538c2fdda0dSAndy Whitcroft					 $op eq '%')
15390a920b5bSAndy Whitcroft				{
1540773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1541de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1542de7d4f0eSAndy Whitcroft							$hereptr);
15430a920b5bSAndy Whitcroft					}
15440a920b5bSAndy Whitcroft
15450a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1546cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
154722f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
154822f2a2efSAndy Whitcroft					if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
154922f2a2efSAndy Whitcroft					    !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1550773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
15510a920b5bSAndy Whitcroft					}
155222f2a2efSAndy Whitcroft				}
15534a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
15540a920b5bSAndy Whitcroft			}
15550a920b5bSAndy Whitcroft		}
15560a920b5bSAndy Whitcroft
1557f0a594c1SAndy Whitcroft# check for multiple assignments
1558f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
15596c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1560f0a594c1SAndy Whitcroft		}
1561f0a594c1SAndy Whitcroft
156222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
156322f2a2efSAndy Whitcroft## # continuation.
156422f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
156522f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
156622f2a2efSAndy Whitcroft##
156722f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
156822f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
156922f2a2efSAndy Whitcroft## 			my $ln = $line;
157022f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
157122f2a2efSAndy Whitcroft## 			}
157222f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
157322f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
157422f2a2efSAndy Whitcroft## 			}
157522f2a2efSAndy Whitcroft## 		}
1576f0a594c1SAndy Whitcroft
15770a920b5bSAndy Whitcroft#need space before brace following if, while, etc
157822f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
157922f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1580773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1581de7d4f0eSAndy Whitcroft		}
1582de7d4f0eSAndy Whitcroft
1583de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1584de7d4f0eSAndy Whitcroft# on the line
1585de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1586773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
15870a920b5bSAndy Whitcroft		}
15880a920b5bSAndy Whitcroft
158922f2a2efSAndy Whitcroft# check spacing on square brackets
159022f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1591773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
159222f2a2efSAndy Whitcroft		}
159322f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1594773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
159522f2a2efSAndy Whitcroft		}
159622f2a2efSAndy Whitcroft
159722f2a2efSAndy Whitcroft# check spacing on paretheses
15989c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
15999c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1600773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
160122f2a2efSAndy Whitcroft		}
160213214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
16039c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/) {
1604773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
160522f2a2efSAndy Whitcroft		}
160622f2a2efSAndy Whitcroft
16070a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
16084a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
16090a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1610de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
16110a920b5bSAndy Whitcroft		}
16120a920b5bSAndy Whitcroft
16130a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
16144a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1615773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
16160a920b5bSAndy Whitcroft		}
16170a920b5bSAndy Whitcroft
16180a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional.
16198905a67cSAndy Whitcroft		if ($line =~ /\bif\s*\(/) {
16208905a67cSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
16218905a67cSAndy Whitcroft
16228905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1623c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
16248905a67cSAndy Whitcroft			}
16258905a67cSAndy Whitcroft
16268905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
16278905a67cSAndy Whitcroft			# conditional.
1628773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
16298905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
163013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
1631773647a0SAndy Whitcroft			if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1632773647a0SAndy Whitcroft			    $c !~ /^.\#\s*if/)
1633773647a0SAndy Whitcroft			{
16348905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
16358905a67cSAndy Whitcroft			}
16368905a67cSAndy Whitcroft		}
16378905a67cSAndy Whitcroft
163813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
163913214adfSAndy Whitcroft		if ($line =~ /
164013214adfSAndy Whitcroft			(?:
164113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
164213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
164313214adfSAndy Whitcroft				(?:\&\&|\|\|)
164413214adfSAndy Whitcroft			|
164513214adfSAndy Whitcroft				(?:\&\&|\|\|)
164613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
164713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
164813214adfSAndy Whitcroft			)/x)
164913214adfSAndy Whitcroft		{
165013214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
165113214adfSAndy Whitcroft		}
165213214adfSAndy Whitcroft
16538905a67cSAndy Whitcroft# if and else should not have general statements after it
165413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
165513214adfSAndy Whitcroft			my $s = $1;
165613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
165713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
16588905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
16590a920b5bSAndy Whitcroft			}
166013214adfSAndy Whitcroft		}
16610a920b5bSAndy Whitcroft
16620a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
16630a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
16640a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
16650a920b5bSAndy Whitcroft						$previndent == $indent) {
1666de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
16670a920b5bSAndy Whitcroft		}
16680a920b5bSAndy Whitcroft
1669c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1670c2fdda0dSAndy Whitcroft						$previndent == $indent) {
1671c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1672c2fdda0dSAndy Whitcroft
1673c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
1674c2fdda0dSAndy Whitcroft			# conditional.
1675773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
1676c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
1677c2fdda0dSAndy Whitcroft
1678c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
1679c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
1680c2fdda0dSAndy Whitcroft			}
1681c2fdda0dSAndy Whitcroft		}
1682c2fdda0dSAndy Whitcroft
16830a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
16840a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
16850a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
16860a920b5bSAndy Whitcroft#		    print "$herecurr";
16870a920b5bSAndy Whitcroft#		    $clean = 0;
16880a920b5bSAndy Whitcroft#		}
16890a920b5bSAndy Whitcroft
16900a920b5bSAndy Whitcroft#no spaces allowed after \ in define
16910a920b5bSAndy Whitcroft		if ($line=~/\#define.*\\\s$/) {
1692de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
16930a920b5bSAndy Whitcroft		}
16940a920b5bSAndy Whitcroft
1695653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1696653d4876SAndy Whitcroft		if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
16976c72ffaaSAndy Whitcroft			my $checkfile = "$root/include/linux/$1.h";
16986c72ffaaSAndy Whitcroft			if (-f $checkfile && $1 ne 'irq.h') {
1699773647a0SAndy Whitcroft				WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1700de7d4f0eSAndy Whitcroft					$herecurr);
17010a920b5bSAndy Whitcroft			}
17020a920b5bSAndy Whitcroft		}
17030a920b5bSAndy Whitcroft
1704653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
1705653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
1706cf655043SAndy Whitcroft# in a known good container
17079c0ca6f9SAndy Whitcroft		if ($prevline =~ /\#define.*\\/ &&
17089c0ca6f9SAndy Whitcroft		   $prevline !~/(?:do\s+{|\(\{|\{)/ &&
17099c0ca6f9SAndy Whitcroft		   $line !~ /(?:do\s+{|\(\{|\{)/ &&
17109c0ca6f9SAndy Whitcroft		   $line !~ /^.\s*$Declare\s/) {
1711653d4876SAndy Whitcroft			# Grab the first statement, if that is the entire macro
1712653d4876SAndy Whitcroft			# its ok.  This may start either on the #define line
1713653d4876SAndy Whitcroft			# or the one below.
1714d8aaf121SAndy Whitcroft			my $ln = $linenr;
1715d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
1716f0a594c1SAndy Whitcroft			my $off = 0;
1717653d4876SAndy Whitcroft
1718f0a594c1SAndy Whitcroft			# If the macro starts on the define line start
1719f0a594c1SAndy Whitcroft			# grabbing the statement after the identifier
1720f0a594c1SAndy Whitcroft			$prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1721f0a594c1SAndy Whitcroft			##print "1<$1> 2<$2>\n";
172222f2a2efSAndy Whitcroft			if (defined $2 && $2 ne '') {
1723f0a594c1SAndy Whitcroft				$off = length($1);
1724d8aaf121SAndy Whitcroft				$ln--;
1725d8aaf121SAndy Whitcroft				$cnt++;
17268905a67cSAndy Whitcroft				while ($lines[$ln - 1] =~ /^-/) {
17278905a67cSAndy Whitcroft					$ln--;
17288905a67cSAndy Whitcroft					$cnt++;
17298905a67cSAndy Whitcroft				}
1730d8aaf121SAndy Whitcroft			}
1731f0a594c1SAndy Whitcroft			my @ctx = ctx_statement($ln, $cnt, $off);
1732de7d4f0eSAndy Whitcroft			my $ctx_ln = $ln + $#ctx + 1;
1733de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1734de7d4f0eSAndy Whitcroft
1735de7d4f0eSAndy Whitcroft			# Pull in any empty extension lines.
1736de7d4f0eSAndy Whitcroft			while ($ctx =~ /\\$/ &&
1737de7d4f0eSAndy Whitcroft			       $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1738de7d4f0eSAndy Whitcroft				$ctx .= $lines[$ctx_ln - 1];
1739de7d4f0eSAndy Whitcroft				$ctx_ln++;
1740de7d4f0eSAndy Whitcroft			}
1741d8aaf121SAndy Whitcroft
1742d8aaf121SAndy Whitcroft			if ($ctx =~ /\\$/) {
1743d8aaf121SAndy Whitcroft				if ($ctx =~ /;/) {
1744de7d4f0eSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1745d8aaf121SAndy Whitcroft				} else {
1746de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1747d8aaf121SAndy Whitcroft				}
17480a920b5bSAndy Whitcroft			}
1749653d4876SAndy Whitcroft		}
17500a920b5bSAndy Whitcroft
1751f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
175213214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
175313214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
1754cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
175513214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1756cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1757cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
175813214adfSAndy Whitcroft				my $allowed = 0;
175913214adfSAndy Whitcroft				my $seen = 0;
1760773647a0SAndy Whitcroft				my $herectx = $here . "\n";
1761cf655043SAndy Whitcroft				my $ln = $linenr - 1;
176213214adfSAndy Whitcroft				for my $chunk (@chunks) {
176313214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
176413214adfSAndy Whitcroft
1765773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
1766773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1767773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
1768773647a0SAndy Whitcroft
1769773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1770773647a0SAndy Whitcroft
1771773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
1772773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
1773773647a0SAndy Whitcroft
1774773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1775cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
1776cf655043SAndy Whitcroft
1777773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
177813214adfSAndy Whitcroft
177913214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
178013214adfSAndy Whitcroft
1781cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
1782cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
1783cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
178413214adfSAndy Whitcroft						$allowed = 1;
178513214adfSAndy Whitcroft					}
178613214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
1787cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
178813214adfSAndy Whitcroft						$allowed = 1;
178913214adfSAndy Whitcroft					}
1790cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
1791cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
179213214adfSAndy Whitcroft						$allowed = 1;
179313214adfSAndy Whitcroft					}
179413214adfSAndy Whitcroft				}
179513214adfSAndy Whitcroft				if ($seen && !$allowed) {
1796cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
179713214adfSAndy Whitcroft				}
179813214adfSAndy Whitcroft			}
179913214adfSAndy Whitcroft		}
1800773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
180113214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
1802cf655043SAndy Whitcroft			my $allowed = 0;
1803f0a594c1SAndy Whitcroft
1804cf655043SAndy Whitcroft			# Check the pre-context.
1805cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1806cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
1807cf655043SAndy Whitcroft				$allowed = 1;
1808f0a594c1SAndy Whitcroft			}
1809773647a0SAndy Whitcroft
1810773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
1811773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
1812773647a0SAndy Whitcroft
1813cf655043SAndy Whitcroft			# Check the condition.
1814cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
1815773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
1816cf655043SAndy Whitcroft			if (defined $cond) {
1817773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
1818cf655043SAndy Whitcroft			}
1819cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
1820cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
1821cf655043SAndy Whitcroft				$allowed = 1;
1822cf655043SAndy Whitcroft			}
1823cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
1824cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
1825cf655043SAndy Whitcroft				$allowed = 1;
1826cf655043SAndy Whitcroft			}
1827cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
1828cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
1829cf655043SAndy Whitcroft				$allowed = 1;
1830cf655043SAndy Whitcroft			}
1831cf655043SAndy Whitcroft			# Check the post-context.
1832cf655043SAndy Whitcroft			if (defined $chunks[1]) {
1833cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
1834cf655043SAndy Whitcroft				if (defined $cond) {
1835773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
1836cf655043SAndy Whitcroft				}
1837cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
1838cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
1839cf655043SAndy Whitcroft					$allowed = 1;
1840cf655043SAndy Whitcroft				}
1841cf655043SAndy Whitcroft			}
1842cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1843cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
1844cf655043SAndy Whitcroft				my $end = $linenr + statement_rawlines($block) - 1;
1845cf655043SAndy Whitcroft
1846cf655043SAndy Whitcroft				for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1847cf655043SAndy Whitcroft					$herectx .= $rawlines[$ln] . "\n";;
1848cf655043SAndy Whitcroft				}
1849cf655043SAndy Whitcroft
1850cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1851f0a594c1SAndy Whitcroft			}
1852f0a594c1SAndy Whitcroft		}
1853f0a594c1SAndy Whitcroft
1854653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
18554a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
1856653d4876SAndy Whitcroft			if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1857de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
18580a920b5bSAndy Whitcroft			}
18590a920b5bSAndy Whitcroft		}
18600a920b5bSAndy Whitcroft
18614a0df2efSAndy Whitcroft# don't use deprecated functions
18624a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
186300df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
1864de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
18654a0df2efSAndy Whitcroft			}
18664a0df2efSAndy Whitcroft		}
18674a0df2efSAndy Whitcroft
18684a0df2efSAndy Whitcroft# no volatiles please
18696c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
18706c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1871de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
18724a0df2efSAndy Whitcroft		}
18734a0df2efSAndy Whitcroft
18749c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
18759c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
18769c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
18779c0ca6f9SAndy Whitcroft		}
18789c0ca6f9SAndy Whitcroft
187900df344fSAndy Whitcroft# warn about #if 0
188000df344fSAndy Whitcroft		if ($line =~ /^.#\s*if\s+0\b/) {
1881de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
1882de7d4f0eSAndy Whitcroft				$herecurr);
18834a0df2efSAndy Whitcroft		}
18844a0df2efSAndy Whitcroft
1885f0a594c1SAndy Whitcroft# check for needless kfree() checks
1886f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1887f0a594c1SAndy Whitcroft			my $expr = $1;
1888f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1889f0a594c1SAndy Whitcroft				WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1890f0a594c1SAndy Whitcroft			}
1891f0a594c1SAndy Whitcroft		}
1892*c2010a3bSGreg Kroah-Hartman# check for needless usb_free_urb() checks
1893*c2010a3bSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1894*c2010a3bSGreg Kroah-Hartman			my $expr = $1;
1895*c2010a3bSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1896*c2010a3bSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1897*c2010a3bSGreg Kroah-Hartman			}
1898*c2010a3bSGreg Kroah-Hartman		}
1899f0a594c1SAndy Whitcroft
190000df344fSAndy Whitcroft# warn about #ifdefs in C files
190100df344fSAndy Whitcroft#		if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
190200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
190300df344fSAndy Whitcroft#			print "$herecurr";
190400df344fSAndy Whitcroft#			$clean = 0;
190500df344fSAndy Whitcroft#		}
190600df344fSAndy Whitcroft
190722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
190822f2a2efSAndy Whitcroft		if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
190922f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
191022f2a2efSAndy Whitcroft		}
191122f2a2efSAndy Whitcroft
19124a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
19134a0df2efSAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
19144a0df2efSAndy Whitcroft			my $which = $1;
19154a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1916de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
19174a0df2efSAndy Whitcroft			}
19184a0df2efSAndy Whitcroft		}
19194a0df2efSAndy Whitcroft# check for memory barriers without a comment.
19204a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
19214a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1922de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
19234a0df2efSAndy Whitcroft			}
19244a0df2efSAndy Whitcroft		}
19254a0df2efSAndy Whitcroft# check of hardware specific defines
192622f2a2efSAndy Whitcroft		if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1927de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
19280a920b5bSAndy Whitcroft		}
1929653d4876SAndy Whitcroft
1930de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
1931de7d4f0eSAndy Whitcroft# storage class and type.
19329c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
19339c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
1934de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1935de7d4f0eSAndy Whitcroft		}
1936de7d4f0eSAndy Whitcroft
19378905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
19388905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
19398905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
19408905a67cSAndy Whitcroft		}
19418905a67cSAndy Whitcroft
1942de7d4f0eSAndy Whitcroft# check for new externs in .c files.
1943de7d4f0eSAndy Whitcroft		if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1944de7d4f0eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
1945de7d4f0eSAndy Whitcroft		}
1946de7d4f0eSAndy Whitcroft
1947de7d4f0eSAndy Whitcroft# checks for new __setup's
1948de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
1949de7d4f0eSAndy Whitcroft			my $name = $1;
1950de7d4f0eSAndy Whitcroft
1951de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
1952de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1953de7d4f0eSAndy Whitcroft			}
1954653d4876SAndy Whitcroft		}
19559c0ca6f9SAndy Whitcroft
19569c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
19579c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
19589c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
19599c0ca6f9SAndy Whitcroft		}
196013214adfSAndy Whitcroft
196113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
196213214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
196313214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
196413214adfSAndy Whitcroft		}
1965773647a0SAndy Whitcroft
1966773647a0SAndy Whitcroft# check for semaphores used as mutexes
1967773647a0SAndy Whitcroft		if ($line =~ /\b(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
1968773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
1969773647a0SAndy Whitcroft		}
1970773647a0SAndy Whitcroft# check for semaphores used as mutexes
1971773647a0SAndy Whitcroft		if ($line =~ /\binit_MUTEX_LOCKED\s*\(/) {
1972773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
1973773647a0SAndy Whitcroft		}
1974773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
1975773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
1976773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
1977773647a0SAndy Whitcroft		}
1978773647a0SAndy Whitcroft
1979773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
1980773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
1981773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
1982773647a0SAndy Whitcroft		    $line !~ /^.#\s*define\s+NR_CPUS\s+/ &&
1983773647a0SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/)
1984773647a0SAndy Whitcroft		{
1985773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
1986773647a0SAndy Whitcroft		}
198713214adfSAndy Whitcroft	}
198813214adfSAndy Whitcroft
198913214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
199013214adfSAndy Whitcroft	# so just keep quiet.
199113214adfSAndy Whitcroft	if ($#rawlines == -1) {
199213214adfSAndy Whitcroft		exit(0);
19930a920b5bSAndy Whitcroft	}
19940a920b5bSAndy Whitcroft
19958905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
19968905a67cSAndy Whitcroft	# things that appear to be patches.
19978905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
19988905a67cSAndy Whitcroft		exit(0);
19998905a67cSAndy Whitcroft	}
20008905a67cSAndy Whitcroft
20018905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
20028905a67cSAndy Whitcroft	# just keep quiet.
20038905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
20048905a67cSAndy Whitcroft		exit(0);
20058905a67cSAndy Whitcroft	}
20068905a67cSAndy Whitcroft
20078905a67cSAndy Whitcroft	if (!$is_patch) {
2008de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
20090a920b5bSAndy Whitcroft	}
20100a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2011de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
20120a920b5bSAndy Whitcroft	}
20130a920b5bSAndy Whitcroft
2014f0a594c1SAndy Whitcroft	print report_dump();
201513214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
201613214adfSAndy Whitcroft		print "$filename " if ($summary_file);
20176c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
20186c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
20196c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
20208905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
20216c72ffaaSAndy Whitcroft	}
20228905a67cSAndy Whitcroft
20230a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2024c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
20250a920b5bSAndy Whitcroft	}
20260a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2027c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
20280a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
20290a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
20300a920b5bSAndy Whitcroft	}
203113214adfSAndy Whitcroft
20320a920b5bSAndy Whitcroft	return $clean;
20330a920b5bSAndy Whitcroft}
2034