xref: /linux-6.15/scripts/checkpatch.pl (revision bb44ad39)
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
125fdd23acSAndy Whitcroftmy $V = '0.23';
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
1498ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
1508ed22cadSAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:\d|\d\d)|
1518ed22cadSAndy Whitcroft	atomic_t
1528ed22cadSAndy Whitcroft)};
1538ed22cadSAndy Whitcroft
1548905a67cSAndy Whitcroftour @typeList = (
1558905a67cSAndy Whitcroft	qr{void},
156c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
157c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
158c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
159c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
160c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
161c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
162c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
1638905a67cSAndy Whitcroft	qr{unsigned},
1648905a67cSAndy Whitcroft	qr{float},
1658905a67cSAndy Whitcroft	qr{double},
1668905a67cSAndy Whitcroft	qr{bool},
1678905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1688905a67cSAndy Whitcroft	qr{union\s+$Ident},
1698905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1708905a67cSAndy Whitcroft	qr{${Ident}_t},
1718905a67cSAndy Whitcroft	qr{${Ident}_handler},
1728905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1738905a67cSAndy Whitcroft);
174c45dcabdSAndy Whitcroftour @modifierList = (
175c45dcabdSAndy Whitcroft	qr{fastcall},
176c45dcabdSAndy Whitcroft);
1778905a67cSAndy Whitcroft
1788905a67cSAndy Whitcroftsub build_types {
179d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
180d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
181c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
1828905a67cSAndy Whitcroft	$NonptrType	= qr{
183d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
184cf655043SAndy Whitcroft			(?:
185c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
1868ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
187c45dcabdSAndy Whitcroft				(?:${all}\b)
188cf655043SAndy Whitcroft			)
189c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
1908905a67cSAndy Whitcroft		  }x;
1918905a67cSAndy Whitcroft	$Type	= qr{
192c45dcabdSAndy Whitcroft			$NonptrType
1938905a67cSAndy Whitcroft			(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
194c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
1958905a67cSAndy Whitcroft		  }x;
1968905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
1978905a67cSAndy Whitcroft}
1988905a67cSAndy Whitcroftbuild_types();
1996c72ffaaSAndy Whitcroft
2006c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2010a920b5bSAndy Whitcroft
2024a0df2efSAndy Whitcroftmy @dep_includes = ();
2034a0df2efSAndy Whitcroftmy @dep_functions = ();
2046c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2056c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
2066c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
2076c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
2080a920b5bSAndy Whitcroft	while (<REMOVE>) {
209f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
210f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
211f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2124a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2134a0df2efSAndy Whitcroft
214f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
215f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
216f0a594c1SAndy Whitcroft				}
2174a0df2efSAndy Whitcroft			}
2180a920b5bSAndy Whitcroft		}
2190a920b5bSAndy Whitcroft	}
2200a920b5bSAndy Whitcroft}
2210a920b5bSAndy Whitcroft
22200df344fSAndy Whitcroftmy @rawlines = ();
223c2fdda0dSAndy Whitcroftmy @lines = ();
224c2fdda0dSAndy Whitcroftmy $vname;
2256c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2266c72ffaaSAndy Whitcroft	if ($file) {
2276c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2286c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2296c72ffaaSAndy Whitcroft	} else {
2306c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2316c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2326c72ffaaSAndy Whitcroft	}
233c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
234c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
235c2fdda0dSAndy Whitcroft	} else {
236c2fdda0dSAndy Whitcroft		$vname = $filename;
237c2fdda0dSAndy Whitcroft	}
2386c72ffaaSAndy Whitcroft	while (<FILE>) {
2390a920b5bSAndy Whitcroft		chomp;
24000df344fSAndy Whitcroft		push(@rawlines, $_);
2416c72ffaaSAndy Whitcroft	}
2426c72ffaaSAndy Whitcroft	close(FILE);
243c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2440a920b5bSAndy Whitcroft		$exit = 1;
2450a920b5bSAndy Whitcroft	}
24600df344fSAndy Whitcroft	@rawlines = ();
24713214adfSAndy Whitcroft	@lines = ();
2480a920b5bSAndy Whitcroft}
2490a920b5bSAndy Whitcroft
2500a920b5bSAndy Whitcroftexit($exit);
2510a920b5bSAndy Whitcroft
2520a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2536c72ffaaSAndy Whitcroft	my ($root) = @_;
2546c72ffaaSAndy Whitcroft
2556c72ffaaSAndy Whitcroft	my @tree_check = (
2566c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2576c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2586c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2596c72ffaaSAndy Whitcroft	);
2606c72ffaaSAndy Whitcroft
2616c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2626c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2630a920b5bSAndy Whitcroft			return 0;
2640a920b5bSAndy Whitcroft		}
2656c72ffaaSAndy Whitcroft	}
2666c72ffaaSAndy Whitcroft	return 1;
2676c72ffaaSAndy Whitcroft}
2680a920b5bSAndy Whitcroft
2690a920b5bSAndy Whitcroftsub expand_tabs {
2700a920b5bSAndy Whitcroft	my ($str) = @_;
2710a920b5bSAndy Whitcroft
2720a920b5bSAndy Whitcroft	my $res = '';
2730a920b5bSAndy Whitcroft	my $n = 0;
2740a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2750a920b5bSAndy Whitcroft		if ($c eq "\t") {
2760a920b5bSAndy Whitcroft			$res .= ' ';
2770a920b5bSAndy Whitcroft			$n++;
2780a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2790a920b5bSAndy Whitcroft				$res .= ' ';
2800a920b5bSAndy Whitcroft			}
2810a920b5bSAndy Whitcroft			next;
2820a920b5bSAndy Whitcroft		}
2830a920b5bSAndy Whitcroft		$res .= $c;
2840a920b5bSAndy Whitcroft		$n++;
2850a920b5bSAndy Whitcroft	}
2860a920b5bSAndy Whitcroft
2870a920b5bSAndy Whitcroft	return $res;
2880a920b5bSAndy Whitcroft}
2896c72ffaaSAndy Whitcroftsub copy_spacing {
290773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
2916c72ffaaSAndy Whitcroft	return $res;
2926c72ffaaSAndy Whitcroft}
2930a920b5bSAndy Whitcroft
2944a0df2efSAndy Whitcroftsub line_stats {
2954a0df2efSAndy Whitcroft	my ($line) = @_;
2964a0df2efSAndy Whitcroft
2974a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
2984a0df2efSAndy Whitcroft	$line =~ s/^.//;
2994a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3004a0df2efSAndy Whitcroft
3014a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3024a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3034a0df2efSAndy Whitcroft
3044a0df2efSAndy Whitcroft	return (length($line), length($white));
3054a0df2efSAndy Whitcroft}
3064a0df2efSAndy Whitcroft
307773647a0SAndy Whitcroftmy $sanitise_quote = '';
308773647a0SAndy Whitcroft
309773647a0SAndy Whitcroftsub sanitise_line_reset {
310773647a0SAndy Whitcroft	my ($in_comment) = @_;
311773647a0SAndy Whitcroft
312773647a0SAndy Whitcroft	if ($in_comment) {
313773647a0SAndy Whitcroft		$sanitise_quote = '*/';
314773647a0SAndy Whitcroft	} else {
315773647a0SAndy Whitcroft		$sanitise_quote = '';
316773647a0SAndy Whitcroft	}
317773647a0SAndy Whitcroft}
31800df344fSAndy Whitcroftsub sanitise_line {
31900df344fSAndy Whitcroft	my ($line) = @_;
32000df344fSAndy Whitcroft
32100df344fSAndy Whitcroft	my $res = '';
32200df344fSAndy Whitcroft	my $l = '';
32300df344fSAndy Whitcroft
324c2fdda0dSAndy Whitcroft	my $qlen = 0;
325773647a0SAndy Whitcroft	my $off = 0;
326773647a0SAndy Whitcroft	my $c;
32700df344fSAndy Whitcroft
328773647a0SAndy Whitcroft	# Always copy over the diff marker.
329773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
330773647a0SAndy Whitcroft
331773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
332773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
333773647a0SAndy Whitcroft
334773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
335773647a0SAndy Whitcroft		# and end, all to $;.
336773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
337773647a0SAndy Whitcroft			$sanitise_quote = '*/';
338773647a0SAndy Whitcroft
339773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
340773647a0SAndy Whitcroft			$off++;
34100df344fSAndy Whitcroft			next;
342773647a0SAndy Whitcroft		}
34381bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
344773647a0SAndy Whitcroft			$sanitise_quote = '';
345773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
346773647a0SAndy Whitcroft			$off++;
347773647a0SAndy Whitcroft			next;
348773647a0SAndy Whitcroft		}
349773647a0SAndy Whitcroft
350773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
351773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
352773647a0SAndy Whitcroft		    $c eq "\\") {
353773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
354773647a0SAndy Whitcroft			$off++;
355773647a0SAndy Whitcroft			next;
356773647a0SAndy Whitcroft		}
357773647a0SAndy Whitcroft		# Regular quotes.
358773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
359773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
360773647a0SAndy Whitcroft				$sanitise_quote = $c;
361773647a0SAndy Whitcroft
362773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
363773647a0SAndy Whitcroft				next;
364773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
365773647a0SAndy Whitcroft				$sanitise_quote = '';
36600df344fSAndy Whitcroft			}
36700df344fSAndy Whitcroft		}
368773647a0SAndy Whitcroft
369773647a0SAndy Whitcroft		#print "SQ:$sanitise_quote\n";
370773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
371773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
372773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
373773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
37400df344fSAndy Whitcroft		} else {
375773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
37600df344fSAndy Whitcroft		}
377c2fdda0dSAndy Whitcroft	}
378c2fdda0dSAndy Whitcroft
379c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
380c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
381c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
382c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
383c2fdda0dSAndy Whitcroft
384c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
385c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
386c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
387c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
388c2fdda0dSAndy Whitcroft	}
389c2fdda0dSAndy Whitcroft
39000df344fSAndy Whitcroft	return $res;
39100df344fSAndy Whitcroft}
39200df344fSAndy Whitcroft
3938905a67cSAndy Whitcroftsub ctx_statement_block {
3948905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3958905a67cSAndy Whitcroft	my $line = $linenr - 1;
3968905a67cSAndy Whitcroft	my $blk = '';
3978905a67cSAndy Whitcroft	my $soff = $off;
3988905a67cSAndy Whitcroft	my $coff = $off - 1;
399773647a0SAndy Whitcroft	my $coff_set = 0;
4008905a67cSAndy Whitcroft
40113214adfSAndy Whitcroft	my $loff = 0;
40213214adfSAndy Whitcroft
4038905a67cSAndy Whitcroft	my $type = '';
4048905a67cSAndy Whitcroft	my $level = 0;
405cf655043SAndy Whitcroft	my $p;
4068905a67cSAndy Whitcroft	my $c;
4078905a67cSAndy Whitcroft	my $len = 0;
40813214adfSAndy Whitcroft
40913214adfSAndy Whitcroft	my $remainder;
4108905a67cSAndy Whitcroft	while (1) {
411773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4128905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4138905a67cSAndy Whitcroft		# context.
4148905a67cSAndy Whitcroft		if ($off >= $len) {
4158905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
416dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
417c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4188905a67cSAndy Whitcroft				$remain--;
41913214adfSAndy Whitcroft				$loff = $len;
420c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4218905a67cSAndy Whitcroft				$len = length($blk);
4228905a67cSAndy Whitcroft				$line++;
4238905a67cSAndy Whitcroft				last;
4248905a67cSAndy Whitcroft			}
4258905a67cSAndy Whitcroft			# Bail if there is no further context.
4268905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
42713214adfSAndy Whitcroft			if ($off >= $len) {
4288905a67cSAndy Whitcroft				last;
4298905a67cSAndy Whitcroft			}
4308905a67cSAndy Whitcroft		}
431cf655043SAndy Whitcroft		$p = $c;
4328905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
43313214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4348905a67cSAndy Whitcroft
435773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4368905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4378905a67cSAndy Whitcroft		# outermost level.
4388905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4398905a67cSAndy Whitcroft			last;
4408905a67cSAndy Whitcroft		}
4418905a67cSAndy Whitcroft
44213214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
443773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
444773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
445773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
446773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
447773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
448773647a0SAndy Whitcroft			$coff_set = 1;
449773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
450773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
45113214adfSAndy Whitcroft		}
45213214adfSAndy Whitcroft
4538905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4548905a67cSAndy Whitcroft			$level++;
4558905a67cSAndy Whitcroft			$type = '(';
4568905a67cSAndy Whitcroft		}
4578905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4588905a67cSAndy Whitcroft			$level--;
4598905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4608905a67cSAndy Whitcroft
4618905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4628905a67cSAndy Whitcroft				$coff = $off;
463773647a0SAndy Whitcroft				$coff_set = 1;
464773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4658905a67cSAndy Whitcroft			}
4668905a67cSAndy Whitcroft		}
4678905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4688905a67cSAndy Whitcroft			$level++;
4698905a67cSAndy Whitcroft			$type = '{';
4708905a67cSAndy Whitcroft		}
4718905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4728905a67cSAndy Whitcroft			$level--;
4738905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4748905a67cSAndy Whitcroft
4758905a67cSAndy Whitcroft			if ($level == 0) {
4768905a67cSAndy Whitcroft				last;
4778905a67cSAndy Whitcroft			}
4788905a67cSAndy Whitcroft		}
4798905a67cSAndy Whitcroft		$off++;
4808905a67cSAndy Whitcroft	}
481a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
48213214adfSAndy Whitcroft	if ($off == $len) {
483a3bb97a7SAndy Whitcroft		$loff = $len + 1;
48413214adfSAndy Whitcroft		$line++;
48513214adfSAndy Whitcroft		$remain--;
48613214adfSAndy Whitcroft	}
4878905a67cSAndy Whitcroft
4888905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4898905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4908905a67cSAndy Whitcroft
4918905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4928905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4938905a67cSAndy Whitcroft
494773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
49513214adfSAndy Whitcroft
49613214adfSAndy Whitcroft	return ($statement, $condition,
49713214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
49813214adfSAndy Whitcroft}
49913214adfSAndy Whitcroft
500cf655043SAndy Whitcroftsub statement_lines {
501cf655043SAndy Whitcroft	my ($stmt) = @_;
502cf655043SAndy Whitcroft
503cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
504cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
505cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
506cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
507cf655043SAndy Whitcroft
508cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
509cf655043SAndy Whitcroft
510cf655043SAndy Whitcroft	return $#stmt_lines + 2;
511cf655043SAndy Whitcroft}
512cf655043SAndy Whitcroft
513cf655043SAndy Whitcroftsub statement_rawlines {
514cf655043SAndy Whitcroft	my ($stmt) = @_;
515cf655043SAndy Whitcroft
516cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
517cf655043SAndy Whitcroft
518cf655043SAndy Whitcroft	return $#stmt_lines + 2;
519cf655043SAndy Whitcroft}
520cf655043SAndy Whitcroft
521cf655043SAndy Whitcroftsub statement_block_size {
522cf655043SAndy Whitcroft	my ($stmt) = @_;
523cf655043SAndy Whitcroft
524cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
525cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
526cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
527cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
528cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
529cf655043SAndy Whitcroft
530cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
531cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
532cf655043SAndy Whitcroft
533cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
534cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
535cf655043SAndy Whitcroft
536cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
537cf655043SAndy Whitcroft		return $stmt_lines;
538cf655043SAndy Whitcroft	} else {
539cf655043SAndy Whitcroft		return $stmt_statements;
540cf655043SAndy Whitcroft	}
541cf655043SAndy Whitcroft}
542cf655043SAndy Whitcroft
54313214adfSAndy Whitcroftsub ctx_statement_full {
54413214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
54513214adfSAndy Whitcroft	my ($statement, $condition, $level);
54613214adfSAndy Whitcroft
54713214adfSAndy Whitcroft	my (@chunks);
54813214adfSAndy Whitcroft
549cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
55013214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
55113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
552773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
55313214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
554cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
555cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
556cf655043SAndy Whitcroft	}
557cf655043SAndy Whitcroft
558cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
559cf655043SAndy Whitcroft	# could continue the statement.
560cf655043SAndy Whitcroft	for (;;) {
56113214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
56213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
563cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
564773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
565cf655043SAndy Whitcroft		#print "C: push\n";
566cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
56713214adfSAndy Whitcroft	}
56813214adfSAndy Whitcroft
56913214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5708905a67cSAndy Whitcroft}
5718905a67cSAndy Whitcroft
5724a0df2efSAndy Whitcroftsub ctx_block_get {
573f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5744a0df2efSAndy Whitcroft	my $line;
5754a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5764a0df2efSAndy Whitcroft	my $blk = '';
5774a0df2efSAndy Whitcroft	my @o;
5784a0df2efSAndy Whitcroft	my @c;
5794a0df2efSAndy Whitcroft	my @res = ();
5804a0df2efSAndy Whitcroft
581f0a594c1SAndy Whitcroft	my $level = 0;
58200df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
58300df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
58400df344fSAndy Whitcroft		$remain--;
58500df344fSAndy Whitcroft
58600df344fSAndy Whitcroft		$blk .= $rawlines[$line];
587f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
588f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
589f0a594c1SAndy Whitcroft			if ($off > 0) {
590f0a594c1SAndy Whitcroft				$off--;
591f0a594c1SAndy Whitcroft				next;
592f0a594c1SAndy Whitcroft			}
5934a0df2efSAndy Whitcroft
594f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
595f0a594c1SAndy Whitcroft				$level--;
596f0a594c1SAndy Whitcroft				last if ($level == 0);
597f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
598f0a594c1SAndy Whitcroft				$level++;
599f0a594c1SAndy Whitcroft			}
600f0a594c1SAndy Whitcroft		}
6014a0df2efSAndy Whitcroft
602f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
60300df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
6044a0df2efSAndy Whitcroft		}
6054a0df2efSAndy Whitcroft
606f0a594c1SAndy Whitcroft		last if ($level == 0);
6074a0df2efSAndy Whitcroft	}
6084a0df2efSAndy Whitcroft
609f0a594c1SAndy Whitcroft	return ($level, @res);
6104a0df2efSAndy Whitcroft}
6114a0df2efSAndy Whitcroftsub ctx_block_outer {
6124a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6134a0df2efSAndy Whitcroft
614f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
615f0a594c1SAndy Whitcroft	return @r;
6164a0df2efSAndy Whitcroft}
6174a0df2efSAndy Whitcroftsub ctx_block {
6184a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6194a0df2efSAndy Whitcroft
620f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
621f0a594c1SAndy Whitcroft	return @r;
622653d4876SAndy Whitcroft}
623653d4876SAndy Whitcroftsub ctx_statement {
624f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
625f0a594c1SAndy Whitcroft
626f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
627f0a594c1SAndy Whitcroft	return @r;
628f0a594c1SAndy Whitcroft}
629f0a594c1SAndy Whitcroftsub ctx_block_level {
630653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
631653d4876SAndy Whitcroft
632f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6334a0df2efSAndy Whitcroft}
6349c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6359c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6369c0ca6f9SAndy Whitcroft
6379c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6389c0ca6f9SAndy Whitcroft}
6394a0df2efSAndy Whitcroft
6404a0df2efSAndy Whitcroftsub ctx_locate_comment {
6414a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6424a0df2efSAndy Whitcroft
6434a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
644beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
6454a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6464a0df2efSAndy Whitcroft
6474a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6484a0df2efSAndy Whitcroft	# comment.
6494a0df2efSAndy Whitcroft	my $in_comment = 0;
6504a0df2efSAndy Whitcroft	$current_comment = '';
6514a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
65200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
65300df344fSAndy Whitcroft		#warn "           $line\n";
6544a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6554a0df2efSAndy Whitcroft			$in_comment = 1;
6564a0df2efSAndy Whitcroft		}
6574a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6584a0df2efSAndy Whitcroft			$in_comment = 1;
6594a0df2efSAndy Whitcroft		}
6604a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6614a0df2efSAndy Whitcroft			$current_comment = '';
6624a0df2efSAndy Whitcroft		}
6634a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6644a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6654a0df2efSAndy Whitcroft			$in_comment = 0;
6664a0df2efSAndy Whitcroft		}
6674a0df2efSAndy Whitcroft	}
6684a0df2efSAndy Whitcroft
6694a0df2efSAndy Whitcroft	chomp($current_comment);
6704a0df2efSAndy Whitcroft	return($current_comment);
6714a0df2efSAndy Whitcroft}
6724a0df2efSAndy Whitcroftsub ctx_has_comment {
6734a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6744a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6754a0df2efSAndy Whitcroft
67600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6774a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6784a0df2efSAndy Whitcroft
6794a0df2efSAndy Whitcroft	return ($cmt ne '');
6804a0df2efSAndy Whitcroft}
6814a0df2efSAndy Whitcroft
6824d001e4dSAndy Whitcroftsub raw_line {
6834d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
6844d001e4dSAndy Whitcroft
6854d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
6864d001e4dSAndy Whitcroft	$cnt++;
6874d001e4dSAndy Whitcroft
6884d001e4dSAndy Whitcroft	my $line;
6894d001e4dSAndy Whitcroft	while ($cnt) {
6904d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
6914d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
6924d001e4dSAndy Whitcroft		$cnt--;
6934d001e4dSAndy Whitcroft	}
6944d001e4dSAndy Whitcroft
6954d001e4dSAndy Whitcroft	return $line;
6964d001e4dSAndy Whitcroft}
6974d001e4dSAndy Whitcroft
6980a920b5bSAndy Whitcroftsub cat_vet {
6990a920b5bSAndy Whitcroft	my ($vet) = @_;
7009c0ca6f9SAndy Whitcroft	my ($res, $coded);
7010a920b5bSAndy Whitcroft
7029c0ca6f9SAndy Whitcroft	$res = '';
7036c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
7046c72ffaaSAndy Whitcroft		$res .= $1;
7056c72ffaaSAndy Whitcroft		if ($2 ne '') {
7069c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
7076c72ffaaSAndy Whitcroft			$res .= $coded;
7086c72ffaaSAndy Whitcroft		}
7099c0ca6f9SAndy Whitcroft	}
7109c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
7110a920b5bSAndy Whitcroft
7129c0ca6f9SAndy Whitcroft	return $res;
7130a920b5bSAndy Whitcroft}
7140a920b5bSAndy Whitcroft
715c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
716cf655043SAndy Whitcroftmy $av_pending;
717c2fdda0dSAndy Whitcroftmy @av_paren_type;
7181f65f947SAndy Whitcroftmy $av_pend_colon;
719c2fdda0dSAndy Whitcroft
720c2fdda0dSAndy Whitcroftsub annotate_reset {
721c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
722cf655043SAndy Whitcroft	$av_pending = '_';
723cf655043SAndy Whitcroft	@av_paren_type = ('E');
7241f65f947SAndy Whitcroft	$av_pend_colon = 'O';
725c2fdda0dSAndy Whitcroft}
726c2fdda0dSAndy Whitcroft
7276c72ffaaSAndy Whitcroftsub annotate_values {
7286c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
7296c72ffaaSAndy Whitcroft
7306c72ffaaSAndy Whitcroft	my $res;
7311f65f947SAndy Whitcroft	my $var = '_' x length($stream);
7326c72ffaaSAndy Whitcroft	my $cur = $stream;
7336c72ffaaSAndy Whitcroft
734c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
7356c72ffaaSAndy Whitcroft
7366c72ffaaSAndy Whitcroft	while (length($cur)) {
737773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
738cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
739171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
7406c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
741c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
742c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
743cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
744c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7456c72ffaaSAndy Whitcroft			}
7466c72ffaaSAndy Whitcroft
7478ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
748c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7496c72ffaaSAndy Whitcroft			$type = 'T';
7506c72ffaaSAndy Whitcroft
751389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
752389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
753389a2fe5SAndy Whitcroft			$type = 'T';
754389a2fe5SAndy Whitcroft
755c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
756171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
757c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
758171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
759171ae1a4SAndy Whitcroft			if ($2 ne '') {
760cf655043SAndy Whitcroft				$av_pending = 'N';
761171ae1a4SAndy Whitcroft			}
762171ae1a4SAndy Whitcroft			$type = 'E';
763171ae1a4SAndy Whitcroft
764c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
765171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
766171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
767171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
7686c72ffaaSAndy Whitcroft
769c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
770cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
771c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
772cf655043SAndy Whitcroft
773cf655043SAndy Whitcroft			push(@av_paren_type, $type);
774cf655043SAndy Whitcroft			push(@av_paren_type, $type);
775171ae1a4SAndy Whitcroft			$type = 'E';
776cf655043SAndy Whitcroft
777c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
778cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
779cf655043SAndy Whitcroft			$av_preprocessor = 1;
780cf655043SAndy Whitcroft
781cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
782cf655043SAndy Whitcroft
783171ae1a4SAndy Whitcroft			$type = 'E';
784cf655043SAndy Whitcroft
785c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
786cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
787cf655043SAndy Whitcroft
788cf655043SAndy Whitcroft			$av_preprocessor = 1;
789cf655043SAndy Whitcroft
790cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
791cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
792cf655043SAndy Whitcroft			pop(@av_paren_type);
793cf655043SAndy Whitcroft			push(@av_paren_type, $type);
794171ae1a4SAndy Whitcroft			$type = 'E';
7956c72ffaaSAndy Whitcroft
7966c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
797c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7986c72ffaaSAndy Whitcroft
799171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
800171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
801171ae1a4SAndy Whitcroft			$av_pending = $type;
802171ae1a4SAndy Whitcroft			$type = 'N';
803171ae1a4SAndy Whitcroft
8046c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
805c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
8066c72ffaaSAndy Whitcroft			if (defined $2) {
807cf655043SAndy Whitcroft				$av_pending = 'V';
8086c72ffaaSAndy Whitcroft			}
8096c72ffaaSAndy Whitcroft			$type = 'N';
8106c72ffaaSAndy Whitcroft
81114b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
812c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
81314b111c1SAndy Whitcroft			$av_pending = 'E';
8146c72ffaaSAndy Whitcroft			$type = 'N';
8156c72ffaaSAndy Whitcroft
8161f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
8171f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
8181f65f947SAndy Whitcroft			$av_pend_colon = 'C';
8191f65f947SAndy Whitcroft			$type = 'N';
8201f65f947SAndy Whitcroft
82114b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
822c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
8236c72ffaaSAndy Whitcroft			$type = 'N';
8246c72ffaaSAndy Whitcroft
8256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
826c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
827cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
828cf655043SAndy Whitcroft			$av_pending = '_';
8296c72ffaaSAndy Whitcroft			$type = 'N';
8306c72ffaaSAndy Whitcroft
8316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
832cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
833cf655043SAndy Whitcroft			if ($new_type ne '_') {
834cf655043SAndy Whitcroft				$type = $new_type;
835c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
836c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
8376c72ffaaSAndy Whitcroft			} else {
838c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
8396c72ffaaSAndy Whitcroft			}
8406c72ffaaSAndy Whitcroft
841c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
842c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
843c8cb2ca3SAndy Whitcroft			$type = 'V';
844cf655043SAndy Whitcroft			$av_pending = 'V';
8456c72ffaaSAndy Whitcroft
8461f65f947SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):/) {
8471f65f947SAndy Whitcroft			if ($type eq 'E') {
8481f65f947SAndy Whitcroft				$av_pend_colon = 'L';
8491f65f947SAndy Whitcroft			} elsif ($type eq 'T') {
8501f65f947SAndy Whitcroft				$av_pend_colon = 'B';
8511f65f947SAndy Whitcroft			}
8521f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
8531f65f947SAndy Whitcroft			$type = 'V';
8541f65f947SAndy Whitcroft
8556c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
856c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
8576c72ffaaSAndy Whitcroft			$type = 'V';
8586c72ffaaSAndy Whitcroft
8596c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
860c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
8616c72ffaaSAndy Whitcroft			$type = 'N';
8626c72ffaaSAndy Whitcroft
863cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
864c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
86513214adfSAndy Whitcroft			$type = 'E';
8661f65f947SAndy Whitcroft			$av_pend_colon = 'O';
86713214adfSAndy Whitcroft
8681f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
8691f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
8701f65f947SAndy Whitcroft			$type = 'N';
8711f65f947SAndy Whitcroft
8721f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
8731f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
8741f65f947SAndy Whitcroft
8751f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
8761f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
8771f65f947SAndy Whitcroft				$type = 'E';
8781f65f947SAndy Whitcroft			} else {
8791f65f947SAndy Whitcroft				$type = 'N';
8801f65f947SAndy Whitcroft			}
8811f65f947SAndy Whitcroft			$av_pend_colon = 'O';
8821f65f947SAndy Whitcroft
8831f65f947SAndy Whitcroft		} elsif ($cur =~ /^(;|\[)/o) {
88413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
8856c72ffaaSAndy Whitcroft			$type = 'N';
8866c72ffaaSAndy Whitcroft
8870d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
88874048ed8SAndy Whitcroft			my $variant;
88974048ed8SAndy Whitcroft
89074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
89174048ed8SAndy Whitcroft			if ($type eq 'V') {
89274048ed8SAndy Whitcroft				$variant = 'B';
89374048ed8SAndy Whitcroft			} else {
89474048ed8SAndy Whitcroft				$variant = 'U';
89574048ed8SAndy Whitcroft			}
89674048ed8SAndy Whitcroft
89774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
89874048ed8SAndy Whitcroft			$type = 'N';
89974048ed8SAndy Whitcroft
9006c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
901c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
9026c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
9036c72ffaaSAndy Whitcroft				$type = 'N';
9046c72ffaaSAndy Whitcroft			}
9056c72ffaaSAndy Whitcroft
9066c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
907c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
9086c72ffaaSAndy Whitcroft		}
9096c72ffaaSAndy Whitcroft		if (defined $1) {
9106c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
9116c72ffaaSAndy Whitcroft			$res .= $type x length($1);
9126c72ffaaSAndy Whitcroft		}
9136c72ffaaSAndy Whitcroft	}
9146c72ffaaSAndy Whitcroft
9151f65f947SAndy Whitcroft	return ($res, $var);
9166c72ffaaSAndy Whitcroft}
9176c72ffaaSAndy Whitcroft
9188905a67cSAndy Whitcroftsub possible {
91913214adfSAndy Whitcroft	my ($possible, $line) = @_;
9208905a67cSAndy Whitcroft
9210776e594SAndy Whitcroft	print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
9220776e594SAndy Whitcroft	if ($possible !~ /(?:
9230776e594SAndy Whitcroft		^(?:
9240776e594SAndy Whitcroft			$Modifier|
9250776e594SAndy Whitcroft			$Storage|
9260776e594SAndy Whitcroft			$Type|
9270776e594SAndy Whitcroft			DEFINE_\S+|
9280776e594SAndy Whitcroft			goto|
9290776e594SAndy Whitcroft			return|
9300776e594SAndy Whitcroft			case|
9310776e594SAndy Whitcroft			else|
9320776e594SAndy Whitcroft			asm|__asm__|
9330776e594SAndy Whitcroft			do
9340776e594SAndy Whitcroft		)$|
9350776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
9360776e594SAndy Whitcroft	    )/x) {
937c45dcabdSAndy Whitcroft		# Check for modifiers.
938c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
939c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
940c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
941c45dcabdSAndy Whitcroft
942c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
943c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
944d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
945d2506586SAndy Whitcroft				warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
946d2506586SAndy Whitcroft				push(@modifierList, $modifier);
947d2506586SAndy Whitcroft			}
948c45dcabdSAndy Whitcroft
949c45dcabdSAndy Whitcroft		} else {
95013214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
9518905a67cSAndy Whitcroft			push(@typeList, $possible);
952c45dcabdSAndy Whitcroft		}
9538905a67cSAndy Whitcroft		build_types();
9540776e594SAndy Whitcroft	} else {
9550776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
9568905a67cSAndy Whitcroft	}
9578905a67cSAndy Whitcroft}
9588905a67cSAndy Whitcroft
9596c72ffaaSAndy Whitcroftmy $prefix = '';
9606c72ffaaSAndy Whitcroft
961f0a594c1SAndy Whitcroftsub report {
962773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
963773647a0SAndy Whitcroft		return 0;
964773647a0SAndy Whitcroft	}
9658905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
9668905a67cSAndy Whitcroft
9678905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
9688905a67cSAndy Whitcroft
96913214adfSAndy Whitcroft	push(our @report, $line);
970773647a0SAndy Whitcroft
971773647a0SAndy Whitcroft	return 1;
972f0a594c1SAndy Whitcroft}
973f0a594c1SAndy Whitcroftsub report_dump {
97413214adfSAndy Whitcroft	our @report;
975f0a594c1SAndy Whitcroft}
976de7d4f0eSAndy Whitcroftsub ERROR {
977773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
978de7d4f0eSAndy Whitcroft		our $clean = 0;
9796c72ffaaSAndy Whitcroft		our $cnt_error++;
980de7d4f0eSAndy Whitcroft	}
981773647a0SAndy Whitcroft}
982de7d4f0eSAndy Whitcroftsub WARN {
983773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
984de7d4f0eSAndy Whitcroft		our $clean = 0;
9856c72ffaaSAndy Whitcroft		our $cnt_warn++;
986de7d4f0eSAndy Whitcroft	}
987773647a0SAndy Whitcroft}
988de7d4f0eSAndy Whitcroftsub CHK {
989773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
990de7d4f0eSAndy Whitcroft		our $clean = 0;
9916c72ffaaSAndy Whitcroft		our $cnt_chk++;
9926c72ffaaSAndy Whitcroft	}
993de7d4f0eSAndy Whitcroft}
994de7d4f0eSAndy Whitcroft
9956ecd9674SAndy Whitcroftsub check_absolute_file {
9966ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
9976ecd9674SAndy Whitcroft	my $file = $absolute;
9986ecd9674SAndy Whitcroft
9996ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
10006ecd9674SAndy Whitcroft
10016ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
10026ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
10036ecd9674SAndy Whitcroft		if (-f "$root/$file") {
10046ecd9674SAndy Whitcroft			##print "file<$file>\n";
10056ecd9674SAndy Whitcroft			last;
10066ecd9674SAndy Whitcroft		}
10076ecd9674SAndy Whitcroft	}
10086ecd9674SAndy Whitcroft	if (! -f _)  {
10096ecd9674SAndy Whitcroft		return 0;
10106ecd9674SAndy Whitcroft	}
10116ecd9674SAndy Whitcroft
10126ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
10136ecd9674SAndy Whitcroft	my $prefix = $absolute;
10146ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
10156ecd9674SAndy Whitcroft
10166ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
10176ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
10186ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
10196ecd9674SAndy Whitcroft	}
10206ecd9674SAndy Whitcroft}
10216ecd9674SAndy Whitcroft
10220a920b5bSAndy Whitcroftsub process {
10230a920b5bSAndy Whitcroft	my $filename = shift;
10240a920b5bSAndy Whitcroft
10250a920b5bSAndy Whitcroft	my $linenr=0;
10260a920b5bSAndy Whitcroft	my $prevline="";
1027c2fdda0dSAndy Whitcroft	my $prevrawline="";
10280a920b5bSAndy Whitcroft	my $stashline="";
1029c2fdda0dSAndy Whitcroft	my $stashrawline="";
10300a920b5bSAndy Whitcroft
10314a0df2efSAndy Whitcroft	my $length;
10320a920b5bSAndy Whitcroft	my $indent;
10330a920b5bSAndy Whitcroft	my $previndent=0;
10340a920b5bSAndy Whitcroft	my $stashindent=0;
10350a920b5bSAndy Whitcroft
1036de7d4f0eSAndy Whitcroft	our $clean = 1;
10370a920b5bSAndy Whitcroft	my $signoff = 0;
10380a920b5bSAndy Whitcroft	my $is_patch = 0;
10390a920b5bSAndy Whitcroft
104013214adfSAndy Whitcroft	our @report = ();
10416c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
10426c72ffaaSAndy Whitcroft	our $cnt_error = 0;
10436c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
10446c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
10456c72ffaaSAndy Whitcroft
10460a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
10470a920b5bSAndy Whitcroft	my $realfile = '';
10480a920b5bSAndy Whitcroft	my $realline = 0;
10490a920b5bSAndy Whitcroft	my $realcnt = 0;
10500a920b5bSAndy Whitcroft	my $here = '';
10510a920b5bSAndy Whitcroft	my $in_comment = 0;
1052c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
10530a920b5bSAndy Whitcroft	my $first_line = 0;
10540a920b5bSAndy Whitcroft
105513214adfSAndy Whitcroft	my $prev_values = 'E';
105613214adfSAndy Whitcroft
105713214adfSAndy Whitcroft	# suppression flags
1058773647a0SAndy Whitcroft	my %suppress_ifbraces;
1059170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
1060653d4876SAndy Whitcroft
1061c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1062de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1063c2fdda0dSAndy Whitcroft	#
1064de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1065de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1066773647a0SAndy Whitcroft
1067773647a0SAndy Whitcroft	sanitise_line_reset();
1068c2fdda0dSAndy Whitcroft	my $line;
1069c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1070773647a0SAndy Whitcroft		$linenr++;
1071773647a0SAndy Whitcroft		$line = $rawline;
1072c2fdda0dSAndy Whitcroft
1073773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1074de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1075de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1076de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1077de7d4f0eSAndy Whitcroft			}
1078773647a0SAndy Whitcroft			#next;
1079de7d4f0eSAndy Whitcroft		}
1080773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1081773647a0SAndy Whitcroft			$realline=$1-1;
1082773647a0SAndy Whitcroft			if (defined $2) {
1083773647a0SAndy Whitcroft				$realcnt=$3+1;
1084773647a0SAndy Whitcroft			} else {
1085773647a0SAndy Whitcroft				$realcnt=1+1;
1086773647a0SAndy Whitcroft			}
1087c45dcabdSAndy Whitcroft			$in_comment = 0;
1088773647a0SAndy Whitcroft
1089773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1090773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1091773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1092773647a0SAndy Whitcroft			# at context start.
1093773647a0SAndy Whitcroft			my $edge;
109401fa9147SAndy Whitcroft			my $cnt = $realcnt;
109501fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
109601fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
109701fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
109801fa9147SAndy Whitcroft				$cnt--;
109901fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
110001fa9147SAndy Whitcroft				($edge) = (defined $rawlines[$ln - 1] &&
110101fa9147SAndy Whitcroft					$rawlines[$ln - 1] =~ m@(/\*|\*/)@);
1102773647a0SAndy Whitcroft				last if (defined $edge);
1103773647a0SAndy Whitcroft			}
1104773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1105773647a0SAndy Whitcroft				$in_comment = 1;
1106773647a0SAndy Whitcroft			}
1107773647a0SAndy Whitcroft
1108773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1109773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1110773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1111773647a0SAndy Whitcroft			if (!defined $edge &&
1112773647a0SAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
1113773647a0SAndy Whitcroft			{
1114773647a0SAndy Whitcroft				$in_comment = 1;
1115773647a0SAndy Whitcroft			}
1116773647a0SAndy Whitcroft
1117773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1118773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1119773647a0SAndy Whitcroft
1120171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1121773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1122171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1123773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1124773647a0SAndy Whitcroft		}
1125773647a0SAndy Whitcroft		push(@lines, $line);
1126773647a0SAndy Whitcroft
1127773647a0SAndy Whitcroft		if ($realcnt > 1) {
1128773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1129773647a0SAndy Whitcroft		} else {
1130773647a0SAndy Whitcroft			$realcnt = 0;
1131773647a0SAndy Whitcroft		}
1132773647a0SAndy Whitcroft
1133773647a0SAndy Whitcroft		#print "==>$rawline\n";
1134773647a0SAndy Whitcroft		#print "-->$line\n";
1135de7d4f0eSAndy Whitcroft
1136de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1137de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1138de7d4f0eSAndy Whitcroft		}
1139de7d4f0eSAndy Whitcroft	}
1140de7d4f0eSAndy Whitcroft
11416c72ffaaSAndy Whitcroft	$prefix = '';
11426c72ffaaSAndy Whitcroft
1143773647a0SAndy Whitcroft	$realcnt = 0;
1144773647a0SAndy Whitcroft	$linenr = 0;
11450a920b5bSAndy Whitcroft	foreach my $line (@lines) {
11460a920b5bSAndy Whitcroft		$linenr++;
11470a920b5bSAndy Whitcroft
1148c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
114930670854SAndy Whitcroft		my $hunk_line = ($realcnt != 0);
11506c72ffaaSAndy Whitcroft
11510a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
11526c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
11530a920b5bSAndy Whitcroft			$is_patch = 1;
11544a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
11550a920b5bSAndy Whitcroft			$realline=$1-1;
11560a920b5bSAndy Whitcroft			if (defined $2) {
11570a920b5bSAndy Whitcroft				$realcnt=$3+1;
11580a920b5bSAndy Whitcroft			} else {
11590a920b5bSAndy Whitcroft				$realcnt=1+1;
11600a920b5bSAndy Whitcroft			}
1161c2fdda0dSAndy Whitcroft			annotate_reset();
116213214adfSAndy Whitcroft			$prev_values = 'E';
116313214adfSAndy Whitcroft
1164773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1165170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
11660a920b5bSAndy Whitcroft			next;
11670a920b5bSAndy Whitcroft
11684a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
11694a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
11704a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1171773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
11720a920b5bSAndy Whitcroft			$realline++;
1173d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
11740a920b5bSAndy Whitcroft
11754a0df2efSAndy Whitcroft			# Measure the line length and indent.
1176c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
11770a920b5bSAndy Whitcroft
11780a920b5bSAndy Whitcroft			# Track the previous line.
11790a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
11800a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1181c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1182c2fdda0dSAndy Whitcroft
1183773647a0SAndy Whitcroft			#warn "line<$line>\n";
11846c72ffaaSAndy Whitcroft
1185d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1186d8aaf121SAndy Whitcroft			$realcnt--;
11870a920b5bSAndy Whitcroft		}
11880a920b5bSAndy Whitcroft
11890a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1190773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1191773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1192773647a0SAndy Whitcroft
11936c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
11946c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1195773647a0SAndy Whitcroft
1196773647a0SAndy Whitcroft		# extract the filename as it passes
1197773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1198773647a0SAndy Whitcroft			$realfile = $1;
1199773647a0SAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
1200773647a0SAndy Whitcroft
1201c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1202773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1203773647a0SAndy Whitcroft			}
1204773647a0SAndy Whitcroft			next;
1205773647a0SAndy Whitcroft		}
1206773647a0SAndy Whitcroft
1207389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
12080a920b5bSAndy Whitcroft
1209c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1210c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1211c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
12120a920b5bSAndy Whitcroft
12136c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
12146c72ffaaSAndy Whitcroft
12150a920b5bSAndy Whitcroft#check the patch for a signoff:
1216d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
12174a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
12184a0df2efSAndy Whitcroft			$signoff++;
12190a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1220de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1221de7d4f0eSAndy Whitcroft					$herecurr);
12220a920b5bSAndy Whitcroft			}
12230a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1224773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1225de7d4f0eSAndy Whitcroft					$herecurr);
12260a920b5bSAndy Whitcroft			}
12270a920b5bSAndy Whitcroft		}
12280a920b5bSAndy Whitcroft
122900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
12308905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1231de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
12326c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1233de7d4f0eSAndy Whitcroft		}
1234de7d4f0eSAndy Whitcroft
12356ecd9674SAndy Whitcroft# Check for absolute kernel paths.
12366ecd9674SAndy Whitcroft		if ($tree) {
12376ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
12386ecd9674SAndy Whitcroft				my $file = $1;
12396ecd9674SAndy Whitcroft
12406ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
12416ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
12426ecd9674SAndy Whitcroft					#
12436ecd9674SAndy Whitcroft				} else {
12446ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
12456ecd9674SAndy Whitcroft				}
12466ecd9674SAndy Whitcroft			}
12476ecd9674SAndy Whitcroft		}
12486ecd9674SAndy Whitcroft
1249de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1250de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1251171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1252171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1253171ae1a4SAndy Whitcroft
1254171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1255171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1256171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1257171ae1a4SAndy Whitcroft
1258171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
125900df344fSAndy Whitcroft		}
12600a920b5bSAndy Whitcroft
126130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
126230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
126300df344fSAndy Whitcroft
12640a920b5bSAndy Whitcroft#trailing whitespace
12659c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1266c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
12679c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
12689c0ca6f9SAndy Whitcroft
1269c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1270c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1271de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
12720a920b5bSAndy Whitcroft		}
12735368df20SAndy Whitcroft
12745368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
12755368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
12765368df20SAndy Whitcroft
12770a920b5bSAndy Whitcroft#80 column limit
1278c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1279f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1280f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1281f4c014c0SAndy Whitcroft		    $length > 80)
1282c45dcabdSAndy Whitcroft		{
1283de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
12840a920b5bSAndy Whitcroft		}
12850a920b5bSAndy Whitcroft
12868905a67cSAndy Whitcroft# check for adding lines without a newline.
12878905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
12888905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
12898905a67cSAndy Whitcroft		}
12908905a67cSAndy Whitcroft
1291b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1292b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
12930a920b5bSAndy Whitcroft
12940a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
12950a920b5bSAndy Whitcroft# more than 8 must use tabs.
1296c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1297c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1298c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1299171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
13000a920b5bSAndy Whitcroft		}
13010a920b5bSAndy Whitcroft
1302b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1303b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1304b9ea10d6SAndy Whitcroft
1305c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1306cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1307c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1308c2fdda0dSAndy Whitcroft		}
130922f2a2efSAndy Whitcroft
13109c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1311170d3a22SAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
13129c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1313170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1314f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1315171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1316171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1317171ae1a4SAndy Whitcroft
1318171ae1a4SAndy Whitcroft			my $s = $stat;
1319171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1320cf655043SAndy Whitcroft
1321c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1322171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1323c2fdda0dSAndy Whitcroft
1324c2fdda0dSAndy Whitcroft			# Ignore functions being called
1325171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1326c2fdda0dSAndy Whitcroft
1327c45dcabdSAndy Whitcroft			# declarations always start with types
1328d2506586SAndy 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) {
1329c45dcabdSAndy Whitcroft				my $type = $1;
1330c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1331c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1332c45dcabdSAndy Whitcroft
13336c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1334a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1335c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1336c2fdda0dSAndy Whitcroft			}
13378905a67cSAndy Whitcroft
13386c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1339171ae1a4SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1340c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
13419c0ca6f9SAndy Whitcroft			}
13428905a67cSAndy Whitcroft
13438905a67cSAndy Whitcroft			# Check for any sort of function declaration.
13448905a67cSAndy Whitcroft			# int foo(something bar, other baz);
13458905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1346171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
13478905a67cSAndy Whitcroft				my ($name_len) = length($1);
13488905a67cSAndy Whitcroft
1349cf655043SAndy Whitcroft				my $ctx = $s;
1350773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
13518905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1352cf655043SAndy Whitcroft
13538905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1354c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
13558905a67cSAndy Whitcroft
1356c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
13578905a67cSAndy Whitcroft					}
13588905a67cSAndy Whitcroft				}
13598905a67cSAndy Whitcroft			}
13608905a67cSAndy Whitcroft
13619c0ca6f9SAndy Whitcroft		}
13629c0ca6f9SAndy Whitcroft
136300df344fSAndy Whitcroft#
136400df344fSAndy Whitcroft# Checks which may be anchored in the context.
136500df344fSAndy Whitcroft#
136600df344fSAndy Whitcroft
136700df344fSAndy Whitcroft# Check for switch () and associated case and default
136800df344fSAndy Whitcroft# statements should be at the same indent.
136900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
137000df344fSAndy Whitcroft			my $err = '';
137100df344fSAndy Whitcroft			my $sep = '';
137200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
137300df344fSAndy Whitcroft			shift(@ctx);
137400df344fSAndy Whitcroft			for my $ctx (@ctx) {
137500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
137600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
137700df344fSAndy Whitcroft							$indent != $cindent) {
137800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
137900df344fSAndy Whitcroft					$sep = '';
138000df344fSAndy Whitcroft				} else {
138100df344fSAndy Whitcroft					$sep = "[...]\n";
138200df344fSAndy Whitcroft				}
138300df344fSAndy Whitcroft			}
138400df344fSAndy Whitcroft			if ($err ne '') {
13859c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1386de7d4f0eSAndy Whitcroft			}
1387de7d4f0eSAndy Whitcroft		}
1388de7d4f0eSAndy Whitcroft
1389de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1390de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1391c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1392773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1393773647a0SAndy Whitcroft
13949c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1395de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1396de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1397de7d4f0eSAndy Whitcroft
1398548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1399548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1400de7d4f0eSAndy Whitcroft
1401548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1402548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1403548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1404548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1405548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1406773647a0SAndy Whitcroft				$ctx_ln++;
1407773647a0SAndy Whitcroft			}
1408548596d5SAndy Whitcroft
140953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
141053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1411773647a0SAndy Whitcroft
1412773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1413773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1414c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
141500df344fSAndy Whitcroft			}
1416773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1417773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1418773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1419773647a0SAndy Whitcroft			{
14209c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
14219c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1422773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1423c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
14249c0ca6f9SAndy Whitcroft				}
14259c0ca6f9SAndy Whitcroft			}
142600df344fSAndy Whitcroft		}
142700df344fSAndy Whitcroft
14284d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
14294d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
14304d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
14314d001e4dSAndy Whitcroft
14324d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
14334d001e4dSAndy Whitcroft
14344d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
14354d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
14364d001e4dSAndy Whitcroft			# where necessary.
14374d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
14384d001e4dSAndy Whitcroft
14394d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
14406f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
14416f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
14424d001e4dSAndy Whitcroft
14434d001e4dSAndy Whitcroft			# We want to check the first line inside the block
14444d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
14454d001e4dSAndy Whitcroft			#  1) any blank line termination
14464d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
14474d001e4dSAndy Whitcroft			#  3) any do (...) {
14484d001e4dSAndy Whitcroft			my $continuation = 0;
14494d001e4dSAndy Whitcroft			my $check = 0;
14504d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
14514d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
14524d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
14534d001e4dSAndy Whitcroft				$continuation = 1;
14544d001e4dSAndy Whitcroft			}
14559bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
14564d001e4dSAndy Whitcroft				$check = 1;
14574d001e4dSAndy Whitcroft				$cond_lines++;
14584d001e4dSAndy Whitcroft			}
14594d001e4dSAndy Whitcroft
14604d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
14614d001e4dSAndy Whitcroft			# preprocessor statement.
14624d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
14634d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
14644d001e4dSAndy Whitcroft				$check = 0;
14654d001e4dSAndy Whitcroft			}
14664d001e4dSAndy Whitcroft
14679bd49efeSAndy Whitcroft			my $cond_ptr = -1;
14689bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
14699bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
14704d001e4dSAndy Whitcroft
1471f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1472f16fa28fSAndy Whitcroft				# is not linear.
1473f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1474f16fa28fSAndy Whitcroft					$check = 0;
1475f16fa28fSAndy Whitcroft				}
1476f16fa28fSAndy Whitcroft
14779bd49efeSAndy Whitcroft				# Ignore:
14789bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
14799bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
14809bd49efeSAndy Whitcroft				#  3) labels.
14819bd49efeSAndy Whitcroft				if ($s =~ /^\s*?\n/ ||
14829bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
14839bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
14849bd49efeSAndy Whitcroft					$s =~ s/^.*?\n//;
14859bd49efeSAndy Whitcroft					$cond_lines++;
14869bd49efeSAndy Whitcroft				}
14874d001e4dSAndy Whitcroft			}
14884d001e4dSAndy Whitcroft
14894d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
14904d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
14914d001e4dSAndy Whitcroft
14924d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
14934d001e4dSAndy Whitcroft			# this is not this patch's fault.
14944d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
14954d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
14964d001e4dSAndy Whitcroft				$check = 0;
14974d001e4dSAndy Whitcroft			}
14984d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
14994d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
15004d001e4dSAndy Whitcroft			}
15014d001e4dSAndy Whitcroft
15029bd49efeSAndy 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";
15034d001e4dSAndy Whitcroft
15044d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
15054d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
15064d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
15074d001e4dSAndy Whitcroft			}
15084d001e4dSAndy Whitcroft		}
15094d001e4dSAndy Whitcroft
15106c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
15116c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
15121f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
15131f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
15146c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1515c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1516c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1517cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1518cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
15191f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1520c2fdda0dSAndy Whitcroft		}
15216c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
15226c72ffaaSAndy Whitcroft
152300df344fSAndy Whitcroft#ignore lines not being added
152400df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
152500df344fSAndy Whitcroft
1526653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
15277429c690SAndy Whitcroft		if ($dbg_type) {
15287429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
15297429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
15307429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
15317429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
15327429c690SAndy Whitcroft			}
1533653d4876SAndy Whitcroft			next;
1534653d4876SAndy Whitcroft		}
1535a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1536a1ef277eSAndy Whitcroft		if ($dbg_attr) {
1537a1ef277eSAndy Whitcroft			if ($line =~ /^.\s*$Attribute\s*$/) {
1538a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
1539a1ef277eSAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1540a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1541a1ef277eSAndy Whitcroft			}
1542a1ef277eSAndy Whitcroft			next;
1543a1ef277eSAndy Whitcroft		}
1544653d4876SAndy Whitcroft
1545f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1546f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1547f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1548773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1549f0a594c1SAndy Whitcroft		}
1550f0a594c1SAndy Whitcroft
155100df344fSAndy Whitcroft#
155200df344fSAndy Whitcroft# Checks which are anchored on the added line.
155300df344fSAndy Whitcroft#
155400df344fSAndy Whitcroft
1555653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1556c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1557653d4876SAndy Whitcroft			my $path = $1;
1558653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1559de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1560de7d4f0eSAndy Whitcroft					$herecurr);
1561653d4876SAndy Whitcroft			}
1562653d4876SAndy Whitcroft		}
1563653d4876SAndy Whitcroft
156400df344fSAndy Whitcroft# no C99 // comments
156500df344fSAndy Whitcroft		if ($line =~ m{//}) {
1566de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
156700df344fSAndy Whitcroft		}
156800df344fSAndy Whitcroft		# Remove C99 comments.
15690a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
15706c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
15710a920b5bSAndy Whitcroft
15720a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1573653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1574653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1575653d4876SAndy Whitcroft			my $name = $1;
157648012058SAndy Whitcroft			if ($prevline !~ /(?:
157748012058SAndy Whitcroft				^.}|
157848012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
157948012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
158048012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
158148012058SAndy Whitcroft				^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
158248012058SAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
158348012058SAndy Whitcroft			    )/x) {
1584de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
15850a920b5bSAndy Whitcroft			}
15860a920b5bSAndy Whitcroft		}
15870a920b5bSAndy Whitcroft
1588f0a594c1SAndy Whitcroft# check for external initialisers.
1589c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1590f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1591f0a594c1SAndy Whitcroft				$herecurr);
1592f0a594c1SAndy Whitcroft		}
15930a920b5bSAndy Whitcroft# check for static initialisers.
15949c9ba34eSAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1595de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1596de7d4f0eSAndy Whitcroft				$herecurr);
15970a920b5bSAndy Whitcroft		}
15980a920b5bSAndy Whitcroft
1599653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1600653d4876SAndy Whitcroft# make sense.
1601653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
16029c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1603c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
16048ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1605653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1606de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
16070a920b5bSAndy Whitcroft		}
16080a920b5bSAndy Whitcroft
16090a920b5bSAndy Whitcroft# * goes on variable not on type
1610d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1611de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1612de7d4f0eSAndy Whitcroft				$herecurr);
1613d8aaf121SAndy Whitcroft
1614d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1615de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1616de7d4f0eSAndy Whitcroft				$herecurr);
1617d8aaf121SAndy Whitcroft
1618234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1619de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1620de7d4f0eSAndy Whitcroft				$herecurr);
1621d8aaf121SAndy Whitcroft
1622234fff65SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1623de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1624de7d4f0eSAndy Whitcroft				$herecurr);
16250a920b5bSAndy Whitcroft		}
16260a920b5bSAndy Whitcroft
16270a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
16280a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
16290a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
16300a920b5bSAndy Whitcroft# 			print "$herecurr";
16310a920b5bSAndy Whitcroft# 			$clean = 0;
16320a920b5bSAndy Whitcroft# 		}
16330a920b5bSAndy Whitcroft
16348905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1635c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
16368905a67cSAndy Whitcroft		}
16378905a67cSAndy Whitcroft
163800df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
163900df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
164000df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
164100df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
164200df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1643f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
164400df344fSAndy Whitcroft			my $ok = 0;
164500df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
164600df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
164700df344fSAndy Whitcroft				# we have a preceeding printk if it ends
164800df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
164900df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
165000df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
165100df344fSAndy Whitcroft						$ok = 1;
165200df344fSAndy Whitcroft					}
165300df344fSAndy Whitcroft					last;
165400df344fSAndy Whitcroft				}
165500df344fSAndy Whitcroft			}
165600df344fSAndy Whitcroft			if ($ok == 0) {
1657de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
16580a920b5bSAndy Whitcroft			}
165900df344fSAndy Whitcroft		}
16600a920b5bSAndy Whitcroft
1661653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1662653d4876SAndy Whitcroft# or if closed on same line
1663c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1664c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1665de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
16660a920b5bSAndy Whitcroft		}
1667653d4876SAndy Whitcroft
16688905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
16698905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
16708905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
16718905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
16728905a67cSAndy Whitcroft		}
16738905a67cSAndy Whitcroft
16748d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
16758d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1676fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1677fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
16788d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
16798d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
16808d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1681fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1682fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
16838d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
16848d31cfceSAndy Whitcroft			}
16858d31cfceSAndy Whitcroft		}
16868d31cfceSAndy Whitcroft
1687f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
16886c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1689c2fdda0dSAndy Whitcroft			my $name = $1;
1690773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1691773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1692c2fdda0dSAndy Whitcroft
1693c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1694773647a0SAndy Whitcroft			if ($name =~ /^(?:
1695773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1696773647a0SAndy Whitcroft				volatile|__volatile__|
1697773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1698773647a0SAndy Whitcroft				asm|__asm__)$/x)
1699773647a0SAndy Whitcroft			{
1700c2fdda0dSAndy Whitcroft
1701c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1702c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1703c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1704c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1705773647a0SAndy Whitcroft
1706773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1707c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1708c2fdda0dSAndy Whitcroft
1709c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1710c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1711773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1712c2fdda0dSAndy Whitcroft
1713c2fdda0dSAndy Whitcroft			} else {
1714773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1715f0a594c1SAndy Whitcroft			}
17166c72ffaaSAndy Whitcroft		}
1717653d4876SAndy Whitcroft# Check operator spacing.
17180a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
17199c0ca6f9SAndy Whitcroft			my $ops = qr{
17209c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
17219c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
17229c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
17231f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
17241f65f947SAndy Whitcroft				\?|:
17259c0ca6f9SAndy Whitcroft			}x;
1726cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
172700df344fSAndy Whitcroft			my $off = 0;
17286c72ffaaSAndy Whitcroft
17296c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
17306c72ffaaSAndy Whitcroft
17310a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
17324a0df2efSAndy Whitcroft				$off += length($elements[$n]);
17334a0df2efSAndy Whitcroft
1734773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1735773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1736773647a0SAndy Whitcroft				my $cc = '';
1737773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1738773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1739773647a0SAndy Whitcroft				}
1740773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1741773647a0SAndy Whitcroft
17424a0df2efSAndy Whitcroft				my $a = '';
17434a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
17444a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1745cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
17464a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
17474a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1748773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
17494a0df2efSAndy Whitcroft
17500a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
17514a0df2efSAndy Whitcroft
17524a0df2efSAndy Whitcroft				my $c = '';
17530a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
17544a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
17554a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1756cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
17574a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
17584a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
175922f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
17604a0df2efSAndy Whitcroft				} else {
17614a0df2efSAndy Whitcroft					$c = 'E';
17620a920b5bSAndy Whitcroft				}
17630a920b5bSAndy Whitcroft
17644a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
17654a0df2efSAndy Whitcroft
17664a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
17674a0df2efSAndy Whitcroft
17686c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1769de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
17700a920b5bSAndy Whitcroft
177174048ed8SAndy Whitcroft				# Pull out the value of this operator.
17726c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
17730a920b5bSAndy Whitcroft
17741f65f947SAndy Whitcroft				# Get the full operator variant.
17751f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
17761f65f947SAndy Whitcroft
177713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
177813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
177913214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
178013214adfSAndy Whitcroft
1781cf655043SAndy Whitcroft#				# Ignore comments
1782cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
178313214adfSAndy Whitcroft
1784d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
178513214adfSAndy Whitcroft				} elsif ($op eq ';') {
1786cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1787cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1788773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1789d8aaf121SAndy Whitcroft					}
1790d8aaf121SAndy Whitcroft
1791d8aaf121SAndy Whitcroft				# // is a comment
1792d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
17930a920b5bSAndy Whitcroft
17941f65f947SAndy Whitcroft				# No spaces for:
17951f65f947SAndy Whitcroft				#   ->
17961f65f947SAndy Whitcroft				#   :   when part of a bitfield
17971f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
17984a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1799773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
18000a920b5bSAndy Whitcroft					}
18010a920b5bSAndy Whitcroft
18020a920b5bSAndy Whitcroft				# , must have a space on the right.
18030a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1804cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1805773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
18060a920b5bSAndy Whitcroft					}
18070a920b5bSAndy Whitcroft
18089c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
180974048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
18109c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
18119c0ca6f9SAndy Whitcroft
18129c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
18139c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
18149c0ca6f9SAndy Whitcroft				# unary operator, or a cast
18159c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
181674048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
18170d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1818cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1819773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
18200a920b5bSAndy Whitcroft					}
1821171ae1a4SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*const\b/) {
1822171ae1a4SAndy Whitcroft						# A unary '*' may be const
1823171ae1a4SAndy Whitcroft
1824171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1825773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
18260a920b5bSAndy Whitcroft					}
18270a920b5bSAndy Whitcroft
18280a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
18290a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1830773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1831773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
18320a920b5bSAndy Whitcroft					}
1833773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1834773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1835773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1836653d4876SAndy Whitcroft					}
1837773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1838773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1839773647a0SAndy Whitcroft					}
1840773647a0SAndy Whitcroft
18410a920b5bSAndy Whitcroft
18420a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
18439c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
18449c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
18459c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1846c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1847c2fdda0dSAndy Whitcroft					 $op eq '%')
18480a920b5bSAndy Whitcroft				{
1849773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1850de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1851de7d4f0eSAndy Whitcroft							$hereptr);
18520a920b5bSAndy Whitcroft					}
18530a920b5bSAndy Whitcroft
18541f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
18551f65f947SAndy Whitcroft				# terminating a case value or a label.
18561f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
18571f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
18581f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
18591f65f947SAndy Whitcroft					}
18601f65f947SAndy Whitcroft
18610a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1862cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
18631f65f947SAndy Whitcroft					my $ok = 0;
18641f65f947SAndy Whitcroft
186522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
18661f65f947SAndy Whitcroft					if (($op eq '<' &&
18671f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
18681f65f947SAndy Whitcroft					    ($op eq '>' &&
18691f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
18701f65f947SAndy Whitcroft					{
18711f65f947SAndy Whitcroft					    	$ok = 1;
18721f65f947SAndy Whitcroft					}
18731f65f947SAndy Whitcroft
18741f65f947SAndy Whitcroft					# Ignore ?:
18751f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
18761f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
18771f65f947SAndy Whitcroft					    	$ok = 1;
18781f65f947SAndy Whitcroft					}
18791f65f947SAndy Whitcroft
18801f65f947SAndy Whitcroft					if ($ok == 0) {
1881773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
18820a920b5bSAndy Whitcroft					}
188322f2a2efSAndy Whitcroft				}
18844a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
18850a920b5bSAndy Whitcroft			}
18860a920b5bSAndy Whitcroft		}
18870a920b5bSAndy Whitcroft
1888f0a594c1SAndy Whitcroft# check for multiple assignments
1889f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
18906c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1891f0a594c1SAndy Whitcroft		}
1892f0a594c1SAndy Whitcroft
189322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
189422f2a2efSAndy Whitcroft## # continuation.
189522f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
189622f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
189722f2a2efSAndy Whitcroft##
189822f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
189922f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
190022f2a2efSAndy Whitcroft## 			my $ln = $line;
190122f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
190222f2a2efSAndy Whitcroft## 			}
190322f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
190422f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
190522f2a2efSAndy Whitcroft## 			}
190622f2a2efSAndy Whitcroft## 		}
1907f0a594c1SAndy Whitcroft
19080a920b5bSAndy Whitcroft#need space before brace following if, while, etc
190922f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
191022f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1911773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1912de7d4f0eSAndy Whitcroft		}
1913de7d4f0eSAndy Whitcroft
1914de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1915de7d4f0eSAndy Whitcroft# on the line
1916de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1917773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
19180a920b5bSAndy Whitcroft		}
19190a920b5bSAndy Whitcroft
192022f2a2efSAndy Whitcroft# check spacing on square brackets
192122f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1922773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
192322f2a2efSAndy Whitcroft		}
192422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1925773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
192622f2a2efSAndy Whitcroft		}
192722f2a2efSAndy Whitcroft
1928c45dcabdSAndy Whitcroft# check spacing on parentheses
19299c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
19309c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1931773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
193222f2a2efSAndy Whitcroft		}
193313214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1934c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
1935c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
1936773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
193722f2a2efSAndy Whitcroft		}
193822f2a2efSAndy Whitcroft
19390a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
19404a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
19410a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1942de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
19430a920b5bSAndy Whitcroft		}
19440a920b5bSAndy Whitcroft
1945c45dcabdSAndy Whitcroft# Return is not a function.
1946c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1947c45dcabdSAndy Whitcroft			my $spacing = $1;
1948c45dcabdSAndy Whitcroft			my $value = $2;
1949c45dcabdSAndy Whitcroft
1950c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
1951fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
1952c45dcabdSAndy Whitcroft			while ($value =~ s/\([^\(\)]*\)/1/) {
1953c45dcabdSAndy Whitcroft			}
1954c45dcabdSAndy Whitcroft
1955c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1956c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1957c45dcabdSAndy Whitcroft
1958c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
1959c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
1960c45dcabdSAndy Whitcroft			}
1961c45dcabdSAndy Whitcroft		}
1962c45dcabdSAndy Whitcroft
19630a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
19644a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1965773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
19660a920b5bSAndy Whitcroft		}
19670a920b5bSAndy Whitcroft
1968f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
1969f5fe35ddSAndy Whitcroft# statements after the conditional.
1970170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
1971170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
1972170d3a22SAndy Whitcroft						$remain_next, $off_next);
1973170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
1974170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
1975170d3a22SAndy Whitcroft
1976170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
1977170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
1978170d3a22SAndy Whitcroft				# then count those as offsets.
1979170d3a22SAndy Whitcroft				my ($whitespace) =
1980170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
1981170d3a22SAndy Whitcroft				my $offset =
1982170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
1983170d3a22SAndy Whitcroft
1984170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
1985170d3a22SAndy Whitcroft								$offset} = 1;
1986170d3a22SAndy Whitcroft			}
1987170d3a22SAndy Whitcroft		}
1988170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
1989170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
1990171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
19918905a67cSAndy Whitcroft
19928905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1993c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
19948905a67cSAndy Whitcroft			}
19958905a67cSAndy Whitcroft
19968905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
19978905a67cSAndy Whitcroft			# conditional.
1998773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
19998905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
200013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
200153210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
200253210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2003773647a0SAndy Whitcroft			{
2004*bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2005*bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2006*bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
2007*bb44ad39SAndy Whitcroft
2008*bb44ad39SAndy Whitcroft				my $stat_real = raw_line($linenr, $cond_lines);
2009*bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2010*bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2011*bb44ad39SAndy Whitcroft				}
2012*bb44ad39SAndy Whitcroft
2013*bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
20148905a67cSAndy Whitcroft			}
20158905a67cSAndy Whitcroft		}
20168905a67cSAndy Whitcroft
201713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
201813214adfSAndy Whitcroft		if ($line =~ /
201913214adfSAndy Whitcroft			(?:
202013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
202113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
202213214adfSAndy Whitcroft				(?:\&\&|\|\|)
202313214adfSAndy Whitcroft			|
202413214adfSAndy Whitcroft				(?:\&\&|\|\|)
202513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
202613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
202713214adfSAndy Whitcroft			)/x)
202813214adfSAndy Whitcroft		{
202913214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
203013214adfSAndy Whitcroft		}
203113214adfSAndy Whitcroft
20328905a67cSAndy Whitcroft# if and else should not have general statements after it
203313214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
203413214adfSAndy Whitcroft			my $s = $1;
203513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
203613214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
20378905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
20380a920b5bSAndy Whitcroft			}
203913214adfSAndy Whitcroft		}
2040a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2041a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2042a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
2043a1080bf8SAndy Whitcroft			(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2044a1080bf8SAndy Whitcroft			\s*return\s+
2045a1080bf8SAndy Whitcroft		    )/xg)
2046a1080bf8SAndy Whitcroft		{
2047a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2048a1080bf8SAndy Whitcroft		}
20490a920b5bSAndy Whitcroft
20500a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
20510a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
20520a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
20530a920b5bSAndy Whitcroft						$previndent == $indent) {
2054de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
20550a920b5bSAndy Whitcroft		}
20560a920b5bSAndy Whitcroft
2057c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2058c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2059c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2060c2fdda0dSAndy Whitcroft
2061c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2062c2fdda0dSAndy Whitcroft			# conditional.
2063773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2064c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2065c2fdda0dSAndy Whitcroft
2066c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2067c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2068c2fdda0dSAndy Whitcroft			}
2069c2fdda0dSAndy Whitcroft		}
2070c2fdda0dSAndy Whitcroft
20710a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
20720a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
20730a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
20740a920b5bSAndy Whitcroft#		    print "$herecurr";
20750a920b5bSAndy Whitcroft#		    $clean = 0;
20760a920b5bSAndy Whitcroft#		}
20770a920b5bSAndy Whitcroft
20780a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2079c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2080de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
20810a920b5bSAndy Whitcroft		}
20820a920b5bSAndy Whitcroft
2083653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2084c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2085e09dec48SAndy Whitcroft			my $file = "$1.h";
2086e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2087e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2088e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2089c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2090c45dcabdSAndy Whitcroft			{
2091e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2092e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2093e09dec48SAndy Whitcroft				} else {
2094e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2095e09dec48SAndy Whitcroft				}
20960a920b5bSAndy Whitcroft			}
20970a920b5bSAndy Whitcroft		}
20980a920b5bSAndy Whitcroft
2099653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2100653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2101cf655043SAndy Whitcroft# in a known good container
2102b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2103b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2104d8aaf121SAndy Whitcroft			my $ln = $linenr;
2105d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2106c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2107c45dcabdSAndy Whitcroft			my $ctx = '';
2108653d4876SAndy Whitcroft
2109c45dcabdSAndy Whitcroft			my $args = defined($1);
2110de7d4f0eSAndy Whitcroft
2111c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2112c45dcabdSAndy Whitcroft			# search to that.
2113c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2114c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2115c45dcabdSAndy Whitcroft			{
2116c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2117a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2118c45dcabdSAndy Whitcroft				$ln++;
2119de7d4f0eSAndy Whitcroft			}
2120c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2121d8aaf121SAndy Whitcroft
2122c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2123c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2124c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2125a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2126c45dcabdSAndy Whitcroft
2127c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2128c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2129c45dcabdSAndy Whitcroft			$rest = '';
2130636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2131636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2132a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2133c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2134c45dcabdSAndy Whitcroft					$cnt--;
2135a3bb97a7SAndy Whitcroft				}
2136a3bb97a7SAndy Whitcroft				$ln++;
2137c45dcabdSAndy Whitcroft				$off = 0;
2138c45dcabdSAndy Whitcroft			}
2139c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2140c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2141c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2142c45dcabdSAndy Whitcroft
2143c45dcabdSAndy Whitcroft			# Clean up the original statement.
2144c45dcabdSAndy Whitcroft			if ($args) {
2145c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2146d8aaf121SAndy Whitcroft			} else {
2147c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2148c45dcabdSAndy Whitcroft			}
2149292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2150c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2151c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2152c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2153c45dcabdSAndy Whitcroft
2154c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2155bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2156bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2157bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2158bf30d6edSAndy Whitcroft			{
2159c45dcabdSAndy Whitcroft			}
2160c45dcabdSAndy Whitcroft
2161c45dcabdSAndy Whitcroft			my $exceptions = qr{
2162c45dcabdSAndy Whitcroft				$Declare|
2163c45dcabdSAndy Whitcroft				module_param_named|
2164c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2165c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2166c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2167c45dcabdSAndy Whitcroft				__typeof__\(
2168c45dcabdSAndy Whitcroft			}x;
2169a3bb97a7SAndy Whitcroft			#print "REST<$rest>\n";
2170c45dcabdSAndy Whitcroft			if ($rest ne '') {
2171c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2172c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2173c45dcabdSAndy Whitcroft				{
2174c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2175c45dcabdSAndy Whitcroft				}
2176c45dcabdSAndy Whitcroft
2177c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2178c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2179c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2180c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2181b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2182c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2183c45dcabdSAndy Whitcroft				{
2184de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2185d8aaf121SAndy Whitcroft				}
21860a920b5bSAndy Whitcroft			}
2187653d4876SAndy Whitcroft		}
21880a920b5bSAndy Whitcroft
2189f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
219013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
219113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2192cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
219313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2194cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2195cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
219613214adfSAndy Whitcroft				my $allowed = 0;
219713214adfSAndy Whitcroft				my $seen = 0;
2198773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2199cf655043SAndy Whitcroft				my $ln = $linenr - 1;
220013214adfSAndy Whitcroft				for my $chunk (@chunks) {
220113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
220213214adfSAndy Whitcroft
2203773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2204773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2205773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2206773647a0SAndy Whitcroft
2207773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2208773647a0SAndy Whitcroft
2209773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2210773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2211773647a0SAndy Whitcroft
2212773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2213cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2214cf655043SAndy Whitcroft
2215773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
221613214adfSAndy Whitcroft
221713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
221813214adfSAndy Whitcroft
2219cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2220cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2221cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
222213214adfSAndy Whitcroft						$allowed = 1;
222313214adfSAndy Whitcroft					}
222413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2225cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
222613214adfSAndy Whitcroft						$allowed = 1;
222713214adfSAndy Whitcroft					}
2228cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2229cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
223013214adfSAndy Whitcroft						$allowed = 1;
223113214adfSAndy Whitcroft					}
223213214adfSAndy Whitcroft				}
223313214adfSAndy Whitcroft				if ($seen && !$allowed) {
2234cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
223513214adfSAndy Whitcroft				}
223613214adfSAndy Whitcroft			}
223713214adfSAndy Whitcroft		}
2238773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
223913214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2240cf655043SAndy Whitcroft			my $allowed = 0;
2241f0a594c1SAndy Whitcroft
2242cf655043SAndy Whitcroft			# Check the pre-context.
2243cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2244cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2245cf655043SAndy Whitcroft				$allowed = 1;
2246f0a594c1SAndy Whitcroft			}
2247773647a0SAndy Whitcroft
2248773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2249773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2250773647a0SAndy Whitcroft
2251cf655043SAndy Whitcroft			# Check the condition.
2252cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2253773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2254cf655043SAndy Whitcroft			if (defined $cond) {
2255773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2256cf655043SAndy Whitcroft			}
2257cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2258cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2259cf655043SAndy Whitcroft				$allowed = 1;
2260cf655043SAndy Whitcroft			}
2261cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2262cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2263cf655043SAndy Whitcroft				$allowed = 1;
2264cf655043SAndy Whitcroft			}
2265cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2266cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2267cf655043SAndy Whitcroft				$allowed = 1;
2268cf655043SAndy Whitcroft			}
2269cf655043SAndy Whitcroft			# Check the post-context.
2270cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2271cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2272cf655043SAndy Whitcroft				if (defined $cond) {
2273773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2274cf655043SAndy Whitcroft				}
2275cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2276cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2277cf655043SAndy Whitcroft					$allowed = 1;
2278cf655043SAndy Whitcroft				}
2279cf655043SAndy Whitcroft			}
2280cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2281cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2282f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2283cf655043SAndy Whitcroft
2284f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2285f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2286cf655043SAndy Whitcroft				}
2287cf655043SAndy Whitcroft
2288cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2289f0a594c1SAndy Whitcroft			}
2290f0a594c1SAndy Whitcroft		}
2291f0a594c1SAndy Whitcroft
2292653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
22934a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2294c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2295de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
22960a920b5bSAndy Whitcroft			}
22970a920b5bSAndy Whitcroft		}
22980a920b5bSAndy Whitcroft
22994a0df2efSAndy Whitcroft# don't use deprecated functions
23004a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
230100df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2302de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
23034a0df2efSAndy Whitcroft			}
23044a0df2efSAndy Whitcroft		}
23054a0df2efSAndy Whitcroft
23064a0df2efSAndy Whitcroft# no volatiles please
23076c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
23086c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2309de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
23104a0df2efSAndy Whitcroft		}
23114a0df2efSAndy Whitcroft
23129c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
23139c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
23149c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
23159c0ca6f9SAndy Whitcroft		}
23169c0ca6f9SAndy Whitcroft
231700df344fSAndy Whitcroft# warn about #if 0
2318c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2319de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2320de7d4f0eSAndy Whitcroft				$herecurr);
23214a0df2efSAndy Whitcroft		}
23224a0df2efSAndy Whitcroft
2323f0a594c1SAndy Whitcroft# check for needless kfree() checks
2324f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2325f0a594c1SAndy Whitcroft			my $expr = $1;
2326f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
23273c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2328f0a594c1SAndy Whitcroft			}
2329f0a594c1SAndy Whitcroft		}
23304c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
23314c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
23324c432a8fSGreg Kroah-Hartman			my $expr = $1;
23334c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
23344c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
23354c432a8fSGreg Kroah-Hartman			}
23364c432a8fSGreg Kroah-Hartman		}
2337f0a594c1SAndy Whitcroft
233800df344fSAndy Whitcroft# warn about #ifdefs in C files
2339c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
234000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
234100df344fSAndy Whitcroft#			print "$herecurr";
234200df344fSAndy Whitcroft#			$clean = 0;
234300df344fSAndy Whitcroft#		}
234400df344fSAndy Whitcroft
234522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2346c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
234722f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
234822f2a2efSAndy Whitcroft		}
234922f2a2efSAndy Whitcroft
23504a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2351171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2352171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
23534a0df2efSAndy Whitcroft			my $which = $1;
23544a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2355de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
23564a0df2efSAndy Whitcroft			}
23574a0df2efSAndy Whitcroft		}
23584a0df2efSAndy Whitcroft# check for memory barriers without a comment.
23594a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
23604a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2361de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
23624a0df2efSAndy Whitcroft			}
23634a0df2efSAndy Whitcroft		}
23644a0df2efSAndy Whitcroft# check of hardware specific defines
2365c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2366de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
23670a920b5bSAndy Whitcroft		}
2368653d4876SAndy Whitcroft
2369de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2370de7d4f0eSAndy Whitcroft# storage class and type.
23719c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
23729c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2373de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2374de7d4f0eSAndy Whitcroft		}
2375de7d4f0eSAndy Whitcroft
23768905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
23778905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
23788905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
23798905a67cSAndy Whitcroft		}
23808905a67cSAndy Whitcroft
2381de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2382171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2383c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2384171ae1a4SAndy Whitcroft		{
2385c45dcabdSAndy Whitcroft			my $function_name = $1;
2386c45dcabdSAndy Whitcroft			my $paren_space = $2;
2387171ae1a4SAndy Whitcroft
2388171ae1a4SAndy Whitcroft			my $s = $stat;
2389171ae1a4SAndy Whitcroft			if (defined $cond) {
2390171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2391171ae1a4SAndy Whitcroft			}
2392c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2393c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2394c45dcabdSAndy Whitcroft			{
2395de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2396de7d4f0eSAndy Whitcroft			}
2397de7d4f0eSAndy Whitcroft
2398171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2399171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2400171ae1a4SAndy Whitcroft			}
24019c9ba34eSAndy Whitcroft
24029c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
24039c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
24049c9ba34eSAndy Whitcroft		{
24059c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2406171ae1a4SAndy Whitcroft		}
2407171ae1a4SAndy Whitcroft
2408de7d4f0eSAndy Whitcroft# checks for new __setup's
2409de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2410de7d4f0eSAndy Whitcroft			my $name = $1;
2411de7d4f0eSAndy Whitcroft
2412de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2413de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2414de7d4f0eSAndy Whitcroft			}
2415653d4876SAndy Whitcroft		}
24169c0ca6f9SAndy Whitcroft
24179c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
24189c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
24199c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
24209c0ca6f9SAndy Whitcroft		}
242113214adfSAndy Whitcroft
242213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
242313214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
242413214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
242513214adfSAndy Whitcroft		}
2426773647a0SAndy Whitcroft
2427773647a0SAndy Whitcroft# check for semaphores used as mutexes
2428171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2429773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2430773647a0SAndy Whitcroft		}
2431773647a0SAndy Whitcroft# check for semaphores used as mutexes
2432171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2433773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2434773647a0SAndy Whitcroft		}
2435773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2436773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2437773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2438773647a0SAndy Whitcroft		}
2439f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2440f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2441f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2442f3db6639SMichael Ellerman		}
2443773647a0SAndy Whitcroft
2444773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2445773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2446773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2447c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2448c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2449171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2450171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2451171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2452773647a0SAndy Whitcroft		{
2453773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2454773647a0SAndy Whitcroft		}
24559c9ba34eSAndy Whitcroft
24569c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
24579c9ba34eSAndy Whitcroft		my $string;
24589c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
24599c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
24602a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
24619c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
24629c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
24639c9ba34eSAndy Whitcroft				last;
24649c9ba34eSAndy Whitcroft			}
24659c9ba34eSAndy Whitcroft		}
246613214adfSAndy Whitcroft	}
246713214adfSAndy Whitcroft
246813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
246913214adfSAndy Whitcroft	# so just keep quiet.
247013214adfSAndy Whitcroft	if ($#rawlines == -1) {
247113214adfSAndy Whitcroft		exit(0);
24720a920b5bSAndy Whitcroft	}
24730a920b5bSAndy Whitcroft
24748905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
24758905a67cSAndy Whitcroft	# things that appear to be patches.
24768905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
24778905a67cSAndy Whitcroft		exit(0);
24788905a67cSAndy Whitcroft	}
24798905a67cSAndy Whitcroft
24808905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
24818905a67cSAndy Whitcroft	# just keep quiet.
24828905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
24838905a67cSAndy Whitcroft		exit(0);
24848905a67cSAndy Whitcroft	}
24858905a67cSAndy Whitcroft
24868905a67cSAndy Whitcroft	if (!$is_patch) {
2487de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
24880a920b5bSAndy Whitcroft	}
24890a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2490de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
24910a920b5bSAndy Whitcroft	}
24920a920b5bSAndy Whitcroft
2493f0a594c1SAndy Whitcroft	print report_dump();
249413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
249513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
24966c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
24976c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
24986c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
24998905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
25006c72ffaaSAndy Whitcroft	}
25018905a67cSAndy Whitcroft
25020a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2503c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
25040a920b5bSAndy Whitcroft	}
25050a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2506c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
25070a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
25080a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
25090a920b5bSAndy Whitcroft	}
251013214adfSAndy Whitcroft
25110a920b5bSAndy Whitcroft	return $clean;
25120a920b5bSAndy Whitcroft}
2513