xref: /linux-6.15/scripts/checkpatch.pl (revision cf655043)
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
12*cf655043SAndy Whitcroftmy $V = '0.15';
130a920b5bSAndy Whitcroft
140a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftmy $quiet = 0;
170a920b5bSAndy Whitcroftmy $tree = 1;
180a920b5bSAndy Whitcroftmy $chk_signoff = 1;
190a920b5bSAndy Whitcroftmy $chk_patch = 1;
20653d4876SAndy Whitcroftmy $tst_type = 0;
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,
4613214adfSAndy Whitcroft	'test-type!'	=> \$tst_type,
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;
68c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
69c2fdda0dSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';"
70c2fdda0dSAndy Whitcroft}
71c2fdda0dSAndy Whitcroft
728905a67cSAndy Whitcroftif ($terse) {
738905a67cSAndy Whitcroft	$emacs = 1;
748905a67cSAndy Whitcroft	$quiet++;
758905a67cSAndy Whitcroft}
768905a67cSAndy Whitcroft
776c72ffaaSAndy Whitcroftif ($tree) {
786c72ffaaSAndy Whitcroft	if (defined $root) {
796c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
806c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
816c72ffaaSAndy Whitcroft		}
826c72ffaaSAndy Whitcroft	} else {
836c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
846c72ffaaSAndy Whitcroft			$root = '.';
856c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
866c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
876c72ffaaSAndy Whitcroft			$root = $1;
886c72ffaaSAndy Whitcroft		}
896c72ffaaSAndy Whitcroft	}
906c72ffaaSAndy Whitcroft
916c72ffaaSAndy Whitcroft	if (!defined $root) {
920a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
930a920b5bSAndy Whitcroft		exit(2);
940a920b5bSAndy Whitcroft	}
956c72ffaaSAndy Whitcroft}
966c72ffaaSAndy Whitcroft
976c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
986c72ffaaSAndy Whitcroft
996c72ffaaSAndy Whitcroftour $Ident       = qr{[A-Za-z_][A-Za-z\d_]*};
1006c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1016c72ffaaSAndy Whitcroftour $Sparse	= qr{
1026c72ffaaSAndy Whitcroft			__user|
1036c72ffaaSAndy Whitcroft			__kernel|
1046c72ffaaSAndy Whitcroft			__force|
1056c72ffaaSAndy Whitcroft			__iomem|
1066c72ffaaSAndy Whitcroft			__must_check|
1076c72ffaaSAndy Whitcroft			__init_refok|
108*cf655043SAndy Whitcroft			__kprobes
1096c72ffaaSAndy Whitcroft		}x;
1106c72ffaaSAndy Whitcroftour $Attribute	= qr{
1116c72ffaaSAndy Whitcroft			const|
1126c72ffaaSAndy Whitcroft			__read_mostly|
1136c72ffaaSAndy Whitcroft			__kprobes|
1146c72ffaaSAndy Whitcroft			__(?:mem|cpu|dev|)(?:initdata|init)
1156c72ffaaSAndy Whitcroft		  }x;
1166c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1176c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1186c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1196c72ffaaSAndy Whitcroft
1206c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1216c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
1226c72ffaaSAndy Whitcroftour $Operators	= qr{
1236c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1246c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
125c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1266c72ffaaSAndy Whitcroft		  }x;
1276c72ffaaSAndy Whitcroft
1288905a67cSAndy Whitcroftour $NonptrType;
1298905a67cSAndy Whitcroftour $Type;
1308905a67cSAndy Whitcroftour $Declare;
1318905a67cSAndy Whitcroft
1328905a67cSAndy Whitcroftour @typeList = (
1338905a67cSAndy Whitcroft	qr{void},
1348905a67cSAndy Whitcroft	qr{char},
1358905a67cSAndy Whitcroft	qr{short},
1368905a67cSAndy Whitcroft	qr{int},
1378905a67cSAndy Whitcroft	qr{long},
1388905a67cSAndy Whitcroft	qr{unsigned},
1398905a67cSAndy Whitcroft	qr{float},
1408905a67cSAndy Whitcroft	qr{double},
1418905a67cSAndy Whitcroft	qr{bool},
1428905a67cSAndy Whitcroft	qr{long\s+int},
1438905a67cSAndy Whitcroft	qr{long\s+long},
1448905a67cSAndy Whitcroft	qr{long\s+long\s+int},
1458905a67cSAndy Whitcroft	qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
1468905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1478905a67cSAndy Whitcroft	qr{union\s+$Ident},
1488905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1498905a67cSAndy Whitcroft	qr{${Ident}_t},
1508905a67cSAndy Whitcroft	qr{${Ident}_handler},
1518905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1528905a67cSAndy Whitcroft);
1538905a67cSAndy Whitcroft
1548905a67cSAndy Whitcroftsub build_types {
1558905a67cSAndy Whitcroft	my $all = "(?:  \n" . join("|\n  ", @typeList) . "\n)";
1568905a67cSAndy Whitcroft	$NonptrType	= qr{
1578905a67cSAndy Whitcroft			\b
1588905a67cSAndy Whitcroft			(?:const\s+)?
1598905a67cSAndy Whitcroft			(?:unsigned\s+)?
160*cf655043SAndy Whitcroft			(?:
161*cf655043SAndy Whitcroft				$all|
162*cf655043SAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
163*cf655043SAndy Whitcroft			)
1648905a67cSAndy Whitcroft			(?:\s+$Sparse|\s+const)*
1658905a67cSAndy Whitcroft			\b
1668905a67cSAndy Whitcroft		  }x;
1678905a67cSAndy Whitcroft	$Type	= qr{
1688905a67cSAndy Whitcroft			\b$NonptrType\b
1698905a67cSAndy Whitcroft			(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
170c2fdda0dSAndy Whitcroft			(?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
1718905a67cSAndy Whitcroft		  }x;
1728905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
1738905a67cSAndy Whitcroft}
1748905a67cSAndy Whitcroftbuild_types();
1756c72ffaaSAndy Whitcroft
1766c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
1770a920b5bSAndy Whitcroft
1784a0df2efSAndy Whitcroftmy @dep_includes = ();
1794a0df2efSAndy Whitcroftmy @dep_functions = ();
1806c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
1816c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
1826c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
1836c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
1840a920b5bSAndy Whitcroft	while (<REMOVE>) {
185f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
186f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
187f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
1884a0df2efSAndy Whitcroft					push(@dep_includes, $1);
1894a0df2efSAndy Whitcroft
190f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
191f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
192f0a594c1SAndy Whitcroft				}
1934a0df2efSAndy Whitcroft			}
1940a920b5bSAndy Whitcroft		}
1950a920b5bSAndy Whitcroft	}
1960a920b5bSAndy Whitcroft}
1970a920b5bSAndy Whitcroft
19800df344fSAndy Whitcroftmy @rawlines = ();
199c2fdda0dSAndy Whitcroftmy @lines = ();
200c2fdda0dSAndy Whitcroftmy $vname;
2016c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2026c72ffaaSAndy Whitcroft	if ($file) {
2036c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2046c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2056c72ffaaSAndy Whitcroft	} else {
2066c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2076c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2086c72ffaaSAndy Whitcroft	}
209c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
210c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
211c2fdda0dSAndy Whitcroft	} else {
212c2fdda0dSAndy Whitcroft		$vname = $filename;
213c2fdda0dSAndy Whitcroft	}
2146c72ffaaSAndy Whitcroft	while (<FILE>) {
2150a920b5bSAndy Whitcroft		chomp;
21600df344fSAndy Whitcroft		push(@rawlines, $_);
2176c72ffaaSAndy Whitcroft	}
2186c72ffaaSAndy Whitcroft	close(FILE);
219c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2200a920b5bSAndy Whitcroft		$exit = 1;
2210a920b5bSAndy Whitcroft	}
22200df344fSAndy Whitcroft	@rawlines = ();
22313214adfSAndy Whitcroft	@lines = ();
2240a920b5bSAndy Whitcroft}
2250a920b5bSAndy Whitcroft
2260a920b5bSAndy Whitcroftexit($exit);
2270a920b5bSAndy Whitcroft
2280a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2296c72ffaaSAndy Whitcroft	my ($root) = @_;
2306c72ffaaSAndy Whitcroft
2316c72ffaaSAndy Whitcroft	my @tree_check = (
2326c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2336c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2346c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2356c72ffaaSAndy Whitcroft	);
2366c72ffaaSAndy Whitcroft
2376c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2386c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2390a920b5bSAndy Whitcroft			return 0;
2400a920b5bSAndy Whitcroft		}
2416c72ffaaSAndy Whitcroft	}
2426c72ffaaSAndy Whitcroft	return 1;
2436c72ffaaSAndy Whitcroft}
2440a920b5bSAndy Whitcroft
2450a920b5bSAndy Whitcroftsub expand_tabs {
2460a920b5bSAndy Whitcroft	my ($str) = @_;
2470a920b5bSAndy Whitcroft
2480a920b5bSAndy Whitcroft	my $res = '';
2490a920b5bSAndy Whitcroft	my $n = 0;
2500a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2510a920b5bSAndy Whitcroft		if ($c eq "\t") {
2520a920b5bSAndy Whitcroft			$res .= ' ';
2530a920b5bSAndy Whitcroft			$n++;
2540a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2550a920b5bSAndy Whitcroft				$res .= ' ';
2560a920b5bSAndy Whitcroft			}
2570a920b5bSAndy Whitcroft			next;
2580a920b5bSAndy Whitcroft		}
2590a920b5bSAndy Whitcroft		$res .= $c;
2600a920b5bSAndy Whitcroft		$n++;
2610a920b5bSAndy Whitcroft	}
2620a920b5bSAndy Whitcroft
2630a920b5bSAndy Whitcroft	return $res;
2640a920b5bSAndy Whitcroft}
2656c72ffaaSAndy Whitcroftsub copy_spacing {
2666c72ffaaSAndy Whitcroft	my ($str) = @_;
2676c72ffaaSAndy Whitcroft
2686c72ffaaSAndy Whitcroft	my $res = '';
2696c72ffaaSAndy Whitcroft	for my $c (split(//, $str)) {
2706c72ffaaSAndy Whitcroft		if ($c eq "\t") {
2716c72ffaaSAndy Whitcroft			$res .= $c;
2726c72ffaaSAndy Whitcroft		} else {
2736c72ffaaSAndy Whitcroft			$res .= ' ';
2746c72ffaaSAndy Whitcroft		}
2756c72ffaaSAndy Whitcroft	}
2766c72ffaaSAndy Whitcroft
2776c72ffaaSAndy Whitcroft	return $res;
2786c72ffaaSAndy Whitcroft}
2790a920b5bSAndy Whitcroft
2804a0df2efSAndy Whitcroftsub line_stats {
2814a0df2efSAndy Whitcroft	my ($line) = @_;
2824a0df2efSAndy Whitcroft
2834a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
2844a0df2efSAndy Whitcroft	$line =~ s/^.//;
2854a0df2efSAndy Whitcroft	$line = expand_tabs($line);
2864a0df2efSAndy Whitcroft
2874a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
2884a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
2894a0df2efSAndy Whitcroft
2904a0df2efSAndy Whitcroft	return (length($line), length($white));
2914a0df2efSAndy Whitcroft}
2924a0df2efSAndy Whitcroft
29300df344fSAndy Whitcroftsub sanitise_line {
29400df344fSAndy Whitcroft	my ($line) = @_;
29500df344fSAndy Whitcroft
29600df344fSAndy Whitcroft	my $res = '';
29700df344fSAndy Whitcroft	my $l = '';
29800df344fSAndy Whitcroft
29900df344fSAndy Whitcroft	my $quote = '';
300c2fdda0dSAndy Whitcroft	my $qlen = 0;
30100df344fSAndy Whitcroft
30200df344fSAndy Whitcroft	foreach my $c (split(//, $line)) {
303c2fdda0dSAndy Whitcroft		# The second backslash of a pair is not a "quote".
304c2fdda0dSAndy Whitcroft		if ($l eq "\\" && $c eq "\\") {
305c2fdda0dSAndy Whitcroft			$c = 'X';
306c2fdda0dSAndy Whitcroft		}
30700df344fSAndy Whitcroft		if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
30800df344fSAndy Whitcroft			if ($quote eq '') {
30900df344fSAndy Whitcroft				$quote = $c;
31000df344fSAndy Whitcroft				$res .= $c;
31100df344fSAndy Whitcroft				$l = $c;
312c2fdda0dSAndy Whitcroft				$qlen = 0;
31300df344fSAndy Whitcroft				next;
31400df344fSAndy Whitcroft			} elsif ($quote eq $c) {
31500df344fSAndy Whitcroft				$quote = '';
31600df344fSAndy Whitcroft			}
31700df344fSAndy Whitcroft		}
318c2fdda0dSAndy Whitcroft		if ($quote eq "'" && $qlen > 1) {
319c2fdda0dSAndy Whitcroft			$quote = '';
320c2fdda0dSAndy Whitcroft		}
32100df344fSAndy Whitcroft		if ($quote && $c ne "\t") {
32200df344fSAndy Whitcroft			$res .= "X";
323c2fdda0dSAndy Whitcroft			$qlen++;
32400df344fSAndy Whitcroft		} else {
32500df344fSAndy Whitcroft			$res .= $c;
32600df344fSAndy Whitcroft		}
32700df344fSAndy Whitcroft
32800df344fSAndy Whitcroft		$l = $c;
32900df344fSAndy Whitcroft	}
33000df344fSAndy Whitcroft
331c2fdda0dSAndy Whitcroft	# Clear out the comments.
33213214adfSAndy Whitcroft	while ($res =~ m@(/\*.*?\*/)@g) {
33313214adfSAndy Whitcroft		substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
334c2fdda0dSAndy Whitcroft	}
335c2fdda0dSAndy Whitcroft	if ($res =~ m@(/\*.*)@) {
33613214adfSAndy Whitcroft		substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
337c2fdda0dSAndy Whitcroft	}
338c2fdda0dSAndy Whitcroft	if ($res =~ m@^.(.*\*/)@) {
33913214adfSAndy Whitcroft		substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
340c2fdda0dSAndy Whitcroft	}
341c2fdda0dSAndy Whitcroft
342c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
343c2fdda0dSAndy Whitcroft	if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
344c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
345c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
346c2fdda0dSAndy Whitcroft
347c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
348c2fdda0dSAndy Whitcroft	} elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
349c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
350c2fdda0dSAndy Whitcroft		$res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
351c2fdda0dSAndy Whitcroft	}
352c2fdda0dSAndy Whitcroft
35300df344fSAndy Whitcroft	return $res;
35400df344fSAndy Whitcroft}
35500df344fSAndy Whitcroft
3568905a67cSAndy Whitcroftsub ctx_statement_block {
3578905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3588905a67cSAndy Whitcroft	my $line = $linenr - 1;
3598905a67cSAndy Whitcroft	my $blk = '';
3608905a67cSAndy Whitcroft	my $soff = $off;
3618905a67cSAndy Whitcroft	my $coff = $off - 1;
3628905a67cSAndy Whitcroft
36313214adfSAndy Whitcroft	my $loff = 0;
36413214adfSAndy Whitcroft
3658905a67cSAndy Whitcroft	my $type = '';
3668905a67cSAndy Whitcroft	my $level = 0;
367*cf655043SAndy Whitcroft	my $p;
3688905a67cSAndy Whitcroft	my $c;
3698905a67cSAndy Whitcroft	my $len = 0;
37013214adfSAndy Whitcroft
37113214adfSAndy Whitcroft	my $remainder;
3728905a67cSAndy Whitcroft	while (1) {
3738905a67cSAndy Whitcroft		#warn "CSB: blk<$blk>\n";
3748905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
3758905a67cSAndy Whitcroft		# context.
3768905a67cSAndy Whitcroft		if ($off >= $len) {
3778905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
378c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
3798905a67cSAndy Whitcroft				$remain--;
38013214adfSAndy Whitcroft				$loff = $len;
381c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
3828905a67cSAndy Whitcroft				$len = length($blk);
3838905a67cSAndy Whitcroft				$line++;
3848905a67cSAndy Whitcroft				last;
3858905a67cSAndy Whitcroft			}
3868905a67cSAndy Whitcroft			# Bail if there is no further context.
3878905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
38813214adfSAndy Whitcroft			if ($off >= $len) {
3898905a67cSAndy Whitcroft				last;
3908905a67cSAndy Whitcroft			}
3918905a67cSAndy Whitcroft		}
392*cf655043SAndy Whitcroft		$p = $c;
3938905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
39413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
3958905a67cSAndy Whitcroft
3968905a67cSAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level>\n";
3978905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
3988905a67cSAndy Whitcroft		# outermost level.
3998905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4008905a67cSAndy Whitcroft			last;
4018905a67cSAndy Whitcroft		}
4028905a67cSAndy Whitcroft
40313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
404*cf655043SAndy Whitcroft		if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
405*cf655043SAndy Whitcroft				$remainder =~ /(else)(?:\s|{)/ &&
406*cf655043SAndy Whitcroft				$remainder !~ /else\s+if\b/) {
40713214adfSAndy Whitcroft			$coff = $off + length($1);
40813214adfSAndy Whitcroft		}
40913214adfSAndy Whitcroft
4108905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4118905a67cSAndy Whitcroft			$level++;
4128905a67cSAndy Whitcroft			$type = '(';
4138905a67cSAndy Whitcroft		}
4148905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4158905a67cSAndy Whitcroft			$level--;
4168905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4178905a67cSAndy Whitcroft
4188905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4198905a67cSAndy Whitcroft				$coff = $off;
4208905a67cSAndy Whitcroft			}
4218905a67cSAndy Whitcroft		}
4228905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4238905a67cSAndy Whitcroft			$level++;
4248905a67cSAndy Whitcroft			$type = '{';
4258905a67cSAndy Whitcroft		}
4268905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4278905a67cSAndy Whitcroft			$level--;
4288905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4298905a67cSAndy Whitcroft
4308905a67cSAndy Whitcroft			if ($level == 0) {
4318905a67cSAndy Whitcroft				last;
4328905a67cSAndy Whitcroft			}
4338905a67cSAndy Whitcroft		}
4348905a67cSAndy Whitcroft		$off++;
4358905a67cSAndy Whitcroft	}
43613214adfSAndy Whitcroft	if ($off == $len) {
43713214adfSAndy Whitcroft		$line++;
43813214adfSAndy Whitcroft		$remain--;
43913214adfSAndy Whitcroft	}
4408905a67cSAndy Whitcroft
4418905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4428905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4438905a67cSAndy Whitcroft
4448905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4458905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4468905a67cSAndy Whitcroft
44713214adfSAndy Whitcroft	#print "off<$off> loff<$loff>\n";
44813214adfSAndy Whitcroft
44913214adfSAndy Whitcroft	return ($statement, $condition,
45013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
45113214adfSAndy Whitcroft}
45213214adfSAndy Whitcroft
453*cf655043SAndy Whitcroftsub statement_lines {
454*cf655043SAndy Whitcroft	my ($stmt) = @_;
455*cf655043SAndy Whitcroft
456*cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
457*cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
458*cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
459*cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
460*cf655043SAndy Whitcroft
461*cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
462*cf655043SAndy Whitcroft
463*cf655043SAndy Whitcroft	return $#stmt_lines + 2;
464*cf655043SAndy Whitcroft}
465*cf655043SAndy Whitcroft
466*cf655043SAndy Whitcroftsub statement_rawlines {
467*cf655043SAndy Whitcroft	my ($stmt) = @_;
468*cf655043SAndy Whitcroft
469*cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
470*cf655043SAndy Whitcroft
471*cf655043SAndy Whitcroft	return $#stmt_lines + 2;
472*cf655043SAndy Whitcroft}
473*cf655043SAndy Whitcroft
474*cf655043SAndy Whitcroftsub statement_block_size {
475*cf655043SAndy Whitcroft	my ($stmt) = @_;
476*cf655043SAndy Whitcroft
477*cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
478*cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
479*cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
480*cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
481*cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
482*cf655043SAndy Whitcroft
483*cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
484*cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
485*cf655043SAndy Whitcroft
486*cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
487*cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
488*cf655043SAndy Whitcroft
489*cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
490*cf655043SAndy Whitcroft		return $stmt_lines;
491*cf655043SAndy Whitcroft	} else {
492*cf655043SAndy Whitcroft		return $stmt_statements;
493*cf655043SAndy Whitcroft	}
494*cf655043SAndy Whitcroft}
495*cf655043SAndy Whitcroft
49613214adfSAndy Whitcroftsub ctx_statement_full {
49713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
49813214adfSAndy Whitcroft	my ($statement, $condition, $level);
49913214adfSAndy Whitcroft
50013214adfSAndy Whitcroft	my (@chunks);
50113214adfSAndy Whitcroft
502*cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
50313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
50413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
50513214adfSAndy Whitcroft	#print "F: c<$condition> s<$statement>\n";
50613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
507*cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
508*cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
509*cf655043SAndy Whitcroft	}
510*cf655043SAndy Whitcroft
511*cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
512*cf655043SAndy Whitcroft	# could continue the statement.
513*cf655043SAndy Whitcroft	for (;;) {
51413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
51513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
516*cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
517*cf655043SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
518*cf655043SAndy Whitcroft		#print "C: push\n";
519*cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
52013214adfSAndy Whitcroft	}
52113214adfSAndy Whitcroft
52213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5238905a67cSAndy Whitcroft}
5248905a67cSAndy Whitcroft
5254a0df2efSAndy Whitcroftsub ctx_block_get {
526f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5274a0df2efSAndy Whitcroft	my $line;
5284a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5294a0df2efSAndy Whitcroft	my $blk = '';
5304a0df2efSAndy Whitcroft	my @o;
5314a0df2efSAndy Whitcroft	my @c;
5324a0df2efSAndy Whitcroft	my @res = ();
5334a0df2efSAndy Whitcroft
534f0a594c1SAndy Whitcroft	my $level = 0;
53500df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
53600df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
53700df344fSAndy Whitcroft		$remain--;
53800df344fSAndy Whitcroft
53900df344fSAndy Whitcroft		$blk .= $rawlines[$line];
540f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
541f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
542f0a594c1SAndy Whitcroft			if ($off > 0) {
543f0a594c1SAndy Whitcroft				$off--;
544f0a594c1SAndy Whitcroft				next;
545f0a594c1SAndy Whitcroft			}
5464a0df2efSAndy Whitcroft
547f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
548f0a594c1SAndy Whitcroft				$level--;
549f0a594c1SAndy Whitcroft				last if ($level == 0);
550f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
551f0a594c1SAndy Whitcroft				$level++;
552f0a594c1SAndy Whitcroft			}
553f0a594c1SAndy Whitcroft		}
5544a0df2efSAndy Whitcroft
555f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
55600df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
5574a0df2efSAndy Whitcroft		}
5584a0df2efSAndy Whitcroft
559f0a594c1SAndy Whitcroft		last if ($level == 0);
5604a0df2efSAndy Whitcroft	}
5614a0df2efSAndy Whitcroft
562f0a594c1SAndy Whitcroft	return ($level, @res);
5634a0df2efSAndy Whitcroft}
5644a0df2efSAndy Whitcroftsub ctx_block_outer {
5654a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
5664a0df2efSAndy Whitcroft
567f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
568f0a594c1SAndy Whitcroft	return @r;
5694a0df2efSAndy Whitcroft}
5704a0df2efSAndy Whitcroftsub ctx_block {
5714a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
5724a0df2efSAndy Whitcroft
573f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
574f0a594c1SAndy Whitcroft	return @r;
575653d4876SAndy Whitcroft}
576653d4876SAndy Whitcroftsub ctx_statement {
577f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
578f0a594c1SAndy Whitcroft
579f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
580f0a594c1SAndy Whitcroft	return @r;
581f0a594c1SAndy Whitcroft}
582f0a594c1SAndy Whitcroftsub ctx_block_level {
583653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
584653d4876SAndy Whitcroft
585f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
5864a0df2efSAndy Whitcroft}
5879c0ca6f9SAndy Whitcroftsub ctx_statement_level {
5889c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
5899c0ca6f9SAndy Whitcroft
5909c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
5919c0ca6f9SAndy Whitcroft}
5924a0df2efSAndy Whitcroft
5934a0df2efSAndy Whitcroftsub ctx_locate_comment {
5944a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
5954a0df2efSAndy Whitcroft
5964a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
59700df344fSAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
5984a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
5994a0df2efSAndy Whitcroft
6004a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6014a0df2efSAndy Whitcroft	# comment.
6024a0df2efSAndy Whitcroft	my $in_comment = 0;
6034a0df2efSAndy Whitcroft	$current_comment = '';
6044a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
60500df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
60600df344fSAndy Whitcroft		#warn "           $line\n";
6074a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6084a0df2efSAndy Whitcroft			$in_comment = 1;
6094a0df2efSAndy Whitcroft		}
6104a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6114a0df2efSAndy Whitcroft			$in_comment = 1;
6124a0df2efSAndy Whitcroft		}
6134a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6144a0df2efSAndy Whitcroft			$current_comment = '';
6154a0df2efSAndy Whitcroft		}
6164a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6174a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6184a0df2efSAndy Whitcroft			$in_comment = 0;
6194a0df2efSAndy Whitcroft		}
6204a0df2efSAndy Whitcroft	}
6214a0df2efSAndy Whitcroft
6224a0df2efSAndy Whitcroft	chomp($current_comment);
6234a0df2efSAndy Whitcroft	return($current_comment);
6244a0df2efSAndy Whitcroft}
6254a0df2efSAndy Whitcroftsub ctx_has_comment {
6264a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6274a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6284a0df2efSAndy Whitcroft
62900df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6304a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6314a0df2efSAndy Whitcroft
6324a0df2efSAndy Whitcroft	return ($cmt ne '');
6334a0df2efSAndy Whitcroft}
6344a0df2efSAndy Whitcroft
6350a920b5bSAndy Whitcroftsub cat_vet {
6360a920b5bSAndy Whitcroft	my ($vet) = @_;
6379c0ca6f9SAndy Whitcroft	my ($res, $coded);
6380a920b5bSAndy Whitcroft
6399c0ca6f9SAndy Whitcroft	$res = '';
6406c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
6416c72ffaaSAndy Whitcroft		$res .= $1;
6426c72ffaaSAndy Whitcroft		if ($2 ne '') {
6439c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
6446c72ffaaSAndy Whitcroft			$res .= $coded;
6456c72ffaaSAndy Whitcroft		}
6469c0ca6f9SAndy Whitcroft	}
6479c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
6480a920b5bSAndy Whitcroft
6499c0ca6f9SAndy Whitcroft	return $res;
6500a920b5bSAndy Whitcroft}
6510a920b5bSAndy Whitcroft
652c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
653*cf655043SAndy Whitcroftmy $av_pending;
654c2fdda0dSAndy Whitcroftmy @av_paren_type;
655c2fdda0dSAndy Whitcroft
656c2fdda0dSAndy Whitcroftsub annotate_reset {
657c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
658*cf655043SAndy Whitcroft	$av_pending = '_';
659*cf655043SAndy Whitcroft	@av_paren_type = ('E');
660c2fdda0dSAndy Whitcroft}
661c2fdda0dSAndy Whitcroft
6626c72ffaaSAndy Whitcroftsub annotate_values {
6636c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
6646c72ffaaSAndy Whitcroft
6656c72ffaaSAndy Whitcroft	my $res;
6666c72ffaaSAndy Whitcroft	my $cur = $stream;
6676c72ffaaSAndy Whitcroft
668c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
6696c72ffaaSAndy Whitcroft
6706c72ffaaSAndy Whitcroft	while (length($cur)) {
671*cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
672*cf655043SAndy Whitcroft					"> <$type> " if ($dbg_values > 1);
6736c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
674c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
675c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
676*cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
677c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
6786c72ffaaSAndy Whitcroft			}
6796c72ffaaSAndy Whitcroft
6808905a67cSAndy Whitcroft		} elsif ($cur =~ /^($Type)/) {
681c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
6826c72ffaaSAndy Whitcroft			$type = 'T';
6836c72ffaaSAndy Whitcroft
6846c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
685c2fdda0dSAndy Whitcroft			print "DEFINE($1)\n" if ($dbg_values > 1);
686c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
687*cf655043SAndy Whitcroft			$av_pending = 'N';
6886c72ffaaSAndy Whitcroft
689*cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
690*cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
691c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
692*cf655043SAndy Whitcroft
693*cf655043SAndy Whitcroft			push(@av_paren_type, $type);
694*cf655043SAndy Whitcroft			push(@av_paren_type, $type);
695*cf655043SAndy Whitcroft			$type = 'N';
696*cf655043SAndy Whitcroft
697*cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
698*cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
699*cf655043SAndy Whitcroft			$av_preprocessor = 1;
700*cf655043SAndy Whitcroft
701*cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
702*cf655043SAndy Whitcroft
703*cf655043SAndy Whitcroft			$type = 'N';
704*cf655043SAndy Whitcroft
705*cf655043SAndy Whitcroft		} elsif ($cur =~ /^(#\s*(?:endif))/o) {
706*cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
707*cf655043SAndy Whitcroft
708*cf655043SAndy Whitcroft			$av_preprocessor = 1;
709*cf655043SAndy Whitcroft
710*cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
711*cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
712*cf655043SAndy Whitcroft			pop(@av_paren_type);
713*cf655043SAndy Whitcroft			push(@av_paren_type, $type);
7146c72ffaaSAndy Whitcroft			$type = 'N';
7156c72ffaaSAndy Whitcroft
7166c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
717c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
7186c72ffaaSAndy Whitcroft
7196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
720c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
7216c72ffaaSAndy Whitcroft			if (defined $2) {
722*cf655043SAndy Whitcroft				$av_pending = 'V';
7236c72ffaaSAndy Whitcroft			}
7246c72ffaaSAndy Whitcroft			$type = 'N';
7256c72ffaaSAndy Whitcroft
72613214adfSAndy Whitcroft		} elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
727c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
728*cf655043SAndy Whitcroft			$av_pending = 'N';
7296c72ffaaSAndy Whitcroft			$type = 'N';
7306c72ffaaSAndy Whitcroft
7316c72ffaaSAndy Whitcroft		} elsif ($cur =~/^(return|case|else)/o) {
732c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
7336c72ffaaSAndy Whitcroft			$type = 'N';
7346c72ffaaSAndy Whitcroft
7356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
736c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
737*cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
738*cf655043SAndy Whitcroft			$av_pending = '_';
7396c72ffaaSAndy Whitcroft			$type = 'N';
7406c72ffaaSAndy Whitcroft
7416c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
742*cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
743*cf655043SAndy Whitcroft			if ($new_type ne '_') {
744*cf655043SAndy Whitcroft				$type = $new_type;
745c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
746c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
7476c72ffaaSAndy Whitcroft			} else {
748c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
7496c72ffaaSAndy Whitcroft			}
7506c72ffaaSAndy Whitcroft
7516c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident)\(/o) {
752c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
753*cf655043SAndy Whitcroft			$av_pending = 'V';
7546c72ffaaSAndy Whitcroft
7556c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
756c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
7576c72ffaaSAndy Whitcroft			$type = 'V';
7586c72ffaaSAndy Whitcroft
7596c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
760c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
7616c72ffaaSAndy Whitcroft			$type = 'N';
7626c72ffaaSAndy Whitcroft
763*cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
764c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
76513214adfSAndy Whitcroft			$type = 'E';
76613214adfSAndy Whitcroft
767*cf655043SAndy Whitcroft		} elsif ($cur =~ /^(;|\?|:|\[)/o) {
76813214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
7696c72ffaaSAndy Whitcroft			$type = 'N';
7706c72ffaaSAndy Whitcroft
7716c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
772c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
7736c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
7746c72ffaaSAndy Whitcroft				$type = 'N';
7756c72ffaaSAndy Whitcroft			}
7766c72ffaaSAndy Whitcroft
7776c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
778c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
7796c72ffaaSAndy Whitcroft		}
7806c72ffaaSAndy Whitcroft		if (defined $1) {
7816c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
7826c72ffaaSAndy Whitcroft			$res .= $type x length($1);
7836c72ffaaSAndy Whitcroft		}
7846c72ffaaSAndy Whitcroft	}
7856c72ffaaSAndy Whitcroft
7866c72ffaaSAndy Whitcroft	return $res;
7876c72ffaaSAndy Whitcroft}
7886c72ffaaSAndy Whitcroft
7898905a67cSAndy Whitcroftsub possible {
79013214adfSAndy Whitcroft	my ($possible, $line) = @_;
7918905a67cSAndy Whitcroft
7928905a67cSAndy Whitcroft	#print "CHECK<$possible>\n";
7938905a67cSAndy Whitcroft	if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
7948905a67cSAndy Whitcroft	    $possible ne 'goto' && $possible ne 'return' &&
7958905a67cSAndy Whitcroft	    $possible ne 'struct' && $possible ne 'enum' &&
7968905a67cSAndy Whitcroft	    $possible ne 'case' && $possible ne 'else' &&
7978905a67cSAndy Whitcroft	    $possible ne 'typedef') {
79813214adfSAndy Whitcroft		warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
7998905a67cSAndy Whitcroft		push(@typeList, $possible);
8008905a67cSAndy Whitcroft		build_types();
8018905a67cSAndy Whitcroft	}
8028905a67cSAndy Whitcroft}
8038905a67cSAndy Whitcroft
8046c72ffaaSAndy Whitcroftmy $prefix = '';
8056c72ffaaSAndy Whitcroft
806f0a594c1SAndy Whitcroftsub report {
8078905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
8088905a67cSAndy Whitcroft
8098905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
8108905a67cSAndy Whitcroft
81113214adfSAndy Whitcroft	push(our @report, $line);
812f0a594c1SAndy Whitcroft}
813f0a594c1SAndy Whitcroftsub report_dump {
81413214adfSAndy Whitcroft	our @report;
815f0a594c1SAndy Whitcroft}
816de7d4f0eSAndy Whitcroftsub ERROR {
817f0a594c1SAndy Whitcroft	report("ERROR: $_[0]\n");
818de7d4f0eSAndy Whitcroft	our $clean = 0;
8196c72ffaaSAndy Whitcroft	our $cnt_error++;
820de7d4f0eSAndy Whitcroft}
821de7d4f0eSAndy Whitcroftsub WARN {
822f0a594c1SAndy Whitcroft	report("WARNING: $_[0]\n");
823de7d4f0eSAndy Whitcroft	our $clean = 0;
8246c72ffaaSAndy Whitcroft	our $cnt_warn++;
825de7d4f0eSAndy Whitcroft}
826de7d4f0eSAndy Whitcroftsub CHK {
8276c72ffaaSAndy Whitcroft	if ($check) {
828f0a594c1SAndy Whitcroft		report("CHECK: $_[0]\n");
829de7d4f0eSAndy Whitcroft		our $clean = 0;
8306c72ffaaSAndy Whitcroft		our $cnt_chk++;
8316c72ffaaSAndy Whitcroft	}
832de7d4f0eSAndy Whitcroft}
833de7d4f0eSAndy Whitcroft
8340a920b5bSAndy Whitcroftsub process {
8350a920b5bSAndy Whitcroft	my $filename = shift;
8360a920b5bSAndy Whitcroft
8370a920b5bSAndy Whitcroft	my $linenr=0;
8380a920b5bSAndy Whitcroft	my $prevline="";
839c2fdda0dSAndy Whitcroft	my $prevrawline="";
8400a920b5bSAndy Whitcroft	my $stashline="";
841c2fdda0dSAndy Whitcroft	my $stashrawline="";
8420a920b5bSAndy Whitcroft
8434a0df2efSAndy Whitcroft	my $length;
8440a920b5bSAndy Whitcroft	my $indent;
8450a920b5bSAndy Whitcroft	my $previndent=0;
8460a920b5bSAndy Whitcroft	my $stashindent=0;
8470a920b5bSAndy Whitcroft
848de7d4f0eSAndy Whitcroft	our $clean = 1;
8490a920b5bSAndy Whitcroft	my $signoff = 0;
8500a920b5bSAndy Whitcroft	my $is_patch = 0;
8510a920b5bSAndy Whitcroft
85213214adfSAndy Whitcroft	our @report = ();
8536c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
8546c72ffaaSAndy Whitcroft	our $cnt_error = 0;
8556c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
8566c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
8576c72ffaaSAndy Whitcroft
8580a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
8590a920b5bSAndy Whitcroft	my $realfile = '';
8600a920b5bSAndy Whitcroft	my $realline = 0;
8610a920b5bSAndy Whitcroft	my $realcnt = 0;
8620a920b5bSAndy Whitcroft	my $here = '';
8630a920b5bSAndy Whitcroft	my $in_comment = 0;
864c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
8650a920b5bSAndy Whitcroft	my $first_line = 0;
8660a920b5bSAndy Whitcroft
86713214adfSAndy Whitcroft	my $prev_values = 'E';
86813214adfSAndy Whitcroft
86913214adfSAndy Whitcroft	# suppression flags
87013214adfSAndy Whitcroft	my $suppress_ifbraces = 0;
871653d4876SAndy Whitcroft
872c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
873de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
874c2fdda0dSAndy Whitcroft	#
875de7d4f0eSAndy Whitcroft	my @setup_docs = ();
876de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
877c2fdda0dSAndy Whitcroft	my $line;
878c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
879c2fdda0dSAndy Whitcroft		# Standardise the strings and chars within the input to
880c2fdda0dSAndy Whitcroft		# simplify matching.
881c2fdda0dSAndy Whitcroft		$line = sanitise_line($rawline);
882c2fdda0dSAndy Whitcroft		push(@lines, $line);
883c2fdda0dSAndy Whitcroft
884c2fdda0dSAndy Whitcroft		##print "==>$rawline\n";
885c2fdda0dSAndy Whitcroft		##print "-->$line\n";
886c2fdda0dSAndy Whitcroft
887de7d4f0eSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
888de7d4f0eSAndy Whitcroft			$setup_docs = 0;
889de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
890de7d4f0eSAndy Whitcroft				$setup_docs = 1;
891de7d4f0eSAndy Whitcroft			}
892de7d4f0eSAndy Whitcroft			next;
893de7d4f0eSAndy Whitcroft		}
894de7d4f0eSAndy Whitcroft
895de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
896de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
897de7d4f0eSAndy Whitcroft		}
898de7d4f0eSAndy Whitcroft	}
899de7d4f0eSAndy Whitcroft
9006c72ffaaSAndy Whitcroft	$prefix = '';
9016c72ffaaSAndy Whitcroft
9020a920b5bSAndy Whitcroft	foreach my $line (@lines) {
9030a920b5bSAndy Whitcroft		$linenr++;
9040a920b5bSAndy Whitcroft
905c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
9066c72ffaaSAndy Whitcroft
9070a920b5bSAndy Whitcroft#extract the filename as it passes
9080a920b5bSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
9090a920b5bSAndy Whitcroft			$realfile=$1;
91000df344fSAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
9110a920b5bSAndy Whitcroft			$in_comment = 0;
9120a920b5bSAndy Whitcroft			next;
9130a920b5bSAndy Whitcroft		}
9140a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
9156c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
9160a920b5bSAndy Whitcroft			$is_patch = 1;
9174a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
9180a920b5bSAndy Whitcroft			$in_comment = 0;
9190a920b5bSAndy Whitcroft			$realline=$1-1;
9200a920b5bSAndy Whitcroft			if (defined $2) {
9210a920b5bSAndy Whitcroft				$realcnt=$3+1;
9220a920b5bSAndy Whitcroft			} else {
9230a920b5bSAndy Whitcroft				$realcnt=1+1;
9240a920b5bSAndy Whitcroft			}
925c2fdda0dSAndy Whitcroft			annotate_reset();
92613214adfSAndy Whitcroft			$prev_values = 'E';
92713214adfSAndy Whitcroft
92813214adfSAndy Whitcroft			$suppress_ifbraces = $linenr - 1;
9290a920b5bSAndy Whitcroft			next;
9300a920b5bSAndy Whitcroft		}
9310a920b5bSAndy Whitcroft
9324a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
9334a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
9344a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
9354a0df2efSAndy Whitcroft		if ($line =~ /^( |\+|$)/) {
9360a920b5bSAndy Whitcroft			$realline++;
937d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
9380a920b5bSAndy Whitcroft
9398905a67cSAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
9408905a67cSAndy Whitcroft			# the context looking for a comment "edge".  If this
9418905a67cSAndy Whitcroft			# edge is a close comment then we must be in a comment
9428905a67cSAndy Whitcroft			# at context start.
9438905a67cSAndy Whitcroft			if ($linenr == $first_line) {
9448905a67cSAndy Whitcroft				my $edge;
9458905a67cSAndy Whitcroft				for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
946c2fdda0dSAndy Whitcroft					($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
9478905a67cSAndy Whitcroft					last if (defined $edge);
9488905a67cSAndy Whitcroft				}
9498905a67cSAndy Whitcroft				if (defined $edge && $edge eq '*/') {
9508905a67cSAndy Whitcroft					$in_comment = 1;
9518905a67cSAndy Whitcroft				}
9528905a67cSAndy Whitcroft			}
9538905a67cSAndy Whitcroft
9540a920b5bSAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
9550a920b5bSAndy Whitcroft			# is the start of a diff block and this line starts
9560a920b5bSAndy Whitcroft			# ' *' then it is very likely a comment.
957c2fdda0dSAndy Whitcroft			if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
9580a920b5bSAndy Whitcroft				$in_comment = 1;
9590a920b5bSAndy Whitcroft			}
9608905a67cSAndy Whitcroft
9618905a67cSAndy Whitcroft			# Find the last comment edge on _this_ line.
962c2fdda0dSAndy Whitcroft			$comment_edge = 0;
963c2fdda0dSAndy Whitcroft			while (($rawline =~ m@(/\*|\*/)@g)) {
9648905a67cSAndy Whitcroft				if ($1 eq '/*') {
9650a920b5bSAndy Whitcroft					$in_comment = 1;
9668905a67cSAndy Whitcroft				} else {
9670a920b5bSAndy Whitcroft					$in_comment = 0;
9680a920b5bSAndy Whitcroft				}
969c2fdda0dSAndy Whitcroft				$comment_edge = 1;
9708905a67cSAndy Whitcroft			}
9710a920b5bSAndy Whitcroft
9724a0df2efSAndy Whitcroft			# Measure the line length and indent.
973c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
9740a920b5bSAndy Whitcroft
9750a920b5bSAndy Whitcroft			# Track the previous line.
9760a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
9770a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
978c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
979c2fdda0dSAndy Whitcroft
980c2fdda0dSAndy Whitcroft			#warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
9816c72ffaaSAndy Whitcroft
982d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
983d8aaf121SAndy Whitcroft			$realcnt--;
9840a920b5bSAndy Whitcroft		}
9850a920b5bSAndy Whitcroft
9860a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
9876c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
9886c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
989389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
9900a920b5bSAndy Whitcroft
991c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
992c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
993c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
9940a920b5bSAndy Whitcroft
9956c72ffaaSAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
9966c72ffaaSAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
9976c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
9986c72ffaaSAndy Whitcroft
9990a920b5bSAndy Whitcroft#check the patch for a signoff:
1000d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
10014a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
10024a0df2efSAndy Whitcroft			$signoff++;
10030a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1004de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1005de7d4f0eSAndy Whitcroft					$herecurr);
10060a920b5bSAndy Whitcroft			}
10070a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1008de7d4f0eSAndy Whitcroft				WARN("need space after Signed-off-by:\n" .
1009de7d4f0eSAndy Whitcroft					$herecurr);
10100a920b5bSAndy Whitcroft			}
10110a920b5bSAndy Whitcroft		}
10120a920b5bSAndy Whitcroft
101300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
10148905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1015de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
10166c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1017de7d4f0eSAndy Whitcroft		}
1018de7d4f0eSAndy Whitcroft
1019de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1020de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1021c2fdda0dSAndy Whitcroft		     !($rawline =~ m/^(
1022de7d4f0eSAndy Whitcroft				[\x09\x0A\x0D\x20-\x7E]              # ASCII
1023de7d4f0eSAndy Whitcroft				| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
1024de7d4f0eSAndy Whitcroft				|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
1025de7d4f0eSAndy Whitcroft				| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
1026de7d4f0eSAndy Whitcroft				|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
1027de7d4f0eSAndy Whitcroft				|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
1028de7d4f0eSAndy Whitcroft				| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
1029de7d4f0eSAndy Whitcroft				|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
1030de7d4f0eSAndy Whitcroft				)*$/x )) {
1031c2fdda0dSAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
103200df344fSAndy Whitcroft		}
10330a920b5bSAndy Whitcroft
103400df344fSAndy Whitcroft#ignore lines being removed
103500df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
103600df344fSAndy Whitcroft
103700df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
103800df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
10390a920b5bSAndy Whitcroft
10400a920b5bSAndy Whitcroft#trailing whitespace
10419c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1042c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
10439c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
10449c0ca6f9SAndy Whitcroft
1045c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1046c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1047de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
10480a920b5bSAndy Whitcroft		}
10490a920b5bSAndy Whitcroft#80 column limit
1050c2fdda0dSAndy Whitcroft		if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
1051de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
10520a920b5bSAndy Whitcroft		}
10530a920b5bSAndy Whitcroft
10548905a67cSAndy Whitcroft# check for adding lines without a newline.
10558905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
10568905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
10578905a67cSAndy Whitcroft		}
10588905a67cSAndy Whitcroft
10590a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
10600a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
10610a920b5bSAndy Whitcroft
10620a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
10630a920b5bSAndy Whitcroft# more than 8 must use tabs.
1064c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1065c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1066c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1067de7d4f0eSAndy Whitcroft			ERROR("use tabs not spaces\n" . $herevet);
10680a920b5bSAndy Whitcroft		}
10690a920b5bSAndy Whitcroft
1070c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1071*cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1072c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1073c2fdda0dSAndy Whitcroft		}
107422f2a2efSAndy Whitcroft
107522f2a2efSAndy Whitcroft# The rest of our checks refer specifically to C style
107622f2a2efSAndy Whitcroft# only apply those _outside_ comments.  Only skip
107722f2a2efSAndy Whitcroft# lines in the middle of comments.
107822f2a2efSAndy Whitcroft		next if (!$comment_edge && $in_comment);
107900df344fSAndy Whitcroft
10809c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1081c2fdda0dSAndy Whitcroft		if ($realcnt) {
1082*cf655043SAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1083*cf655043SAndy Whitcroft			$s =~ s/\n./ /g;
1084*cf655043SAndy Whitcroft			$s =~ s/{.*$//;
1085*cf655043SAndy Whitcroft
1086c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1087*cf655043SAndy Whitcroft			if ($s =~ /$Ident:\*$/) {
1088c2fdda0dSAndy Whitcroft
1089c2fdda0dSAndy Whitcroft			# Ignore functions being called
1090*cf655043SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/) {
1091c2fdda0dSAndy Whitcroft
10926c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1093*cf655043SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1094*cf655043SAndy Whitcroft				possible($1, $s);
10958905a67cSAndy Whitcroft
10966c72ffaaSAndy Whitcroft			# declarations always start with types
1097*cf655043SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1098*cf655043SAndy Whitcroft				possible($1, $s);
1099c2fdda0dSAndy Whitcroft			}
11008905a67cSAndy Whitcroft
11016c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
1102*cf655043SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1103*cf655043SAndy Whitcroft				possible($1, $s);
11049c0ca6f9SAndy Whitcroft			}
11058905a67cSAndy Whitcroft
11068905a67cSAndy Whitcroft			# Check for any sort of function declaration.
11078905a67cSAndy Whitcroft			# int foo(something bar, other baz);
11088905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1109*cf655043SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
11108905a67cSAndy Whitcroft				my ($name_len) = length($1);
11118905a67cSAndy Whitcroft
1112*cf655043SAndy Whitcroft				my $ctx = $s;
11138905a67cSAndy Whitcroft				substr($ctx, 0, $name_len + 1) = '';
11148905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1115*cf655043SAndy Whitcroft
11168905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
11178905a67cSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
11188905a67cSAndy Whitcroft
1119*cf655043SAndy Whitcroft						possible($1, $s);
11208905a67cSAndy Whitcroft					}
11218905a67cSAndy Whitcroft				}
11228905a67cSAndy Whitcroft			}
11238905a67cSAndy Whitcroft
11249c0ca6f9SAndy Whitcroft		}
11259c0ca6f9SAndy Whitcroft
112600df344fSAndy Whitcroft#
112700df344fSAndy Whitcroft# Checks which may be anchored in the context.
112800df344fSAndy Whitcroft#
112900df344fSAndy Whitcroft
113000df344fSAndy Whitcroft# Check for switch () and associated case and default
113100df344fSAndy Whitcroft# statements should be at the same indent.
113200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
113300df344fSAndy Whitcroft			my $err = '';
113400df344fSAndy Whitcroft			my $sep = '';
113500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
113600df344fSAndy Whitcroft			shift(@ctx);
113700df344fSAndy Whitcroft			for my $ctx (@ctx) {
113800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
113900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
114000df344fSAndy Whitcroft							$indent != $cindent) {
114100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
114200df344fSAndy Whitcroft					$sep = '';
114300df344fSAndy Whitcroft				} else {
114400df344fSAndy Whitcroft					$sep = "[...]\n";
114500df344fSAndy Whitcroft				}
114600df344fSAndy Whitcroft			}
114700df344fSAndy Whitcroft			if ($err ne '') {
11489c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1149de7d4f0eSAndy Whitcroft			}
1150de7d4f0eSAndy Whitcroft		}
1151de7d4f0eSAndy Whitcroft
1152de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1153de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1154de7d4f0eSAndy Whitcroft		if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
11559c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1156de7d4f0eSAndy Whitcroft			my $ctx_ln = $linenr + $#ctx + 1;
1157de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1158de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1159de7d4f0eSAndy Whitcroft
11609c0ca6f9SAndy Whitcroft			# Skip over any removed lines in the context following statement.
1161de7d4f0eSAndy Whitcroft			while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1162de7d4f0eSAndy Whitcroft				$ctx_ln++;
1163de7d4f0eSAndy Whitcroft				$ctx_cnt--;
1164de7d4f0eSAndy Whitcroft			}
1165de7d4f0eSAndy Whitcroft			##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1166de7d4f0eSAndy Whitcroft
1167de7d4f0eSAndy Whitcroft			if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1168f0a594c1SAndy Whitcroft				ERROR("That open brace { should be on the previous line\n" .
1169de7d4f0eSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]");
117000df344fSAndy Whitcroft			}
11719c0ca6f9SAndy Whitcroft			if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
11729c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
11739c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
11749c0ca6f9SAndy Whitcroft					WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
11759c0ca6f9SAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]");
11769c0ca6f9SAndy Whitcroft				}
11779c0ca6f9SAndy Whitcroft			}
117800df344fSAndy Whitcroft		}
117900df344fSAndy Whitcroft
11806c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
11816c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
11826c72ffaaSAndy Whitcroft		my $curr_values = annotate_values($opline . "\n", $prev_values);
11836c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1184c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1185c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1186*cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1187*cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
1188c2fdda0dSAndy Whitcroft		}
11896c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
11906c72ffaaSAndy Whitcroft
119100df344fSAndy Whitcroft#ignore lines not being added
119200df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
119300df344fSAndy Whitcroft
1194653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
1195653d4876SAndy Whitcroft		if ($tst_type && $line =~ /^.$Declare$/) {
1196de7d4f0eSAndy Whitcroft			ERROR("TEST: is type $Declare\n" . $herecurr);
1197653d4876SAndy Whitcroft			next;
1198653d4876SAndy Whitcroft		}
1199653d4876SAndy Whitcroft
1200f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1201f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1202f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1203f0a594c1SAndy Whitcroft			ERROR("That open brace { should be on the previous line\n" . $hereprev);
1204f0a594c1SAndy Whitcroft		}
1205f0a594c1SAndy Whitcroft
120600df344fSAndy Whitcroft#
120700df344fSAndy Whitcroft# Checks which are anchored on the added line.
120800df344fSAndy Whitcroft#
120900df344fSAndy Whitcroft
1210653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1211653d4876SAndy Whitcroft		if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1212653d4876SAndy Whitcroft			my $path = $1;
1213653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1214de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1215de7d4f0eSAndy Whitcroft					$herecurr);
1216653d4876SAndy Whitcroft			}
1217653d4876SAndy Whitcroft		}
1218653d4876SAndy Whitcroft
121900df344fSAndy Whitcroft# no C99 // comments
122000df344fSAndy Whitcroft		if ($line =~ m{//}) {
1221de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
122200df344fSAndy Whitcroft		}
122300df344fSAndy Whitcroft		# Remove C99 comments.
12240a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
12256c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
12260a920b5bSAndy Whitcroft
12270a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1228653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1229653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1230653d4876SAndy Whitcroft			my $name = $1;
12310a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
12320a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
1233653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
1234*cf655043SAndy Whitcroft			   ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1235*cf655043SAndy Whitcroft			   ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1236*cf655043SAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1237de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
12380a920b5bSAndy Whitcroft			}
12390a920b5bSAndy Whitcroft		}
12400a920b5bSAndy Whitcroft
1241f0a594c1SAndy Whitcroft# check for external initialisers.
1242f0a594c1SAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1243f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1244f0a594c1SAndy Whitcroft				$herecurr);
1245f0a594c1SAndy Whitcroft		}
12460a920b5bSAndy Whitcroft# check for static initialisers.
1247f0a594c1SAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1248de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1249de7d4f0eSAndy Whitcroft				$herecurr);
12500a920b5bSAndy Whitcroft		}
12510a920b5bSAndy Whitcroft
1252653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1253653d4876SAndy Whitcroft# make sense.
1254653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
12559c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1256653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1257de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
12580a920b5bSAndy Whitcroft		}
12590a920b5bSAndy Whitcroft
12600a920b5bSAndy Whitcroft# * goes on variable not on type
1261d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1262de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1263de7d4f0eSAndy Whitcroft				$herecurr);
1264d8aaf121SAndy Whitcroft
1265d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1266de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1267de7d4f0eSAndy Whitcroft				$herecurr);
1268d8aaf121SAndy Whitcroft
12699c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1270de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1271de7d4f0eSAndy Whitcroft				$herecurr);
1272d8aaf121SAndy Whitcroft
12739c0ca6f9SAndy Whitcroft		} elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1274de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1275de7d4f0eSAndy Whitcroft				$herecurr);
12760a920b5bSAndy Whitcroft		}
12770a920b5bSAndy Whitcroft
12780a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
12790a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
12800a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
12810a920b5bSAndy Whitcroft# 			print "$herecurr";
12820a920b5bSAndy Whitcroft# 			$clean = 0;
12830a920b5bSAndy Whitcroft# 		}
12840a920b5bSAndy Whitcroft
12858905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1286c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
12878905a67cSAndy Whitcroft		}
12888905a67cSAndy Whitcroft
128900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
129000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
129100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
129200df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
129300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1294f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
129500df344fSAndy Whitcroft			my $ok = 0;
129600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
129700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
129800df344fSAndy Whitcroft				# we have a preceeding printk if it ends
129900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
130000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
130100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
130200df344fSAndy Whitcroft						$ok = 1;
130300df344fSAndy Whitcroft					}
130400df344fSAndy Whitcroft					last;
130500df344fSAndy Whitcroft				}
130600df344fSAndy Whitcroft			}
130700df344fSAndy Whitcroft			if ($ok == 0) {
1308de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
13090a920b5bSAndy Whitcroft			}
131000df344fSAndy Whitcroft		}
13110a920b5bSAndy Whitcroft
1312653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1313653d4876SAndy Whitcroft# or if closed on same line
1314c2fdda0dSAndy Whitcroft		if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
13150a920b5bSAndy Whitcroft		    !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1316de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
13170a920b5bSAndy Whitcroft		}
1318653d4876SAndy Whitcroft
13198905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
13208905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
13218905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
13228905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
13238905a67cSAndy Whitcroft		}
13248905a67cSAndy Whitcroft
1325f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
13266c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1327c2fdda0dSAndy Whitcroft			my $name = $1;
1328c2fdda0dSAndy Whitcroft			my $ctx = substr($line, 0, $-[1]);
1329c2fdda0dSAndy Whitcroft
1330c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
133113214adfSAndy Whitcroft			if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
1332c2fdda0dSAndy Whitcroft
1333c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1334c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1335c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1336c2fdda0dSAndy Whitcroft			} elsif ($ctx =~ /^.\#\s*define\s*$/) {
1337c2fdda0dSAndy Whitcroft
1338c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1339c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1340c2fdda0dSAndy Whitcroft			} elsif ("$ctx$name" =~ /$Type$/) {
1341c2fdda0dSAndy Whitcroft
1342c2fdda0dSAndy Whitcroft			} else {
134322f2a2efSAndy Whitcroft				WARN("no space between function name and open parenthesis '('\n" . $herecurr);
1344f0a594c1SAndy Whitcroft			}
13456c72ffaaSAndy Whitcroft		}
1346653d4876SAndy Whitcroft# Check operator spacing.
13470a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
13489c0ca6f9SAndy Whitcroft			my $ops = qr{
13499c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
13509c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
13519c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
1352c2fdda0dSAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
13539c0ca6f9SAndy Whitcroft			}x;
1354*cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
135500df344fSAndy Whitcroft			my $off = 0;
13566c72ffaaSAndy Whitcroft
13576c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
13586c72ffaaSAndy Whitcroft
13590a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
13604a0df2efSAndy Whitcroft				$off += length($elements[$n]);
13614a0df2efSAndy Whitcroft
13624a0df2efSAndy Whitcroft				my $a = '';
13634a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
13644a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1365*cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
13664a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
13674a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
13684a0df2efSAndy Whitcroft				$a = 'E' if ($elements[$n] eq '' && $n == 0);
13694a0df2efSAndy Whitcroft
13700a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
13714a0df2efSAndy Whitcroft
13724a0df2efSAndy Whitcroft				my $c = '';
13730a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
13744a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
13754a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1376*cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
13774a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
13784a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
137922f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
13804a0df2efSAndy Whitcroft				} else {
13814a0df2efSAndy Whitcroft					$c = 'E';
13820a920b5bSAndy Whitcroft				}
13830a920b5bSAndy Whitcroft
138400df344fSAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1385de7d4f0eSAndy Whitcroft				my $ca = substr($opline, 0, $off);
138600df344fSAndy Whitcroft				my $cc = '';
1387653d4876SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1388d8aaf121SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
138900df344fSAndy Whitcroft				}
1390de7d4f0eSAndy Whitcroft				my $cb = "$ca$;$cc";
139100df344fSAndy Whitcroft
13924a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
13934a0df2efSAndy Whitcroft
13944a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
13954a0df2efSAndy Whitcroft
13966c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1397de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
13980a920b5bSAndy Whitcroft
13999c0ca6f9SAndy Whitcroft				# Classify operators into binary, unary, or
14009c0ca6f9SAndy Whitcroft				# definitions (* only) where they have more
14019c0ca6f9SAndy Whitcroft				# than one mode.
14026c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
14036c72ffaaSAndy Whitcroft				my $op_left = substr($curr_values, $off, 1);
14046c72ffaaSAndy Whitcroft				my $is_unary;
14056c72ffaaSAndy Whitcroft				if ($op_type eq 'T') {
14069c0ca6f9SAndy Whitcroft					$is_unary = 2;
14076c72ffaaSAndy Whitcroft				} elsif ($op_left eq 'V') {
14086c72ffaaSAndy Whitcroft					$is_unary = 0;
14096c72ffaaSAndy Whitcroft				} else {
14106c72ffaaSAndy Whitcroft					$is_unary = 1;
14119c0ca6f9SAndy Whitcroft				}
14129c0ca6f9SAndy Whitcroft				#if ($op eq '-' || $op eq '&' || $op eq '*') {
14136c72ffaaSAndy Whitcroft				#	print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
14149c0ca6f9SAndy Whitcroft				#}
14150a920b5bSAndy Whitcroft
141613214adfSAndy Whitcroft				# Ignore operators passed as parameters.
141713214adfSAndy Whitcroft				if ($op_type ne 'V' &&
141813214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
141913214adfSAndy Whitcroft
1420*cf655043SAndy Whitcroft#				# Ignore comments
1421*cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
142213214adfSAndy Whitcroft
1423d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
142413214adfSAndy Whitcroft				} elsif ($op eq ';') {
1425*cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1426*cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1427de7d4f0eSAndy Whitcroft						ERROR("need space after that '$op' $at\n" . $hereptr);
1428d8aaf121SAndy Whitcroft					}
1429d8aaf121SAndy Whitcroft
1430d8aaf121SAndy Whitcroft				# // is a comment
1431d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
14320a920b5bSAndy Whitcroft
14330a920b5bSAndy Whitcroft				# -> should have no spaces
14340a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
14354a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1436de7d4f0eSAndy Whitcroft						ERROR("no spaces around that '$op' $at\n" . $hereptr);
14370a920b5bSAndy Whitcroft					}
14380a920b5bSAndy Whitcroft
14390a920b5bSAndy Whitcroft				# , must have a space on the right.
14400a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1441*cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1442de7d4f0eSAndy Whitcroft						ERROR("need space after that '$op' $at\n" . $hereptr);
14430a920b5bSAndy Whitcroft					}
14440a920b5bSAndy Whitcroft
14459c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
14469c0ca6f9SAndy Whitcroft				} elsif ($op eq '*' && $is_unary == 2) {
14479c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
14489c0ca6f9SAndy Whitcroft
14499c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
14509c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
14519c0ca6f9SAndy Whitcroft				# unary operator, or a cast
14529c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
14539c0ca6f9SAndy Whitcroft				         ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1454*cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1455de7d4f0eSAndy Whitcroft						ERROR("need space before that '$op' $at\n" . $hereptr);
14560a920b5bSAndy Whitcroft					}
14574a0df2efSAndy Whitcroft					if ($ctx =~ /.xW/) {
1458de7d4f0eSAndy Whitcroft						ERROR("no space after that '$op' $at\n" . $hereptr);
14590a920b5bSAndy Whitcroft					}
14600a920b5bSAndy Whitcroft
14610a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
14620a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1463*cf655043SAndy Whitcroft					if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1464de7d4f0eSAndy Whitcroft						ERROR("need space one side of that '$op' $at\n" . $hereptr);
14650a920b5bSAndy Whitcroft					}
146613214adfSAndy Whitcroft					if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1467de7d4f0eSAndy Whitcroft						ERROR("no space before that '$op' $at\n" . $hereptr);
1468653d4876SAndy Whitcroft					}
14690a920b5bSAndy Whitcroft
14700a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
14719c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
14729c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
14739c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1474c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1475c2fdda0dSAndy Whitcroft					 $op eq '%')
14760a920b5bSAndy Whitcroft				{
1477*cf655043SAndy Whitcroft					if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
1478de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1479de7d4f0eSAndy Whitcroft							$hereptr);
14800a920b5bSAndy Whitcroft					}
14810a920b5bSAndy Whitcroft
14820a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1483*cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
148422f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
148522f2a2efSAndy Whitcroft					if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
148622f2a2efSAndy Whitcroft					    !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1487de7d4f0eSAndy Whitcroft						ERROR("need spaces around that '$op' $at\n" . $hereptr);
14880a920b5bSAndy Whitcroft					}
148922f2a2efSAndy Whitcroft				}
14904a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
14910a920b5bSAndy Whitcroft			}
14920a920b5bSAndy Whitcroft		}
14930a920b5bSAndy Whitcroft
1494f0a594c1SAndy Whitcroft# check for multiple assignments
1495f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
14966c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1497f0a594c1SAndy Whitcroft		}
1498f0a594c1SAndy Whitcroft
149922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
150022f2a2efSAndy Whitcroft## # continuation.
150122f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
150222f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
150322f2a2efSAndy Whitcroft##
150422f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
150522f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
150622f2a2efSAndy Whitcroft## 			my $ln = $line;
150722f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
150822f2a2efSAndy Whitcroft## 			}
150922f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
151022f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
151122f2a2efSAndy Whitcroft## 			}
151222f2a2efSAndy Whitcroft## 		}
1513f0a594c1SAndy Whitcroft
15140a920b5bSAndy Whitcroft#need space before brace following if, while, etc
151522f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
151622f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1517de7d4f0eSAndy Whitcroft			ERROR("need a space before the open brace '{'\n" . $herecurr);
1518de7d4f0eSAndy Whitcroft		}
1519de7d4f0eSAndy Whitcroft
1520de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1521de7d4f0eSAndy Whitcroft# on the line
1522de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1523de7d4f0eSAndy Whitcroft			ERROR("need a space after that close brace '}'\n" . $herecurr);
15240a920b5bSAndy Whitcroft		}
15250a920b5bSAndy Whitcroft
152622f2a2efSAndy Whitcroft# check spacing on square brackets
152722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
152822f2a2efSAndy Whitcroft			ERROR("no space after that open square bracket '['\n" . $herecurr);
152922f2a2efSAndy Whitcroft		}
153022f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
153122f2a2efSAndy Whitcroft			ERROR("no space before that close square bracket ']'\n" . $herecurr);
153222f2a2efSAndy Whitcroft		}
153322f2a2efSAndy Whitcroft
153422f2a2efSAndy Whitcroft# check spacing on paretheses
15359c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
15369c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
153722f2a2efSAndy Whitcroft			ERROR("no space after that open parenthesis '('\n" . $herecurr);
153822f2a2efSAndy Whitcroft		}
153913214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
15409c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/) {
154122f2a2efSAndy Whitcroft			ERROR("no space before that close parenthesis ')'\n" . $herecurr);
154222f2a2efSAndy Whitcroft		}
154322f2a2efSAndy Whitcroft
15440a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
15454a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
15460a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1547de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
15480a920b5bSAndy Whitcroft		}
15490a920b5bSAndy Whitcroft
15500a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
15514a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
1552de7d4f0eSAndy Whitcroft			ERROR("need a space before the open parenthesis '('\n" . $herecurr);
15530a920b5bSAndy Whitcroft		}
15540a920b5bSAndy Whitcroft
15550a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional.
15568905a67cSAndy Whitcroft		if ($line =~ /\bif\s*\(/) {
15578905a67cSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
15588905a67cSAndy Whitcroft
15598905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1560c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
15618905a67cSAndy Whitcroft			}
15628905a67cSAndy Whitcroft
15638905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
15648905a67cSAndy Whitcroft			# conditional.
15658905a67cSAndy Whitcroft			substr($s, 0, length($c)) = '';
15668905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
156713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
156813214adfSAndy Whitcroft			if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
15698905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
15708905a67cSAndy Whitcroft			}
15718905a67cSAndy Whitcroft		}
15728905a67cSAndy Whitcroft
157313214adfSAndy Whitcroft# Check for bitwise tests written as boolean
157413214adfSAndy Whitcroft		if ($line =~ /
157513214adfSAndy Whitcroft			(?:
157613214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
157713214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
157813214adfSAndy Whitcroft				(?:\&\&|\|\|)
157913214adfSAndy Whitcroft			|
158013214adfSAndy Whitcroft				(?:\&\&|\|\|)
158113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
158213214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
158313214adfSAndy Whitcroft			)/x)
158413214adfSAndy Whitcroft		{
158513214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
158613214adfSAndy Whitcroft		}
158713214adfSAndy Whitcroft
15888905a67cSAndy Whitcroft# if and else should not have general statements after it
158913214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
159013214adfSAndy Whitcroft			my $s = $1;
159113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
159213214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
15938905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
15940a920b5bSAndy Whitcroft			}
159513214adfSAndy Whitcroft		}
15960a920b5bSAndy Whitcroft
15970a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
15980a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
15990a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
16000a920b5bSAndy Whitcroft						$previndent == $indent) {
1601de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
16020a920b5bSAndy Whitcroft		}
16030a920b5bSAndy Whitcroft
1604c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1605c2fdda0dSAndy Whitcroft						$previndent == $indent) {
1606c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1607c2fdda0dSAndy Whitcroft
1608c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
1609c2fdda0dSAndy Whitcroft			# conditional.
1610c2fdda0dSAndy Whitcroft			substr($s, 0, length($c)) = '';
1611c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
1612c2fdda0dSAndy Whitcroft
1613c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
1614c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
1615c2fdda0dSAndy Whitcroft			}
1616c2fdda0dSAndy Whitcroft		}
1617c2fdda0dSAndy Whitcroft
16180a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
16190a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
16200a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
16210a920b5bSAndy Whitcroft#		    print "$herecurr";
16220a920b5bSAndy Whitcroft#		    $clean = 0;
16230a920b5bSAndy Whitcroft#		}
16240a920b5bSAndy Whitcroft
16250a920b5bSAndy Whitcroft#no spaces allowed after \ in define
16260a920b5bSAndy Whitcroft		if ($line=~/\#define.*\\\s$/) {
1627de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
16280a920b5bSAndy Whitcroft		}
16290a920b5bSAndy Whitcroft
1630653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1631653d4876SAndy Whitcroft		if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
16326c72ffaaSAndy Whitcroft			my $checkfile = "$root/include/linux/$1.h";
16336c72ffaaSAndy Whitcroft			if (-f $checkfile && $1 ne 'irq.h') {
1634de7d4f0eSAndy Whitcroft				CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1635de7d4f0eSAndy Whitcroft					$herecurr);
16360a920b5bSAndy Whitcroft			}
16370a920b5bSAndy Whitcroft		}
16380a920b5bSAndy Whitcroft
1639653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
1640653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
1641*cf655043SAndy Whitcroft# in a known good container
16429c0ca6f9SAndy Whitcroft		if ($prevline =~ /\#define.*\\/ &&
16439c0ca6f9SAndy Whitcroft		   $prevline !~/(?:do\s+{|\(\{|\{)/ &&
16449c0ca6f9SAndy Whitcroft		   $line !~ /(?:do\s+{|\(\{|\{)/ &&
16459c0ca6f9SAndy Whitcroft		   $line !~ /^.\s*$Declare\s/) {
1646653d4876SAndy Whitcroft			# Grab the first statement, if that is the entire macro
1647653d4876SAndy Whitcroft			# its ok.  This may start either on the #define line
1648653d4876SAndy Whitcroft			# or the one below.
1649d8aaf121SAndy Whitcroft			my $ln = $linenr;
1650d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
1651f0a594c1SAndy Whitcroft			my $off = 0;
1652653d4876SAndy Whitcroft
1653f0a594c1SAndy Whitcroft			# If the macro starts on the define line start
1654f0a594c1SAndy Whitcroft			# grabbing the statement after the identifier
1655f0a594c1SAndy Whitcroft			$prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1656f0a594c1SAndy Whitcroft			##print "1<$1> 2<$2>\n";
165722f2a2efSAndy Whitcroft			if (defined $2 && $2 ne '') {
1658f0a594c1SAndy Whitcroft				$off = length($1);
1659d8aaf121SAndy Whitcroft				$ln--;
1660d8aaf121SAndy Whitcroft				$cnt++;
16618905a67cSAndy Whitcroft				while ($lines[$ln - 1] =~ /^-/) {
16628905a67cSAndy Whitcroft					$ln--;
16638905a67cSAndy Whitcroft					$cnt++;
16648905a67cSAndy Whitcroft				}
1665d8aaf121SAndy Whitcroft			}
1666f0a594c1SAndy Whitcroft			my @ctx = ctx_statement($ln, $cnt, $off);
1667de7d4f0eSAndy Whitcroft			my $ctx_ln = $ln + $#ctx + 1;
1668de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1669de7d4f0eSAndy Whitcroft
1670de7d4f0eSAndy Whitcroft			# Pull in any empty extension lines.
1671de7d4f0eSAndy Whitcroft			while ($ctx =~ /\\$/ &&
1672de7d4f0eSAndy Whitcroft			       $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1673de7d4f0eSAndy Whitcroft				$ctx .= $lines[$ctx_ln - 1];
1674de7d4f0eSAndy Whitcroft				$ctx_ln++;
1675de7d4f0eSAndy Whitcroft			}
1676d8aaf121SAndy Whitcroft
1677d8aaf121SAndy Whitcroft			if ($ctx =~ /\\$/) {
1678d8aaf121SAndy Whitcroft				if ($ctx =~ /;/) {
1679de7d4f0eSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1680d8aaf121SAndy Whitcroft				} else {
1681de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1682d8aaf121SAndy Whitcroft				}
16830a920b5bSAndy Whitcroft			}
1684653d4876SAndy Whitcroft		}
16850a920b5bSAndy Whitcroft
1686f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
168713214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
168813214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
1689*cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
169013214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1691*cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1692*cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
169313214adfSAndy Whitcroft				my $allowed = 0;
169413214adfSAndy Whitcroft				my $seen = 0;
1695*cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
1696*cf655043SAndy Whitcroft				my $ln = $linenr - 1;
169713214adfSAndy Whitcroft				for my $chunk (@chunks) {
169813214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
169913214adfSAndy Whitcroft
1700*cf655043SAndy Whitcroft					$herectx .= "$rawlines[$ln]\n[...]\n";
1701*cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
1702*cf655043SAndy Whitcroft
170313214adfSAndy Whitcroft					substr($block, 0, length($cond)) = '';
170413214adfSAndy Whitcroft
170513214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
170613214adfSAndy Whitcroft
1707*cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
1708*cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
1709*cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
171013214adfSAndy Whitcroft						$allowed = 1;
171113214adfSAndy Whitcroft					}
171213214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
1713*cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
171413214adfSAndy Whitcroft						$allowed = 1;
171513214adfSAndy Whitcroft					}
1716*cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
1717*cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
171813214adfSAndy Whitcroft						$allowed = 1;
171913214adfSAndy Whitcroft					}
172013214adfSAndy Whitcroft				}
172113214adfSAndy Whitcroft				if ($seen && !$allowed) {
1722*cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
172313214adfSAndy Whitcroft				}
1724*cf655043SAndy Whitcroft				# Either way we have looked over this whole
1725*cf655043SAndy Whitcroft				# statement and said what needs to be said.
1726*cf655043SAndy Whitcroft				$suppress_ifbraces = $endln;
172713214adfSAndy Whitcroft			}
172813214adfSAndy Whitcroft		}
172913214adfSAndy Whitcroft		if ($linenr > $suppress_ifbraces &&
173013214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
1731*cf655043SAndy Whitcroft			my ($level, $endln, @chunks) =
1732*cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
1733f0a594c1SAndy Whitcroft
1734*cf655043SAndy Whitcroft			my $allowed = 0;
1735f0a594c1SAndy Whitcroft
1736*cf655043SAndy Whitcroft			# Check the pre-context.
1737*cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1738*cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
1739*cf655043SAndy Whitcroft				$allowed = 1;
1740f0a594c1SAndy Whitcroft			}
1741*cf655043SAndy Whitcroft			# Check the condition.
1742*cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
1743*cf655043SAndy Whitcroft			if (defined $cond) {
1744*cf655043SAndy Whitcroft				substr($block, 0, length($cond)) = '';
1745*cf655043SAndy Whitcroft			}
1746*cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
1747*cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
1748*cf655043SAndy Whitcroft				$allowed = 1;
1749*cf655043SAndy Whitcroft			}
1750*cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
1751*cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
1752*cf655043SAndy Whitcroft				$allowed = 1;
1753*cf655043SAndy Whitcroft			}
1754*cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
1755*cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
1756*cf655043SAndy Whitcroft				$allowed = 1;
1757*cf655043SAndy Whitcroft			}
1758*cf655043SAndy Whitcroft			# Check the post-context.
1759*cf655043SAndy Whitcroft			if (defined $chunks[1]) {
1760*cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
1761*cf655043SAndy Whitcroft				if (defined $cond) {
1762*cf655043SAndy Whitcroft					substr($block, 0, length($cond)) = '';
1763*cf655043SAndy Whitcroft				}
1764*cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
1765*cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
1766*cf655043SAndy Whitcroft					$allowed = 1;
1767*cf655043SAndy Whitcroft				}
1768*cf655043SAndy Whitcroft			}
1769*cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1770*cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
1771*cf655043SAndy Whitcroft				my $end = $linenr + statement_rawlines($block) - 1;
1772*cf655043SAndy Whitcroft
1773*cf655043SAndy Whitcroft				for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1774*cf655043SAndy Whitcroft					$herectx .= $rawlines[$ln] . "\n";;
1775*cf655043SAndy Whitcroft				}
1776*cf655043SAndy Whitcroft
1777*cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1778f0a594c1SAndy Whitcroft			}
1779f0a594c1SAndy Whitcroft		}
1780f0a594c1SAndy Whitcroft
1781653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
17824a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
1783653d4876SAndy Whitcroft			if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1784de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
17850a920b5bSAndy Whitcroft			}
17860a920b5bSAndy Whitcroft		}
17870a920b5bSAndy Whitcroft
17884a0df2efSAndy Whitcroft# don't use deprecated functions
17894a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
179000df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
1791de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
17924a0df2efSAndy Whitcroft			}
17934a0df2efSAndy Whitcroft		}
17944a0df2efSAndy Whitcroft
17954a0df2efSAndy Whitcroft# no volatiles please
17966c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
17976c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1798de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
17994a0df2efSAndy Whitcroft		}
18004a0df2efSAndy Whitcroft
18019c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
18029c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
18039c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
18049c0ca6f9SAndy Whitcroft		}
18059c0ca6f9SAndy Whitcroft
180600df344fSAndy Whitcroft# warn about #if 0
180700df344fSAndy Whitcroft		if ($line =~ /^.#\s*if\s+0\b/) {
1808de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
1809de7d4f0eSAndy Whitcroft				$herecurr);
18104a0df2efSAndy Whitcroft		}
18114a0df2efSAndy Whitcroft
1812f0a594c1SAndy Whitcroft# check for needless kfree() checks
1813f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1814f0a594c1SAndy Whitcroft			my $expr = $1;
1815f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1816f0a594c1SAndy Whitcroft				WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1817f0a594c1SAndy Whitcroft			}
1818f0a594c1SAndy Whitcroft		}
1819f0a594c1SAndy Whitcroft
182000df344fSAndy Whitcroft# warn about #ifdefs in C files
182100df344fSAndy Whitcroft#		if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
182200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
182300df344fSAndy Whitcroft#			print "$herecurr";
182400df344fSAndy Whitcroft#			$clean = 0;
182500df344fSAndy Whitcroft#		}
182600df344fSAndy Whitcroft
182722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
182822f2a2efSAndy Whitcroft		if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
182922f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
183022f2a2efSAndy Whitcroft		}
183122f2a2efSAndy Whitcroft
18324a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
18334a0df2efSAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
18344a0df2efSAndy Whitcroft			my $which = $1;
18354a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1836de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
18374a0df2efSAndy Whitcroft			}
18384a0df2efSAndy Whitcroft		}
18394a0df2efSAndy Whitcroft# check for memory barriers without a comment.
18404a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
18414a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1842de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
18434a0df2efSAndy Whitcroft			}
18444a0df2efSAndy Whitcroft		}
18454a0df2efSAndy Whitcroft# check of hardware specific defines
184622f2a2efSAndy Whitcroft		if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1847de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
18480a920b5bSAndy Whitcroft		}
1849653d4876SAndy Whitcroft
1850de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
1851de7d4f0eSAndy Whitcroft# storage class and type.
18529c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
18539c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
1854de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1855de7d4f0eSAndy Whitcroft		}
1856de7d4f0eSAndy Whitcroft
18578905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
18588905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
18598905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
18608905a67cSAndy Whitcroft		}
18618905a67cSAndy Whitcroft
1862de7d4f0eSAndy Whitcroft# check for new externs in .c files.
1863de7d4f0eSAndy Whitcroft		if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1864de7d4f0eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
1865de7d4f0eSAndy Whitcroft		}
1866de7d4f0eSAndy Whitcroft
1867de7d4f0eSAndy Whitcroft# checks for new __setup's
1868de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
1869de7d4f0eSAndy Whitcroft			my $name = $1;
1870de7d4f0eSAndy Whitcroft
1871de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
1872de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1873de7d4f0eSAndy Whitcroft			}
1874653d4876SAndy Whitcroft		}
18759c0ca6f9SAndy Whitcroft
18769c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
18779c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
18789c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
18799c0ca6f9SAndy Whitcroft		}
188013214adfSAndy Whitcroft
188113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
188213214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
188313214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
188413214adfSAndy Whitcroft		}
188513214adfSAndy Whitcroft	}
188613214adfSAndy Whitcroft
188713214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
188813214adfSAndy Whitcroft	# so just keep quiet.
188913214adfSAndy Whitcroft	if ($#rawlines == -1) {
189013214adfSAndy Whitcroft		exit(0);
18910a920b5bSAndy Whitcroft	}
18920a920b5bSAndy Whitcroft
18938905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
18948905a67cSAndy Whitcroft	# things that appear to be patches.
18958905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
18968905a67cSAndy Whitcroft		exit(0);
18978905a67cSAndy Whitcroft	}
18988905a67cSAndy Whitcroft
18998905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
19008905a67cSAndy Whitcroft	# just keep quiet.
19018905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
19028905a67cSAndy Whitcroft		exit(0);
19038905a67cSAndy Whitcroft	}
19048905a67cSAndy Whitcroft
19058905a67cSAndy Whitcroft	if (!$is_patch) {
1906de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
19070a920b5bSAndy Whitcroft	}
19080a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
1909de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
19100a920b5bSAndy Whitcroft	}
19110a920b5bSAndy Whitcroft
1912f0a594c1SAndy Whitcroft	print report_dump();
191313214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
191413214adfSAndy Whitcroft		print "$filename " if ($summary_file);
19156c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
19166c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
19176c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
19188905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
19196c72ffaaSAndy Whitcroft	}
19208905a67cSAndy Whitcroft
19210a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
1922c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
19230a920b5bSAndy Whitcroft	}
19240a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
1925c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
19260a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
19270a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
19280a920b5bSAndy Whitcroft	}
192913214adfSAndy Whitcroft
19300a920b5bSAndy Whitcroft	return $clean;
19310a920b5bSAndy Whitcroft}
1932