xref: /linux-6.15/scripts/checkpatch.pl (revision 6ecd9674)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
20a920b5bSAndy Whitcroft# (c) 2001, Dave Jones. <[email protected]> (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
40a920b5bSAndy Whitcroft# (c) 2007, Andy Whitcroft <[email protected]> (new conditions, test suite, etc)
50a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
60a920b5bSAndy Whitcroft
70a920b5bSAndy Whitcroftuse strict;
80a920b5bSAndy Whitcroft
90a920b5bSAndy Whitcroftmy $P = $0;
1000df344fSAndy Whitcroft$P =~ s@.*/@@g;
110a920b5bSAndy Whitcroft
1233cba065SAndy Whitcroftmy $V = '0.21';
130a920b5bSAndy Whitcroft
140a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftmy $quiet = 0;
170a920b5bSAndy Whitcroftmy $tree = 1;
180a920b5bSAndy Whitcroftmy $chk_signoff = 1;
190a920b5bSAndy Whitcroftmy $chk_patch = 1;
20773647a0SAndy Whitcroftmy $tst_only;
216c72ffaaSAndy Whitcroftmy $emacs = 0;
228905a67cSAndy Whitcroftmy $terse = 0;
236c72ffaaSAndy Whitcroftmy $file = 0;
246c72ffaaSAndy Whitcroftmy $check = 0;
258905a67cSAndy Whitcroftmy $summary = 1;
268905a67cSAndy Whitcroftmy $mailback = 0;
2713214adfSAndy Whitcroftmy $summary_file = 0;
286c72ffaaSAndy Whitcroftmy $root;
29c2fdda0dSAndy Whitcroftmy %debug;
300a920b5bSAndy WhitcroftGetOptions(
316c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
320a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
330a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
340a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
356c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
368905a67cSAndy Whitcroft	'terse!'	=> \$terse,
376c72ffaaSAndy Whitcroft	'file!'		=> \$file,
386c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
396c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
406c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
418905a67cSAndy Whitcroft	'summary!'	=> \$summary,
428905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
4313214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
4413214adfSAndy Whitcroft
45c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
46773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
470a920b5bSAndy Whitcroft) or exit;
480a920b5bSAndy Whitcroft
490a920b5bSAndy Whitcroftmy $exit = 0;
500a920b5bSAndy Whitcroft
510a920b5bSAndy Whitcroftif ($#ARGV < 0) {
5200df344fSAndy Whitcroft	print "usage: $P [options] patchfile\n";
530a920b5bSAndy Whitcroft	print "version: $V\n";
540a920b5bSAndy Whitcroft	print "options: -q               => quiet\n";
550a920b5bSAndy Whitcroft	print "         --no-tree        => run without a kernel tree\n";
568905a67cSAndy Whitcroft	print "         --terse          => one line per report\n";
576c72ffaaSAndy Whitcroft	print "         --emacs          => emacs compile window format\n";
586c72ffaaSAndy Whitcroft	print "         --file           => check a source file\n";
596c72ffaaSAndy Whitcroft	print "         --strict         => enable more subjective tests\n";
606c72ffaaSAndy Whitcroft	print "         --root           => path to the kernel tree root\n";
6113214adfSAndy Whitcroft	print "         --no-summary     => suppress the per-file summary\n";
6213214adfSAndy Whitcroft	print "         --summary-file   => include the filename in summary\n";
630a920b5bSAndy Whitcroft	exit(1);
640a920b5bSAndy Whitcroft}
650a920b5bSAndy Whitcroft
66c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
67c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
687429c690SAndy Whitcroftmy $dbg_type = 0;
69a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
70c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
71c2fdda0dSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';"
72c2fdda0dSAndy Whitcroft}
73c2fdda0dSAndy Whitcroft
748905a67cSAndy Whitcroftif ($terse) {
758905a67cSAndy Whitcroft	$emacs = 1;
768905a67cSAndy Whitcroft	$quiet++;
778905a67cSAndy Whitcroft}
788905a67cSAndy Whitcroft
796c72ffaaSAndy Whitcroftif ($tree) {
806c72ffaaSAndy Whitcroft	if (defined $root) {
816c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
826c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
836c72ffaaSAndy Whitcroft		}
846c72ffaaSAndy Whitcroft	} else {
856c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
866c72ffaaSAndy Whitcroft			$root = '.';
876c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
886c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
896c72ffaaSAndy Whitcroft			$root = $1;
906c72ffaaSAndy Whitcroft		}
916c72ffaaSAndy Whitcroft	}
926c72ffaaSAndy Whitcroft
936c72ffaaSAndy Whitcroft	if (!defined $root) {
940a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
950a920b5bSAndy Whitcroft		exit(2);
960a920b5bSAndy Whitcroft	}
976c72ffaaSAndy Whitcroft}
986c72ffaaSAndy Whitcroft
996c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1006c72ffaaSAndy Whitcroft
1016c72ffaaSAndy Whitcroftour $Ident       = qr{[A-Za-z_][A-Za-z\d_]*};
1026c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1036c72ffaaSAndy Whitcroftour $Sparse	= qr{
1046c72ffaaSAndy Whitcroft			__user|
1056c72ffaaSAndy Whitcroft			__kernel|
1066c72ffaaSAndy Whitcroft			__force|
1076c72ffaaSAndy Whitcroft			__iomem|
1086c72ffaaSAndy Whitcroft			__must_check|
1096c72ffaaSAndy Whitcroft			__init_refok|
110cf655043SAndy Whitcroft			__kprobes
1116c72ffaaSAndy Whitcroft		}x;
1126c72ffaaSAndy Whitcroftour $Attribute	= qr{
1136c72ffaaSAndy Whitcroft			const|
1146c72ffaaSAndy Whitcroft			__read_mostly|
1156c72ffaaSAndy Whitcroft			__kprobes|
11624e1d81aSAndy Whitcroft			__(?:mem|cpu|dev|)(?:initdata|init)|
11724e1d81aSAndy Whitcroft			____cacheline_aligned|
11824e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
11924e1d81aSAndy Whitcroft			____cacheline_internodealigned_in_smp
1206c72ffaaSAndy Whitcroft		  }x;
121c45dcabdSAndy Whitcroftour $Modifier;
1226c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1236c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1246c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1256c72ffaaSAndy Whitcroft
1266c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1276c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
1286c72ffaaSAndy Whitcroftour $Operators	= qr{
1296c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1306c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
131c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1326c72ffaaSAndy Whitcroft		  }x;
1336c72ffaaSAndy Whitcroft
1348905a67cSAndy Whitcroftour $NonptrType;
1358905a67cSAndy Whitcroftour $Type;
1368905a67cSAndy Whitcroftour $Declare;
1378905a67cSAndy Whitcroft
138171ae1a4SAndy Whitcroftour $UTF8	= qr {
139171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
140171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
141171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
142171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
143171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
144171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
145171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
146171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
147171ae1a4SAndy Whitcroft}x;
148171ae1a4SAndy Whitcroft
1498905a67cSAndy Whitcroftour @typeList = (
1508905a67cSAndy Whitcroft	qr{void},
151c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
152c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
153c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
154c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
155c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
156c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
157c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
1588905a67cSAndy Whitcroft	qr{unsigned},
1598905a67cSAndy Whitcroft	qr{float},
1608905a67cSAndy Whitcroft	qr{double},
1618905a67cSAndy Whitcroft	qr{bool},
1628905a67cSAndy Whitcroft	qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
1638905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1648905a67cSAndy Whitcroft	qr{union\s+$Ident},
1658905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1668905a67cSAndy Whitcroft	qr{${Ident}_t},
1678905a67cSAndy Whitcroft	qr{${Ident}_handler},
1688905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1698905a67cSAndy Whitcroft);
170c45dcabdSAndy Whitcroftour @modifierList = (
171c45dcabdSAndy Whitcroft	qr{fastcall},
172c45dcabdSAndy Whitcroft);
1738905a67cSAndy Whitcroft
1748905a67cSAndy Whitcroftsub build_types {
175d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
176d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
177c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
1788905a67cSAndy Whitcroft	$NonptrType	= qr{
179d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
180cf655043SAndy Whitcroft			(?:
181c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
182c45dcabdSAndy Whitcroft				(?:${all}\b)
183cf655043SAndy Whitcroft			)
184c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
1858905a67cSAndy Whitcroft		  }x;
1868905a67cSAndy Whitcroft	$Type	= qr{
187c45dcabdSAndy Whitcroft			$NonptrType
1888905a67cSAndy Whitcroft			(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
189c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
1908905a67cSAndy Whitcroft		  }x;
1918905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
1928905a67cSAndy Whitcroft}
1938905a67cSAndy Whitcroftbuild_types();
1946c72ffaaSAndy Whitcroft
1956c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
1960a920b5bSAndy Whitcroft
1974a0df2efSAndy Whitcroftmy @dep_includes = ();
1984a0df2efSAndy Whitcroftmy @dep_functions = ();
1996c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2006c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
2016c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
2026c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
2030a920b5bSAndy Whitcroft	while (<REMOVE>) {
204f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
205f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
206f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2074a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2084a0df2efSAndy Whitcroft
209f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
210f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
211f0a594c1SAndy Whitcroft				}
2124a0df2efSAndy Whitcroft			}
2130a920b5bSAndy Whitcroft		}
2140a920b5bSAndy Whitcroft	}
2150a920b5bSAndy Whitcroft}
2160a920b5bSAndy Whitcroft
21700df344fSAndy Whitcroftmy @rawlines = ();
218c2fdda0dSAndy Whitcroftmy @lines = ();
219c2fdda0dSAndy Whitcroftmy $vname;
2206c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2216c72ffaaSAndy Whitcroft	if ($file) {
2226c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2236c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2246c72ffaaSAndy Whitcroft	} else {
2256c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2266c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2276c72ffaaSAndy Whitcroft	}
228c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
229c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
230c2fdda0dSAndy Whitcroft	} else {
231c2fdda0dSAndy Whitcroft		$vname = $filename;
232c2fdda0dSAndy Whitcroft	}
2336c72ffaaSAndy Whitcroft	while (<FILE>) {
2340a920b5bSAndy Whitcroft		chomp;
23500df344fSAndy Whitcroft		push(@rawlines, $_);
2366c72ffaaSAndy Whitcroft	}
2376c72ffaaSAndy Whitcroft	close(FILE);
238c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2390a920b5bSAndy Whitcroft		$exit = 1;
2400a920b5bSAndy Whitcroft	}
24100df344fSAndy Whitcroft	@rawlines = ();
24213214adfSAndy Whitcroft	@lines = ();
2430a920b5bSAndy Whitcroft}
2440a920b5bSAndy Whitcroft
2450a920b5bSAndy Whitcroftexit($exit);
2460a920b5bSAndy Whitcroft
2470a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2486c72ffaaSAndy Whitcroft	my ($root) = @_;
2496c72ffaaSAndy Whitcroft
2506c72ffaaSAndy Whitcroft	my @tree_check = (
2516c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2526c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2536c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2546c72ffaaSAndy Whitcroft	);
2556c72ffaaSAndy Whitcroft
2566c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2576c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2580a920b5bSAndy Whitcroft			return 0;
2590a920b5bSAndy Whitcroft		}
2606c72ffaaSAndy Whitcroft	}
2616c72ffaaSAndy Whitcroft	return 1;
2626c72ffaaSAndy Whitcroft}
2630a920b5bSAndy Whitcroft
2640a920b5bSAndy Whitcroftsub expand_tabs {
2650a920b5bSAndy Whitcroft	my ($str) = @_;
2660a920b5bSAndy Whitcroft
2670a920b5bSAndy Whitcroft	my $res = '';
2680a920b5bSAndy Whitcroft	my $n = 0;
2690a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2700a920b5bSAndy Whitcroft		if ($c eq "\t") {
2710a920b5bSAndy Whitcroft			$res .= ' ';
2720a920b5bSAndy Whitcroft			$n++;
2730a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2740a920b5bSAndy Whitcroft				$res .= ' ';
2750a920b5bSAndy Whitcroft			}
2760a920b5bSAndy Whitcroft			next;
2770a920b5bSAndy Whitcroft		}
2780a920b5bSAndy Whitcroft		$res .= $c;
2790a920b5bSAndy Whitcroft		$n++;
2800a920b5bSAndy Whitcroft	}
2810a920b5bSAndy Whitcroft
2820a920b5bSAndy Whitcroft	return $res;
2830a920b5bSAndy Whitcroft}
2846c72ffaaSAndy Whitcroftsub copy_spacing {
285773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
2866c72ffaaSAndy Whitcroft	return $res;
2876c72ffaaSAndy Whitcroft}
2880a920b5bSAndy Whitcroft
2894a0df2efSAndy Whitcroftsub line_stats {
2904a0df2efSAndy Whitcroft	my ($line) = @_;
2914a0df2efSAndy Whitcroft
2924a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
2934a0df2efSAndy Whitcroft	$line =~ s/^.//;
2944a0df2efSAndy Whitcroft	$line = expand_tabs($line);
2954a0df2efSAndy Whitcroft
2964a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
2974a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
2984a0df2efSAndy Whitcroft
2994a0df2efSAndy Whitcroft	return (length($line), length($white));
3004a0df2efSAndy Whitcroft}
3014a0df2efSAndy Whitcroft
302773647a0SAndy Whitcroftmy $sanitise_quote = '';
303773647a0SAndy Whitcroft
304773647a0SAndy Whitcroftsub sanitise_line_reset {
305773647a0SAndy Whitcroft	my ($in_comment) = @_;
306773647a0SAndy Whitcroft
307773647a0SAndy Whitcroft	if ($in_comment) {
308773647a0SAndy Whitcroft		$sanitise_quote = '*/';
309773647a0SAndy Whitcroft	} else {
310773647a0SAndy Whitcroft		$sanitise_quote = '';
311773647a0SAndy Whitcroft	}
312773647a0SAndy Whitcroft}
31300df344fSAndy Whitcroftsub sanitise_line {
31400df344fSAndy Whitcroft	my ($line) = @_;
31500df344fSAndy Whitcroft
31600df344fSAndy Whitcroft	my $res = '';
31700df344fSAndy Whitcroft	my $l = '';
31800df344fSAndy Whitcroft
319c2fdda0dSAndy Whitcroft	my $qlen = 0;
320773647a0SAndy Whitcroft	my $off = 0;
321773647a0SAndy Whitcroft	my $c;
32200df344fSAndy Whitcroft
323773647a0SAndy Whitcroft	# Always copy over the diff marker.
324773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
325773647a0SAndy Whitcroft
326773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
327773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
328773647a0SAndy Whitcroft
329773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
330773647a0SAndy Whitcroft		# and end, all to $;.
331773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
332773647a0SAndy Whitcroft			$sanitise_quote = '*/';
333773647a0SAndy Whitcroft
334773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
335773647a0SAndy Whitcroft			$off++;
33600df344fSAndy Whitcroft			next;
337773647a0SAndy Whitcroft		}
338c45dcabdSAndy Whitcroft		if (substr($line, $off, 2) eq '*/') {
339773647a0SAndy Whitcroft			$sanitise_quote = '';
340773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
341773647a0SAndy Whitcroft			$off++;
342773647a0SAndy Whitcroft			next;
343773647a0SAndy Whitcroft		}
344773647a0SAndy Whitcroft
345773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
346773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
347773647a0SAndy Whitcroft		    $c eq "\\") {
348773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
349773647a0SAndy Whitcroft			$off++;
350773647a0SAndy Whitcroft			next;
351773647a0SAndy Whitcroft		}
352773647a0SAndy Whitcroft		# Regular quotes.
353773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
354773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
355773647a0SAndy Whitcroft				$sanitise_quote = $c;
356773647a0SAndy Whitcroft
357773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
358773647a0SAndy Whitcroft				next;
359773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
360773647a0SAndy Whitcroft				$sanitise_quote = '';
36100df344fSAndy Whitcroft			}
36200df344fSAndy Whitcroft		}
363773647a0SAndy Whitcroft
364773647a0SAndy Whitcroft		#print "SQ:$sanitise_quote\n";
365773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
366773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
367773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
368773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
36900df344fSAndy Whitcroft		} else {
370773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
37100df344fSAndy Whitcroft		}
372c2fdda0dSAndy Whitcroft	}
373c2fdda0dSAndy Whitcroft
374c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
375c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
376c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
377c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
378c2fdda0dSAndy Whitcroft
379c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
380c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
381c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
382c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
383c2fdda0dSAndy Whitcroft	}
384c2fdda0dSAndy Whitcroft
38500df344fSAndy Whitcroft	return $res;
38600df344fSAndy Whitcroft}
38700df344fSAndy Whitcroft
3888905a67cSAndy Whitcroftsub ctx_statement_block {
3898905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3908905a67cSAndy Whitcroft	my $line = $linenr - 1;
3918905a67cSAndy Whitcroft	my $blk = '';
3928905a67cSAndy Whitcroft	my $soff = $off;
3938905a67cSAndy Whitcroft	my $coff = $off - 1;
394773647a0SAndy Whitcroft	my $coff_set = 0;
3958905a67cSAndy Whitcroft
39613214adfSAndy Whitcroft	my $loff = 0;
39713214adfSAndy Whitcroft
3988905a67cSAndy Whitcroft	my $type = '';
3998905a67cSAndy Whitcroft	my $level = 0;
400cf655043SAndy Whitcroft	my $p;
4018905a67cSAndy Whitcroft	my $c;
4028905a67cSAndy Whitcroft	my $len = 0;
40313214adfSAndy Whitcroft
40413214adfSAndy Whitcroft	my $remainder;
4058905a67cSAndy Whitcroft	while (1) {
406773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4078905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4088905a67cSAndy Whitcroft		# context.
4098905a67cSAndy Whitcroft		if ($off >= $len) {
4108905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
411c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4128905a67cSAndy Whitcroft				$remain--;
41313214adfSAndy Whitcroft				$loff = $len;
414c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4158905a67cSAndy Whitcroft				$len = length($blk);
4168905a67cSAndy Whitcroft				$line++;
4178905a67cSAndy Whitcroft				last;
4188905a67cSAndy Whitcroft			}
4198905a67cSAndy Whitcroft			# Bail if there is no further context.
4208905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
42113214adfSAndy Whitcroft			if ($off >= $len) {
4228905a67cSAndy Whitcroft				last;
4238905a67cSAndy Whitcroft			}
4248905a67cSAndy Whitcroft		}
425cf655043SAndy Whitcroft		$p = $c;
4268905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
42713214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4288905a67cSAndy Whitcroft
429773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4308905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4318905a67cSAndy Whitcroft		# outermost level.
4328905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4338905a67cSAndy Whitcroft			last;
4348905a67cSAndy Whitcroft		}
4358905a67cSAndy Whitcroft
43613214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
437773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
438773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
439773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
440773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
441773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
442773647a0SAndy Whitcroft			$coff_set = 1;
443773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
444773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
44513214adfSAndy Whitcroft		}
44613214adfSAndy Whitcroft
4478905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4488905a67cSAndy Whitcroft			$level++;
4498905a67cSAndy Whitcroft			$type = '(';
4508905a67cSAndy Whitcroft		}
4518905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4528905a67cSAndy Whitcroft			$level--;
4538905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4548905a67cSAndy Whitcroft
4558905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4568905a67cSAndy Whitcroft				$coff = $off;
457773647a0SAndy Whitcroft				$coff_set = 1;
458773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4598905a67cSAndy Whitcroft			}
4608905a67cSAndy Whitcroft		}
4618905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4628905a67cSAndy Whitcroft			$level++;
4638905a67cSAndy Whitcroft			$type = '{';
4648905a67cSAndy Whitcroft		}
4658905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4668905a67cSAndy Whitcroft			$level--;
4678905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4688905a67cSAndy Whitcroft
4698905a67cSAndy Whitcroft			if ($level == 0) {
4708905a67cSAndy Whitcroft				last;
4718905a67cSAndy Whitcroft			}
4728905a67cSAndy Whitcroft		}
4738905a67cSAndy Whitcroft		$off++;
4748905a67cSAndy Whitcroft	}
475a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
47613214adfSAndy Whitcroft	if ($off == $len) {
477a3bb97a7SAndy Whitcroft		$loff = $len + 1;
47813214adfSAndy Whitcroft		$line++;
47913214adfSAndy Whitcroft		$remain--;
48013214adfSAndy Whitcroft	}
4818905a67cSAndy Whitcroft
4828905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4838905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4848905a67cSAndy Whitcroft
4858905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4868905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4878905a67cSAndy Whitcroft
488773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
48913214adfSAndy Whitcroft
49013214adfSAndy Whitcroft	return ($statement, $condition,
49113214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
49213214adfSAndy Whitcroft}
49313214adfSAndy Whitcroft
494cf655043SAndy Whitcroftsub statement_lines {
495cf655043SAndy Whitcroft	my ($stmt) = @_;
496cf655043SAndy Whitcroft
497cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
498cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
499cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
500cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
501cf655043SAndy Whitcroft
502cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
503cf655043SAndy Whitcroft
504cf655043SAndy Whitcroft	return $#stmt_lines + 2;
505cf655043SAndy Whitcroft}
506cf655043SAndy Whitcroft
507cf655043SAndy Whitcroftsub statement_rawlines {
508cf655043SAndy Whitcroft	my ($stmt) = @_;
509cf655043SAndy Whitcroft
510cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
511cf655043SAndy Whitcroft
512cf655043SAndy Whitcroft	return $#stmt_lines + 2;
513cf655043SAndy Whitcroft}
514cf655043SAndy Whitcroft
515cf655043SAndy Whitcroftsub statement_block_size {
516cf655043SAndy Whitcroft	my ($stmt) = @_;
517cf655043SAndy Whitcroft
518cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
519cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
520cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
521cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
522cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
523cf655043SAndy Whitcroft
524cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
525cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
526cf655043SAndy Whitcroft
527cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
528cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
529cf655043SAndy Whitcroft
530cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
531cf655043SAndy Whitcroft		return $stmt_lines;
532cf655043SAndy Whitcroft	} else {
533cf655043SAndy Whitcroft		return $stmt_statements;
534cf655043SAndy Whitcroft	}
535cf655043SAndy Whitcroft}
536cf655043SAndy Whitcroft
53713214adfSAndy Whitcroftsub ctx_statement_full {
53813214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
53913214adfSAndy Whitcroft	my ($statement, $condition, $level);
54013214adfSAndy Whitcroft
54113214adfSAndy Whitcroft	my (@chunks);
54213214adfSAndy Whitcroft
543cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
54413214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
54513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
546773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
54713214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
548cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
549cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
550cf655043SAndy Whitcroft	}
551cf655043SAndy Whitcroft
552cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
553cf655043SAndy Whitcroft	# could continue the statement.
554cf655043SAndy Whitcroft	for (;;) {
55513214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
55613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
557cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
558773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
559cf655043SAndy Whitcroft		#print "C: push\n";
560cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
56113214adfSAndy Whitcroft	}
56213214adfSAndy Whitcroft
56313214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5648905a67cSAndy Whitcroft}
5658905a67cSAndy Whitcroft
5664a0df2efSAndy Whitcroftsub ctx_block_get {
567f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5684a0df2efSAndy Whitcroft	my $line;
5694a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5704a0df2efSAndy Whitcroft	my $blk = '';
5714a0df2efSAndy Whitcroft	my @o;
5724a0df2efSAndy Whitcroft	my @c;
5734a0df2efSAndy Whitcroft	my @res = ();
5744a0df2efSAndy Whitcroft
575f0a594c1SAndy Whitcroft	my $level = 0;
57600df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
57700df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
57800df344fSAndy Whitcroft		$remain--;
57900df344fSAndy Whitcroft
58000df344fSAndy Whitcroft		$blk .= $rawlines[$line];
581f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
582f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
583f0a594c1SAndy Whitcroft			if ($off > 0) {
584f0a594c1SAndy Whitcroft				$off--;
585f0a594c1SAndy Whitcroft				next;
586f0a594c1SAndy Whitcroft			}
5874a0df2efSAndy Whitcroft
588f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
589f0a594c1SAndy Whitcroft				$level--;
590f0a594c1SAndy Whitcroft				last if ($level == 0);
591f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
592f0a594c1SAndy Whitcroft				$level++;
593f0a594c1SAndy Whitcroft			}
594f0a594c1SAndy Whitcroft		}
5954a0df2efSAndy Whitcroft
596f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
59700df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
5984a0df2efSAndy Whitcroft		}
5994a0df2efSAndy Whitcroft
600f0a594c1SAndy Whitcroft		last if ($level == 0);
6014a0df2efSAndy Whitcroft	}
6024a0df2efSAndy Whitcroft
603f0a594c1SAndy Whitcroft	return ($level, @res);
6044a0df2efSAndy Whitcroft}
6054a0df2efSAndy Whitcroftsub ctx_block_outer {
6064a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6074a0df2efSAndy Whitcroft
608f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
609f0a594c1SAndy Whitcroft	return @r;
6104a0df2efSAndy Whitcroft}
6114a0df2efSAndy Whitcroftsub ctx_block {
6124a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6134a0df2efSAndy Whitcroft
614f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
615f0a594c1SAndy Whitcroft	return @r;
616653d4876SAndy Whitcroft}
617653d4876SAndy Whitcroftsub ctx_statement {
618f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
619f0a594c1SAndy Whitcroft
620f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
621f0a594c1SAndy Whitcroft	return @r;
622f0a594c1SAndy Whitcroft}
623f0a594c1SAndy Whitcroftsub ctx_block_level {
624653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
625653d4876SAndy Whitcroft
626f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6274a0df2efSAndy Whitcroft}
6289c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6299c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6309c0ca6f9SAndy Whitcroft
6319c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6329c0ca6f9SAndy Whitcroft}
6334a0df2efSAndy Whitcroft
6344a0df2efSAndy Whitcroftsub ctx_locate_comment {
6354a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6364a0df2efSAndy Whitcroft
6374a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
638beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
6394a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6404a0df2efSAndy Whitcroft
6414a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6424a0df2efSAndy Whitcroft	# comment.
6434a0df2efSAndy Whitcroft	my $in_comment = 0;
6444a0df2efSAndy Whitcroft	$current_comment = '';
6454a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
64600df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
64700df344fSAndy Whitcroft		#warn "           $line\n";
6484a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6494a0df2efSAndy Whitcroft			$in_comment = 1;
6504a0df2efSAndy Whitcroft		}
6514a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6524a0df2efSAndy Whitcroft			$in_comment = 1;
6534a0df2efSAndy Whitcroft		}
6544a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6554a0df2efSAndy Whitcroft			$current_comment = '';
6564a0df2efSAndy Whitcroft		}
6574a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6584a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6594a0df2efSAndy Whitcroft			$in_comment = 0;
6604a0df2efSAndy Whitcroft		}
6614a0df2efSAndy Whitcroft	}
6624a0df2efSAndy Whitcroft
6634a0df2efSAndy Whitcroft	chomp($current_comment);
6644a0df2efSAndy Whitcroft	return($current_comment);
6654a0df2efSAndy Whitcroft}
6664a0df2efSAndy Whitcroftsub ctx_has_comment {
6674a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6684a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6694a0df2efSAndy Whitcroft
67000df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6714a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6724a0df2efSAndy Whitcroft
6734a0df2efSAndy Whitcroft	return ($cmt ne '');
6744a0df2efSAndy Whitcroft}
6754a0df2efSAndy Whitcroft
6760a920b5bSAndy Whitcroftsub cat_vet {
6770a920b5bSAndy Whitcroft	my ($vet) = @_;
6789c0ca6f9SAndy Whitcroft	my ($res, $coded);
6790a920b5bSAndy Whitcroft
6809c0ca6f9SAndy Whitcroft	$res = '';
6816c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
6826c72ffaaSAndy Whitcroft		$res .= $1;
6836c72ffaaSAndy Whitcroft		if ($2 ne '') {
6849c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
6856c72ffaaSAndy Whitcroft			$res .= $coded;
6866c72ffaaSAndy Whitcroft		}
6879c0ca6f9SAndy Whitcroft	}
6889c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
6890a920b5bSAndy Whitcroft
6909c0ca6f9SAndy Whitcroft	return $res;
6910a920b5bSAndy Whitcroft}
6920a920b5bSAndy Whitcroft
693c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
694cf655043SAndy Whitcroftmy $av_pending;
695c2fdda0dSAndy Whitcroftmy @av_paren_type;
6961f65f947SAndy Whitcroftmy $av_pend_colon;
697c2fdda0dSAndy Whitcroft
698c2fdda0dSAndy Whitcroftsub annotate_reset {
699c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
700cf655043SAndy Whitcroft	$av_pending = '_';
701cf655043SAndy Whitcroft	@av_paren_type = ('E');
7021f65f947SAndy Whitcroft	$av_pend_colon = 'O';
703c2fdda0dSAndy Whitcroft}
704c2fdda0dSAndy Whitcroft
7056c72ffaaSAndy Whitcroftsub annotate_values {
7066c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
7076c72ffaaSAndy Whitcroft
7086c72ffaaSAndy Whitcroft	my $res;
7091f65f947SAndy Whitcroft	my $var = '_' x length($stream);
7106c72ffaaSAndy Whitcroft	my $cur = $stream;
7116c72ffaaSAndy Whitcroft
712c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
7136c72ffaaSAndy Whitcroft
7146c72ffaaSAndy Whitcroft	while (length($cur)) {
715773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
716cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
717171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
7186c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
719c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
720c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
721cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
722c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7236c72ffaaSAndy Whitcroft			}
7246c72ffaaSAndy Whitcroft
7258ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
726c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7276c72ffaaSAndy Whitcroft			$type = 'T';
7286c72ffaaSAndy Whitcroft
729389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
730389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
731389a2fe5SAndy Whitcroft			$type = 'T';
732389a2fe5SAndy Whitcroft
733c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
734171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
735c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
736171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
737171ae1a4SAndy Whitcroft			if ($2 ne '') {
738cf655043SAndy Whitcroft				$av_pending = 'N';
739171ae1a4SAndy Whitcroft			}
740171ae1a4SAndy Whitcroft			$type = 'E';
741171ae1a4SAndy Whitcroft
742c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
743171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
744171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
745171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
7466c72ffaaSAndy Whitcroft
747c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
748cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
749c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
750cf655043SAndy Whitcroft
751cf655043SAndy Whitcroft			push(@av_paren_type, $type);
752cf655043SAndy Whitcroft			push(@av_paren_type, $type);
753171ae1a4SAndy Whitcroft			$type = 'E';
754cf655043SAndy Whitcroft
755c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
756cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
757cf655043SAndy Whitcroft			$av_preprocessor = 1;
758cf655043SAndy Whitcroft
759cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
760cf655043SAndy Whitcroft
761171ae1a4SAndy Whitcroft			$type = 'E';
762cf655043SAndy Whitcroft
763c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
764cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
765cf655043SAndy Whitcroft
766cf655043SAndy Whitcroft			$av_preprocessor = 1;
767cf655043SAndy Whitcroft
768cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
769cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
770cf655043SAndy Whitcroft			pop(@av_paren_type);
771cf655043SAndy Whitcroft			push(@av_paren_type, $type);
772171ae1a4SAndy Whitcroft			$type = 'E';
7736c72ffaaSAndy Whitcroft
7746c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
775c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7766c72ffaaSAndy Whitcroft
777171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
778171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
779171ae1a4SAndy Whitcroft			$av_pending = $type;
780171ae1a4SAndy Whitcroft			$type = 'N';
781171ae1a4SAndy Whitcroft
7826c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
783c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
7846c72ffaaSAndy Whitcroft			if (defined $2) {
785cf655043SAndy Whitcroft				$av_pending = 'V';
7866c72ffaaSAndy Whitcroft			}
7876c72ffaaSAndy Whitcroft			$type = 'N';
7886c72ffaaSAndy Whitcroft
78914b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
790c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
79114b111c1SAndy Whitcroft			$av_pending = 'E';
7926c72ffaaSAndy Whitcroft			$type = 'N';
7936c72ffaaSAndy Whitcroft
7941f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
7951f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
7961f65f947SAndy Whitcroft			$av_pend_colon = 'C';
7971f65f947SAndy Whitcroft			$type = 'N';
7981f65f947SAndy Whitcroft
79914b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
800c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
8016c72ffaaSAndy Whitcroft			$type = 'N';
8026c72ffaaSAndy Whitcroft
8036c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
804c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
805cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
806cf655043SAndy Whitcroft			$av_pending = '_';
8076c72ffaaSAndy Whitcroft			$type = 'N';
8086c72ffaaSAndy Whitcroft
8096c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
810cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
811cf655043SAndy Whitcroft			if ($new_type ne '_') {
812cf655043SAndy Whitcroft				$type = $new_type;
813c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
814c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
8156c72ffaaSAndy Whitcroft			} else {
816c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
8176c72ffaaSAndy Whitcroft			}
8186c72ffaaSAndy Whitcroft
819c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
820c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
821c8cb2ca3SAndy Whitcroft			$type = 'V';
822cf655043SAndy Whitcroft			$av_pending = 'V';
8236c72ffaaSAndy Whitcroft
8241f65f947SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):/) {
8251f65f947SAndy Whitcroft			if ($type eq 'E') {
8261f65f947SAndy Whitcroft				$av_pend_colon = 'L';
8271f65f947SAndy Whitcroft			} elsif ($type eq 'T') {
8281f65f947SAndy Whitcroft				$av_pend_colon = 'B';
8291f65f947SAndy Whitcroft			}
8301f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
8311f65f947SAndy Whitcroft			$type = 'V';
8321f65f947SAndy Whitcroft
8336c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
834c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
8356c72ffaaSAndy Whitcroft			$type = 'V';
8366c72ffaaSAndy Whitcroft
8376c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
838c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
8396c72ffaaSAndy Whitcroft			$type = 'N';
8406c72ffaaSAndy Whitcroft
841cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
842c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
84313214adfSAndy Whitcroft			$type = 'E';
8441f65f947SAndy Whitcroft			$av_pend_colon = 'O';
84513214adfSAndy Whitcroft
8461f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
8471f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
8481f65f947SAndy Whitcroft			$type = 'N';
8491f65f947SAndy Whitcroft
8501f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
8511f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
8521f65f947SAndy Whitcroft
8531f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
8541f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
8551f65f947SAndy Whitcroft				$type = 'E';
8561f65f947SAndy Whitcroft			} else {
8571f65f947SAndy Whitcroft				$type = 'N';
8581f65f947SAndy Whitcroft			}
8591f65f947SAndy Whitcroft			$av_pend_colon = 'O';
8601f65f947SAndy Whitcroft
8611f65f947SAndy Whitcroft		} elsif ($cur =~ /^(;|\[)/o) {
86213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
8636c72ffaaSAndy Whitcroft			$type = 'N';
8646c72ffaaSAndy Whitcroft
8650d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
86674048ed8SAndy Whitcroft			my $variant;
86774048ed8SAndy Whitcroft
86874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
86974048ed8SAndy Whitcroft			if ($type eq 'V') {
87074048ed8SAndy Whitcroft				$variant = 'B';
87174048ed8SAndy Whitcroft			} else {
87274048ed8SAndy Whitcroft				$variant = 'U';
87374048ed8SAndy Whitcroft			}
87474048ed8SAndy Whitcroft
87574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
87674048ed8SAndy Whitcroft			$type = 'N';
87774048ed8SAndy Whitcroft
8786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
879c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
8806c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
8816c72ffaaSAndy Whitcroft				$type = 'N';
8826c72ffaaSAndy Whitcroft			}
8836c72ffaaSAndy Whitcroft
8846c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
885c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
8866c72ffaaSAndy Whitcroft		}
8876c72ffaaSAndy Whitcroft		if (defined $1) {
8886c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
8896c72ffaaSAndy Whitcroft			$res .= $type x length($1);
8906c72ffaaSAndy Whitcroft		}
8916c72ffaaSAndy Whitcroft	}
8926c72ffaaSAndy Whitcroft
8931f65f947SAndy Whitcroft	return ($res, $var);
8946c72ffaaSAndy Whitcroft}
8956c72ffaaSAndy Whitcroft
8968905a67cSAndy Whitcroftsub possible {
89713214adfSAndy Whitcroft	my ($possible, $line) = @_;
8988905a67cSAndy Whitcroft
899c45dcabdSAndy Whitcroft	print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
9000221f55cSAndy Whitcroft	if ($possible !~ /^(?:$Modifier|$Storage|$Type|DEFINE_\S+)$/ &&
9018905a67cSAndy Whitcroft	    $possible ne 'goto' && $possible ne 'return' &&
9028905a67cSAndy Whitcroft	    $possible ne 'case' && $possible ne 'else' &&
903d3ddcf47SAndy Whitcroft	    $possible ne 'asm' && $possible ne '__asm__' &&
904c45dcabdSAndy Whitcroft	    $possible !~ /^(typedef|struct|enum)\b/) {
905c45dcabdSAndy Whitcroft		# Check for modifiers.
906c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
907c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
908c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
909c45dcabdSAndy Whitcroft
910c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
911c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
912d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
913d2506586SAndy Whitcroft				warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
914d2506586SAndy Whitcroft				push(@modifierList, $modifier);
915d2506586SAndy Whitcroft			}
916c45dcabdSAndy Whitcroft
917c45dcabdSAndy Whitcroft		} else {
91813214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
9198905a67cSAndy Whitcroft			push(@typeList, $possible);
920c45dcabdSAndy Whitcroft		}
9218905a67cSAndy Whitcroft		build_types();
9228905a67cSAndy Whitcroft	}
9238905a67cSAndy Whitcroft}
9248905a67cSAndy Whitcroft
9256c72ffaaSAndy Whitcroftmy $prefix = '';
9266c72ffaaSAndy Whitcroft
927f0a594c1SAndy Whitcroftsub report {
928773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
929773647a0SAndy Whitcroft		return 0;
930773647a0SAndy Whitcroft	}
9318905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
9328905a67cSAndy Whitcroft
9338905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
9348905a67cSAndy Whitcroft
93513214adfSAndy Whitcroft	push(our @report, $line);
936773647a0SAndy Whitcroft
937773647a0SAndy Whitcroft	return 1;
938f0a594c1SAndy Whitcroft}
939f0a594c1SAndy Whitcroftsub report_dump {
94013214adfSAndy Whitcroft	our @report;
941f0a594c1SAndy Whitcroft}
942de7d4f0eSAndy Whitcroftsub ERROR {
943773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
944de7d4f0eSAndy Whitcroft		our $clean = 0;
9456c72ffaaSAndy Whitcroft		our $cnt_error++;
946de7d4f0eSAndy Whitcroft	}
947773647a0SAndy Whitcroft}
948de7d4f0eSAndy Whitcroftsub WARN {
949773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
950de7d4f0eSAndy Whitcroft		our $clean = 0;
9516c72ffaaSAndy Whitcroft		our $cnt_warn++;
952de7d4f0eSAndy Whitcroft	}
953773647a0SAndy Whitcroft}
954de7d4f0eSAndy Whitcroftsub CHK {
955773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
956de7d4f0eSAndy Whitcroft		our $clean = 0;
9576c72ffaaSAndy Whitcroft		our $cnt_chk++;
9586c72ffaaSAndy Whitcroft	}
959de7d4f0eSAndy Whitcroft}
960de7d4f0eSAndy Whitcroft
961*6ecd9674SAndy Whitcroftsub check_absolute_file {
962*6ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
963*6ecd9674SAndy Whitcroft	my $file = $absolute;
964*6ecd9674SAndy Whitcroft
965*6ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
966*6ecd9674SAndy Whitcroft
967*6ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
968*6ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
969*6ecd9674SAndy Whitcroft		if (-f "$root/$file") {
970*6ecd9674SAndy Whitcroft			##print "file<$file>\n";
971*6ecd9674SAndy Whitcroft			last;
972*6ecd9674SAndy Whitcroft		}
973*6ecd9674SAndy Whitcroft	}
974*6ecd9674SAndy Whitcroft	if (! -f _)  {
975*6ecd9674SAndy Whitcroft		return 0;
976*6ecd9674SAndy Whitcroft	}
977*6ecd9674SAndy Whitcroft
978*6ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
979*6ecd9674SAndy Whitcroft	my $prefix = $absolute;
980*6ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
981*6ecd9674SAndy Whitcroft
982*6ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
983*6ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
984*6ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
985*6ecd9674SAndy Whitcroft	}
986*6ecd9674SAndy Whitcroft}
987*6ecd9674SAndy Whitcroft
9880a920b5bSAndy Whitcroftsub process {
9890a920b5bSAndy Whitcroft	my $filename = shift;
9900a920b5bSAndy Whitcroft
9910a920b5bSAndy Whitcroft	my $linenr=0;
9920a920b5bSAndy Whitcroft	my $prevline="";
993c2fdda0dSAndy Whitcroft	my $prevrawline="";
9940a920b5bSAndy Whitcroft	my $stashline="";
995c2fdda0dSAndy Whitcroft	my $stashrawline="";
9960a920b5bSAndy Whitcroft
9974a0df2efSAndy Whitcroft	my $length;
9980a920b5bSAndy Whitcroft	my $indent;
9990a920b5bSAndy Whitcroft	my $previndent=0;
10000a920b5bSAndy Whitcroft	my $stashindent=0;
10010a920b5bSAndy Whitcroft
1002de7d4f0eSAndy Whitcroft	our $clean = 1;
10030a920b5bSAndy Whitcroft	my $signoff = 0;
10040a920b5bSAndy Whitcroft	my $is_patch = 0;
10050a920b5bSAndy Whitcroft
100613214adfSAndy Whitcroft	our @report = ();
10076c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
10086c72ffaaSAndy Whitcroft	our $cnt_error = 0;
10096c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
10106c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
10116c72ffaaSAndy Whitcroft
10120a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
10130a920b5bSAndy Whitcroft	my $realfile = '';
10140a920b5bSAndy Whitcroft	my $realline = 0;
10150a920b5bSAndy Whitcroft	my $realcnt = 0;
10160a920b5bSAndy Whitcroft	my $here = '';
10170a920b5bSAndy Whitcroft	my $in_comment = 0;
1018c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
10190a920b5bSAndy Whitcroft	my $first_line = 0;
10200a920b5bSAndy Whitcroft
102113214adfSAndy Whitcroft	my $prev_values = 'E';
102213214adfSAndy Whitcroft
102313214adfSAndy Whitcroft	# suppression flags
1024773647a0SAndy Whitcroft	my %suppress_ifbraces;
1025653d4876SAndy Whitcroft
1026c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1027de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1028c2fdda0dSAndy Whitcroft	#
1029de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1030de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1031773647a0SAndy Whitcroft
1032773647a0SAndy Whitcroft	sanitise_line_reset();
1033c2fdda0dSAndy Whitcroft	my $line;
1034c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1035773647a0SAndy Whitcroft		$linenr++;
1036773647a0SAndy Whitcroft		$line = $rawline;
1037c2fdda0dSAndy Whitcroft
1038773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1039de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1040de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1041de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1042de7d4f0eSAndy Whitcroft			}
1043773647a0SAndy Whitcroft			#next;
1044de7d4f0eSAndy Whitcroft		}
1045773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1046773647a0SAndy Whitcroft			$realline=$1-1;
1047773647a0SAndy Whitcroft			if (defined $2) {
1048773647a0SAndy Whitcroft				$realcnt=$3+1;
1049773647a0SAndy Whitcroft			} else {
1050773647a0SAndy Whitcroft				$realcnt=1+1;
1051773647a0SAndy Whitcroft			}
1052c45dcabdSAndy Whitcroft			$in_comment = 0;
1053773647a0SAndy Whitcroft
1054773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1055773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1056773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1057773647a0SAndy Whitcroft			# at context start.
1058773647a0SAndy Whitcroft			my $edge;
105901fa9147SAndy Whitcroft			my $cnt = $realcnt;
106001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
106101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
106201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
106301fa9147SAndy Whitcroft				$cnt--;
106401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
106501fa9147SAndy Whitcroft				($edge) = (defined $rawlines[$ln - 1] &&
106601fa9147SAndy Whitcroft					$rawlines[$ln - 1] =~ m@(/\*|\*/)@);
1067773647a0SAndy Whitcroft				last if (defined $edge);
1068773647a0SAndy Whitcroft			}
1069773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1070773647a0SAndy Whitcroft				$in_comment = 1;
1071773647a0SAndy Whitcroft			}
1072773647a0SAndy Whitcroft
1073773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1074773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1075773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1076773647a0SAndy Whitcroft			if (!defined $edge &&
1077773647a0SAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
1078773647a0SAndy Whitcroft			{
1079773647a0SAndy Whitcroft				$in_comment = 1;
1080773647a0SAndy Whitcroft			}
1081773647a0SAndy Whitcroft
1082773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1083773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1084773647a0SAndy Whitcroft
1085171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1086773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1087171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1088773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1089773647a0SAndy Whitcroft		}
1090773647a0SAndy Whitcroft		push(@lines, $line);
1091773647a0SAndy Whitcroft
1092773647a0SAndy Whitcroft		if ($realcnt > 1) {
1093773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1094773647a0SAndy Whitcroft		} else {
1095773647a0SAndy Whitcroft			$realcnt = 0;
1096773647a0SAndy Whitcroft		}
1097773647a0SAndy Whitcroft
1098773647a0SAndy Whitcroft		#print "==>$rawline\n";
1099773647a0SAndy Whitcroft		#print "-->$line\n";
1100de7d4f0eSAndy Whitcroft
1101de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1102de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1103de7d4f0eSAndy Whitcroft		}
1104de7d4f0eSAndy Whitcroft	}
1105de7d4f0eSAndy Whitcroft
11066c72ffaaSAndy Whitcroft	$prefix = '';
11076c72ffaaSAndy Whitcroft
1108773647a0SAndy Whitcroft	$realcnt = 0;
1109773647a0SAndy Whitcroft	$linenr = 0;
11100a920b5bSAndy Whitcroft	foreach my $line (@lines) {
11110a920b5bSAndy Whitcroft		$linenr++;
11120a920b5bSAndy Whitcroft
1113c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
11146c72ffaaSAndy Whitcroft
11150a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
11166c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
11170a920b5bSAndy Whitcroft			$is_patch = 1;
11184a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
11190a920b5bSAndy Whitcroft			$realline=$1-1;
11200a920b5bSAndy Whitcroft			if (defined $2) {
11210a920b5bSAndy Whitcroft				$realcnt=$3+1;
11220a920b5bSAndy Whitcroft			} else {
11230a920b5bSAndy Whitcroft				$realcnt=1+1;
11240a920b5bSAndy Whitcroft			}
1125c2fdda0dSAndy Whitcroft			annotate_reset();
112613214adfSAndy Whitcroft			$prev_values = 'E';
112713214adfSAndy Whitcroft
1128773647a0SAndy Whitcroft			%suppress_ifbraces = ();
11290a920b5bSAndy Whitcroft			next;
11300a920b5bSAndy Whitcroft
11314a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
11324a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
11334a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1134773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
11350a920b5bSAndy Whitcroft			$realline++;
1136d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
11370a920b5bSAndy Whitcroft
11384a0df2efSAndy Whitcroft			# Measure the line length and indent.
1139c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
11400a920b5bSAndy Whitcroft
11410a920b5bSAndy Whitcroft			# Track the previous line.
11420a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
11430a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1144c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1145c2fdda0dSAndy Whitcroft
1146773647a0SAndy Whitcroft			#warn "line<$line>\n";
11476c72ffaaSAndy Whitcroft
1148d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1149d8aaf121SAndy Whitcroft			$realcnt--;
11500a920b5bSAndy Whitcroft		}
11510a920b5bSAndy Whitcroft
11520a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1153773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1154773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1155773647a0SAndy Whitcroft
11566c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
11576c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1158773647a0SAndy Whitcroft
1159773647a0SAndy Whitcroft		# extract the filename as it passes
1160773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1161773647a0SAndy Whitcroft			$realfile = $1;
1162773647a0SAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
1163773647a0SAndy Whitcroft
1164c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1165773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1166773647a0SAndy Whitcroft			}
1167773647a0SAndy Whitcroft			next;
1168773647a0SAndy Whitcroft		}
1169773647a0SAndy Whitcroft
1170389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
11710a920b5bSAndy Whitcroft
1172c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1173c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1174c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
11750a920b5bSAndy Whitcroft
11766c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
11776c72ffaaSAndy Whitcroft
11780a920b5bSAndy Whitcroft#check the patch for a signoff:
1179d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
11804a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
11814a0df2efSAndy Whitcroft			$signoff++;
11820a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1183de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1184de7d4f0eSAndy Whitcroft					$herecurr);
11850a920b5bSAndy Whitcroft			}
11860a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1187773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1188de7d4f0eSAndy Whitcroft					$herecurr);
11890a920b5bSAndy Whitcroft			}
11900a920b5bSAndy Whitcroft		}
11910a920b5bSAndy Whitcroft
119200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
11938905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1194de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
11956c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1196de7d4f0eSAndy Whitcroft		}
1197de7d4f0eSAndy Whitcroft
1198*6ecd9674SAndy Whitcroft# Check for absolute kernel paths.
1199*6ecd9674SAndy Whitcroft		if ($tree) {
1200*6ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
1201*6ecd9674SAndy Whitcroft				my $file = $1;
1202*6ecd9674SAndy Whitcroft
1203*6ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1204*6ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
1205*6ecd9674SAndy Whitcroft					#
1206*6ecd9674SAndy Whitcroft				} else {
1207*6ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
1208*6ecd9674SAndy Whitcroft				}
1209*6ecd9674SAndy Whitcroft			}
1210*6ecd9674SAndy Whitcroft		}
1211*6ecd9674SAndy Whitcroft
1212de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1213de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1214171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1215171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1216171ae1a4SAndy Whitcroft
1217171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1218171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1219171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1220171ae1a4SAndy Whitcroft
1221171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
122200df344fSAndy Whitcroft		}
12230a920b5bSAndy Whitcroft
122400df344fSAndy Whitcroft#ignore lines being removed
122500df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
122600df344fSAndy Whitcroft
122700df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
122800df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
12290a920b5bSAndy Whitcroft
12300a920b5bSAndy Whitcroft#trailing whitespace
12319c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1232c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
12339c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
12349c0ca6f9SAndy Whitcroft
1235c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1236c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1237de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
12380a920b5bSAndy Whitcroft		}
12390a920b5bSAndy Whitcroft#80 column limit
1240c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1241f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1242f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1243f4c014c0SAndy Whitcroft		    $length > 80)
1244c45dcabdSAndy Whitcroft		{
1245de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
12460a920b5bSAndy Whitcroft		}
12470a920b5bSAndy Whitcroft
12488905a67cSAndy Whitcroft# check for adding lines without a newline.
12498905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
12508905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
12518905a67cSAndy Whitcroft		}
12528905a67cSAndy Whitcroft
12530a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
12540a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
12550a920b5bSAndy Whitcroft
12560a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
12570a920b5bSAndy Whitcroft# more than 8 must use tabs.
1258c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1259c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1260c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1261171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
12620a920b5bSAndy Whitcroft		}
12630a920b5bSAndy Whitcroft
1264c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1265cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1266c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1267c2fdda0dSAndy Whitcroft		}
126822f2a2efSAndy Whitcroft
12699c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1270f5fe35ddSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next);
12719c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1272f5fe35ddSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next) =
1273f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1274171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1275171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1276171ae1a4SAndy Whitcroft
1277171ae1a4SAndy Whitcroft			my $s = $stat;
1278171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1279cf655043SAndy Whitcroft
1280c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1281171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1282c2fdda0dSAndy Whitcroft
1283c2fdda0dSAndy Whitcroft			# Ignore functions being called
1284171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1285c2fdda0dSAndy Whitcroft
1286c45dcabdSAndy Whitcroft			# declarations always start with types
1287d2506586SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1288c45dcabdSAndy Whitcroft				my $type = $1;
1289c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1290c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1291c45dcabdSAndy Whitcroft
12926c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1293171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1294c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1295c2fdda0dSAndy Whitcroft			}
12968905a67cSAndy Whitcroft
12976c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1298171ae1a4SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1299c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
13009c0ca6f9SAndy Whitcroft			}
13018905a67cSAndy Whitcroft
13028905a67cSAndy Whitcroft			# Check for any sort of function declaration.
13038905a67cSAndy Whitcroft			# int foo(something bar, other baz);
13048905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1305171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
13068905a67cSAndy Whitcroft				my ($name_len) = length($1);
13078905a67cSAndy Whitcroft
1308cf655043SAndy Whitcroft				my $ctx = $s;
1309773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
13108905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1311cf655043SAndy Whitcroft
13128905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1313c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
13148905a67cSAndy Whitcroft
1315c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
13168905a67cSAndy Whitcroft					}
13178905a67cSAndy Whitcroft				}
13188905a67cSAndy Whitcroft			}
13198905a67cSAndy Whitcroft
13209c0ca6f9SAndy Whitcroft		}
13219c0ca6f9SAndy Whitcroft
132200df344fSAndy Whitcroft#
132300df344fSAndy Whitcroft# Checks which may be anchored in the context.
132400df344fSAndy Whitcroft#
132500df344fSAndy Whitcroft
132600df344fSAndy Whitcroft# Check for switch () and associated case and default
132700df344fSAndy Whitcroft# statements should be at the same indent.
132800df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
132900df344fSAndy Whitcroft			my $err = '';
133000df344fSAndy Whitcroft			my $sep = '';
133100df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
133200df344fSAndy Whitcroft			shift(@ctx);
133300df344fSAndy Whitcroft			for my $ctx (@ctx) {
133400df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
133500df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
133600df344fSAndy Whitcroft							$indent != $cindent) {
133700df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
133800df344fSAndy Whitcroft					$sep = '';
133900df344fSAndy Whitcroft				} else {
134000df344fSAndy Whitcroft					$sep = "[...]\n";
134100df344fSAndy Whitcroft				}
134200df344fSAndy Whitcroft			}
134300df344fSAndy Whitcroft			if ($err ne '') {
13449c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1345de7d4f0eSAndy Whitcroft			}
1346de7d4f0eSAndy Whitcroft		}
1347e2a763c2SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
13481bdab9e5SAndy Whitcroft		    $line !~ /\G(?:
13491bdab9e5SAndy Whitcroft			(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
13501bdab9e5SAndy Whitcroft			\s*return\s+
13511bdab9e5SAndy Whitcroft		    )/xg)
13521bdab9e5SAndy Whitcroft		{
1353e2a763c2SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
1354e2a763c2SAndy Whitcroft		}
1355de7d4f0eSAndy Whitcroft
1356de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1357de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1358c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1359773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1360773647a0SAndy Whitcroft
13619c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1362de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1363de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1364de7d4f0eSAndy Whitcroft
1365548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1366548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1367de7d4f0eSAndy Whitcroft
1368548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1369548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1370548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1371548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1372548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1373773647a0SAndy Whitcroft				$ctx_ln++;
1374773647a0SAndy Whitcroft			}
1375548596d5SAndy Whitcroft
137653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
137753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1378773647a0SAndy Whitcroft
1379773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1380773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1381c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
138200df344fSAndy Whitcroft			}
1383773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1384773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1385773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1386773647a0SAndy Whitcroft			{
13879c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
13889c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1389773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1390c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
13919c0ca6f9SAndy Whitcroft				}
13929c0ca6f9SAndy Whitcroft			}
139300df344fSAndy Whitcroft		}
139400df344fSAndy Whitcroft
13956c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
13966c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
13971f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
13981f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
13996c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1400c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1401c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1402cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1403cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
14041f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1405c2fdda0dSAndy Whitcroft		}
14066c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
14076c72ffaaSAndy Whitcroft
140800df344fSAndy Whitcroft#ignore lines not being added
140900df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
141000df344fSAndy Whitcroft
1411653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
14127429c690SAndy Whitcroft		if ($dbg_type) {
14137429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
14147429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
14157429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
14167429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
14177429c690SAndy Whitcroft			}
1418653d4876SAndy Whitcroft			next;
1419653d4876SAndy Whitcroft		}
1420a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1421a1ef277eSAndy Whitcroft		if ($dbg_attr) {
1422a1ef277eSAndy Whitcroft			if ($line =~ /^.\s*$Attribute\s*$/) {
1423a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
1424a1ef277eSAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1425a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1426a1ef277eSAndy Whitcroft			}
1427a1ef277eSAndy Whitcroft			next;
1428a1ef277eSAndy Whitcroft		}
1429653d4876SAndy Whitcroft
1430f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1431f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1432f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1433773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1434f0a594c1SAndy Whitcroft		}
1435f0a594c1SAndy Whitcroft
143600df344fSAndy Whitcroft#
143700df344fSAndy Whitcroft# Checks which are anchored on the added line.
143800df344fSAndy Whitcroft#
143900df344fSAndy Whitcroft
1440653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1441c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1442653d4876SAndy Whitcroft			my $path = $1;
1443653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1444de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1445de7d4f0eSAndy Whitcroft					$herecurr);
1446653d4876SAndy Whitcroft			}
1447653d4876SAndy Whitcroft		}
1448653d4876SAndy Whitcroft
144900df344fSAndy Whitcroft# no C99 // comments
145000df344fSAndy Whitcroft		if ($line =~ m{//}) {
1451de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
145200df344fSAndy Whitcroft		}
145300df344fSAndy Whitcroft		# Remove C99 comments.
14540a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
14556c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
14560a920b5bSAndy Whitcroft
14570a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1458653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1459653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1460653d4876SAndy Whitcroft			my $name = $1;
14610a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
14620a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
1463653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
1464cf655043SAndy Whitcroft			   ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1465cf655043SAndy Whitcroft			   ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1466171ae1a4SAndy Whitcroft			   ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
1467cf655043SAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1468de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
14690a920b5bSAndy Whitcroft			}
14700a920b5bSAndy Whitcroft		}
14710a920b5bSAndy Whitcroft
1472f0a594c1SAndy Whitcroft# check for external initialisers.
1473c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1474f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1475f0a594c1SAndy Whitcroft				$herecurr);
1476f0a594c1SAndy Whitcroft		}
14770a920b5bSAndy Whitcroft# check for static initialisers.
14789c9ba34eSAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1479de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1480de7d4f0eSAndy Whitcroft				$herecurr);
14810a920b5bSAndy Whitcroft		}
14820a920b5bSAndy Whitcroft
1483653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1484653d4876SAndy Whitcroft# make sense.
1485653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
14869c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1487c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1488653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1489de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
14900a920b5bSAndy Whitcroft		}
14910a920b5bSAndy Whitcroft
14920a920b5bSAndy Whitcroft# * goes on variable not on type
1493d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1494de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1495de7d4f0eSAndy Whitcroft				$herecurr);
1496d8aaf121SAndy Whitcroft
1497d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1498de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1499de7d4f0eSAndy Whitcroft				$herecurr);
1500d8aaf121SAndy Whitcroft
1501234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1502de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1503de7d4f0eSAndy Whitcroft				$herecurr);
1504d8aaf121SAndy Whitcroft
1505234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1506de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1507de7d4f0eSAndy Whitcroft				$herecurr);
15080a920b5bSAndy Whitcroft		}
15090a920b5bSAndy Whitcroft
15100a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
15110a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
15120a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
15130a920b5bSAndy Whitcroft# 			print "$herecurr";
15140a920b5bSAndy Whitcroft# 			$clean = 0;
15150a920b5bSAndy Whitcroft# 		}
15160a920b5bSAndy Whitcroft
15178905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1518c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
15198905a67cSAndy Whitcroft		}
15208905a67cSAndy Whitcroft
152100df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
152200df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
152300df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
152400df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
152500df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1526f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
152700df344fSAndy Whitcroft			my $ok = 0;
152800df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
152900df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
153000df344fSAndy Whitcroft				# we have a preceeding printk if it ends
153100df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
153200df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
153300df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
153400df344fSAndy Whitcroft						$ok = 1;
153500df344fSAndy Whitcroft					}
153600df344fSAndy Whitcroft					last;
153700df344fSAndy Whitcroft				}
153800df344fSAndy Whitcroft			}
153900df344fSAndy Whitcroft			if ($ok == 0) {
1540de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
15410a920b5bSAndy Whitcroft			}
154200df344fSAndy Whitcroft		}
15430a920b5bSAndy Whitcroft
1544653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1545653d4876SAndy Whitcroft# or if closed on same line
1546c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1547c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1548de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
15490a920b5bSAndy Whitcroft		}
1550653d4876SAndy Whitcroft
15518905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
15528905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
15538905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
15548905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
15558905a67cSAndy Whitcroft		}
15568905a67cSAndy Whitcroft
15578d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
15588d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1559fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1560fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
15618d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
15628d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
15638d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1564fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1565fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
15668d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
15678d31cfceSAndy Whitcroft			}
15688d31cfceSAndy Whitcroft		}
15698d31cfceSAndy Whitcroft
1570f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
15716c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1572c2fdda0dSAndy Whitcroft			my $name = $1;
1573773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1574773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1575c2fdda0dSAndy Whitcroft
1576c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1577773647a0SAndy Whitcroft			if ($name =~ /^(?:
1578773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1579773647a0SAndy Whitcroft				volatile|__volatile__|
1580773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1581773647a0SAndy Whitcroft				asm|__asm__)$/x)
1582773647a0SAndy Whitcroft			{
1583c2fdda0dSAndy Whitcroft
1584c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1585c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1586c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1587c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1588773647a0SAndy Whitcroft
1589773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1590c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1591c2fdda0dSAndy Whitcroft
1592c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1593c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1594773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1595c2fdda0dSAndy Whitcroft
1596c2fdda0dSAndy Whitcroft			} else {
1597773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1598f0a594c1SAndy Whitcroft			}
15996c72ffaaSAndy Whitcroft		}
1600653d4876SAndy Whitcroft# Check operator spacing.
16010a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
16029c0ca6f9SAndy Whitcroft			my $ops = qr{
16039c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
16049c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
16059c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
16061f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
16071f65f947SAndy Whitcroft				\?|:
16089c0ca6f9SAndy Whitcroft			}x;
1609cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
161000df344fSAndy Whitcroft			my $off = 0;
16116c72ffaaSAndy Whitcroft
16126c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
16136c72ffaaSAndy Whitcroft
16140a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
16154a0df2efSAndy Whitcroft				$off += length($elements[$n]);
16164a0df2efSAndy Whitcroft
1617773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1618773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1619773647a0SAndy Whitcroft				my $cc = '';
1620773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1621773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1622773647a0SAndy Whitcroft				}
1623773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1624773647a0SAndy Whitcroft
16254a0df2efSAndy Whitcroft				my $a = '';
16264a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
16274a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1628cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
16294a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
16304a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1631773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
16324a0df2efSAndy Whitcroft
16330a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
16344a0df2efSAndy Whitcroft
16354a0df2efSAndy Whitcroft				my $c = '';
16360a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
16374a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
16384a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1639cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
16404a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
16414a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
164222f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
16434a0df2efSAndy Whitcroft				} else {
16444a0df2efSAndy Whitcroft					$c = 'E';
16450a920b5bSAndy Whitcroft				}
16460a920b5bSAndy Whitcroft
16474a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
16484a0df2efSAndy Whitcroft
16494a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
16504a0df2efSAndy Whitcroft
16516c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1652de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
16530a920b5bSAndy Whitcroft
165474048ed8SAndy Whitcroft				# Pull out the value of this operator.
16556c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
16560a920b5bSAndy Whitcroft
16571f65f947SAndy Whitcroft				# Get the full operator variant.
16581f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
16591f65f947SAndy Whitcroft
166013214adfSAndy Whitcroft				# Ignore operators passed as parameters.
166113214adfSAndy Whitcroft				if ($op_type ne 'V' &&
166213214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
166313214adfSAndy Whitcroft
1664cf655043SAndy Whitcroft#				# Ignore comments
1665cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
166613214adfSAndy Whitcroft
1667d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
166813214adfSAndy Whitcroft				} elsif ($op eq ';') {
1669cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1670cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1671773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1672d8aaf121SAndy Whitcroft					}
1673d8aaf121SAndy Whitcroft
1674d8aaf121SAndy Whitcroft				# // is a comment
1675d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
16760a920b5bSAndy Whitcroft
16771f65f947SAndy Whitcroft				# No spaces for:
16781f65f947SAndy Whitcroft				#   ->
16791f65f947SAndy Whitcroft				#   :   when part of a bitfield
16801f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
16814a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1682773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
16830a920b5bSAndy Whitcroft					}
16840a920b5bSAndy Whitcroft
16850a920b5bSAndy Whitcroft				# , must have a space on the right.
16860a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1687cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1688773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
16890a920b5bSAndy Whitcroft					}
16900a920b5bSAndy Whitcroft
16919c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
169274048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
16939c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
16949c0ca6f9SAndy Whitcroft
16959c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
16969c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
16979c0ca6f9SAndy Whitcroft				# unary operator, or a cast
16989c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
169974048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
17000d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1701cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1702773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
17030a920b5bSAndy Whitcroft					}
1704171ae1a4SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*const\b/) {
1705171ae1a4SAndy Whitcroft						# A unary '*' may be const
1706171ae1a4SAndy Whitcroft
1707171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1708773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
17090a920b5bSAndy Whitcroft					}
17100a920b5bSAndy Whitcroft
17110a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
17120a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1713773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1714773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
17150a920b5bSAndy Whitcroft					}
1716773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1717773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1718773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1719653d4876SAndy Whitcroft					}
1720773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1721773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1722773647a0SAndy Whitcroft					}
1723773647a0SAndy Whitcroft
17240a920b5bSAndy Whitcroft
17250a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
17269c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
17279c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
17289c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1729c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1730c2fdda0dSAndy Whitcroft					 $op eq '%')
17310a920b5bSAndy Whitcroft				{
1732773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1733de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1734de7d4f0eSAndy Whitcroft							$hereptr);
17350a920b5bSAndy Whitcroft					}
17360a920b5bSAndy Whitcroft
17371f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
17381f65f947SAndy Whitcroft				# terminating a case value or a label.
17391f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
17401f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
17411f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
17421f65f947SAndy Whitcroft					}
17431f65f947SAndy Whitcroft
17440a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1745cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
17461f65f947SAndy Whitcroft					my $ok = 0;
17471f65f947SAndy Whitcroft
174822f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
17491f65f947SAndy Whitcroft					if (($op eq '<' &&
17501f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
17511f65f947SAndy Whitcroft					    ($op eq '>' &&
17521f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
17531f65f947SAndy Whitcroft					{
17541f65f947SAndy Whitcroft					    	$ok = 1;
17551f65f947SAndy Whitcroft					}
17561f65f947SAndy Whitcroft
17571f65f947SAndy Whitcroft					# Ignore ?:
17581f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
17591f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
17601f65f947SAndy Whitcroft					    	$ok = 1;
17611f65f947SAndy Whitcroft					}
17621f65f947SAndy Whitcroft
17631f65f947SAndy Whitcroft					if ($ok == 0) {
1764773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
17650a920b5bSAndy Whitcroft					}
176622f2a2efSAndy Whitcroft				}
17674a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
17680a920b5bSAndy Whitcroft			}
17690a920b5bSAndy Whitcroft		}
17700a920b5bSAndy Whitcroft
1771f0a594c1SAndy Whitcroft# check for multiple assignments
1772f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
17736c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1774f0a594c1SAndy Whitcroft		}
1775f0a594c1SAndy Whitcroft
177622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
177722f2a2efSAndy Whitcroft## # continuation.
177822f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
177922f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
178022f2a2efSAndy Whitcroft##
178122f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
178222f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
178322f2a2efSAndy Whitcroft## 			my $ln = $line;
178422f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
178522f2a2efSAndy Whitcroft## 			}
178622f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
178722f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
178822f2a2efSAndy Whitcroft## 			}
178922f2a2efSAndy Whitcroft## 		}
1790f0a594c1SAndy Whitcroft
17910a920b5bSAndy Whitcroft#need space before brace following if, while, etc
179222f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
179322f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1794773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1795de7d4f0eSAndy Whitcroft		}
1796de7d4f0eSAndy Whitcroft
1797de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1798de7d4f0eSAndy Whitcroft# on the line
1799de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1800773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
18010a920b5bSAndy Whitcroft		}
18020a920b5bSAndy Whitcroft
180322f2a2efSAndy Whitcroft# check spacing on square brackets
180422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1805773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
180622f2a2efSAndy Whitcroft		}
180722f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1808773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
180922f2a2efSAndy Whitcroft		}
181022f2a2efSAndy Whitcroft
1811c45dcabdSAndy Whitcroft# check spacing on parentheses
18129c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
18139c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1814773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
181522f2a2efSAndy Whitcroft		}
181613214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1817c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
1818c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
1819773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
182022f2a2efSAndy Whitcroft		}
182122f2a2efSAndy Whitcroft
18220a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
18234a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
18240a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1825de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
18260a920b5bSAndy Whitcroft		}
18270a920b5bSAndy Whitcroft
1828c45dcabdSAndy Whitcroft# Return is not a function.
1829c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1830c45dcabdSAndy Whitcroft			my $spacing = $1;
1831c45dcabdSAndy Whitcroft			my $value = $2;
1832c45dcabdSAndy Whitcroft
1833c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
1834fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
1835c45dcabdSAndy Whitcroft			while ($value =~ s/\([^\(\)]*\)/1/) {
1836c45dcabdSAndy Whitcroft			}
1837c45dcabdSAndy Whitcroft
1838c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1839c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1840c45dcabdSAndy Whitcroft
1841c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
1842c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
1843c45dcabdSAndy Whitcroft			}
1844c45dcabdSAndy Whitcroft		}
1845c45dcabdSAndy Whitcroft
18460a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
18474a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1848773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
18490a920b5bSAndy Whitcroft		}
18500a920b5bSAndy Whitcroft
1851f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
1852f5fe35ddSAndy Whitcroft# statements after the conditional.
185353210168SAndy Whitcroft		if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
1854171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
18558905a67cSAndy Whitcroft
18568905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1857c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
18588905a67cSAndy Whitcroft			}
18598905a67cSAndy Whitcroft
18608905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
18618905a67cSAndy Whitcroft			# conditional.
1862773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
18638905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
186413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
186553210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
186653210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
1867773647a0SAndy Whitcroft			{
18688905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
18698905a67cSAndy Whitcroft			}
18708905a67cSAndy Whitcroft		}
18718905a67cSAndy Whitcroft
1872f5fe35ddSAndy Whitcroft# Check relative indent for conditionals and blocks.
1873f5fe35ddSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1874f5fe35ddSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
1875f5fe35ddSAndy Whitcroft
1876f5fe35ddSAndy Whitcroft			substr($s, 0, length($c), '');
1877f5fe35ddSAndy Whitcroft
1878f5fe35ddSAndy Whitcroft			# Make sure we remove the line prefixes as we have
1879f5fe35ddSAndy Whitcroft			# none on the first line, and are going to readd them
1880f5fe35ddSAndy Whitcroft			# where necessary.
1881f5fe35ddSAndy Whitcroft			$s =~ s/\n./\n/gs;
1882f5fe35ddSAndy Whitcroft
1883f5fe35ddSAndy Whitcroft			# We want to check the first line inside the block
1884f5fe35ddSAndy Whitcroft			# starting at the end of the conditional, so remove:
1885f5fe35ddSAndy Whitcroft			#  1) any blank line termination
1886f5fe35ddSAndy Whitcroft			#  2) any opening brace { on end of the line
1887f5fe35ddSAndy Whitcroft			#  3) any do (...) {
1888f5fe35ddSAndy Whitcroft			my $continuation = 0;
1889f5fe35ddSAndy Whitcroft			my $check = 0;
1890f5fe35ddSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
1891f5fe35ddSAndy Whitcroft			$s =~ s/^\s*{//;
1892f5fe35ddSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
1893f5fe35ddSAndy Whitcroft				$continuation = 1;
1894f5fe35ddSAndy Whitcroft			}
1895f5fe35ddSAndy Whitcroft			if ($s =~ s/^\s*\n//) {
1896f5fe35ddSAndy Whitcroft				$check = 1;
1897f5fe35ddSAndy Whitcroft			}
1898f5fe35ddSAndy Whitcroft
1899f5fe35ddSAndy Whitcroft			# Also ignore a loop construct at the end of a
1900f5fe35ddSAndy Whitcroft			# preprocessor statement.
1901f5fe35ddSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
1902f5fe35ddSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
1903f5fe35ddSAndy Whitcroft				$check = 0;
1904f5fe35ddSAndy Whitcroft			}
1905f5fe35ddSAndy Whitcroft
1906f5fe35ddSAndy Whitcroft			# Ignore the current line if its is a preprocessor
1907f5fe35ddSAndy Whitcroft			# line.
1908f5fe35ddSAndy Whitcroft			if ($s =~ /^\s*#\s*/) {
1909f5fe35ddSAndy Whitcroft				$check = 0;
1910f5fe35ddSAndy Whitcroft			}
1911f5fe35ddSAndy Whitcroft
191214b111c1SAndy Whitcroft			# Ignore the current line if it is label.
191314b111c1SAndy Whitcroft			if ($s =~ /^\s*$Ident\s*:/) {
191414b111c1SAndy Whitcroft				$check = 0;
191514b111c1SAndy Whitcroft			}
191614b111c1SAndy Whitcroft
1917f5fe35ddSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
1918f5fe35ddSAndy Whitcroft
1919f5fe35ddSAndy Whitcroft			##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n";
1920f5fe35ddSAndy Whitcroft
1921f5fe35ddSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
1922f5fe35ddSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
1923f5fe35ddSAndy Whitcroft				WARN("suspect code indent for conditional statements\n" . $herecurr);
1924f5fe35ddSAndy Whitcroft			}
1925f5fe35ddSAndy Whitcroft		}
1926f5fe35ddSAndy Whitcroft
192713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
192813214adfSAndy Whitcroft		if ($line =~ /
192913214adfSAndy Whitcroft			(?:
193013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
193113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
193213214adfSAndy Whitcroft				(?:\&\&|\|\|)
193313214adfSAndy Whitcroft			|
193413214adfSAndy Whitcroft				(?:\&\&|\|\|)
193513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
193613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
193713214adfSAndy Whitcroft			)/x)
193813214adfSAndy Whitcroft		{
193913214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
194013214adfSAndy Whitcroft		}
194113214adfSAndy Whitcroft
19428905a67cSAndy Whitcroft# if and else should not have general statements after it
194313214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
194413214adfSAndy Whitcroft			my $s = $1;
194513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
194613214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
19478905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
19480a920b5bSAndy Whitcroft			}
194913214adfSAndy Whitcroft		}
19500a920b5bSAndy Whitcroft
19510a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
19520a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
19530a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
19540a920b5bSAndy Whitcroft						$previndent == $indent) {
1955de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
19560a920b5bSAndy Whitcroft		}
19570a920b5bSAndy Whitcroft
1958c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1959c2fdda0dSAndy Whitcroft						$previndent == $indent) {
1960c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1961c2fdda0dSAndy Whitcroft
1962c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
1963c2fdda0dSAndy Whitcroft			# conditional.
1964773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
1965c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
1966c2fdda0dSAndy Whitcroft
1967c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
1968c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
1969c2fdda0dSAndy Whitcroft			}
1970c2fdda0dSAndy Whitcroft		}
1971c2fdda0dSAndy Whitcroft
19720a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
19730a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
19740a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
19750a920b5bSAndy Whitcroft#		    print "$herecurr";
19760a920b5bSAndy Whitcroft#		    $clean = 0;
19770a920b5bSAndy Whitcroft#		}
19780a920b5bSAndy Whitcroft
19790a920b5bSAndy Whitcroft#no spaces allowed after \ in define
1980c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
1981de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
19820a920b5bSAndy Whitcroft		}
19830a920b5bSAndy Whitcroft
1984653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1985c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
1986e09dec48SAndy Whitcroft			my $file = "$1.h";
1987e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
1988e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
1989e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
1990c45dcabdSAndy Whitcroft			    $1 ne 'irq')
1991c45dcabdSAndy Whitcroft			{
1992e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
1993e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
1994e09dec48SAndy Whitcroft				} else {
1995e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
1996e09dec48SAndy Whitcroft				}
19970a920b5bSAndy Whitcroft			}
19980a920b5bSAndy Whitcroft		}
19990a920b5bSAndy Whitcroft
2000653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2001653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2002cf655043SAndy Whitcroft# in a known good container
2003b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2004b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2005d8aaf121SAndy Whitcroft			my $ln = $linenr;
2006d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2007c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2008c45dcabdSAndy Whitcroft			my $ctx = '';
2009653d4876SAndy Whitcroft
2010c45dcabdSAndy Whitcroft			my $args = defined($1);
2011de7d4f0eSAndy Whitcroft
2012c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2013c45dcabdSAndy Whitcroft			# search to that.
2014c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2015c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2016c45dcabdSAndy Whitcroft			{
2017c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2018a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2019c45dcabdSAndy Whitcroft				$ln++;
2020de7d4f0eSAndy Whitcroft			}
2021c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2022d8aaf121SAndy Whitcroft
2023c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2024c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2025c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2026a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2027c45dcabdSAndy Whitcroft
2028c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2029c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2030c45dcabdSAndy Whitcroft			$rest = '';
2031636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2032636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2033a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2034c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2035c45dcabdSAndy Whitcroft					$cnt--;
2036a3bb97a7SAndy Whitcroft				}
2037a3bb97a7SAndy Whitcroft				$ln++;
2038c45dcabdSAndy Whitcroft				$off = 0;
2039c45dcabdSAndy Whitcroft			}
2040c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2041c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2042c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2043c45dcabdSAndy Whitcroft
2044c45dcabdSAndy Whitcroft			# Clean up the original statement.
2045c45dcabdSAndy Whitcroft			if ($args) {
2046c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2047d8aaf121SAndy Whitcroft			} else {
2048c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2049c45dcabdSAndy Whitcroft			}
2050292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2051c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2052c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2053c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2054c45dcabdSAndy Whitcroft
2055c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2056c45dcabdSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/) {
2057c45dcabdSAndy Whitcroft			}
2058c45dcabdSAndy Whitcroft			while ($dstat =~ s/\{[^\{\}]*\}/1/) {
2059c45dcabdSAndy Whitcroft			}
2060c45dcabdSAndy Whitcroft
2061c45dcabdSAndy Whitcroft			my $exceptions = qr{
2062c45dcabdSAndy Whitcroft				$Declare|
2063c45dcabdSAndy Whitcroft				module_param_named|
2064c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2065c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2066c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2067c45dcabdSAndy Whitcroft				__typeof__\(
2068c45dcabdSAndy Whitcroft			}x;
2069a3bb97a7SAndy Whitcroft			#print "REST<$rest>\n";
2070c45dcabdSAndy Whitcroft			if ($rest ne '') {
2071c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2072c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2073c45dcabdSAndy Whitcroft				{
2074c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2075c45dcabdSAndy Whitcroft				}
2076c45dcabdSAndy Whitcroft
2077c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2078c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2079c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2080c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2081c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2082c45dcabdSAndy Whitcroft				{
2083de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2084d8aaf121SAndy Whitcroft				}
20850a920b5bSAndy Whitcroft			}
2086653d4876SAndy Whitcroft		}
20870a920b5bSAndy Whitcroft
2088f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
208913214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
209013214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2091cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
209213214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2093cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2094cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
209513214adfSAndy Whitcroft				my $allowed = 0;
209613214adfSAndy Whitcroft				my $seen = 0;
2097773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2098cf655043SAndy Whitcroft				my $ln = $linenr - 1;
209913214adfSAndy Whitcroft				for my $chunk (@chunks) {
210013214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
210113214adfSAndy Whitcroft
2102773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2103773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2104773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2105773647a0SAndy Whitcroft
2106773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2107773647a0SAndy Whitcroft
2108773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2109773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2110773647a0SAndy Whitcroft
2111773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2112cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2113cf655043SAndy Whitcroft
2114773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
211513214adfSAndy Whitcroft
211613214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
211713214adfSAndy Whitcroft
2118cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2119cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2120cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
212113214adfSAndy Whitcroft						$allowed = 1;
212213214adfSAndy Whitcroft					}
212313214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2124cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
212513214adfSAndy Whitcroft						$allowed = 1;
212613214adfSAndy Whitcroft					}
2127cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2128cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
212913214adfSAndy Whitcroft						$allowed = 1;
213013214adfSAndy Whitcroft					}
213113214adfSAndy Whitcroft				}
213213214adfSAndy Whitcroft				if ($seen && !$allowed) {
2133cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
213413214adfSAndy Whitcroft				}
213513214adfSAndy Whitcroft			}
213613214adfSAndy Whitcroft		}
2137773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
213813214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2139cf655043SAndy Whitcroft			my $allowed = 0;
2140f0a594c1SAndy Whitcroft
2141cf655043SAndy Whitcroft			# Check the pre-context.
2142cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2143cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2144cf655043SAndy Whitcroft				$allowed = 1;
2145f0a594c1SAndy Whitcroft			}
2146773647a0SAndy Whitcroft
2147773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2148773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2149773647a0SAndy Whitcroft
2150cf655043SAndy Whitcroft			# Check the condition.
2151cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2152773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2153cf655043SAndy Whitcroft			if (defined $cond) {
2154773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2155cf655043SAndy Whitcroft			}
2156cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2157cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2158cf655043SAndy Whitcroft				$allowed = 1;
2159cf655043SAndy Whitcroft			}
2160cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2161cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2162cf655043SAndy Whitcroft				$allowed = 1;
2163cf655043SAndy Whitcroft			}
2164cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2165cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2166cf655043SAndy Whitcroft				$allowed = 1;
2167cf655043SAndy Whitcroft			}
2168cf655043SAndy Whitcroft			# Check the post-context.
2169cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2170cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2171cf655043SAndy Whitcroft				if (defined $cond) {
2172773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2173cf655043SAndy Whitcroft				}
2174cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2175cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2176cf655043SAndy Whitcroft					$allowed = 1;
2177cf655043SAndy Whitcroft				}
2178cf655043SAndy Whitcroft			}
2179cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2180cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2181cf655043SAndy Whitcroft				my $end = $linenr + statement_rawlines($block) - 1;
2182cf655043SAndy Whitcroft
2183cf655043SAndy Whitcroft				for (my $ln = $linenr - 1; $ln < $end; $ln++) {
2184cf655043SAndy Whitcroft					$herectx .= $rawlines[$ln] . "\n";;
2185cf655043SAndy Whitcroft				}
2186cf655043SAndy Whitcroft
2187cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2188f0a594c1SAndy Whitcroft			}
2189f0a594c1SAndy Whitcroft		}
2190f0a594c1SAndy Whitcroft
2191653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
21924a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2193c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2194de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
21950a920b5bSAndy Whitcroft			}
21960a920b5bSAndy Whitcroft		}
21970a920b5bSAndy Whitcroft
21984a0df2efSAndy Whitcroft# don't use deprecated functions
21994a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
220000df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2201de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
22024a0df2efSAndy Whitcroft			}
22034a0df2efSAndy Whitcroft		}
22044a0df2efSAndy Whitcroft
22054a0df2efSAndy Whitcroft# no volatiles please
22066c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
22076c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2208de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
22094a0df2efSAndy Whitcroft		}
22104a0df2efSAndy Whitcroft
22119c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
22129c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
22139c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
22149c0ca6f9SAndy Whitcroft		}
22159c0ca6f9SAndy Whitcroft
221600df344fSAndy Whitcroft# warn about #if 0
2217c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2218de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2219de7d4f0eSAndy Whitcroft				$herecurr);
22204a0df2efSAndy Whitcroft		}
22214a0df2efSAndy Whitcroft
2222f0a594c1SAndy Whitcroft# check for needless kfree() checks
2223f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2224f0a594c1SAndy Whitcroft			my $expr = $1;
2225f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
22263c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2227f0a594c1SAndy Whitcroft			}
2228f0a594c1SAndy Whitcroft		}
22294c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
22304c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
22314c432a8fSGreg Kroah-Hartman			my $expr = $1;
22324c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
22334c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
22344c432a8fSGreg Kroah-Hartman			}
22354c432a8fSGreg Kroah-Hartman		}
2236f0a594c1SAndy Whitcroft
223700df344fSAndy Whitcroft# warn about #ifdefs in C files
2238c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
223900df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
224000df344fSAndy Whitcroft#			print "$herecurr";
224100df344fSAndy Whitcroft#			$clean = 0;
224200df344fSAndy Whitcroft#		}
224300df344fSAndy Whitcroft
224422f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2245c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
224622f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
224722f2a2efSAndy Whitcroft		}
224822f2a2efSAndy Whitcroft
22494a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2250171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2251171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
22524a0df2efSAndy Whitcroft			my $which = $1;
22534a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2254de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
22554a0df2efSAndy Whitcroft			}
22564a0df2efSAndy Whitcroft		}
22574a0df2efSAndy Whitcroft# check for memory barriers without a comment.
22584a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
22594a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2260de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
22614a0df2efSAndy Whitcroft			}
22624a0df2efSAndy Whitcroft		}
22634a0df2efSAndy Whitcroft# check of hardware specific defines
2264c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2265de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
22660a920b5bSAndy Whitcroft		}
2267653d4876SAndy Whitcroft
2268de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2269de7d4f0eSAndy Whitcroft# storage class and type.
22709c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
22719c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2272de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2273de7d4f0eSAndy Whitcroft		}
2274de7d4f0eSAndy Whitcroft
22758905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
22768905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
22778905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
22788905a67cSAndy Whitcroft		}
22798905a67cSAndy Whitcroft
2280de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2281171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2282c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2283171ae1a4SAndy Whitcroft		{
2284c45dcabdSAndy Whitcroft			my $function_name = $1;
2285c45dcabdSAndy Whitcroft			my $paren_space = $2;
2286171ae1a4SAndy Whitcroft
2287171ae1a4SAndy Whitcroft			my $s = $stat;
2288171ae1a4SAndy Whitcroft			if (defined $cond) {
2289171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2290171ae1a4SAndy Whitcroft			}
2291c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2292c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2293c45dcabdSAndy Whitcroft			{
2294de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2295de7d4f0eSAndy Whitcroft			}
2296de7d4f0eSAndy Whitcroft
2297171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2298171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2299171ae1a4SAndy Whitcroft			}
23009c9ba34eSAndy Whitcroft
23019c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
23029c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
23039c9ba34eSAndy Whitcroft		{
23049c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2305171ae1a4SAndy Whitcroft		}
2306171ae1a4SAndy Whitcroft
2307de7d4f0eSAndy Whitcroft# checks for new __setup's
2308de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2309de7d4f0eSAndy Whitcroft			my $name = $1;
2310de7d4f0eSAndy Whitcroft
2311de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2312de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2313de7d4f0eSAndy Whitcroft			}
2314653d4876SAndy Whitcroft		}
23159c0ca6f9SAndy Whitcroft
23169c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
23179c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
23189c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
23199c0ca6f9SAndy Whitcroft		}
232013214adfSAndy Whitcroft
232113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
232213214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
232313214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
232413214adfSAndy Whitcroft		}
2325773647a0SAndy Whitcroft
2326773647a0SAndy Whitcroft# check for semaphores used as mutexes
2327171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2328773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2329773647a0SAndy Whitcroft		}
2330773647a0SAndy Whitcroft# check for semaphores used as mutexes
2331171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2332773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2333773647a0SAndy Whitcroft		}
2334773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2335773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2336773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2337773647a0SAndy Whitcroft		}
2338f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2339f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2340f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2341f3db6639SMichael Ellerman		}
2342773647a0SAndy Whitcroft
2343773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2344773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2345773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2346c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2347c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2348171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2349171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2350171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2351773647a0SAndy Whitcroft		{
2352773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2353773647a0SAndy Whitcroft		}
23549c9ba34eSAndy Whitcroft
23559c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
23569c9ba34eSAndy Whitcroft		my $string;
23579c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
23589c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
23599c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
23609c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
23619c9ba34eSAndy Whitcroft				last;
23629c9ba34eSAndy Whitcroft			}
23639c9ba34eSAndy Whitcroft		}
236413214adfSAndy Whitcroft	}
236513214adfSAndy Whitcroft
236613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
236713214adfSAndy Whitcroft	# so just keep quiet.
236813214adfSAndy Whitcroft	if ($#rawlines == -1) {
236913214adfSAndy Whitcroft		exit(0);
23700a920b5bSAndy Whitcroft	}
23710a920b5bSAndy Whitcroft
23728905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
23738905a67cSAndy Whitcroft	# things that appear to be patches.
23748905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
23758905a67cSAndy Whitcroft		exit(0);
23768905a67cSAndy Whitcroft	}
23778905a67cSAndy Whitcroft
23788905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
23798905a67cSAndy Whitcroft	# just keep quiet.
23808905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
23818905a67cSAndy Whitcroft		exit(0);
23828905a67cSAndy Whitcroft	}
23838905a67cSAndy Whitcroft
23848905a67cSAndy Whitcroft	if (!$is_patch) {
2385de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
23860a920b5bSAndy Whitcroft	}
23870a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2388de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
23890a920b5bSAndy Whitcroft	}
23900a920b5bSAndy Whitcroft
2391f0a594c1SAndy Whitcroft	print report_dump();
239213214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
239313214adfSAndy Whitcroft		print "$filename " if ($summary_file);
23946c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
23956c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
23966c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
23978905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
23986c72ffaaSAndy Whitcroft	}
23998905a67cSAndy Whitcroft
24000a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2401c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
24020a920b5bSAndy Whitcroft	}
24030a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2404c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
24050a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
24060a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
24070a920b5bSAndy Whitcroft	}
240813214adfSAndy Whitcroft
24090a920b5bSAndy Whitcroft	return $clean;
24100a920b5bSAndy Whitcroft}
2411