xref: /linux-6.15/scripts/checkpatch.pl (revision 7429c690)
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
126cbb2e71SAndy Whitcroftmy $V = '0.20';
130a920b5bSAndy Whitcroft
140a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftmy $quiet = 0;
170a920b5bSAndy Whitcroftmy $tree = 1;
180a920b5bSAndy Whitcroftmy $chk_signoff = 1;
190a920b5bSAndy Whitcroftmy $chk_patch = 1;
20773647a0SAndy Whitcroftmy $tst_only;
216c72ffaaSAndy Whitcroftmy $emacs = 0;
228905a67cSAndy Whitcroftmy $terse = 0;
236c72ffaaSAndy Whitcroftmy $file = 0;
246c72ffaaSAndy Whitcroftmy $check = 0;
258905a67cSAndy Whitcroftmy $summary = 1;
268905a67cSAndy Whitcroftmy $mailback = 0;
2713214adfSAndy Whitcroftmy $summary_file = 0;
286c72ffaaSAndy Whitcroftmy $root;
29c2fdda0dSAndy Whitcroftmy %debug;
300a920b5bSAndy WhitcroftGetOptions(
316c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
320a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
330a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
340a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
356c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
368905a67cSAndy Whitcroft	'terse!'	=> \$terse,
376c72ffaaSAndy Whitcroft	'file!'		=> \$file,
386c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
396c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
406c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
418905a67cSAndy Whitcroft	'summary!'	=> \$summary,
428905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
4313214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
4413214adfSAndy Whitcroft
45c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
46773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
470a920b5bSAndy Whitcroft) or exit;
480a920b5bSAndy Whitcroft
490a920b5bSAndy Whitcroftmy $exit = 0;
500a920b5bSAndy Whitcroft
510a920b5bSAndy Whitcroftif ($#ARGV < 0) {
5200df344fSAndy Whitcroft	print "usage: $P [options] patchfile\n";
530a920b5bSAndy Whitcroft	print "version: $V\n";
540a920b5bSAndy Whitcroft	print "options: -q               => quiet\n";
550a920b5bSAndy Whitcroft	print "         --no-tree        => run without a kernel tree\n";
568905a67cSAndy Whitcroft	print "         --terse          => one line per report\n";
576c72ffaaSAndy Whitcroft	print "         --emacs          => emacs compile window format\n";
586c72ffaaSAndy Whitcroft	print "         --file           => check a source file\n";
596c72ffaaSAndy Whitcroft	print "         --strict         => enable more subjective tests\n";
606c72ffaaSAndy Whitcroft	print "         --root           => path to the kernel tree root\n";
6113214adfSAndy Whitcroft	print "         --no-summary     => suppress the per-file summary\n";
6213214adfSAndy Whitcroft	print "         --summary-file   => include the filename in summary\n";
630a920b5bSAndy Whitcroft	exit(1);
640a920b5bSAndy Whitcroft}
650a920b5bSAndy Whitcroft
66c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
67c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
68*7429c690SAndy Whitcroftmy $dbg_type = 0;
69c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
70c2fdda0dSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';"
71c2fdda0dSAndy Whitcroft}
72c2fdda0dSAndy Whitcroft
738905a67cSAndy Whitcroftif ($terse) {
748905a67cSAndy Whitcroft	$emacs = 1;
758905a67cSAndy Whitcroft	$quiet++;
768905a67cSAndy Whitcroft}
778905a67cSAndy Whitcroft
786c72ffaaSAndy Whitcroftif ($tree) {
796c72ffaaSAndy Whitcroft	if (defined $root) {
806c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
816c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
826c72ffaaSAndy Whitcroft		}
836c72ffaaSAndy Whitcroft	} else {
846c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
856c72ffaaSAndy Whitcroft			$root = '.';
866c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
876c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
886c72ffaaSAndy Whitcroft			$root = $1;
896c72ffaaSAndy Whitcroft		}
906c72ffaaSAndy Whitcroft	}
916c72ffaaSAndy Whitcroft
926c72ffaaSAndy Whitcroft	if (!defined $root) {
930a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
940a920b5bSAndy Whitcroft		exit(2);
950a920b5bSAndy Whitcroft	}
966c72ffaaSAndy Whitcroft}
976c72ffaaSAndy Whitcroft
986c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
996c72ffaaSAndy Whitcroft
1006c72ffaaSAndy Whitcroftour $Ident       = qr{[A-Za-z_][A-Za-z\d_]*};
1016c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1026c72ffaaSAndy Whitcroftour $Sparse	= qr{
1036c72ffaaSAndy Whitcroft			__user|
1046c72ffaaSAndy Whitcroft			__kernel|
1056c72ffaaSAndy Whitcroft			__force|
1066c72ffaaSAndy Whitcroft			__iomem|
1076c72ffaaSAndy Whitcroft			__must_check|
1086c72ffaaSAndy Whitcroft			__init_refok|
109cf655043SAndy Whitcroft			__kprobes
1106c72ffaaSAndy Whitcroft		}x;
1116c72ffaaSAndy Whitcroftour $Attribute	= qr{
1126c72ffaaSAndy Whitcroft			const|
1136c72ffaaSAndy Whitcroft			__read_mostly|
1146c72ffaaSAndy Whitcroft			__kprobes|
1156c72ffaaSAndy Whitcroft			__(?:mem|cpu|dev|)(?:initdata|init)
1166c72ffaaSAndy Whitcroft		  }x;
117c45dcabdSAndy Whitcroftour $Modifier;
1186c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1196c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1206c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1216c72ffaaSAndy Whitcroft
1226c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1236c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
1246c72ffaaSAndy Whitcroftour $Operators	= qr{
1256c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1266c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
127c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1286c72ffaaSAndy Whitcroft		  }x;
1296c72ffaaSAndy Whitcroft
1308905a67cSAndy Whitcroftour $NonptrType;
1318905a67cSAndy Whitcroftour $Type;
1328905a67cSAndy Whitcroftour $Declare;
1338905a67cSAndy Whitcroft
134171ae1a4SAndy Whitcroftour $UTF8	= qr {
135171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
136171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
137171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
138171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
139171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
140171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
141171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
142171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
143171ae1a4SAndy Whitcroft}x;
144171ae1a4SAndy Whitcroft
1458905a67cSAndy Whitcroftour @typeList = (
1468905a67cSAndy Whitcroft	qr{void},
147c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
148c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
149c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
150c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
151c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
152c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
153c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
1548905a67cSAndy Whitcroft	qr{unsigned},
1558905a67cSAndy Whitcroft	qr{float},
1568905a67cSAndy Whitcroft	qr{double},
1578905a67cSAndy Whitcroft	qr{bool},
1588905a67cSAndy Whitcroft	qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
1598905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1608905a67cSAndy Whitcroft	qr{union\s+$Ident},
1618905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1628905a67cSAndy Whitcroft	qr{${Ident}_t},
1638905a67cSAndy Whitcroft	qr{${Ident}_handler},
1648905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1658905a67cSAndy Whitcroft);
166c45dcabdSAndy Whitcroftour @modifierList = (
167c45dcabdSAndy Whitcroft	qr{fastcall},
168c45dcabdSAndy Whitcroft);
1698905a67cSAndy Whitcroft
1708905a67cSAndy Whitcroftsub build_types {
171c45dcabdSAndy Whitcroft	my $mods = "(?:  \n" . join("|\n  ", @modifierList) . "\n)";
1728905a67cSAndy Whitcroft	my $all = "(?:  \n" . join("|\n  ", @typeList) . "\n)";
173c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
1748905a67cSAndy Whitcroft	$NonptrType	= qr{
1758905a67cSAndy Whitcroft			(?:const\s+)?
176c45dcabdSAndy Whitcroft			(?:$mods\s+)?
177cf655043SAndy Whitcroft			(?:
178c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
179c45dcabdSAndy Whitcroft				(?:${all}\b)
180cf655043SAndy Whitcroft			)
181c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
1828905a67cSAndy Whitcroft		  }x;
1838905a67cSAndy Whitcroft	$Type	= qr{
184c45dcabdSAndy Whitcroft			$NonptrType
1858905a67cSAndy Whitcroft			(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
186c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
1878905a67cSAndy Whitcroft		  }x;
1888905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
1898905a67cSAndy Whitcroft}
1908905a67cSAndy Whitcroftbuild_types();
1916c72ffaaSAndy Whitcroft
1926c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
1930a920b5bSAndy Whitcroft
1944a0df2efSAndy Whitcroftmy @dep_includes = ();
1954a0df2efSAndy Whitcroftmy @dep_functions = ();
1966c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
1976c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
1986c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
1996c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
2000a920b5bSAndy Whitcroft	while (<REMOVE>) {
201f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
202f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
203f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2044a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2054a0df2efSAndy Whitcroft
206f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
207f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
208f0a594c1SAndy Whitcroft				}
2094a0df2efSAndy Whitcroft			}
2100a920b5bSAndy Whitcroft		}
2110a920b5bSAndy Whitcroft	}
2120a920b5bSAndy Whitcroft}
2130a920b5bSAndy Whitcroft
21400df344fSAndy Whitcroftmy @rawlines = ();
215c2fdda0dSAndy Whitcroftmy @lines = ();
216c2fdda0dSAndy Whitcroftmy $vname;
2176c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2186c72ffaaSAndy Whitcroft	if ($file) {
2196c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2206c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2216c72ffaaSAndy Whitcroft	} else {
2226c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2236c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2246c72ffaaSAndy Whitcroft	}
225c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
226c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
227c2fdda0dSAndy Whitcroft	} else {
228c2fdda0dSAndy Whitcroft		$vname = $filename;
229c2fdda0dSAndy Whitcroft	}
2306c72ffaaSAndy Whitcroft	while (<FILE>) {
2310a920b5bSAndy Whitcroft		chomp;
23200df344fSAndy Whitcroft		push(@rawlines, $_);
2336c72ffaaSAndy Whitcroft	}
2346c72ffaaSAndy Whitcroft	close(FILE);
235c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2360a920b5bSAndy Whitcroft		$exit = 1;
2370a920b5bSAndy Whitcroft	}
23800df344fSAndy Whitcroft	@rawlines = ();
23913214adfSAndy Whitcroft	@lines = ();
2400a920b5bSAndy Whitcroft}
2410a920b5bSAndy Whitcroft
2420a920b5bSAndy Whitcroftexit($exit);
2430a920b5bSAndy Whitcroft
2440a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2456c72ffaaSAndy Whitcroft	my ($root) = @_;
2466c72ffaaSAndy Whitcroft
2476c72ffaaSAndy Whitcroft	my @tree_check = (
2486c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2496c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2506c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2516c72ffaaSAndy Whitcroft	);
2526c72ffaaSAndy Whitcroft
2536c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2546c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2550a920b5bSAndy Whitcroft			return 0;
2560a920b5bSAndy Whitcroft		}
2576c72ffaaSAndy Whitcroft	}
2586c72ffaaSAndy Whitcroft	return 1;
2596c72ffaaSAndy Whitcroft}
2600a920b5bSAndy Whitcroft
2610a920b5bSAndy Whitcroftsub expand_tabs {
2620a920b5bSAndy Whitcroft	my ($str) = @_;
2630a920b5bSAndy Whitcroft
2640a920b5bSAndy Whitcroft	my $res = '';
2650a920b5bSAndy Whitcroft	my $n = 0;
2660a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2670a920b5bSAndy Whitcroft		if ($c eq "\t") {
2680a920b5bSAndy Whitcroft			$res .= ' ';
2690a920b5bSAndy Whitcroft			$n++;
2700a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2710a920b5bSAndy Whitcroft				$res .= ' ';
2720a920b5bSAndy Whitcroft			}
2730a920b5bSAndy Whitcroft			next;
2740a920b5bSAndy Whitcroft		}
2750a920b5bSAndy Whitcroft		$res .= $c;
2760a920b5bSAndy Whitcroft		$n++;
2770a920b5bSAndy Whitcroft	}
2780a920b5bSAndy Whitcroft
2790a920b5bSAndy Whitcroft	return $res;
2800a920b5bSAndy Whitcroft}
2816c72ffaaSAndy Whitcroftsub copy_spacing {
282773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
2836c72ffaaSAndy Whitcroft	return $res;
2846c72ffaaSAndy Whitcroft}
2850a920b5bSAndy Whitcroft
2864a0df2efSAndy Whitcroftsub line_stats {
2874a0df2efSAndy Whitcroft	my ($line) = @_;
2884a0df2efSAndy Whitcroft
2894a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
2904a0df2efSAndy Whitcroft	$line =~ s/^.//;
2914a0df2efSAndy Whitcroft	$line = expand_tabs($line);
2924a0df2efSAndy Whitcroft
2934a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
2944a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
2954a0df2efSAndy Whitcroft
2964a0df2efSAndy Whitcroft	return (length($line), length($white));
2974a0df2efSAndy Whitcroft}
2984a0df2efSAndy Whitcroft
299773647a0SAndy Whitcroftmy $sanitise_quote = '';
300773647a0SAndy Whitcroft
301773647a0SAndy Whitcroftsub sanitise_line_reset {
302773647a0SAndy Whitcroft	my ($in_comment) = @_;
303773647a0SAndy Whitcroft
304773647a0SAndy Whitcroft	if ($in_comment) {
305773647a0SAndy Whitcroft		$sanitise_quote = '*/';
306773647a0SAndy Whitcroft	} else {
307773647a0SAndy Whitcroft		$sanitise_quote = '';
308773647a0SAndy Whitcroft	}
309773647a0SAndy Whitcroft}
31000df344fSAndy Whitcroftsub sanitise_line {
31100df344fSAndy Whitcroft	my ($line) = @_;
31200df344fSAndy Whitcroft
31300df344fSAndy Whitcroft	my $res = '';
31400df344fSAndy Whitcroft	my $l = '';
31500df344fSAndy Whitcroft
316c2fdda0dSAndy Whitcroft	my $qlen = 0;
317773647a0SAndy Whitcroft	my $off = 0;
318773647a0SAndy Whitcroft	my $c;
31900df344fSAndy Whitcroft
320773647a0SAndy Whitcroft	# Always copy over the diff marker.
321773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
322773647a0SAndy Whitcroft
323773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
324773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
325773647a0SAndy Whitcroft
326773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
327773647a0SAndy Whitcroft		# and end, all to $;.
328773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
329773647a0SAndy Whitcroft			$sanitise_quote = '*/';
330773647a0SAndy Whitcroft
331773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
332773647a0SAndy Whitcroft			$off++;
33300df344fSAndy Whitcroft			next;
334773647a0SAndy Whitcroft		}
335c45dcabdSAndy Whitcroft		if (substr($line, $off, 2) eq '*/') {
336773647a0SAndy Whitcroft			$sanitise_quote = '';
337773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
338773647a0SAndy Whitcroft			$off++;
339773647a0SAndy Whitcroft			next;
340773647a0SAndy Whitcroft		}
341773647a0SAndy Whitcroft
342773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
343773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
344773647a0SAndy Whitcroft		    $c eq "\\") {
345773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
346773647a0SAndy Whitcroft			$off++;
347773647a0SAndy Whitcroft			next;
348773647a0SAndy Whitcroft		}
349773647a0SAndy Whitcroft		# Regular quotes.
350773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
351773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
352773647a0SAndy Whitcroft				$sanitise_quote = $c;
353773647a0SAndy Whitcroft
354773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
355773647a0SAndy Whitcroft				next;
356773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
357773647a0SAndy Whitcroft				$sanitise_quote = '';
35800df344fSAndy Whitcroft			}
35900df344fSAndy Whitcroft		}
360773647a0SAndy Whitcroft
361773647a0SAndy Whitcroft		#print "SQ:$sanitise_quote\n";
362773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
363773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
364773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
365773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
36600df344fSAndy Whitcroft		} else {
367773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
36800df344fSAndy Whitcroft		}
369c2fdda0dSAndy Whitcroft	}
370c2fdda0dSAndy Whitcroft
371c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
372c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
373c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
374c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
375c2fdda0dSAndy Whitcroft
376c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
377c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
378c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
379c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
380c2fdda0dSAndy Whitcroft	}
381c2fdda0dSAndy Whitcroft
38200df344fSAndy Whitcroft	return $res;
38300df344fSAndy Whitcroft}
38400df344fSAndy Whitcroft
3858905a67cSAndy Whitcroftsub ctx_statement_block {
3868905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3878905a67cSAndy Whitcroft	my $line = $linenr - 1;
3888905a67cSAndy Whitcroft	my $blk = '';
3898905a67cSAndy Whitcroft	my $soff = $off;
3908905a67cSAndy Whitcroft	my $coff = $off - 1;
391773647a0SAndy Whitcroft	my $coff_set = 0;
3928905a67cSAndy Whitcroft
39313214adfSAndy Whitcroft	my $loff = 0;
39413214adfSAndy Whitcroft
3958905a67cSAndy Whitcroft	my $type = '';
3968905a67cSAndy Whitcroft	my $level = 0;
397cf655043SAndy Whitcroft	my $p;
3988905a67cSAndy Whitcroft	my $c;
3998905a67cSAndy Whitcroft	my $len = 0;
40013214adfSAndy Whitcroft
40113214adfSAndy Whitcroft	my $remainder;
4028905a67cSAndy Whitcroft	while (1) {
403773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4048905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4058905a67cSAndy Whitcroft		# context.
4068905a67cSAndy Whitcroft		if ($off >= $len) {
4078905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
408c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4098905a67cSAndy Whitcroft				$remain--;
41013214adfSAndy Whitcroft				$loff = $len;
411c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4128905a67cSAndy Whitcroft				$len = length($blk);
4138905a67cSAndy Whitcroft				$line++;
4148905a67cSAndy Whitcroft				last;
4158905a67cSAndy Whitcroft			}
4168905a67cSAndy Whitcroft			# Bail if there is no further context.
4178905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
41813214adfSAndy Whitcroft			if ($off >= $len) {
4198905a67cSAndy Whitcroft				last;
4208905a67cSAndy Whitcroft			}
4218905a67cSAndy Whitcroft		}
422cf655043SAndy Whitcroft		$p = $c;
4238905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
42413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4258905a67cSAndy Whitcroft
426773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4278905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4288905a67cSAndy Whitcroft		# outermost level.
4298905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4308905a67cSAndy Whitcroft			last;
4318905a67cSAndy Whitcroft		}
4328905a67cSAndy Whitcroft
43313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
434773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
435773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
436773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
437773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
438773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
439773647a0SAndy Whitcroft			$coff_set = 1;
440773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
441773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
44213214adfSAndy Whitcroft		}
44313214adfSAndy Whitcroft
4448905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4458905a67cSAndy Whitcroft			$level++;
4468905a67cSAndy Whitcroft			$type = '(';
4478905a67cSAndy Whitcroft		}
4488905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4498905a67cSAndy Whitcroft			$level--;
4508905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4518905a67cSAndy Whitcroft
4528905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4538905a67cSAndy Whitcroft				$coff = $off;
454773647a0SAndy Whitcroft				$coff_set = 1;
455773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4568905a67cSAndy Whitcroft			}
4578905a67cSAndy Whitcroft		}
4588905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4598905a67cSAndy Whitcroft			$level++;
4608905a67cSAndy Whitcroft			$type = '{';
4618905a67cSAndy Whitcroft		}
4628905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4638905a67cSAndy Whitcroft			$level--;
4648905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4658905a67cSAndy Whitcroft
4668905a67cSAndy Whitcroft			if ($level == 0) {
4678905a67cSAndy Whitcroft				last;
4688905a67cSAndy Whitcroft			}
4698905a67cSAndy Whitcroft		}
4708905a67cSAndy Whitcroft		$off++;
4718905a67cSAndy Whitcroft	}
472a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
47313214adfSAndy Whitcroft	if ($off == $len) {
474a3bb97a7SAndy Whitcroft		$loff = $len + 1;
47513214adfSAndy Whitcroft		$line++;
47613214adfSAndy Whitcroft		$remain--;
47713214adfSAndy Whitcroft	}
4788905a67cSAndy Whitcroft
4798905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4808905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4818905a67cSAndy Whitcroft
4828905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4838905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4848905a67cSAndy Whitcroft
485773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
48613214adfSAndy Whitcroft
48713214adfSAndy Whitcroft	return ($statement, $condition,
48813214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
48913214adfSAndy Whitcroft}
49013214adfSAndy Whitcroft
491cf655043SAndy Whitcroftsub statement_lines {
492cf655043SAndy Whitcroft	my ($stmt) = @_;
493cf655043SAndy Whitcroft
494cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
495cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
496cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
497cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
498cf655043SAndy Whitcroft
499cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
500cf655043SAndy Whitcroft
501cf655043SAndy Whitcroft	return $#stmt_lines + 2;
502cf655043SAndy Whitcroft}
503cf655043SAndy Whitcroft
504cf655043SAndy Whitcroftsub statement_rawlines {
505cf655043SAndy Whitcroft	my ($stmt) = @_;
506cf655043SAndy Whitcroft
507cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
508cf655043SAndy Whitcroft
509cf655043SAndy Whitcroft	return $#stmt_lines + 2;
510cf655043SAndy Whitcroft}
511cf655043SAndy Whitcroft
512cf655043SAndy Whitcroftsub statement_block_size {
513cf655043SAndy Whitcroft	my ($stmt) = @_;
514cf655043SAndy Whitcroft
515cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
516cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
517cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
518cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
519cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
520cf655043SAndy Whitcroft
521cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
522cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
523cf655043SAndy Whitcroft
524cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
525cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
526cf655043SAndy Whitcroft
527cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
528cf655043SAndy Whitcroft		return $stmt_lines;
529cf655043SAndy Whitcroft	} else {
530cf655043SAndy Whitcroft		return $stmt_statements;
531cf655043SAndy Whitcroft	}
532cf655043SAndy Whitcroft}
533cf655043SAndy Whitcroft
53413214adfSAndy Whitcroftsub ctx_statement_full {
53513214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
53613214adfSAndy Whitcroft	my ($statement, $condition, $level);
53713214adfSAndy Whitcroft
53813214adfSAndy Whitcroft	my (@chunks);
53913214adfSAndy Whitcroft
540cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
54113214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
54213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
543773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
54413214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
545cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
546cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
547cf655043SAndy Whitcroft	}
548cf655043SAndy Whitcroft
549cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
550cf655043SAndy Whitcroft	# could continue the statement.
551cf655043SAndy Whitcroft	for (;;) {
55213214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
55313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
554cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
555773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
556cf655043SAndy Whitcroft		#print "C: push\n";
557cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
55813214adfSAndy Whitcroft	}
55913214adfSAndy Whitcroft
56013214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5618905a67cSAndy Whitcroft}
5628905a67cSAndy Whitcroft
5634a0df2efSAndy Whitcroftsub ctx_block_get {
564f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5654a0df2efSAndy Whitcroft	my $line;
5664a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5674a0df2efSAndy Whitcroft	my $blk = '';
5684a0df2efSAndy Whitcroft	my @o;
5694a0df2efSAndy Whitcroft	my @c;
5704a0df2efSAndy Whitcroft	my @res = ();
5714a0df2efSAndy Whitcroft
572f0a594c1SAndy Whitcroft	my $level = 0;
57300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
57400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
57500df344fSAndy Whitcroft		$remain--;
57600df344fSAndy Whitcroft
57700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
578f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
579f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
580f0a594c1SAndy Whitcroft			if ($off > 0) {
581f0a594c1SAndy Whitcroft				$off--;
582f0a594c1SAndy Whitcroft				next;
583f0a594c1SAndy Whitcroft			}
5844a0df2efSAndy Whitcroft
585f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
586f0a594c1SAndy Whitcroft				$level--;
587f0a594c1SAndy Whitcroft				last if ($level == 0);
588f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
589f0a594c1SAndy Whitcroft				$level++;
590f0a594c1SAndy Whitcroft			}
591f0a594c1SAndy Whitcroft		}
5924a0df2efSAndy Whitcroft
593f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
59400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
5954a0df2efSAndy Whitcroft		}
5964a0df2efSAndy Whitcroft
597f0a594c1SAndy Whitcroft		last if ($level == 0);
5984a0df2efSAndy Whitcroft	}
5994a0df2efSAndy Whitcroft
600f0a594c1SAndy Whitcroft	return ($level, @res);
6014a0df2efSAndy Whitcroft}
6024a0df2efSAndy Whitcroftsub ctx_block_outer {
6034a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6044a0df2efSAndy Whitcroft
605f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
606f0a594c1SAndy Whitcroft	return @r;
6074a0df2efSAndy Whitcroft}
6084a0df2efSAndy Whitcroftsub ctx_block {
6094a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6104a0df2efSAndy Whitcroft
611f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
612f0a594c1SAndy Whitcroft	return @r;
613653d4876SAndy Whitcroft}
614653d4876SAndy Whitcroftsub ctx_statement {
615f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
616f0a594c1SAndy Whitcroft
617f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
618f0a594c1SAndy Whitcroft	return @r;
619f0a594c1SAndy Whitcroft}
620f0a594c1SAndy Whitcroftsub ctx_block_level {
621653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
622653d4876SAndy Whitcroft
623f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6244a0df2efSAndy Whitcroft}
6259c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6269c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6279c0ca6f9SAndy Whitcroft
6289c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6299c0ca6f9SAndy Whitcroft}
6304a0df2efSAndy Whitcroft
6314a0df2efSAndy Whitcroftsub ctx_locate_comment {
6324a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6334a0df2efSAndy Whitcroft
6344a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
635beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
6364a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6374a0df2efSAndy Whitcroft
6384a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6394a0df2efSAndy Whitcroft	# comment.
6404a0df2efSAndy Whitcroft	my $in_comment = 0;
6414a0df2efSAndy Whitcroft	$current_comment = '';
6424a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
64300df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
64400df344fSAndy Whitcroft		#warn "           $line\n";
6454a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6464a0df2efSAndy Whitcroft			$in_comment = 1;
6474a0df2efSAndy Whitcroft		}
6484a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6494a0df2efSAndy Whitcroft			$in_comment = 1;
6504a0df2efSAndy Whitcroft		}
6514a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6524a0df2efSAndy Whitcroft			$current_comment = '';
6534a0df2efSAndy Whitcroft		}
6544a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6554a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6564a0df2efSAndy Whitcroft			$in_comment = 0;
6574a0df2efSAndy Whitcroft		}
6584a0df2efSAndy Whitcroft	}
6594a0df2efSAndy Whitcroft
6604a0df2efSAndy Whitcroft	chomp($current_comment);
6614a0df2efSAndy Whitcroft	return($current_comment);
6624a0df2efSAndy Whitcroft}
6634a0df2efSAndy Whitcroftsub ctx_has_comment {
6644a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6654a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6664a0df2efSAndy Whitcroft
66700df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6684a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6694a0df2efSAndy Whitcroft
6704a0df2efSAndy Whitcroft	return ($cmt ne '');
6714a0df2efSAndy Whitcroft}
6724a0df2efSAndy Whitcroft
6730a920b5bSAndy Whitcroftsub cat_vet {
6740a920b5bSAndy Whitcroft	my ($vet) = @_;
6759c0ca6f9SAndy Whitcroft	my ($res, $coded);
6760a920b5bSAndy Whitcroft
6779c0ca6f9SAndy Whitcroft	$res = '';
6786c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
6796c72ffaaSAndy Whitcroft		$res .= $1;
6806c72ffaaSAndy Whitcroft		if ($2 ne '') {
6819c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
6826c72ffaaSAndy Whitcroft			$res .= $coded;
6836c72ffaaSAndy Whitcroft		}
6849c0ca6f9SAndy Whitcroft	}
6859c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
6860a920b5bSAndy Whitcroft
6879c0ca6f9SAndy Whitcroft	return $res;
6880a920b5bSAndy Whitcroft}
6890a920b5bSAndy Whitcroft
690c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
691cf655043SAndy Whitcroftmy $av_pending;
692c2fdda0dSAndy Whitcroftmy @av_paren_type;
693c2fdda0dSAndy Whitcroft
694c2fdda0dSAndy Whitcroftsub annotate_reset {
695c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
696cf655043SAndy Whitcroft	$av_pending = '_';
697cf655043SAndy Whitcroft	@av_paren_type = ('E');
698c2fdda0dSAndy Whitcroft}
699c2fdda0dSAndy Whitcroft
7006c72ffaaSAndy Whitcroftsub annotate_values {
7016c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
7026c72ffaaSAndy Whitcroft
7036c72ffaaSAndy Whitcroft	my $res;
7046c72ffaaSAndy Whitcroft	my $cur = $stream;
7056c72ffaaSAndy Whitcroft
706c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
7076c72ffaaSAndy Whitcroft
7086c72ffaaSAndy Whitcroft	while (length($cur)) {
709773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
710cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
711171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
7126c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
713c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
714c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
715cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
716c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7176c72ffaaSAndy Whitcroft			}
7186c72ffaaSAndy Whitcroft
719c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\))/) {
720c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7216c72ffaaSAndy Whitcroft			$type = 'T';
7226c72ffaaSAndy Whitcroft
723389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
724389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
725389a2fe5SAndy Whitcroft			$type = 'T';
726389a2fe5SAndy Whitcroft
727c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
728171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
729c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
730171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
731171ae1a4SAndy Whitcroft			if ($2 ne '') {
732cf655043SAndy Whitcroft				$av_pending = 'N';
733171ae1a4SAndy Whitcroft			}
734171ae1a4SAndy Whitcroft			$type = 'E';
735171ae1a4SAndy Whitcroft
736c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
737171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
738171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
739171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
7406c72ffaaSAndy Whitcroft
741c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
742cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
743c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
744cf655043SAndy Whitcroft
745cf655043SAndy Whitcroft			push(@av_paren_type, $type);
746cf655043SAndy Whitcroft			push(@av_paren_type, $type);
747171ae1a4SAndy Whitcroft			$type = 'E';
748cf655043SAndy Whitcroft
749c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
750cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
751cf655043SAndy Whitcroft			$av_preprocessor = 1;
752cf655043SAndy Whitcroft
753cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
754cf655043SAndy Whitcroft
755171ae1a4SAndy Whitcroft			$type = 'E';
756cf655043SAndy Whitcroft
757c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
758cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
759cf655043SAndy Whitcroft
760cf655043SAndy Whitcroft			$av_preprocessor = 1;
761cf655043SAndy Whitcroft
762cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
763cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
764cf655043SAndy Whitcroft			pop(@av_paren_type);
765cf655043SAndy Whitcroft			push(@av_paren_type, $type);
766171ae1a4SAndy Whitcroft			$type = 'E';
7676c72ffaaSAndy Whitcroft
7686c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
769c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7706c72ffaaSAndy Whitcroft
771171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
772171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
773171ae1a4SAndy Whitcroft			$av_pending = $type;
774171ae1a4SAndy Whitcroft			$type = 'N';
775171ae1a4SAndy Whitcroft
7766c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
777c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
7786c72ffaaSAndy Whitcroft			if (defined $2) {
779cf655043SAndy Whitcroft				$av_pending = 'V';
7806c72ffaaSAndy Whitcroft			}
7816c72ffaaSAndy Whitcroft			$type = 'N';
7826c72ffaaSAndy Whitcroft
78313214adfSAndy Whitcroft		} elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
784c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
785cf655043SAndy Whitcroft			$av_pending = 'N';
7866c72ffaaSAndy Whitcroft			$type = 'N';
7876c72ffaaSAndy Whitcroft
7886ef9b297SAndy Whitcroft		} elsif ($cur =~/^(return|case|else|goto)/o) {
789c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
7906c72ffaaSAndy Whitcroft			$type = 'N';
7916c72ffaaSAndy Whitcroft
7926c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
793c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
794cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
795cf655043SAndy Whitcroft			$av_pending = '_';
7966c72ffaaSAndy Whitcroft			$type = 'N';
7976c72ffaaSAndy Whitcroft
7986c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
799cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
800cf655043SAndy Whitcroft			if ($new_type ne '_') {
801cf655043SAndy Whitcroft				$type = $new_type;
802c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
803c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
8046c72ffaaSAndy Whitcroft			} else {
805c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
8066c72ffaaSAndy Whitcroft			}
8076c72ffaaSAndy Whitcroft
808c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
809c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
810c8cb2ca3SAndy Whitcroft			$type = 'V';
811cf655043SAndy Whitcroft			$av_pending = 'V';
8126c72ffaaSAndy Whitcroft
8136c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
814c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
8156c72ffaaSAndy Whitcroft			$type = 'V';
8166c72ffaaSAndy Whitcroft
8176c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
818c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
8196c72ffaaSAndy Whitcroft			$type = 'N';
8206c72ffaaSAndy Whitcroft
821cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
822c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
82313214adfSAndy Whitcroft			$type = 'E';
82413214adfSAndy Whitcroft
825cf655043SAndy Whitcroft		} elsif ($cur =~ /^(;|\?|:|\[)/o) {
82613214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
8276c72ffaaSAndy Whitcroft			$type = 'N';
8286c72ffaaSAndy Whitcroft
8296c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
830c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
8316c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
8326c72ffaaSAndy Whitcroft				$type = 'N';
8336c72ffaaSAndy Whitcroft			}
8346c72ffaaSAndy Whitcroft
8356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
836c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
8376c72ffaaSAndy Whitcroft		}
8386c72ffaaSAndy Whitcroft		if (defined $1) {
8396c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
8406c72ffaaSAndy Whitcroft			$res .= $type x length($1);
8416c72ffaaSAndy Whitcroft		}
8426c72ffaaSAndy Whitcroft	}
8436c72ffaaSAndy Whitcroft
8446c72ffaaSAndy Whitcroft	return $res;
8456c72ffaaSAndy Whitcroft}
8466c72ffaaSAndy Whitcroft
8478905a67cSAndy Whitcroftsub possible {
84813214adfSAndy Whitcroft	my ($possible, $line) = @_;
8498905a67cSAndy Whitcroft
850c45dcabdSAndy Whitcroft	print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
8518905a67cSAndy Whitcroft	if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
8528905a67cSAndy Whitcroft	    $possible ne 'goto' && $possible ne 'return' &&
8538905a67cSAndy Whitcroft	    $possible ne 'case' && $possible ne 'else' &&
854d3ddcf47SAndy Whitcroft	    $possible ne 'asm' && $possible ne '__asm__' &&
855c45dcabdSAndy Whitcroft	    $possible !~ /^(typedef|struct|enum)\b/) {
856c45dcabdSAndy Whitcroft		# Check for modifiers.
857c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
858c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
859c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
860c45dcabdSAndy Whitcroft
861c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
862c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
863c45dcabdSAndy Whitcroft			warn "MODIFIER: $possible ($line)\n" if ($dbg_possible);
864c45dcabdSAndy Whitcroft			push(@modifierList, $possible);
865c45dcabdSAndy Whitcroft
866c45dcabdSAndy Whitcroft		} else {
86713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
8688905a67cSAndy Whitcroft			push(@typeList, $possible);
869c45dcabdSAndy Whitcroft		}
8708905a67cSAndy Whitcroft		build_types();
8718905a67cSAndy Whitcroft	}
8728905a67cSAndy Whitcroft}
8738905a67cSAndy Whitcroft
8746c72ffaaSAndy Whitcroftmy $prefix = '';
8756c72ffaaSAndy Whitcroft
876f0a594c1SAndy Whitcroftsub report {
877773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
878773647a0SAndy Whitcroft		return 0;
879773647a0SAndy Whitcroft	}
8808905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
8818905a67cSAndy Whitcroft
8828905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
8838905a67cSAndy Whitcroft
88413214adfSAndy Whitcroft	push(our @report, $line);
885773647a0SAndy Whitcroft
886773647a0SAndy Whitcroft	return 1;
887f0a594c1SAndy Whitcroft}
888f0a594c1SAndy Whitcroftsub report_dump {
88913214adfSAndy Whitcroft	our @report;
890f0a594c1SAndy Whitcroft}
891de7d4f0eSAndy Whitcroftsub ERROR {
892773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
893de7d4f0eSAndy Whitcroft		our $clean = 0;
8946c72ffaaSAndy Whitcroft		our $cnt_error++;
895de7d4f0eSAndy Whitcroft	}
896773647a0SAndy Whitcroft}
897de7d4f0eSAndy Whitcroftsub WARN {
898773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
899de7d4f0eSAndy Whitcroft		our $clean = 0;
9006c72ffaaSAndy Whitcroft		our $cnt_warn++;
901de7d4f0eSAndy Whitcroft	}
902773647a0SAndy Whitcroft}
903de7d4f0eSAndy Whitcroftsub CHK {
904773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
905de7d4f0eSAndy Whitcroft		our $clean = 0;
9066c72ffaaSAndy Whitcroft		our $cnt_chk++;
9076c72ffaaSAndy Whitcroft	}
908de7d4f0eSAndy Whitcroft}
909de7d4f0eSAndy Whitcroft
9100a920b5bSAndy Whitcroftsub process {
9110a920b5bSAndy Whitcroft	my $filename = shift;
9120a920b5bSAndy Whitcroft
9130a920b5bSAndy Whitcroft	my $linenr=0;
9140a920b5bSAndy Whitcroft	my $prevline="";
915c2fdda0dSAndy Whitcroft	my $prevrawline="";
9160a920b5bSAndy Whitcroft	my $stashline="";
917c2fdda0dSAndy Whitcroft	my $stashrawline="";
9180a920b5bSAndy Whitcroft
9194a0df2efSAndy Whitcroft	my $length;
9200a920b5bSAndy Whitcroft	my $indent;
9210a920b5bSAndy Whitcroft	my $previndent=0;
9220a920b5bSAndy Whitcroft	my $stashindent=0;
9230a920b5bSAndy Whitcroft
924de7d4f0eSAndy Whitcroft	our $clean = 1;
9250a920b5bSAndy Whitcroft	my $signoff = 0;
9260a920b5bSAndy Whitcroft	my $is_patch = 0;
9270a920b5bSAndy Whitcroft
92813214adfSAndy Whitcroft	our @report = ();
9296c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
9306c72ffaaSAndy Whitcroft	our $cnt_error = 0;
9316c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
9326c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
9336c72ffaaSAndy Whitcroft
9340a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
9350a920b5bSAndy Whitcroft	my $realfile = '';
9360a920b5bSAndy Whitcroft	my $realline = 0;
9370a920b5bSAndy Whitcroft	my $realcnt = 0;
9380a920b5bSAndy Whitcroft	my $here = '';
9390a920b5bSAndy Whitcroft	my $in_comment = 0;
940c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
9410a920b5bSAndy Whitcroft	my $first_line = 0;
9420a920b5bSAndy Whitcroft
94313214adfSAndy Whitcroft	my $prev_values = 'E';
94413214adfSAndy Whitcroft
94513214adfSAndy Whitcroft	# suppression flags
946773647a0SAndy Whitcroft	my %suppress_ifbraces;
947653d4876SAndy Whitcroft
948c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
949de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
950c2fdda0dSAndy Whitcroft	#
951de7d4f0eSAndy Whitcroft	my @setup_docs = ();
952de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
953773647a0SAndy Whitcroft
954773647a0SAndy Whitcroft	sanitise_line_reset();
955c2fdda0dSAndy Whitcroft	my $line;
956c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
957773647a0SAndy Whitcroft		$linenr++;
958773647a0SAndy Whitcroft		$line = $rawline;
959c2fdda0dSAndy Whitcroft
960773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
961de7d4f0eSAndy Whitcroft			$setup_docs = 0;
962de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
963de7d4f0eSAndy Whitcroft				$setup_docs = 1;
964de7d4f0eSAndy Whitcroft			}
965773647a0SAndy Whitcroft			#next;
966de7d4f0eSAndy Whitcroft		}
967773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
968773647a0SAndy Whitcroft			$realline=$1-1;
969773647a0SAndy Whitcroft			if (defined $2) {
970773647a0SAndy Whitcroft				$realcnt=$3+1;
971773647a0SAndy Whitcroft			} else {
972773647a0SAndy Whitcroft				$realcnt=1+1;
973773647a0SAndy Whitcroft			}
974c45dcabdSAndy Whitcroft			$in_comment = 0;
975773647a0SAndy Whitcroft
976773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
977773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
978773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
979773647a0SAndy Whitcroft			# at context start.
980773647a0SAndy Whitcroft			my $edge;
981171ae1a4SAndy Whitcroft			for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
982773647a0SAndy Whitcroft				next if ($line =~ /^-/);
983773647a0SAndy Whitcroft				($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
984773647a0SAndy Whitcroft				last if (defined $edge);
985773647a0SAndy Whitcroft			}
986773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
987773647a0SAndy Whitcroft				$in_comment = 1;
988773647a0SAndy Whitcroft			}
989773647a0SAndy Whitcroft
990773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
991773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
992773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
993773647a0SAndy Whitcroft			if (!defined $edge &&
994773647a0SAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
995773647a0SAndy Whitcroft			{
996773647a0SAndy Whitcroft				$in_comment = 1;
997773647a0SAndy Whitcroft			}
998773647a0SAndy Whitcroft
999773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1000773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1001773647a0SAndy Whitcroft
1002171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1003773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1004171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1005773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1006773647a0SAndy Whitcroft		}
1007773647a0SAndy Whitcroft		push(@lines, $line);
1008773647a0SAndy Whitcroft
1009773647a0SAndy Whitcroft		if ($realcnt > 1) {
1010773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1011773647a0SAndy Whitcroft		} else {
1012773647a0SAndy Whitcroft			$realcnt = 0;
1013773647a0SAndy Whitcroft		}
1014773647a0SAndy Whitcroft
1015773647a0SAndy Whitcroft		#print "==>$rawline\n";
1016773647a0SAndy Whitcroft		#print "-->$line\n";
1017de7d4f0eSAndy Whitcroft
1018de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1019de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1020de7d4f0eSAndy Whitcroft		}
1021de7d4f0eSAndy Whitcroft	}
1022de7d4f0eSAndy Whitcroft
10236c72ffaaSAndy Whitcroft	$prefix = '';
10246c72ffaaSAndy Whitcroft
1025773647a0SAndy Whitcroft	$realcnt = 0;
1026773647a0SAndy Whitcroft	$linenr = 0;
10270a920b5bSAndy Whitcroft	foreach my $line (@lines) {
10280a920b5bSAndy Whitcroft		$linenr++;
10290a920b5bSAndy Whitcroft
1030c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
10316c72ffaaSAndy Whitcroft
10320a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
10336c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
10340a920b5bSAndy Whitcroft			$is_patch = 1;
10354a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
10360a920b5bSAndy Whitcroft			$realline=$1-1;
10370a920b5bSAndy Whitcroft			if (defined $2) {
10380a920b5bSAndy Whitcroft				$realcnt=$3+1;
10390a920b5bSAndy Whitcroft			} else {
10400a920b5bSAndy Whitcroft				$realcnt=1+1;
10410a920b5bSAndy Whitcroft			}
1042c2fdda0dSAndy Whitcroft			annotate_reset();
104313214adfSAndy Whitcroft			$prev_values = 'E';
104413214adfSAndy Whitcroft
1045773647a0SAndy Whitcroft			%suppress_ifbraces = ();
10460a920b5bSAndy Whitcroft			next;
10470a920b5bSAndy Whitcroft
10484a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
10494a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
10504a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1051773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
10520a920b5bSAndy Whitcroft			$realline++;
1053d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
10540a920b5bSAndy Whitcroft
10554a0df2efSAndy Whitcroft			# Measure the line length and indent.
1056c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
10570a920b5bSAndy Whitcroft
10580a920b5bSAndy Whitcroft			# Track the previous line.
10590a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
10600a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1061c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1062c2fdda0dSAndy Whitcroft
1063773647a0SAndy Whitcroft			#warn "line<$line>\n";
10646c72ffaaSAndy Whitcroft
1065d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1066d8aaf121SAndy Whitcroft			$realcnt--;
10670a920b5bSAndy Whitcroft		}
10680a920b5bSAndy Whitcroft
10690a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1070773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1071773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1072773647a0SAndy Whitcroft
10736c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
10746c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1075773647a0SAndy Whitcroft
1076773647a0SAndy Whitcroft		# extract the filename as it passes
1077773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1078773647a0SAndy Whitcroft			$realfile = $1;
1079773647a0SAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
1080773647a0SAndy Whitcroft
1081773647a0SAndy Whitcroft			if ($realfile =~ m@include/asm/@) {
1082773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1083773647a0SAndy Whitcroft			}
1084773647a0SAndy Whitcroft			next;
1085773647a0SAndy Whitcroft		}
1086773647a0SAndy Whitcroft
1087389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
10880a920b5bSAndy Whitcroft
1089c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1090c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1091c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
10920a920b5bSAndy Whitcroft
10936c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
10946c72ffaaSAndy Whitcroft
10950a920b5bSAndy Whitcroft#check the patch for a signoff:
1096d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
10974a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
10984a0df2efSAndy Whitcroft			$signoff++;
10990a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1100de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1101de7d4f0eSAndy Whitcroft					$herecurr);
11020a920b5bSAndy Whitcroft			}
11030a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1104773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1105de7d4f0eSAndy Whitcroft					$herecurr);
11060a920b5bSAndy Whitcroft			}
11070a920b5bSAndy Whitcroft		}
11080a920b5bSAndy Whitcroft
110900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
11108905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1111de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
11126c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1113de7d4f0eSAndy Whitcroft		}
1114de7d4f0eSAndy Whitcroft
1115de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1116de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1117171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1118171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1119171ae1a4SAndy Whitcroft
1120171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1121171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1122171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1123171ae1a4SAndy Whitcroft
1124171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
112500df344fSAndy Whitcroft		}
11260a920b5bSAndy Whitcroft
112700df344fSAndy Whitcroft#ignore lines being removed
112800df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
112900df344fSAndy Whitcroft
113000df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
113100df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
11320a920b5bSAndy Whitcroft
11330a920b5bSAndy Whitcroft#trailing whitespace
11349c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1135c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
11369c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
11379c0ca6f9SAndy Whitcroft
1138c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1139c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1140de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
11410a920b5bSAndy Whitcroft		}
11420a920b5bSAndy Whitcroft#80 column limit
1143c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1144f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1145f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1146f4c014c0SAndy Whitcroft		    $length > 80)
1147c45dcabdSAndy Whitcroft		{
1148de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
11490a920b5bSAndy Whitcroft		}
11500a920b5bSAndy Whitcroft
11518905a67cSAndy Whitcroft# check for adding lines without a newline.
11528905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
11538905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
11548905a67cSAndy Whitcroft		}
11558905a67cSAndy Whitcroft
11560a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
11570a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
11580a920b5bSAndy Whitcroft
11590a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
11600a920b5bSAndy Whitcroft# more than 8 must use tabs.
1161c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1162c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1163c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1164171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
11650a920b5bSAndy Whitcroft		}
11660a920b5bSAndy Whitcroft
1167c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1168cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1169c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1170c2fdda0dSAndy Whitcroft		}
117122f2a2efSAndy Whitcroft
11729c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1173f5fe35ddSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next);
11749c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1175f5fe35ddSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next) =
1176f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1177171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1178171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1179171ae1a4SAndy Whitcroft
1180171ae1a4SAndy Whitcroft			my $s = $stat;
1181171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1182cf655043SAndy Whitcroft
1183c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1184171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1185c2fdda0dSAndy Whitcroft
1186c2fdda0dSAndy Whitcroft			# Ignore functions being called
1187171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1188c2fdda0dSAndy Whitcroft
1189c45dcabdSAndy Whitcroft			# declarations always start with types
1190c45dcabdSAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))\s*(?:;|=|,|\()/s) {
1191c45dcabdSAndy Whitcroft				my $type = $1;
1192c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1193c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1194c45dcabdSAndy Whitcroft
11956c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1196171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1197c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1198c2fdda0dSAndy Whitcroft			}
11998905a67cSAndy Whitcroft
12006c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1201171ae1a4SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1202c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
12039c0ca6f9SAndy Whitcroft			}
12048905a67cSAndy Whitcroft
12058905a67cSAndy Whitcroft			# Check for any sort of function declaration.
12068905a67cSAndy Whitcroft			# int foo(something bar, other baz);
12078905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1208171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
12098905a67cSAndy Whitcroft				my ($name_len) = length($1);
12108905a67cSAndy Whitcroft
1211cf655043SAndy Whitcroft				my $ctx = $s;
1212773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
12138905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1214cf655043SAndy Whitcroft
12158905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1216c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
12178905a67cSAndy Whitcroft
1218c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
12198905a67cSAndy Whitcroft					}
12208905a67cSAndy Whitcroft				}
12218905a67cSAndy Whitcroft			}
12228905a67cSAndy Whitcroft
12239c0ca6f9SAndy Whitcroft		}
12249c0ca6f9SAndy Whitcroft
122500df344fSAndy Whitcroft#
122600df344fSAndy Whitcroft# Checks which may be anchored in the context.
122700df344fSAndy Whitcroft#
122800df344fSAndy Whitcroft
122900df344fSAndy Whitcroft# Check for switch () and associated case and default
123000df344fSAndy Whitcroft# statements should be at the same indent.
123100df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
123200df344fSAndy Whitcroft			my $err = '';
123300df344fSAndy Whitcroft			my $sep = '';
123400df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
123500df344fSAndy Whitcroft			shift(@ctx);
123600df344fSAndy Whitcroft			for my $ctx (@ctx) {
123700df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
123800df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
123900df344fSAndy Whitcroft							$indent != $cindent) {
124000df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
124100df344fSAndy Whitcroft					$sep = '';
124200df344fSAndy Whitcroft				} else {
124300df344fSAndy Whitcroft					$sep = "[...]\n";
124400df344fSAndy Whitcroft				}
124500df344fSAndy Whitcroft			}
124600df344fSAndy Whitcroft			if ($err ne '') {
12479c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1248de7d4f0eSAndy Whitcroft			}
1249de7d4f0eSAndy Whitcroft		}
1250e2a763c2SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
1251e2a763c2SAndy Whitcroft		    $line !~ /\G(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$/g) {
1252e2a763c2SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
1253e2a763c2SAndy Whitcroft		}
1254de7d4f0eSAndy Whitcroft
1255de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1256de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1257c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1258773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1259773647a0SAndy Whitcroft
12609c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1261de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1262de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1263de7d4f0eSAndy Whitcroft
1264548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1265548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1266de7d4f0eSAndy Whitcroft
1267548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1268548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1269548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1270548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1271548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1272773647a0SAndy Whitcroft				$ctx_ln++;
1273773647a0SAndy Whitcroft			}
1274548596d5SAndy Whitcroft
127553210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
127653210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1277773647a0SAndy Whitcroft
1278773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1279773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1280c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
128100df344fSAndy Whitcroft			}
1282773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1283773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1284773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1285773647a0SAndy Whitcroft			{
12869c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
12879c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1288773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1289c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
12909c0ca6f9SAndy Whitcroft				}
12919c0ca6f9SAndy Whitcroft			}
129200df344fSAndy Whitcroft		}
129300df344fSAndy Whitcroft
12946c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
12956c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
12966c72ffaaSAndy Whitcroft		my $curr_values = annotate_values($opline . "\n", $prev_values);
12976c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1298c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1299c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1300cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1301cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
1302c2fdda0dSAndy Whitcroft		}
13036c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
13046c72ffaaSAndy Whitcroft
130500df344fSAndy Whitcroft#ignore lines not being added
130600df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
130700df344fSAndy Whitcroft
1308653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
1309*7429c690SAndy Whitcroft		if ($dbg_type) {
1310*7429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
1311*7429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
1312*7429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1313*7429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
1314*7429c690SAndy Whitcroft			}
1315653d4876SAndy Whitcroft			next;
1316653d4876SAndy Whitcroft		}
1317653d4876SAndy Whitcroft
1318f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1319f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1320f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1321773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1322f0a594c1SAndy Whitcroft		}
1323f0a594c1SAndy Whitcroft
132400df344fSAndy Whitcroft#
132500df344fSAndy Whitcroft# Checks which are anchored on the added line.
132600df344fSAndy Whitcroft#
132700df344fSAndy Whitcroft
1328653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1329c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1330653d4876SAndy Whitcroft			my $path = $1;
1331653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1332de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1333de7d4f0eSAndy Whitcroft					$herecurr);
1334653d4876SAndy Whitcroft			}
1335653d4876SAndy Whitcroft		}
1336653d4876SAndy Whitcroft
133700df344fSAndy Whitcroft# no C99 // comments
133800df344fSAndy Whitcroft		if ($line =~ m{//}) {
1339de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
134000df344fSAndy Whitcroft		}
134100df344fSAndy Whitcroft		# Remove C99 comments.
13420a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
13436c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
13440a920b5bSAndy Whitcroft
13450a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1346653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1347653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1348653d4876SAndy Whitcroft			my $name = $1;
13490a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
13500a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
1351653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
1352cf655043SAndy Whitcroft			   ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1353cf655043SAndy Whitcroft			   ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1354171ae1a4SAndy Whitcroft			   ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
1355cf655043SAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1356de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
13570a920b5bSAndy Whitcroft			}
13580a920b5bSAndy Whitcroft		}
13590a920b5bSAndy Whitcroft
1360f0a594c1SAndy Whitcroft# check for external initialisers.
1361c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1362f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1363f0a594c1SAndy Whitcroft				$herecurr);
1364f0a594c1SAndy Whitcroft		}
13650a920b5bSAndy Whitcroft# check for static initialisers.
13669c9ba34eSAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1367de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1368de7d4f0eSAndy Whitcroft				$herecurr);
13690a920b5bSAndy Whitcroft		}
13700a920b5bSAndy Whitcroft
1371653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1372653d4876SAndy Whitcroft# make sense.
1373653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
13749c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1375c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1376653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1377de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
13780a920b5bSAndy Whitcroft		}
13790a920b5bSAndy Whitcroft
13800a920b5bSAndy Whitcroft# * goes on variable not on type
1381d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1382de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1383de7d4f0eSAndy Whitcroft				$herecurr);
1384d8aaf121SAndy Whitcroft
1385d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1386de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1387de7d4f0eSAndy Whitcroft				$herecurr);
1388d8aaf121SAndy Whitcroft
13899c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1390de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1391de7d4f0eSAndy Whitcroft				$herecurr);
1392d8aaf121SAndy Whitcroft
13939c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1394de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1395de7d4f0eSAndy Whitcroft				$herecurr);
13960a920b5bSAndy Whitcroft		}
13970a920b5bSAndy Whitcroft
13980a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
13990a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
14000a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
14010a920b5bSAndy Whitcroft# 			print "$herecurr";
14020a920b5bSAndy Whitcroft# 			$clean = 0;
14030a920b5bSAndy Whitcroft# 		}
14040a920b5bSAndy Whitcroft
14058905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1406c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
14078905a67cSAndy Whitcroft		}
14088905a67cSAndy Whitcroft
140900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
141000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
141100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
141200df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
141300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1414f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
141500df344fSAndy Whitcroft			my $ok = 0;
141600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
141700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
141800df344fSAndy Whitcroft				# we have a preceeding printk if it ends
141900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
142000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
142100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
142200df344fSAndy Whitcroft						$ok = 1;
142300df344fSAndy Whitcroft					}
142400df344fSAndy Whitcroft					last;
142500df344fSAndy Whitcroft				}
142600df344fSAndy Whitcroft			}
142700df344fSAndy Whitcroft			if ($ok == 0) {
1428de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
14290a920b5bSAndy Whitcroft			}
143000df344fSAndy Whitcroft		}
14310a920b5bSAndy Whitcroft
1432653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1433653d4876SAndy Whitcroft# or if closed on same line
1434c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1435c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1436de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
14370a920b5bSAndy Whitcroft		}
1438653d4876SAndy Whitcroft
14398905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
14408905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
14418905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
14428905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
14438905a67cSAndy Whitcroft		}
14448905a67cSAndy Whitcroft
14458d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
14468d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
14478d31cfceSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0..10] = 5,
14488d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
14498d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
14508d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
14518d31cfceSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/)) {
14528d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
14538d31cfceSAndy Whitcroft			}
14548d31cfceSAndy Whitcroft		}
14558d31cfceSAndy Whitcroft
1456f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
14576c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1458c2fdda0dSAndy Whitcroft			my $name = $1;
1459773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1460773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1461c2fdda0dSAndy Whitcroft
1462c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1463773647a0SAndy Whitcroft			if ($name =~ /^(?:
1464773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1465773647a0SAndy Whitcroft				volatile|__volatile__|
1466773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1467773647a0SAndy Whitcroft				asm|__asm__)$/x)
1468773647a0SAndy Whitcroft			{
1469c2fdda0dSAndy Whitcroft
1470c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1471c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1472c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1473c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1474773647a0SAndy Whitcroft
1475773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1476c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1477c2fdda0dSAndy Whitcroft
1478c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1479c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1480773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1481c2fdda0dSAndy Whitcroft
1482c2fdda0dSAndy Whitcroft			} else {
1483773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1484f0a594c1SAndy Whitcroft			}
14856c72ffaaSAndy Whitcroft		}
1486653d4876SAndy Whitcroft# Check operator spacing.
14870a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
14889c0ca6f9SAndy Whitcroft			my $ops = qr{
14899c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
14909c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
14919c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
1492c2fdda0dSAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
14939c0ca6f9SAndy Whitcroft			}x;
1494cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
149500df344fSAndy Whitcroft			my $off = 0;
14966c72ffaaSAndy Whitcroft
14976c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
14986c72ffaaSAndy Whitcroft
14990a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
15004a0df2efSAndy Whitcroft				$off += length($elements[$n]);
15014a0df2efSAndy Whitcroft
1502773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1503773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1504773647a0SAndy Whitcroft				my $cc = '';
1505773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1506773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1507773647a0SAndy Whitcroft				}
1508773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1509773647a0SAndy Whitcroft
15104a0df2efSAndy Whitcroft				my $a = '';
15114a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
15124a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1513cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
15144a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
15154a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1516773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
15174a0df2efSAndy Whitcroft
15180a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
15194a0df2efSAndy Whitcroft
15204a0df2efSAndy Whitcroft				my $c = '';
15210a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
15224a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
15234a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1524cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
15254a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
15264a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
152722f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
15284a0df2efSAndy Whitcroft				} else {
15294a0df2efSAndy Whitcroft					$c = 'E';
15300a920b5bSAndy Whitcroft				}
15310a920b5bSAndy Whitcroft
15324a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
15334a0df2efSAndy Whitcroft
15344a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
15354a0df2efSAndy Whitcroft
15366c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1537de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
15380a920b5bSAndy Whitcroft
15399c0ca6f9SAndy Whitcroft				# Classify operators into binary, unary, or
15409c0ca6f9SAndy Whitcroft				# definitions (* only) where they have more
15419c0ca6f9SAndy Whitcroft				# than one mode.
15426c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
15436c72ffaaSAndy Whitcroft				my $op_left = substr($curr_values, $off, 1);
15446c72ffaaSAndy Whitcroft				my $is_unary;
15456c72ffaaSAndy Whitcroft				if ($op_type eq 'T') {
15469c0ca6f9SAndy Whitcroft					$is_unary = 2;
15476c72ffaaSAndy Whitcroft				} elsif ($op_left eq 'V') {
15486c72ffaaSAndy Whitcroft					$is_unary = 0;
15496c72ffaaSAndy Whitcroft				} else {
15506c72ffaaSAndy Whitcroft					$is_unary = 1;
15519c0ca6f9SAndy Whitcroft				}
15529c0ca6f9SAndy Whitcroft				#if ($op eq '-' || $op eq '&' || $op eq '*') {
15536c72ffaaSAndy Whitcroft				#	print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
15549c0ca6f9SAndy Whitcroft				#}
15550a920b5bSAndy Whitcroft
155613214adfSAndy Whitcroft				# Ignore operators passed as parameters.
155713214adfSAndy Whitcroft				if ($op_type ne 'V' &&
155813214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
155913214adfSAndy Whitcroft
1560cf655043SAndy Whitcroft#				# Ignore comments
1561cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
156213214adfSAndy Whitcroft
1563d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
156413214adfSAndy Whitcroft				} elsif ($op eq ';') {
1565cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1566cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1567773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1568d8aaf121SAndy Whitcroft					}
1569d8aaf121SAndy Whitcroft
1570d8aaf121SAndy Whitcroft				# // is a comment
1571d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
15720a920b5bSAndy Whitcroft
15730a920b5bSAndy Whitcroft				# -> should have no spaces
15740a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
15754a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1576773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
15770a920b5bSAndy Whitcroft					}
15780a920b5bSAndy Whitcroft
15790a920b5bSAndy Whitcroft				# , must have a space on the right.
15800a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1581cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1582773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
15830a920b5bSAndy Whitcroft					}
15840a920b5bSAndy Whitcroft
15859c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
15869c0ca6f9SAndy Whitcroft				} elsif ($op eq '*' && $is_unary == 2) {
15879c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
15889c0ca6f9SAndy Whitcroft
15899c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
15909c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
15919c0ca6f9SAndy Whitcroft				# unary operator, or a cast
15929c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
15939c0ca6f9SAndy Whitcroft				         ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1594cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1595773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
15960a920b5bSAndy Whitcroft					}
1597171ae1a4SAndy Whitcroft					if ($op  eq '*' && $cc =~/\s*const\b/) {
1598171ae1a4SAndy Whitcroft						# A unary '*' may be const
1599171ae1a4SAndy Whitcroft
1600171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1601773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
16020a920b5bSAndy Whitcroft					}
16030a920b5bSAndy Whitcroft
16040a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
16050a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1606773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1607773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
16080a920b5bSAndy Whitcroft					}
1609773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1610773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1611773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1612653d4876SAndy Whitcroft					}
1613773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1614773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1615773647a0SAndy Whitcroft					}
1616773647a0SAndy Whitcroft
16170a920b5bSAndy Whitcroft
16180a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
16199c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
16209c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
16219c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1622c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1623c2fdda0dSAndy Whitcroft					 $op eq '%')
16240a920b5bSAndy Whitcroft				{
1625773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1626de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1627de7d4f0eSAndy Whitcroft							$hereptr);
16280a920b5bSAndy Whitcroft					}
16290a920b5bSAndy Whitcroft
16300a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1631cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
163222f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
163322f2a2efSAndy Whitcroft					if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
163422f2a2efSAndy Whitcroft					    !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1635773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
16360a920b5bSAndy Whitcroft					}
163722f2a2efSAndy Whitcroft				}
16384a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
16390a920b5bSAndy Whitcroft			}
16400a920b5bSAndy Whitcroft		}
16410a920b5bSAndy Whitcroft
1642f0a594c1SAndy Whitcroft# check for multiple assignments
1643f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
16446c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1645f0a594c1SAndy Whitcroft		}
1646f0a594c1SAndy Whitcroft
164722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
164822f2a2efSAndy Whitcroft## # continuation.
164922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
165022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
165122f2a2efSAndy Whitcroft##
165222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
165322f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
165422f2a2efSAndy Whitcroft## 			my $ln = $line;
165522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
165622f2a2efSAndy Whitcroft## 			}
165722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
165822f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
165922f2a2efSAndy Whitcroft## 			}
166022f2a2efSAndy Whitcroft## 		}
1661f0a594c1SAndy Whitcroft
16620a920b5bSAndy Whitcroft#need space before brace following if, while, etc
166322f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
166422f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1665773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1666de7d4f0eSAndy Whitcroft		}
1667de7d4f0eSAndy Whitcroft
1668de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1669de7d4f0eSAndy Whitcroft# on the line
1670de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1671773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
16720a920b5bSAndy Whitcroft		}
16730a920b5bSAndy Whitcroft
167422f2a2efSAndy Whitcroft# check spacing on square brackets
167522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1676773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
167722f2a2efSAndy Whitcroft		}
167822f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1679773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
168022f2a2efSAndy Whitcroft		}
168122f2a2efSAndy Whitcroft
1682c45dcabdSAndy Whitcroft# check spacing on parentheses
16839c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
16849c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1685773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
168622f2a2efSAndy Whitcroft		}
168713214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1688c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
1689c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
1690773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
169122f2a2efSAndy Whitcroft		}
169222f2a2efSAndy Whitcroft
16930a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
16944a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
16950a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1696de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
16970a920b5bSAndy Whitcroft		}
16980a920b5bSAndy Whitcroft
1699c45dcabdSAndy Whitcroft# Return is not a function.
1700c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1701c45dcabdSAndy Whitcroft			my $spacing = $1;
1702c45dcabdSAndy Whitcroft			my $value = $2;
1703c45dcabdSAndy Whitcroft
1704c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
1705fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
1706c45dcabdSAndy Whitcroft			while ($value =~ s/\([^\(\)]*\)/1/) {
1707c45dcabdSAndy Whitcroft			}
1708c45dcabdSAndy Whitcroft
1709c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1710c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1711c45dcabdSAndy Whitcroft
1712c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
1713c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
1714c45dcabdSAndy Whitcroft			}
1715c45dcabdSAndy Whitcroft		}
1716c45dcabdSAndy Whitcroft
17170a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
17184a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1719773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
17200a920b5bSAndy Whitcroft		}
17210a920b5bSAndy Whitcroft
1722f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
1723f5fe35ddSAndy Whitcroft# statements after the conditional.
172453210168SAndy Whitcroft		if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
1725171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
17268905a67cSAndy Whitcroft
17278905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1728c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
17298905a67cSAndy Whitcroft			}
17308905a67cSAndy Whitcroft
17318905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
17328905a67cSAndy Whitcroft			# conditional.
1733773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
17348905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
173513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
173653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
173753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
1738773647a0SAndy Whitcroft			{
17398905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
17408905a67cSAndy Whitcroft			}
17418905a67cSAndy Whitcroft		}
17428905a67cSAndy Whitcroft
1743f5fe35ddSAndy Whitcroft# Check relative indent for conditionals and blocks.
1744f5fe35ddSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1745f5fe35ddSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
1746f5fe35ddSAndy Whitcroft
1747f5fe35ddSAndy Whitcroft			substr($s, 0, length($c), '');
1748f5fe35ddSAndy Whitcroft
1749f5fe35ddSAndy Whitcroft			# Make sure we remove the line prefixes as we have
1750f5fe35ddSAndy Whitcroft			# none on the first line, and are going to readd them
1751f5fe35ddSAndy Whitcroft			# where necessary.
1752f5fe35ddSAndy Whitcroft			$s =~ s/\n./\n/gs;
1753f5fe35ddSAndy Whitcroft
1754f5fe35ddSAndy Whitcroft			# We want to check the first line inside the block
1755f5fe35ddSAndy Whitcroft			# starting at the end of the conditional, so remove:
1756f5fe35ddSAndy Whitcroft			#  1) any blank line termination
1757f5fe35ddSAndy Whitcroft			#  2) any opening brace { on end of the line
1758f5fe35ddSAndy Whitcroft			#  3) any do (...) {
1759f5fe35ddSAndy Whitcroft			my $continuation = 0;
1760f5fe35ddSAndy Whitcroft			my $check = 0;
1761f5fe35ddSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
1762f5fe35ddSAndy Whitcroft			$s =~ s/^\s*{//;
1763f5fe35ddSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
1764f5fe35ddSAndy Whitcroft				$continuation = 1;
1765f5fe35ddSAndy Whitcroft			}
1766f5fe35ddSAndy Whitcroft			if ($s =~ s/^\s*\n//) {
1767f5fe35ddSAndy Whitcroft				$check = 1;
1768f5fe35ddSAndy Whitcroft			}
1769f5fe35ddSAndy Whitcroft
1770f5fe35ddSAndy Whitcroft			# Also ignore a loop construct at the end of a
1771f5fe35ddSAndy Whitcroft			# preprocessor statement.
1772f5fe35ddSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
1773f5fe35ddSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
1774f5fe35ddSAndy Whitcroft				$check = 0;
1775f5fe35ddSAndy Whitcroft			}
1776f5fe35ddSAndy Whitcroft
1777f5fe35ddSAndy Whitcroft			# Ignore the current line if its is a preprocessor
1778f5fe35ddSAndy Whitcroft			# line.
1779f5fe35ddSAndy Whitcroft			if ($s =~ /^\s*#\s*/) {
1780f5fe35ddSAndy Whitcroft				$check = 0;
1781f5fe35ddSAndy Whitcroft			}
1782f5fe35ddSAndy Whitcroft
1783f5fe35ddSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
1784f5fe35ddSAndy Whitcroft
1785f5fe35ddSAndy Whitcroft			##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n";
1786f5fe35ddSAndy Whitcroft
1787f5fe35ddSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
1788f5fe35ddSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
1789f5fe35ddSAndy Whitcroft				WARN("suspect code indent for conditional statements\n" . $herecurr);
1790f5fe35ddSAndy Whitcroft			}
1791f5fe35ddSAndy Whitcroft		}
1792f5fe35ddSAndy Whitcroft
179313214adfSAndy Whitcroft# Check for bitwise tests written as boolean
179413214adfSAndy Whitcroft		if ($line =~ /
179513214adfSAndy Whitcroft			(?:
179613214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
179713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
179813214adfSAndy Whitcroft				(?:\&\&|\|\|)
179913214adfSAndy Whitcroft			|
180013214adfSAndy Whitcroft				(?:\&\&|\|\|)
180113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
180213214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
180313214adfSAndy Whitcroft			)/x)
180413214adfSAndy Whitcroft		{
180513214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
180613214adfSAndy Whitcroft		}
180713214adfSAndy Whitcroft
18088905a67cSAndy Whitcroft# if and else should not have general statements after it
180913214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
181013214adfSAndy Whitcroft			my $s = $1;
181113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
181213214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
18138905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
18140a920b5bSAndy Whitcroft			}
181513214adfSAndy Whitcroft		}
18160a920b5bSAndy Whitcroft
18170a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
18180a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
18190a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
18200a920b5bSAndy Whitcroft						$previndent == $indent) {
1821de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
18220a920b5bSAndy Whitcroft		}
18230a920b5bSAndy Whitcroft
1824c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1825c2fdda0dSAndy Whitcroft						$previndent == $indent) {
1826c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1827c2fdda0dSAndy Whitcroft
1828c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
1829c2fdda0dSAndy Whitcroft			# conditional.
1830773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
1831c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
1832c2fdda0dSAndy Whitcroft
1833c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
1834c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
1835c2fdda0dSAndy Whitcroft			}
1836c2fdda0dSAndy Whitcroft		}
1837c2fdda0dSAndy Whitcroft
18380a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
18390a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
18400a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
18410a920b5bSAndy Whitcroft#		    print "$herecurr";
18420a920b5bSAndy Whitcroft#		    $clean = 0;
18430a920b5bSAndy Whitcroft#		}
18440a920b5bSAndy Whitcroft
18450a920b5bSAndy Whitcroft#no spaces allowed after \ in define
1846c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
1847de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
18480a920b5bSAndy Whitcroft		}
18490a920b5bSAndy Whitcroft
1850653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1851c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
1852c45dcabdSAndy Whitcroft			my $checkfile = "include/linux/$1.h";
1853c45dcabdSAndy Whitcroft			if (-f "$root/$checkfile" && $realfile ne $checkfile &&
1854c45dcabdSAndy Whitcroft			    $1 ne 'irq')
1855c45dcabdSAndy Whitcroft			{
1856773647a0SAndy Whitcroft				WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1857de7d4f0eSAndy Whitcroft					$herecurr);
18580a920b5bSAndy Whitcroft			}
18590a920b5bSAndy Whitcroft		}
18600a920b5bSAndy Whitcroft
1861653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
1862653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
1863cf655043SAndy Whitcroft# in a known good container
1864c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
1865d8aaf121SAndy Whitcroft			my $ln = $linenr;
1866d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
1867c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
1868c45dcabdSAndy Whitcroft			my $ctx = '';
1869653d4876SAndy Whitcroft
1870c45dcabdSAndy Whitcroft			my $args = defined($1);
1871de7d4f0eSAndy Whitcroft
1872c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
1873c45dcabdSAndy Whitcroft			# search to that.
1874c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
1875c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
1876c45dcabdSAndy Whitcroft			{
1877c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
1878a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
1879c45dcabdSAndy Whitcroft				$ln++;
1880de7d4f0eSAndy Whitcroft			}
1881c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
1882d8aaf121SAndy Whitcroft
1883c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
1884c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
1885c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
1886a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
1887c45dcabdSAndy Whitcroft
1888c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
1889c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
1890c45dcabdSAndy Whitcroft			$rest = '';
1891a3bb97a7SAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /(?:^|\\)\s*$/)) {
1892a3bb97a7SAndy Whitcroft				#print "ADDING $off <" . substr($lines[$ln - 1], $off) . ">\n";
1893a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
1894c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
1895c45dcabdSAndy Whitcroft					$cnt--;
1896a3bb97a7SAndy Whitcroft				}
1897a3bb97a7SAndy Whitcroft				$ln++;
1898c45dcabdSAndy Whitcroft				$off = 0;
1899c45dcabdSAndy Whitcroft			}
1900c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
1901c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
1902c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
1903c45dcabdSAndy Whitcroft
1904c45dcabdSAndy Whitcroft			# Clean up the original statement.
1905c45dcabdSAndy Whitcroft			if ($args) {
1906c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
1907d8aaf121SAndy Whitcroft			} else {
1908c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
1909c45dcabdSAndy Whitcroft			}
1910c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
1911c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
1912c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
1913c45dcabdSAndy Whitcroft
1914c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
1915c45dcabdSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/) {
1916c45dcabdSAndy Whitcroft			}
1917c45dcabdSAndy Whitcroft			while ($dstat =~ s/\{[^\{\}]*\}/1/) {
1918c45dcabdSAndy Whitcroft			}
1919c45dcabdSAndy Whitcroft
1920c45dcabdSAndy Whitcroft			my $exceptions = qr{
1921c45dcabdSAndy Whitcroft				$Declare|
1922c45dcabdSAndy Whitcroft				module_param_named|
1923c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
1924c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
1925c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
1926c45dcabdSAndy Whitcroft				__typeof__\(
1927c45dcabdSAndy Whitcroft			}x;
1928a3bb97a7SAndy Whitcroft			#print "REST<$rest>\n";
1929c45dcabdSAndy Whitcroft			if ($rest ne '') {
1930c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
1931c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
1932c45dcabdSAndy Whitcroft				{
1933c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1934c45dcabdSAndy Whitcroft				}
1935c45dcabdSAndy Whitcroft
1936c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
1937c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
1938c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
1939c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
1940c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
1941c45dcabdSAndy Whitcroft				{
1942de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1943d8aaf121SAndy Whitcroft				}
19440a920b5bSAndy Whitcroft			}
1945653d4876SAndy Whitcroft		}
19460a920b5bSAndy Whitcroft
1947f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
194813214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
194913214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
1950cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
195113214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1952cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1953cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
195413214adfSAndy Whitcroft				my $allowed = 0;
195513214adfSAndy Whitcroft				my $seen = 0;
1956773647a0SAndy Whitcroft				my $herectx = $here . "\n";
1957cf655043SAndy Whitcroft				my $ln = $linenr - 1;
195813214adfSAndy Whitcroft				for my $chunk (@chunks) {
195913214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
196013214adfSAndy Whitcroft
1961773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
1962773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1963773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
1964773647a0SAndy Whitcroft
1965773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1966773647a0SAndy Whitcroft
1967773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
1968773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
1969773647a0SAndy Whitcroft
1970773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1971cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
1972cf655043SAndy Whitcroft
1973773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
197413214adfSAndy Whitcroft
197513214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
197613214adfSAndy Whitcroft
1977cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
1978cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
1979cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
198013214adfSAndy Whitcroft						$allowed = 1;
198113214adfSAndy Whitcroft					}
198213214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
1983cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
198413214adfSAndy Whitcroft						$allowed = 1;
198513214adfSAndy Whitcroft					}
1986cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
1987cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
198813214adfSAndy Whitcroft						$allowed = 1;
198913214adfSAndy Whitcroft					}
199013214adfSAndy Whitcroft				}
199113214adfSAndy Whitcroft				if ($seen && !$allowed) {
1992cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
199313214adfSAndy Whitcroft				}
199413214adfSAndy Whitcroft			}
199513214adfSAndy Whitcroft		}
1996773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
199713214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
1998cf655043SAndy Whitcroft			my $allowed = 0;
1999f0a594c1SAndy Whitcroft
2000cf655043SAndy Whitcroft			# Check the pre-context.
2001cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2002cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2003cf655043SAndy Whitcroft				$allowed = 1;
2004f0a594c1SAndy Whitcroft			}
2005773647a0SAndy Whitcroft
2006773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2007773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2008773647a0SAndy Whitcroft
2009cf655043SAndy Whitcroft			# Check the condition.
2010cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2011773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2012cf655043SAndy Whitcroft			if (defined $cond) {
2013773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2014cf655043SAndy Whitcroft			}
2015cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2016cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2017cf655043SAndy Whitcroft				$allowed = 1;
2018cf655043SAndy Whitcroft			}
2019cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2020cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2021cf655043SAndy Whitcroft				$allowed = 1;
2022cf655043SAndy Whitcroft			}
2023cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2024cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2025cf655043SAndy Whitcroft				$allowed = 1;
2026cf655043SAndy Whitcroft			}
2027cf655043SAndy Whitcroft			# Check the post-context.
2028cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2029cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2030cf655043SAndy Whitcroft				if (defined $cond) {
2031773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2032cf655043SAndy Whitcroft				}
2033cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2034cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2035cf655043SAndy Whitcroft					$allowed = 1;
2036cf655043SAndy Whitcroft				}
2037cf655043SAndy Whitcroft			}
2038cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2039cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2040cf655043SAndy Whitcroft				my $end = $linenr + statement_rawlines($block) - 1;
2041cf655043SAndy Whitcroft
2042cf655043SAndy Whitcroft				for (my $ln = $linenr - 1; $ln < $end; $ln++) {
2043cf655043SAndy Whitcroft					$herectx .= $rawlines[$ln] . "\n";;
2044cf655043SAndy Whitcroft				}
2045cf655043SAndy Whitcroft
2046cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2047f0a594c1SAndy Whitcroft			}
2048f0a594c1SAndy Whitcroft		}
2049f0a594c1SAndy Whitcroft
2050653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
20514a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2052c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2053de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
20540a920b5bSAndy Whitcroft			}
20550a920b5bSAndy Whitcroft		}
20560a920b5bSAndy Whitcroft
20574a0df2efSAndy Whitcroft# don't use deprecated functions
20584a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
205900df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2060de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
20614a0df2efSAndy Whitcroft			}
20624a0df2efSAndy Whitcroft		}
20634a0df2efSAndy Whitcroft
20644a0df2efSAndy Whitcroft# no volatiles please
20656c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
20666c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2067de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
20684a0df2efSAndy Whitcroft		}
20694a0df2efSAndy Whitcroft
20709c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
20719c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
20729c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
20739c0ca6f9SAndy Whitcroft		}
20749c0ca6f9SAndy Whitcroft
207500df344fSAndy Whitcroft# warn about #if 0
2076c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2077de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2078de7d4f0eSAndy Whitcroft				$herecurr);
20794a0df2efSAndy Whitcroft		}
20804a0df2efSAndy Whitcroft
2081f0a594c1SAndy Whitcroft# check for needless kfree() checks
2082f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2083f0a594c1SAndy Whitcroft			my $expr = $1;
2084f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
20853c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2086f0a594c1SAndy Whitcroft			}
2087f0a594c1SAndy Whitcroft		}
20884c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
20894c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
20904c432a8fSGreg Kroah-Hartman			my $expr = $1;
20914c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
20924c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
20934c432a8fSGreg Kroah-Hartman			}
20944c432a8fSGreg Kroah-Hartman		}
2095f0a594c1SAndy Whitcroft
209600df344fSAndy Whitcroft# warn about #ifdefs in C files
2097c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
209800df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
209900df344fSAndy Whitcroft#			print "$herecurr";
210000df344fSAndy Whitcroft#			$clean = 0;
210100df344fSAndy Whitcroft#		}
210200df344fSAndy Whitcroft
210322f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2104c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
210522f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
210622f2a2efSAndy Whitcroft		}
210722f2a2efSAndy Whitcroft
21084a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2109171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2110171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
21114a0df2efSAndy Whitcroft			my $which = $1;
21124a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2113de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
21144a0df2efSAndy Whitcroft			}
21154a0df2efSAndy Whitcroft		}
21164a0df2efSAndy Whitcroft# check for memory barriers without a comment.
21174a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
21184a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2119de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
21204a0df2efSAndy Whitcroft			}
21214a0df2efSAndy Whitcroft		}
21224a0df2efSAndy Whitcroft# check of hardware specific defines
2123c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2124de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
21250a920b5bSAndy Whitcroft		}
2126653d4876SAndy Whitcroft
2127de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2128de7d4f0eSAndy Whitcroft# storage class and type.
21299c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
21309c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2131de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2132de7d4f0eSAndy Whitcroft		}
2133de7d4f0eSAndy Whitcroft
21348905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
21358905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
21368905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
21378905a67cSAndy Whitcroft		}
21388905a67cSAndy Whitcroft
2139de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2140171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2141c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2142171ae1a4SAndy Whitcroft		{
2143c45dcabdSAndy Whitcroft			my $function_name = $1;
2144c45dcabdSAndy Whitcroft			my $paren_space = $2;
2145171ae1a4SAndy Whitcroft
2146171ae1a4SAndy Whitcroft			my $s = $stat;
2147171ae1a4SAndy Whitcroft			if (defined $cond) {
2148171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2149171ae1a4SAndy Whitcroft			}
2150c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2151c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2152c45dcabdSAndy Whitcroft			{
2153de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2154de7d4f0eSAndy Whitcroft			}
2155de7d4f0eSAndy Whitcroft
2156171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2157171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2158171ae1a4SAndy Whitcroft			}
21599c9ba34eSAndy Whitcroft
21609c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
21619c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
21629c9ba34eSAndy Whitcroft		{
21639c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2164171ae1a4SAndy Whitcroft		}
2165171ae1a4SAndy Whitcroft
2166de7d4f0eSAndy Whitcroft# checks for new __setup's
2167de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2168de7d4f0eSAndy Whitcroft			my $name = $1;
2169de7d4f0eSAndy Whitcroft
2170de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2171de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2172de7d4f0eSAndy Whitcroft			}
2173653d4876SAndy Whitcroft		}
21749c0ca6f9SAndy Whitcroft
21759c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
21769c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
21779c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
21789c0ca6f9SAndy Whitcroft		}
217913214adfSAndy Whitcroft
218013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
218113214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
218213214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
218313214adfSAndy Whitcroft		}
2184773647a0SAndy Whitcroft
2185773647a0SAndy Whitcroft# check for semaphores used as mutexes
2186171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2187773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2188773647a0SAndy Whitcroft		}
2189773647a0SAndy Whitcroft# check for semaphores used as mutexes
2190171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2191773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2192773647a0SAndy Whitcroft		}
2193773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2194773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2195773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2196773647a0SAndy Whitcroft		}
2197f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2198f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2199f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2200f3db6639SMichael Ellerman		}
2201773647a0SAndy Whitcroft
2202773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2203773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2204773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2205c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2206c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2207171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2208171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2209171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2210773647a0SAndy Whitcroft		{
2211773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2212773647a0SAndy Whitcroft		}
22139c9ba34eSAndy Whitcroft
22149c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
22159c9ba34eSAndy Whitcroft		my $string;
22169c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
22179c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
22189c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
22199c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
22209c9ba34eSAndy Whitcroft				last;
22219c9ba34eSAndy Whitcroft			}
22229c9ba34eSAndy Whitcroft		}
222313214adfSAndy Whitcroft	}
222413214adfSAndy Whitcroft
222513214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
222613214adfSAndy Whitcroft	# so just keep quiet.
222713214adfSAndy Whitcroft	if ($#rawlines == -1) {
222813214adfSAndy Whitcroft		exit(0);
22290a920b5bSAndy Whitcroft	}
22300a920b5bSAndy Whitcroft
22318905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
22328905a67cSAndy Whitcroft	# things that appear to be patches.
22338905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
22348905a67cSAndy Whitcroft		exit(0);
22358905a67cSAndy Whitcroft	}
22368905a67cSAndy Whitcroft
22378905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
22388905a67cSAndy Whitcroft	# just keep quiet.
22398905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
22408905a67cSAndy Whitcroft		exit(0);
22418905a67cSAndy Whitcroft	}
22428905a67cSAndy Whitcroft
22438905a67cSAndy Whitcroft	if (!$is_patch) {
2244de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
22450a920b5bSAndy Whitcroft	}
22460a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2247de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
22480a920b5bSAndy Whitcroft	}
22490a920b5bSAndy Whitcroft
2250f0a594c1SAndy Whitcroft	print report_dump();
225113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
225213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
22536c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
22546c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
22556c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
22568905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
22576c72ffaaSAndy Whitcroft	}
22588905a67cSAndy Whitcroft
22590a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2260c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
22610a920b5bSAndy Whitcroft	}
22620a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2263c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
22640a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
22650a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
22660a920b5bSAndy Whitcroft	}
226713214adfSAndy Whitcroft
22680a920b5bSAndy Whitcroft	return $clean;
22690a920b5bSAndy Whitcroft}
2270