xref: /linux-6.15/scripts/checkpatch.pl (revision ea71a0a0)
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
13bea5606dSAndy Whitcroftmy $V = '0.28';
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;
3177f5b10aSHannes Edermy $help = 0;
3277f5b10aSHannes Eder
3377f5b10aSHannes Edersub help {
3477f5b10aSHannes Eder	my ($exitcode) = @_;
3577f5b10aSHannes Eder
3677f5b10aSHannes Eder	print << "EOM";
3777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
3877f5b10aSHannes EderVersion: $V
3977f5b10aSHannes Eder
4077f5b10aSHannes EderOptions:
4177f5b10aSHannes Eder  -q, --quiet                quiet
4277f5b10aSHannes Eder  --no-tree                  run without a kernel tree
4377f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
4477f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
4577f5b10aSHannes Eder  --emacs                    emacs compile window format
4677f5b10aSHannes Eder  --terse                    one line per report
4777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
4877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
4977f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
5077f5b10aSHannes Eder  --no-summary               suppress the per-file summary
5177f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
5277f5b10aSHannes Eder  --summary-file             include the filename in summary
5377f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
5477f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
5577f5b10aSHannes Eder                             is all off)
5677f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
5777f5b10aSHannes Eder                             literally
5877f5b10aSHannes Eder  -h, --help, --version      display this help and exit
5977f5b10aSHannes Eder
6077f5b10aSHannes EderWhen FILE is - read standard input.
6177f5b10aSHannes EderEOM
6277f5b10aSHannes Eder
6377f5b10aSHannes Eder	exit($exitcode);
6477f5b10aSHannes Eder}
6577f5b10aSHannes Eder
660a920b5bSAndy WhitcroftGetOptions(
676c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
680a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
690a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
700a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
716c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
728905a67cSAndy Whitcroft	'terse!'	=> \$terse,
7377f5b10aSHannes Eder	'f|file!'	=> \$file,
746c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
756c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
766c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
778905a67cSAndy Whitcroft	'summary!'	=> \$summary,
788905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
7913214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
8013214adfSAndy Whitcroft
81c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
82773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
8377f5b10aSHannes Eder	'h|help'	=> \$help,
8477f5b10aSHannes Eder	'version'	=> \$help
8577f5b10aSHannes Eder) or help(1);
8677f5b10aSHannes Eder
8777f5b10aSHannes Ederhelp(0) if ($help);
880a920b5bSAndy Whitcroft
890a920b5bSAndy Whitcroftmy $exit = 0;
900a920b5bSAndy Whitcroft
910a920b5bSAndy Whitcroftif ($#ARGV < 0) {
9277f5b10aSHannes Eder	print "$P: no input files\n";
930a920b5bSAndy Whitcroft	exit(1);
940a920b5bSAndy Whitcroft}
950a920b5bSAndy Whitcroft
96c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
97c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
987429c690SAndy Whitcroftmy $dbg_type = 0;
99a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
100c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
10121caa13cSAndy Whitcroft	## no critic
10221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
10321caa13cSAndy Whitcroft	die "$@" if ($@);
104c2fdda0dSAndy Whitcroft}
105c2fdda0dSAndy Whitcroft
1068905a67cSAndy Whitcroftif ($terse) {
1078905a67cSAndy Whitcroft	$emacs = 1;
1088905a67cSAndy Whitcroft	$quiet++;
1098905a67cSAndy Whitcroft}
1108905a67cSAndy Whitcroft
1116c72ffaaSAndy Whitcroftif ($tree) {
1126c72ffaaSAndy Whitcroft	if (defined $root) {
1136c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
1146c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
1156c72ffaaSAndy Whitcroft		}
1166c72ffaaSAndy Whitcroft	} else {
1176c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
1186c72ffaaSAndy Whitcroft			$root = '.';
1196c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
1206c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
1216c72ffaaSAndy Whitcroft			$root = $1;
1226c72ffaaSAndy Whitcroft		}
1236c72ffaaSAndy Whitcroft	}
1246c72ffaaSAndy Whitcroft
1256c72ffaaSAndy Whitcroft	if (!defined $root) {
1260a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
1270a920b5bSAndy Whitcroft		exit(2);
1280a920b5bSAndy Whitcroft	}
1296c72ffaaSAndy Whitcroft}
1306c72ffaaSAndy Whitcroft
1316c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
1326c72ffaaSAndy Whitcroft
1336c72ffaaSAndy Whitcroftour $Ident       = qr{[A-Za-z_][A-Za-z\d_]*};
1346c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
1356c72ffaaSAndy Whitcroftour $Sparse	= qr{
1366c72ffaaSAndy Whitcroft			__user|
1376c72ffaaSAndy Whitcroft			__kernel|
1386c72ffaaSAndy Whitcroft			__force|
1396c72ffaaSAndy Whitcroft			__iomem|
1406c72ffaaSAndy Whitcroft			__must_check|
1416c72ffaaSAndy Whitcroft			__init_refok|
142417495edSAndy Whitcroft			__kprobes|
143417495edSAndy Whitcroft			__ref
1446c72ffaaSAndy Whitcroft		}x;
1456c72ffaaSAndy Whitcroftour $Attribute	= qr{
1466c72ffaaSAndy Whitcroft			const|
1476c72ffaaSAndy Whitcroft			__read_mostly|
1486c72ffaaSAndy Whitcroft			__kprobes|
14924e1d81aSAndy Whitcroft			__(?:mem|cpu|dev|)(?:initdata|init)|
15024e1d81aSAndy Whitcroft			____cacheline_aligned|
15124e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
1525fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
1535fe3af11SAndy Whitcroft			__weak
1546c72ffaaSAndy Whitcroft		  }x;
155c45dcabdSAndy Whitcroftour $Modifier;
1566c72ffaaSAndy Whitcroftour $Inline	= qr{inline|__always_inline|noinline};
1576c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
1586c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
1596c72ffaaSAndy Whitcroft
1606c72ffaaSAndy Whitcroftour $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
1616c72ffaaSAndy Whitcroftour $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
16286f9d059SAndy Whitcroftour $Compare    = qr{<=|>=|==|!=|<|>};
1636c72ffaaSAndy Whitcroftour $Operators	= qr{
1646c72ffaaSAndy Whitcroft			<=|>=|==|!=|
1656c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
166c2fdda0dSAndy Whitcroft			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1676c72ffaaSAndy Whitcroft		  }x;
1686c72ffaaSAndy Whitcroft
1698905a67cSAndy Whitcroftour $NonptrType;
1708905a67cSAndy Whitcroftour $Type;
1718905a67cSAndy Whitcroftour $Declare;
1728905a67cSAndy Whitcroft
173171ae1a4SAndy Whitcroftour $UTF8	= qr {
174171ae1a4SAndy Whitcroft	[\x09\x0A\x0D\x20-\x7E]              # ASCII
175171ae1a4SAndy Whitcroft	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
176171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
177171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
178171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
179171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
180171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
181171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
182171ae1a4SAndy Whitcroft}x;
183171ae1a4SAndy Whitcroft
1848ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x:
1858ed22cadSAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:\d|\d\d)|
1868ed22cadSAndy Whitcroft	atomic_t
1878ed22cadSAndy Whitcroft)};
1888ed22cadSAndy Whitcroft
1898905a67cSAndy Whitcroftour @typeList = (
1908905a67cSAndy Whitcroft	qr{void},
191c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?char},
192c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?short},
193c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?int},
194c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long},
195c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+int},
196c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long},
197c45dcabdSAndy Whitcroft	qr{(?:unsigned\s+)?long\s+long\s+int},
1988905a67cSAndy Whitcroft	qr{unsigned},
1998905a67cSAndy Whitcroft	qr{float},
2008905a67cSAndy Whitcroft	qr{double},
2018905a67cSAndy Whitcroft	qr{bool},
2028905a67cSAndy Whitcroft	qr{struct\s+$Ident},
2038905a67cSAndy Whitcroft	qr{union\s+$Ident},
2048905a67cSAndy Whitcroft	qr{enum\s+$Ident},
2058905a67cSAndy Whitcroft	qr{${Ident}_t},
2068905a67cSAndy Whitcroft	qr{${Ident}_handler},
2078905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
2088905a67cSAndy Whitcroft);
209c45dcabdSAndy Whitcroftour @modifierList = (
210c45dcabdSAndy Whitcroft	qr{fastcall},
211c45dcabdSAndy Whitcroft);
2128905a67cSAndy Whitcroft
2138905a67cSAndy Whitcroftsub build_types {
214d2172eb5SAndy Whitcroft	my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
215d2172eb5SAndy Whitcroft	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
216c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
2178905a67cSAndy Whitcroft	$NonptrType	= qr{
218d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
219cf655043SAndy Whitcroft			(?:
220c45dcabdSAndy Whitcroft				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
2218ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
222c45dcabdSAndy Whitcroft				(?:${all}\b)
223cf655043SAndy Whitcroft			)
224c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
2258905a67cSAndy Whitcroft		  }x;
2268905a67cSAndy Whitcroft	$Type	= qr{
227c45dcabdSAndy Whitcroft			$NonptrType
22865863862SAndy Whitcroft			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
229c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
2308905a67cSAndy Whitcroft		  }x;
2318905a67cSAndy Whitcroft	$Declare	= qr{(?:$Storage\s+)?$Type};
2328905a67cSAndy Whitcroft}
2338905a67cSAndy Whitcroftbuild_types();
2346c72ffaaSAndy Whitcroft
2356c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
2360a920b5bSAndy Whitcroft
2374a0df2efSAndy Whitcroftmy @dep_includes = ();
2384a0df2efSAndy Whitcroftmy @dep_functions = ();
2396c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt";
2406c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") {
24121caa13cSAndy Whitcroft	open(my $REMOVE, '<', "$root/$removal") ||
2426c72ffaaSAndy Whitcroft				die "$P: $removal: open failed - $!\n";
24321caa13cSAndy Whitcroft	while (<$REMOVE>) {
244f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
245f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
246f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
2474a0df2efSAndy Whitcroft					push(@dep_includes, $1);
2484a0df2efSAndy Whitcroft
249f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
250f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
251f0a594c1SAndy Whitcroft				}
2524a0df2efSAndy Whitcroft			}
2530a920b5bSAndy Whitcroft		}
2540a920b5bSAndy Whitcroft	}
25521caa13cSAndy Whitcroft	close($REMOVE);
2560a920b5bSAndy Whitcroft}
2570a920b5bSAndy Whitcroft
25800df344fSAndy Whitcroftmy @rawlines = ();
259c2fdda0dSAndy Whitcroftmy @lines = ();
260c2fdda0dSAndy Whitcroftmy $vname;
2616c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
26221caa13cSAndy Whitcroft	my $FILE;
2636c72ffaaSAndy Whitcroft	if ($file) {
26421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
2656c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
26621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
26721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
2686c72ffaaSAndy Whitcroft	} else {
26921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
2706c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
2716c72ffaaSAndy Whitcroft	}
272c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
273c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
274c2fdda0dSAndy Whitcroft	} else {
275c2fdda0dSAndy Whitcroft		$vname = $filename;
276c2fdda0dSAndy Whitcroft	}
27721caa13cSAndy Whitcroft	while (<$FILE>) {
2780a920b5bSAndy Whitcroft		chomp;
27900df344fSAndy Whitcroft		push(@rawlines, $_);
2806c72ffaaSAndy Whitcroft	}
28121caa13cSAndy Whitcroft	close($FILE);
282c2fdda0dSAndy Whitcroft	if (!process($filename)) {
2830a920b5bSAndy Whitcroft		$exit = 1;
2840a920b5bSAndy Whitcroft	}
28500df344fSAndy Whitcroft	@rawlines = ();
28613214adfSAndy Whitcroft	@lines = ();
2870a920b5bSAndy Whitcroft}
2880a920b5bSAndy Whitcroft
2890a920b5bSAndy Whitcroftexit($exit);
2900a920b5bSAndy Whitcroft
2910a920b5bSAndy Whitcroftsub top_of_kernel_tree {
2926c72ffaaSAndy Whitcroft	my ($root) = @_;
2936c72ffaaSAndy Whitcroft
2946c72ffaaSAndy Whitcroft	my @tree_check = (
2956c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
2966c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
2976c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
2986c72ffaaSAndy Whitcroft	);
2996c72ffaaSAndy Whitcroft
3006c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
3016c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
3020a920b5bSAndy Whitcroft			return 0;
3030a920b5bSAndy Whitcroft		}
3046c72ffaaSAndy Whitcroft	}
3056c72ffaaSAndy Whitcroft	return 1;
3066c72ffaaSAndy Whitcroft}
3070a920b5bSAndy Whitcroft
3080a920b5bSAndy Whitcroftsub expand_tabs {
3090a920b5bSAndy Whitcroft	my ($str) = @_;
3100a920b5bSAndy Whitcroft
3110a920b5bSAndy Whitcroft	my $res = '';
3120a920b5bSAndy Whitcroft	my $n = 0;
3130a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
3140a920b5bSAndy Whitcroft		if ($c eq "\t") {
3150a920b5bSAndy Whitcroft			$res .= ' ';
3160a920b5bSAndy Whitcroft			$n++;
3170a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
3180a920b5bSAndy Whitcroft				$res .= ' ';
3190a920b5bSAndy Whitcroft			}
3200a920b5bSAndy Whitcroft			next;
3210a920b5bSAndy Whitcroft		}
3220a920b5bSAndy Whitcroft		$res .= $c;
3230a920b5bSAndy Whitcroft		$n++;
3240a920b5bSAndy Whitcroft	}
3250a920b5bSAndy Whitcroft
3260a920b5bSAndy Whitcroft	return $res;
3270a920b5bSAndy Whitcroft}
3286c72ffaaSAndy Whitcroftsub copy_spacing {
329773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
3306c72ffaaSAndy Whitcroft	return $res;
3316c72ffaaSAndy Whitcroft}
3320a920b5bSAndy Whitcroft
3334a0df2efSAndy Whitcroftsub line_stats {
3344a0df2efSAndy Whitcroft	my ($line) = @_;
3354a0df2efSAndy Whitcroft
3364a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
3374a0df2efSAndy Whitcroft	$line =~ s/^.//;
3384a0df2efSAndy Whitcroft	$line = expand_tabs($line);
3394a0df2efSAndy Whitcroft
3404a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
3414a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
3424a0df2efSAndy Whitcroft
3434a0df2efSAndy Whitcroft	return (length($line), length($white));
3444a0df2efSAndy Whitcroft}
3454a0df2efSAndy Whitcroft
346773647a0SAndy Whitcroftmy $sanitise_quote = '';
347773647a0SAndy Whitcroft
348773647a0SAndy Whitcroftsub sanitise_line_reset {
349773647a0SAndy Whitcroft	my ($in_comment) = @_;
350773647a0SAndy Whitcroft
351773647a0SAndy Whitcroft	if ($in_comment) {
352773647a0SAndy Whitcroft		$sanitise_quote = '*/';
353773647a0SAndy Whitcroft	} else {
354773647a0SAndy Whitcroft		$sanitise_quote = '';
355773647a0SAndy Whitcroft	}
356773647a0SAndy Whitcroft}
35700df344fSAndy Whitcroftsub sanitise_line {
35800df344fSAndy Whitcroft	my ($line) = @_;
35900df344fSAndy Whitcroft
36000df344fSAndy Whitcroft	my $res = '';
36100df344fSAndy Whitcroft	my $l = '';
36200df344fSAndy Whitcroft
363c2fdda0dSAndy Whitcroft	my $qlen = 0;
364773647a0SAndy Whitcroft	my $off = 0;
365773647a0SAndy Whitcroft	my $c;
36600df344fSAndy Whitcroft
367773647a0SAndy Whitcroft	# Always copy over the diff marker.
368773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
369773647a0SAndy Whitcroft
370773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
371773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
372773647a0SAndy Whitcroft
373773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
374773647a0SAndy Whitcroft		# and end, all to $;.
375773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
376773647a0SAndy Whitcroft			$sanitise_quote = '*/';
377773647a0SAndy Whitcroft
378773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
379773647a0SAndy Whitcroft			$off++;
38000df344fSAndy Whitcroft			next;
381773647a0SAndy Whitcroft		}
38281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
383773647a0SAndy Whitcroft			$sanitise_quote = '';
384773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
385773647a0SAndy Whitcroft			$off++;
386773647a0SAndy Whitcroft			next;
387773647a0SAndy Whitcroft		}
388113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
389113f04a8SDaniel Walker			$sanitise_quote = '//';
390113f04a8SDaniel Walker
391113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
392113f04a8SDaniel Walker			$off++;
393113f04a8SDaniel Walker			next;
394113f04a8SDaniel Walker		}
395773647a0SAndy Whitcroft
396773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
397773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
398773647a0SAndy Whitcroft		    $c eq "\\") {
399773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
400773647a0SAndy Whitcroft			$off++;
401773647a0SAndy Whitcroft			next;
402773647a0SAndy Whitcroft		}
403773647a0SAndy Whitcroft		# Regular quotes.
404773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
405773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
406773647a0SAndy Whitcroft				$sanitise_quote = $c;
407773647a0SAndy Whitcroft
408773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
409773647a0SAndy Whitcroft				next;
410773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
411773647a0SAndy Whitcroft				$sanitise_quote = '';
41200df344fSAndy Whitcroft			}
41300df344fSAndy Whitcroft		}
414773647a0SAndy Whitcroft
415fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
416773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
417773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
418113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
419113f04a8SDaniel Walker			substr($res, $off, 1, $;);
420773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
421773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
42200df344fSAndy Whitcroft		} else {
423773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
42400df344fSAndy Whitcroft		}
425c2fdda0dSAndy Whitcroft	}
426c2fdda0dSAndy Whitcroft
427113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
428113f04a8SDaniel Walker		$sanitise_quote = '';
429113f04a8SDaniel Walker	}
430113f04a8SDaniel Walker
431c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
432c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
433c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
434c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
435c2fdda0dSAndy Whitcroft
436c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
437c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
438c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
439c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
440c2fdda0dSAndy Whitcroft	}
441c2fdda0dSAndy Whitcroft
44200df344fSAndy Whitcroft	return $res;
44300df344fSAndy Whitcroft}
44400df344fSAndy Whitcroft
4458905a67cSAndy Whitcroftsub ctx_statement_block {
4468905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
4478905a67cSAndy Whitcroft	my $line = $linenr - 1;
4488905a67cSAndy Whitcroft	my $blk = '';
4498905a67cSAndy Whitcroft	my $soff = $off;
4508905a67cSAndy Whitcroft	my $coff = $off - 1;
451773647a0SAndy Whitcroft	my $coff_set = 0;
4528905a67cSAndy Whitcroft
45313214adfSAndy Whitcroft	my $loff = 0;
45413214adfSAndy Whitcroft
4558905a67cSAndy Whitcroft	my $type = '';
4568905a67cSAndy Whitcroft	my $level = 0;
457a2750645SAndy Whitcroft	my @stack = ();
458cf655043SAndy Whitcroft	my $p;
4598905a67cSAndy Whitcroft	my $c;
4608905a67cSAndy Whitcroft	my $len = 0;
46113214adfSAndy Whitcroft
46213214adfSAndy Whitcroft	my $remainder;
4638905a67cSAndy Whitcroft	while (1) {
464a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
465a2750645SAndy Whitcroft
466773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
4678905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
4688905a67cSAndy Whitcroft		# context.
4698905a67cSAndy Whitcroft		if ($off >= $len) {
4708905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
471dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
472c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
4738905a67cSAndy Whitcroft				$remain--;
47413214adfSAndy Whitcroft				$loff = $len;
475c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
4768905a67cSAndy Whitcroft				$len = length($blk);
4778905a67cSAndy Whitcroft				$line++;
4788905a67cSAndy Whitcroft				last;
4798905a67cSAndy Whitcroft			}
4808905a67cSAndy Whitcroft			# Bail if there is no further context.
4818905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
48213214adfSAndy Whitcroft			if ($off >= $len) {
4838905a67cSAndy Whitcroft				last;
4848905a67cSAndy Whitcroft			}
4858905a67cSAndy Whitcroft		}
486cf655043SAndy Whitcroft		$p = $c;
4878905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
48813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
4898905a67cSAndy Whitcroft
490773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4914635f4fbSAndy Whitcroft
4924635f4fbSAndy Whitcroft		# Handle nested #if/#else.
4934635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
4944635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
4954635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
4964635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
4974635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
4984635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
4994635f4fbSAndy Whitcroft		}
5004635f4fbSAndy Whitcroft
5018905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
5028905a67cSAndy Whitcroft		# outermost level.
5038905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
5048905a67cSAndy Whitcroft			last;
5058905a67cSAndy Whitcroft		}
5068905a67cSAndy Whitcroft
50713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
508773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
509773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
510773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
511773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
512773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
513773647a0SAndy Whitcroft			$coff_set = 1;
514773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
515773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
51613214adfSAndy Whitcroft		}
51713214adfSAndy Whitcroft
5188905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
5198905a67cSAndy Whitcroft			$level++;
5208905a67cSAndy Whitcroft			$type = '(';
5218905a67cSAndy Whitcroft		}
5228905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
5238905a67cSAndy Whitcroft			$level--;
5248905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
5258905a67cSAndy Whitcroft
5268905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
5278905a67cSAndy Whitcroft				$coff = $off;
528773647a0SAndy Whitcroft				$coff_set = 1;
529773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
5308905a67cSAndy Whitcroft			}
5318905a67cSAndy Whitcroft		}
5328905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
5338905a67cSAndy Whitcroft			$level++;
5348905a67cSAndy Whitcroft			$type = '{';
5358905a67cSAndy Whitcroft		}
5368905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
5378905a67cSAndy Whitcroft			$level--;
5388905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
5398905a67cSAndy Whitcroft
5408905a67cSAndy Whitcroft			if ($level == 0) {
5418905a67cSAndy Whitcroft				last;
5428905a67cSAndy Whitcroft			}
5438905a67cSAndy Whitcroft		}
5448905a67cSAndy Whitcroft		$off++;
5458905a67cSAndy Whitcroft	}
546a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
54713214adfSAndy Whitcroft	if ($off == $len) {
548a3bb97a7SAndy Whitcroft		$loff = $len + 1;
54913214adfSAndy Whitcroft		$line++;
55013214adfSAndy Whitcroft		$remain--;
55113214adfSAndy Whitcroft	}
5528905a67cSAndy Whitcroft
5538905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
5548905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
5558905a67cSAndy Whitcroft
5568905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
5578905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
5588905a67cSAndy Whitcroft
559773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
56013214adfSAndy Whitcroft
56113214adfSAndy Whitcroft	return ($statement, $condition,
56213214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
56313214adfSAndy Whitcroft}
56413214adfSAndy Whitcroft
565cf655043SAndy Whitcroftsub statement_lines {
566cf655043SAndy Whitcroft	my ($stmt) = @_;
567cf655043SAndy Whitcroft
568cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
569cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
570cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
571cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
572cf655043SAndy Whitcroft
573cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
574cf655043SAndy Whitcroft
575cf655043SAndy Whitcroft	return $#stmt_lines + 2;
576cf655043SAndy Whitcroft}
577cf655043SAndy Whitcroft
578cf655043SAndy Whitcroftsub statement_rawlines {
579cf655043SAndy Whitcroft	my ($stmt) = @_;
580cf655043SAndy Whitcroft
581cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
582cf655043SAndy Whitcroft
583cf655043SAndy Whitcroft	return $#stmt_lines + 2;
584cf655043SAndy Whitcroft}
585cf655043SAndy Whitcroft
586cf655043SAndy Whitcroftsub statement_block_size {
587cf655043SAndy Whitcroft	my ($stmt) = @_;
588cf655043SAndy Whitcroft
589cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
590cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
591cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
592cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
593cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
594cf655043SAndy Whitcroft
595cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
596cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
597cf655043SAndy Whitcroft
598cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
599cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
600cf655043SAndy Whitcroft
601cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
602cf655043SAndy Whitcroft		return $stmt_lines;
603cf655043SAndy Whitcroft	} else {
604cf655043SAndy Whitcroft		return $stmt_statements;
605cf655043SAndy Whitcroft	}
606cf655043SAndy Whitcroft}
607cf655043SAndy Whitcroft
60813214adfSAndy Whitcroftsub ctx_statement_full {
60913214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
61013214adfSAndy Whitcroft	my ($statement, $condition, $level);
61113214adfSAndy Whitcroft
61213214adfSAndy Whitcroft	my (@chunks);
61313214adfSAndy Whitcroft
614cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
61513214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
61613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
617773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
61813214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
619cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
620cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
621cf655043SAndy Whitcroft	}
622cf655043SAndy Whitcroft
623cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
624cf655043SAndy Whitcroft	# could continue the statement.
625cf655043SAndy Whitcroft	for (;;) {
62613214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
62713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
628cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
629773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
630cf655043SAndy Whitcroft		#print "C: push\n";
631cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
63213214adfSAndy Whitcroft	}
63313214adfSAndy Whitcroft
63413214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
6358905a67cSAndy Whitcroft}
6368905a67cSAndy Whitcroft
6374a0df2efSAndy Whitcroftsub ctx_block_get {
638f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
6394a0df2efSAndy Whitcroft	my $line;
6404a0df2efSAndy Whitcroft	my $start = $linenr - 1;
6414a0df2efSAndy Whitcroft	my $blk = '';
6424a0df2efSAndy Whitcroft	my @o;
6434a0df2efSAndy Whitcroft	my @c;
6444a0df2efSAndy Whitcroft	my @res = ();
6454a0df2efSAndy Whitcroft
646f0a594c1SAndy Whitcroft	my $level = 0;
6474635f4fbSAndy Whitcroft	my @stack = ($level);
64800df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
64900df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
65000df344fSAndy Whitcroft		$remain--;
65100df344fSAndy Whitcroft
65200df344fSAndy Whitcroft		$blk .= $rawlines[$line];
6534635f4fbSAndy Whitcroft
6544635f4fbSAndy Whitcroft		# Handle nested #if/#else.
6554635f4fbSAndy Whitcroft		if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
6564635f4fbSAndy Whitcroft			push(@stack, $level);
6574635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
6584635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
6594635f4fbSAndy Whitcroft		} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
6604635f4fbSAndy Whitcroft			$level = pop(@stack);
6614635f4fbSAndy Whitcroft		}
6624635f4fbSAndy Whitcroft
663f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
664f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
665f0a594c1SAndy Whitcroft			if ($off > 0) {
666f0a594c1SAndy Whitcroft				$off--;
667f0a594c1SAndy Whitcroft				next;
668f0a594c1SAndy Whitcroft			}
6694a0df2efSAndy Whitcroft
670f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
671f0a594c1SAndy Whitcroft				$level--;
672f0a594c1SAndy Whitcroft				last if ($level == 0);
673f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
674f0a594c1SAndy Whitcroft				$level++;
675f0a594c1SAndy Whitcroft			}
676f0a594c1SAndy Whitcroft		}
6774a0df2efSAndy Whitcroft
678f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
67900df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
6804a0df2efSAndy Whitcroft		}
6814a0df2efSAndy Whitcroft
682f0a594c1SAndy Whitcroft		last if ($level == 0);
6834a0df2efSAndy Whitcroft	}
6844a0df2efSAndy Whitcroft
685f0a594c1SAndy Whitcroft	return ($level, @res);
6864a0df2efSAndy Whitcroft}
6874a0df2efSAndy Whitcroftsub ctx_block_outer {
6884a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6894a0df2efSAndy Whitcroft
690f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
691f0a594c1SAndy Whitcroft	return @r;
6924a0df2efSAndy Whitcroft}
6934a0df2efSAndy Whitcroftsub ctx_block {
6944a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
6954a0df2efSAndy Whitcroft
696f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
697f0a594c1SAndy Whitcroft	return @r;
698653d4876SAndy Whitcroft}
699653d4876SAndy Whitcroftsub ctx_statement {
700f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
701f0a594c1SAndy Whitcroft
702f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
703f0a594c1SAndy Whitcroft	return @r;
704f0a594c1SAndy Whitcroft}
705f0a594c1SAndy Whitcroftsub ctx_block_level {
706653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
707653d4876SAndy Whitcroft
708f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
7094a0df2efSAndy Whitcroft}
7109c0ca6f9SAndy Whitcroftsub ctx_statement_level {
7119c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
7129c0ca6f9SAndy Whitcroft
7139c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
7149c0ca6f9SAndy Whitcroft}
7154a0df2efSAndy Whitcroft
7164a0df2efSAndy Whitcroftsub ctx_locate_comment {
7174a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7184a0df2efSAndy Whitcroft
7194a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
720beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
7214a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
7224a0df2efSAndy Whitcroft
7234a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
7244a0df2efSAndy Whitcroft	# comment.
7254a0df2efSAndy Whitcroft	my $in_comment = 0;
7264a0df2efSAndy Whitcroft	$current_comment = '';
7274a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
72800df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
72900df344fSAndy Whitcroft		#warn "           $line\n";
7304a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
7314a0df2efSAndy Whitcroft			$in_comment = 1;
7324a0df2efSAndy Whitcroft		}
7334a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
7344a0df2efSAndy Whitcroft			$in_comment = 1;
7354a0df2efSAndy Whitcroft		}
7364a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
7374a0df2efSAndy Whitcroft			$current_comment = '';
7384a0df2efSAndy Whitcroft		}
7394a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
7404a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
7414a0df2efSAndy Whitcroft			$in_comment = 0;
7424a0df2efSAndy Whitcroft		}
7434a0df2efSAndy Whitcroft	}
7444a0df2efSAndy Whitcroft
7454a0df2efSAndy Whitcroft	chomp($current_comment);
7464a0df2efSAndy Whitcroft	return($current_comment);
7474a0df2efSAndy Whitcroft}
7484a0df2efSAndy Whitcroftsub ctx_has_comment {
7494a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
7504a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
7514a0df2efSAndy Whitcroft
75200df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
7534a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
7544a0df2efSAndy Whitcroft
7554a0df2efSAndy Whitcroft	return ($cmt ne '');
7564a0df2efSAndy Whitcroft}
7574a0df2efSAndy Whitcroft
7584d001e4dSAndy Whitcroftsub raw_line {
7594d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
7604d001e4dSAndy Whitcroft
7614d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
7624d001e4dSAndy Whitcroft	$cnt++;
7634d001e4dSAndy Whitcroft
7644d001e4dSAndy Whitcroft	my $line;
7654d001e4dSAndy Whitcroft	while ($cnt) {
7664d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
7674d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
7684d001e4dSAndy Whitcroft		$cnt--;
7694d001e4dSAndy Whitcroft	}
7704d001e4dSAndy Whitcroft
7714d001e4dSAndy Whitcroft	return $line;
7724d001e4dSAndy Whitcroft}
7734d001e4dSAndy Whitcroft
7740a920b5bSAndy Whitcroftsub cat_vet {
7750a920b5bSAndy Whitcroft	my ($vet) = @_;
7769c0ca6f9SAndy Whitcroft	my ($res, $coded);
7770a920b5bSAndy Whitcroft
7789c0ca6f9SAndy Whitcroft	$res = '';
7796c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
7806c72ffaaSAndy Whitcroft		$res .= $1;
7816c72ffaaSAndy Whitcroft		if ($2 ne '') {
7829c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
7836c72ffaaSAndy Whitcroft			$res .= $coded;
7846c72ffaaSAndy Whitcroft		}
7859c0ca6f9SAndy Whitcroft	}
7869c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
7870a920b5bSAndy Whitcroft
7889c0ca6f9SAndy Whitcroft	return $res;
7890a920b5bSAndy Whitcroft}
7900a920b5bSAndy Whitcroft
791c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
792cf655043SAndy Whitcroftmy $av_pending;
793c2fdda0dSAndy Whitcroftmy @av_paren_type;
7941f65f947SAndy Whitcroftmy $av_pend_colon;
795c2fdda0dSAndy Whitcroft
796c2fdda0dSAndy Whitcroftsub annotate_reset {
797c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
798cf655043SAndy Whitcroft	$av_pending = '_';
799cf655043SAndy Whitcroft	@av_paren_type = ('E');
8001f65f947SAndy Whitcroft	$av_pend_colon = 'O';
801c2fdda0dSAndy Whitcroft}
802c2fdda0dSAndy Whitcroft
8036c72ffaaSAndy Whitcroftsub annotate_values {
8046c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
8056c72ffaaSAndy Whitcroft
8066c72ffaaSAndy Whitcroft	my $res;
8071f65f947SAndy Whitcroft	my $var = '_' x length($stream);
8086c72ffaaSAndy Whitcroft	my $cur = $stream;
8096c72ffaaSAndy Whitcroft
810c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
8116c72ffaaSAndy Whitcroft
8126c72ffaaSAndy Whitcroft	while (length($cur)) {
813773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
814cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
815171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
8166c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
817c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
818c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
819cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
820c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
8216c72ffaaSAndy Whitcroft			}
8226c72ffaaSAndy Whitcroft
8238ea3eb9aSAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
824c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
8256c72ffaaSAndy Whitcroft			$type = 'T';
8266c72ffaaSAndy Whitcroft
827389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
828389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
829389a2fe5SAndy Whitcroft			$type = 'T';
830389a2fe5SAndy Whitcroft
831c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
832171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
833c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
834171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
835171ae1a4SAndy Whitcroft			if ($2 ne '') {
836cf655043SAndy Whitcroft				$av_pending = 'N';
837171ae1a4SAndy Whitcroft			}
838171ae1a4SAndy Whitcroft			$type = 'E';
839171ae1a4SAndy Whitcroft
840c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
841171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
842171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
843171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
8446c72ffaaSAndy Whitcroft
845c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
846cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
847c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
848cf655043SAndy Whitcroft
849cf655043SAndy Whitcroft			push(@av_paren_type, $type);
850cf655043SAndy Whitcroft			push(@av_paren_type, $type);
851171ae1a4SAndy Whitcroft			$type = 'E';
852cf655043SAndy Whitcroft
853c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
854cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
855cf655043SAndy Whitcroft			$av_preprocessor = 1;
856cf655043SAndy Whitcroft
857cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
858cf655043SAndy Whitcroft
859171ae1a4SAndy Whitcroft			$type = 'E';
860cf655043SAndy Whitcroft
861c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
862cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
863cf655043SAndy Whitcroft
864cf655043SAndy Whitcroft			$av_preprocessor = 1;
865cf655043SAndy Whitcroft
866cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
867cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
868cf655043SAndy Whitcroft			pop(@av_paren_type);
869cf655043SAndy Whitcroft			push(@av_paren_type, $type);
870171ae1a4SAndy Whitcroft			$type = 'E';
8716c72ffaaSAndy Whitcroft
8726c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
873c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
8746c72ffaaSAndy Whitcroft
875171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
876171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
877171ae1a4SAndy Whitcroft			$av_pending = $type;
878171ae1a4SAndy Whitcroft			$type = 'N';
879171ae1a4SAndy Whitcroft
8806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
881c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
8826c72ffaaSAndy Whitcroft			if (defined $2) {
883cf655043SAndy Whitcroft				$av_pending = 'V';
8846c72ffaaSAndy Whitcroft			}
8856c72ffaaSAndy Whitcroft			$type = 'N';
8866c72ffaaSAndy Whitcroft
88714b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
888c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
88914b111c1SAndy Whitcroft			$av_pending = 'E';
8906c72ffaaSAndy Whitcroft			$type = 'N';
8916c72ffaaSAndy Whitcroft
8921f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
8931f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
8941f65f947SAndy Whitcroft			$av_pend_colon = 'C';
8951f65f947SAndy Whitcroft			$type = 'N';
8961f65f947SAndy Whitcroft
89714b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
898c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
8996c72ffaaSAndy Whitcroft			$type = 'N';
9006c72ffaaSAndy Whitcroft
9016c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
902c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
903cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
904cf655043SAndy Whitcroft			$av_pending = '_';
9056c72ffaaSAndy Whitcroft			$type = 'N';
9066c72ffaaSAndy Whitcroft
9076c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
908cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
909cf655043SAndy Whitcroft			if ($new_type ne '_') {
910cf655043SAndy Whitcroft				$type = $new_type;
911c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
912c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
9136c72ffaaSAndy Whitcroft			} else {
914c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
9156c72ffaaSAndy Whitcroft			}
9166c72ffaaSAndy Whitcroft
917c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
918c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
919c8cb2ca3SAndy Whitcroft			$type = 'V';
920cf655043SAndy Whitcroft			$av_pending = 'V';
9216c72ffaaSAndy Whitcroft
9228e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
9238e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
9241f65f947SAndy Whitcroft				$av_pend_colon = 'B';
9258e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
9268e761b04SAndy Whitcroft				$av_pend_colon = 'L';
9271f65f947SAndy Whitcroft			}
9281f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
9291f65f947SAndy Whitcroft			$type = 'V';
9301f65f947SAndy Whitcroft
9316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
932c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
9336c72ffaaSAndy Whitcroft			$type = 'V';
9346c72ffaaSAndy Whitcroft
9356c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
936c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
9376c72ffaaSAndy Whitcroft			$type = 'N';
9386c72ffaaSAndy Whitcroft
939cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
940c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
94113214adfSAndy Whitcroft			$type = 'E';
9421f65f947SAndy Whitcroft			$av_pend_colon = 'O';
94313214adfSAndy Whitcroft
9448e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
9458e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
9468e761b04SAndy Whitcroft			$type = 'C';
9478e761b04SAndy Whitcroft
9481f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
9491f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
9501f65f947SAndy Whitcroft			$type = 'N';
9511f65f947SAndy Whitcroft
9521f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
9531f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
9541f65f947SAndy Whitcroft
9551f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
9561f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
9571f65f947SAndy Whitcroft				$type = 'E';
9581f65f947SAndy Whitcroft			} else {
9591f65f947SAndy Whitcroft				$type = 'N';
9601f65f947SAndy Whitcroft			}
9611f65f947SAndy Whitcroft			$av_pend_colon = 'O';
9621f65f947SAndy Whitcroft
9638e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
96413214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
9656c72ffaaSAndy Whitcroft			$type = 'N';
9666c72ffaaSAndy Whitcroft
9670d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
96874048ed8SAndy Whitcroft			my $variant;
96974048ed8SAndy Whitcroft
97074048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
97174048ed8SAndy Whitcroft			if ($type eq 'V') {
97274048ed8SAndy Whitcroft				$variant = 'B';
97374048ed8SAndy Whitcroft			} else {
97474048ed8SAndy Whitcroft				$variant = 'U';
97574048ed8SAndy Whitcroft			}
97674048ed8SAndy Whitcroft
97774048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
97874048ed8SAndy Whitcroft			$type = 'N';
97974048ed8SAndy Whitcroft
9806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
981c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
9826c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
9836c72ffaaSAndy Whitcroft				$type = 'N';
9846c72ffaaSAndy Whitcroft			}
9856c72ffaaSAndy Whitcroft
9866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
987c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
9886c72ffaaSAndy Whitcroft		}
9896c72ffaaSAndy Whitcroft		if (defined $1) {
9906c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
9916c72ffaaSAndy Whitcroft			$res .= $type x length($1);
9926c72ffaaSAndy Whitcroft		}
9936c72ffaaSAndy Whitcroft	}
9946c72ffaaSAndy Whitcroft
9951f65f947SAndy Whitcroft	return ($res, $var);
9966c72ffaaSAndy Whitcroft}
9976c72ffaaSAndy Whitcroft
9988905a67cSAndy Whitcroftsub possible {
99913214adfSAndy Whitcroft	my ($possible, $line) = @_;
10008905a67cSAndy Whitcroft
10010776e594SAndy Whitcroft	print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
10020776e594SAndy Whitcroft	if ($possible !~ /(?:
10030776e594SAndy Whitcroft		^(?:
10040776e594SAndy Whitcroft			$Modifier|
10050776e594SAndy Whitcroft			$Storage|
10060776e594SAndy Whitcroft			$Type|
10070776e594SAndy Whitcroft			DEFINE_\S+|
10080776e594SAndy Whitcroft			goto|
10090776e594SAndy Whitcroft			return|
10100776e594SAndy Whitcroft			case|
10110776e594SAndy Whitcroft			else|
10120776e594SAndy Whitcroft			asm|__asm__|
10130776e594SAndy Whitcroft			do
10140776e594SAndy Whitcroft		)$|
10150776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
10160776e594SAndy Whitcroft	    )/x) {
1017c45dcabdSAndy Whitcroft		# Check for modifiers.
1018c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1019c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1020c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1021c45dcabdSAndy Whitcroft
1022c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1023c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1024d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
1025d2506586SAndy Whitcroft				warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1026d2506586SAndy Whitcroft				push(@modifierList, $modifier);
1027d2506586SAndy Whitcroft			}
1028c45dcabdSAndy Whitcroft
1029c45dcabdSAndy Whitcroft		} else {
103013214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
10318905a67cSAndy Whitcroft			push(@typeList, $possible);
1032c45dcabdSAndy Whitcroft		}
10338905a67cSAndy Whitcroft		build_types();
10340776e594SAndy Whitcroft	} else {
10350776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
10368905a67cSAndy Whitcroft	}
10378905a67cSAndy Whitcroft}
10388905a67cSAndy Whitcroft
10396c72ffaaSAndy Whitcroftmy $prefix = '';
10406c72ffaaSAndy Whitcroft
1041f0a594c1SAndy Whitcroftsub report {
1042773647a0SAndy Whitcroft	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1043773647a0SAndy Whitcroft		return 0;
1044773647a0SAndy Whitcroft	}
10458905a67cSAndy Whitcroft	my $line = $prefix . $_[0];
10468905a67cSAndy Whitcroft
10478905a67cSAndy Whitcroft	$line = (split('\n', $line))[0] . "\n" if ($terse);
10488905a67cSAndy Whitcroft
104913214adfSAndy Whitcroft	push(our @report, $line);
1050773647a0SAndy Whitcroft
1051773647a0SAndy Whitcroft	return 1;
1052f0a594c1SAndy Whitcroft}
1053f0a594c1SAndy Whitcroftsub report_dump {
105413214adfSAndy Whitcroft	our @report;
1055f0a594c1SAndy Whitcroft}
1056de7d4f0eSAndy Whitcroftsub ERROR {
1057773647a0SAndy Whitcroft	if (report("ERROR: $_[0]\n")) {
1058de7d4f0eSAndy Whitcroft		our $clean = 0;
10596c72ffaaSAndy Whitcroft		our $cnt_error++;
1060de7d4f0eSAndy Whitcroft	}
1061773647a0SAndy Whitcroft}
1062de7d4f0eSAndy Whitcroftsub WARN {
1063773647a0SAndy Whitcroft	if (report("WARNING: $_[0]\n")) {
1064de7d4f0eSAndy Whitcroft		our $clean = 0;
10656c72ffaaSAndy Whitcroft		our $cnt_warn++;
1066de7d4f0eSAndy Whitcroft	}
1067773647a0SAndy Whitcroft}
1068de7d4f0eSAndy Whitcroftsub CHK {
1069773647a0SAndy Whitcroft	if ($check && report("CHECK: $_[0]\n")) {
1070de7d4f0eSAndy Whitcroft		our $clean = 0;
10716c72ffaaSAndy Whitcroft		our $cnt_chk++;
10726c72ffaaSAndy Whitcroft	}
1073de7d4f0eSAndy Whitcroft}
1074de7d4f0eSAndy Whitcroft
10756ecd9674SAndy Whitcroftsub check_absolute_file {
10766ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
10776ecd9674SAndy Whitcroft	my $file = $absolute;
10786ecd9674SAndy Whitcroft
10796ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
10806ecd9674SAndy Whitcroft
10816ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
10826ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
10836ecd9674SAndy Whitcroft		if (-f "$root/$file") {
10846ecd9674SAndy Whitcroft			##print "file<$file>\n";
10856ecd9674SAndy Whitcroft			last;
10866ecd9674SAndy Whitcroft		}
10876ecd9674SAndy Whitcroft	}
10886ecd9674SAndy Whitcroft	if (! -f _)  {
10896ecd9674SAndy Whitcroft		return 0;
10906ecd9674SAndy Whitcroft	}
10916ecd9674SAndy Whitcroft
10926ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
10936ecd9674SAndy Whitcroft	my $prefix = $absolute;
10946ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
10956ecd9674SAndy Whitcroft
10966ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
10976ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
10986ecd9674SAndy Whitcroft		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
10996ecd9674SAndy Whitcroft	}
11006ecd9674SAndy Whitcroft}
11016ecd9674SAndy Whitcroft
11020a920b5bSAndy Whitcroftsub process {
11030a920b5bSAndy Whitcroft	my $filename = shift;
11040a920b5bSAndy Whitcroft
11050a920b5bSAndy Whitcroft	my $linenr=0;
11060a920b5bSAndy Whitcroft	my $prevline="";
1107c2fdda0dSAndy Whitcroft	my $prevrawline="";
11080a920b5bSAndy Whitcroft	my $stashline="";
1109c2fdda0dSAndy Whitcroft	my $stashrawline="";
11100a920b5bSAndy Whitcroft
11114a0df2efSAndy Whitcroft	my $length;
11120a920b5bSAndy Whitcroft	my $indent;
11130a920b5bSAndy Whitcroft	my $previndent=0;
11140a920b5bSAndy Whitcroft	my $stashindent=0;
11150a920b5bSAndy Whitcroft
1116de7d4f0eSAndy Whitcroft	our $clean = 1;
11170a920b5bSAndy Whitcroft	my $signoff = 0;
11180a920b5bSAndy Whitcroft	my $is_patch = 0;
11190a920b5bSAndy Whitcroft
112013214adfSAndy Whitcroft	our @report = ();
11216c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
11226c72ffaaSAndy Whitcroft	our $cnt_error = 0;
11236c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
11246c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
11256c72ffaaSAndy Whitcroft
11260a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
11270a920b5bSAndy Whitcroft	my $realfile = '';
11280a920b5bSAndy Whitcroft	my $realline = 0;
11290a920b5bSAndy Whitcroft	my $realcnt = 0;
11300a920b5bSAndy Whitcroft	my $here = '';
11310a920b5bSAndy Whitcroft	my $in_comment = 0;
1132c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
11330a920b5bSAndy Whitcroft	my $first_line = 0;
11341e855726SWolfram Sang	my $p1_prefix = '';
11350a920b5bSAndy Whitcroft
113613214adfSAndy Whitcroft	my $prev_values = 'E';
113713214adfSAndy Whitcroft
113813214adfSAndy Whitcroft	# suppression flags
1139773647a0SAndy Whitcroft	my %suppress_ifbraces;
1140170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
1141653d4876SAndy Whitcroft
1142c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
1143de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
1144c2fdda0dSAndy Whitcroft	#
1145de7d4f0eSAndy Whitcroft	my @setup_docs = ();
1146de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
1147773647a0SAndy Whitcroft
1148773647a0SAndy Whitcroft	sanitise_line_reset();
1149c2fdda0dSAndy Whitcroft	my $line;
1150c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
1151773647a0SAndy Whitcroft		$linenr++;
1152773647a0SAndy Whitcroft		$line = $rawline;
1153c2fdda0dSAndy Whitcroft
1154773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
1155de7d4f0eSAndy Whitcroft			$setup_docs = 0;
1156de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1157de7d4f0eSAndy Whitcroft				$setup_docs = 1;
1158de7d4f0eSAndy Whitcroft			}
1159773647a0SAndy Whitcroft			#next;
1160de7d4f0eSAndy Whitcroft		}
1161773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1162773647a0SAndy Whitcroft			$realline=$1-1;
1163773647a0SAndy Whitcroft			if (defined $2) {
1164773647a0SAndy Whitcroft				$realcnt=$3+1;
1165773647a0SAndy Whitcroft			} else {
1166773647a0SAndy Whitcroft				$realcnt=1+1;
1167773647a0SAndy Whitcroft			}
1168c45dcabdSAndy Whitcroft			$in_comment = 0;
1169773647a0SAndy Whitcroft
1170773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
1171773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
1172773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
1173773647a0SAndy Whitcroft			# at context start.
1174773647a0SAndy Whitcroft			my $edge;
117501fa9147SAndy Whitcroft			my $cnt = $realcnt;
117601fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
117701fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
117801fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
117901fa9147SAndy Whitcroft				$cnt--;
118001fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
1181721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
1182fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1183fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1184fae17daeSAndy Whitcroft					($edge) = $1;
1185fae17daeSAndy Whitcroft					last;
1186fae17daeSAndy Whitcroft				}
1187773647a0SAndy Whitcroft			}
1188773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
1189773647a0SAndy Whitcroft				$in_comment = 1;
1190773647a0SAndy Whitcroft			}
1191773647a0SAndy Whitcroft
1192773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
1193773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
1194773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
1195773647a0SAndy Whitcroft			if (!defined $edge &&
119683242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1197773647a0SAndy Whitcroft			{
1198773647a0SAndy Whitcroft				$in_comment = 1;
1199773647a0SAndy Whitcroft			}
1200773647a0SAndy Whitcroft
1201773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1202773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
1203773647a0SAndy Whitcroft
1204171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1205773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
1206171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
1207773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
1208773647a0SAndy Whitcroft		}
1209773647a0SAndy Whitcroft		push(@lines, $line);
1210773647a0SAndy Whitcroft
1211773647a0SAndy Whitcroft		if ($realcnt > 1) {
1212773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1213773647a0SAndy Whitcroft		} else {
1214773647a0SAndy Whitcroft			$realcnt = 0;
1215773647a0SAndy Whitcroft		}
1216773647a0SAndy Whitcroft
1217773647a0SAndy Whitcroft		#print "==>$rawline\n";
1218773647a0SAndy Whitcroft		#print "-->$line\n";
1219de7d4f0eSAndy Whitcroft
1220de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
1221de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
1222de7d4f0eSAndy Whitcroft		}
1223de7d4f0eSAndy Whitcroft	}
1224de7d4f0eSAndy Whitcroft
12256c72ffaaSAndy Whitcroft	$prefix = '';
12266c72ffaaSAndy Whitcroft
1227773647a0SAndy Whitcroft	$realcnt = 0;
1228773647a0SAndy Whitcroft	$linenr = 0;
12290a920b5bSAndy Whitcroft	foreach my $line (@lines) {
12300a920b5bSAndy Whitcroft		$linenr++;
12310a920b5bSAndy Whitcroft
1232c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
123330670854SAndy Whitcroft		my $hunk_line = ($realcnt != 0);
12346c72ffaaSAndy Whitcroft
12350a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
12366c72ffaaSAndy Whitcroft		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
12370a920b5bSAndy Whitcroft			$is_patch = 1;
12384a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
12390a920b5bSAndy Whitcroft			$realline=$1-1;
12400a920b5bSAndy Whitcroft			if (defined $2) {
12410a920b5bSAndy Whitcroft				$realcnt=$3+1;
12420a920b5bSAndy Whitcroft			} else {
12430a920b5bSAndy Whitcroft				$realcnt=1+1;
12440a920b5bSAndy Whitcroft			}
1245c2fdda0dSAndy Whitcroft			annotate_reset();
124613214adfSAndy Whitcroft			$prev_values = 'E';
124713214adfSAndy Whitcroft
1248773647a0SAndy Whitcroft			%suppress_ifbraces = ();
1249170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
12500a920b5bSAndy Whitcroft			next;
12510a920b5bSAndy Whitcroft
12524a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
12534a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
12544a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
1255773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
12560a920b5bSAndy Whitcroft			$realline++;
1257d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
12580a920b5bSAndy Whitcroft
12594a0df2efSAndy Whitcroft			# Measure the line length and indent.
1260c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
12610a920b5bSAndy Whitcroft
12620a920b5bSAndy Whitcroft			# Track the previous line.
12630a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
12640a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
1265c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1266c2fdda0dSAndy Whitcroft
1267773647a0SAndy Whitcroft			#warn "line<$line>\n";
12686c72ffaaSAndy Whitcroft
1269d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
1270d8aaf121SAndy Whitcroft			$realcnt--;
12710a920b5bSAndy Whitcroft		}
12720a920b5bSAndy Whitcroft
12730a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
1274773647a0SAndy Whitcroft		$prefix = "$filename:$realline: " if ($emacs && $file);
1275773647a0SAndy Whitcroft		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1276773647a0SAndy Whitcroft
12776c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
12786c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
1279773647a0SAndy Whitcroft
1280773647a0SAndy Whitcroft		# extract the filename as it passes
1281773647a0SAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
1282773647a0SAndy Whitcroft			$realfile = $1;
12831e855726SWolfram Sang			$realfile =~ s@^([^/]*)/@@;
12841e855726SWolfram Sang
12851e855726SWolfram Sang			$p1_prefix = $1;
1286e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
1287e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
12881e855726SWolfram Sang				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
12891e855726SWolfram Sang			}
1290773647a0SAndy Whitcroft
1291c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
1292773647a0SAndy Whitcroft				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1293773647a0SAndy Whitcroft			}
1294773647a0SAndy Whitcroft			next;
1295773647a0SAndy Whitcroft		}
1296773647a0SAndy Whitcroft
1297389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
12980a920b5bSAndy Whitcroft
1299c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
1300c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
1301c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
13020a920b5bSAndy Whitcroft
13036c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
13046c72ffaaSAndy Whitcroft
13050a920b5bSAndy Whitcroft#check the patch for a signoff:
1306d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
13074a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
13084a0df2efSAndy Whitcroft			$signoff++;
13090a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
1310de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
1311de7d4f0eSAndy Whitcroft					$herecurr);
13120a920b5bSAndy Whitcroft			}
13130a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
1314773647a0SAndy Whitcroft				WARN("space required after Signed-off-by:\n" .
1315de7d4f0eSAndy Whitcroft					$herecurr);
13160a920b5bSAndy Whitcroft			}
13170a920b5bSAndy Whitcroft		}
13180a920b5bSAndy Whitcroft
131900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
13208905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1321de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
13226c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
1323de7d4f0eSAndy Whitcroft		}
1324de7d4f0eSAndy Whitcroft
13256ecd9674SAndy Whitcroft# Check for absolute kernel paths.
13266ecd9674SAndy Whitcroft		if ($tree) {
13276ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
13286ecd9674SAndy Whitcroft				my $file = $1;
13296ecd9674SAndy Whitcroft
13306ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
13316ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
13326ecd9674SAndy Whitcroft					#
13336ecd9674SAndy Whitcroft				} else {
13346ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
13356ecd9674SAndy Whitcroft				}
13366ecd9674SAndy Whitcroft			}
13376ecd9674SAndy Whitcroft		}
13386ecd9674SAndy Whitcroft
1339de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1340de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1341171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
1342171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1343171ae1a4SAndy Whitcroft
1344171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
1345171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1346171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
1347171ae1a4SAndy Whitcroft
1348171ae1a4SAndy Whitcroft			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
134900df344fSAndy Whitcroft		}
13500a920b5bSAndy Whitcroft
135130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
135230670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
135300df344fSAndy Whitcroft
13540a920b5bSAndy Whitcroft#trailing whitespace
13559c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
1356c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
13579c0ca6f9SAndy Whitcroft			ERROR("DOS line endings\n" . $herevet);
13589c0ca6f9SAndy Whitcroft
1359c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1360c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1361de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
13620a920b5bSAndy Whitcroft		}
13635368df20SAndy Whitcroft
13645368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
13655368df20SAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
13665368df20SAndy Whitcroft
13670a920b5bSAndy Whitcroft#80 column limit
1368c45dcabdSAndy Whitcroft		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1369f4c014c0SAndy Whitcroft		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1370f4c014c0SAndy Whitcroft		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1371f4c014c0SAndy Whitcroft		    $length > 80)
1372c45dcabdSAndy Whitcroft		{
1373de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
13740a920b5bSAndy Whitcroft		}
13750a920b5bSAndy Whitcroft
13768905a67cSAndy Whitcroft# check for adding lines without a newline.
13778905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
13788905a67cSAndy Whitcroft			WARN("adding a line without newline at end of file\n" . $herecurr);
13798905a67cSAndy Whitcroft		}
13808905a67cSAndy Whitcroft
1381b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
1382b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c|pl)$/);
13830a920b5bSAndy Whitcroft
13840a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
13850a920b5bSAndy Whitcroft# more than 8 must use tabs.
1386c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
1387c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
1388c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1389171ae1a4SAndy Whitcroft			ERROR("code indent should use tabs where possible\n" . $herevet);
13900a920b5bSAndy Whitcroft		}
13910a920b5bSAndy Whitcroft
1392b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
1393b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
1394b9ea10d6SAndy Whitcroft
1395c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
1396cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1397c2fdda0dSAndy Whitcroft			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1398c2fdda0dSAndy Whitcroft		}
139922f2a2efSAndy Whitcroft
14009c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
1401170d3a22SAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
14029c9ba34eSAndy Whitcroft		if ($realcnt && $line =~ /.\s*\S/) {
1403170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1404f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
1405171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
1406171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
1407171ae1a4SAndy Whitcroft
1408171ae1a4SAndy Whitcroft			my $s = $stat;
1409171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
1410cf655043SAndy Whitcroft
1411c2fdda0dSAndy Whitcroft			# Ignore goto labels.
1412171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
1413c2fdda0dSAndy Whitcroft
1414c2fdda0dSAndy Whitcroft			# Ignore functions being called
1415171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1416c2fdda0dSAndy Whitcroft
1417463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
1418463f2864SAndy Whitcroft
1419c45dcabdSAndy Whitcroft			# declarations always start with types
1420d2506586SAndy 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) {
1421c45dcabdSAndy Whitcroft				my $type = $1;
1422c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
1423c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
1424c45dcabdSAndy Whitcroft
14256c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
1426a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1427c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
1428c2fdda0dSAndy Whitcroft			}
14298905a67cSAndy Whitcroft
14306c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
143165863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1432c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
14339c0ca6f9SAndy Whitcroft			}
14348905a67cSAndy Whitcroft
14358905a67cSAndy Whitcroft			# Check for any sort of function declaration.
14368905a67cSAndy Whitcroft			# int foo(something bar, other baz);
14378905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
1438171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
14398905a67cSAndy Whitcroft				my ($name_len) = length($1);
14408905a67cSAndy Whitcroft
1441cf655043SAndy Whitcroft				my $ctx = $s;
1442773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
14438905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
1444cf655043SAndy Whitcroft
14458905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
1446c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
14478905a67cSAndy Whitcroft
1448c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
14498905a67cSAndy Whitcroft					}
14508905a67cSAndy Whitcroft				}
14518905a67cSAndy Whitcroft			}
14528905a67cSAndy Whitcroft
14539c0ca6f9SAndy Whitcroft		}
14549c0ca6f9SAndy Whitcroft
145500df344fSAndy Whitcroft#
145600df344fSAndy Whitcroft# Checks which may be anchored in the context.
145700df344fSAndy Whitcroft#
145800df344fSAndy Whitcroft
145900df344fSAndy Whitcroft# Check for switch () and associated case and default
146000df344fSAndy Whitcroft# statements should be at the same indent.
146100df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
146200df344fSAndy Whitcroft			my $err = '';
146300df344fSAndy Whitcroft			my $sep = '';
146400df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
146500df344fSAndy Whitcroft			shift(@ctx);
146600df344fSAndy Whitcroft			for my $ctx (@ctx) {
146700df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
146800df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
146900df344fSAndy Whitcroft							$indent != $cindent) {
147000df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
147100df344fSAndy Whitcroft					$sep = '';
147200df344fSAndy Whitcroft				} else {
147300df344fSAndy Whitcroft					$sep = "[...]\n";
147400df344fSAndy Whitcroft				}
147500df344fSAndy Whitcroft			}
147600df344fSAndy Whitcroft			if ($err ne '') {
14779c0ca6f9SAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline$err");
1478de7d4f0eSAndy Whitcroft			}
1479de7d4f0eSAndy Whitcroft		}
1480de7d4f0eSAndy Whitcroft
1481de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
1482de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
1483c45dcabdSAndy Whitcroft		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1484773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
1485773647a0SAndy Whitcroft
14869c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1487de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
1488de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
1489de7d4f0eSAndy Whitcroft
1490548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
1491548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
1492de7d4f0eSAndy Whitcroft
1493548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1494548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
1495548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
1496548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1497548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1498773647a0SAndy Whitcroft				$ctx_ln++;
1499773647a0SAndy Whitcroft			}
1500548596d5SAndy Whitcroft
150153210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
150253210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1503773647a0SAndy Whitcroft
1504773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1505773647a0SAndy Whitcroft				ERROR("that open brace { should be on the previous line\n" .
1506c45dcabdSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
150700df344fSAndy Whitcroft			}
1508773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1509773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
1510773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
1511773647a0SAndy Whitcroft			{
15129c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
15139c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
1514773647a0SAndy Whitcroft					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1515c45dcabdSAndy Whitcroft						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
15169c0ca6f9SAndy Whitcroft				}
15179c0ca6f9SAndy Whitcroft			}
151800df344fSAndy Whitcroft		}
151900df344fSAndy Whitcroft
15204d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
15214d001e4dSAndy Whitcroft		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
15224d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
15234d001e4dSAndy Whitcroft
15244d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
15254d001e4dSAndy Whitcroft
15264d001e4dSAndy Whitcroft			# Make sure we remove the line prefixes as we have
15274d001e4dSAndy Whitcroft			# none on the first line, and are going to readd them
15284d001e4dSAndy Whitcroft			# where necessary.
15294d001e4dSAndy Whitcroft			$s =~ s/\n./\n/gs;
15304d001e4dSAndy Whitcroft
15314d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
15326f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
15336f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
15344d001e4dSAndy Whitcroft
15354d001e4dSAndy Whitcroft			# We want to check the first line inside the block
15364d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
15374d001e4dSAndy Whitcroft			#  1) any blank line termination
15384d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
15394d001e4dSAndy Whitcroft			#  3) any do (...) {
15404d001e4dSAndy Whitcroft			my $continuation = 0;
15414d001e4dSAndy Whitcroft			my $check = 0;
15424d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
15434d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
15444d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
15454d001e4dSAndy Whitcroft				$continuation = 1;
15464d001e4dSAndy Whitcroft			}
15479bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
15484d001e4dSAndy Whitcroft				$check = 1;
15494d001e4dSAndy Whitcroft				$cond_lines++;
15504d001e4dSAndy Whitcroft			}
15514d001e4dSAndy Whitcroft
15524d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
15534d001e4dSAndy Whitcroft			# preprocessor statement.
15544d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
15554d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
15564d001e4dSAndy Whitcroft				$check = 0;
15574d001e4dSAndy Whitcroft			}
15584d001e4dSAndy Whitcroft
15599bd49efeSAndy Whitcroft			my $cond_ptr = -1;
1560740504c6SAndy Whitcroft			$continuation = 0;
15619bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
15629bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
15634d001e4dSAndy Whitcroft
1564f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
1565f16fa28fSAndy Whitcroft				# is not linear.
1566f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1567f16fa28fSAndy Whitcroft					$check = 0;
1568f16fa28fSAndy Whitcroft				}
1569f16fa28fSAndy Whitcroft
15709bd49efeSAndy Whitcroft				# Ignore:
15719bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
15729bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
15739bd49efeSAndy Whitcroft				#  3) labels.
1574740504c6SAndy Whitcroft				if ($continuation ||
1575740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
15769bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
15779bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
1578740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
157930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
15809bd49efeSAndy Whitcroft						$cond_lines++;
15819bd49efeSAndy Whitcroft					}
15824d001e4dSAndy Whitcroft				}
158330dad6ebSAndy Whitcroft			}
15844d001e4dSAndy Whitcroft
15854d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
15864d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
15874d001e4dSAndy Whitcroft
15884d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
15894d001e4dSAndy Whitcroft			# this is not this patch's fault.
15904d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
15914d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
15924d001e4dSAndy Whitcroft				$check = 0;
15934d001e4dSAndy Whitcroft			}
15944d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
15954d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
15964d001e4dSAndy Whitcroft			}
15974d001e4dSAndy Whitcroft
15989bd49efeSAndy 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";
15994d001e4dSAndy Whitcroft
16004d001e4dSAndy Whitcroft			if ($check && (($sindent % 8) != 0 ||
16014d001e4dSAndy Whitcroft			    ($sindent <= $indent && $s ne ''))) {
16024d001e4dSAndy Whitcroft				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
16034d001e4dSAndy Whitcroft			}
16044d001e4dSAndy Whitcroft		}
16054d001e4dSAndy Whitcroft
16066c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
16076c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
16081f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
16091f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
16106c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
1611c2fdda0dSAndy Whitcroft		if ($dbg_values) {
1612c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
1613cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
1614cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
16151f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
1616c2fdda0dSAndy Whitcroft		}
16176c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
16186c72ffaaSAndy Whitcroft
161900df344fSAndy Whitcroft#ignore lines not being added
162000df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
162100df344fSAndy Whitcroft
1622653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
16237429c690SAndy Whitcroft		if ($dbg_type) {
16247429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
16257429c690SAndy Whitcroft				ERROR("TEST: is type\n" . $herecurr);
16267429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
16277429c690SAndy Whitcroft				ERROR("TEST: is not type ($1 is)\n". $herecurr);
16287429c690SAndy Whitcroft			}
1629653d4876SAndy Whitcroft			next;
1630653d4876SAndy Whitcroft		}
1631a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
1632a1ef277eSAndy Whitcroft		if ($dbg_attr) {
16339360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
1634a1ef277eSAndy Whitcroft				ERROR("TEST: is attr\n" . $herecurr);
16359360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1636a1ef277eSAndy Whitcroft				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1637a1ef277eSAndy Whitcroft			}
1638a1ef277eSAndy Whitcroft			next;
1639a1ef277eSAndy Whitcroft		}
1640653d4876SAndy Whitcroft
1641f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
1642f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1643f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
1644773647a0SAndy Whitcroft			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1645f0a594c1SAndy Whitcroft		}
1646f0a594c1SAndy Whitcroft
164700df344fSAndy Whitcroft#
164800df344fSAndy Whitcroft# Checks which are anchored on the added line.
164900df344fSAndy Whitcroft#
165000df344fSAndy Whitcroft
1651653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
1652c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1653653d4876SAndy Whitcroft			my $path = $1;
1654653d4876SAndy Whitcroft			if ($path =~ m{//}) {
1655de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
1656de7d4f0eSAndy Whitcroft					$herecurr);
1657653d4876SAndy Whitcroft			}
1658653d4876SAndy Whitcroft		}
1659653d4876SAndy Whitcroft
166000df344fSAndy Whitcroft# no C99 // comments
166100df344fSAndy Whitcroft		if ($line =~ m{//}) {
1662de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
166300df344fSAndy Whitcroft		}
166400df344fSAndy Whitcroft		# Remove C99 comments.
16650a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
16666c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
16670a920b5bSAndy Whitcroft
16680a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
1669653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1670653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1671653d4876SAndy Whitcroft			my $name = $1;
167248012058SAndy Whitcroft			if ($prevline !~ /(?:
167348012058SAndy Whitcroft				^.}|
167448012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
167548012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
167648012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
167748012058SAndy Whitcroft				^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
167848012058SAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
167948012058SAndy Whitcroft			    )/x) {
1680de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
16810a920b5bSAndy Whitcroft			}
16820a920b5bSAndy Whitcroft		}
16830a920b5bSAndy Whitcroft
1684f0a594c1SAndy Whitcroft# check for external initialisers.
1685c45dcabdSAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1686f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
1687f0a594c1SAndy Whitcroft				$herecurr);
1688f0a594c1SAndy Whitcroft		}
16890a920b5bSAndy Whitcroft# check for static initialisers.
16902d1bafd7SAndy Whitcroft		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1691de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
1692de7d4f0eSAndy Whitcroft				$herecurr);
16930a920b5bSAndy Whitcroft		}
16940a920b5bSAndy Whitcroft
1695653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
1696653d4876SAndy Whitcroft# make sense.
1697653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
16988054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1699c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
17008ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
1701653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
1702de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
17030a920b5bSAndy Whitcroft		}
17040a920b5bSAndy Whitcroft
17050a920b5bSAndy Whitcroft# * goes on variable not on type
170665863862SAndy Whitcroft		# (char*[ const])
170700ef4eceSAndy Whitcroft		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
170865863862SAndy Whitcroft			my ($from, $to) = ($1, $1);
1709d8aaf121SAndy Whitcroft
171065863862SAndy Whitcroft			# Should start with a space.
171165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
171265863862SAndy Whitcroft			# Should not end with a space.
171365863862SAndy Whitcroft			$to =~ s/\s+$//;
171465863862SAndy Whitcroft			# '*'s should not have spaces between.
1715f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
171665863862SAndy Whitcroft			}
1717d8aaf121SAndy Whitcroft
171865863862SAndy Whitcroft			#print "from<$from> to<$to>\n";
171965863862SAndy Whitcroft			if ($from ne $to) {
172065863862SAndy Whitcroft				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
172165863862SAndy Whitcroft			}
172200ef4eceSAndy Whitcroft		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
172365863862SAndy Whitcroft			my ($from, $to, $ident) = ($1, $1, $2);
1724d8aaf121SAndy Whitcroft
172565863862SAndy Whitcroft			# Should start with a space.
172665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
172765863862SAndy Whitcroft			# Should not end with a space.
172865863862SAndy Whitcroft			$to =~ s/\s+$//;
172965863862SAndy Whitcroft			# '*'s should not have spaces between.
1730f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
173165863862SAndy Whitcroft			}
173265863862SAndy Whitcroft			# Modifiers should have spaces.
173365863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
173465863862SAndy Whitcroft
1735667026e7SAndy Whitcroft			#print "from<$from> to<$to> ident<$ident>\n";
1736667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
173765863862SAndy Whitcroft				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
173865863862SAndy Whitcroft			}
17390a920b5bSAndy Whitcroft		}
17400a920b5bSAndy Whitcroft
17410a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
17420a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
17430a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
17440a920b5bSAndy Whitcroft# 			print "$herecurr";
17450a920b5bSAndy Whitcroft# 			$clean = 0;
17460a920b5bSAndy Whitcroft# 		}
17470a920b5bSAndy Whitcroft
17488905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1749c2fdda0dSAndy Whitcroft			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
17508905a67cSAndy Whitcroft		}
17518905a67cSAndy Whitcroft
175200df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
175300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
175400df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
175500df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
175600df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
1757f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
175800df344fSAndy Whitcroft			my $ok = 0;
175900df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
176000df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
176100df344fSAndy Whitcroft				# we have a preceeding printk if it ends
176200df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
176300df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
176400df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
176500df344fSAndy Whitcroft						$ok = 1;
176600df344fSAndy Whitcroft					}
176700df344fSAndy Whitcroft					last;
176800df344fSAndy Whitcroft				}
176900df344fSAndy Whitcroft			}
177000df344fSAndy Whitcroft			if ($ok == 0) {
1771de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
17720a920b5bSAndy Whitcroft			}
177300df344fSAndy Whitcroft		}
17740a920b5bSAndy Whitcroft
1775653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
1776653d4876SAndy Whitcroft# or if closed on same line
1777c45dcabdSAndy Whitcroft		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1778c45dcabdSAndy Whitcroft		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1779de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
17800a920b5bSAndy Whitcroft		}
1781653d4876SAndy Whitcroft
17828905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
17838905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
17848905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
17858905a67cSAndy Whitcroft			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
17868905a67cSAndy Whitcroft		}
17878905a67cSAndy Whitcroft
17888d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
17898d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
1790fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1791fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
17928d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
17938d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
17948d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
1795fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1796fe2a7dbcSAndy Whitcroft			    $prefix !~ /{\s+$/) {
17978d31cfceSAndy Whitcroft				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
17988d31cfceSAndy Whitcroft			}
17998d31cfceSAndy Whitcroft		}
18008d31cfceSAndy Whitcroft
1801f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
18026c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
1803c2fdda0dSAndy Whitcroft			my $name = $1;
1804773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
1805773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
1806c2fdda0dSAndy Whitcroft
1807c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
1808773647a0SAndy Whitcroft			if ($name =~ /^(?:
1809773647a0SAndy Whitcroft				if|for|while|switch|return|case|
1810773647a0SAndy Whitcroft				volatile|__volatile__|
1811773647a0SAndy Whitcroft				__attribute__|format|__extension__|
1812773647a0SAndy Whitcroft				asm|__asm__)$/x)
1813773647a0SAndy Whitcroft			{
1814c2fdda0dSAndy Whitcroft
1815c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
1816c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
1817c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
1818c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1819773647a0SAndy Whitcroft
1820773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
1821c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1822c2fdda0dSAndy Whitcroft
1823c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
1824c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
1825773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
1826c2fdda0dSAndy Whitcroft
1827c2fdda0dSAndy Whitcroft			} else {
1828773647a0SAndy Whitcroft				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1829f0a594c1SAndy Whitcroft			}
18306c72ffaaSAndy Whitcroft		}
1831653d4876SAndy Whitcroft# Check operator spacing.
18320a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
18339c0ca6f9SAndy Whitcroft			my $ops = qr{
18349c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
18359c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
18369c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
18371f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
18381f65f947SAndy Whitcroft				\?|:
18399c0ca6f9SAndy Whitcroft			}x;
1840cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
184100df344fSAndy Whitcroft			my $off = 0;
18426c72ffaaSAndy Whitcroft
18436c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
18446c72ffaaSAndy Whitcroft
18450a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
18464a0df2efSAndy Whitcroft				$off += length($elements[$n]);
18474a0df2efSAndy Whitcroft
1848773647a0SAndy Whitcroft				# Pick up the preceeding and succeeding characters.
1849773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
1850773647a0SAndy Whitcroft				my $cc = '';
1851773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1852773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
1853773647a0SAndy Whitcroft				}
1854773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
1855773647a0SAndy Whitcroft
18564a0df2efSAndy Whitcroft				my $a = '';
18574a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
18584a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
1859cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
18604a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
18614a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
1862773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
18634a0df2efSAndy Whitcroft
18640a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
18654a0df2efSAndy Whitcroft
18664a0df2efSAndy Whitcroft				my $c = '';
18670a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
18684a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
18694a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1870cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
18714a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
18724a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
18738b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
18744a0df2efSAndy Whitcroft				} else {
18754a0df2efSAndy Whitcroft					$c = 'E';
18760a920b5bSAndy Whitcroft				}
18770a920b5bSAndy Whitcroft
18784a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
18794a0df2efSAndy Whitcroft
18804a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
18814a0df2efSAndy Whitcroft
18826c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
1883de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
18840a920b5bSAndy Whitcroft
188574048ed8SAndy Whitcroft				# Pull out the value of this operator.
18866c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
18870a920b5bSAndy Whitcroft
18881f65f947SAndy Whitcroft				# Get the full operator variant.
18891f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
18901f65f947SAndy Whitcroft
189113214adfSAndy Whitcroft				# Ignore operators passed as parameters.
189213214adfSAndy Whitcroft				if ($op_type ne 'V' &&
189313214adfSAndy Whitcroft				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
189413214adfSAndy Whitcroft
1895cf655043SAndy Whitcroft#				# Ignore comments
1896cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
189713214adfSAndy Whitcroft
1898d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
189913214adfSAndy Whitcroft				} elsif ($op eq ';') {
1900cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
1901cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
1902773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
1903d8aaf121SAndy Whitcroft					}
1904d8aaf121SAndy Whitcroft
1905d8aaf121SAndy Whitcroft				# // is a comment
1906d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
19070a920b5bSAndy Whitcroft
19081f65f947SAndy Whitcroft				# No spaces for:
19091f65f947SAndy Whitcroft				#   ->
19101f65f947SAndy Whitcroft				#   :   when part of a bitfield
19111f65f947SAndy Whitcroft				} elsif ($op eq '->' || $opv eq ':B') {
19124a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
1913773647a0SAndy Whitcroft						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
19140a920b5bSAndy Whitcroft					}
19150a920b5bSAndy Whitcroft
19160a920b5bSAndy Whitcroft				# , must have a space on the right.
19170a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
1918cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1919773647a0SAndy Whitcroft						ERROR("space required after that '$op' $at\n" . $hereptr);
19200a920b5bSAndy Whitcroft					}
19210a920b5bSAndy Whitcroft
19229c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
192374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
19249c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
19259c0ca6f9SAndy Whitcroft
19269c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
19279c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
19289c0ca6f9SAndy Whitcroft				# unary operator, or a cast
19299c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
193074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
19310d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
1932cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1933773647a0SAndy Whitcroft						ERROR("space required before that '$op' $at\n" . $hereptr);
19340a920b5bSAndy Whitcroft					}
1935a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1936171ae1a4SAndy Whitcroft						# A unary '*' may be const
1937171ae1a4SAndy Whitcroft
1938171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
1939a3340b35SAndy Whitcroft						ERROR("Aspace prohibited after that '$op' $at\n" . $hereptr);
19400a920b5bSAndy Whitcroft					}
19410a920b5bSAndy Whitcroft
19420a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
19430a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
1944773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1945773647a0SAndy Whitcroft						ERROR("space required one side of that '$op' $at\n" . $hereptr);
19460a920b5bSAndy Whitcroft					}
1947773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
1948773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1949773647a0SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1950653d4876SAndy Whitcroft					}
1951773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
1952773647a0SAndy Whitcroft						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1953773647a0SAndy Whitcroft					}
1954773647a0SAndy Whitcroft
19550a920b5bSAndy Whitcroft
19560a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
19579c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
19589c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
19599c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
1960c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
1961c2fdda0dSAndy Whitcroft					 $op eq '%')
19620a920b5bSAndy Whitcroft				{
1963773647a0SAndy Whitcroft					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1964de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
1965de7d4f0eSAndy Whitcroft							$hereptr);
19660a920b5bSAndy Whitcroft					}
19670a920b5bSAndy Whitcroft
19681f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
19691f65f947SAndy Whitcroft				# terminating a case value or a label.
19701f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
19711f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
19721f65f947SAndy Whitcroft						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
19731f65f947SAndy Whitcroft					}
19741f65f947SAndy Whitcroft
19750a920b5bSAndy Whitcroft				# All the others need spaces both sides.
1976cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
19771f65f947SAndy Whitcroft					my $ok = 0;
19781f65f947SAndy Whitcroft
197922f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
19801f65f947SAndy Whitcroft					if (($op eq '<' &&
19811f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
19821f65f947SAndy Whitcroft					    ($op eq '>' &&
19831f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
19841f65f947SAndy Whitcroft					{
19851f65f947SAndy Whitcroft					    	$ok = 1;
19861f65f947SAndy Whitcroft					}
19871f65f947SAndy Whitcroft
19881f65f947SAndy Whitcroft					# Ignore ?:
19891f65f947SAndy Whitcroft					if (($opv eq ':O' && $ca =~ /\?$/) ||
19901f65f947SAndy Whitcroft					    ($op eq '?' && $cc =~ /^:/)) {
19911f65f947SAndy Whitcroft					    	$ok = 1;
19921f65f947SAndy Whitcroft					}
19931f65f947SAndy Whitcroft
19941f65f947SAndy Whitcroft					if ($ok == 0) {
1995773647a0SAndy Whitcroft						ERROR("spaces required around that '$op' $at\n" . $hereptr);
19960a920b5bSAndy Whitcroft					}
199722f2a2efSAndy Whitcroft				}
19984a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
19990a920b5bSAndy Whitcroft			}
20000a920b5bSAndy Whitcroft		}
20010a920b5bSAndy Whitcroft
2002f0a594c1SAndy Whitcroft# check for multiple assignments
2003f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
20046c72ffaaSAndy Whitcroft			CHK("multiple assignments should be avoided\n" . $herecurr);
2005f0a594c1SAndy Whitcroft		}
2006f0a594c1SAndy Whitcroft
200722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
200822f2a2efSAndy Whitcroft## # continuation.
200922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
201022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
201122f2a2efSAndy Whitcroft##
201222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
201322f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
201422f2a2efSAndy Whitcroft## 			my $ln = $line;
201522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
201622f2a2efSAndy Whitcroft## 			}
201722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
201822f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
201922f2a2efSAndy Whitcroft## 			}
202022f2a2efSAndy Whitcroft## 		}
2021f0a594c1SAndy Whitcroft
20220a920b5bSAndy Whitcroft#need space before brace following if, while, etc
202322f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
202422f2a2efSAndy Whitcroft		    $line =~ /do{/) {
2025773647a0SAndy Whitcroft			ERROR("space required before the open brace '{'\n" . $herecurr);
2026de7d4f0eSAndy Whitcroft		}
2027de7d4f0eSAndy Whitcroft
2028de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
2029de7d4f0eSAndy Whitcroft# on the line
2030de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
2031773647a0SAndy Whitcroft			ERROR("space required after that close brace '}'\n" . $herecurr);
20320a920b5bSAndy Whitcroft		}
20330a920b5bSAndy Whitcroft
203422f2a2efSAndy Whitcroft# check spacing on square brackets
203522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2036773647a0SAndy Whitcroft			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
203722f2a2efSAndy Whitcroft		}
203822f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
2039773647a0SAndy Whitcroft			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
204022f2a2efSAndy Whitcroft		}
204122f2a2efSAndy Whitcroft
2042c45dcabdSAndy Whitcroft# check spacing on parentheses
20439c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
20449c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
2045773647a0SAndy Whitcroft			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
204622f2a2efSAndy Whitcroft		}
204713214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2048c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
2049c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
2050773647a0SAndy Whitcroft			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
205122f2a2efSAndy Whitcroft		}
205222f2a2efSAndy Whitcroft
20530a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
20544a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
20550a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2056de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
20570a920b5bSAndy Whitcroft		}
20580a920b5bSAndy Whitcroft
2059c45dcabdSAndy Whitcroft# Return is not a function.
2060c45dcabdSAndy Whitcroft		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2061c45dcabdSAndy Whitcroft			my $spacing = $1;
2062c45dcabdSAndy Whitcroft			my $value = $2;
2063c45dcabdSAndy Whitcroft
206486f9d059SAndy Whitcroft			# Flatten any parentheses
2065fee61c47SAndy Whitcroft			$value =~ s/\)\(/\) \(/g;
206663f17f89SAndy Whitcroft			while ($value =~ s/\[[^\{\}]*\]/1/ ||
206763f17f89SAndy Whitcroft			       $value !~ /(?:$Ident|-?$Constant)\s*
206863f17f89SAndy Whitcroft					     $Compare\s*
206963f17f89SAndy Whitcroft					     (?:$Ident|-?$Constant)/x &&
207063f17f89SAndy Whitcroft			       $value =~ s/\([^\(\)]*\)/1/) {
2071c45dcabdSAndy Whitcroft			}
2072c45dcabdSAndy Whitcroft
2073c45dcabdSAndy Whitcroft			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2074c45dcabdSAndy Whitcroft				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2075c45dcabdSAndy Whitcroft
2076c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
2077c45dcabdSAndy Whitcroft				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2078c45dcabdSAndy Whitcroft			}
2079c45dcabdSAndy Whitcroft		}
2080c45dcabdSAndy Whitcroft
20810a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
20824a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
2083773647a0SAndy Whitcroft			ERROR("space required before the open parenthesis '('\n" . $herecurr);
20840a920b5bSAndy Whitcroft		}
20850a920b5bSAndy Whitcroft
2086f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
2087f5fe35ddSAndy Whitcroft# statements after the conditional.
2088170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
2089170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
2090170d3a22SAndy Whitcroft						$remain_next, $off_next);
2091170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
2092170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
2093170d3a22SAndy Whitcroft
2094170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
2095170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
2096170d3a22SAndy Whitcroft				# then count those as offsets.
2097170d3a22SAndy Whitcroft				my ($whitespace) =
2098170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2099170d3a22SAndy Whitcroft				my $offset =
2100170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
2101170d3a22SAndy Whitcroft
2102170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
2103170d3a22SAndy Whitcroft								$offset} = 1;
2104170d3a22SAndy Whitcroft			}
2105170d3a22SAndy Whitcroft		}
2106170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
2107170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2108171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
21098905a67cSAndy Whitcroft
2110b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2111c2fdda0dSAndy Whitcroft				ERROR("do not use assignment in if condition\n" . $herecurr);
21128905a67cSAndy Whitcroft			}
21138905a67cSAndy Whitcroft
21148905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
21158905a67cSAndy Whitcroft			# conditional.
2116773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
21178905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
211813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
211953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
212053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
2121773647a0SAndy Whitcroft			{
2122bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
2123bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
2124bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
2125bb44ad39SAndy Whitcroft
2126bb44ad39SAndy Whitcroft				my $stat_real = raw_line($linenr, $cond_lines);
2127bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
2128bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
2129bb44ad39SAndy Whitcroft				}
2130bb44ad39SAndy Whitcroft
2131bb44ad39SAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
21328905a67cSAndy Whitcroft			}
21338905a67cSAndy Whitcroft		}
21348905a67cSAndy Whitcroft
213513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
213613214adfSAndy Whitcroft		if ($line =~ /
213713214adfSAndy Whitcroft			(?:
213813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
213913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
214013214adfSAndy Whitcroft				(?:\&\&|\|\|)
214113214adfSAndy Whitcroft			|
214213214adfSAndy Whitcroft				(?:\&\&|\|\|)
214313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
214413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
214513214adfSAndy Whitcroft			)/x)
214613214adfSAndy Whitcroft		{
214713214adfSAndy Whitcroft			WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
214813214adfSAndy Whitcroft		}
214913214adfSAndy Whitcroft
21508905a67cSAndy Whitcroft# if and else should not have general statements after it
215113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
215213214adfSAndy Whitcroft			my $s = $1;
215313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
215413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
21558905a67cSAndy Whitcroft				ERROR("trailing statements should be on next line\n" . $herecurr);
21560a920b5bSAndy Whitcroft			}
215713214adfSAndy Whitcroft		}
215839667782SAndy Whitcroft# if should not continue a brace
215939667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
216039667782SAndy Whitcroft			ERROR("trailing statements should be on next line\n" .
216139667782SAndy Whitcroft				$herecurr);
216239667782SAndy Whitcroft		}
2163a1080bf8SAndy Whitcroft# case and default should not have general statements after them
2164a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2165a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
21663fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2167a1080bf8SAndy Whitcroft			\s*return\s+
2168a1080bf8SAndy Whitcroft		    )/xg)
2169a1080bf8SAndy Whitcroft		{
2170a1080bf8SAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
2171a1080bf8SAndy Whitcroft		}
21720a920b5bSAndy Whitcroft
21730a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
21740a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
21750a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
21760a920b5bSAndy Whitcroft						$previndent == $indent) {
2177de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
21780a920b5bSAndy Whitcroft		}
21790a920b5bSAndy Whitcroft
2180c2fdda0dSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2181c2fdda0dSAndy Whitcroft						$previndent == $indent) {
2182c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2183c2fdda0dSAndy Whitcroft
2184c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
2185c2fdda0dSAndy Whitcroft			# conditional.
2186773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
2187c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
2188c2fdda0dSAndy Whitcroft
2189c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
2190c2fdda0dSAndy Whitcroft				ERROR("while should follow close brace '}'\n" . $hereprev);
2191c2fdda0dSAndy Whitcroft			}
2192c2fdda0dSAndy Whitcroft		}
2193c2fdda0dSAndy Whitcroft
21940a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
21950a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
21960a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
21970a920b5bSAndy Whitcroft#		    print "$herecurr";
21980a920b5bSAndy Whitcroft#		    $clean = 0;
21990a920b5bSAndy Whitcroft#		}
22000a920b5bSAndy Whitcroft
22010a920b5bSAndy Whitcroft#no spaces allowed after \ in define
2202c45dcabdSAndy Whitcroft		if ($line=~/\#\s*define.*\\\s$/) {
2203de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
22040a920b5bSAndy Whitcroft		}
22050a920b5bSAndy Whitcroft
2206653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2207c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2208e09dec48SAndy Whitcroft			my $file = "$1.h";
2209e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
2210e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
2211e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
2212c45dcabdSAndy Whitcroft			    $1 ne 'irq')
2213c45dcabdSAndy Whitcroft			{
2214e09dec48SAndy Whitcroft				if ($realfile =~ m{^arch/}) {
2215e09dec48SAndy Whitcroft					CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2216e09dec48SAndy Whitcroft				} else {
2217e09dec48SAndy Whitcroft					WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2218e09dec48SAndy Whitcroft				}
22190a920b5bSAndy Whitcroft			}
22200a920b5bSAndy Whitcroft		}
22210a920b5bSAndy Whitcroft
2222653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
2223653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
2224cf655043SAndy Whitcroft# in a known good container
2225b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2226b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2227d8aaf121SAndy Whitcroft			my $ln = $linenr;
2228d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
2229c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
2230c45dcabdSAndy Whitcroft			my $ctx = '';
2231653d4876SAndy Whitcroft
2232c45dcabdSAndy Whitcroft			my $args = defined($1);
2233de7d4f0eSAndy Whitcroft
2234c45dcabdSAndy Whitcroft			# Find the end of the macro and limit our statement
2235c45dcabdSAndy Whitcroft			# search to that.
2236c45dcabdSAndy Whitcroft			while ($cnt > 0 && defined $lines[$ln - 1] &&
2237c45dcabdSAndy Whitcroft				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2238c45dcabdSAndy Whitcroft			{
2239c45dcabdSAndy Whitcroft				$ctx .= $rawlines[$ln - 1] . "\n";
2240a3bb97a7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2241c45dcabdSAndy Whitcroft				$ln++;
2242de7d4f0eSAndy Whitcroft			}
2243c45dcabdSAndy Whitcroft			$ctx .= $rawlines[$ln - 1];
2244d8aaf121SAndy Whitcroft
2245c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
2246c45dcabdSAndy Whitcroft				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2247c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2248a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2249c45dcabdSAndy Whitcroft
2250c45dcabdSAndy Whitcroft			# Extract the remainder of the define (if any) and
2251c45dcabdSAndy Whitcroft			# rip off surrounding spaces, and trailing \'s.
2252c45dcabdSAndy Whitcroft			$rest = '';
2253636d140aSAndy Whitcroft			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2254636d140aSAndy Whitcroft				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2255a3bb97a7SAndy Whitcroft				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2256c45dcabdSAndy Whitcroft					$rest .= substr($lines[$ln - 1], $off) . "\n";
2257c45dcabdSAndy Whitcroft					$cnt--;
2258a3bb97a7SAndy Whitcroft				}
2259a3bb97a7SAndy Whitcroft				$ln++;
2260c45dcabdSAndy Whitcroft				$off = 0;
2261c45dcabdSAndy Whitcroft			}
2262c45dcabdSAndy Whitcroft			$rest =~ s/\\\n.//g;
2263c45dcabdSAndy Whitcroft			$rest =~ s/^\s*//s;
2264c45dcabdSAndy Whitcroft			$rest =~ s/\s*$//s;
2265c45dcabdSAndy Whitcroft
2266c45dcabdSAndy Whitcroft			# Clean up the original statement.
2267c45dcabdSAndy Whitcroft			if ($args) {
2268c45dcabdSAndy Whitcroft				substr($dstat, 0, length($dcond), '');
2269d8aaf121SAndy Whitcroft			} else {
2270c45dcabdSAndy Whitcroft				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2271c45dcabdSAndy Whitcroft			}
2272292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
2273c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
2274c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
2275c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
2276c45dcabdSAndy Whitcroft
2277c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
2278bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2279bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2280bf30d6edSAndy Whitcroft			       $dstat =~ s/\[[^\{\}]*\]/1/)
2281bf30d6edSAndy Whitcroft			{
2282c45dcabdSAndy Whitcroft			}
2283c45dcabdSAndy Whitcroft
2284c45dcabdSAndy Whitcroft			my $exceptions = qr{
2285c45dcabdSAndy Whitcroft				$Declare|
2286c45dcabdSAndy Whitcroft				module_param_named|
2287c45dcabdSAndy Whitcroft				MODULE_PARAM_DESC|
2288c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
2289c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
2290383099fdSAndy Whitcroft				__typeof__\(|
2291*ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
2292*ea71a0a0SAndy Whitcroft				^\"|\"$
2293c45dcabdSAndy Whitcroft			}x;
2294383099fdSAndy Whitcroft			#print "REST<$rest> dstat<$dstat>\n";
2295c45dcabdSAndy Whitcroft			if ($rest ne '') {
2296c45dcabdSAndy Whitcroft				if ($rest !~ /while\s*\(/ &&
2297c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/)
2298c45dcabdSAndy Whitcroft				{
2299c45dcabdSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2300c45dcabdSAndy Whitcroft				}
2301c45dcabdSAndy Whitcroft
2302c45dcabdSAndy Whitcroft			} elsif ($ctx !~ /;/) {
2303c45dcabdSAndy Whitcroft				if ($dstat ne '' &&
2304c45dcabdSAndy Whitcroft				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2305c45dcabdSAndy Whitcroft				    $dstat !~ /$exceptions/ &&
2306b132e5d5SAndy Whitcroft				    $dstat !~ /^\.$Ident\s*=/ &&
2307c45dcabdSAndy Whitcroft				    $dstat =~ /$Operators/)
2308c45dcabdSAndy Whitcroft				{
2309de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2310d8aaf121SAndy Whitcroft				}
23110a920b5bSAndy Whitcroft			}
2312653d4876SAndy Whitcroft		}
23130a920b5bSAndy Whitcroft
2314080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2315080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
2316080ba929SMike Frysinger#	.
2317080ba929SMike Frysinger#	ALIGN(...)
2318080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
2319080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2320080ba929SMike Frysinger			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2321080ba929SMike Frysinger		}
2322080ba929SMike Frysinger
2323f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
232413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
232513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
2326cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
232713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2328cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2329cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
233013214adfSAndy Whitcroft				my $allowed = 0;
233113214adfSAndy Whitcroft				my $seen = 0;
2332773647a0SAndy Whitcroft				my $herectx = $here . "\n";
2333cf655043SAndy Whitcroft				my $ln = $linenr - 1;
233413214adfSAndy Whitcroft				for my $chunk (@chunks) {
233513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
233613214adfSAndy Whitcroft
2337773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
2338773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2339773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
2340773647a0SAndy Whitcroft
2341773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2342773647a0SAndy Whitcroft
2343773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
2344773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
2345773647a0SAndy Whitcroft
2346773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2347cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
2348cf655043SAndy Whitcroft
2349773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
235013214adfSAndy Whitcroft
235113214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
235213214adfSAndy Whitcroft
2353cf655043SAndy Whitcroft					#print "cond<$cond> block<$block> allowed<$allowed>\n";
2354cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
2355cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
235613214adfSAndy Whitcroft						$allowed = 1;
235713214adfSAndy Whitcroft					}
235813214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
2359cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
236013214adfSAndy Whitcroft						$allowed = 1;
236113214adfSAndy Whitcroft					}
2362cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
2363cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
236413214adfSAndy Whitcroft						$allowed = 1;
236513214adfSAndy Whitcroft					}
236613214adfSAndy Whitcroft				}
236713214adfSAndy Whitcroft				if ($seen && !$allowed) {
2368cf655043SAndy Whitcroft					WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
236913214adfSAndy Whitcroft				}
237013214adfSAndy Whitcroft			}
237113214adfSAndy Whitcroft		}
2372773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
237313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
2374cf655043SAndy Whitcroft			my $allowed = 0;
2375f0a594c1SAndy Whitcroft
2376cf655043SAndy Whitcroft			# Check the pre-context.
2377cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2378cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
2379cf655043SAndy Whitcroft				$allowed = 1;
2380f0a594c1SAndy Whitcroft			}
2381773647a0SAndy Whitcroft
2382773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
2383773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
2384773647a0SAndy Whitcroft
2385cf655043SAndy Whitcroft			# Check the condition.
2386cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
2387773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2388cf655043SAndy Whitcroft			if (defined $cond) {
2389773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
2390cf655043SAndy Whitcroft			}
2391cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
2392cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
2393cf655043SAndy Whitcroft				$allowed = 1;
2394cf655043SAndy Whitcroft			}
2395cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
2396cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
2397cf655043SAndy Whitcroft				$allowed = 1;
2398cf655043SAndy Whitcroft			}
2399cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
2400cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
2401cf655043SAndy Whitcroft				$allowed = 1;
2402cf655043SAndy Whitcroft			}
2403cf655043SAndy Whitcroft			# Check the post-context.
2404cf655043SAndy Whitcroft			if (defined $chunks[1]) {
2405cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
2406cf655043SAndy Whitcroft				if (defined $cond) {
2407773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
2408cf655043SAndy Whitcroft				}
2409cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
2410cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
2411cf655043SAndy Whitcroft					$allowed = 1;
2412cf655043SAndy Whitcroft				}
2413cf655043SAndy Whitcroft			}
2414cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2415cf655043SAndy Whitcroft				my $herectx = $here . "\n";;
2416f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
2417cf655043SAndy Whitcroft
2418f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
2419f055663cSAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";;
2420cf655043SAndy Whitcroft				}
2421cf655043SAndy Whitcroft
2422cf655043SAndy Whitcroft				WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2423f0a594c1SAndy Whitcroft			}
2424f0a594c1SAndy Whitcroft		}
2425f0a594c1SAndy Whitcroft
2426653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
24274a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
2428c45dcabdSAndy Whitcroft			if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2429de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24300a920b5bSAndy Whitcroft			}
24310a920b5bSAndy Whitcroft		}
24320a920b5bSAndy Whitcroft
24334a0df2efSAndy Whitcroft# don't use deprecated functions
24344a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
243500df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
2436de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
24374a0df2efSAndy Whitcroft			}
24384a0df2efSAndy Whitcroft		}
24394a0df2efSAndy Whitcroft
24404a0df2efSAndy Whitcroft# no volatiles please
24416c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
24426c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2443de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
24444a0df2efSAndy Whitcroft		}
24454a0df2efSAndy Whitcroft
24469c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
24479c0ca6f9SAndy Whitcroft		if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
24489c0ca6f9SAndy Whitcroft			ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
24499c0ca6f9SAndy Whitcroft		}
24509c0ca6f9SAndy Whitcroft
245100df344fSAndy Whitcroft# warn about #if 0
2452c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2453de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
2454de7d4f0eSAndy Whitcroft				$herecurr);
24554a0df2efSAndy Whitcroft		}
24564a0df2efSAndy Whitcroft
2457f0a594c1SAndy Whitcroft# check for needless kfree() checks
2458f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2459f0a594c1SAndy Whitcroft			my $expr = $1;
2460f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
24613c232147SWolfram Sang				WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2462f0a594c1SAndy Whitcroft			}
2463f0a594c1SAndy Whitcroft		}
24644c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks
24654c432a8fSGreg Kroah-Hartman		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
24664c432a8fSGreg Kroah-Hartman			my $expr = $1;
24674c432a8fSGreg Kroah-Hartman			if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
24684c432a8fSGreg Kroah-Hartman				WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
24694c432a8fSGreg Kroah-Hartman			}
24704c432a8fSGreg Kroah-Hartman		}
2471f0a594c1SAndy Whitcroft
247200df344fSAndy Whitcroft# warn about #ifdefs in C files
2473c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
247400df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
247500df344fSAndy Whitcroft#			print "$herecurr";
247600df344fSAndy Whitcroft#			$clean = 0;
247700df344fSAndy Whitcroft#		}
247800df344fSAndy Whitcroft
247922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
2480c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
248122f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
248222f2a2efSAndy Whitcroft		}
248322f2a2efSAndy Whitcroft
24844a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
2485171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2486171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
24874a0df2efSAndy Whitcroft			my $which = $1;
24884a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2489de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
24904a0df2efSAndy Whitcroft			}
24914a0df2efSAndy Whitcroft		}
24924a0df2efSAndy Whitcroft# check for memory barriers without a comment.
24934a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
24944a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
2495de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
24964a0df2efSAndy Whitcroft			}
24974a0df2efSAndy Whitcroft		}
24984a0df2efSAndy Whitcroft# check of hardware specific defines
2499c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2500de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
25010a920b5bSAndy Whitcroft		}
2502653d4876SAndy Whitcroft
2503de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
2504de7d4f0eSAndy Whitcroft# storage class and type.
25059c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
25069c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
2507de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2508de7d4f0eSAndy Whitcroft		}
2509de7d4f0eSAndy Whitcroft
25108905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
25118905a67cSAndy Whitcroft		if ($line =~ /\b(__inline__|__inline)\b/) {
25128905a67cSAndy Whitcroft			WARN("plain inline is preferred over $1\n" . $herecurr);
25138905a67cSAndy Whitcroft		}
25148905a67cSAndy Whitcroft
2515de7d4f0eSAndy Whitcroft# check for new externs in .c files.
2516171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
2517c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2518171ae1a4SAndy Whitcroft		{
2519c45dcabdSAndy Whitcroft			my $function_name = $1;
2520c45dcabdSAndy Whitcroft			my $paren_space = $2;
2521171ae1a4SAndy Whitcroft
2522171ae1a4SAndy Whitcroft			my $s = $stat;
2523171ae1a4SAndy Whitcroft			if (defined $cond) {
2524171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
2525171ae1a4SAndy Whitcroft			}
2526c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
2527c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
2528c45dcabdSAndy Whitcroft			{
2529de7d4f0eSAndy Whitcroft				WARN("externs should be avoided in .c files\n" .  $herecurr);
2530de7d4f0eSAndy Whitcroft			}
2531de7d4f0eSAndy Whitcroft
2532171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
2533171ae1a4SAndy Whitcroft				WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2534171ae1a4SAndy Whitcroft			}
25359c9ba34eSAndy Whitcroft
25369c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
25379c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
25389c9ba34eSAndy Whitcroft		{
25399c9ba34eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
2540171ae1a4SAndy Whitcroft		}
2541171ae1a4SAndy Whitcroft
2542de7d4f0eSAndy Whitcroft# checks for new __setup's
2543de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
2544de7d4f0eSAndy Whitcroft			my $name = $1;
2545de7d4f0eSAndy Whitcroft
2546de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
2547de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2548de7d4f0eSAndy Whitcroft			}
2549653d4876SAndy Whitcroft		}
25509c0ca6f9SAndy Whitcroft
25519c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
25529c0ca6f9SAndy Whitcroft		if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
25539c0ca6f9SAndy Whitcroft			WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
25549c0ca6f9SAndy Whitcroft		}
255513214adfSAndy Whitcroft
255613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
255713214adfSAndy Whitcroft		if ($line =~ /__FUNCTION__/) {
255813214adfSAndy Whitcroft			WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
255913214adfSAndy Whitcroft		}
2560773647a0SAndy Whitcroft
2561773647a0SAndy Whitcroft# check for semaphores used as mutexes
2562171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2563773647a0SAndy Whitcroft			WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2564773647a0SAndy Whitcroft		}
2565773647a0SAndy Whitcroft# check for semaphores used as mutexes
2566171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2567773647a0SAndy Whitcroft			WARN("consider using a completion\n" . $herecurr);
2568773647a0SAndy Whitcroft		}
2569773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto*
2570773647a0SAndy Whitcroft		if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2571773647a0SAndy Whitcroft			WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2572773647a0SAndy Whitcroft		}
2573f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please
2574f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
2575f3db6639SMichael Ellerman			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2576f3db6639SMichael Ellerman		}
25772b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const.
25786903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
25796903ffb2SAndy Whitcroft		    $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) {
25806903ffb2SAndy Whitcroft			WARN("struct $1 should normally be const\n" .
25816903ffb2SAndy Whitcroft				$herecurr);
25822b6db5cbSAndy Whitcroft		}
2583773647a0SAndy Whitcroft
2584773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
2585773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
2586773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
2587c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2588c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2589171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2590171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2591171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2592773647a0SAndy Whitcroft		{
2593773647a0SAndy Whitcroft			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2594773647a0SAndy Whitcroft		}
25959c9ba34eSAndy Whitcroft
25969c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings
25979c9ba34eSAndy Whitcroft		my $string;
25989c9ba34eSAndy Whitcroft		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
25999c9ba34eSAndy Whitcroft			$string = substr($rawline, $-[1], $+[1] - $-[1]);
26002a1bc5d5SAndy Whitcroft			$string =~ s/%%/__/g;
26019c9ba34eSAndy Whitcroft			if ($string =~ /(?<!%)%L[udi]/) {
26029c9ba34eSAndy Whitcroft				WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
26039c9ba34eSAndy Whitcroft				last;
26049c9ba34eSAndy Whitcroft			}
26059c9ba34eSAndy Whitcroft		}
2606691d77b6SAndy Whitcroft
2607691d77b6SAndy Whitcroft# whine mightly about in_atomic
2608691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
2609691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
2610691d77b6SAndy Whitcroft				ERROR("do not use in_atomic in drivers\n" . $herecurr);
2611f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
2612691d77b6SAndy Whitcroft				WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2613691d77b6SAndy Whitcroft			}
2614691d77b6SAndy Whitcroft		}
261513214adfSAndy Whitcroft	}
261613214adfSAndy Whitcroft
261713214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
261813214adfSAndy Whitcroft	# so just keep quiet.
261913214adfSAndy Whitcroft	if ($#rawlines == -1) {
262013214adfSAndy Whitcroft		exit(0);
26210a920b5bSAndy Whitcroft	}
26220a920b5bSAndy Whitcroft
26238905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
26248905a67cSAndy Whitcroft	# things that appear to be patches.
26258905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
26268905a67cSAndy Whitcroft		exit(0);
26278905a67cSAndy Whitcroft	}
26288905a67cSAndy Whitcroft
26298905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
26308905a67cSAndy Whitcroft	# just keep quiet.
26318905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
26328905a67cSAndy Whitcroft		exit(0);
26338905a67cSAndy Whitcroft	}
26348905a67cSAndy Whitcroft
26358905a67cSAndy Whitcroft	if (!$is_patch) {
2636de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
26370a920b5bSAndy Whitcroft	}
26380a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
2639de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
26400a920b5bSAndy Whitcroft	}
26410a920b5bSAndy Whitcroft
2642f0a594c1SAndy Whitcroft	print report_dump();
264313214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
264413214adfSAndy Whitcroft		print "$filename " if ($summary_file);
26456c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
26466c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
26476c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
26488905a67cSAndy Whitcroft		print "\n" if ($quiet == 0);
26496c72ffaaSAndy Whitcroft	}
26508905a67cSAndy Whitcroft
26510a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
2652c2fdda0dSAndy Whitcroft		print "$vname has no obvious style problems and is ready for submission.\n"
26530a920b5bSAndy Whitcroft	}
26540a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
2655c2fdda0dSAndy Whitcroft		print "$vname has style problems, please review.  If any of these errors\n";
26560a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
26570a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
26580a920b5bSAndy Whitcroft	}
265913214adfSAndy Whitcroft
26600a920b5bSAndy Whitcroft	return $clean;
26610a920b5bSAndy Whitcroft}
2662