xref: /linux-6.15/scripts/checkpatch.pl (revision 5368df20)
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
12dea79cd3SAndy Whitcroftmy $V = '0.22';
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		}
33881bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && 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++) {
411dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
412c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4138905a67cSAndy Whitcroft				$remain--;
41413214adfSAndy Whitcroft				$loff = $len;
415c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4168905a67cSAndy Whitcroft				$len = length($blk);
4178905a67cSAndy Whitcroft				$line++;
4188905a67cSAndy Whitcroft				last;
4198905a67cSAndy Whitcroft			}
4208905a67cSAndy Whitcroft			# Bail if there is no further context.
4218905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
42213214adfSAndy Whitcroft			if ($off >= $len) {
4238905a67cSAndy Whitcroft				last;
4248905a67cSAndy Whitcroft			}
4258905a67cSAndy Whitcroft		}
426cf655043SAndy Whitcroft		$p = $c;
4278905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
42813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4298905a67cSAndy Whitcroft
430773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4318905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4328905a67cSAndy Whitcroft		# outermost level.
4338905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4348905a67cSAndy Whitcroft			last;
4358905a67cSAndy Whitcroft		}
4368905a67cSAndy Whitcroft
43713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
438773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
439773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
440773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
441773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
442773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
443773647a0SAndy Whitcroft			$coff_set = 1;
444773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
445773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
44613214adfSAndy Whitcroft		}
44713214adfSAndy Whitcroft
4488905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4498905a67cSAndy Whitcroft			$level++;
4508905a67cSAndy Whitcroft			$type = '(';
4518905a67cSAndy Whitcroft		}
4528905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4538905a67cSAndy Whitcroft			$level--;
4548905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4558905a67cSAndy Whitcroft
4568905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4578905a67cSAndy Whitcroft				$coff = $off;
458773647a0SAndy Whitcroft				$coff_set = 1;
459773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4608905a67cSAndy Whitcroft			}
4618905a67cSAndy Whitcroft		}
4628905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4638905a67cSAndy Whitcroft			$level++;
4648905a67cSAndy Whitcroft			$type = '{';
4658905a67cSAndy Whitcroft		}
4668905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4678905a67cSAndy Whitcroft			$level--;
4688905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4698905a67cSAndy Whitcroft
4708905a67cSAndy Whitcroft			if ($level == 0) {
4718905a67cSAndy Whitcroft				last;
4728905a67cSAndy Whitcroft			}
4738905a67cSAndy Whitcroft		}
4748905a67cSAndy Whitcroft		$off++;
4758905a67cSAndy Whitcroft	}
476a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
47713214adfSAndy Whitcroft	if ($off == $len) {
478a3bb97a7SAndy Whitcroft		$loff = $len + 1;
47913214adfSAndy Whitcroft		$line++;
48013214adfSAndy Whitcroft		$remain--;
48113214adfSAndy Whitcroft	}
4828905a67cSAndy Whitcroft
4838905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4848905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4858905a67cSAndy Whitcroft
4868905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4878905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4888905a67cSAndy Whitcroft
489773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
49013214adfSAndy Whitcroft
49113214adfSAndy Whitcroft	return ($statement, $condition,
49213214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
49313214adfSAndy Whitcroft}
49413214adfSAndy Whitcroft
495cf655043SAndy Whitcroftsub statement_lines {
496cf655043SAndy Whitcroft	my ($stmt) = @_;
497cf655043SAndy Whitcroft
498cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
499cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
500cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
501cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
502cf655043SAndy Whitcroft
503cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
504cf655043SAndy Whitcroft
505cf655043SAndy Whitcroft	return $#stmt_lines + 2;
506cf655043SAndy Whitcroft}
507cf655043SAndy Whitcroft
508cf655043SAndy Whitcroftsub statement_rawlines {
509cf655043SAndy Whitcroft	my ($stmt) = @_;
510cf655043SAndy Whitcroft
511cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
512cf655043SAndy Whitcroft
513cf655043SAndy Whitcroft	return $#stmt_lines + 2;
514cf655043SAndy Whitcroft}
515cf655043SAndy Whitcroft
516cf655043SAndy Whitcroftsub statement_block_size {
517cf655043SAndy Whitcroft	my ($stmt) = @_;
518cf655043SAndy Whitcroft
519cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
520cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
521cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
522cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
523cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
524cf655043SAndy Whitcroft
525cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
526cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
527cf655043SAndy Whitcroft
528cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
529cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
530cf655043SAndy Whitcroft
531cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
532cf655043SAndy Whitcroft		return $stmt_lines;
533cf655043SAndy Whitcroft	} else {
534cf655043SAndy Whitcroft		return $stmt_statements;
535cf655043SAndy Whitcroft	}
536cf655043SAndy Whitcroft}
537cf655043SAndy Whitcroft
53813214adfSAndy Whitcroftsub ctx_statement_full {
53913214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
54013214adfSAndy Whitcroft	my ($statement, $condition, $level);
54113214adfSAndy Whitcroft
54213214adfSAndy Whitcroft	my (@chunks);
54313214adfSAndy Whitcroft
544cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
54513214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
54613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
547773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
54813214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
549cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
550cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
551cf655043SAndy Whitcroft	}
552cf655043SAndy Whitcroft
553cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
554cf655043SAndy Whitcroft	# could continue the statement.
555cf655043SAndy Whitcroft	for (;;) {
55613214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
55713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
558cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
559773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
560cf655043SAndy Whitcroft		#print "C: push\n";
561cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
56213214adfSAndy Whitcroft	}
56313214adfSAndy Whitcroft
56413214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5658905a67cSAndy Whitcroft}
5668905a67cSAndy Whitcroft
5674a0df2efSAndy Whitcroftsub ctx_block_get {
568f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5694a0df2efSAndy Whitcroft	my $line;
5704a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5714a0df2efSAndy Whitcroft	my $blk = '';
5724a0df2efSAndy Whitcroft	my @o;
5734a0df2efSAndy Whitcroft	my @c;
5744a0df2efSAndy Whitcroft	my @res = ();
5754a0df2efSAndy Whitcroft
576f0a594c1SAndy Whitcroft	my $level = 0;
57700df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
57800df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
57900df344fSAndy Whitcroft		$remain--;
58000df344fSAndy Whitcroft
58100df344fSAndy Whitcroft		$blk .= $rawlines[$line];
582f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
583f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
584f0a594c1SAndy Whitcroft			if ($off > 0) {
585f0a594c1SAndy Whitcroft				$off--;
586f0a594c1SAndy Whitcroft				next;
587f0a594c1SAndy Whitcroft			}
5884a0df2efSAndy Whitcroft
589f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
590f0a594c1SAndy Whitcroft				$level--;
591f0a594c1SAndy Whitcroft				last if ($level == 0);
592f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
593f0a594c1SAndy Whitcroft				$level++;
594f0a594c1SAndy Whitcroft			}
595f0a594c1SAndy Whitcroft		}
5964a0df2efSAndy Whitcroft
597f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
59800df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
5994a0df2efSAndy Whitcroft		}
6004a0df2efSAndy Whitcroft
601f0a594c1SAndy Whitcroft		last if ($level == 0);
6024a0df2efSAndy Whitcroft	}
6034a0df2efSAndy Whitcroft
604f0a594c1SAndy Whitcroft	return ($level, @res);
6054a0df2efSAndy Whitcroft}
6064a0df2efSAndy Whitcroftsub ctx_block_outer {
6074a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6084a0df2efSAndy Whitcroft
609f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
610f0a594c1SAndy Whitcroft	return @r;
6114a0df2efSAndy Whitcroft}
6124a0df2efSAndy Whitcroftsub ctx_block {
6134a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6144a0df2efSAndy Whitcroft
615f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
616f0a594c1SAndy Whitcroft	return @r;
617653d4876SAndy Whitcroft}
618653d4876SAndy Whitcroftsub ctx_statement {
619f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
620f0a594c1SAndy Whitcroft
621f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
622f0a594c1SAndy Whitcroft	return @r;
623f0a594c1SAndy Whitcroft}
624f0a594c1SAndy Whitcroftsub ctx_block_level {
625653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
626653d4876SAndy Whitcroft
627f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6284a0df2efSAndy Whitcroft}
6299c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6309c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6319c0ca6f9SAndy Whitcroft
6329c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6339c0ca6f9SAndy Whitcroft}
6344a0df2efSAndy Whitcroft
6354a0df2efSAndy Whitcroftsub ctx_locate_comment {
6364a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6374a0df2efSAndy Whitcroft
6384a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
639beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
6404a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6414a0df2efSAndy Whitcroft
6424a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6434a0df2efSAndy Whitcroft	# comment.
6444a0df2efSAndy Whitcroft	my $in_comment = 0;
6454a0df2efSAndy Whitcroft	$current_comment = '';
6464a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
64700df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
64800df344fSAndy Whitcroft		#warn "           $line\n";
6494a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6504a0df2efSAndy Whitcroft			$in_comment = 1;
6514a0df2efSAndy Whitcroft		}
6524a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6534a0df2efSAndy Whitcroft			$in_comment = 1;
6544a0df2efSAndy Whitcroft		}
6554a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6564a0df2efSAndy Whitcroft			$current_comment = '';
6574a0df2efSAndy Whitcroft		}
6584a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6594a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6604a0df2efSAndy Whitcroft			$in_comment = 0;
6614a0df2efSAndy Whitcroft		}
6624a0df2efSAndy Whitcroft	}
6634a0df2efSAndy Whitcroft
6644a0df2efSAndy Whitcroft	chomp($current_comment);
6654a0df2efSAndy Whitcroft	return($current_comment);
6664a0df2efSAndy Whitcroft}
6674a0df2efSAndy Whitcroftsub ctx_has_comment {
6684a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6694a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6704a0df2efSAndy Whitcroft
67100df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6724a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6734a0df2efSAndy Whitcroft
6744a0df2efSAndy Whitcroft	return ($cmt ne '');
6754a0df2efSAndy Whitcroft}
6764a0df2efSAndy Whitcroft
6774d001e4dSAndy Whitcroftsub raw_line {
6784d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
6794d001e4dSAndy Whitcroft
6804d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
6814d001e4dSAndy Whitcroft	$cnt++;
6824d001e4dSAndy Whitcroft
6834d001e4dSAndy Whitcroft	my $line;
6844d001e4dSAndy Whitcroft	while ($cnt) {
6854d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
6864d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
6874d001e4dSAndy Whitcroft		$cnt--;
6884d001e4dSAndy Whitcroft	}
6894d001e4dSAndy Whitcroft
6904d001e4dSAndy Whitcroft	return $line;
6914d001e4dSAndy Whitcroft}
6924d001e4dSAndy Whitcroft
6930a920b5bSAndy Whitcroftsub cat_vet {
6940a920b5bSAndy Whitcroft	my ($vet) = @_;
6959c0ca6f9SAndy Whitcroft	my ($res, $coded);
6960a920b5bSAndy Whitcroft
6979c0ca6f9SAndy Whitcroft	$res = '';
6986c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
6996c72ffaaSAndy Whitcroft		$res .= $1;
7006c72ffaaSAndy Whitcroft		if ($2 ne '') {
7019c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
7026c72ffaaSAndy Whitcroft			$res .= $coded;
7036c72ffaaSAndy Whitcroft		}
7049c0ca6f9SAndy Whitcroft	}
7059c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
7060a920b5bSAndy Whitcroft
7079c0ca6f9SAndy Whitcroft	return $res;
7080a920b5bSAndy Whitcroft}
7090a920b5bSAndy Whitcroft
710c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
711cf655043SAndy Whitcroftmy $av_pending;
712c2fdda0dSAndy Whitcroftmy @av_paren_type;
7131f65f947SAndy Whitcroftmy $av_pend_colon;
714c2fdda0dSAndy Whitcroft
715c2fdda0dSAndy Whitcroftsub annotate_reset {
716c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
717cf655043SAndy Whitcroft	$av_pending = '_';
718cf655043SAndy Whitcroft	@av_paren_type = ('E');
7191f65f947SAndy Whitcroft	$av_pend_colon = 'O';
720c2fdda0dSAndy Whitcroft}
721c2fdda0dSAndy Whitcroft
7226c72ffaaSAndy Whitcroftsub annotate_values {
7236c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
7246c72ffaaSAndy Whitcroft
7256c72ffaaSAndy Whitcroft	my $res;
7261f65f947SAndy Whitcroft	my $var = '_' x length($stream);
7276c72ffaaSAndy Whitcroft	my $cur = $stream;
7286c72ffaaSAndy Whitcroft
729c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
7306c72ffaaSAndy Whitcroft
7316c72ffaaSAndy Whitcroft	while (length($cur)) {
732773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
733cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
734171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
7356c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
736c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
737c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
738cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
739c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7406c72ffaaSAndy Whitcroft			}
7416c72ffaaSAndy Whitcroft
7428ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
743c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7446c72ffaaSAndy Whitcroft			$type = 'T';
7456c72ffaaSAndy Whitcroft
746389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
747389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
748389a2fe5SAndy Whitcroft			$type = 'T';
749389a2fe5SAndy Whitcroft
750c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
751171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
752c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
753171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
754171ae1a4SAndy Whitcroft			if ($2 ne '') {
755cf655043SAndy Whitcroft				$av_pending = 'N';
756171ae1a4SAndy Whitcroft			}
757171ae1a4SAndy Whitcroft			$type = 'E';
758171ae1a4SAndy Whitcroft
759c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
760171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
761171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
762171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
7636c72ffaaSAndy Whitcroft
764c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
765cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
766c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
767cf655043SAndy Whitcroft
768cf655043SAndy Whitcroft			push(@av_paren_type, $type);
769cf655043SAndy Whitcroft			push(@av_paren_type, $type);
770171ae1a4SAndy Whitcroft			$type = 'E';
771cf655043SAndy Whitcroft
772c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
773cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
774cf655043SAndy Whitcroft			$av_preprocessor = 1;
775cf655043SAndy Whitcroft
776cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
777cf655043SAndy Whitcroft
778171ae1a4SAndy Whitcroft			$type = 'E';
779cf655043SAndy Whitcroft
780c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
781cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
782cf655043SAndy Whitcroft
783cf655043SAndy Whitcroft			$av_preprocessor = 1;
784cf655043SAndy Whitcroft
785cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
786cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
787cf655043SAndy Whitcroft			pop(@av_paren_type);
788cf655043SAndy Whitcroft			push(@av_paren_type, $type);
789171ae1a4SAndy Whitcroft			$type = 'E';
7906c72ffaaSAndy Whitcroft
7916c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
792c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7936c72ffaaSAndy Whitcroft
794171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
795171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
796171ae1a4SAndy Whitcroft			$av_pending = $type;
797171ae1a4SAndy Whitcroft			$type = 'N';
798171ae1a4SAndy Whitcroft
7996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
800c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
8016c72ffaaSAndy Whitcroft			if (defined $2) {
802cf655043SAndy Whitcroft				$av_pending = 'V';
8036c72ffaaSAndy Whitcroft			}
8046c72ffaaSAndy Whitcroft			$type = 'N';
8056c72ffaaSAndy Whitcroft
80614b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
807c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
80814b111c1SAndy Whitcroft			$av_pending = 'E';
8096c72ffaaSAndy Whitcroft			$type = 'N';
8106c72ffaaSAndy Whitcroft
8111f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
8121f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
8131f65f947SAndy Whitcroft			$av_pend_colon = 'C';
8141f65f947SAndy Whitcroft			$type = 'N';
8151f65f947SAndy Whitcroft
81614b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
817c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
8186c72ffaaSAndy Whitcroft			$type = 'N';
8196c72ffaaSAndy Whitcroft
8206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
821c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
822cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
823cf655043SAndy Whitcroft			$av_pending = '_';
8246c72ffaaSAndy Whitcroft			$type = 'N';
8256c72ffaaSAndy Whitcroft
8266c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
827cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
828cf655043SAndy Whitcroft			if ($new_type ne '_') {
829cf655043SAndy Whitcroft				$type = $new_type;
830c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
831c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
8326c72ffaaSAndy Whitcroft			} else {
833c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
8346c72ffaaSAndy Whitcroft			}
8356c72ffaaSAndy Whitcroft
836c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
837c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
838c8cb2ca3SAndy Whitcroft			$type = 'V';
839cf655043SAndy Whitcroft			$av_pending = 'V';
8406c72ffaaSAndy Whitcroft
8411f65f947SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):/) {
8421f65f947SAndy Whitcroft			if ($type eq 'E') {
8431f65f947SAndy Whitcroft				$av_pend_colon = 'L';
8441f65f947SAndy Whitcroft			} elsif ($type eq 'T') {
8451f65f947SAndy Whitcroft				$av_pend_colon = 'B';
8461f65f947SAndy Whitcroft			}
8471f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
8481f65f947SAndy Whitcroft			$type = 'V';
8491f65f947SAndy Whitcroft
8506c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
851c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
8526c72ffaaSAndy Whitcroft			$type = 'V';
8536c72ffaaSAndy Whitcroft
8546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
855c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
8566c72ffaaSAndy Whitcroft			$type = 'N';
8576c72ffaaSAndy Whitcroft
858cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
859c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
86013214adfSAndy Whitcroft			$type = 'E';
8611f65f947SAndy Whitcroft			$av_pend_colon = 'O';
86213214adfSAndy Whitcroft
8631f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
8641f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
8651f65f947SAndy Whitcroft			$type = 'N';
8661f65f947SAndy Whitcroft
8671f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
8681f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
8691f65f947SAndy Whitcroft
8701f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
8711f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
8721f65f947SAndy Whitcroft				$type = 'E';
8731f65f947SAndy Whitcroft			} else {
8741f65f947SAndy Whitcroft				$type = 'N';
8751f65f947SAndy Whitcroft			}
8761f65f947SAndy Whitcroft			$av_pend_colon = 'O';
8771f65f947SAndy Whitcroft
8781f65f947SAndy Whitcroft		} elsif ($cur =~ /^(;|\[)/o) {
87913214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
8806c72ffaaSAndy Whitcroft			$type = 'N';
8816c72ffaaSAndy Whitcroft
8820d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
88374048ed8SAndy Whitcroft			my $variant;
88474048ed8SAndy Whitcroft
88574048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
88674048ed8SAndy Whitcroft			if ($type eq 'V') {
88774048ed8SAndy Whitcroft				$variant = 'B';
88874048ed8SAndy Whitcroft			} else {
88974048ed8SAndy Whitcroft				$variant = 'U';
89074048ed8SAndy Whitcroft			}
89174048ed8SAndy Whitcroft
89274048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
89374048ed8SAndy Whitcroft			$type = 'N';
89474048ed8SAndy Whitcroft
8956c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
896c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
8976c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
8986c72ffaaSAndy Whitcroft				$type = 'N';
8996c72ffaaSAndy Whitcroft			}
9006c72ffaaSAndy Whitcroft
9016c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
902c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
9036c72ffaaSAndy Whitcroft		}
9046c72ffaaSAndy Whitcroft		if (defined $1) {
9056c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
9066c72ffaaSAndy Whitcroft			$res .= $type x length($1);
9076c72ffaaSAndy Whitcroft		}
9086c72ffaaSAndy Whitcroft	}
9096c72ffaaSAndy Whitcroft
9101f65f947SAndy Whitcroft	return ($res, $var);
9116c72ffaaSAndy Whitcroft}
9126c72ffaaSAndy Whitcroft
9138905a67cSAndy Whitcroftsub possible {
91413214adfSAndy Whitcroft	my ($possible, $line) = @_;
9158905a67cSAndy Whitcroft
916c45dcabdSAndy Whitcroft	print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
9170221f55cSAndy Whitcroft	if ($possible !~ /^(?:$Modifier|$Storage|$Type|DEFINE_\S+)$/ &&
9188905a67cSAndy Whitcroft	    $possible ne 'goto' && $possible ne 'return' &&
9198905a67cSAndy Whitcroft	    $possible ne 'case' && $possible ne 'else' &&
920d3ddcf47SAndy Whitcroft	    $possible ne 'asm' && $possible ne '__asm__' &&
921c45dcabdSAndy Whitcroft	    $possible !~ /^(typedef|struct|enum)\b/) {
922c45dcabdSAndy Whitcroft		# Check for modifiers.
923c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
924c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
925c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
926c45dcabdSAndy Whitcroft
927c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
928c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
929d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
930d2506586SAndy Whitcroft				warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
931d2506586SAndy Whitcroft				push(@modifierList, $modifier);
932d2506586SAndy Whitcroft			}
933c45dcabdSAndy Whitcroft
934c45dcabdSAndy Whitcroft		} else {
93513214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
9368905a67cSAndy Whitcroft			push(@typeList, $possible);
937c45dcabdSAndy Whitcroft		}
9388905a67cSAndy Whitcroft		build_types();
9398905a67cSAndy Whitcroft	}
9408905a67cSAndy Whitcroft}
9418905a67cSAndy Whitcroft
9426c72ffaaSAndy Whitcroftmy $prefix = '';
9436c72ffaaSAndy Whitcroft
944f0a594c1SAndy Whitcroftsub report {
945773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
946773647a0SAndy Whitcroft		return 0;
947773647a0SAndy Whitcroft	}
9488905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
9498905a67cSAndy Whitcroft
9508905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
9518905a67cSAndy Whitcroft
95213214adfSAndy Whitcroft	push(our @report, $line);
953773647a0SAndy Whitcroft
954773647a0SAndy Whitcroft	return 1;
955f0a594c1SAndy Whitcroft}
956f0a594c1SAndy Whitcroftsub report_dump {
95713214adfSAndy Whitcroft	our @report;
958f0a594c1SAndy Whitcroft}
959de7d4f0eSAndy Whitcroftsub ERROR {
960773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
961de7d4f0eSAndy Whitcroft		our $clean = 0;
9626c72ffaaSAndy Whitcroft		our $cnt_error++;
963de7d4f0eSAndy Whitcroft	}
964773647a0SAndy Whitcroft}
965de7d4f0eSAndy Whitcroftsub WARN {
966773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
967de7d4f0eSAndy Whitcroft		our $clean = 0;
9686c72ffaaSAndy Whitcroft		our $cnt_warn++;
969de7d4f0eSAndy Whitcroft	}
970773647a0SAndy Whitcroft}
971de7d4f0eSAndy Whitcroftsub CHK {
972773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
973de7d4f0eSAndy Whitcroft		our $clean = 0;
9746c72ffaaSAndy Whitcroft		our $cnt_chk++;
9756c72ffaaSAndy Whitcroft	}
976de7d4f0eSAndy Whitcroft}
977de7d4f0eSAndy Whitcroft
9786ecd9674SAndy Whitcroftsub check_absolute_file {
9796ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
9806ecd9674SAndy Whitcroft	my $file = $absolute;
9816ecd9674SAndy Whitcroft
9826ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
9836ecd9674SAndy Whitcroft
9846ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
9856ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
9866ecd9674SAndy Whitcroft		if (-f "$root/$file") {
9876ecd9674SAndy Whitcroft			##print "file<$file>\n";
9886ecd9674SAndy Whitcroft			last;
9896ecd9674SAndy Whitcroft		}
9906ecd9674SAndy Whitcroft	}
9916ecd9674SAndy Whitcroft	if (! -f _)  {
9926ecd9674SAndy Whitcroft		return 0;
9936ecd9674SAndy Whitcroft	}
9946ecd9674SAndy Whitcroft
9956ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
9966ecd9674SAndy Whitcroft	my $prefix = $absolute;
9976ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
9986ecd9674SAndy Whitcroft
9996ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
10006ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
10016ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
10026ecd9674SAndy Whitcroft	}
10036ecd9674SAndy Whitcroft}
10046ecd9674SAndy Whitcroft
10050a920b5bSAndy Whitcroftsub process {
10060a920b5bSAndy Whitcroft	my $filename = shift;
10070a920b5bSAndy Whitcroft
10080a920b5bSAndy Whitcroft	my $linenr=0;
10090a920b5bSAndy Whitcroft	my $prevline="";
1010c2fdda0dSAndy Whitcroft	my $prevrawline="";
10110a920b5bSAndy Whitcroft	my $stashline="";
1012c2fdda0dSAndy Whitcroft	my $stashrawline="";
10130a920b5bSAndy Whitcroft
10144a0df2efSAndy Whitcroft	my $length;
10150a920b5bSAndy Whitcroft	my $indent;
10160a920b5bSAndy Whitcroft	my $previndent=0;
10170a920b5bSAndy Whitcroft	my $stashindent=0;
10180a920b5bSAndy Whitcroft
1019de7d4f0eSAndy Whitcroft	our $clean = 1;
10200a920b5bSAndy Whitcroft	my $signoff = 0;
10210a920b5bSAndy Whitcroft	my $is_patch = 0;
10220a920b5bSAndy Whitcroft
102313214adfSAndy Whitcroft	our @report = ();
10246c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
10256c72ffaaSAndy Whitcroft	our $cnt_error = 0;
10266c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
10276c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
10286c72ffaaSAndy Whitcroft
10290a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
10300a920b5bSAndy Whitcroft	my $realfile = '';
10310a920b5bSAndy Whitcroft	my $realline = 0;
10320a920b5bSAndy Whitcroft	my $realcnt = 0;
10330a920b5bSAndy Whitcroft	my $here = '';
10340a920b5bSAndy Whitcroft	my $in_comment = 0;
1035c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
10360a920b5bSAndy Whitcroft	my $first_line = 0;
10370a920b5bSAndy Whitcroft
103813214adfSAndy Whitcroft	my $prev_values = 'E';
103913214adfSAndy Whitcroft
104013214adfSAndy Whitcroft	# suppression flags
1041773647a0SAndy Whitcroft	my %suppress_ifbraces;
1042653d4876SAndy Whitcroft
1043c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1044de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1045c2fdda0dSAndy Whitcroft	#
1046de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1047de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1048773647a0SAndy Whitcroft
1049773647a0SAndy Whitcroft	sanitise_line_reset();
1050c2fdda0dSAndy Whitcroft	my $line;
1051c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1052773647a0SAndy Whitcroft		$linenr++;
1053773647a0SAndy Whitcroft		$line = $rawline;
1054c2fdda0dSAndy Whitcroft
1055773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1056de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1057de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1058de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1059de7d4f0eSAndy Whitcroft			}
1060773647a0SAndy Whitcroft			#next;
1061de7d4f0eSAndy Whitcroft		}
1062773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1063773647a0SAndy Whitcroft			$realline=$1-1;
1064773647a0SAndy Whitcroft			if (defined $2) {
1065773647a0SAndy Whitcroft				$realcnt=$3+1;
1066773647a0SAndy Whitcroft			} else {
1067773647a0SAndy Whitcroft				$realcnt=1+1;
1068773647a0SAndy Whitcroft			}
1069c45dcabdSAndy Whitcroft			$in_comment = 0;
1070773647a0SAndy Whitcroft
1071773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1072773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1073773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1074773647a0SAndy Whitcroft			# at context start.
1075773647a0SAndy Whitcroft			my $edge;
107601fa9147SAndy Whitcroft			my $cnt = $realcnt;
107701fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
107801fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
107901fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
108001fa9147SAndy Whitcroft				$cnt--;
108101fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
108201fa9147SAndy Whitcroft				($edge) = (defined $rawlines[$ln - 1] &&
108301fa9147SAndy Whitcroft					$rawlines[$ln - 1] =~ m@(/\*|\*/)@);
1084773647a0SAndy Whitcroft				last if (defined $edge);
1085773647a0SAndy Whitcroft			}
1086773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1087773647a0SAndy Whitcroft				$in_comment = 1;
1088773647a0SAndy Whitcroft			}
1089773647a0SAndy Whitcroft
1090773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1091773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1092773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1093773647a0SAndy Whitcroft			if (!defined $edge &&
1094773647a0SAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
1095773647a0SAndy Whitcroft			{
1096773647a0SAndy Whitcroft				$in_comment = 1;
1097773647a0SAndy Whitcroft			}
1098773647a0SAndy Whitcroft
1099773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1100773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1101773647a0SAndy Whitcroft
1102171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1103773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1104171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1105773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1106773647a0SAndy Whitcroft		}
1107773647a0SAndy Whitcroft		push(@lines, $line);
1108773647a0SAndy Whitcroft
1109773647a0SAndy Whitcroft		if ($realcnt > 1) {
1110773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1111773647a0SAndy Whitcroft		} else {
1112773647a0SAndy Whitcroft			$realcnt = 0;
1113773647a0SAndy Whitcroft		}
1114773647a0SAndy Whitcroft
1115773647a0SAndy Whitcroft		#print "==>$rawline\n";
1116773647a0SAndy Whitcroft		#print "-->$line\n";
1117de7d4f0eSAndy Whitcroft
1118de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1119de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1120de7d4f0eSAndy Whitcroft		}
1121de7d4f0eSAndy Whitcroft	}
1122de7d4f0eSAndy Whitcroft
11236c72ffaaSAndy Whitcroft	$prefix = '';
11246c72ffaaSAndy Whitcroft
1125773647a0SAndy Whitcroft	$realcnt = 0;
1126773647a0SAndy Whitcroft	$linenr = 0;
11270a920b5bSAndy Whitcroft	foreach my $line (@lines) {
11280a920b5bSAndy Whitcroft		$linenr++;
11290a920b5bSAndy Whitcroft
1130c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
11316c72ffaaSAndy Whitcroft
11320a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
11336c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
11340a920b5bSAndy Whitcroft			$is_patch = 1;
11354a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
11360a920b5bSAndy Whitcroft			$realline=$1-1;
11370a920b5bSAndy Whitcroft			if (defined $2) {
11380a920b5bSAndy Whitcroft				$realcnt=$3+1;
11390a920b5bSAndy Whitcroft			} else {
11400a920b5bSAndy Whitcroft				$realcnt=1+1;
11410a920b5bSAndy Whitcroft			}
1142c2fdda0dSAndy Whitcroft			annotate_reset();
114313214adfSAndy Whitcroft			$prev_values = 'E';
114413214adfSAndy Whitcroft
1145773647a0SAndy Whitcroft			%suppress_ifbraces = ();
11460a920b5bSAndy Whitcroft			next;
11470a920b5bSAndy Whitcroft
11484a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
11494a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
11504a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1151773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
11520a920b5bSAndy Whitcroft			$realline++;
1153d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
11540a920b5bSAndy Whitcroft
11554a0df2efSAndy Whitcroft			# Measure the line length and indent.
1156c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
11570a920b5bSAndy Whitcroft
11580a920b5bSAndy Whitcroft			# Track the previous line.
11590a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
11600a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1161c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1162c2fdda0dSAndy Whitcroft
1163773647a0SAndy Whitcroft			#warn "line<$line>\n";
11646c72ffaaSAndy Whitcroft
1165d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1166d8aaf121SAndy Whitcroft			$realcnt--;
11670a920b5bSAndy Whitcroft		}
11680a920b5bSAndy Whitcroft
11690a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1170773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1171773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1172773647a0SAndy Whitcroft
11736c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
11746c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1175773647a0SAndy Whitcroft
1176773647a0SAndy Whitcroft		# extract the filename as it passes
1177773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1178773647a0SAndy Whitcroft			$realfile = $1;
1179773647a0SAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
1180773647a0SAndy Whitcroft
1181c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1182773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1183773647a0SAndy Whitcroft			}
1184773647a0SAndy Whitcroft			next;
1185773647a0SAndy Whitcroft		}
1186773647a0SAndy Whitcroft
1187389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
11880a920b5bSAndy Whitcroft
1189c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1190c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1191c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
11920a920b5bSAndy Whitcroft
11936c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
11946c72ffaaSAndy Whitcroft
11950a920b5bSAndy Whitcroft#check the patch for a signoff:
1196d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
11974a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
11984a0df2efSAndy Whitcroft			$signoff++;
11990a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1200de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1201de7d4f0eSAndy Whitcroft					$herecurr);
12020a920b5bSAndy Whitcroft			}
12030a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1204773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1205de7d4f0eSAndy Whitcroft					$herecurr);
12060a920b5bSAndy Whitcroft			}
12070a920b5bSAndy Whitcroft		}
12080a920b5bSAndy Whitcroft
120900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
12108905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1211de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
12126c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1213de7d4f0eSAndy Whitcroft		}
1214de7d4f0eSAndy Whitcroft
12156ecd9674SAndy Whitcroft# Check for absolute kernel paths.
12166ecd9674SAndy Whitcroft		if ($tree) {
12176ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
12186ecd9674SAndy Whitcroft				my $file = $1;
12196ecd9674SAndy Whitcroft
12206ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
12216ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
12226ecd9674SAndy Whitcroft					#
12236ecd9674SAndy Whitcroft				} else {
12246ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
12256ecd9674SAndy Whitcroft				}
12266ecd9674SAndy Whitcroft			}
12276ecd9674SAndy Whitcroft		}
12286ecd9674SAndy Whitcroft
1229de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1230de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1231171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1232171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1233171ae1a4SAndy Whitcroft
1234171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1235171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1236171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1237171ae1a4SAndy Whitcroft
1238171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
123900df344fSAndy Whitcroft		}
12400a920b5bSAndy Whitcroft
124100df344fSAndy Whitcroft#ignore lines being removed
124200df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
124300df344fSAndy Whitcroft
12440a920b5bSAndy Whitcroft#trailing whitespace
12459c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1246c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
12479c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
12489c0ca6f9SAndy Whitcroft
1249c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1250c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1251de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
12520a920b5bSAndy Whitcroft		}
1253*5368df20SAndy Whitcroft
1254*5368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
1255*5368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1256*5368df20SAndy Whitcroft
12570a920b5bSAndy Whitcroft#80 column limit
1258c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1259f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1260f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1261f4c014c0SAndy Whitcroft		    $length > 80)
1262c45dcabdSAndy Whitcroft		{
1263de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
12640a920b5bSAndy Whitcroft		}
12650a920b5bSAndy Whitcroft
12668905a67cSAndy Whitcroft# check for adding lines without a newline.
12678905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
12688905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
12698905a67cSAndy Whitcroft		}
12708905a67cSAndy Whitcroft
1271b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1272b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
12730a920b5bSAndy Whitcroft
12740a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
12750a920b5bSAndy Whitcroft# more than 8 must use tabs.
1276c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1277c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1278c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1279171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
12800a920b5bSAndy Whitcroft		}
12810a920b5bSAndy Whitcroft
1282b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1283b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1284b9ea10d6SAndy Whitcroft
1285c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1286cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1287c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1288c2fdda0dSAndy Whitcroft		}
128922f2a2efSAndy Whitcroft
12909c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1291f5fe35ddSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next);
12929c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1293f5fe35ddSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next) =
1294f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1295171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1296171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1297171ae1a4SAndy Whitcroft
1298171ae1a4SAndy Whitcroft			my $s = $stat;
1299171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1300cf655043SAndy Whitcroft
1301c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1302171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1303c2fdda0dSAndy Whitcroft
1304c2fdda0dSAndy Whitcroft			# Ignore functions being called
1305171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1306c2fdda0dSAndy Whitcroft
1307c45dcabdSAndy Whitcroft			# declarations always start with types
1308d2506586SAndy 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) {
1309c45dcabdSAndy Whitcroft				my $type = $1;
1310c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1311c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1312c45dcabdSAndy Whitcroft
13136c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1314171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1315c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1316c2fdda0dSAndy Whitcroft			}
13178905a67cSAndy Whitcroft
13186c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1319171ae1a4SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1320c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
13219c0ca6f9SAndy Whitcroft			}
13228905a67cSAndy Whitcroft
13238905a67cSAndy Whitcroft			# Check for any sort of function declaration.
13248905a67cSAndy Whitcroft			# int foo(something bar, other baz);
13258905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1326171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
13278905a67cSAndy Whitcroft				my ($name_len) = length($1);
13288905a67cSAndy Whitcroft
1329cf655043SAndy Whitcroft				my $ctx = $s;
1330773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
13318905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1332cf655043SAndy Whitcroft
13338905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1334c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
13358905a67cSAndy Whitcroft
1336c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
13378905a67cSAndy Whitcroft					}
13388905a67cSAndy Whitcroft				}
13398905a67cSAndy Whitcroft			}
13408905a67cSAndy Whitcroft
13419c0ca6f9SAndy Whitcroft		}
13429c0ca6f9SAndy Whitcroft
134300df344fSAndy Whitcroft#
134400df344fSAndy Whitcroft# Checks which may be anchored in the context.
134500df344fSAndy Whitcroft#
134600df344fSAndy Whitcroft
134700df344fSAndy Whitcroft# Check for switch () and associated case and default
134800df344fSAndy Whitcroft# statements should be at the same indent.
134900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
135000df344fSAndy Whitcroft			my $err = '';
135100df344fSAndy Whitcroft			my $sep = '';
135200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
135300df344fSAndy Whitcroft			shift(@ctx);
135400df344fSAndy Whitcroft			for my $ctx (@ctx) {
135500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
135600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
135700df344fSAndy Whitcroft							$indent != $cindent) {
135800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
135900df344fSAndy Whitcroft					$sep = '';
136000df344fSAndy Whitcroft				} else {
136100df344fSAndy Whitcroft					$sep = "[...]\n";
136200df344fSAndy Whitcroft				}
136300df344fSAndy Whitcroft			}
136400df344fSAndy Whitcroft			if ($err ne '') {
13659c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1366de7d4f0eSAndy Whitcroft			}
1367de7d4f0eSAndy Whitcroft		}
1368de7d4f0eSAndy Whitcroft
1369de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1370de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1371c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1372773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1373773647a0SAndy Whitcroft
13749c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1375de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1376de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1377de7d4f0eSAndy Whitcroft
1378548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1379548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1380de7d4f0eSAndy Whitcroft
1381548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1382548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1383548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1384548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1385548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1386773647a0SAndy Whitcroft				$ctx_ln++;
1387773647a0SAndy Whitcroft			}
1388548596d5SAndy Whitcroft
138953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
139053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1391773647a0SAndy Whitcroft
1392773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1393773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1394c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
139500df344fSAndy Whitcroft			}
1396773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1397773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1398773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1399773647a0SAndy Whitcroft			{
14009c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
14019c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1402773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1403c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
14049c0ca6f9SAndy Whitcroft				}
14059c0ca6f9SAndy Whitcroft			}
140600df344fSAndy Whitcroft		}
140700df344fSAndy Whitcroft
14084d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
14094d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
14104d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
14114d001e4dSAndy Whitcroft
14124d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
14134d001e4dSAndy Whitcroft
14144d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
14154d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
14164d001e4dSAndy Whitcroft			# where necessary.
14174d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
14184d001e4dSAndy Whitcroft
14194d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
14204d001e4dSAndy Whitcroft			my $cond_lines = 0 + $c =~ /\n/gs;
14214d001e4dSAndy Whitcroft
14224d001e4dSAndy Whitcroft			# We want to check the first line inside the block
14234d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
14244d001e4dSAndy Whitcroft			#  1) any blank line termination
14254d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
14264d001e4dSAndy Whitcroft			#  3) any do (...) {
14274d001e4dSAndy Whitcroft			my $continuation = 0;
14284d001e4dSAndy Whitcroft			my $check = 0;
14294d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
14304d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
14314d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
14324d001e4dSAndy Whitcroft				$continuation = 1;
14334d001e4dSAndy Whitcroft			}
14349bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
14354d001e4dSAndy Whitcroft				$check = 1;
14364d001e4dSAndy Whitcroft				$cond_lines++;
14374d001e4dSAndy Whitcroft			}
14384d001e4dSAndy Whitcroft
14394d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
14404d001e4dSAndy Whitcroft			# preprocessor statement.
14414d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
14424d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
14434d001e4dSAndy Whitcroft				$check = 0;
14444d001e4dSAndy Whitcroft			}
14454d001e4dSAndy Whitcroft
14469bd49efeSAndy Whitcroft			my $cond_ptr = -1;
14479bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
14489bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
14494d001e4dSAndy Whitcroft
14509bd49efeSAndy Whitcroft				# Ignore:
14519bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
14529bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
14539bd49efeSAndy Whitcroft				#  3) labels.
14549bd49efeSAndy Whitcroft				if ($s =~ /^\s*?\n/ ||
14559bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
14569bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
14579bd49efeSAndy Whitcroft					$s =~ s/^.*?\n//;
14589bd49efeSAndy Whitcroft					$cond_lines++;
14599bd49efeSAndy Whitcroft				}
14604d001e4dSAndy Whitcroft			}
14614d001e4dSAndy Whitcroft
14624d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
14634d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
14644d001e4dSAndy Whitcroft
14654d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
14664d001e4dSAndy Whitcroft			# this is not this patch's fault.
14674d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
14684d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
14694d001e4dSAndy Whitcroft				$check = 0;
14704d001e4dSAndy Whitcroft			}
14714d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
14724d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
14734d001e4dSAndy Whitcroft			}
14744d001e4dSAndy Whitcroft
14759bd49efeSAndy Whitcroft			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
14764d001e4dSAndy Whitcroft
14774d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
14784d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
14794d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
14804d001e4dSAndy Whitcroft			}
14814d001e4dSAndy Whitcroft		}
14824d001e4dSAndy Whitcroft
14836c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
14846c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
14851f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
14861f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
14876c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1488c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1489c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1490cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1491cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
14921f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1493c2fdda0dSAndy Whitcroft		}
14946c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
14956c72ffaaSAndy Whitcroft
149600df344fSAndy Whitcroft#ignore lines not being added
149700df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
149800df344fSAndy Whitcroft
1499653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
15007429c690SAndy Whitcroft		if ($dbg_type) {
15017429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
15027429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
15037429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
15047429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
15057429c690SAndy Whitcroft			}
1506653d4876SAndy Whitcroft			next;
1507653d4876SAndy Whitcroft		}
1508a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1509a1ef277eSAndy Whitcroft		if ($dbg_attr) {
1510a1ef277eSAndy Whitcroft			if ($line =~ /^.\s*$Attribute\s*$/) {
1511a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
1512a1ef277eSAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1513a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1514a1ef277eSAndy Whitcroft			}
1515a1ef277eSAndy Whitcroft			next;
1516a1ef277eSAndy Whitcroft		}
1517653d4876SAndy Whitcroft
1518f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1519f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1520f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1521773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1522f0a594c1SAndy Whitcroft		}
1523f0a594c1SAndy Whitcroft
152400df344fSAndy Whitcroft#
152500df344fSAndy Whitcroft# Checks which are anchored on the added line.
152600df344fSAndy Whitcroft#
152700df344fSAndy Whitcroft
1528653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1529c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1530653d4876SAndy Whitcroft			my $path = $1;
1531653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1532de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1533de7d4f0eSAndy Whitcroft					$herecurr);
1534653d4876SAndy Whitcroft			}
1535653d4876SAndy Whitcroft		}
1536653d4876SAndy Whitcroft
153700df344fSAndy Whitcroft# no C99 // comments
153800df344fSAndy Whitcroft		if ($line =~ m{//}) {
1539de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
154000df344fSAndy Whitcroft		}
154100df344fSAndy Whitcroft		# Remove C99 comments.
15420a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
15436c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
15440a920b5bSAndy Whitcroft
15450a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1546653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1547653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1548653d4876SAndy Whitcroft			my $name = $1;
15490a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
15500a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
1551653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
1552cf655043SAndy Whitcroft			   ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1553cf655043SAndy Whitcroft			   ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1554171ae1a4SAndy Whitcroft			   ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
1555cf655043SAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1556de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
15570a920b5bSAndy Whitcroft			}
15580a920b5bSAndy Whitcroft		}
15590a920b5bSAndy Whitcroft
1560f0a594c1SAndy Whitcroft# check for external initialisers.
1561c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1562f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1563f0a594c1SAndy Whitcroft				$herecurr);
1564f0a594c1SAndy Whitcroft		}
15650a920b5bSAndy Whitcroft# check for static initialisers.
15669c9ba34eSAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1567de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1568de7d4f0eSAndy Whitcroft				$herecurr);
15690a920b5bSAndy Whitcroft		}
15700a920b5bSAndy Whitcroft
1571653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1572653d4876SAndy Whitcroft# make sense.
1573653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
15749c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1575c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1576653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1577de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
15780a920b5bSAndy Whitcroft		}
15790a920b5bSAndy Whitcroft
15800a920b5bSAndy Whitcroft# * goes on variable not on type
1581d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1582de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1583de7d4f0eSAndy Whitcroft				$herecurr);
1584d8aaf121SAndy Whitcroft
1585d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1586de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1587de7d4f0eSAndy Whitcroft				$herecurr);
1588d8aaf121SAndy Whitcroft
1589234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1590de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1591de7d4f0eSAndy Whitcroft				$herecurr);
1592d8aaf121SAndy Whitcroft
1593234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1594de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1595de7d4f0eSAndy Whitcroft				$herecurr);
15960a920b5bSAndy Whitcroft		}
15970a920b5bSAndy Whitcroft
15980a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
15990a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
16000a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
16010a920b5bSAndy Whitcroft# 			print "$herecurr";
16020a920b5bSAndy Whitcroft# 			$clean = 0;
16030a920b5bSAndy Whitcroft# 		}
16040a920b5bSAndy Whitcroft
16058905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1606c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
16078905a67cSAndy Whitcroft		}
16088905a67cSAndy Whitcroft
160900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
161000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
161100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
161200df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
161300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1614f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
161500df344fSAndy Whitcroft			my $ok = 0;
161600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
161700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
161800df344fSAndy Whitcroft				# we have a preceeding printk if it ends
161900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
162000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
162100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
162200df344fSAndy Whitcroft						$ok = 1;
162300df344fSAndy Whitcroft					}
162400df344fSAndy Whitcroft					last;
162500df344fSAndy Whitcroft				}
162600df344fSAndy Whitcroft			}
162700df344fSAndy Whitcroft			if ($ok == 0) {
1628de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
16290a920b5bSAndy Whitcroft			}
163000df344fSAndy Whitcroft		}
16310a920b5bSAndy Whitcroft
1632653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1633653d4876SAndy Whitcroft# or if closed on same line
1634c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1635c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1636de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
16370a920b5bSAndy Whitcroft		}
1638653d4876SAndy Whitcroft
16398905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
16408905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
16418905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
16428905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
16438905a67cSAndy Whitcroft		}
16448905a67cSAndy Whitcroft
16458d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
16468d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1647fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1648fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
16498d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
16508d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
16518d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1652fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1653fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
16548d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
16558d31cfceSAndy Whitcroft			}
16568d31cfceSAndy Whitcroft		}
16578d31cfceSAndy Whitcroft
1658f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
16596c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1660c2fdda0dSAndy Whitcroft			my $name = $1;
1661773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1662773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1663c2fdda0dSAndy Whitcroft
1664c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1665773647a0SAndy Whitcroft			if ($name =~ /^(?:
1666773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1667773647a0SAndy Whitcroft				volatile|__volatile__|
1668773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1669773647a0SAndy Whitcroft				asm|__asm__)$/x)
1670773647a0SAndy Whitcroft			{
1671c2fdda0dSAndy Whitcroft
1672c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1673c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1674c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1675c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1676773647a0SAndy Whitcroft
1677773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1678c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1679c2fdda0dSAndy Whitcroft
1680c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1681c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1682773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1683c2fdda0dSAndy Whitcroft
1684c2fdda0dSAndy Whitcroft			} else {
1685773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1686f0a594c1SAndy Whitcroft			}
16876c72ffaaSAndy Whitcroft		}
1688653d4876SAndy Whitcroft# Check operator spacing.
16890a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
16909c0ca6f9SAndy Whitcroft			my $ops = qr{
16919c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
16929c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
16939c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
16941f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
16951f65f947SAndy Whitcroft				\?|:
16969c0ca6f9SAndy Whitcroft			}x;
1697cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
169800df344fSAndy Whitcroft			my $off = 0;
16996c72ffaaSAndy Whitcroft
17006c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
17016c72ffaaSAndy Whitcroft
17020a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
17034a0df2efSAndy Whitcroft				$off += length($elements[$n]);
17044a0df2efSAndy Whitcroft
1705773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1706773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1707773647a0SAndy Whitcroft				my $cc = '';
1708773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1709773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1710773647a0SAndy Whitcroft				}
1711773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1712773647a0SAndy Whitcroft
17134a0df2efSAndy Whitcroft				my $a = '';
17144a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
17154a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1716cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
17174a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
17184a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1719773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
17204a0df2efSAndy Whitcroft
17210a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
17224a0df2efSAndy Whitcroft
17234a0df2efSAndy Whitcroft				my $c = '';
17240a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
17254a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
17264a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1727cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
17284a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
17294a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
173022f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
17314a0df2efSAndy Whitcroft				} else {
17324a0df2efSAndy Whitcroft					$c = 'E';
17330a920b5bSAndy Whitcroft				}
17340a920b5bSAndy Whitcroft
17354a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
17364a0df2efSAndy Whitcroft
17374a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
17384a0df2efSAndy Whitcroft
17396c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1740de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
17410a920b5bSAndy Whitcroft
174274048ed8SAndy Whitcroft				# Pull out the value of this operator.
17436c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
17440a920b5bSAndy Whitcroft
17451f65f947SAndy Whitcroft				# Get the full operator variant.
17461f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
17471f65f947SAndy Whitcroft
174813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
174913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
175013214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
175113214adfSAndy Whitcroft
1752cf655043SAndy Whitcroft#				# Ignore comments
1753cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
175413214adfSAndy Whitcroft
1755d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
175613214adfSAndy Whitcroft				} elsif ($op eq ';') {
1757cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1758cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1759773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1760d8aaf121SAndy Whitcroft					}
1761d8aaf121SAndy Whitcroft
1762d8aaf121SAndy Whitcroft				# // is a comment
1763d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
17640a920b5bSAndy Whitcroft
17651f65f947SAndy Whitcroft				# No spaces for:
17661f65f947SAndy Whitcroft				#   ->
17671f65f947SAndy Whitcroft				#   :   when part of a bitfield
17681f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
17694a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1770773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
17710a920b5bSAndy Whitcroft					}
17720a920b5bSAndy Whitcroft
17730a920b5bSAndy Whitcroft				# , must have a space on the right.
17740a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1775cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1776773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
17770a920b5bSAndy Whitcroft					}
17780a920b5bSAndy Whitcroft
17799c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
178074048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
17819c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
17829c0ca6f9SAndy Whitcroft
17839c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
17849c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
17859c0ca6f9SAndy Whitcroft				# unary operator, or a cast
17869c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
178774048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
17880d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1789cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1790773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
17910a920b5bSAndy Whitcroft					}
1792171ae1a4SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*const\b/) {
1793171ae1a4SAndy Whitcroft						# A unary '*' may be const
1794171ae1a4SAndy Whitcroft
1795171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1796773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
17970a920b5bSAndy Whitcroft					}
17980a920b5bSAndy Whitcroft
17990a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
18000a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1801773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1802773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
18030a920b5bSAndy Whitcroft					}
1804773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1805773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1806773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1807653d4876SAndy Whitcroft					}
1808773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1809773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1810773647a0SAndy Whitcroft					}
1811773647a0SAndy Whitcroft
18120a920b5bSAndy Whitcroft
18130a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
18149c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
18159c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
18169c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1817c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1818c2fdda0dSAndy Whitcroft					 $op eq '%')
18190a920b5bSAndy Whitcroft				{
1820773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1821de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1822de7d4f0eSAndy Whitcroft							$hereptr);
18230a920b5bSAndy Whitcroft					}
18240a920b5bSAndy Whitcroft
18251f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
18261f65f947SAndy Whitcroft				# terminating a case value or a label.
18271f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
18281f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
18291f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
18301f65f947SAndy Whitcroft					}
18311f65f947SAndy Whitcroft
18320a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1833cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
18341f65f947SAndy Whitcroft					my $ok = 0;
18351f65f947SAndy Whitcroft
183622f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
18371f65f947SAndy Whitcroft					if (($op eq '<' &&
18381f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
18391f65f947SAndy Whitcroft					    ($op eq '>' &&
18401f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
18411f65f947SAndy Whitcroft					{
18421f65f947SAndy Whitcroft					    	$ok = 1;
18431f65f947SAndy Whitcroft					}
18441f65f947SAndy Whitcroft
18451f65f947SAndy Whitcroft					# Ignore ?:
18461f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
18471f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
18481f65f947SAndy Whitcroft					    	$ok = 1;
18491f65f947SAndy Whitcroft					}
18501f65f947SAndy Whitcroft
18511f65f947SAndy Whitcroft					if ($ok == 0) {
1852773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
18530a920b5bSAndy Whitcroft					}
185422f2a2efSAndy Whitcroft				}
18554a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
18560a920b5bSAndy Whitcroft			}
18570a920b5bSAndy Whitcroft		}
18580a920b5bSAndy Whitcroft
1859f0a594c1SAndy Whitcroft# check for multiple assignments
1860f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
18616c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1862f0a594c1SAndy Whitcroft		}
1863f0a594c1SAndy Whitcroft
186422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
186522f2a2efSAndy Whitcroft## # continuation.
186622f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
186722f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
186822f2a2efSAndy Whitcroft##
186922f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
187022f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
187122f2a2efSAndy Whitcroft## 			my $ln = $line;
187222f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
187322f2a2efSAndy Whitcroft## 			}
187422f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
187522f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
187622f2a2efSAndy Whitcroft## 			}
187722f2a2efSAndy Whitcroft## 		}
1878f0a594c1SAndy Whitcroft
18790a920b5bSAndy Whitcroft#need space before brace following if, while, etc
188022f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
188122f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1882773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1883de7d4f0eSAndy Whitcroft		}
1884de7d4f0eSAndy Whitcroft
1885de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1886de7d4f0eSAndy Whitcroft# on the line
1887de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1888773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
18890a920b5bSAndy Whitcroft		}
18900a920b5bSAndy Whitcroft
189122f2a2efSAndy Whitcroft# check spacing on square brackets
189222f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1893773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
189422f2a2efSAndy Whitcroft		}
189522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1896773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
189722f2a2efSAndy Whitcroft		}
189822f2a2efSAndy Whitcroft
1899c45dcabdSAndy Whitcroft# check spacing on parentheses
19009c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
19019c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1902773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
190322f2a2efSAndy Whitcroft		}
190413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1905c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
1906c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
1907773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
190822f2a2efSAndy Whitcroft		}
190922f2a2efSAndy Whitcroft
19100a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
19114a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
19120a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1913de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
19140a920b5bSAndy Whitcroft		}
19150a920b5bSAndy Whitcroft
1916c45dcabdSAndy Whitcroft# Return is not a function.
1917c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1918c45dcabdSAndy Whitcroft			my $spacing = $1;
1919c45dcabdSAndy Whitcroft			my $value = $2;
1920c45dcabdSAndy Whitcroft
1921c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
1922fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
1923c45dcabdSAndy Whitcroft			while ($value =~ s/\([^\(\)]*\)/1/) {
1924c45dcabdSAndy Whitcroft			}
1925c45dcabdSAndy Whitcroft
1926c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1927c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1928c45dcabdSAndy Whitcroft
1929c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
1930c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
1931c45dcabdSAndy Whitcroft			}
1932c45dcabdSAndy Whitcroft		}
1933c45dcabdSAndy Whitcroft
19340a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
19354a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1936773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
19370a920b5bSAndy Whitcroft		}
19380a920b5bSAndy Whitcroft
1939f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
1940f5fe35ddSAndy Whitcroft# statements after the conditional.
194153210168SAndy Whitcroft		if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
1942171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
19438905a67cSAndy Whitcroft
19448905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1945c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
19468905a67cSAndy Whitcroft			}
19478905a67cSAndy Whitcroft
19488905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
19498905a67cSAndy Whitcroft			# conditional.
1950773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
19518905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
195213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
195353210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
195453210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
1955773647a0SAndy Whitcroft			{
19568905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
19578905a67cSAndy Whitcroft			}
19588905a67cSAndy Whitcroft		}
19598905a67cSAndy Whitcroft
196013214adfSAndy Whitcroft# Check for bitwise tests written as boolean
196113214adfSAndy Whitcroft		if ($line =~ /
196213214adfSAndy Whitcroft			(?:
196313214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
196413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
196513214adfSAndy Whitcroft				(?:\&\&|\|\|)
196613214adfSAndy Whitcroft			|
196713214adfSAndy Whitcroft				(?:\&\&|\|\|)
196813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
196913214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
197013214adfSAndy Whitcroft			)/x)
197113214adfSAndy Whitcroft		{
197213214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
197313214adfSAndy Whitcroft		}
197413214adfSAndy Whitcroft
19758905a67cSAndy Whitcroft# if and else should not have general statements after it
197613214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
197713214adfSAndy Whitcroft			my $s = $1;
197813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
197913214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
19808905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
19810a920b5bSAndy Whitcroft			}
198213214adfSAndy Whitcroft		}
1983a1080bf8SAndy Whitcroft# case and default should not have general statements after them
1984a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
1985a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
1986a1080bf8SAndy Whitcroft			(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
1987a1080bf8SAndy Whitcroft			\s*return\s+
1988a1080bf8SAndy Whitcroft		    )/xg)
1989a1080bf8SAndy Whitcroft		{
1990a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
1991a1080bf8SAndy Whitcroft		}
19920a920b5bSAndy Whitcroft
19930a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
19940a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
19950a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
19960a920b5bSAndy Whitcroft						$previndent == $indent) {
1997de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
19980a920b5bSAndy Whitcroft		}
19990a920b5bSAndy Whitcroft
2000c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2001c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2002c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2003c2fdda0dSAndy Whitcroft
2004c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2005c2fdda0dSAndy Whitcroft			# conditional.
2006773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2007c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2008c2fdda0dSAndy Whitcroft
2009c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2010c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2011c2fdda0dSAndy Whitcroft			}
2012c2fdda0dSAndy Whitcroft		}
2013c2fdda0dSAndy Whitcroft
20140a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
20150a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
20160a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
20170a920b5bSAndy Whitcroft#		    print "$herecurr";
20180a920b5bSAndy Whitcroft#		    $clean = 0;
20190a920b5bSAndy Whitcroft#		}
20200a920b5bSAndy Whitcroft
20210a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2022c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2023de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
20240a920b5bSAndy Whitcroft		}
20250a920b5bSAndy Whitcroft
2026653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2027c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2028e09dec48SAndy Whitcroft			my $file = "$1.h";
2029e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2030e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2031e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2032c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2033c45dcabdSAndy Whitcroft			{
2034e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2035e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2036e09dec48SAndy Whitcroft				} else {
2037e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2038e09dec48SAndy Whitcroft				}
20390a920b5bSAndy Whitcroft			}
20400a920b5bSAndy Whitcroft		}
20410a920b5bSAndy Whitcroft
2042653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2043653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2044cf655043SAndy Whitcroft# in a known good container
2045b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2046b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2047d8aaf121SAndy Whitcroft			my $ln = $linenr;
2048d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2049c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2050c45dcabdSAndy Whitcroft			my $ctx = '';
2051653d4876SAndy Whitcroft
2052c45dcabdSAndy Whitcroft			my $args = defined($1);
2053de7d4f0eSAndy Whitcroft
2054c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2055c45dcabdSAndy Whitcroft			# search to that.
2056c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2057c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2058c45dcabdSAndy Whitcroft			{
2059c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2060a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2061c45dcabdSAndy Whitcroft				$ln++;
2062de7d4f0eSAndy Whitcroft			}
2063c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2064d8aaf121SAndy Whitcroft
2065c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2066c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2067c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2068a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2069c45dcabdSAndy Whitcroft
2070c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2071c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2072c45dcabdSAndy Whitcroft			$rest = '';
2073636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2074636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2075a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2076c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2077c45dcabdSAndy Whitcroft					$cnt--;
2078a3bb97a7SAndy Whitcroft				}
2079a3bb97a7SAndy Whitcroft				$ln++;
2080c45dcabdSAndy Whitcroft				$off = 0;
2081c45dcabdSAndy Whitcroft			}
2082c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2083c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2084c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2085c45dcabdSAndy Whitcroft
2086c45dcabdSAndy Whitcroft			# Clean up the original statement.
2087c45dcabdSAndy Whitcroft			if ($args) {
2088c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2089d8aaf121SAndy Whitcroft			} else {
2090c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2091c45dcabdSAndy Whitcroft			}
2092292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2093c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2094c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2095c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2096c45dcabdSAndy Whitcroft
2097c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2098c45dcabdSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/) {
2099c45dcabdSAndy Whitcroft			}
2100c45dcabdSAndy Whitcroft			while ($dstat =~ s/\{[^\{\}]*\}/1/) {
2101c45dcabdSAndy Whitcroft			}
2102c45dcabdSAndy Whitcroft
2103c45dcabdSAndy Whitcroft			my $exceptions = qr{
2104c45dcabdSAndy Whitcroft				$Declare|
2105c45dcabdSAndy Whitcroft				module_param_named|
2106c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2107c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2108c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2109c45dcabdSAndy Whitcroft				__typeof__\(
2110c45dcabdSAndy Whitcroft			}x;
2111a3bb97a7SAndy Whitcroft			#print "REST<$rest>\n";
2112c45dcabdSAndy Whitcroft			if ($rest ne '') {
2113c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2114c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2115c45dcabdSAndy Whitcroft				{
2116c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2117c45dcabdSAndy Whitcroft				}
2118c45dcabdSAndy Whitcroft
2119c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2120c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2121c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2122c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2123c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2124c45dcabdSAndy Whitcroft				{
2125de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2126d8aaf121SAndy Whitcroft				}
21270a920b5bSAndy Whitcroft			}
2128653d4876SAndy Whitcroft		}
21290a920b5bSAndy Whitcroft
2130f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
213113214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
213213214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2133cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
213413214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2135cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2136cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
213713214adfSAndy Whitcroft				my $allowed = 0;
213813214adfSAndy Whitcroft				my $seen = 0;
2139773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2140cf655043SAndy Whitcroft				my $ln = $linenr - 1;
214113214adfSAndy Whitcroft				for my $chunk (@chunks) {
214213214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
214313214adfSAndy Whitcroft
2144773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2145773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2146773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2147773647a0SAndy Whitcroft
2148773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2149773647a0SAndy Whitcroft
2150773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2151773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2152773647a0SAndy Whitcroft
2153773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2154cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2155cf655043SAndy Whitcroft
2156773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
215713214adfSAndy Whitcroft
215813214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
215913214adfSAndy Whitcroft
2160cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2161cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2162cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
216313214adfSAndy Whitcroft						$allowed = 1;
216413214adfSAndy Whitcroft					}
216513214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2166cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
216713214adfSAndy Whitcroft						$allowed = 1;
216813214adfSAndy Whitcroft					}
2169cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2170cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
217113214adfSAndy Whitcroft						$allowed = 1;
217213214adfSAndy Whitcroft					}
217313214adfSAndy Whitcroft				}
217413214adfSAndy Whitcroft				if ($seen && !$allowed) {
2175cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
217613214adfSAndy Whitcroft				}
217713214adfSAndy Whitcroft			}
217813214adfSAndy Whitcroft		}
2179773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
218013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2181cf655043SAndy Whitcroft			my $allowed = 0;
2182f0a594c1SAndy Whitcroft
2183cf655043SAndy Whitcroft			# Check the pre-context.
2184cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2185cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2186cf655043SAndy Whitcroft				$allowed = 1;
2187f0a594c1SAndy Whitcroft			}
2188773647a0SAndy Whitcroft
2189773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2190773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2191773647a0SAndy Whitcroft
2192cf655043SAndy Whitcroft			# Check the condition.
2193cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2194773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2195cf655043SAndy Whitcroft			if (defined $cond) {
2196773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2197cf655043SAndy Whitcroft			}
2198cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2199cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2200cf655043SAndy Whitcroft				$allowed = 1;
2201cf655043SAndy Whitcroft			}
2202cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2203cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2204cf655043SAndy Whitcroft				$allowed = 1;
2205cf655043SAndy Whitcroft			}
2206cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2207cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2208cf655043SAndy Whitcroft				$allowed = 1;
2209cf655043SAndy Whitcroft			}
2210cf655043SAndy Whitcroft			# Check the post-context.
2211cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2212cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2213cf655043SAndy Whitcroft				if (defined $cond) {
2214773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2215cf655043SAndy Whitcroft				}
2216cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2217cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2218cf655043SAndy Whitcroft					$allowed = 1;
2219cf655043SAndy Whitcroft				}
2220cf655043SAndy Whitcroft			}
2221cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2222cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2223f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2224cf655043SAndy Whitcroft
2225f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2226f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2227cf655043SAndy Whitcroft				}
2228cf655043SAndy Whitcroft
2229cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2230f0a594c1SAndy Whitcroft			}
2231f0a594c1SAndy Whitcroft		}
2232f0a594c1SAndy Whitcroft
2233653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
22344a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2235c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2236de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
22370a920b5bSAndy Whitcroft			}
22380a920b5bSAndy Whitcroft		}
22390a920b5bSAndy Whitcroft
22404a0df2efSAndy Whitcroft# don't use deprecated functions
22414a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
224200df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2243de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
22444a0df2efSAndy Whitcroft			}
22454a0df2efSAndy Whitcroft		}
22464a0df2efSAndy Whitcroft
22474a0df2efSAndy Whitcroft# no volatiles please
22486c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
22496c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2250de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
22514a0df2efSAndy Whitcroft		}
22524a0df2efSAndy Whitcroft
22539c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
22549c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
22559c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
22569c0ca6f9SAndy Whitcroft		}
22579c0ca6f9SAndy Whitcroft
225800df344fSAndy Whitcroft# warn about #if 0
2259c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2260de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2261de7d4f0eSAndy Whitcroft				$herecurr);
22624a0df2efSAndy Whitcroft		}
22634a0df2efSAndy Whitcroft
2264f0a594c1SAndy Whitcroft# check for needless kfree() checks
2265f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2266f0a594c1SAndy Whitcroft			my $expr = $1;
2267f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
22683c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2269f0a594c1SAndy Whitcroft			}
2270f0a594c1SAndy Whitcroft		}
22714c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
22724c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
22734c432a8fSGreg Kroah-Hartman			my $expr = $1;
22744c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
22754c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
22764c432a8fSGreg Kroah-Hartman			}
22774c432a8fSGreg Kroah-Hartman		}
2278f0a594c1SAndy Whitcroft
227900df344fSAndy Whitcroft# warn about #ifdefs in C files
2280c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
228100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
228200df344fSAndy Whitcroft#			print "$herecurr";
228300df344fSAndy Whitcroft#			$clean = 0;
228400df344fSAndy Whitcroft#		}
228500df344fSAndy Whitcroft
228622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2287c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
228822f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
228922f2a2efSAndy Whitcroft		}
229022f2a2efSAndy Whitcroft
22914a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2292171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2293171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
22944a0df2efSAndy Whitcroft			my $which = $1;
22954a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2296de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
22974a0df2efSAndy Whitcroft			}
22984a0df2efSAndy Whitcroft		}
22994a0df2efSAndy Whitcroft# check for memory barriers without a comment.
23004a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
23014a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2302de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
23034a0df2efSAndy Whitcroft			}
23044a0df2efSAndy Whitcroft		}
23054a0df2efSAndy Whitcroft# check of hardware specific defines
2306c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2307de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
23080a920b5bSAndy Whitcroft		}
2309653d4876SAndy Whitcroft
2310de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2311de7d4f0eSAndy Whitcroft# storage class and type.
23129c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
23139c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2314de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2315de7d4f0eSAndy Whitcroft		}
2316de7d4f0eSAndy Whitcroft
23178905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
23188905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
23198905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
23208905a67cSAndy Whitcroft		}
23218905a67cSAndy Whitcroft
2322de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2323171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2324c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2325171ae1a4SAndy Whitcroft		{
2326c45dcabdSAndy Whitcroft			my $function_name = $1;
2327c45dcabdSAndy Whitcroft			my $paren_space = $2;
2328171ae1a4SAndy Whitcroft
2329171ae1a4SAndy Whitcroft			my $s = $stat;
2330171ae1a4SAndy Whitcroft			if (defined $cond) {
2331171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2332171ae1a4SAndy Whitcroft			}
2333c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2334c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2335c45dcabdSAndy Whitcroft			{
2336de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2337de7d4f0eSAndy Whitcroft			}
2338de7d4f0eSAndy Whitcroft
2339171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2340171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2341171ae1a4SAndy Whitcroft			}
23429c9ba34eSAndy Whitcroft
23439c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
23449c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
23459c9ba34eSAndy Whitcroft		{
23469c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2347171ae1a4SAndy Whitcroft		}
2348171ae1a4SAndy Whitcroft
2349de7d4f0eSAndy Whitcroft# checks for new __setup's
2350de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2351de7d4f0eSAndy Whitcroft			my $name = $1;
2352de7d4f0eSAndy Whitcroft
2353de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2354de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2355de7d4f0eSAndy Whitcroft			}
2356653d4876SAndy Whitcroft		}
23579c0ca6f9SAndy Whitcroft
23589c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
23599c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
23609c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
23619c0ca6f9SAndy Whitcroft		}
236213214adfSAndy Whitcroft
236313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
236413214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
236513214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
236613214adfSAndy Whitcroft		}
2367773647a0SAndy Whitcroft
2368773647a0SAndy Whitcroft# check for semaphores used as mutexes
2369171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2370773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2371773647a0SAndy Whitcroft		}
2372773647a0SAndy Whitcroft# check for semaphores used as mutexes
2373171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2374773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2375773647a0SAndy Whitcroft		}
2376773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2377773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2378773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2379773647a0SAndy Whitcroft		}
2380f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2381f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2382f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2383f3db6639SMichael Ellerman		}
2384773647a0SAndy Whitcroft
2385773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2386773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2387773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2388c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2389c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2390171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2391171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2392171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2393773647a0SAndy Whitcroft		{
2394773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2395773647a0SAndy Whitcroft		}
23969c9ba34eSAndy Whitcroft
23979c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
23989c9ba34eSAndy Whitcroft		my $string;
23999c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
24009c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
24012a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
24029c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
24039c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
24049c9ba34eSAndy Whitcroft				last;
24059c9ba34eSAndy Whitcroft			}
24069c9ba34eSAndy Whitcroft		}
240713214adfSAndy Whitcroft	}
240813214adfSAndy Whitcroft
240913214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
241013214adfSAndy Whitcroft	# so just keep quiet.
241113214adfSAndy Whitcroft	if ($#rawlines == -1) {
241213214adfSAndy Whitcroft		exit(0);
24130a920b5bSAndy Whitcroft	}
24140a920b5bSAndy Whitcroft
24158905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
24168905a67cSAndy Whitcroft	# things that appear to be patches.
24178905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
24188905a67cSAndy Whitcroft		exit(0);
24198905a67cSAndy Whitcroft	}
24208905a67cSAndy Whitcroft
24218905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
24228905a67cSAndy Whitcroft	# just keep quiet.
24238905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
24248905a67cSAndy Whitcroft		exit(0);
24258905a67cSAndy Whitcroft	}
24268905a67cSAndy Whitcroft
24278905a67cSAndy Whitcroft	if (!$is_patch) {
2428de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
24290a920b5bSAndy Whitcroft	}
24300a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2431de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
24320a920b5bSAndy Whitcroft	}
24330a920b5bSAndy Whitcroft
2434f0a594c1SAndy Whitcroft	print report_dump();
243513214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
243613214adfSAndy Whitcroft		print "$filename " if ($summary_file);
24376c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
24386c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
24396c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
24408905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
24416c72ffaaSAndy Whitcroft	}
24428905a67cSAndy Whitcroft
24430a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2444c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
24450a920b5bSAndy Whitcroft	}
24460a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2447c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
24480a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
24490a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
24500a920b5bSAndy Whitcroft	}
245113214adfSAndy Whitcroft
24520a920b5bSAndy Whitcroft	return $clean;
24530a920b5bSAndy Whitcroft}
2454