xref: /linux-6.15/scripts/checkpatch.pl (revision 080ba929)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2f4432c5cSDave Jones# (c) 2001, Dave Jones. <[email protected]> (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
52a5a2c25SAndy Whitcroft# (c) 2008, Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftmy $P = $0;
1100df344fSAndy Whitcroft$P =~ s@.*/@@g;
120a920b5bSAndy Whitcroft
1350a7dcfbSAndy Whitcroftmy $V = '0.25';
140a920b5bSAndy Whitcroft
150a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
160a920b5bSAndy Whitcroft
170a920b5bSAndy Whitcroftmy $quiet = 0;
180a920b5bSAndy Whitcroftmy $tree = 1;
190a920b5bSAndy Whitcroftmy $chk_signoff = 1;
200a920b5bSAndy Whitcroftmy $chk_patch = 1;
21773647a0SAndy Whitcroftmy $tst_only;
226c72ffaaSAndy Whitcroftmy $emacs = 0;
238905a67cSAndy Whitcroftmy $terse = 0;
246c72ffaaSAndy Whitcroftmy $file = 0;
256c72ffaaSAndy Whitcroftmy $check = 0;
268905a67cSAndy Whitcroftmy $summary = 1;
278905a67cSAndy Whitcroftmy $mailback = 0;
2813214adfSAndy Whitcroftmy $summary_file = 0;
296c72ffaaSAndy Whitcroftmy $root;
30c2fdda0dSAndy Whitcroftmy %debug;
310a920b5bSAndy WhitcroftGetOptions(
326c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
330a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
340a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
350a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
366c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
378905a67cSAndy Whitcroft	'terse!'	=> \$terse,
386c72ffaaSAndy Whitcroft	'file!'		=> \$file,
396c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
406c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
416c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
428905a67cSAndy Whitcroft	'summary!'	=> \$summary,
438905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
4413214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
4513214adfSAndy Whitcroft
46c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
47773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
480a920b5bSAndy Whitcroft) or exit;
490a920b5bSAndy Whitcroft
500a920b5bSAndy Whitcroftmy $exit = 0;
510a920b5bSAndy Whitcroft
520a920b5bSAndy Whitcroftif ($#ARGV < 0) {
5300df344fSAndy Whitcroft	print "usage: $P [options] patchfile\n";
540a920b5bSAndy Whitcroft	print "version: $V\n";
550a920b5bSAndy Whitcroft	print "options: -q               => quiet\n";
560a920b5bSAndy Whitcroft	print "         --no-tree        => run without a kernel tree\n";
578905a67cSAndy Whitcroft	print "         --terse          => one line per report\n";
586c72ffaaSAndy Whitcroft	print "         --emacs          => emacs compile window format\n";
596c72ffaaSAndy Whitcroft	print "         --file           => check a source file\n";
606c72ffaaSAndy Whitcroft	print "         --strict         => enable more subjective tests\n";
616c72ffaaSAndy Whitcroft	print "         --root           => path to the kernel tree root\n";
6213214adfSAndy Whitcroft	print "         --no-summary     => suppress the per-file summary\n";
6313214adfSAndy Whitcroft	print "         --summary-file   => include the filename in summary\n";
640a920b5bSAndy Whitcroft	exit(1);
650a920b5bSAndy Whitcroft}
660a920b5bSAndy Whitcroft
67c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
68c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
697429c690SAndy Whitcroftmy $dbg_type = 0;
70a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
71c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
72c2fdda0dSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';"
73c2fdda0dSAndy Whitcroft}
74c2fdda0dSAndy Whitcroft
758905a67cSAndy Whitcroftif ($terse) {
768905a67cSAndy Whitcroft	$emacs = 1;
778905a67cSAndy Whitcroft	$quiet++;
788905a67cSAndy Whitcroft}
798905a67cSAndy Whitcroft
806c72ffaaSAndy Whitcroftif ($tree) {
816c72ffaaSAndy Whitcroft	if (defined $root) {
826c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
836c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
846c72ffaaSAndy Whitcroft		}
856c72ffaaSAndy Whitcroft	} else {
866c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
876c72ffaaSAndy Whitcroft			$root = '.';
886c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
896c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
906c72ffaaSAndy Whitcroft			$root = $1;
916c72ffaaSAndy Whitcroft		}
926c72ffaaSAndy Whitcroft	}
936c72ffaaSAndy Whitcroft
946c72ffaaSAndy Whitcroft	if (!defined $root) {
950a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
960a920b5bSAndy Whitcroft		exit(2);
970a920b5bSAndy Whitcroft	}
986c72ffaaSAndy Whitcroft}
996c72ffaaSAndy Whitcroft
1006c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1016c72ffaaSAndy Whitcroft
1026c72ffaaSAndy Whitcroftour $Ident       = qr{[A-Za-z_][A-Za-z\d_]*};
1036c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1046c72ffaaSAndy Whitcroftour $Sparse	= qr{
1056c72ffaaSAndy Whitcroft			__user|
1066c72ffaaSAndy Whitcroft			__kernel|
1076c72ffaaSAndy Whitcroft			__force|
1086c72ffaaSAndy Whitcroft			__iomem|
1096c72ffaaSAndy Whitcroft			__must_check|
1106c72ffaaSAndy Whitcroft			__init_refok|
111cf655043SAndy Whitcroft			__kprobes
1126c72ffaaSAndy Whitcroft		}x;
1136c72ffaaSAndy Whitcroftour $Attribute	= qr{
1146c72ffaaSAndy Whitcroft			const|
1156c72ffaaSAndy Whitcroft			__read_mostly|
1166c72ffaaSAndy Whitcroft			__kprobes|
11724e1d81aSAndy Whitcroft			__(?:mem|cpu|dev|)(?:initdata|init)|
11824e1d81aSAndy Whitcroft			____cacheline_aligned|
11924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1205fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1215fe3af11SAndy Whitcroft			__weak
1226c72ffaaSAndy Whitcroft		  }x;
123c45dcabdSAndy Whitcroftour $Modifier;
1246c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1256c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1266c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1276c72ffaaSAndy Whitcroft
1286c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1296c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
13086f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1316c72ffaaSAndy Whitcroftour $Operators	= qr{
1326c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1336c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
134c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1356c72ffaaSAndy Whitcroft		  }x;
1366c72ffaaSAndy Whitcroft
1378905a67cSAndy Whitcroftour $NonptrType;
1388905a67cSAndy Whitcroftour $Type;
1398905a67cSAndy Whitcroftour $Declare;
1408905a67cSAndy Whitcroft
141171ae1a4SAndy Whitcroftour $UTF8	= qr {
142171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
143171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
144171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
145171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
146171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
147171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
148171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
149171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
150171ae1a4SAndy Whitcroft}x;
151171ae1a4SAndy Whitcroft
1528ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
1538ed22cadSAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:\d|\d\d)|
1548ed22cadSAndy Whitcroft	atomic_t
1558ed22cadSAndy Whitcroft)};
1568ed22cadSAndy Whitcroft
1578905a67cSAndy Whitcroftour @typeList = (
1588905a67cSAndy Whitcroft	qr{void},
159c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
160c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
161c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
162c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
163c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
164c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
165c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
1668905a67cSAndy Whitcroft	qr{unsigned},
1678905a67cSAndy Whitcroft	qr{float},
1688905a67cSAndy Whitcroft	qr{double},
1698905a67cSAndy Whitcroft	qr{bool},
1708905a67cSAndy Whitcroft	qr{struct\s+$Ident},
1718905a67cSAndy Whitcroft	qr{union\s+$Ident},
1728905a67cSAndy Whitcroft	qr{enum\s+$Ident},
1738905a67cSAndy Whitcroft	qr{${Ident}_t},
1748905a67cSAndy Whitcroft	qr{${Ident}_handler},
1758905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
1768905a67cSAndy Whitcroft);
177c45dcabdSAndy Whitcroftour @modifierList = (
178c45dcabdSAndy Whitcroft	qr{fastcall},
179c45dcabdSAndy Whitcroft);
1808905a67cSAndy Whitcroft
1818905a67cSAndy Whitcroftsub build_types {
182d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
183d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
184c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
1858905a67cSAndy Whitcroft	$NonptrType	= qr{
186d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
187cf655043SAndy Whitcroft			(?:
188c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
1898ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
190c45dcabdSAndy Whitcroft				(?:${all}\b)
191cf655043SAndy Whitcroft			)
192c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
1938905a67cSAndy Whitcroft		  }x;
1948905a67cSAndy Whitcroft	$Type	= qr{
195c45dcabdSAndy Whitcroft			$NonptrType
19665863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
197c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
1988905a67cSAndy Whitcroft		  }x;
1998905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2008905a67cSAndy Whitcroft}
2018905a67cSAndy Whitcroftbuild_types();
2026c72ffaaSAndy Whitcroft
2036c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2040a920b5bSAndy Whitcroft
2054a0df2efSAndy Whitcroftmy @dep_includes = ();
2064a0df2efSAndy Whitcroftmy @dep_functions = ();
2076c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2086c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
2096c72ffaaSAndy Whitcroft	open(REMOVE, "<$root/$removal") ||
2106c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
2110a920b5bSAndy Whitcroft	while (<REMOVE>) {
212f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
213f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
214f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2154a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2164a0df2efSAndy Whitcroft
217f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
218f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
219f0a594c1SAndy Whitcroft				}
2204a0df2efSAndy Whitcroft			}
2210a920b5bSAndy Whitcroft		}
2220a920b5bSAndy Whitcroft	}
2230a920b5bSAndy Whitcroft}
2240a920b5bSAndy Whitcroft
22500df344fSAndy Whitcroftmy @rawlines = ();
226c2fdda0dSAndy Whitcroftmy @lines = ();
227c2fdda0dSAndy Whitcroftmy $vname;
2286c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
2296c72ffaaSAndy Whitcroft	if ($file) {
2306c72ffaaSAndy Whitcroft		open(FILE, "diff -u /dev/null $filename|") ||
2316c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
2326c72ffaaSAndy Whitcroft	} else {
2336c72ffaaSAndy Whitcroft		open(FILE, "<$filename") ||
2346c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2356c72ffaaSAndy Whitcroft	}
236c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
237c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
238c2fdda0dSAndy Whitcroft	} else {
239c2fdda0dSAndy Whitcroft		$vname = $filename;
240c2fdda0dSAndy Whitcroft	}
2416c72ffaaSAndy Whitcroft	while (<FILE>) {
2420a920b5bSAndy Whitcroft		chomp;
24300df344fSAndy Whitcroft		push(@rawlines, $_);
2446c72ffaaSAndy Whitcroft	}
2456c72ffaaSAndy Whitcroft	close(FILE);
246c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2470a920b5bSAndy Whitcroft		$exit = 1;
2480a920b5bSAndy Whitcroft	}
24900df344fSAndy Whitcroft	@rawlines = ();
25013214adfSAndy Whitcroft	@lines = ();
2510a920b5bSAndy Whitcroft}
2520a920b5bSAndy Whitcroft
2530a920b5bSAndy Whitcroftexit($exit);
2540a920b5bSAndy Whitcroft
2550a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2566c72ffaaSAndy Whitcroft	my ($root) = @_;
2576c72ffaaSAndy Whitcroft
2586c72ffaaSAndy Whitcroft	my @tree_check = (
2596c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2606c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2616c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2626c72ffaaSAndy Whitcroft	);
2636c72ffaaSAndy Whitcroft
2646c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
2656c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
2660a920b5bSAndy Whitcroft			return 0;
2670a920b5bSAndy Whitcroft		}
2686c72ffaaSAndy Whitcroft	}
2696c72ffaaSAndy Whitcroft	return 1;
2706c72ffaaSAndy Whitcroft}
2710a920b5bSAndy Whitcroft
2720a920b5bSAndy Whitcroftsub expand_tabs {
2730a920b5bSAndy Whitcroft	my ($str) = @_;
2740a920b5bSAndy Whitcroft
2750a920b5bSAndy Whitcroft	my $res = '';
2760a920b5bSAndy Whitcroft	my $n = 0;
2770a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
2780a920b5bSAndy Whitcroft		if ($c eq "\t") {
2790a920b5bSAndy Whitcroft			$res .= ' ';
2800a920b5bSAndy Whitcroft			$n++;
2810a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
2820a920b5bSAndy Whitcroft				$res .= ' ';
2830a920b5bSAndy Whitcroft			}
2840a920b5bSAndy Whitcroft			next;
2850a920b5bSAndy Whitcroft		}
2860a920b5bSAndy Whitcroft		$res .= $c;
2870a920b5bSAndy Whitcroft		$n++;
2880a920b5bSAndy Whitcroft	}
2890a920b5bSAndy Whitcroft
2900a920b5bSAndy Whitcroft	return $res;
2910a920b5bSAndy Whitcroft}
2926c72ffaaSAndy Whitcroftsub copy_spacing {
293773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
2946c72ffaaSAndy Whitcroft	return $res;
2956c72ffaaSAndy Whitcroft}
2960a920b5bSAndy Whitcroft
2974a0df2efSAndy Whitcroftsub line_stats {
2984a0df2efSAndy Whitcroft	my ($line) = @_;
2994a0df2efSAndy Whitcroft
3004a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3014a0df2efSAndy Whitcroft	$line =~ s/^.//;
3024a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3034a0df2efSAndy Whitcroft
3044a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3054a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3064a0df2efSAndy Whitcroft
3074a0df2efSAndy Whitcroft	return (length($line), length($white));
3084a0df2efSAndy Whitcroft}
3094a0df2efSAndy Whitcroft
310773647a0SAndy Whitcroftmy $sanitise_quote = '';
311773647a0SAndy Whitcroft
312773647a0SAndy Whitcroftsub sanitise_line_reset {
313773647a0SAndy Whitcroft	my ($in_comment) = @_;
314773647a0SAndy Whitcroft
315773647a0SAndy Whitcroft	if ($in_comment) {
316773647a0SAndy Whitcroft		$sanitise_quote = '*/';
317773647a0SAndy Whitcroft	} else {
318773647a0SAndy Whitcroft		$sanitise_quote = '';
319773647a0SAndy Whitcroft	}
320773647a0SAndy Whitcroft}
32100df344fSAndy Whitcroftsub sanitise_line {
32200df344fSAndy Whitcroft	my ($line) = @_;
32300df344fSAndy Whitcroft
32400df344fSAndy Whitcroft	my $res = '';
32500df344fSAndy Whitcroft	my $l = '';
32600df344fSAndy Whitcroft
327c2fdda0dSAndy Whitcroft	my $qlen = 0;
328773647a0SAndy Whitcroft	my $off = 0;
329773647a0SAndy Whitcroft	my $c;
33000df344fSAndy Whitcroft
331773647a0SAndy Whitcroft	# Always copy over the diff marker.
332773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
333773647a0SAndy Whitcroft
334773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
335773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
336773647a0SAndy Whitcroft
337773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
338773647a0SAndy Whitcroft		# and end, all to $;.
339773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
340773647a0SAndy Whitcroft			$sanitise_quote = '*/';
341773647a0SAndy Whitcroft
342773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
343773647a0SAndy Whitcroft			$off++;
34400df344fSAndy Whitcroft			next;
345773647a0SAndy Whitcroft		}
34681bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
347773647a0SAndy Whitcroft			$sanitise_quote = '';
348773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
349773647a0SAndy Whitcroft			$off++;
350773647a0SAndy Whitcroft			next;
351773647a0SAndy Whitcroft		}
352773647a0SAndy Whitcroft
353773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
354773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
355773647a0SAndy Whitcroft		    $c eq "\\") {
356773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
357773647a0SAndy Whitcroft			$off++;
358773647a0SAndy Whitcroft			next;
359773647a0SAndy Whitcroft		}
360773647a0SAndy Whitcroft		# Regular quotes.
361773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
362773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
363773647a0SAndy Whitcroft				$sanitise_quote = $c;
364773647a0SAndy Whitcroft
365773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
366773647a0SAndy Whitcroft				next;
367773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
368773647a0SAndy Whitcroft				$sanitise_quote = '';
36900df344fSAndy Whitcroft			}
37000df344fSAndy Whitcroft		}
371773647a0SAndy Whitcroft
372fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
373773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
374773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
375773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
376773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
37700df344fSAndy Whitcroft		} else {
378773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
37900df344fSAndy Whitcroft		}
380c2fdda0dSAndy Whitcroft	}
381c2fdda0dSAndy Whitcroft
382c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
383c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
384c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
385c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
386c2fdda0dSAndy Whitcroft
387c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
388c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
389c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
390c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
391c2fdda0dSAndy Whitcroft	}
392c2fdda0dSAndy Whitcroft
39300df344fSAndy Whitcroft	return $res;
39400df344fSAndy Whitcroft}
39500df344fSAndy Whitcroft
3968905a67cSAndy Whitcroftsub ctx_statement_block {
3978905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
3988905a67cSAndy Whitcroft	my $line = $linenr - 1;
3998905a67cSAndy Whitcroft	my $blk = '';
4008905a67cSAndy Whitcroft	my $soff = $off;
4018905a67cSAndy Whitcroft	my $coff = $off - 1;
402773647a0SAndy Whitcroft	my $coff_set = 0;
4038905a67cSAndy Whitcroft
40413214adfSAndy Whitcroft	my $loff = 0;
40513214adfSAndy Whitcroft
4068905a67cSAndy Whitcroft	my $type = '';
4078905a67cSAndy Whitcroft	my $level = 0;
408cf655043SAndy Whitcroft	my $p;
4098905a67cSAndy Whitcroft	my $c;
4108905a67cSAndy Whitcroft	my $len = 0;
41113214adfSAndy Whitcroft
41213214adfSAndy Whitcroft	my $remainder;
4138905a67cSAndy Whitcroft	while (1) {
414773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4158905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4168905a67cSAndy Whitcroft		# context.
4178905a67cSAndy Whitcroft		if ($off >= $len) {
4188905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
419dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
420c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4218905a67cSAndy Whitcroft				$remain--;
42213214adfSAndy Whitcroft				$loff = $len;
423c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4248905a67cSAndy Whitcroft				$len = length($blk);
4258905a67cSAndy Whitcroft				$line++;
4268905a67cSAndy Whitcroft				last;
4278905a67cSAndy Whitcroft			}
4288905a67cSAndy Whitcroft			# Bail if there is no further context.
4298905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
43013214adfSAndy Whitcroft			if ($off >= $len) {
4318905a67cSAndy Whitcroft				last;
4328905a67cSAndy Whitcroft			}
4338905a67cSAndy Whitcroft		}
434cf655043SAndy Whitcroft		$p = $c;
4358905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
43613214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4378905a67cSAndy Whitcroft
438773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4398905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
4408905a67cSAndy Whitcroft		# outermost level.
4418905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
4428905a67cSAndy Whitcroft			last;
4438905a67cSAndy Whitcroft		}
4448905a67cSAndy Whitcroft
44513214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
446773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
447773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
448773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
449773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
450773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
451773647a0SAndy Whitcroft			$coff_set = 1;
452773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
453773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
45413214adfSAndy Whitcroft		}
45513214adfSAndy Whitcroft
4568905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
4578905a67cSAndy Whitcroft			$level++;
4588905a67cSAndy Whitcroft			$type = '(';
4598905a67cSAndy Whitcroft		}
4608905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
4618905a67cSAndy Whitcroft			$level--;
4628905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
4638905a67cSAndy Whitcroft
4648905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
4658905a67cSAndy Whitcroft				$coff = $off;
466773647a0SAndy Whitcroft				$coff_set = 1;
467773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
4688905a67cSAndy Whitcroft			}
4698905a67cSAndy Whitcroft		}
4708905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
4718905a67cSAndy Whitcroft			$level++;
4728905a67cSAndy Whitcroft			$type = '{';
4738905a67cSAndy Whitcroft		}
4748905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
4758905a67cSAndy Whitcroft			$level--;
4768905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
4778905a67cSAndy Whitcroft
4788905a67cSAndy Whitcroft			if ($level == 0) {
4798905a67cSAndy Whitcroft				last;
4808905a67cSAndy Whitcroft			}
4818905a67cSAndy Whitcroft		}
4828905a67cSAndy Whitcroft		$off++;
4838905a67cSAndy Whitcroft	}
484a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
48513214adfSAndy Whitcroft	if ($off == $len) {
486a3bb97a7SAndy Whitcroft		$loff = $len + 1;
48713214adfSAndy Whitcroft		$line++;
48813214adfSAndy Whitcroft		$remain--;
48913214adfSAndy Whitcroft	}
4908905a67cSAndy Whitcroft
4918905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
4928905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
4938905a67cSAndy Whitcroft
4948905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
4958905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
4968905a67cSAndy Whitcroft
497773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
49813214adfSAndy Whitcroft
49913214adfSAndy Whitcroft	return ($statement, $condition,
50013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
50113214adfSAndy Whitcroft}
50213214adfSAndy Whitcroft
503cf655043SAndy Whitcroftsub statement_lines {
504cf655043SAndy Whitcroft	my ($stmt) = @_;
505cf655043SAndy Whitcroft
506cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
507cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
508cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
509cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
510cf655043SAndy Whitcroft
511cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
512cf655043SAndy Whitcroft
513cf655043SAndy Whitcroft	return $#stmt_lines + 2;
514cf655043SAndy Whitcroft}
515cf655043SAndy Whitcroft
516cf655043SAndy Whitcroftsub statement_rawlines {
517cf655043SAndy Whitcroft	my ($stmt) = @_;
518cf655043SAndy Whitcroft
519cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
520cf655043SAndy Whitcroft
521cf655043SAndy Whitcroft	return $#stmt_lines + 2;
522cf655043SAndy Whitcroft}
523cf655043SAndy Whitcroft
524cf655043SAndy Whitcroftsub statement_block_size {
525cf655043SAndy Whitcroft	my ($stmt) = @_;
526cf655043SAndy Whitcroft
527cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
528cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
529cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
530cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
531cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
532cf655043SAndy Whitcroft
533cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
534cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
535cf655043SAndy Whitcroft
536cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
537cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
538cf655043SAndy Whitcroft
539cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
540cf655043SAndy Whitcroft		return $stmt_lines;
541cf655043SAndy Whitcroft	} else {
542cf655043SAndy Whitcroft		return $stmt_statements;
543cf655043SAndy Whitcroft	}
544cf655043SAndy Whitcroft}
545cf655043SAndy Whitcroft
54613214adfSAndy Whitcroftsub ctx_statement_full {
54713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
54813214adfSAndy Whitcroft	my ($statement, $condition, $level);
54913214adfSAndy Whitcroft
55013214adfSAndy Whitcroft	my (@chunks);
55113214adfSAndy Whitcroft
552cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
55313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
55413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
555773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
55613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
557cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
558cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
559cf655043SAndy Whitcroft	}
560cf655043SAndy Whitcroft
561cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
562cf655043SAndy Whitcroft	# could continue the statement.
563cf655043SAndy Whitcroft	for (;;) {
56413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
56513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
566cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
567773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
568cf655043SAndy Whitcroft		#print "C: push\n";
569cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
57013214adfSAndy Whitcroft	}
57113214adfSAndy Whitcroft
57213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
5738905a67cSAndy Whitcroft}
5748905a67cSAndy Whitcroft
5754a0df2efSAndy Whitcroftsub ctx_block_get {
576f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
5774a0df2efSAndy Whitcroft	my $line;
5784a0df2efSAndy Whitcroft	my $start = $linenr - 1;
5794a0df2efSAndy Whitcroft	my $blk = '';
5804a0df2efSAndy Whitcroft	my @o;
5814a0df2efSAndy Whitcroft	my @c;
5824a0df2efSAndy Whitcroft	my @res = ();
5834a0df2efSAndy Whitcroft
584f0a594c1SAndy Whitcroft	my $level = 0;
58500df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
58600df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
58700df344fSAndy Whitcroft		$remain--;
58800df344fSAndy Whitcroft
58900df344fSAndy Whitcroft		$blk .= $rawlines[$line];
590f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
591f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
592f0a594c1SAndy Whitcroft			if ($off > 0) {
593f0a594c1SAndy Whitcroft				$off--;
594f0a594c1SAndy Whitcroft				next;
595f0a594c1SAndy Whitcroft			}
5964a0df2efSAndy Whitcroft
597f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
598f0a594c1SAndy Whitcroft				$level--;
599f0a594c1SAndy Whitcroft				last if ($level == 0);
600f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
601f0a594c1SAndy Whitcroft				$level++;
602f0a594c1SAndy Whitcroft			}
603f0a594c1SAndy Whitcroft		}
6044a0df2efSAndy Whitcroft
605f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
60600df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
6074a0df2efSAndy Whitcroft		}
6084a0df2efSAndy Whitcroft
609f0a594c1SAndy Whitcroft		last if ($level == 0);
6104a0df2efSAndy Whitcroft	}
6114a0df2efSAndy Whitcroft
612f0a594c1SAndy Whitcroft	return ($level, @res);
6134a0df2efSAndy Whitcroft}
6144a0df2efSAndy Whitcroftsub ctx_block_outer {
6154a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6164a0df2efSAndy Whitcroft
617f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
618f0a594c1SAndy Whitcroft	return @r;
6194a0df2efSAndy Whitcroft}
6204a0df2efSAndy Whitcroftsub ctx_block {
6214a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6224a0df2efSAndy Whitcroft
623f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
624f0a594c1SAndy Whitcroft	return @r;
625653d4876SAndy Whitcroft}
626653d4876SAndy Whitcroftsub ctx_statement {
627f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
628f0a594c1SAndy Whitcroft
629f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
630f0a594c1SAndy Whitcroft	return @r;
631f0a594c1SAndy Whitcroft}
632f0a594c1SAndy Whitcroftsub ctx_block_level {
633653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
634653d4876SAndy Whitcroft
635f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
6364a0df2efSAndy Whitcroft}
6379c0ca6f9SAndy Whitcroftsub ctx_statement_level {
6389c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
6399c0ca6f9SAndy Whitcroft
6409c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
6419c0ca6f9SAndy Whitcroft}
6424a0df2efSAndy Whitcroft
6434a0df2efSAndy Whitcroftsub ctx_locate_comment {
6444a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6454a0df2efSAndy Whitcroft
6464a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
647beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
6484a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
6494a0df2efSAndy Whitcroft
6504a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
6514a0df2efSAndy Whitcroft	# comment.
6524a0df2efSAndy Whitcroft	my $in_comment = 0;
6534a0df2efSAndy Whitcroft	$current_comment = '';
6544a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
65500df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
65600df344fSAndy Whitcroft		#warn "           $line\n";
6574a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
6584a0df2efSAndy Whitcroft			$in_comment = 1;
6594a0df2efSAndy Whitcroft		}
6604a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
6614a0df2efSAndy Whitcroft			$in_comment = 1;
6624a0df2efSAndy Whitcroft		}
6634a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
6644a0df2efSAndy Whitcroft			$current_comment = '';
6654a0df2efSAndy Whitcroft		}
6664a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
6674a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
6684a0df2efSAndy Whitcroft			$in_comment = 0;
6694a0df2efSAndy Whitcroft		}
6704a0df2efSAndy Whitcroft	}
6714a0df2efSAndy Whitcroft
6724a0df2efSAndy Whitcroft	chomp($current_comment);
6734a0df2efSAndy Whitcroft	return($current_comment);
6744a0df2efSAndy Whitcroft}
6754a0df2efSAndy Whitcroftsub ctx_has_comment {
6764a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
6774a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
6784a0df2efSAndy Whitcroft
67900df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
6804a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
6814a0df2efSAndy Whitcroft
6824a0df2efSAndy Whitcroft	return ($cmt ne '');
6834a0df2efSAndy Whitcroft}
6844a0df2efSAndy Whitcroft
6854d001e4dSAndy Whitcroftsub raw_line {
6864d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
6874d001e4dSAndy Whitcroft
6884d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
6894d001e4dSAndy Whitcroft	$cnt++;
6904d001e4dSAndy Whitcroft
6914d001e4dSAndy Whitcroft	my $line;
6924d001e4dSAndy Whitcroft	while ($cnt) {
6934d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
6944d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
6954d001e4dSAndy Whitcroft		$cnt--;
6964d001e4dSAndy Whitcroft	}
6974d001e4dSAndy Whitcroft
6984d001e4dSAndy Whitcroft	return $line;
6994d001e4dSAndy Whitcroft}
7004d001e4dSAndy Whitcroft
7010a920b5bSAndy Whitcroftsub cat_vet {
7020a920b5bSAndy Whitcroft	my ($vet) = @_;
7039c0ca6f9SAndy Whitcroft	my ($res, $coded);
7040a920b5bSAndy Whitcroft
7059c0ca6f9SAndy Whitcroft	$res = '';
7066c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
7076c72ffaaSAndy Whitcroft		$res .= $1;
7086c72ffaaSAndy Whitcroft		if ($2 ne '') {
7099c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
7106c72ffaaSAndy Whitcroft			$res .= $coded;
7116c72ffaaSAndy Whitcroft		}
7129c0ca6f9SAndy Whitcroft	}
7139c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
7140a920b5bSAndy Whitcroft
7159c0ca6f9SAndy Whitcroft	return $res;
7160a920b5bSAndy Whitcroft}
7170a920b5bSAndy Whitcroft
718c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
719cf655043SAndy Whitcroftmy $av_pending;
720c2fdda0dSAndy Whitcroftmy @av_paren_type;
7211f65f947SAndy Whitcroftmy $av_pend_colon;
722c2fdda0dSAndy Whitcroft
723c2fdda0dSAndy Whitcroftsub annotate_reset {
724c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
725cf655043SAndy Whitcroft	$av_pending = '_';
726cf655043SAndy Whitcroft	@av_paren_type = ('E');
7271f65f947SAndy Whitcroft	$av_pend_colon = 'O';
728c2fdda0dSAndy Whitcroft}
729c2fdda0dSAndy Whitcroft
7306c72ffaaSAndy Whitcroftsub annotate_values {
7316c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
7326c72ffaaSAndy Whitcroft
7336c72ffaaSAndy Whitcroft	my $res;
7341f65f947SAndy Whitcroft	my $var = '_' x length($stream);
7356c72ffaaSAndy Whitcroft	my $cur = $stream;
7366c72ffaaSAndy Whitcroft
737c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
7386c72ffaaSAndy Whitcroft
7396c72ffaaSAndy Whitcroft	while (length($cur)) {
740773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
741cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
742171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
7436c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
744c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
745c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
746cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
747c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
7486c72ffaaSAndy Whitcroft			}
7496c72ffaaSAndy Whitcroft
7508ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
751c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
7526c72ffaaSAndy Whitcroft			$type = 'T';
7536c72ffaaSAndy Whitcroft
754389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
755389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
756389a2fe5SAndy Whitcroft			$type = 'T';
757389a2fe5SAndy Whitcroft
758c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
759171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
760c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
761171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
762171ae1a4SAndy Whitcroft			if ($2 ne '') {
763cf655043SAndy Whitcroft				$av_pending = 'N';
764171ae1a4SAndy Whitcroft			}
765171ae1a4SAndy Whitcroft			$type = 'E';
766171ae1a4SAndy Whitcroft
767c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
768171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
769171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
770171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
7716c72ffaaSAndy Whitcroft
772c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
773cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
774c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
775cf655043SAndy Whitcroft
776cf655043SAndy Whitcroft			push(@av_paren_type, $type);
777cf655043SAndy Whitcroft			push(@av_paren_type, $type);
778171ae1a4SAndy Whitcroft			$type = 'E';
779cf655043SAndy Whitcroft
780c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
781cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
782cf655043SAndy Whitcroft			$av_preprocessor = 1;
783cf655043SAndy Whitcroft
784cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
785cf655043SAndy Whitcroft
786171ae1a4SAndy Whitcroft			$type = 'E';
787cf655043SAndy Whitcroft
788c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
789cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
790cf655043SAndy Whitcroft
791cf655043SAndy Whitcroft			$av_preprocessor = 1;
792cf655043SAndy Whitcroft
793cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
794cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
795cf655043SAndy Whitcroft			pop(@av_paren_type);
796cf655043SAndy Whitcroft			push(@av_paren_type, $type);
797171ae1a4SAndy Whitcroft			$type = 'E';
7986c72ffaaSAndy Whitcroft
7996c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
800c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
8016c72ffaaSAndy Whitcroft
802171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
803171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
804171ae1a4SAndy Whitcroft			$av_pending = $type;
805171ae1a4SAndy Whitcroft			$type = 'N';
806171ae1a4SAndy Whitcroft
8076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
808c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
8096c72ffaaSAndy Whitcroft			if (defined $2) {
810cf655043SAndy Whitcroft				$av_pending = 'V';
8116c72ffaaSAndy Whitcroft			}
8126c72ffaaSAndy Whitcroft			$type = 'N';
8136c72ffaaSAndy Whitcroft
81414b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
815c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
81614b111c1SAndy Whitcroft			$av_pending = 'E';
8176c72ffaaSAndy Whitcroft			$type = 'N';
8186c72ffaaSAndy Whitcroft
8191f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
8201f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
8211f65f947SAndy Whitcroft			$av_pend_colon = 'C';
8221f65f947SAndy Whitcroft			$type = 'N';
8231f65f947SAndy Whitcroft
82414b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
825c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
8266c72ffaaSAndy Whitcroft			$type = 'N';
8276c72ffaaSAndy Whitcroft
8286c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
829c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
830cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
831cf655043SAndy Whitcroft			$av_pending = '_';
8326c72ffaaSAndy Whitcroft			$type = 'N';
8336c72ffaaSAndy Whitcroft
8346c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
835cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
836cf655043SAndy Whitcroft			if ($new_type ne '_') {
837cf655043SAndy Whitcroft				$type = $new_type;
838c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
839c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
8406c72ffaaSAndy Whitcroft			} else {
841c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
8426c72ffaaSAndy Whitcroft			}
8436c72ffaaSAndy Whitcroft
844c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
845c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
846c8cb2ca3SAndy Whitcroft			$type = 'V';
847cf655043SAndy Whitcroft			$av_pending = 'V';
8486c72ffaaSAndy Whitcroft
8498e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
8508e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
8511f65f947SAndy Whitcroft				$av_pend_colon = 'B';
8528e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
8538e761b04SAndy Whitcroft				$av_pend_colon = 'L';
8541f65f947SAndy Whitcroft			}
8551f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
8561f65f947SAndy Whitcroft			$type = 'V';
8571f65f947SAndy Whitcroft
8586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
859c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
8606c72ffaaSAndy Whitcroft			$type = 'V';
8616c72ffaaSAndy Whitcroft
8626c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
863c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
8646c72ffaaSAndy Whitcroft			$type = 'N';
8656c72ffaaSAndy Whitcroft
866cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
867c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
86813214adfSAndy Whitcroft			$type = 'E';
8691f65f947SAndy Whitcroft			$av_pend_colon = 'O';
87013214adfSAndy Whitcroft
8718e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
8728e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
8738e761b04SAndy Whitcroft			$type = 'C';
8748e761b04SAndy Whitcroft
8751f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
8761f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
8771f65f947SAndy Whitcroft			$type = 'N';
8781f65f947SAndy Whitcroft
8791f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
8801f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
8811f65f947SAndy Whitcroft
8821f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
8831f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
8841f65f947SAndy Whitcroft				$type = 'E';
8851f65f947SAndy Whitcroft			} else {
8861f65f947SAndy Whitcroft				$type = 'N';
8871f65f947SAndy Whitcroft			}
8881f65f947SAndy Whitcroft			$av_pend_colon = 'O';
8891f65f947SAndy Whitcroft
8908e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
89113214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
8926c72ffaaSAndy Whitcroft			$type = 'N';
8936c72ffaaSAndy Whitcroft
8940d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
89574048ed8SAndy Whitcroft			my $variant;
89674048ed8SAndy Whitcroft
89774048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
89874048ed8SAndy Whitcroft			if ($type eq 'V') {
89974048ed8SAndy Whitcroft				$variant = 'B';
90074048ed8SAndy Whitcroft			} else {
90174048ed8SAndy Whitcroft				$variant = 'U';
90274048ed8SAndy Whitcroft			}
90374048ed8SAndy Whitcroft
90474048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
90574048ed8SAndy Whitcroft			$type = 'N';
90674048ed8SAndy Whitcroft
9076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
908c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
9096c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
9106c72ffaaSAndy Whitcroft				$type = 'N';
9116c72ffaaSAndy Whitcroft			}
9126c72ffaaSAndy Whitcroft
9136c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
914c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
9156c72ffaaSAndy Whitcroft		}
9166c72ffaaSAndy Whitcroft		if (defined $1) {
9176c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
9186c72ffaaSAndy Whitcroft			$res .= $type x length($1);
9196c72ffaaSAndy Whitcroft		}
9206c72ffaaSAndy Whitcroft	}
9216c72ffaaSAndy Whitcroft
9221f65f947SAndy Whitcroft	return ($res, $var);
9236c72ffaaSAndy Whitcroft}
9246c72ffaaSAndy Whitcroft
9258905a67cSAndy Whitcroftsub possible {
92613214adfSAndy Whitcroft	my ($possible, $line) = @_;
9278905a67cSAndy Whitcroft
9280776e594SAndy Whitcroft	print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
9290776e594SAndy Whitcroft	if ($possible !~ /(?:
9300776e594SAndy Whitcroft		^(?:
9310776e594SAndy Whitcroft			$Modifier|
9320776e594SAndy Whitcroft			$Storage|
9330776e594SAndy Whitcroft			$Type|
9340776e594SAndy Whitcroft			DEFINE_\S+|
9350776e594SAndy Whitcroft			goto|
9360776e594SAndy Whitcroft			return|
9370776e594SAndy Whitcroft			case|
9380776e594SAndy Whitcroft			else|
9390776e594SAndy Whitcroft			asm|__asm__|
9400776e594SAndy Whitcroft			do
9410776e594SAndy Whitcroft		)$|
9420776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
9430776e594SAndy Whitcroft	    )/x) {
944c45dcabdSAndy Whitcroft		# Check for modifiers.
945c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
946c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
947c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
948c45dcabdSAndy Whitcroft
949c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
950c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
951d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
952d2506586SAndy Whitcroft				warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
953d2506586SAndy Whitcroft				push(@modifierList, $modifier);
954d2506586SAndy Whitcroft			}
955c45dcabdSAndy Whitcroft
956c45dcabdSAndy Whitcroft		} else {
95713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
9588905a67cSAndy Whitcroft			push(@typeList, $possible);
959c45dcabdSAndy Whitcroft		}
9608905a67cSAndy Whitcroft		build_types();
9610776e594SAndy Whitcroft	} else {
9620776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
9638905a67cSAndy Whitcroft	}
9648905a67cSAndy Whitcroft}
9658905a67cSAndy Whitcroft
9666c72ffaaSAndy Whitcroftmy $prefix = '';
9676c72ffaaSAndy Whitcroft
968f0a594c1SAndy Whitcroftsub report {
969773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
970773647a0SAndy Whitcroft		return 0;
971773647a0SAndy Whitcroft	}
9728905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
9738905a67cSAndy Whitcroft
9748905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
9758905a67cSAndy Whitcroft
97613214adfSAndy Whitcroft	push(our @report, $line);
977773647a0SAndy Whitcroft
978773647a0SAndy Whitcroft	return 1;
979f0a594c1SAndy Whitcroft}
980f0a594c1SAndy Whitcroftsub report_dump {
98113214adfSAndy Whitcroft	our @report;
982f0a594c1SAndy Whitcroft}
983de7d4f0eSAndy Whitcroftsub ERROR {
984773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
985de7d4f0eSAndy Whitcroft		our $clean = 0;
9866c72ffaaSAndy Whitcroft		our $cnt_error++;
987de7d4f0eSAndy Whitcroft	}
988773647a0SAndy Whitcroft}
989de7d4f0eSAndy Whitcroftsub WARN {
990773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
991de7d4f0eSAndy Whitcroft		our $clean = 0;
9926c72ffaaSAndy Whitcroft		our $cnt_warn++;
993de7d4f0eSAndy Whitcroft	}
994773647a0SAndy Whitcroft}
995de7d4f0eSAndy Whitcroftsub CHK {
996773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
997de7d4f0eSAndy Whitcroft		our $clean = 0;
9986c72ffaaSAndy Whitcroft		our $cnt_chk++;
9996c72ffaaSAndy Whitcroft	}
1000de7d4f0eSAndy Whitcroft}
1001de7d4f0eSAndy Whitcroft
10026ecd9674SAndy Whitcroftsub check_absolute_file {
10036ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
10046ecd9674SAndy Whitcroft	my $file = $absolute;
10056ecd9674SAndy Whitcroft
10066ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
10076ecd9674SAndy Whitcroft
10086ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
10096ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
10106ecd9674SAndy Whitcroft		if (-f "$root/$file") {
10116ecd9674SAndy Whitcroft			##print "file<$file>\n";
10126ecd9674SAndy Whitcroft			last;
10136ecd9674SAndy Whitcroft		}
10146ecd9674SAndy Whitcroft	}
10156ecd9674SAndy Whitcroft	if (! -f _)  {
10166ecd9674SAndy Whitcroft		return 0;
10176ecd9674SAndy Whitcroft	}
10186ecd9674SAndy Whitcroft
10196ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
10206ecd9674SAndy Whitcroft	my $prefix = $absolute;
10216ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
10226ecd9674SAndy Whitcroft
10236ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
10246ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
10256ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
10266ecd9674SAndy Whitcroft	}
10276ecd9674SAndy Whitcroft}
10286ecd9674SAndy Whitcroft
10290a920b5bSAndy Whitcroftsub process {
10300a920b5bSAndy Whitcroft	my $filename = shift;
10310a920b5bSAndy Whitcroft
10320a920b5bSAndy Whitcroft	my $linenr=0;
10330a920b5bSAndy Whitcroft	my $prevline="";
1034c2fdda0dSAndy Whitcroft	my $prevrawline="";
10350a920b5bSAndy Whitcroft	my $stashline="";
1036c2fdda0dSAndy Whitcroft	my $stashrawline="";
10370a920b5bSAndy Whitcroft
10384a0df2efSAndy Whitcroft	my $length;
10390a920b5bSAndy Whitcroft	my $indent;
10400a920b5bSAndy Whitcroft	my $previndent=0;
10410a920b5bSAndy Whitcroft	my $stashindent=0;
10420a920b5bSAndy Whitcroft
1043de7d4f0eSAndy Whitcroft	our $clean = 1;
10440a920b5bSAndy Whitcroft	my $signoff = 0;
10450a920b5bSAndy Whitcroft	my $is_patch = 0;
10460a920b5bSAndy Whitcroft
104713214adfSAndy Whitcroft	our @report = ();
10486c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
10496c72ffaaSAndy Whitcroft	our $cnt_error = 0;
10506c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
10516c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
10526c72ffaaSAndy Whitcroft
10530a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
10540a920b5bSAndy Whitcroft	my $realfile = '';
10550a920b5bSAndy Whitcroft	my $realline = 0;
10560a920b5bSAndy Whitcroft	my $realcnt = 0;
10570a920b5bSAndy Whitcroft	my $here = '';
10580a920b5bSAndy Whitcroft	my $in_comment = 0;
1059c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
10600a920b5bSAndy Whitcroft	my $first_line = 0;
10611e855726SWolfram Sang	my $p1_prefix = '';
10620a920b5bSAndy Whitcroft
106313214adfSAndy Whitcroft	my $prev_values = 'E';
106413214adfSAndy Whitcroft
106513214adfSAndy Whitcroft	# suppression flags
1066773647a0SAndy Whitcroft	my %suppress_ifbraces;
1067170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
1068653d4876SAndy Whitcroft
1069c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1070de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1071c2fdda0dSAndy Whitcroft	#
1072de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1073de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1074773647a0SAndy Whitcroft
1075773647a0SAndy Whitcroft	sanitise_line_reset();
1076c2fdda0dSAndy Whitcroft	my $line;
1077c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1078773647a0SAndy Whitcroft		$linenr++;
1079773647a0SAndy Whitcroft		$line = $rawline;
1080c2fdda0dSAndy Whitcroft
1081773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1082de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1083de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1084de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1085de7d4f0eSAndy Whitcroft			}
1086773647a0SAndy Whitcroft			#next;
1087de7d4f0eSAndy Whitcroft		}
1088773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1089773647a0SAndy Whitcroft			$realline=$1-1;
1090773647a0SAndy Whitcroft			if (defined $2) {
1091773647a0SAndy Whitcroft				$realcnt=$3+1;
1092773647a0SAndy Whitcroft			} else {
1093773647a0SAndy Whitcroft				$realcnt=1+1;
1094773647a0SAndy Whitcroft			}
1095c45dcabdSAndy Whitcroft			$in_comment = 0;
1096773647a0SAndy Whitcroft
1097773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1098773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1099773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1100773647a0SAndy Whitcroft			# at context start.
1101773647a0SAndy Whitcroft			my $edge;
110201fa9147SAndy Whitcroft			my $cnt = $realcnt;
110301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
110401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
110501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
110601fa9147SAndy Whitcroft				$cnt--;
110701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1108721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1109fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1110fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1111fae17daeSAndy Whitcroft					($edge) = $1;
1112fae17daeSAndy Whitcroft					last;
1113fae17daeSAndy Whitcroft				}
1114773647a0SAndy Whitcroft			}
1115773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1116773647a0SAndy Whitcroft				$in_comment = 1;
1117773647a0SAndy Whitcroft			}
1118773647a0SAndy Whitcroft
1119773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1120773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1121773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1122773647a0SAndy Whitcroft			if (!defined $edge &&
112383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1124773647a0SAndy Whitcroft			{
1125773647a0SAndy Whitcroft				$in_comment = 1;
1126773647a0SAndy Whitcroft			}
1127773647a0SAndy Whitcroft
1128773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1129773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1130773647a0SAndy Whitcroft
1131171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1132773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1133171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1134773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1135773647a0SAndy Whitcroft		}
1136773647a0SAndy Whitcroft		push(@lines, $line);
1137773647a0SAndy Whitcroft
1138773647a0SAndy Whitcroft		if ($realcnt > 1) {
1139773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1140773647a0SAndy Whitcroft		} else {
1141773647a0SAndy Whitcroft			$realcnt = 0;
1142773647a0SAndy Whitcroft		}
1143773647a0SAndy Whitcroft
1144773647a0SAndy Whitcroft		#print "==>$rawline\n";
1145773647a0SAndy Whitcroft		#print "-->$line\n";
1146de7d4f0eSAndy Whitcroft
1147de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1148de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1149de7d4f0eSAndy Whitcroft		}
1150de7d4f0eSAndy Whitcroft	}
1151de7d4f0eSAndy Whitcroft
11526c72ffaaSAndy Whitcroft	$prefix = '';
11536c72ffaaSAndy Whitcroft
1154773647a0SAndy Whitcroft	$realcnt = 0;
1155773647a0SAndy Whitcroft	$linenr = 0;
11560a920b5bSAndy Whitcroft	foreach my $line (@lines) {
11570a920b5bSAndy Whitcroft		$linenr++;
11580a920b5bSAndy Whitcroft
1159c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
116030670854SAndy Whitcroft		my $hunk_line = ($realcnt != 0);
11616c72ffaaSAndy Whitcroft
11620a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
11636c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
11640a920b5bSAndy Whitcroft			$is_patch = 1;
11654a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
11660a920b5bSAndy Whitcroft			$realline=$1-1;
11670a920b5bSAndy Whitcroft			if (defined $2) {
11680a920b5bSAndy Whitcroft				$realcnt=$3+1;
11690a920b5bSAndy Whitcroft			} else {
11700a920b5bSAndy Whitcroft				$realcnt=1+1;
11710a920b5bSAndy Whitcroft			}
1172c2fdda0dSAndy Whitcroft			annotate_reset();
117313214adfSAndy Whitcroft			$prev_values = 'E';
117413214adfSAndy Whitcroft
1175773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1176170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
11770a920b5bSAndy Whitcroft			next;
11780a920b5bSAndy Whitcroft
11794a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
11804a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
11814a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1182773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
11830a920b5bSAndy Whitcroft			$realline++;
1184d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
11850a920b5bSAndy Whitcroft
11864a0df2efSAndy Whitcroft			# Measure the line length and indent.
1187c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
11880a920b5bSAndy Whitcroft
11890a920b5bSAndy Whitcroft			# Track the previous line.
11900a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
11910a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1192c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1193c2fdda0dSAndy Whitcroft
1194773647a0SAndy Whitcroft			#warn "line<$line>\n";
11956c72ffaaSAndy Whitcroft
1196d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1197d8aaf121SAndy Whitcroft			$realcnt--;
11980a920b5bSAndy Whitcroft		}
11990a920b5bSAndy Whitcroft
12000a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1201773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1202773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1203773647a0SAndy Whitcroft
12046c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
12056c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1206773647a0SAndy Whitcroft
1207773647a0SAndy Whitcroft		# extract the filename as it passes
1208773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1209773647a0SAndy Whitcroft			$realfile = $1;
12101e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
12111e855726SWolfram Sang
12121e855726SWolfram Sang			$p1_prefix = $1;
12131e855726SWolfram Sang			if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
12141e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
12151e855726SWolfram Sang			}
1216773647a0SAndy Whitcroft
1217c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1218773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1219773647a0SAndy Whitcroft			}
1220773647a0SAndy Whitcroft			next;
1221773647a0SAndy Whitcroft		}
1222773647a0SAndy Whitcroft
1223389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
12240a920b5bSAndy Whitcroft
1225c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1226c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1227c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
12280a920b5bSAndy Whitcroft
12296c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
12306c72ffaaSAndy Whitcroft
12310a920b5bSAndy Whitcroft#check the patch for a signoff:
1232d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
12334a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
12344a0df2efSAndy Whitcroft			$signoff++;
12350a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1236de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1237de7d4f0eSAndy Whitcroft					$herecurr);
12380a920b5bSAndy Whitcroft			}
12390a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1240773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1241de7d4f0eSAndy Whitcroft					$herecurr);
12420a920b5bSAndy Whitcroft			}
12430a920b5bSAndy Whitcroft		}
12440a920b5bSAndy Whitcroft
124500df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
12468905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1247de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
12486c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1249de7d4f0eSAndy Whitcroft		}
1250de7d4f0eSAndy Whitcroft
12516ecd9674SAndy Whitcroft# Check for absolute kernel paths.
12526ecd9674SAndy Whitcroft		if ($tree) {
12536ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
12546ecd9674SAndy Whitcroft				my $file = $1;
12556ecd9674SAndy Whitcroft
12566ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
12576ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
12586ecd9674SAndy Whitcroft					#
12596ecd9674SAndy Whitcroft				} else {
12606ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
12616ecd9674SAndy Whitcroft				}
12626ecd9674SAndy Whitcroft			}
12636ecd9674SAndy Whitcroft		}
12646ecd9674SAndy Whitcroft
1265de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1266de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1267171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1268171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1269171ae1a4SAndy Whitcroft
1270171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1271171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1272171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1273171ae1a4SAndy Whitcroft
1274171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
127500df344fSAndy Whitcroft		}
12760a920b5bSAndy Whitcroft
127730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
127830670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
127900df344fSAndy Whitcroft
12800a920b5bSAndy Whitcroft#trailing whitespace
12819c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1282c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
12839c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
12849c0ca6f9SAndy Whitcroft
1285c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1286c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1287de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
12880a920b5bSAndy Whitcroft		}
12895368df20SAndy Whitcroft
12905368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
12915368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
12925368df20SAndy Whitcroft
12930a920b5bSAndy Whitcroft#80 column limit
1294c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1295f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1296f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1297f4c014c0SAndy Whitcroft		    $length > 80)
1298c45dcabdSAndy Whitcroft		{
1299de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
13000a920b5bSAndy Whitcroft		}
13010a920b5bSAndy Whitcroft
13028905a67cSAndy Whitcroft# check for adding lines without a newline.
13038905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
13048905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
13058905a67cSAndy Whitcroft		}
13068905a67cSAndy Whitcroft
1307b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1308b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
13090a920b5bSAndy Whitcroft
13100a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
13110a920b5bSAndy Whitcroft# more than 8 must use tabs.
1312c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1313c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1314c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1315171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
13160a920b5bSAndy Whitcroft		}
13170a920b5bSAndy Whitcroft
1318b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1319b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1320b9ea10d6SAndy Whitcroft
1321c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1322cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1323c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1324c2fdda0dSAndy Whitcroft		}
132522f2a2efSAndy Whitcroft
13269c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1327170d3a22SAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
13289c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1329170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1330f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1331171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1332171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1333171ae1a4SAndy Whitcroft
1334171ae1a4SAndy Whitcroft			my $s = $stat;
1335171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1336cf655043SAndy Whitcroft
1337c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1338171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1339c2fdda0dSAndy Whitcroft
1340c2fdda0dSAndy Whitcroft			# Ignore functions being called
1341171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1342c2fdda0dSAndy Whitcroft
1343c45dcabdSAndy Whitcroft			# declarations always start with types
1344d2506586SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1345c45dcabdSAndy Whitcroft				my $type = $1;
1346c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1347c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1348c45dcabdSAndy Whitcroft
13496c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1350a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1351c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1352c2fdda0dSAndy Whitcroft			}
13538905a67cSAndy Whitcroft
13546c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
135565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1356c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
13579c0ca6f9SAndy Whitcroft			}
13588905a67cSAndy Whitcroft
13598905a67cSAndy Whitcroft			# Check for any sort of function declaration.
13608905a67cSAndy Whitcroft			# int foo(something bar, other baz);
13618905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1362171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
13638905a67cSAndy Whitcroft				my ($name_len) = length($1);
13648905a67cSAndy Whitcroft
1365cf655043SAndy Whitcroft				my $ctx = $s;
1366773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
13678905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1368cf655043SAndy Whitcroft
13698905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1370c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
13718905a67cSAndy Whitcroft
1372c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
13738905a67cSAndy Whitcroft					}
13748905a67cSAndy Whitcroft				}
13758905a67cSAndy Whitcroft			}
13768905a67cSAndy Whitcroft
13779c0ca6f9SAndy Whitcroft		}
13789c0ca6f9SAndy Whitcroft
137900df344fSAndy Whitcroft#
138000df344fSAndy Whitcroft# Checks which may be anchored in the context.
138100df344fSAndy Whitcroft#
138200df344fSAndy Whitcroft
138300df344fSAndy Whitcroft# Check for switch () and associated case and default
138400df344fSAndy Whitcroft# statements should be at the same indent.
138500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
138600df344fSAndy Whitcroft			my $err = '';
138700df344fSAndy Whitcroft			my $sep = '';
138800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
138900df344fSAndy Whitcroft			shift(@ctx);
139000df344fSAndy Whitcroft			for my $ctx (@ctx) {
139100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
139200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
139300df344fSAndy Whitcroft							$indent != $cindent) {
139400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
139500df344fSAndy Whitcroft					$sep = '';
139600df344fSAndy Whitcroft				} else {
139700df344fSAndy Whitcroft					$sep = "[...]\n";
139800df344fSAndy Whitcroft				}
139900df344fSAndy Whitcroft			}
140000df344fSAndy Whitcroft			if ($err ne '') {
14019c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1402de7d4f0eSAndy Whitcroft			}
1403de7d4f0eSAndy Whitcroft		}
1404de7d4f0eSAndy Whitcroft
1405de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1406de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1407c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1408773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1409773647a0SAndy Whitcroft
14109c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1411de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1412de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1413de7d4f0eSAndy Whitcroft
1414548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1415548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1416de7d4f0eSAndy Whitcroft
1417548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1418548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1419548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1420548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1421548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1422773647a0SAndy Whitcroft				$ctx_ln++;
1423773647a0SAndy Whitcroft			}
1424548596d5SAndy Whitcroft
142553210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
142653210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1427773647a0SAndy Whitcroft
1428773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1429773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1430c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
143100df344fSAndy Whitcroft			}
1432773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1433773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1434773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1435773647a0SAndy Whitcroft			{
14369c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
14379c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1438773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1439c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
14409c0ca6f9SAndy Whitcroft				}
14419c0ca6f9SAndy Whitcroft			}
144200df344fSAndy Whitcroft		}
144300df344fSAndy Whitcroft
14444d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
14454d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
14464d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
14474d001e4dSAndy Whitcroft
14484d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
14494d001e4dSAndy Whitcroft
14504d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
14514d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
14524d001e4dSAndy Whitcroft			# where necessary.
14534d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
14544d001e4dSAndy Whitcroft
14554d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
14566f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
14576f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
14584d001e4dSAndy Whitcroft
14594d001e4dSAndy Whitcroft			# We want to check the first line inside the block
14604d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
14614d001e4dSAndy Whitcroft			#  1) any blank line termination
14624d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
14634d001e4dSAndy Whitcroft			#  3) any do (...) {
14644d001e4dSAndy Whitcroft			my $continuation = 0;
14654d001e4dSAndy Whitcroft			my $check = 0;
14664d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
14674d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
14684d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
14694d001e4dSAndy Whitcroft				$continuation = 1;
14704d001e4dSAndy Whitcroft			}
14719bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
14724d001e4dSAndy Whitcroft				$check = 1;
14734d001e4dSAndy Whitcroft				$cond_lines++;
14744d001e4dSAndy Whitcroft			}
14754d001e4dSAndy Whitcroft
14764d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
14774d001e4dSAndy Whitcroft			# preprocessor statement.
14784d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
14794d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
14804d001e4dSAndy Whitcroft				$check = 0;
14814d001e4dSAndy Whitcroft			}
14824d001e4dSAndy Whitcroft
14839bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1484740504c6SAndy Whitcroft			$continuation = 0;
14859bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
14869bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
14874d001e4dSAndy Whitcroft
1488f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1489f16fa28fSAndy Whitcroft				# is not linear.
1490f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1491f16fa28fSAndy Whitcroft					$check = 0;
1492f16fa28fSAndy Whitcroft				}
1493f16fa28fSAndy Whitcroft
14949bd49efeSAndy Whitcroft				# Ignore:
14959bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
14969bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
14979bd49efeSAndy Whitcroft				#  3) labels.
1498740504c6SAndy Whitcroft				if ($continuation ||
1499740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
15009bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
15019bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1502740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
15039bd49efeSAndy Whitcroft					$s =~ s/^.*?\n//;
15049bd49efeSAndy Whitcroft					$cond_lines++;
15059bd49efeSAndy Whitcroft				}
15064d001e4dSAndy Whitcroft			}
15074d001e4dSAndy Whitcroft
15084d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
15094d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
15104d001e4dSAndy Whitcroft
15114d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
15124d001e4dSAndy Whitcroft			# this is not this patch's fault.
15134d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
15144d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
15154d001e4dSAndy Whitcroft				$check = 0;
15164d001e4dSAndy Whitcroft			}
15174d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
15184d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
15194d001e4dSAndy Whitcroft			}
15204d001e4dSAndy Whitcroft
15219bd49efeSAndy Whitcroft			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
15224d001e4dSAndy Whitcroft
15234d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
15244d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
15254d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
15264d001e4dSAndy Whitcroft			}
15274d001e4dSAndy Whitcroft		}
15284d001e4dSAndy Whitcroft
15296c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
15306c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
15311f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
15321f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
15336c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1534c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1535c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1536cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1537cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
15381f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1539c2fdda0dSAndy Whitcroft		}
15406c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
15416c72ffaaSAndy Whitcroft
154200df344fSAndy Whitcroft#ignore lines not being added
154300df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
154400df344fSAndy Whitcroft
1545653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
15467429c690SAndy Whitcroft		if ($dbg_type) {
15477429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
15487429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
15497429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
15507429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
15517429c690SAndy Whitcroft			}
1552653d4876SAndy Whitcroft			next;
1553653d4876SAndy Whitcroft		}
1554a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1555a1ef277eSAndy Whitcroft		if ($dbg_attr) {
1556a1ef277eSAndy Whitcroft			if ($line =~ /^.\s*$Attribute\s*$/) {
1557a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
1558a1ef277eSAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1559a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1560a1ef277eSAndy Whitcroft			}
1561a1ef277eSAndy Whitcroft			next;
1562a1ef277eSAndy Whitcroft		}
1563653d4876SAndy Whitcroft
1564f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1565f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1566f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1567773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1568f0a594c1SAndy Whitcroft		}
1569f0a594c1SAndy Whitcroft
157000df344fSAndy Whitcroft#
157100df344fSAndy Whitcroft# Checks which are anchored on the added line.
157200df344fSAndy Whitcroft#
157300df344fSAndy Whitcroft
1574653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1575c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1576653d4876SAndy Whitcroft			my $path = $1;
1577653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1578de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1579de7d4f0eSAndy Whitcroft					$herecurr);
1580653d4876SAndy Whitcroft			}
1581653d4876SAndy Whitcroft		}
1582653d4876SAndy Whitcroft
158300df344fSAndy Whitcroft# no C99 // comments
158400df344fSAndy Whitcroft		if ($line =~ m{//}) {
1585de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
158600df344fSAndy Whitcroft		}
158700df344fSAndy Whitcroft		# Remove C99 comments.
15880a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
15896c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
15900a920b5bSAndy Whitcroft
15910a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1592653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1593653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1594653d4876SAndy Whitcroft			my $name = $1;
159548012058SAndy Whitcroft			if ($prevline !~ /(?:
159648012058SAndy Whitcroft				^.}|
159748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
159848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
159948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
160048012058SAndy Whitcroft				^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
160148012058SAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
160248012058SAndy Whitcroft			    )/x) {
1603de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
16040a920b5bSAndy Whitcroft			}
16050a920b5bSAndy Whitcroft		}
16060a920b5bSAndy Whitcroft
1607f0a594c1SAndy Whitcroft# check for external initialisers.
1608c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1609f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1610f0a594c1SAndy Whitcroft				$herecurr);
1611f0a594c1SAndy Whitcroft		}
16120a920b5bSAndy Whitcroft# check for static initialisers.
16139c9ba34eSAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1614de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1615de7d4f0eSAndy Whitcroft				$herecurr);
16160a920b5bSAndy Whitcroft		}
16170a920b5bSAndy Whitcroft
1618653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1619653d4876SAndy Whitcroft# make sense.
1620653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
16219c0ca6f9SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1622c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
16238ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1624653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1625de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
16260a920b5bSAndy Whitcroft		}
16270a920b5bSAndy Whitcroft
16280a920b5bSAndy Whitcroft# * goes on variable not on type
162965863862SAndy Whitcroft		# (char*[ const])
163065863862SAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
163165863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1632d8aaf121SAndy Whitcroft
163365863862SAndy Whitcroft			# Should start with a space.
163465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
163565863862SAndy Whitcroft			# Should not end with a space.
163665863862SAndy Whitcroft			$to =~ s/\s+$//;
163765863862SAndy Whitcroft			# '*'s should not have spaces between.
163865863862SAndy Whitcroft			while ($to =~ s/(.)\s\*/$1\*/) {
163965863862SAndy Whitcroft			}
1640d8aaf121SAndy Whitcroft
164165863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
164265863862SAndy Whitcroft			if ($from ne $to) {
164365863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
164465863862SAndy Whitcroft			}
164565863862SAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
164665863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1647d8aaf121SAndy Whitcroft
164865863862SAndy Whitcroft			# Should start with a space.
164965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
165065863862SAndy Whitcroft			# Should not end with a space.
165165863862SAndy Whitcroft			$to =~ s/\s+$//;
165265863862SAndy Whitcroft			# '*'s should not have spaces between.
165365863862SAndy Whitcroft			while ($to =~ s/(.)\s\*/$1\*/) {
165465863862SAndy Whitcroft			}
165565863862SAndy Whitcroft			# Modifiers should have spaces.
165665863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
165765863862SAndy Whitcroft
165865863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
165965863862SAndy Whitcroft			if ($from ne $to) {
166065863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
166165863862SAndy Whitcroft			}
16620a920b5bSAndy Whitcroft		}
16630a920b5bSAndy Whitcroft
16640a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
16650a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
16660a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
16670a920b5bSAndy Whitcroft# 			print "$herecurr";
16680a920b5bSAndy Whitcroft# 			$clean = 0;
16690a920b5bSAndy Whitcroft# 		}
16700a920b5bSAndy Whitcroft
16718905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1672c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
16738905a67cSAndy Whitcroft		}
16748905a67cSAndy Whitcroft
167500df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
167600df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
167700df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
167800df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
167900df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1680f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
168100df344fSAndy Whitcroft			my $ok = 0;
168200df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
168300df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
168400df344fSAndy Whitcroft				# we have a preceeding printk if it ends
168500df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
168600df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
168700df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
168800df344fSAndy Whitcroft						$ok = 1;
168900df344fSAndy Whitcroft					}
169000df344fSAndy Whitcroft					last;
169100df344fSAndy Whitcroft				}
169200df344fSAndy Whitcroft			}
169300df344fSAndy Whitcroft			if ($ok == 0) {
1694de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
16950a920b5bSAndy Whitcroft			}
169600df344fSAndy Whitcroft		}
16970a920b5bSAndy Whitcroft
1698653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1699653d4876SAndy Whitcroft# or if closed on same line
1700c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1701c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1702de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
17030a920b5bSAndy Whitcroft		}
1704653d4876SAndy Whitcroft
17058905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
17068905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
17078905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
17088905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
17098905a67cSAndy Whitcroft		}
17108905a67cSAndy Whitcroft
17118d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
17128d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1713fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1714fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
17158d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
17168d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
17178d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1718fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1719fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
17208d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
17218d31cfceSAndy Whitcroft			}
17228d31cfceSAndy Whitcroft		}
17238d31cfceSAndy Whitcroft
1724f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
17256c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1726c2fdda0dSAndy Whitcroft			my $name = $1;
1727773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1728773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1729c2fdda0dSAndy Whitcroft
1730c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1731773647a0SAndy Whitcroft			if ($name =~ /^(?:
1732773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1733773647a0SAndy Whitcroft				volatile|__volatile__|
1734773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1735773647a0SAndy Whitcroft				asm|__asm__)$/x)
1736773647a0SAndy Whitcroft			{
1737c2fdda0dSAndy Whitcroft
1738c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1739c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1740c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1741c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1742773647a0SAndy Whitcroft
1743773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1744c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1745c2fdda0dSAndy Whitcroft
1746c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1747c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1748773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1749c2fdda0dSAndy Whitcroft
1750c2fdda0dSAndy Whitcroft			} else {
1751773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1752f0a594c1SAndy Whitcroft			}
17536c72ffaaSAndy Whitcroft		}
1754653d4876SAndy Whitcroft# Check operator spacing.
17550a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
17569c0ca6f9SAndy Whitcroft			my $ops = qr{
17579c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
17589c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
17599c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
17601f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
17611f65f947SAndy Whitcroft				\?|:
17629c0ca6f9SAndy Whitcroft			}x;
1763cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
176400df344fSAndy Whitcroft			my $off = 0;
17656c72ffaaSAndy Whitcroft
17666c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
17676c72ffaaSAndy Whitcroft
17680a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
17694a0df2efSAndy Whitcroft				$off += length($elements[$n]);
17704a0df2efSAndy Whitcroft
1771773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1772773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1773773647a0SAndy Whitcroft				my $cc = '';
1774773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1775773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1776773647a0SAndy Whitcroft				}
1777773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1778773647a0SAndy Whitcroft
17794a0df2efSAndy Whitcroft				my $a = '';
17804a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
17814a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1782cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
17834a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
17844a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1785773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
17864a0df2efSAndy Whitcroft
17870a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
17884a0df2efSAndy Whitcroft
17894a0df2efSAndy Whitcroft				my $c = '';
17900a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
17914a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
17924a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1793cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
17944a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
17954a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
179622f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
17974a0df2efSAndy Whitcroft				} else {
17984a0df2efSAndy Whitcroft					$c = 'E';
17990a920b5bSAndy Whitcroft				}
18000a920b5bSAndy Whitcroft
18014a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
18024a0df2efSAndy Whitcroft
18034a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
18044a0df2efSAndy Whitcroft
18056c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1806de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
18070a920b5bSAndy Whitcroft
180874048ed8SAndy Whitcroft				# Pull out the value of this operator.
18096c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
18100a920b5bSAndy Whitcroft
18111f65f947SAndy Whitcroft				# Get the full operator variant.
18121f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
18131f65f947SAndy Whitcroft
181413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
181513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
181613214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
181713214adfSAndy Whitcroft
1818cf655043SAndy Whitcroft#				# Ignore comments
1819cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
182013214adfSAndy Whitcroft
1821d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
182213214adfSAndy Whitcroft				} elsif ($op eq ';') {
1823cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1824cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1825773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1826d8aaf121SAndy Whitcroft					}
1827d8aaf121SAndy Whitcroft
1828d8aaf121SAndy Whitcroft				# // is a comment
1829d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
18300a920b5bSAndy Whitcroft
18311f65f947SAndy Whitcroft				# No spaces for:
18321f65f947SAndy Whitcroft				#   ->
18331f65f947SAndy Whitcroft				#   :   when part of a bitfield
18341f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
18354a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1836773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
18370a920b5bSAndy Whitcroft					}
18380a920b5bSAndy Whitcroft
18390a920b5bSAndy Whitcroft				# , must have a space on the right.
18400a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1841cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1842773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
18430a920b5bSAndy Whitcroft					}
18440a920b5bSAndy Whitcroft
18459c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
184674048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
18479c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
18489c0ca6f9SAndy Whitcroft
18499c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
18509c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
18519c0ca6f9SAndy Whitcroft				# unary operator, or a cast
18529c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
185374048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
18540d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1855cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1856773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
18570a920b5bSAndy Whitcroft					}
1858171ae1a4SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*const\b/) {
1859171ae1a4SAndy Whitcroft						# A unary '*' may be const
1860171ae1a4SAndy Whitcroft
1861171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1862773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
18630a920b5bSAndy Whitcroft					}
18640a920b5bSAndy Whitcroft
18650a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
18660a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1867773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1868773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
18690a920b5bSAndy Whitcroft					}
1870773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1871773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1872773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1873653d4876SAndy Whitcroft					}
1874773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1875773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1876773647a0SAndy Whitcroft					}
1877773647a0SAndy Whitcroft
18780a920b5bSAndy Whitcroft
18790a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
18809c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
18819c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
18829c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1883c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1884c2fdda0dSAndy Whitcroft					 $op eq '%')
18850a920b5bSAndy Whitcroft				{
1886773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1887de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1888de7d4f0eSAndy Whitcroft							$hereptr);
18890a920b5bSAndy Whitcroft					}
18900a920b5bSAndy Whitcroft
18911f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
18921f65f947SAndy Whitcroft				# terminating a case value or a label.
18931f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
18941f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
18951f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
18961f65f947SAndy Whitcroft					}
18971f65f947SAndy Whitcroft
18980a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1899cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
19001f65f947SAndy Whitcroft					my $ok = 0;
19011f65f947SAndy Whitcroft
190222f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
19031f65f947SAndy Whitcroft					if (($op eq '<' &&
19041f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
19051f65f947SAndy Whitcroft					    ($op eq '>' &&
19061f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
19071f65f947SAndy Whitcroft					{
19081f65f947SAndy Whitcroft					    	$ok = 1;
19091f65f947SAndy Whitcroft					}
19101f65f947SAndy Whitcroft
19111f65f947SAndy Whitcroft					# Ignore ?:
19121f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
19131f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
19141f65f947SAndy Whitcroft					    	$ok = 1;
19151f65f947SAndy Whitcroft					}
19161f65f947SAndy Whitcroft
19171f65f947SAndy Whitcroft					if ($ok == 0) {
1918773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
19190a920b5bSAndy Whitcroft					}
192022f2a2efSAndy Whitcroft				}
19214a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
19220a920b5bSAndy Whitcroft			}
19230a920b5bSAndy Whitcroft		}
19240a920b5bSAndy Whitcroft
1925f0a594c1SAndy Whitcroft# check for multiple assignments
1926f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
19276c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
1928f0a594c1SAndy Whitcroft		}
1929f0a594c1SAndy Whitcroft
193022f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
193122f2a2efSAndy Whitcroft## # continuation.
193222f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
193322f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
193422f2a2efSAndy Whitcroft##
193522f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
193622f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
193722f2a2efSAndy Whitcroft## 			my $ln = $line;
193822f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
193922f2a2efSAndy Whitcroft## 			}
194022f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
194122f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
194222f2a2efSAndy Whitcroft## 			}
194322f2a2efSAndy Whitcroft## 		}
1944f0a594c1SAndy Whitcroft
19450a920b5bSAndy Whitcroft#need space before brace following if, while, etc
194622f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
194722f2a2efSAndy Whitcroft		    $line =~ /do{/) {
1948773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
1949de7d4f0eSAndy Whitcroft		}
1950de7d4f0eSAndy Whitcroft
1951de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
1952de7d4f0eSAndy Whitcroft# on the line
1953de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1954773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
19550a920b5bSAndy Whitcroft		}
19560a920b5bSAndy Whitcroft
195722f2a2efSAndy Whitcroft# check spacing on square brackets
195822f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1959773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
196022f2a2efSAndy Whitcroft		}
196122f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
1962773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
196322f2a2efSAndy Whitcroft		}
196422f2a2efSAndy Whitcroft
1965c45dcabdSAndy Whitcroft# check spacing on parentheses
19669c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
19679c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
1968773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
196922f2a2efSAndy Whitcroft		}
197013214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1971c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
1972c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
1973773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
197422f2a2efSAndy Whitcroft		}
197522f2a2efSAndy Whitcroft
19760a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
19774a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
19780a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1979de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
19800a920b5bSAndy Whitcroft		}
19810a920b5bSAndy Whitcroft
1982c45dcabdSAndy Whitcroft# Return is not a function.
1983c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1984c45dcabdSAndy Whitcroft			my $spacing = $1;
1985c45dcabdSAndy Whitcroft			my $value = $2;
1986c45dcabdSAndy Whitcroft
198786f9d059SAndy Whitcroft			# Flatten any parentheses
1988fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
198986f9d059SAndy Whitcroft			while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) {
1990c45dcabdSAndy Whitcroft			}
1991c45dcabdSAndy Whitcroft
1992c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1993c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1994c45dcabdSAndy Whitcroft
1995c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
1996c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
1997c45dcabdSAndy Whitcroft			}
1998c45dcabdSAndy Whitcroft		}
1999c45dcabdSAndy Whitcroft
20000a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
20014a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2002773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
20030a920b5bSAndy Whitcroft		}
20040a920b5bSAndy Whitcroft
2005f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2006f5fe35ddSAndy Whitcroft# statements after the conditional.
2007170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2008170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2009170d3a22SAndy Whitcroft						$remain_next, $off_next);
2010170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2011170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2012170d3a22SAndy Whitcroft
2013170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2014170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2015170d3a22SAndy Whitcroft				# then count those as offsets.
2016170d3a22SAndy Whitcroft				my ($whitespace) =
2017170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2018170d3a22SAndy Whitcroft				my $offset =
2019170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2020170d3a22SAndy Whitcroft
2021170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2022170d3a22SAndy Whitcroft								$offset} = 1;
2023170d3a22SAndy Whitcroft			}
2024170d3a22SAndy Whitcroft		}
2025170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2026170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2027171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
20288905a67cSAndy Whitcroft
20298905a67cSAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
2030c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
20318905a67cSAndy Whitcroft			}
20328905a67cSAndy Whitcroft
20338905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
20348905a67cSAndy Whitcroft			# conditional.
2035773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
20368905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
203713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
203853210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
203953210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2040773647a0SAndy Whitcroft			{
2041bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2042bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2043bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
2044bb44ad39SAndy Whitcroft
2045bb44ad39SAndy Whitcroft				my $stat_real = raw_line($linenr, $cond_lines);
2046bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2047bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2048bb44ad39SAndy Whitcroft				}
2049bb44ad39SAndy Whitcroft
2050bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
20518905a67cSAndy Whitcroft			}
20528905a67cSAndy Whitcroft		}
20538905a67cSAndy Whitcroft
205413214adfSAndy Whitcroft# Check for bitwise tests written as boolean
205513214adfSAndy Whitcroft		if ($line =~ /
205613214adfSAndy Whitcroft			(?:
205713214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
205813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
205913214adfSAndy Whitcroft				(?:\&\&|\|\|)
206013214adfSAndy Whitcroft			|
206113214adfSAndy Whitcroft				(?:\&\&|\|\|)
206213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
206313214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
206413214adfSAndy Whitcroft			)/x)
206513214adfSAndy Whitcroft		{
206613214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
206713214adfSAndy Whitcroft		}
206813214adfSAndy Whitcroft
20698905a67cSAndy Whitcroft# if and else should not have general statements after it
207013214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
207113214adfSAndy Whitcroft			my $s = $1;
207213214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
207313214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
20748905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
20750a920b5bSAndy Whitcroft			}
207613214adfSAndy Whitcroft		}
2077a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2078a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2079a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
20803fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2081a1080bf8SAndy Whitcroft			\s*return\s+
2082a1080bf8SAndy Whitcroft		    )/xg)
2083a1080bf8SAndy Whitcroft		{
2084a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2085a1080bf8SAndy Whitcroft		}
20860a920b5bSAndy Whitcroft
20870a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
20880a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
20890a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
20900a920b5bSAndy Whitcroft						$previndent == $indent) {
2091de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
20920a920b5bSAndy Whitcroft		}
20930a920b5bSAndy Whitcroft
2094c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2095c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2096c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2097c2fdda0dSAndy Whitcroft
2098c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2099c2fdda0dSAndy Whitcroft			# conditional.
2100773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2101c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2102c2fdda0dSAndy Whitcroft
2103c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2104c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2105c2fdda0dSAndy Whitcroft			}
2106c2fdda0dSAndy Whitcroft		}
2107c2fdda0dSAndy Whitcroft
21080a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
21090a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
21100a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
21110a920b5bSAndy Whitcroft#		    print "$herecurr";
21120a920b5bSAndy Whitcroft#		    $clean = 0;
21130a920b5bSAndy Whitcroft#		}
21140a920b5bSAndy Whitcroft
21150a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2116c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2117de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
21180a920b5bSAndy Whitcroft		}
21190a920b5bSAndy Whitcroft
2120653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2121c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2122e09dec48SAndy Whitcroft			my $file = "$1.h";
2123e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2124e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2125e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2126c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2127c45dcabdSAndy Whitcroft			{
2128e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2129e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2130e09dec48SAndy Whitcroft				} else {
2131e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2132e09dec48SAndy Whitcroft				}
21330a920b5bSAndy Whitcroft			}
21340a920b5bSAndy Whitcroft		}
21350a920b5bSAndy Whitcroft
2136653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2137653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2138cf655043SAndy Whitcroft# in a known good container
2139b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2140b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2141d8aaf121SAndy Whitcroft			my $ln = $linenr;
2142d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2143c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2144c45dcabdSAndy Whitcroft			my $ctx = '';
2145653d4876SAndy Whitcroft
2146c45dcabdSAndy Whitcroft			my $args = defined($1);
2147de7d4f0eSAndy Whitcroft
2148c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2149c45dcabdSAndy Whitcroft			# search to that.
2150c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2151c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2152c45dcabdSAndy Whitcroft			{
2153c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2154a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2155c45dcabdSAndy Whitcroft				$ln++;
2156de7d4f0eSAndy Whitcroft			}
2157c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2158d8aaf121SAndy Whitcroft
2159c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2160c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2161c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2162a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2163c45dcabdSAndy Whitcroft
2164c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2165c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2166c45dcabdSAndy Whitcroft			$rest = '';
2167636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2168636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2169a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2170c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2171c45dcabdSAndy Whitcroft					$cnt--;
2172a3bb97a7SAndy Whitcroft				}
2173a3bb97a7SAndy Whitcroft				$ln++;
2174c45dcabdSAndy Whitcroft				$off = 0;
2175c45dcabdSAndy Whitcroft			}
2176c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2177c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2178c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2179c45dcabdSAndy Whitcroft
2180c45dcabdSAndy Whitcroft			# Clean up the original statement.
2181c45dcabdSAndy Whitcroft			if ($args) {
2182c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2183d8aaf121SAndy Whitcroft			} else {
2184c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2185c45dcabdSAndy Whitcroft			}
2186292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2187c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2188c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2189c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2190c45dcabdSAndy Whitcroft
2191c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2192bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2193bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2194bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2195bf30d6edSAndy Whitcroft			{
2196c45dcabdSAndy Whitcroft			}
2197c45dcabdSAndy Whitcroft
2198c45dcabdSAndy Whitcroft			my $exceptions = qr{
2199c45dcabdSAndy Whitcroft				$Declare|
2200c45dcabdSAndy Whitcroft				module_param_named|
2201c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2202c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2203c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2204383099fdSAndy Whitcroft				__typeof__\(|
2205383099fdSAndy Whitcroft				\.$Ident\s*=\s*
2206c45dcabdSAndy Whitcroft			}x;
2207383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2208c45dcabdSAndy Whitcroft			if ($rest ne '') {
2209c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2210c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2211c45dcabdSAndy Whitcroft				{
2212c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2213c45dcabdSAndy Whitcroft				}
2214c45dcabdSAndy Whitcroft
2215c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2216c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2217c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2218c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2219b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2220c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2221c45dcabdSAndy Whitcroft				{
2222de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2223d8aaf121SAndy Whitcroft				}
22240a920b5bSAndy Whitcroft			}
2225653d4876SAndy Whitcroft		}
22260a920b5bSAndy Whitcroft
2227*080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2228*080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2229*080ba929SMike Frysinger#	.
2230*080ba929SMike Frysinger#	ALIGN(...)
2231*080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2232*080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2233*080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2234*080ba929SMike Frysinger		}
2235*080ba929SMike Frysinger
2236f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
223713214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
223813214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2239cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
224013214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2241cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2242cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
224313214adfSAndy Whitcroft				my $allowed = 0;
224413214adfSAndy Whitcroft				my $seen = 0;
2245773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2246cf655043SAndy Whitcroft				my $ln = $linenr - 1;
224713214adfSAndy Whitcroft				for my $chunk (@chunks) {
224813214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
224913214adfSAndy Whitcroft
2250773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2251773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2252773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2253773647a0SAndy Whitcroft
2254773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2255773647a0SAndy Whitcroft
2256773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2257773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2258773647a0SAndy Whitcroft
2259773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2260cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2261cf655043SAndy Whitcroft
2262773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
226313214adfSAndy Whitcroft
226413214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
226513214adfSAndy Whitcroft
2266cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2267cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2268cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
226913214adfSAndy Whitcroft						$allowed = 1;
227013214adfSAndy Whitcroft					}
227113214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2272cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
227313214adfSAndy Whitcroft						$allowed = 1;
227413214adfSAndy Whitcroft					}
2275cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2276cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
227713214adfSAndy Whitcroft						$allowed = 1;
227813214adfSAndy Whitcroft					}
227913214adfSAndy Whitcroft				}
228013214adfSAndy Whitcroft				if ($seen && !$allowed) {
2281cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
228213214adfSAndy Whitcroft				}
228313214adfSAndy Whitcroft			}
228413214adfSAndy Whitcroft		}
2285773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
228613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2287cf655043SAndy Whitcroft			my $allowed = 0;
2288f0a594c1SAndy Whitcroft
2289cf655043SAndy Whitcroft			# Check the pre-context.
2290cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2291cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2292cf655043SAndy Whitcroft				$allowed = 1;
2293f0a594c1SAndy Whitcroft			}
2294773647a0SAndy Whitcroft
2295773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2296773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2297773647a0SAndy Whitcroft
2298cf655043SAndy Whitcroft			# Check the condition.
2299cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2300773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2301cf655043SAndy Whitcroft			if (defined $cond) {
2302773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2303cf655043SAndy Whitcroft			}
2304cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2305cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2306cf655043SAndy Whitcroft				$allowed = 1;
2307cf655043SAndy Whitcroft			}
2308cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2309cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2310cf655043SAndy Whitcroft				$allowed = 1;
2311cf655043SAndy Whitcroft			}
2312cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2313cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2314cf655043SAndy Whitcroft				$allowed = 1;
2315cf655043SAndy Whitcroft			}
2316cf655043SAndy Whitcroft			# Check the post-context.
2317cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2318cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2319cf655043SAndy Whitcroft				if (defined $cond) {
2320773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2321cf655043SAndy Whitcroft				}
2322cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2323cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2324cf655043SAndy Whitcroft					$allowed = 1;
2325cf655043SAndy Whitcroft				}
2326cf655043SAndy Whitcroft			}
2327cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2328cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2329f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2330cf655043SAndy Whitcroft
2331f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2332f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2333cf655043SAndy Whitcroft				}
2334cf655043SAndy Whitcroft
2335cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2336f0a594c1SAndy Whitcroft			}
2337f0a594c1SAndy Whitcroft		}
2338f0a594c1SAndy Whitcroft
2339653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
23404a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2341c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2342de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
23430a920b5bSAndy Whitcroft			}
23440a920b5bSAndy Whitcroft		}
23450a920b5bSAndy Whitcroft
23464a0df2efSAndy Whitcroft# don't use deprecated functions
23474a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
234800df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2349de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
23504a0df2efSAndy Whitcroft			}
23514a0df2efSAndy Whitcroft		}
23524a0df2efSAndy Whitcroft
23534a0df2efSAndy Whitcroft# no volatiles please
23546c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
23556c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2356de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
23574a0df2efSAndy Whitcroft		}
23584a0df2efSAndy Whitcroft
23599c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
23609c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
23619c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
23629c0ca6f9SAndy Whitcroft		}
23639c0ca6f9SAndy Whitcroft
236400df344fSAndy Whitcroft# warn about #if 0
2365c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2366de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2367de7d4f0eSAndy Whitcroft				$herecurr);
23684a0df2efSAndy Whitcroft		}
23694a0df2efSAndy Whitcroft
2370f0a594c1SAndy Whitcroft# check for needless kfree() checks
2371f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2372f0a594c1SAndy Whitcroft			my $expr = $1;
2373f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
23743c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2375f0a594c1SAndy Whitcroft			}
2376f0a594c1SAndy Whitcroft		}
23774c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
23784c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
23794c432a8fSGreg Kroah-Hartman			my $expr = $1;
23804c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
23814c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
23824c432a8fSGreg Kroah-Hartman			}
23834c432a8fSGreg Kroah-Hartman		}
2384f0a594c1SAndy Whitcroft
238500df344fSAndy Whitcroft# warn about #ifdefs in C files
2386c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
238700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
238800df344fSAndy Whitcroft#			print "$herecurr";
238900df344fSAndy Whitcroft#			$clean = 0;
239000df344fSAndy Whitcroft#		}
239100df344fSAndy Whitcroft
239222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2393c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
239422f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
239522f2a2efSAndy Whitcroft		}
239622f2a2efSAndy Whitcroft
23974a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2398171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2399171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
24004a0df2efSAndy Whitcroft			my $which = $1;
24014a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2402de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
24034a0df2efSAndy Whitcroft			}
24044a0df2efSAndy Whitcroft		}
24054a0df2efSAndy Whitcroft# check for memory barriers without a comment.
24064a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
24074a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2408de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
24094a0df2efSAndy Whitcroft			}
24104a0df2efSAndy Whitcroft		}
24114a0df2efSAndy Whitcroft# check of hardware specific defines
2412c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2413de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
24140a920b5bSAndy Whitcroft		}
2415653d4876SAndy Whitcroft
2416de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2417de7d4f0eSAndy Whitcroft# storage class and type.
24189c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
24199c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2420de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2421de7d4f0eSAndy Whitcroft		}
2422de7d4f0eSAndy Whitcroft
24238905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
24248905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
24258905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
24268905a67cSAndy Whitcroft		}
24278905a67cSAndy Whitcroft
2428de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2429171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2430c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2431171ae1a4SAndy Whitcroft		{
2432c45dcabdSAndy Whitcroft			my $function_name = $1;
2433c45dcabdSAndy Whitcroft			my $paren_space = $2;
2434171ae1a4SAndy Whitcroft
2435171ae1a4SAndy Whitcroft			my $s = $stat;
2436171ae1a4SAndy Whitcroft			if (defined $cond) {
2437171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2438171ae1a4SAndy Whitcroft			}
2439c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2440c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2441c45dcabdSAndy Whitcroft			{
2442de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2443de7d4f0eSAndy Whitcroft			}
2444de7d4f0eSAndy Whitcroft
2445171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2446171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2447171ae1a4SAndy Whitcroft			}
24489c9ba34eSAndy Whitcroft
24499c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
24509c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
24519c9ba34eSAndy Whitcroft		{
24529c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2453171ae1a4SAndy Whitcroft		}
2454171ae1a4SAndy Whitcroft
2455de7d4f0eSAndy Whitcroft# checks for new __setup's
2456de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2457de7d4f0eSAndy Whitcroft			my $name = $1;
2458de7d4f0eSAndy Whitcroft
2459de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2460de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2461de7d4f0eSAndy Whitcroft			}
2462653d4876SAndy Whitcroft		}
24639c0ca6f9SAndy Whitcroft
24649c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
24659c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
24669c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
24679c0ca6f9SAndy Whitcroft		}
246813214adfSAndy Whitcroft
246913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
247013214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
247113214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
247213214adfSAndy Whitcroft		}
2473773647a0SAndy Whitcroft
2474773647a0SAndy Whitcroft# check for semaphores used as mutexes
2475171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2476773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2477773647a0SAndy Whitcroft		}
2478773647a0SAndy Whitcroft# check for semaphores used as mutexes
2479171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2480773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2481773647a0SAndy Whitcroft		}
2482773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2483773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2484773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2485773647a0SAndy Whitcroft		}
2486f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2487f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2488f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2489f3db6639SMichael Ellerman		}
2490773647a0SAndy Whitcroft
2491773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2492773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2493773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2494c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2495c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2496171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2497171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2498171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2499773647a0SAndy Whitcroft		{
2500773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2501773647a0SAndy Whitcroft		}
25029c9ba34eSAndy Whitcroft
25039c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
25049c9ba34eSAndy Whitcroft		my $string;
25059c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
25069c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
25072a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
25089c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
25099c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
25109c9ba34eSAndy Whitcroft				last;
25119c9ba34eSAndy Whitcroft			}
25129c9ba34eSAndy Whitcroft		}
2513691d77b6SAndy Whitcroft
2514691d77b6SAndy Whitcroft# whine mightly about in_atomic
2515691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2516691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2517691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2518691d77b6SAndy Whitcroft			} else {
2519691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2520691d77b6SAndy Whitcroft			}
2521691d77b6SAndy Whitcroft		}
252213214adfSAndy Whitcroft	}
252313214adfSAndy Whitcroft
252413214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
252513214adfSAndy Whitcroft	# so just keep quiet.
252613214adfSAndy Whitcroft	if ($#rawlines == -1) {
252713214adfSAndy Whitcroft		exit(0);
25280a920b5bSAndy Whitcroft	}
25290a920b5bSAndy Whitcroft
25308905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
25318905a67cSAndy Whitcroft	# things that appear to be patches.
25328905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
25338905a67cSAndy Whitcroft		exit(0);
25348905a67cSAndy Whitcroft	}
25358905a67cSAndy Whitcroft
25368905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
25378905a67cSAndy Whitcroft	# just keep quiet.
25388905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
25398905a67cSAndy Whitcroft		exit(0);
25408905a67cSAndy Whitcroft	}
25418905a67cSAndy Whitcroft
25428905a67cSAndy Whitcroft	if (!$is_patch) {
2543de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
25440a920b5bSAndy Whitcroft	}
25450a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2546de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
25470a920b5bSAndy Whitcroft	}
25480a920b5bSAndy Whitcroft
2549f0a594c1SAndy Whitcroft	print report_dump();
255013214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
255113214adfSAndy Whitcroft		print "$filename " if ($summary_file);
25526c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
25536c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
25546c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
25558905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
25566c72ffaaSAndy Whitcroft	}
25578905a67cSAndy Whitcroft
25580a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2559c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
25600a920b5bSAndy Whitcroft	}
25610a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2562c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
25630a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
25640a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
25650a920b5bSAndy Whitcroft	}
256613214adfSAndy Whitcroft
25670a920b5bSAndy Whitcroft	return $clean;
25680a920b5bSAndy Whitcroft}
2569